1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Daniel Hartmeier 5 * Copyright (c) 2002,2003 Henning Brauer 6 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * - Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * - Redistributions in binary form must reproduce the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided 18 * with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Effort sponsored in part by the Defense Advanced Research Projects 34 * Agency (DARPA) and Air Force Research Laboratory, Air Force 35 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 36 * 37 * $OpenBSD: pf_ioctl.c,v 1.213 2009/02/15 21:46:12 mbalmer Exp $ 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include "opt_inet.h" 44 #include "opt_inet6.h" 45 #include "opt_bpf.h" 46 #include "opt_pf.h" 47 48 #include <sys/param.h> 49 #include <sys/_bitset.h> 50 #include <sys/bitset.h> 51 #include <sys/bus.h> 52 #include <sys/conf.h> 53 #include <sys/endian.h> 54 #include <sys/fcntl.h> 55 #include <sys/filio.h> 56 #include <sys/hash.h> 57 #include <sys/interrupt.h> 58 #include <sys/jail.h> 59 #include <sys/kernel.h> 60 #include <sys/kthread.h> 61 #include <sys/lock.h> 62 #include <sys/mbuf.h> 63 #include <sys/module.h> 64 #include <sys/proc.h> 65 #include <sys/smp.h> 66 #include <sys/socket.h> 67 #include <sys/sysctl.h> 68 #include <sys/md5.h> 69 #include <sys/ucred.h> 70 71 #include <net/if.h> 72 #include <net/if_var.h> 73 #include <net/vnet.h> 74 #include <net/route.h> 75 #include <net/pfil.h> 76 #include <net/pfvar.h> 77 #include <net/if_pfsync.h> 78 #include <net/if_pflog.h> 79 80 #include <netinet/in.h> 81 #include <netinet/ip.h> 82 #include <netinet/ip_var.h> 83 #include <netinet6/ip6_var.h> 84 #include <netinet/ip_icmp.h> 85 86 #ifdef INET6 87 #include <netinet/ip6.h> 88 #endif /* INET6 */ 89 90 #ifdef ALTQ 91 #include <net/altq/altq.h> 92 #endif 93 94 static struct pf_kpool *pf_get_kpool(char *, u_int32_t, u_int8_t, u_int32_t, 95 u_int8_t, u_int8_t, u_int8_t); 96 97 static void pf_mv_kpool(struct pf_kpalist *, struct pf_kpalist *); 98 static void pf_empty_kpool(struct pf_kpalist *); 99 static int pfioctl(struct cdev *, u_long, caddr_t, int, 100 struct thread *); 101 #ifdef ALTQ 102 static int pf_begin_altq(u_int32_t *); 103 static int pf_rollback_altq(u_int32_t); 104 static int pf_commit_altq(u_int32_t); 105 static int pf_enable_altq(struct pf_altq *); 106 static int pf_disable_altq(struct pf_altq *); 107 static u_int32_t pf_qname2qid(char *); 108 static void pf_qid_unref(u_int32_t); 109 #endif /* ALTQ */ 110 static int pf_begin_rules(u_int32_t *, int, const char *); 111 static int pf_rollback_rules(u_int32_t, int, char *); 112 static int pf_setup_pfsync_matching(struct pf_kruleset *); 113 static void pf_hash_rule(MD5_CTX *, struct pf_krule *); 114 static void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *); 115 static int pf_commit_rules(u_int32_t, int, char *); 116 static int pf_addr_setup(struct pf_kruleset *, 117 struct pf_addr_wrap *, sa_family_t); 118 static void pf_addr_copyout(struct pf_addr_wrap *); 119 static void pf_src_node_copy(const struct pf_ksrc_node *, 120 struct pf_src_node *); 121 #ifdef ALTQ 122 static int pf_export_kaltq(struct pf_altq *, 123 struct pfioc_altq_v1 *, size_t); 124 static int pf_import_kaltq(struct pfioc_altq_v1 *, 125 struct pf_altq *, size_t); 126 #endif /* ALTQ */ 127 128 VNET_DEFINE(struct pf_krule, pf_default_rule); 129 130 #ifdef ALTQ 131 VNET_DEFINE_STATIC(int, pf_altq_running); 132 #define V_pf_altq_running VNET(pf_altq_running) 133 #endif 134 135 #define TAGID_MAX 50000 136 struct pf_tagname { 137 TAILQ_ENTRY(pf_tagname) namehash_entries; 138 TAILQ_ENTRY(pf_tagname) taghash_entries; 139 char name[PF_TAG_NAME_SIZE]; 140 uint16_t tag; 141 int ref; 142 }; 143 144 struct pf_tagset { 145 TAILQ_HEAD(, pf_tagname) *namehash; 146 TAILQ_HEAD(, pf_tagname) *taghash; 147 unsigned int mask; 148 uint32_t seed; 149 BITSET_DEFINE(, TAGID_MAX) avail; 150 }; 151 152 VNET_DEFINE(struct pf_tagset, pf_tags); 153 #define V_pf_tags VNET(pf_tags) 154 static unsigned int pf_rule_tag_hashsize; 155 #define PF_RULE_TAG_HASH_SIZE_DEFAULT 128 156 SYSCTL_UINT(_net_pf, OID_AUTO, rule_tag_hashsize, CTLFLAG_RDTUN, 157 &pf_rule_tag_hashsize, PF_RULE_TAG_HASH_SIZE_DEFAULT, 158 "Size of pf(4) rule tag hashtable"); 159 160 #ifdef ALTQ 161 VNET_DEFINE(struct pf_tagset, pf_qids); 162 #define V_pf_qids VNET(pf_qids) 163 static unsigned int pf_queue_tag_hashsize; 164 #define PF_QUEUE_TAG_HASH_SIZE_DEFAULT 128 165 SYSCTL_UINT(_net_pf, OID_AUTO, queue_tag_hashsize, CTLFLAG_RDTUN, 166 &pf_queue_tag_hashsize, PF_QUEUE_TAG_HASH_SIZE_DEFAULT, 167 "Size of pf(4) queue tag hashtable"); 168 #endif 169 VNET_DEFINE(uma_zone_t, pf_tag_z); 170 #define V_pf_tag_z VNET(pf_tag_z) 171 static MALLOC_DEFINE(M_PFALTQ, "pf_altq", "pf(4) altq configuration db"); 172 static MALLOC_DEFINE(M_PFRULE, "pf_rule", "pf(4) rules"); 173 174 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE) 175 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE 176 #endif 177 178 static void pf_init_tagset(struct pf_tagset *, unsigned int *, 179 unsigned int); 180 static void pf_cleanup_tagset(struct pf_tagset *); 181 static uint16_t tagname2hashindex(const struct pf_tagset *, const char *); 182 static uint16_t tag2hashindex(const struct pf_tagset *, uint16_t); 183 static u_int16_t tagname2tag(struct pf_tagset *, char *); 184 static u_int16_t pf_tagname2tag(char *); 185 static void tag_unref(struct pf_tagset *, u_int16_t); 186 187 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x 188 189 struct cdev *pf_dev; 190 191 /* 192 * XXX - These are new and need to be checked when moveing to a new version 193 */ 194 static void pf_clear_states(void); 195 static int pf_clear_tables(void); 196 static void pf_clear_srcnodes(struct pf_ksrc_node *); 197 static void pf_kill_srcnodes(struct pfioc_src_node_kill *); 198 static void pf_tbladdr_copyout(struct pf_addr_wrap *); 199 200 /* 201 * Wrapper functions for pfil(9) hooks 202 */ 203 #ifdef INET 204 static pfil_return_t pf_check_in(struct mbuf **m, struct ifnet *ifp, 205 int flags, void *ruleset __unused, struct inpcb *inp); 206 static pfil_return_t pf_check_out(struct mbuf **m, struct ifnet *ifp, 207 int flags, void *ruleset __unused, struct inpcb *inp); 208 #endif 209 #ifdef INET6 210 static pfil_return_t pf_check6_in(struct mbuf **m, struct ifnet *ifp, 211 int flags, void *ruleset __unused, struct inpcb *inp); 212 static pfil_return_t pf_check6_out(struct mbuf **m, struct ifnet *ifp, 213 int flags, void *ruleset __unused, struct inpcb *inp); 214 #endif 215 216 static int hook_pf(void); 217 static int dehook_pf(void); 218 static int shutdown_pf(void); 219 static int pf_load(void); 220 static void pf_unload(void); 221 222 static struct cdevsw pf_cdevsw = { 223 .d_ioctl = pfioctl, 224 .d_name = PF_NAME, 225 .d_version = D_VERSION, 226 }; 227 228 volatile VNET_DEFINE_STATIC(int, pf_pfil_hooked); 229 #define V_pf_pfil_hooked VNET(pf_pfil_hooked) 230 231 /* 232 * We need a flag that is neither hooked nor running to know when 233 * the VNET is "valid". We primarily need this to control (global) 234 * external event, e.g., eventhandlers. 235 */ 236 VNET_DEFINE(int, pf_vnet_active); 237 #define V_pf_vnet_active VNET(pf_vnet_active) 238 239 int pf_end_threads; 240 struct proc *pf_purge_proc; 241 242 struct rmlock pf_rules_lock; 243 struct sx pf_ioctl_lock; 244 struct sx pf_end_lock; 245 246 /* pfsync */ 247 VNET_DEFINE(pfsync_state_import_t *, pfsync_state_import_ptr); 248 VNET_DEFINE(pfsync_insert_state_t *, pfsync_insert_state_ptr); 249 VNET_DEFINE(pfsync_update_state_t *, pfsync_update_state_ptr); 250 VNET_DEFINE(pfsync_delete_state_t *, pfsync_delete_state_ptr); 251 VNET_DEFINE(pfsync_clear_states_t *, pfsync_clear_states_ptr); 252 VNET_DEFINE(pfsync_defer_t *, pfsync_defer_ptr); 253 pfsync_detach_ifnet_t *pfsync_detach_ifnet_ptr; 254 255 /* pflog */ 256 pflog_packet_t *pflog_packet_ptr = NULL; 257 258 extern u_long pf_ioctl_maxcount; 259 260 static void 261 pfattach_vnet(void) 262 { 263 u_int32_t *my_timeout = V_pf_default_rule.timeout; 264 265 pf_initialize(); 266 pfr_initialize(); 267 pfi_initialize_vnet(); 268 pf_normalize_init(); 269 270 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT; 271 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT; 272 273 RB_INIT(&V_pf_anchors); 274 pf_init_kruleset(&pf_main_ruleset); 275 276 /* default rule should never be garbage collected */ 277 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next; 278 #ifdef PF_DEFAULT_TO_DROP 279 V_pf_default_rule.action = PF_DROP; 280 #else 281 V_pf_default_rule.action = PF_PASS; 282 #endif 283 V_pf_default_rule.nr = -1; 284 V_pf_default_rule.rtableid = -1; 285 286 V_pf_default_rule.evaluations = counter_u64_alloc(M_WAITOK); 287 for (int i = 0; i < 2; i++) { 288 V_pf_default_rule.packets[i] = counter_u64_alloc(M_WAITOK); 289 V_pf_default_rule.bytes[i] = counter_u64_alloc(M_WAITOK); 290 } 291 V_pf_default_rule.states_cur = counter_u64_alloc(M_WAITOK); 292 V_pf_default_rule.states_tot = counter_u64_alloc(M_WAITOK); 293 V_pf_default_rule.src_nodes = counter_u64_alloc(M_WAITOK); 294 295 /* initialize default timeouts */ 296 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL; 297 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL; 298 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL; 299 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL; 300 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL; 301 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL; 302 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL; 303 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL; 304 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL; 305 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL; 306 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL; 307 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL; 308 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL; 309 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL; 310 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL; 311 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL; 312 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL; 313 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL; 314 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START; 315 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END; 316 317 bzero(&V_pf_status, sizeof(V_pf_status)); 318 V_pf_status.debug = PF_DEBUG_URGENT; 319 320 V_pf_pfil_hooked = 0; 321 322 /* XXX do our best to avoid a conflict */ 323 V_pf_status.hostid = arc4random(); 324 325 for (int i = 0; i < PFRES_MAX; i++) 326 V_pf_status.counters[i] = counter_u64_alloc(M_WAITOK); 327 for (int i = 0; i < LCNT_MAX; i++) 328 V_pf_status.lcounters[i] = counter_u64_alloc(M_WAITOK); 329 for (int i = 0; i < FCNT_MAX; i++) 330 V_pf_status.fcounters[i] = counter_u64_alloc(M_WAITOK); 331 for (int i = 0; i < SCNT_MAX; i++) 332 V_pf_status.scounters[i] = counter_u64_alloc(M_WAITOK); 333 334 if (swi_add(NULL, "pf send", pf_intr, curvnet, SWI_NET, 335 INTR_MPSAFE, &V_pf_swi_cookie) != 0) 336 /* XXXGL: leaked all above. */ 337 return; 338 } 339 340 static struct pf_kpool * 341 pf_get_kpool(char *anchor, u_int32_t ticket, u_int8_t rule_action, 342 u_int32_t rule_number, u_int8_t r_last, u_int8_t active, 343 u_int8_t check_ticket) 344 { 345 struct pf_kruleset *ruleset; 346 struct pf_krule *rule; 347 int rs_num; 348 349 ruleset = pf_find_kruleset(anchor); 350 if (ruleset == NULL) 351 return (NULL); 352 rs_num = pf_get_ruleset_number(rule_action); 353 if (rs_num >= PF_RULESET_MAX) 354 return (NULL); 355 if (active) { 356 if (check_ticket && ticket != 357 ruleset->rules[rs_num].active.ticket) 358 return (NULL); 359 if (r_last) 360 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr, 361 pf_krulequeue); 362 else 363 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr); 364 } else { 365 if (check_ticket && ticket != 366 ruleset->rules[rs_num].inactive.ticket) 367 return (NULL); 368 if (r_last) 369 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr, 370 pf_krulequeue); 371 else 372 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr); 373 } 374 if (!r_last) { 375 while ((rule != NULL) && (rule->nr != rule_number)) 376 rule = TAILQ_NEXT(rule, entries); 377 } 378 if (rule == NULL) 379 return (NULL); 380 381 return (&rule->rpool); 382 } 383 384 static void 385 pf_mv_kpool(struct pf_kpalist *poola, struct pf_kpalist *poolb) 386 { 387 struct pf_kpooladdr *mv_pool_pa; 388 389 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) { 390 TAILQ_REMOVE(poola, mv_pool_pa, entries); 391 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries); 392 } 393 } 394 395 static void 396 pf_empty_kpool(struct pf_kpalist *poola) 397 { 398 struct pf_kpooladdr *pa; 399 400 while ((pa = TAILQ_FIRST(poola)) != NULL) { 401 switch (pa->addr.type) { 402 case PF_ADDR_DYNIFTL: 403 pfi_dynaddr_remove(pa->addr.p.dyn); 404 break; 405 case PF_ADDR_TABLE: 406 /* XXX: this could be unfinished pooladdr on pabuf */ 407 if (pa->addr.p.tbl != NULL) 408 pfr_detach_table(pa->addr.p.tbl); 409 break; 410 } 411 if (pa->kif) 412 pfi_kkif_unref(pa->kif); 413 TAILQ_REMOVE(poola, pa, entries); 414 free(pa, M_PFRULE); 415 } 416 } 417 418 static void 419 pf_unlink_rule(struct pf_krulequeue *rulequeue, struct pf_krule *rule) 420 { 421 422 PF_RULES_WASSERT(); 423 424 TAILQ_REMOVE(rulequeue, rule, entries); 425 426 PF_UNLNKDRULES_LOCK(); 427 rule->rule_flag |= PFRULE_REFS; 428 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries); 429 PF_UNLNKDRULES_UNLOCK(); 430 } 431 432 void 433 pf_free_rule(struct pf_krule *rule) 434 { 435 436 PF_RULES_WASSERT(); 437 438 if (rule->tag) 439 tag_unref(&V_pf_tags, rule->tag); 440 if (rule->match_tag) 441 tag_unref(&V_pf_tags, rule->match_tag); 442 #ifdef ALTQ 443 if (rule->pqid != rule->qid) 444 pf_qid_unref(rule->pqid); 445 pf_qid_unref(rule->qid); 446 #endif 447 switch (rule->src.addr.type) { 448 case PF_ADDR_DYNIFTL: 449 pfi_dynaddr_remove(rule->src.addr.p.dyn); 450 break; 451 case PF_ADDR_TABLE: 452 pfr_detach_table(rule->src.addr.p.tbl); 453 break; 454 } 455 switch (rule->dst.addr.type) { 456 case PF_ADDR_DYNIFTL: 457 pfi_dynaddr_remove(rule->dst.addr.p.dyn); 458 break; 459 case PF_ADDR_TABLE: 460 pfr_detach_table(rule->dst.addr.p.tbl); 461 break; 462 } 463 if (rule->overload_tbl) 464 pfr_detach_table(rule->overload_tbl); 465 if (rule->kif) 466 pfi_kkif_unref(rule->kif); 467 pf_kanchor_remove(rule); 468 pf_empty_kpool(&rule->rpool.list); 469 counter_u64_free(rule->evaluations); 470 for (int i = 0; i < 2; i++) { 471 counter_u64_free(rule->packets[i]); 472 counter_u64_free(rule->bytes[i]); 473 } 474 counter_u64_free(rule->states_cur); 475 counter_u64_free(rule->states_tot); 476 counter_u64_free(rule->src_nodes); 477 free(rule, M_PFRULE); 478 } 479 480 static void 481 pf_init_tagset(struct pf_tagset *ts, unsigned int *tunable_size, 482 unsigned int default_size) 483 { 484 unsigned int i; 485 unsigned int hashsize; 486 487 if (*tunable_size == 0 || !powerof2(*tunable_size)) 488 *tunable_size = default_size; 489 490 hashsize = *tunable_size; 491 ts->namehash = mallocarray(hashsize, sizeof(*ts->namehash), M_PFHASH, 492 M_WAITOK); 493 ts->taghash = mallocarray(hashsize, sizeof(*ts->taghash), M_PFHASH, 494 M_WAITOK); 495 ts->mask = hashsize - 1; 496 ts->seed = arc4random(); 497 for (i = 0; i < hashsize; i++) { 498 TAILQ_INIT(&ts->namehash[i]); 499 TAILQ_INIT(&ts->taghash[i]); 500 } 501 BIT_FILL(TAGID_MAX, &ts->avail); 502 } 503 504 static void 505 pf_cleanup_tagset(struct pf_tagset *ts) 506 { 507 unsigned int i; 508 unsigned int hashsize; 509 struct pf_tagname *t, *tmp; 510 511 /* 512 * Only need to clean up one of the hashes as each tag is hashed 513 * into each table. 514 */ 515 hashsize = ts->mask + 1; 516 for (i = 0; i < hashsize; i++) 517 TAILQ_FOREACH_SAFE(t, &ts->namehash[i], namehash_entries, tmp) 518 uma_zfree(V_pf_tag_z, t); 519 520 free(ts->namehash, M_PFHASH); 521 free(ts->taghash, M_PFHASH); 522 } 523 524 static uint16_t 525 tagname2hashindex(const struct pf_tagset *ts, const char *tagname) 526 { 527 size_t len; 528 529 len = strnlen(tagname, PF_TAG_NAME_SIZE - 1); 530 return (murmur3_32_hash(tagname, len, ts->seed) & ts->mask); 531 } 532 533 static uint16_t 534 tag2hashindex(const struct pf_tagset *ts, uint16_t tag) 535 { 536 537 return (tag & ts->mask); 538 } 539 540 static u_int16_t 541 tagname2tag(struct pf_tagset *ts, char *tagname) 542 { 543 struct pf_tagname *tag; 544 u_int32_t index; 545 u_int16_t new_tagid; 546 547 PF_RULES_WASSERT(); 548 549 index = tagname2hashindex(ts, tagname); 550 TAILQ_FOREACH(tag, &ts->namehash[index], namehash_entries) 551 if (strcmp(tagname, tag->name) == 0) { 552 tag->ref++; 553 return (tag->tag); 554 } 555 556 /* 557 * new entry 558 * 559 * to avoid fragmentation, we do a linear search from the beginning 560 * and take the first free slot we find. 561 */ 562 new_tagid = BIT_FFS(TAGID_MAX, &ts->avail); 563 /* 564 * Tags are 1-based, with valid tags in the range [1..TAGID_MAX]. 565 * BIT_FFS() returns a 1-based bit number, with 0 indicating no bits 566 * set. It may also return a bit number greater than TAGID_MAX due 567 * to rounding of the number of bits in the vector up to a multiple 568 * of the vector word size at declaration/allocation time. 569 */ 570 if ((new_tagid == 0) || (new_tagid > TAGID_MAX)) 571 return (0); 572 573 /* Mark the tag as in use. Bits are 0-based for BIT_CLR() */ 574 BIT_CLR(TAGID_MAX, new_tagid - 1, &ts->avail); 575 576 /* allocate and fill new struct pf_tagname */ 577 tag = uma_zalloc(V_pf_tag_z, M_NOWAIT); 578 if (tag == NULL) 579 return (0); 580 strlcpy(tag->name, tagname, sizeof(tag->name)); 581 tag->tag = new_tagid; 582 tag->ref = 1; 583 584 /* Insert into namehash */ 585 TAILQ_INSERT_TAIL(&ts->namehash[index], tag, namehash_entries); 586 587 /* Insert into taghash */ 588 index = tag2hashindex(ts, new_tagid); 589 TAILQ_INSERT_TAIL(&ts->taghash[index], tag, taghash_entries); 590 591 return (tag->tag); 592 } 593 594 static void 595 tag_unref(struct pf_tagset *ts, u_int16_t tag) 596 { 597 struct pf_tagname *t; 598 uint16_t index; 599 600 PF_RULES_WASSERT(); 601 602 index = tag2hashindex(ts, tag); 603 TAILQ_FOREACH(t, &ts->taghash[index], taghash_entries) 604 if (tag == t->tag) { 605 if (--t->ref == 0) { 606 TAILQ_REMOVE(&ts->taghash[index], t, 607 taghash_entries); 608 index = tagname2hashindex(ts, t->name); 609 TAILQ_REMOVE(&ts->namehash[index], t, 610 namehash_entries); 611 /* Bits are 0-based for BIT_SET() */ 612 BIT_SET(TAGID_MAX, tag - 1, &ts->avail); 613 uma_zfree(V_pf_tag_z, t); 614 } 615 break; 616 } 617 } 618 619 static u_int16_t 620 pf_tagname2tag(char *tagname) 621 { 622 return (tagname2tag(&V_pf_tags, tagname)); 623 } 624 625 #ifdef ALTQ 626 static u_int32_t 627 pf_qname2qid(char *qname) 628 { 629 return ((u_int32_t)tagname2tag(&V_pf_qids, qname)); 630 } 631 632 static void 633 pf_qid_unref(u_int32_t qid) 634 { 635 tag_unref(&V_pf_qids, (u_int16_t)qid); 636 } 637 638 static int 639 pf_begin_altq(u_int32_t *ticket) 640 { 641 struct pf_altq *altq, *tmp; 642 int error = 0; 643 644 PF_RULES_WASSERT(); 645 646 /* Purge the old altq lists */ 647 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) { 648 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 649 /* detach and destroy the discipline */ 650 error = altq_remove(altq); 651 } 652 free(altq, M_PFALTQ); 653 } 654 TAILQ_INIT(V_pf_altq_ifs_inactive); 655 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) { 656 pf_qid_unref(altq->qid); 657 free(altq, M_PFALTQ); 658 } 659 TAILQ_INIT(V_pf_altqs_inactive); 660 if (error) 661 return (error); 662 *ticket = ++V_ticket_altqs_inactive; 663 V_altqs_inactive_open = 1; 664 return (0); 665 } 666 667 static int 668 pf_rollback_altq(u_int32_t ticket) 669 { 670 struct pf_altq *altq, *tmp; 671 int error = 0; 672 673 PF_RULES_WASSERT(); 674 675 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive) 676 return (0); 677 /* Purge the old altq lists */ 678 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) { 679 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 680 /* detach and destroy the discipline */ 681 error = altq_remove(altq); 682 } 683 free(altq, M_PFALTQ); 684 } 685 TAILQ_INIT(V_pf_altq_ifs_inactive); 686 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) { 687 pf_qid_unref(altq->qid); 688 free(altq, M_PFALTQ); 689 } 690 TAILQ_INIT(V_pf_altqs_inactive); 691 V_altqs_inactive_open = 0; 692 return (error); 693 } 694 695 static int 696 pf_commit_altq(u_int32_t ticket) 697 { 698 struct pf_altqqueue *old_altqs, *old_altq_ifs; 699 struct pf_altq *altq, *tmp; 700 int err, error = 0; 701 702 PF_RULES_WASSERT(); 703 704 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive) 705 return (EBUSY); 706 707 /* swap altqs, keep the old. */ 708 old_altqs = V_pf_altqs_active; 709 old_altq_ifs = V_pf_altq_ifs_active; 710 V_pf_altqs_active = V_pf_altqs_inactive; 711 V_pf_altq_ifs_active = V_pf_altq_ifs_inactive; 712 V_pf_altqs_inactive = old_altqs; 713 V_pf_altq_ifs_inactive = old_altq_ifs; 714 V_ticket_altqs_active = V_ticket_altqs_inactive; 715 716 /* Attach new disciplines */ 717 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 718 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 719 /* attach the discipline */ 720 error = altq_pfattach(altq); 721 if (error == 0 && V_pf_altq_running) 722 error = pf_enable_altq(altq); 723 if (error != 0) 724 return (error); 725 } 726 } 727 728 /* Purge the old altq lists */ 729 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) { 730 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 731 /* detach and destroy the discipline */ 732 if (V_pf_altq_running) 733 error = pf_disable_altq(altq); 734 err = altq_pfdetach(altq); 735 if (err != 0 && error == 0) 736 error = err; 737 err = altq_remove(altq); 738 if (err != 0 && error == 0) 739 error = err; 740 } 741 free(altq, M_PFALTQ); 742 } 743 TAILQ_INIT(V_pf_altq_ifs_inactive); 744 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) { 745 pf_qid_unref(altq->qid); 746 free(altq, M_PFALTQ); 747 } 748 TAILQ_INIT(V_pf_altqs_inactive); 749 750 V_altqs_inactive_open = 0; 751 return (error); 752 } 753 754 static int 755 pf_enable_altq(struct pf_altq *altq) 756 { 757 struct ifnet *ifp; 758 struct tb_profile tb; 759 int error = 0; 760 761 if ((ifp = ifunit(altq->ifname)) == NULL) 762 return (EINVAL); 763 764 if (ifp->if_snd.altq_type != ALTQT_NONE) 765 error = altq_enable(&ifp->if_snd); 766 767 /* set tokenbucket regulator */ 768 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) { 769 tb.rate = altq->ifbandwidth; 770 tb.depth = altq->tbrsize; 771 error = tbr_set(&ifp->if_snd, &tb); 772 } 773 774 return (error); 775 } 776 777 static int 778 pf_disable_altq(struct pf_altq *altq) 779 { 780 struct ifnet *ifp; 781 struct tb_profile tb; 782 int error; 783 784 if ((ifp = ifunit(altq->ifname)) == NULL) 785 return (EINVAL); 786 787 /* 788 * when the discipline is no longer referenced, it was overridden 789 * by a new one. if so, just return. 790 */ 791 if (altq->altq_disc != ifp->if_snd.altq_disc) 792 return (0); 793 794 error = altq_disable(&ifp->if_snd); 795 796 if (error == 0) { 797 /* clear tokenbucket regulator */ 798 tb.rate = 0; 799 error = tbr_set(&ifp->if_snd, &tb); 800 } 801 802 return (error); 803 } 804 805 static int 806 pf_altq_ifnet_event_add(struct ifnet *ifp, int remove, u_int32_t ticket, 807 struct pf_altq *altq) 808 { 809 struct ifnet *ifp1; 810 int error = 0; 811 812 /* Deactivate the interface in question */ 813 altq->local_flags &= ~PFALTQ_FLAG_IF_REMOVED; 814 if ((ifp1 = ifunit(altq->ifname)) == NULL || 815 (remove && ifp1 == ifp)) { 816 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED; 817 } else { 818 error = altq_add(ifp1, altq); 819 820 if (ticket != V_ticket_altqs_inactive) 821 error = EBUSY; 822 823 if (error) 824 free(altq, M_PFALTQ); 825 } 826 827 return (error); 828 } 829 830 void 831 pf_altq_ifnet_event(struct ifnet *ifp, int remove) 832 { 833 struct pf_altq *a1, *a2, *a3; 834 u_int32_t ticket; 835 int error = 0; 836 837 /* 838 * No need to re-evaluate the configuration for events on interfaces 839 * that do not support ALTQ, as it's not possible for such 840 * interfaces to be part of the configuration. 841 */ 842 if (!ALTQ_IS_READY(&ifp->if_snd)) 843 return; 844 845 /* Interrupt userland queue modifications */ 846 if (V_altqs_inactive_open) 847 pf_rollback_altq(V_ticket_altqs_inactive); 848 849 /* Start new altq ruleset */ 850 if (pf_begin_altq(&ticket)) 851 return; 852 853 /* Copy the current active set */ 854 TAILQ_FOREACH(a1, V_pf_altq_ifs_active, entries) { 855 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT); 856 if (a2 == NULL) { 857 error = ENOMEM; 858 break; 859 } 860 bcopy(a1, a2, sizeof(struct pf_altq)); 861 862 error = pf_altq_ifnet_event_add(ifp, remove, ticket, a2); 863 if (error) 864 break; 865 866 TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, a2, entries); 867 } 868 if (error) 869 goto out; 870 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) { 871 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT); 872 if (a2 == NULL) { 873 error = ENOMEM; 874 break; 875 } 876 bcopy(a1, a2, sizeof(struct pf_altq)); 877 878 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) { 879 error = EBUSY; 880 free(a2, M_PFALTQ); 881 break; 882 } 883 a2->altq_disc = NULL; 884 TAILQ_FOREACH(a3, V_pf_altq_ifs_inactive, entries) { 885 if (strncmp(a3->ifname, a2->ifname, 886 IFNAMSIZ) == 0) { 887 a2->altq_disc = a3->altq_disc; 888 break; 889 } 890 } 891 error = pf_altq_ifnet_event_add(ifp, remove, ticket, a2); 892 if (error) 893 break; 894 895 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries); 896 } 897 898 out: 899 if (error != 0) 900 pf_rollback_altq(ticket); 901 else 902 pf_commit_altq(ticket); 903 } 904 #endif /* ALTQ */ 905 906 static int 907 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor) 908 { 909 struct pf_kruleset *rs; 910 struct pf_krule *rule; 911 912 PF_RULES_WASSERT(); 913 914 if (rs_num < 0 || rs_num >= PF_RULESET_MAX) 915 return (EINVAL); 916 rs = pf_find_or_create_kruleset(anchor); 917 if (rs == NULL) 918 return (EINVAL); 919 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) { 920 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule); 921 rs->rules[rs_num].inactive.rcount--; 922 } 923 *ticket = ++rs->rules[rs_num].inactive.ticket; 924 rs->rules[rs_num].inactive.open = 1; 925 return (0); 926 } 927 928 static int 929 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor) 930 { 931 struct pf_kruleset *rs; 932 struct pf_krule *rule; 933 934 PF_RULES_WASSERT(); 935 936 if (rs_num < 0 || rs_num >= PF_RULESET_MAX) 937 return (EINVAL); 938 rs = pf_find_kruleset(anchor); 939 if (rs == NULL || !rs->rules[rs_num].inactive.open || 940 rs->rules[rs_num].inactive.ticket != ticket) 941 return (0); 942 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) { 943 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule); 944 rs->rules[rs_num].inactive.rcount--; 945 } 946 rs->rules[rs_num].inactive.open = 0; 947 return (0); 948 } 949 950 #define PF_MD5_UPD(st, elm) \ 951 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm)) 952 953 #define PF_MD5_UPD_STR(st, elm) \ 954 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm)) 955 956 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \ 957 (stor) = htonl((st)->elm); \ 958 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\ 959 } while (0) 960 961 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \ 962 (stor) = htons((st)->elm); \ 963 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\ 964 } while (0) 965 966 static void 967 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr) 968 { 969 PF_MD5_UPD(pfr, addr.type); 970 switch (pfr->addr.type) { 971 case PF_ADDR_DYNIFTL: 972 PF_MD5_UPD(pfr, addr.v.ifname); 973 PF_MD5_UPD(pfr, addr.iflags); 974 break; 975 case PF_ADDR_TABLE: 976 PF_MD5_UPD(pfr, addr.v.tblname); 977 break; 978 case PF_ADDR_ADDRMASK: 979 /* XXX ignore af? */ 980 PF_MD5_UPD(pfr, addr.v.a.addr.addr32); 981 PF_MD5_UPD(pfr, addr.v.a.mask.addr32); 982 break; 983 } 984 985 PF_MD5_UPD(pfr, port[0]); 986 PF_MD5_UPD(pfr, port[1]); 987 PF_MD5_UPD(pfr, neg); 988 PF_MD5_UPD(pfr, port_op); 989 } 990 991 static void 992 pf_hash_rule(MD5_CTX *ctx, struct pf_krule *rule) 993 { 994 u_int16_t x; 995 u_int32_t y; 996 997 pf_hash_rule_addr(ctx, &rule->src); 998 pf_hash_rule_addr(ctx, &rule->dst); 999 PF_MD5_UPD_STR(rule, label); 1000 PF_MD5_UPD_STR(rule, ifname); 1001 PF_MD5_UPD_STR(rule, match_tagname); 1002 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */ 1003 PF_MD5_UPD_HTONL(rule, os_fingerprint, y); 1004 PF_MD5_UPD_HTONL(rule, prob, y); 1005 PF_MD5_UPD_HTONL(rule, uid.uid[0], y); 1006 PF_MD5_UPD_HTONL(rule, uid.uid[1], y); 1007 PF_MD5_UPD(rule, uid.op); 1008 PF_MD5_UPD_HTONL(rule, gid.gid[0], y); 1009 PF_MD5_UPD_HTONL(rule, gid.gid[1], y); 1010 PF_MD5_UPD(rule, gid.op); 1011 PF_MD5_UPD_HTONL(rule, rule_flag, y); 1012 PF_MD5_UPD(rule, action); 1013 PF_MD5_UPD(rule, direction); 1014 PF_MD5_UPD(rule, af); 1015 PF_MD5_UPD(rule, quick); 1016 PF_MD5_UPD(rule, ifnot); 1017 PF_MD5_UPD(rule, match_tag_not); 1018 PF_MD5_UPD(rule, natpass); 1019 PF_MD5_UPD(rule, keep_state); 1020 PF_MD5_UPD(rule, proto); 1021 PF_MD5_UPD(rule, type); 1022 PF_MD5_UPD(rule, code); 1023 PF_MD5_UPD(rule, flags); 1024 PF_MD5_UPD(rule, flagset); 1025 PF_MD5_UPD(rule, allow_opts); 1026 PF_MD5_UPD(rule, rt); 1027 PF_MD5_UPD(rule, tos); 1028 } 1029 1030 static int 1031 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor) 1032 { 1033 struct pf_kruleset *rs; 1034 struct pf_krule *rule, **old_array; 1035 struct pf_krulequeue *old_rules; 1036 int error; 1037 u_int32_t old_rcount; 1038 1039 PF_RULES_WASSERT(); 1040 1041 if (rs_num < 0 || rs_num >= PF_RULESET_MAX) 1042 return (EINVAL); 1043 rs = pf_find_kruleset(anchor); 1044 if (rs == NULL || !rs->rules[rs_num].inactive.open || 1045 ticket != rs->rules[rs_num].inactive.ticket) 1046 return (EBUSY); 1047 1048 /* Calculate checksum for the main ruleset */ 1049 if (rs == &pf_main_ruleset) { 1050 error = pf_setup_pfsync_matching(rs); 1051 if (error != 0) 1052 return (error); 1053 } 1054 1055 /* Swap rules, keep the old. */ 1056 old_rules = rs->rules[rs_num].active.ptr; 1057 old_rcount = rs->rules[rs_num].active.rcount; 1058 old_array = rs->rules[rs_num].active.ptr_array; 1059 1060 rs->rules[rs_num].active.ptr = 1061 rs->rules[rs_num].inactive.ptr; 1062 rs->rules[rs_num].active.ptr_array = 1063 rs->rules[rs_num].inactive.ptr_array; 1064 rs->rules[rs_num].active.rcount = 1065 rs->rules[rs_num].inactive.rcount; 1066 rs->rules[rs_num].inactive.ptr = old_rules; 1067 rs->rules[rs_num].inactive.ptr_array = old_array; 1068 rs->rules[rs_num].inactive.rcount = old_rcount; 1069 1070 rs->rules[rs_num].active.ticket = 1071 rs->rules[rs_num].inactive.ticket; 1072 pf_calc_skip_steps(rs->rules[rs_num].active.ptr); 1073 1074 /* Purge the old rule list. */ 1075 while ((rule = TAILQ_FIRST(old_rules)) != NULL) 1076 pf_unlink_rule(old_rules, rule); 1077 if (rs->rules[rs_num].inactive.ptr_array) 1078 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP); 1079 rs->rules[rs_num].inactive.ptr_array = NULL; 1080 rs->rules[rs_num].inactive.rcount = 0; 1081 rs->rules[rs_num].inactive.open = 0; 1082 pf_remove_if_empty_kruleset(rs); 1083 1084 return (0); 1085 } 1086 1087 static int 1088 pf_setup_pfsync_matching(struct pf_kruleset *rs) 1089 { 1090 MD5_CTX ctx; 1091 struct pf_krule *rule; 1092 int rs_cnt; 1093 u_int8_t digest[PF_MD5_DIGEST_LENGTH]; 1094 1095 MD5Init(&ctx); 1096 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) { 1097 /* XXX PF_RULESET_SCRUB as well? */ 1098 if (rs_cnt == PF_RULESET_SCRUB) 1099 continue; 1100 1101 if (rs->rules[rs_cnt].inactive.ptr_array) 1102 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP); 1103 rs->rules[rs_cnt].inactive.ptr_array = NULL; 1104 1105 if (rs->rules[rs_cnt].inactive.rcount) { 1106 rs->rules[rs_cnt].inactive.ptr_array = 1107 malloc(sizeof(caddr_t) * 1108 rs->rules[rs_cnt].inactive.rcount, 1109 M_TEMP, M_NOWAIT); 1110 1111 if (!rs->rules[rs_cnt].inactive.ptr_array) 1112 return (ENOMEM); 1113 } 1114 1115 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr, 1116 entries) { 1117 pf_hash_rule(&ctx, rule); 1118 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule; 1119 } 1120 } 1121 1122 MD5Final(digest, &ctx); 1123 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum)); 1124 return (0); 1125 } 1126 1127 static int 1128 pf_addr_setup(struct pf_kruleset *ruleset, struct pf_addr_wrap *addr, 1129 sa_family_t af) 1130 { 1131 int error = 0; 1132 1133 switch (addr->type) { 1134 case PF_ADDR_TABLE: 1135 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname); 1136 if (addr->p.tbl == NULL) 1137 error = ENOMEM; 1138 break; 1139 case PF_ADDR_DYNIFTL: 1140 error = pfi_dynaddr_setup(addr, af); 1141 break; 1142 } 1143 1144 return (error); 1145 } 1146 1147 static void 1148 pf_addr_copyout(struct pf_addr_wrap *addr) 1149 { 1150 1151 switch (addr->type) { 1152 case PF_ADDR_DYNIFTL: 1153 pfi_dynaddr_copyout(addr); 1154 break; 1155 case PF_ADDR_TABLE: 1156 pf_tbladdr_copyout(addr); 1157 break; 1158 } 1159 } 1160 1161 static void 1162 pf_src_node_copy(const struct pf_ksrc_node *in, struct pf_src_node *out) 1163 { 1164 int secs = time_uptime, diff; 1165 1166 bzero(out, sizeof(struct pf_src_node)); 1167 1168 bcopy(&in->addr, &out->addr, sizeof(struct pf_addr)); 1169 bcopy(&in->raddr, &out->raddr, sizeof(struct pf_addr)); 1170 1171 if (in->rule.ptr != NULL) 1172 out->rule.nr = in->rule.ptr->nr; 1173 1174 for (int i = 0; i < 2; i++) { 1175 out->bytes[i] = counter_u64_fetch(in->bytes[i]); 1176 out->packets[i] = counter_u64_fetch(in->packets[i]); 1177 } 1178 1179 out->states = in->states; 1180 out->conn = in->conn; 1181 out->af = in->af; 1182 out->ruletype = in->ruletype; 1183 1184 out->creation = secs - in->creation; 1185 if (out->expire > secs) 1186 out->expire -= secs; 1187 else 1188 out->expire = 0; 1189 1190 /* Adjust the connection rate estimate. */ 1191 diff = secs - in->conn_rate.last; 1192 if (diff >= in->conn_rate.seconds) 1193 out->conn_rate.count = 0; 1194 else 1195 out->conn_rate.count -= 1196 in->conn_rate.count * diff / 1197 in->conn_rate.seconds; 1198 } 1199 1200 #ifdef ALTQ 1201 /* 1202 * Handle export of struct pf_kaltq to user binaries that may be using any 1203 * version of struct pf_altq. 1204 */ 1205 static int 1206 pf_export_kaltq(struct pf_altq *q, struct pfioc_altq_v1 *pa, size_t ioc_size) 1207 { 1208 u_int32_t version; 1209 1210 if (ioc_size == sizeof(struct pfioc_altq_v0)) 1211 version = 0; 1212 else 1213 version = pa->version; 1214 1215 if (version > PFIOC_ALTQ_VERSION) 1216 return (EINVAL); 1217 1218 #define ASSIGN(x) exported_q->x = q->x 1219 #define COPY(x) \ 1220 bcopy(&q->x, &exported_q->x, min(sizeof(q->x), sizeof(exported_q->x))) 1221 #define SATU16(x) (u_int32_t)uqmin((x), USHRT_MAX) 1222 #define SATU32(x) (u_int32_t)uqmin((x), UINT_MAX) 1223 1224 switch (version) { 1225 case 0: { 1226 struct pf_altq_v0 *exported_q = 1227 &((struct pfioc_altq_v0 *)pa)->altq; 1228 1229 COPY(ifname); 1230 1231 ASSIGN(scheduler); 1232 ASSIGN(tbrsize); 1233 exported_q->tbrsize = SATU16(q->tbrsize); 1234 exported_q->ifbandwidth = SATU32(q->ifbandwidth); 1235 1236 COPY(qname); 1237 COPY(parent); 1238 ASSIGN(parent_qid); 1239 exported_q->bandwidth = SATU32(q->bandwidth); 1240 ASSIGN(priority); 1241 ASSIGN(local_flags); 1242 1243 ASSIGN(qlimit); 1244 ASSIGN(flags); 1245 1246 if (q->scheduler == ALTQT_HFSC) { 1247 #define ASSIGN_OPT(x) exported_q->pq_u.hfsc_opts.x = q->pq_u.hfsc_opts.x 1248 #define ASSIGN_OPT_SATU32(x) exported_q->pq_u.hfsc_opts.x = \ 1249 SATU32(q->pq_u.hfsc_opts.x) 1250 1251 ASSIGN_OPT_SATU32(rtsc_m1); 1252 ASSIGN_OPT(rtsc_d); 1253 ASSIGN_OPT_SATU32(rtsc_m2); 1254 1255 ASSIGN_OPT_SATU32(lssc_m1); 1256 ASSIGN_OPT(lssc_d); 1257 ASSIGN_OPT_SATU32(lssc_m2); 1258 1259 ASSIGN_OPT_SATU32(ulsc_m1); 1260 ASSIGN_OPT(ulsc_d); 1261 ASSIGN_OPT_SATU32(ulsc_m2); 1262 1263 ASSIGN_OPT(flags); 1264 1265 #undef ASSIGN_OPT 1266 #undef ASSIGN_OPT_SATU32 1267 } else 1268 COPY(pq_u); 1269 1270 ASSIGN(qid); 1271 break; 1272 } 1273 case 1: { 1274 struct pf_altq_v1 *exported_q = 1275 &((struct pfioc_altq_v1 *)pa)->altq; 1276 1277 COPY(ifname); 1278 1279 ASSIGN(scheduler); 1280 ASSIGN(tbrsize); 1281 ASSIGN(ifbandwidth); 1282 1283 COPY(qname); 1284 COPY(parent); 1285 ASSIGN(parent_qid); 1286 ASSIGN(bandwidth); 1287 ASSIGN(priority); 1288 ASSIGN(local_flags); 1289 1290 ASSIGN(qlimit); 1291 ASSIGN(flags); 1292 COPY(pq_u); 1293 1294 ASSIGN(qid); 1295 break; 1296 } 1297 default: 1298 panic("%s: unhandled struct pfioc_altq version", __func__); 1299 break; 1300 } 1301 1302 #undef ASSIGN 1303 #undef COPY 1304 #undef SATU16 1305 #undef SATU32 1306 1307 return (0); 1308 } 1309 1310 /* 1311 * Handle import to struct pf_kaltq of struct pf_altq from user binaries 1312 * that may be using any version of it. 1313 */ 1314 static int 1315 pf_import_kaltq(struct pfioc_altq_v1 *pa, struct pf_altq *q, size_t ioc_size) 1316 { 1317 u_int32_t version; 1318 1319 if (ioc_size == sizeof(struct pfioc_altq_v0)) 1320 version = 0; 1321 else 1322 version = pa->version; 1323 1324 if (version > PFIOC_ALTQ_VERSION) 1325 return (EINVAL); 1326 1327 #define ASSIGN(x) q->x = imported_q->x 1328 #define COPY(x) \ 1329 bcopy(&imported_q->x, &q->x, min(sizeof(imported_q->x), sizeof(q->x))) 1330 1331 switch (version) { 1332 case 0: { 1333 struct pf_altq_v0 *imported_q = 1334 &((struct pfioc_altq_v0 *)pa)->altq; 1335 1336 COPY(ifname); 1337 1338 ASSIGN(scheduler); 1339 ASSIGN(tbrsize); /* 16-bit -> 32-bit */ 1340 ASSIGN(ifbandwidth); /* 32-bit -> 64-bit */ 1341 1342 COPY(qname); 1343 COPY(parent); 1344 ASSIGN(parent_qid); 1345 ASSIGN(bandwidth); /* 32-bit -> 64-bit */ 1346 ASSIGN(priority); 1347 ASSIGN(local_flags); 1348 1349 ASSIGN(qlimit); 1350 ASSIGN(flags); 1351 1352 if (imported_q->scheduler == ALTQT_HFSC) { 1353 #define ASSIGN_OPT(x) q->pq_u.hfsc_opts.x = imported_q->pq_u.hfsc_opts.x 1354 1355 /* 1356 * The m1 and m2 parameters are being copied from 1357 * 32-bit to 64-bit. 1358 */ 1359 ASSIGN_OPT(rtsc_m1); 1360 ASSIGN_OPT(rtsc_d); 1361 ASSIGN_OPT(rtsc_m2); 1362 1363 ASSIGN_OPT(lssc_m1); 1364 ASSIGN_OPT(lssc_d); 1365 ASSIGN_OPT(lssc_m2); 1366 1367 ASSIGN_OPT(ulsc_m1); 1368 ASSIGN_OPT(ulsc_d); 1369 ASSIGN_OPT(ulsc_m2); 1370 1371 ASSIGN_OPT(flags); 1372 1373 #undef ASSIGN_OPT 1374 } else 1375 COPY(pq_u); 1376 1377 ASSIGN(qid); 1378 break; 1379 } 1380 case 1: { 1381 struct pf_altq_v1 *imported_q = 1382 &((struct pfioc_altq_v1 *)pa)->altq; 1383 1384 COPY(ifname); 1385 1386 ASSIGN(scheduler); 1387 ASSIGN(tbrsize); 1388 ASSIGN(ifbandwidth); 1389 1390 COPY(qname); 1391 COPY(parent); 1392 ASSIGN(parent_qid); 1393 ASSIGN(bandwidth); 1394 ASSIGN(priority); 1395 ASSIGN(local_flags); 1396 1397 ASSIGN(qlimit); 1398 ASSIGN(flags); 1399 COPY(pq_u); 1400 1401 ASSIGN(qid); 1402 break; 1403 } 1404 default: 1405 panic("%s: unhandled struct pfioc_altq version", __func__); 1406 break; 1407 } 1408 1409 #undef ASSIGN 1410 #undef COPY 1411 1412 return (0); 1413 } 1414 1415 static struct pf_altq * 1416 pf_altq_get_nth_active(u_int32_t n) 1417 { 1418 struct pf_altq *altq; 1419 u_int32_t nr; 1420 1421 nr = 0; 1422 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 1423 if (nr == n) 1424 return (altq); 1425 nr++; 1426 } 1427 1428 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) { 1429 if (nr == n) 1430 return (altq); 1431 nr++; 1432 } 1433 1434 return (NULL); 1435 } 1436 #endif /* ALTQ */ 1437 1438 static void 1439 pf_kpooladdr_to_pooladdr(const struct pf_kpooladdr *kpool, 1440 struct pf_pooladdr *pool) 1441 { 1442 1443 bzero(pool, sizeof(*pool)); 1444 bcopy(&kpool->addr, &pool->addr, sizeof(pool->addr)); 1445 strlcpy(pool->ifname, kpool->ifname, sizeof(pool->ifname)); 1446 } 1447 1448 static void 1449 pf_pooladdr_to_kpooladdr(const struct pf_pooladdr *pool, 1450 struct pf_kpooladdr *kpool) 1451 { 1452 1453 bzero(kpool, sizeof(*kpool)); 1454 bcopy(&pool->addr, &kpool->addr, sizeof(kpool->addr)); 1455 strlcpy(kpool->ifname, pool->ifname, sizeof(kpool->ifname)); 1456 } 1457 1458 static void 1459 pf_krule_to_rule(const struct pf_krule *krule, struct pf_rule *rule) 1460 { 1461 1462 bzero(rule, sizeof(*rule)); 1463 1464 bcopy(&krule->src, &rule->src, sizeof(rule->src)); 1465 bcopy(&krule->dst, &rule->dst, sizeof(rule->dst)); 1466 1467 for (int i = 0; i < PF_SKIP_COUNT; ++i) { 1468 if (rule->skip[i].ptr == NULL) 1469 rule->skip[i].nr = -1; 1470 else 1471 rule->skip[i].nr = krule->skip[i].ptr->nr; 1472 } 1473 1474 strlcpy(rule->label, krule->label, sizeof(rule->label)); 1475 strlcpy(rule->ifname, krule->ifname, sizeof(rule->ifname)); 1476 strlcpy(rule->qname, krule->qname, sizeof(rule->qname)); 1477 strlcpy(rule->pqname, krule->pqname, sizeof(rule->pqname)); 1478 strlcpy(rule->tagname, krule->tagname, sizeof(rule->tagname)); 1479 strlcpy(rule->match_tagname, krule->match_tagname, 1480 sizeof(rule->match_tagname)); 1481 strlcpy(rule->overload_tblname, krule->overload_tblname, 1482 sizeof(rule->overload_tblname)); 1483 1484 bcopy(&krule->rpool, &rule->rpool, sizeof(krule->rpool)); 1485 1486 rule->evaluations = counter_u64_fetch(krule->evaluations); 1487 for (int i = 0; i < 2; i++) { 1488 rule->packets[i] = counter_u64_fetch(krule->packets[i]); 1489 rule->bytes[i] = counter_u64_fetch(krule->bytes[i]); 1490 } 1491 1492 /* kif, anchor, overload_tbl are not copied over. */ 1493 1494 rule->os_fingerprint = krule->os_fingerprint; 1495 1496 rule->rtableid = krule->rtableid; 1497 bcopy(krule->timeout, rule->timeout, sizeof(krule->timeout)); 1498 rule->max_states = krule->max_states; 1499 rule->max_src_nodes = krule->max_src_nodes; 1500 rule->max_src_states = krule->max_src_states; 1501 rule->max_src_conn = krule->max_src_conn; 1502 rule->max_src_conn_rate.limit = krule->max_src_conn_rate.limit; 1503 rule->max_src_conn_rate.seconds = krule->max_src_conn_rate.seconds; 1504 rule->qid = krule->qid; 1505 rule->pqid = krule->pqid; 1506 rule->rt_listid = krule->rt_listid; 1507 rule->nr = krule->nr; 1508 rule->prob = krule->prob; 1509 rule->cuid = krule->cuid; 1510 rule->cpid = krule->cpid; 1511 1512 rule->return_icmp = krule->return_icmp; 1513 rule->return_icmp6 = krule->return_icmp6; 1514 rule->max_mss = krule->max_mss; 1515 rule->tag = krule->tag; 1516 rule->match_tag = krule->match_tag; 1517 rule->scrub_flags = krule->scrub_flags; 1518 1519 bcopy(&krule->uid, &rule->uid, sizeof(krule->uid)); 1520 bcopy(&krule->gid, &rule->gid, sizeof(krule->gid)); 1521 1522 rule->rule_flag = krule->rule_flag; 1523 rule->action = krule->action; 1524 rule->direction = krule->direction; 1525 rule->log = krule->log; 1526 rule->logif = krule->logif; 1527 rule->quick = krule->quick; 1528 rule->ifnot = krule->ifnot; 1529 rule->match_tag_not = krule->match_tag_not; 1530 rule->natpass = krule->natpass; 1531 1532 rule->keep_state = krule->keep_state; 1533 rule->af = krule->af; 1534 rule->proto = krule->proto; 1535 rule->type = krule->type; 1536 rule->code = krule->code; 1537 rule->flags = krule->flags; 1538 rule->flagset = krule->flagset; 1539 rule->min_ttl = krule->min_ttl; 1540 rule->allow_opts = krule->allow_opts; 1541 rule->rt = krule->rt; 1542 rule->return_ttl = krule->return_ttl; 1543 rule->tos = krule->tos; 1544 rule->set_tos = krule->set_tos; 1545 rule->anchor_relative = krule->anchor_relative; 1546 rule->anchor_wildcard = krule->anchor_wildcard; 1547 1548 rule->flush = krule->flush; 1549 rule->prio = krule->prio; 1550 rule->set_prio[0] = krule->set_prio[0]; 1551 rule->set_prio[1] = krule->set_prio[1]; 1552 1553 bcopy(&krule->divert, &rule->divert, sizeof(krule->divert)); 1554 1555 rule->u_states_cur = counter_u64_fetch(krule->states_cur); 1556 rule->u_states_tot = counter_u64_fetch(krule->states_tot); 1557 rule->u_src_nodes = counter_u64_fetch(krule->src_nodes); 1558 } 1559 1560 static void 1561 pf_rule_to_krule(const struct pf_rule *rule, struct pf_krule *krule) 1562 { 1563 1564 bzero(krule, sizeof(*krule)); 1565 1566 bcopy(&rule->src, &krule->src, sizeof(rule->src)); 1567 bcopy(&rule->dst, &krule->dst, sizeof(rule->dst)); 1568 1569 strlcpy(krule->label, rule->label, sizeof(rule->label)); 1570 strlcpy(krule->ifname, rule->ifname, sizeof(rule->ifname)); 1571 strlcpy(krule->qname, rule->qname, sizeof(rule->qname)); 1572 strlcpy(krule->pqname, rule->pqname, sizeof(rule->pqname)); 1573 strlcpy(krule->tagname, rule->tagname, sizeof(rule->tagname)); 1574 strlcpy(krule->match_tagname, rule->match_tagname, 1575 sizeof(rule->match_tagname)); 1576 strlcpy(krule->overload_tblname, rule->overload_tblname, 1577 sizeof(rule->overload_tblname)); 1578 1579 bcopy(&rule->rpool, &krule->rpool, sizeof(krule->rpool)); 1580 1581 /* Don't allow userspace to set evaulations, packets or bytes. */ 1582 /* kif, anchor, overload_tbl are not copied over. */ 1583 1584 krule->os_fingerprint = krule->os_fingerprint; 1585 1586 krule->rtableid = rule->rtableid; 1587 bcopy(rule->timeout, krule->timeout, sizeof(krule->timeout)); 1588 krule->max_states = rule->max_states; 1589 krule->max_src_nodes = rule->max_src_nodes; 1590 krule->max_src_states = rule->max_src_states; 1591 krule->max_src_conn = rule->max_src_conn; 1592 krule->max_src_conn_rate.limit = rule->max_src_conn_rate.limit; 1593 krule->max_src_conn_rate.seconds = rule->max_src_conn_rate.seconds; 1594 krule->qid = rule->qid; 1595 krule->pqid = rule->pqid; 1596 krule->rt_listid = rule->rt_listid; 1597 krule->nr = rule->nr; 1598 krule->prob = rule->prob; 1599 krule->cuid = rule->cuid; 1600 krule->cpid = rule->cpid; 1601 1602 krule->return_icmp = rule->return_icmp; 1603 krule->return_icmp6 = rule->return_icmp6; 1604 krule->max_mss = rule->max_mss; 1605 krule->tag = rule->tag; 1606 krule->match_tag = rule->match_tag; 1607 krule->scrub_flags = rule->scrub_flags; 1608 1609 bcopy(&rule->uid, &krule->uid, sizeof(krule->uid)); 1610 bcopy(&rule->gid, &krule->gid, sizeof(krule->gid)); 1611 1612 krule->rule_flag = rule->rule_flag; 1613 krule->action = rule->action; 1614 krule->direction = rule->direction; 1615 krule->log = rule->log; 1616 krule->logif = rule->logif; 1617 krule->quick = rule->quick; 1618 krule->ifnot = rule->ifnot; 1619 krule->match_tag_not = rule->match_tag_not; 1620 krule->natpass = rule->natpass; 1621 1622 krule->keep_state = rule->keep_state; 1623 krule->af = rule->af; 1624 krule->proto = rule->proto; 1625 krule->type = rule->type; 1626 krule->code = rule->code; 1627 krule->flags = rule->flags; 1628 krule->flagset = rule->flagset; 1629 krule->min_ttl = rule->min_ttl; 1630 krule->allow_opts = rule->allow_opts; 1631 krule->rt = rule->rt; 1632 krule->return_ttl = rule->return_ttl; 1633 krule->tos = rule->tos; 1634 krule->set_tos = rule->set_tos; 1635 krule->anchor_relative = rule->anchor_relative; 1636 krule->anchor_wildcard = rule->anchor_wildcard; 1637 1638 krule->flush = rule->flush; 1639 krule->prio = rule->prio; 1640 krule->set_prio[0] = rule->set_prio[0]; 1641 krule->set_prio[1] = rule->set_prio[1]; 1642 1643 bcopy(&rule->divert, &krule->divert, sizeof(krule->divert)); 1644 } 1645 1646 static int 1647 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 1648 { 1649 int error = 0; 1650 PF_RULES_RLOCK_TRACKER; 1651 1652 /* XXX keep in sync with switch() below */ 1653 if (securelevel_gt(td->td_ucred, 2)) 1654 switch (cmd) { 1655 case DIOCGETRULES: 1656 case DIOCGETRULE: 1657 case DIOCGETADDRS: 1658 case DIOCGETADDR: 1659 case DIOCGETSTATE: 1660 case DIOCSETSTATUSIF: 1661 case DIOCGETSTATUS: 1662 case DIOCCLRSTATUS: 1663 case DIOCNATLOOK: 1664 case DIOCSETDEBUG: 1665 case DIOCGETSTATES: 1666 case DIOCGETTIMEOUT: 1667 case DIOCCLRRULECTRS: 1668 case DIOCGETLIMIT: 1669 case DIOCGETALTQSV0: 1670 case DIOCGETALTQSV1: 1671 case DIOCGETALTQV0: 1672 case DIOCGETALTQV1: 1673 case DIOCGETQSTATSV0: 1674 case DIOCGETQSTATSV1: 1675 case DIOCGETRULESETS: 1676 case DIOCGETRULESET: 1677 case DIOCRGETTABLES: 1678 case DIOCRGETTSTATS: 1679 case DIOCRCLRTSTATS: 1680 case DIOCRCLRADDRS: 1681 case DIOCRADDADDRS: 1682 case DIOCRDELADDRS: 1683 case DIOCRSETADDRS: 1684 case DIOCRGETADDRS: 1685 case DIOCRGETASTATS: 1686 case DIOCRCLRASTATS: 1687 case DIOCRTSTADDRS: 1688 case DIOCOSFPGET: 1689 case DIOCGETSRCNODES: 1690 case DIOCCLRSRCNODES: 1691 case DIOCIGETIFACES: 1692 case DIOCGIFSPEEDV0: 1693 case DIOCGIFSPEEDV1: 1694 case DIOCSETIFFLAG: 1695 case DIOCCLRIFFLAG: 1696 break; 1697 case DIOCRCLRTABLES: 1698 case DIOCRADDTABLES: 1699 case DIOCRDELTABLES: 1700 case DIOCRSETTFLAGS: 1701 if (((struct pfioc_table *)addr)->pfrio_flags & 1702 PFR_FLAG_DUMMY) 1703 break; /* dummy operation ok */ 1704 return (EPERM); 1705 default: 1706 return (EPERM); 1707 } 1708 1709 if (!(flags & FWRITE)) 1710 switch (cmd) { 1711 case DIOCGETRULES: 1712 case DIOCGETADDRS: 1713 case DIOCGETADDR: 1714 case DIOCGETSTATE: 1715 case DIOCGETSTATUS: 1716 case DIOCGETSTATES: 1717 case DIOCGETTIMEOUT: 1718 case DIOCGETLIMIT: 1719 case DIOCGETALTQSV0: 1720 case DIOCGETALTQSV1: 1721 case DIOCGETALTQV0: 1722 case DIOCGETALTQV1: 1723 case DIOCGETQSTATSV0: 1724 case DIOCGETQSTATSV1: 1725 case DIOCGETRULESETS: 1726 case DIOCGETRULESET: 1727 case DIOCNATLOOK: 1728 case DIOCRGETTABLES: 1729 case DIOCRGETTSTATS: 1730 case DIOCRGETADDRS: 1731 case DIOCRGETASTATS: 1732 case DIOCRTSTADDRS: 1733 case DIOCOSFPGET: 1734 case DIOCGETSRCNODES: 1735 case DIOCIGETIFACES: 1736 case DIOCGIFSPEEDV1: 1737 case DIOCGIFSPEEDV0: 1738 break; 1739 case DIOCRCLRTABLES: 1740 case DIOCRADDTABLES: 1741 case DIOCRDELTABLES: 1742 case DIOCRCLRTSTATS: 1743 case DIOCRCLRADDRS: 1744 case DIOCRADDADDRS: 1745 case DIOCRDELADDRS: 1746 case DIOCRSETADDRS: 1747 case DIOCRSETTFLAGS: 1748 if (((struct pfioc_table *)addr)->pfrio_flags & 1749 PFR_FLAG_DUMMY) { 1750 flags |= FWRITE; /* need write lock for dummy */ 1751 break; /* dummy operation ok */ 1752 } 1753 return (EACCES); 1754 case DIOCGETRULE: 1755 if (((struct pfioc_rule *)addr)->action == 1756 PF_GET_CLR_CNTR) 1757 return (EACCES); 1758 break; 1759 default: 1760 return (EACCES); 1761 } 1762 1763 CURVNET_SET(TD_TO_VNET(td)); 1764 1765 switch (cmd) { 1766 case DIOCSTART: 1767 sx_xlock(&pf_ioctl_lock); 1768 if (V_pf_status.running) 1769 error = EEXIST; 1770 else { 1771 int cpu; 1772 1773 error = hook_pf(); 1774 if (error) { 1775 DPFPRINTF(PF_DEBUG_MISC, 1776 ("pf: pfil registration failed\n")); 1777 break; 1778 } 1779 V_pf_status.running = 1; 1780 V_pf_status.since = time_second; 1781 1782 CPU_FOREACH(cpu) 1783 V_pf_stateid[cpu] = time_second; 1784 1785 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n")); 1786 } 1787 break; 1788 1789 case DIOCSTOP: 1790 sx_xlock(&pf_ioctl_lock); 1791 if (!V_pf_status.running) 1792 error = ENOENT; 1793 else { 1794 V_pf_status.running = 0; 1795 error = dehook_pf(); 1796 if (error) { 1797 V_pf_status.running = 1; 1798 DPFPRINTF(PF_DEBUG_MISC, 1799 ("pf: pfil unregistration failed\n")); 1800 } 1801 V_pf_status.since = time_second; 1802 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n")); 1803 } 1804 break; 1805 1806 case DIOCADDRULE: { 1807 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 1808 struct pf_kruleset *ruleset; 1809 struct pf_krule *rule, *tail; 1810 struct pf_kpooladdr *pa; 1811 struct pfi_kkif *kif = NULL; 1812 int rs_num; 1813 1814 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) { 1815 error = EINVAL; 1816 break; 1817 } 1818 if (pr->rule.src.addr.p.dyn != NULL || 1819 pr->rule.dst.addr.p.dyn != NULL) { 1820 error = EINVAL; 1821 break; 1822 } 1823 #ifndef INET 1824 if (pr->rule.af == AF_INET) { 1825 error = EAFNOSUPPORT; 1826 break; 1827 } 1828 #endif /* INET */ 1829 #ifndef INET6 1830 if (pr->rule.af == AF_INET6) { 1831 error = EAFNOSUPPORT; 1832 break; 1833 } 1834 #endif /* INET6 */ 1835 1836 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK); 1837 pf_rule_to_krule(&pr->rule, rule); 1838 1839 if (rule->ifname[0]) 1840 kif = pf_kkif_create(M_WAITOK); 1841 rule->evaluations = counter_u64_alloc(M_WAITOK); 1842 for (int i = 0; i < 2; i++) { 1843 rule->packets[i] = counter_u64_alloc(M_WAITOK); 1844 rule->bytes[i] = counter_u64_alloc(M_WAITOK); 1845 } 1846 rule->states_cur = counter_u64_alloc(M_WAITOK); 1847 rule->states_tot = counter_u64_alloc(M_WAITOK); 1848 rule->src_nodes = counter_u64_alloc(M_WAITOK); 1849 rule->cuid = td->td_ucred->cr_ruid; 1850 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0; 1851 TAILQ_INIT(&rule->rpool.list); 1852 #define ERROUT(x) { error = (x); goto DIOCADDRULE_error; } 1853 1854 PF_RULES_WLOCK(); 1855 pr->anchor[sizeof(pr->anchor) - 1] = 0; 1856 ruleset = pf_find_kruleset(pr->anchor); 1857 if (ruleset == NULL) 1858 ERROUT(EINVAL); 1859 rs_num = pf_get_ruleset_number(pr->rule.action); 1860 if (rs_num >= PF_RULESET_MAX) 1861 ERROUT(EINVAL); 1862 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) { 1863 DPFPRINTF(PF_DEBUG_MISC, 1864 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num, 1865 ruleset->rules[rs_num].inactive.ticket)); 1866 ERROUT(EBUSY); 1867 } 1868 if (pr->pool_ticket != V_ticket_pabuf) { 1869 DPFPRINTF(PF_DEBUG_MISC, 1870 ("pool_ticket: %d != %d\n", pr->pool_ticket, 1871 V_ticket_pabuf)); 1872 ERROUT(EBUSY); 1873 } 1874 1875 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr, 1876 pf_krulequeue); 1877 if (tail) 1878 rule->nr = tail->nr + 1; 1879 else 1880 rule->nr = 0; 1881 if (rule->ifname[0]) { 1882 rule->kif = pfi_kkif_attach(kif, rule->ifname); 1883 pfi_kkif_ref(rule->kif); 1884 } else 1885 rule->kif = NULL; 1886 1887 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs) 1888 error = EBUSY; 1889 1890 #ifdef ALTQ 1891 /* set queue IDs */ 1892 if (rule->qname[0] != 0) { 1893 if ((rule->qid = pf_qname2qid(rule->qname)) == 0) 1894 error = EBUSY; 1895 else if (rule->pqname[0] != 0) { 1896 if ((rule->pqid = 1897 pf_qname2qid(rule->pqname)) == 0) 1898 error = EBUSY; 1899 } else 1900 rule->pqid = rule->qid; 1901 } 1902 #endif 1903 if (rule->tagname[0]) 1904 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0) 1905 error = EBUSY; 1906 if (rule->match_tagname[0]) 1907 if ((rule->match_tag = 1908 pf_tagname2tag(rule->match_tagname)) == 0) 1909 error = EBUSY; 1910 if (rule->rt && !rule->direction) 1911 error = EINVAL; 1912 if (!rule->log) 1913 rule->logif = 0; 1914 if (rule->logif >= PFLOGIFS_MAX) 1915 error = EINVAL; 1916 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af)) 1917 error = ENOMEM; 1918 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af)) 1919 error = ENOMEM; 1920 if (pf_kanchor_setup(rule, ruleset, pr->anchor_call)) 1921 error = EINVAL; 1922 if (rule->scrub_flags & PFSTATE_SETPRIO && 1923 (rule->set_prio[0] > PF_PRIO_MAX || 1924 rule->set_prio[1] > PF_PRIO_MAX)) 1925 error = EINVAL; 1926 TAILQ_FOREACH(pa, &V_pf_pabuf, entries) 1927 if (pa->addr.type == PF_ADDR_TABLE) { 1928 pa->addr.p.tbl = pfr_attach_table(ruleset, 1929 pa->addr.v.tblname); 1930 if (pa->addr.p.tbl == NULL) 1931 error = ENOMEM; 1932 } 1933 1934 rule->overload_tbl = NULL; 1935 if (rule->overload_tblname[0]) { 1936 if ((rule->overload_tbl = pfr_attach_table(ruleset, 1937 rule->overload_tblname)) == NULL) 1938 error = EINVAL; 1939 else 1940 rule->overload_tbl->pfrkt_flags |= 1941 PFR_TFLAG_ACTIVE; 1942 } 1943 1944 pf_mv_kpool(&V_pf_pabuf, &rule->rpool.list); 1945 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) || 1946 (rule->action == PF_BINAT)) && rule->anchor == NULL) || 1947 (rule->rt > PF_NOPFROUTE)) && 1948 (TAILQ_FIRST(&rule->rpool.list) == NULL)) 1949 error = EINVAL; 1950 1951 if (error) { 1952 pf_free_rule(rule); 1953 PF_RULES_WUNLOCK(); 1954 break; 1955 } 1956 1957 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list); 1958 counter_u64_zero(rule->evaluations); 1959 for (int i = 0; i < 2; i++) { 1960 counter_u64_zero(rule->packets[i]); 1961 counter_u64_zero(rule->bytes[i]); 1962 } 1963 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr, 1964 rule, entries); 1965 ruleset->rules[rs_num].inactive.rcount++; 1966 PF_RULES_WUNLOCK(); 1967 break; 1968 1969 #undef ERROUT 1970 DIOCADDRULE_error: 1971 PF_RULES_WUNLOCK(); 1972 counter_u64_free(rule->evaluations); 1973 for (int i = 0; i < 2; i++) { 1974 counter_u64_free(rule->packets[i]); 1975 counter_u64_free(rule->bytes[i]); 1976 } 1977 counter_u64_free(rule->states_cur); 1978 counter_u64_free(rule->states_tot); 1979 counter_u64_free(rule->src_nodes); 1980 free(rule, M_PFRULE); 1981 if (kif) 1982 pf_kkif_free(kif); 1983 break; 1984 } 1985 1986 case DIOCGETRULES: { 1987 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 1988 struct pf_kruleset *ruleset; 1989 struct pf_krule *tail; 1990 int rs_num; 1991 1992 PF_RULES_WLOCK(); 1993 pr->anchor[sizeof(pr->anchor) - 1] = 0; 1994 ruleset = pf_find_kruleset(pr->anchor); 1995 if (ruleset == NULL) { 1996 PF_RULES_WUNLOCK(); 1997 error = EINVAL; 1998 break; 1999 } 2000 rs_num = pf_get_ruleset_number(pr->rule.action); 2001 if (rs_num >= PF_RULESET_MAX) { 2002 PF_RULES_WUNLOCK(); 2003 error = EINVAL; 2004 break; 2005 } 2006 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr, 2007 pf_krulequeue); 2008 if (tail) 2009 pr->nr = tail->nr + 1; 2010 else 2011 pr->nr = 0; 2012 pr->ticket = ruleset->rules[rs_num].active.ticket; 2013 PF_RULES_WUNLOCK(); 2014 break; 2015 } 2016 2017 case DIOCGETRULE: { 2018 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 2019 struct pf_kruleset *ruleset; 2020 struct pf_krule *rule; 2021 int rs_num; 2022 2023 PF_RULES_WLOCK(); 2024 pr->anchor[sizeof(pr->anchor) - 1] = 0; 2025 ruleset = pf_find_kruleset(pr->anchor); 2026 if (ruleset == NULL) { 2027 PF_RULES_WUNLOCK(); 2028 error = EINVAL; 2029 break; 2030 } 2031 rs_num = pf_get_ruleset_number(pr->rule.action); 2032 if (rs_num >= PF_RULESET_MAX) { 2033 PF_RULES_WUNLOCK(); 2034 error = EINVAL; 2035 break; 2036 } 2037 if (pr->ticket != ruleset->rules[rs_num].active.ticket) { 2038 PF_RULES_WUNLOCK(); 2039 error = EBUSY; 2040 break; 2041 } 2042 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr); 2043 while ((rule != NULL) && (rule->nr != pr->nr)) 2044 rule = TAILQ_NEXT(rule, entries); 2045 if (rule == NULL) { 2046 PF_RULES_WUNLOCK(); 2047 error = EBUSY; 2048 break; 2049 } 2050 2051 pf_krule_to_rule(rule, &pr->rule); 2052 2053 if (pf_kanchor_copyout(ruleset, rule, pr)) { 2054 PF_RULES_WUNLOCK(); 2055 error = EBUSY; 2056 break; 2057 } 2058 pf_addr_copyout(&pr->rule.src.addr); 2059 pf_addr_copyout(&pr->rule.dst.addr); 2060 2061 if (pr->action == PF_GET_CLR_CNTR) { 2062 counter_u64_zero(rule->evaluations); 2063 for (int i = 0; i < 2; i++) { 2064 counter_u64_zero(rule->packets[i]); 2065 counter_u64_zero(rule->bytes[i]); 2066 } 2067 counter_u64_zero(rule->states_tot); 2068 } 2069 PF_RULES_WUNLOCK(); 2070 break; 2071 } 2072 2073 case DIOCCHANGERULE: { 2074 struct pfioc_rule *pcr = (struct pfioc_rule *)addr; 2075 struct pf_kruleset *ruleset; 2076 struct pf_krule *oldrule = NULL, *newrule = NULL; 2077 struct pfi_kkif *kif = NULL; 2078 struct pf_kpooladdr *pa; 2079 u_int32_t nr = 0; 2080 int rs_num; 2081 2082 if (pcr->action < PF_CHANGE_ADD_HEAD || 2083 pcr->action > PF_CHANGE_GET_TICKET) { 2084 error = EINVAL; 2085 break; 2086 } 2087 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) { 2088 error = EINVAL; 2089 break; 2090 } 2091 2092 if (pcr->action != PF_CHANGE_REMOVE) { 2093 #ifndef INET 2094 if (pcr->rule.af == AF_INET) { 2095 error = EAFNOSUPPORT; 2096 break; 2097 } 2098 #endif /* INET */ 2099 #ifndef INET6 2100 if (pcr->rule.af == AF_INET6) { 2101 error = EAFNOSUPPORT; 2102 break; 2103 } 2104 #endif /* INET6 */ 2105 newrule = malloc(sizeof(*newrule), M_PFRULE, M_WAITOK); 2106 pf_rule_to_krule(&pcr->rule, newrule); 2107 2108 if (newrule->ifname[0]) 2109 kif = pf_kkif_create(M_WAITOK); 2110 newrule->evaluations = counter_u64_alloc(M_WAITOK); 2111 for (int i = 0; i < 2; i++) { 2112 newrule->packets[i] = 2113 counter_u64_alloc(M_WAITOK); 2114 newrule->bytes[i] = 2115 counter_u64_alloc(M_WAITOK); 2116 } 2117 newrule->states_cur = counter_u64_alloc(M_WAITOK); 2118 newrule->states_tot = counter_u64_alloc(M_WAITOK); 2119 newrule->src_nodes = counter_u64_alloc(M_WAITOK); 2120 newrule->cuid = td->td_ucred->cr_ruid; 2121 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0; 2122 TAILQ_INIT(&newrule->rpool.list); 2123 } 2124 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; } 2125 2126 PF_RULES_WLOCK(); 2127 if (!(pcr->action == PF_CHANGE_REMOVE || 2128 pcr->action == PF_CHANGE_GET_TICKET) && 2129 pcr->pool_ticket != V_ticket_pabuf) 2130 ERROUT(EBUSY); 2131 2132 ruleset = pf_find_kruleset(pcr->anchor); 2133 if (ruleset == NULL) 2134 ERROUT(EINVAL); 2135 2136 rs_num = pf_get_ruleset_number(pcr->rule.action); 2137 if (rs_num >= PF_RULESET_MAX) 2138 ERROUT(EINVAL); 2139 2140 if (pcr->action == PF_CHANGE_GET_TICKET) { 2141 pcr->ticket = ++ruleset->rules[rs_num].active.ticket; 2142 ERROUT(0); 2143 } else if (pcr->ticket != 2144 ruleset->rules[rs_num].active.ticket) 2145 ERROUT(EINVAL); 2146 2147 if (pcr->action != PF_CHANGE_REMOVE) { 2148 if (newrule->ifname[0]) { 2149 newrule->kif = pfi_kkif_attach(kif, 2150 newrule->ifname); 2151 pfi_kkif_ref(newrule->kif); 2152 } else 2153 newrule->kif = NULL; 2154 2155 if (newrule->rtableid > 0 && 2156 newrule->rtableid >= rt_numfibs) 2157 error = EBUSY; 2158 2159 #ifdef ALTQ 2160 /* set queue IDs */ 2161 if (newrule->qname[0] != 0) { 2162 if ((newrule->qid = 2163 pf_qname2qid(newrule->qname)) == 0) 2164 error = EBUSY; 2165 else if (newrule->pqname[0] != 0) { 2166 if ((newrule->pqid = 2167 pf_qname2qid(newrule->pqname)) == 0) 2168 error = EBUSY; 2169 } else 2170 newrule->pqid = newrule->qid; 2171 } 2172 #endif /* ALTQ */ 2173 if (newrule->tagname[0]) 2174 if ((newrule->tag = 2175 pf_tagname2tag(newrule->tagname)) == 0) 2176 error = EBUSY; 2177 if (newrule->match_tagname[0]) 2178 if ((newrule->match_tag = pf_tagname2tag( 2179 newrule->match_tagname)) == 0) 2180 error = EBUSY; 2181 if (newrule->rt && !newrule->direction) 2182 error = EINVAL; 2183 if (!newrule->log) 2184 newrule->logif = 0; 2185 if (newrule->logif >= PFLOGIFS_MAX) 2186 error = EINVAL; 2187 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af)) 2188 error = ENOMEM; 2189 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af)) 2190 error = ENOMEM; 2191 if (pf_kanchor_setup(newrule, ruleset, pcr->anchor_call)) 2192 error = EINVAL; 2193 TAILQ_FOREACH(pa, &V_pf_pabuf, entries) 2194 if (pa->addr.type == PF_ADDR_TABLE) { 2195 pa->addr.p.tbl = 2196 pfr_attach_table(ruleset, 2197 pa->addr.v.tblname); 2198 if (pa->addr.p.tbl == NULL) 2199 error = ENOMEM; 2200 } 2201 2202 newrule->overload_tbl = NULL; 2203 if (newrule->overload_tblname[0]) { 2204 if ((newrule->overload_tbl = pfr_attach_table( 2205 ruleset, newrule->overload_tblname)) == 2206 NULL) 2207 error = EINVAL; 2208 else 2209 newrule->overload_tbl->pfrkt_flags |= 2210 PFR_TFLAG_ACTIVE; 2211 } 2212 2213 pf_mv_kpool(&V_pf_pabuf, &newrule->rpool.list); 2214 if (((((newrule->action == PF_NAT) || 2215 (newrule->action == PF_RDR) || 2216 (newrule->action == PF_BINAT) || 2217 (newrule->rt > PF_NOPFROUTE)) && 2218 !newrule->anchor)) && 2219 (TAILQ_FIRST(&newrule->rpool.list) == NULL)) 2220 error = EINVAL; 2221 2222 if (error) { 2223 pf_free_rule(newrule); 2224 PF_RULES_WUNLOCK(); 2225 break; 2226 } 2227 2228 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list); 2229 } 2230 pf_empty_kpool(&V_pf_pabuf); 2231 2232 if (pcr->action == PF_CHANGE_ADD_HEAD) 2233 oldrule = TAILQ_FIRST( 2234 ruleset->rules[rs_num].active.ptr); 2235 else if (pcr->action == PF_CHANGE_ADD_TAIL) 2236 oldrule = TAILQ_LAST( 2237 ruleset->rules[rs_num].active.ptr, pf_krulequeue); 2238 else { 2239 oldrule = TAILQ_FIRST( 2240 ruleset->rules[rs_num].active.ptr); 2241 while ((oldrule != NULL) && (oldrule->nr != pcr->nr)) 2242 oldrule = TAILQ_NEXT(oldrule, entries); 2243 if (oldrule == NULL) { 2244 if (newrule != NULL) 2245 pf_free_rule(newrule); 2246 PF_RULES_WUNLOCK(); 2247 error = EINVAL; 2248 break; 2249 } 2250 } 2251 2252 if (pcr->action == PF_CHANGE_REMOVE) { 2253 pf_unlink_rule(ruleset->rules[rs_num].active.ptr, 2254 oldrule); 2255 ruleset->rules[rs_num].active.rcount--; 2256 } else { 2257 if (oldrule == NULL) 2258 TAILQ_INSERT_TAIL( 2259 ruleset->rules[rs_num].active.ptr, 2260 newrule, entries); 2261 else if (pcr->action == PF_CHANGE_ADD_HEAD || 2262 pcr->action == PF_CHANGE_ADD_BEFORE) 2263 TAILQ_INSERT_BEFORE(oldrule, newrule, entries); 2264 else 2265 TAILQ_INSERT_AFTER( 2266 ruleset->rules[rs_num].active.ptr, 2267 oldrule, newrule, entries); 2268 ruleset->rules[rs_num].active.rcount++; 2269 } 2270 2271 nr = 0; 2272 TAILQ_FOREACH(oldrule, 2273 ruleset->rules[rs_num].active.ptr, entries) 2274 oldrule->nr = nr++; 2275 2276 ruleset->rules[rs_num].active.ticket++; 2277 2278 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr); 2279 pf_remove_if_empty_kruleset(ruleset); 2280 2281 PF_RULES_WUNLOCK(); 2282 break; 2283 2284 #undef ERROUT 2285 DIOCCHANGERULE_error: 2286 PF_RULES_WUNLOCK(); 2287 if (newrule != NULL) { 2288 counter_u64_free(newrule->evaluations); 2289 for (int i = 0; i < 2; i++) { 2290 counter_u64_free(newrule->packets[i]); 2291 counter_u64_free(newrule->bytes[i]); 2292 } 2293 counter_u64_free(newrule->states_cur); 2294 counter_u64_free(newrule->states_tot); 2295 counter_u64_free(newrule->src_nodes); 2296 free(newrule, M_PFRULE); 2297 } 2298 if (kif != NULL) 2299 pf_kkif_free(kif); 2300 break; 2301 } 2302 2303 case DIOCCLRSTATES: { 2304 struct pf_state *s; 2305 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr; 2306 u_int i, killed = 0; 2307 2308 for (i = 0; i <= pf_hashmask; i++) { 2309 struct pf_idhash *ih = &V_pf_idhash[i]; 2310 2311 relock_DIOCCLRSTATES: 2312 PF_HASHROW_LOCK(ih); 2313 LIST_FOREACH(s, &ih->states, entry) 2314 if (!psk->psk_ifname[0] || 2315 !strcmp(psk->psk_ifname, 2316 s->kif->pfik_name)) { 2317 /* 2318 * Don't send out individual 2319 * delete messages. 2320 */ 2321 s->state_flags |= PFSTATE_NOSYNC; 2322 pf_unlink_state(s, PF_ENTER_LOCKED); 2323 killed++; 2324 goto relock_DIOCCLRSTATES; 2325 } 2326 PF_HASHROW_UNLOCK(ih); 2327 } 2328 psk->psk_killed = killed; 2329 if (V_pfsync_clear_states_ptr != NULL) 2330 V_pfsync_clear_states_ptr(V_pf_status.hostid, psk->psk_ifname); 2331 break; 2332 } 2333 2334 case DIOCKILLSTATES: { 2335 struct pf_state *s; 2336 struct pf_state_key *sk; 2337 struct pf_addr *srcaddr, *dstaddr; 2338 u_int16_t srcport, dstport; 2339 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr; 2340 u_int i, killed = 0; 2341 2342 if (psk->psk_pfcmp.id) { 2343 if (psk->psk_pfcmp.creatorid == 0) 2344 psk->psk_pfcmp.creatorid = V_pf_status.hostid; 2345 if ((s = pf_find_state_byid(psk->psk_pfcmp.id, 2346 psk->psk_pfcmp.creatorid))) { 2347 pf_unlink_state(s, PF_ENTER_LOCKED); 2348 psk->psk_killed = 1; 2349 } 2350 break; 2351 } 2352 2353 for (i = 0; i <= pf_hashmask; i++) { 2354 struct pf_idhash *ih = &V_pf_idhash[i]; 2355 2356 relock_DIOCKILLSTATES: 2357 PF_HASHROW_LOCK(ih); 2358 LIST_FOREACH(s, &ih->states, entry) { 2359 sk = s->key[PF_SK_WIRE]; 2360 if (s->direction == PF_OUT) { 2361 srcaddr = &sk->addr[1]; 2362 dstaddr = &sk->addr[0]; 2363 srcport = sk->port[1]; 2364 dstport = sk->port[0]; 2365 } else { 2366 srcaddr = &sk->addr[0]; 2367 dstaddr = &sk->addr[1]; 2368 srcport = sk->port[0]; 2369 dstport = sk->port[1]; 2370 } 2371 2372 if ((!psk->psk_af || sk->af == psk->psk_af) 2373 && (!psk->psk_proto || psk->psk_proto == 2374 sk->proto) && 2375 PF_MATCHA(psk->psk_src.neg, 2376 &psk->psk_src.addr.v.a.addr, 2377 &psk->psk_src.addr.v.a.mask, 2378 srcaddr, sk->af) && 2379 PF_MATCHA(psk->psk_dst.neg, 2380 &psk->psk_dst.addr.v.a.addr, 2381 &psk->psk_dst.addr.v.a.mask, 2382 dstaddr, sk->af) && 2383 (psk->psk_src.port_op == 0 || 2384 pf_match_port(psk->psk_src.port_op, 2385 psk->psk_src.port[0], psk->psk_src.port[1], 2386 srcport)) && 2387 (psk->psk_dst.port_op == 0 || 2388 pf_match_port(psk->psk_dst.port_op, 2389 psk->psk_dst.port[0], psk->psk_dst.port[1], 2390 dstport)) && 2391 (!psk->psk_label[0] || 2392 (s->rule.ptr->label[0] && 2393 !strcmp(psk->psk_label, 2394 s->rule.ptr->label))) && 2395 (!psk->psk_ifname[0] || 2396 !strcmp(psk->psk_ifname, 2397 s->kif->pfik_name))) { 2398 pf_unlink_state(s, PF_ENTER_LOCKED); 2399 killed++; 2400 goto relock_DIOCKILLSTATES; 2401 } 2402 } 2403 PF_HASHROW_UNLOCK(ih); 2404 } 2405 psk->psk_killed = killed; 2406 break; 2407 } 2408 2409 case DIOCADDSTATE: { 2410 struct pfioc_state *ps = (struct pfioc_state *)addr; 2411 struct pfsync_state *sp = &ps->state; 2412 2413 if (sp->timeout >= PFTM_MAX) { 2414 error = EINVAL; 2415 break; 2416 } 2417 if (V_pfsync_state_import_ptr != NULL) { 2418 PF_RULES_RLOCK(); 2419 error = V_pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL); 2420 PF_RULES_RUNLOCK(); 2421 } else 2422 error = EOPNOTSUPP; 2423 break; 2424 } 2425 2426 case DIOCGETSTATE: { 2427 struct pfioc_state *ps = (struct pfioc_state *)addr; 2428 struct pf_state *s; 2429 2430 s = pf_find_state_byid(ps->state.id, ps->state.creatorid); 2431 if (s == NULL) { 2432 error = ENOENT; 2433 break; 2434 } 2435 2436 pfsync_state_export(&ps->state, s); 2437 PF_STATE_UNLOCK(s); 2438 break; 2439 } 2440 2441 case DIOCGETSTATES: { 2442 struct pfioc_states *ps = (struct pfioc_states *)addr; 2443 struct pf_state *s; 2444 struct pfsync_state *pstore, *p; 2445 int i, nr; 2446 2447 if (ps->ps_len <= 0) { 2448 nr = uma_zone_get_cur(V_pf_state_z); 2449 ps->ps_len = sizeof(struct pfsync_state) * nr; 2450 break; 2451 } 2452 2453 p = pstore = malloc(ps->ps_len, M_TEMP, M_WAITOK | M_ZERO); 2454 nr = 0; 2455 2456 for (i = 0; i <= pf_hashmask; i++) { 2457 struct pf_idhash *ih = &V_pf_idhash[i]; 2458 2459 PF_HASHROW_LOCK(ih); 2460 LIST_FOREACH(s, &ih->states, entry) { 2461 if (s->timeout == PFTM_UNLINKED) 2462 continue; 2463 2464 if ((nr+1) * sizeof(*p) > ps->ps_len) { 2465 PF_HASHROW_UNLOCK(ih); 2466 goto DIOCGETSTATES_full; 2467 } 2468 pfsync_state_export(p, s); 2469 p++; 2470 nr++; 2471 } 2472 PF_HASHROW_UNLOCK(ih); 2473 } 2474 DIOCGETSTATES_full: 2475 error = copyout(pstore, ps->ps_states, 2476 sizeof(struct pfsync_state) * nr); 2477 if (error) { 2478 free(pstore, M_TEMP); 2479 break; 2480 } 2481 ps->ps_len = sizeof(struct pfsync_state) * nr; 2482 free(pstore, M_TEMP); 2483 2484 break; 2485 } 2486 2487 case DIOCGETSTATUS: { 2488 struct pf_status *s = (struct pf_status *)addr; 2489 2490 PF_RULES_RLOCK(); 2491 s->running = V_pf_status.running; 2492 s->since = V_pf_status.since; 2493 s->debug = V_pf_status.debug; 2494 s->hostid = V_pf_status.hostid; 2495 s->states = V_pf_status.states; 2496 s->src_nodes = V_pf_status.src_nodes; 2497 2498 for (int i = 0; i < PFRES_MAX; i++) 2499 s->counters[i] = 2500 counter_u64_fetch(V_pf_status.counters[i]); 2501 for (int i = 0; i < LCNT_MAX; i++) 2502 s->lcounters[i] = 2503 counter_u64_fetch(V_pf_status.lcounters[i]); 2504 for (int i = 0; i < FCNT_MAX; i++) 2505 s->fcounters[i] = 2506 counter_u64_fetch(V_pf_status.fcounters[i]); 2507 for (int i = 0; i < SCNT_MAX; i++) 2508 s->scounters[i] = 2509 counter_u64_fetch(V_pf_status.scounters[i]); 2510 2511 bcopy(V_pf_status.ifname, s->ifname, IFNAMSIZ); 2512 bcopy(V_pf_status.pf_chksum, s->pf_chksum, 2513 PF_MD5_DIGEST_LENGTH); 2514 2515 pfi_update_status(s->ifname, s); 2516 PF_RULES_RUNLOCK(); 2517 break; 2518 } 2519 2520 case DIOCSETSTATUSIF: { 2521 struct pfioc_if *pi = (struct pfioc_if *)addr; 2522 2523 if (pi->ifname[0] == 0) { 2524 bzero(V_pf_status.ifname, IFNAMSIZ); 2525 break; 2526 } 2527 PF_RULES_WLOCK(); 2528 strlcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ); 2529 PF_RULES_WUNLOCK(); 2530 break; 2531 } 2532 2533 case DIOCCLRSTATUS: { 2534 PF_RULES_WLOCK(); 2535 for (int i = 0; i < PFRES_MAX; i++) 2536 counter_u64_zero(V_pf_status.counters[i]); 2537 for (int i = 0; i < FCNT_MAX; i++) 2538 counter_u64_zero(V_pf_status.fcounters[i]); 2539 for (int i = 0; i < SCNT_MAX; i++) 2540 counter_u64_zero(V_pf_status.scounters[i]); 2541 for (int i = 0; i < LCNT_MAX; i++) 2542 counter_u64_zero(V_pf_status.lcounters[i]); 2543 V_pf_status.since = time_second; 2544 if (*V_pf_status.ifname) 2545 pfi_update_status(V_pf_status.ifname, NULL); 2546 PF_RULES_WUNLOCK(); 2547 break; 2548 } 2549 2550 case DIOCNATLOOK: { 2551 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr; 2552 struct pf_state_key *sk; 2553 struct pf_state *state; 2554 struct pf_state_key_cmp key; 2555 int m = 0, direction = pnl->direction; 2556 int sidx, didx; 2557 2558 /* NATLOOK src and dst are reversed, so reverse sidx/didx */ 2559 sidx = (direction == PF_IN) ? 1 : 0; 2560 didx = (direction == PF_IN) ? 0 : 1; 2561 2562 if (!pnl->proto || 2563 PF_AZERO(&pnl->saddr, pnl->af) || 2564 PF_AZERO(&pnl->daddr, pnl->af) || 2565 ((pnl->proto == IPPROTO_TCP || 2566 pnl->proto == IPPROTO_UDP) && 2567 (!pnl->dport || !pnl->sport))) 2568 error = EINVAL; 2569 else { 2570 bzero(&key, sizeof(key)); 2571 key.af = pnl->af; 2572 key.proto = pnl->proto; 2573 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af); 2574 key.port[sidx] = pnl->sport; 2575 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af); 2576 key.port[didx] = pnl->dport; 2577 2578 state = pf_find_state_all(&key, direction, &m); 2579 2580 if (m > 1) 2581 error = E2BIG; /* more than one state */ 2582 else if (state != NULL) { 2583 /* XXXGL: not locked read */ 2584 sk = state->key[sidx]; 2585 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af); 2586 pnl->rsport = sk->port[sidx]; 2587 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af); 2588 pnl->rdport = sk->port[didx]; 2589 } else 2590 error = ENOENT; 2591 } 2592 break; 2593 } 2594 2595 case DIOCSETTIMEOUT: { 2596 struct pfioc_tm *pt = (struct pfioc_tm *)addr; 2597 int old; 2598 2599 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX || 2600 pt->seconds < 0) { 2601 error = EINVAL; 2602 break; 2603 } 2604 PF_RULES_WLOCK(); 2605 old = V_pf_default_rule.timeout[pt->timeout]; 2606 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0) 2607 pt->seconds = 1; 2608 V_pf_default_rule.timeout[pt->timeout] = pt->seconds; 2609 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old) 2610 wakeup(pf_purge_thread); 2611 pt->seconds = old; 2612 PF_RULES_WUNLOCK(); 2613 break; 2614 } 2615 2616 case DIOCGETTIMEOUT: { 2617 struct pfioc_tm *pt = (struct pfioc_tm *)addr; 2618 2619 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) { 2620 error = EINVAL; 2621 break; 2622 } 2623 PF_RULES_RLOCK(); 2624 pt->seconds = V_pf_default_rule.timeout[pt->timeout]; 2625 PF_RULES_RUNLOCK(); 2626 break; 2627 } 2628 2629 case DIOCGETLIMIT: { 2630 struct pfioc_limit *pl = (struct pfioc_limit *)addr; 2631 2632 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) { 2633 error = EINVAL; 2634 break; 2635 } 2636 PF_RULES_RLOCK(); 2637 pl->limit = V_pf_limits[pl->index].limit; 2638 PF_RULES_RUNLOCK(); 2639 break; 2640 } 2641 2642 case DIOCSETLIMIT: { 2643 struct pfioc_limit *pl = (struct pfioc_limit *)addr; 2644 int old_limit; 2645 2646 PF_RULES_WLOCK(); 2647 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX || 2648 V_pf_limits[pl->index].zone == NULL) { 2649 PF_RULES_WUNLOCK(); 2650 error = EINVAL; 2651 break; 2652 } 2653 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit); 2654 old_limit = V_pf_limits[pl->index].limit; 2655 V_pf_limits[pl->index].limit = pl->limit; 2656 pl->limit = old_limit; 2657 PF_RULES_WUNLOCK(); 2658 break; 2659 } 2660 2661 case DIOCSETDEBUG: { 2662 u_int32_t *level = (u_int32_t *)addr; 2663 2664 PF_RULES_WLOCK(); 2665 V_pf_status.debug = *level; 2666 PF_RULES_WUNLOCK(); 2667 break; 2668 } 2669 2670 case DIOCCLRRULECTRS: { 2671 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */ 2672 struct pf_kruleset *ruleset = &pf_main_ruleset; 2673 struct pf_krule *rule; 2674 2675 PF_RULES_WLOCK(); 2676 TAILQ_FOREACH(rule, 2677 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) { 2678 counter_u64_zero(rule->evaluations); 2679 for (int i = 0; i < 2; i++) { 2680 counter_u64_zero(rule->packets[i]); 2681 counter_u64_zero(rule->bytes[i]); 2682 } 2683 } 2684 PF_RULES_WUNLOCK(); 2685 break; 2686 } 2687 2688 case DIOCGIFSPEEDV0: 2689 case DIOCGIFSPEEDV1: { 2690 struct pf_ifspeed_v1 *psp = (struct pf_ifspeed_v1 *)addr; 2691 struct pf_ifspeed_v1 ps; 2692 struct ifnet *ifp; 2693 2694 if (psp->ifname[0] != 0) { 2695 /* Can we completely trust user-land? */ 2696 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ); 2697 ifp = ifunit(ps.ifname); 2698 if (ifp != NULL) { 2699 psp->baudrate32 = 2700 (u_int32_t)uqmin(ifp->if_baudrate, UINT_MAX); 2701 if (cmd == DIOCGIFSPEEDV1) 2702 psp->baudrate = ifp->if_baudrate; 2703 } else 2704 error = EINVAL; 2705 } else 2706 error = EINVAL; 2707 break; 2708 } 2709 2710 #ifdef ALTQ 2711 case DIOCSTARTALTQ: { 2712 struct pf_altq *altq; 2713 2714 PF_RULES_WLOCK(); 2715 /* enable all altq interfaces on active list */ 2716 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 2717 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 2718 error = pf_enable_altq(altq); 2719 if (error != 0) 2720 break; 2721 } 2722 } 2723 if (error == 0) 2724 V_pf_altq_running = 1; 2725 PF_RULES_WUNLOCK(); 2726 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n")); 2727 break; 2728 } 2729 2730 case DIOCSTOPALTQ: { 2731 struct pf_altq *altq; 2732 2733 PF_RULES_WLOCK(); 2734 /* disable all altq interfaces on active list */ 2735 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 2736 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 2737 error = pf_disable_altq(altq); 2738 if (error != 0) 2739 break; 2740 } 2741 } 2742 if (error == 0) 2743 V_pf_altq_running = 0; 2744 PF_RULES_WUNLOCK(); 2745 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n")); 2746 break; 2747 } 2748 2749 case DIOCADDALTQV0: 2750 case DIOCADDALTQV1: { 2751 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 2752 struct pf_altq *altq, *a; 2753 struct ifnet *ifp; 2754 2755 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK | M_ZERO); 2756 error = pf_import_kaltq(pa, altq, IOCPARM_LEN(cmd)); 2757 if (error) 2758 break; 2759 altq->local_flags = 0; 2760 2761 PF_RULES_WLOCK(); 2762 if (pa->ticket != V_ticket_altqs_inactive) { 2763 PF_RULES_WUNLOCK(); 2764 free(altq, M_PFALTQ); 2765 error = EBUSY; 2766 break; 2767 } 2768 2769 /* 2770 * if this is for a queue, find the discipline and 2771 * copy the necessary fields 2772 */ 2773 if (altq->qname[0] != 0) { 2774 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) { 2775 PF_RULES_WUNLOCK(); 2776 error = EBUSY; 2777 free(altq, M_PFALTQ); 2778 break; 2779 } 2780 altq->altq_disc = NULL; 2781 TAILQ_FOREACH(a, V_pf_altq_ifs_inactive, entries) { 2782 if (strncmp(a->ifname, altq->ifname, 2783 IFNAMSIZ) == 0) { 2784 altq->altq_disc = a->altq_disc; 2785 break; 2786 } 2787 } 2788 } 2789 2790 if ((ifp = ifunit(altq->ifname)) == NULL) 2791 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED; 2792 else 2793 error = altq_add(ifp, altq); 2794 2795 if (error) { 2796 PF_RULES_WUNLOCK(); 2797 free(altq, M_PFALTQ); 2798 break; 2799 } 2800 2801 if (altq->qname[0] != 0) 2802 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries); 2803 else 2804 TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, altq, entries); 2805 /* version error check done on import above */ 2806 pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd)); 2807 PF_RULES_WUNLOCK(); 2808 break; 2809 } 2810 2811 case DIOCGETALTQSV0: 2812 case DIOCGETALTQSV1: { 2813 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 2814 struct pf_altq *altq; 2815 2816 PF_RULES_RLOCK(); 2817 pa->nr = 0; 2818 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) 2819 pa->nr++; 2820 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) 2821 pa->nr++; 2822 pa->ticket = V_ticket_altqs_active; 2823 PF_RULES_RUNLOCK(); 2824 break; 2825 } 2826 2827 case DIOCGETALTQV0: 2828 case DIOCGETALTQV1: { 2829 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 2830 struct pf_altq *altq; 2831 2832 PF_RULES_RLOCK(); 2833 if (pa->ticket != V_ticket_altqs_active) { 2834 PF_RULES_RUNLOCK(); 2835 error = EBUSY; 2836 break; 2837 } 2838 altq = pf_altq_get_nth_active(pa->nr); 2839 if (altq == NULL) { 2840 PF_RULES_RUNLOCK(); 2841 error = EBUSY; 2842 break; 2843 } 2844 pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd)); 2845 PF_RULES_RUNLOCK(); 2846 break; 2847 } 2848 2849 case DIOCCHANGEALTQV0: 2850 case DIOCCHANGEALTQV1: 2851 /* CHANGEALTQ not supported yet! */ 2852 error = ENODEV; 2853 break; 2854 2855 case DIOCGETQSTATSV0: 2856 case DIOCGETQSTATSV1: { 2857 struct pfioc_qstats_v1 *pq = (struct pfioc_qstats_v1 *)addr; 2858 struct pf_altq *altq; 2859 int nbytes; 2860 u_int32_t version; 2861 2862 PF_RULES_RLOCK(); 2863 if (pq->ticket != V_ticket_altqs_active) { 2864 PF_RULES_RUNLOCK(); 2865 error = EBUSY; 2866 break; 2867 } 2868 nbytes = pq->nbytes; 2869 altq = pf_altq_get_nth_active(pq->nr); 2870 if (altq == NULL) { 2871 PF_RULES_RUNLOCK(); 2872 error = EBUSY; 2873 break; 2874 } 2875 2876 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) { 2877 PF_RULES_RUNLOCK(); 2878 error = ENXIO; 2879 break; 2880 } 2881 PF_RULES_RUNLOCK(); 2882 if (cmd == DIOCGETQSTATSV0) 2883 version = 0; /* DIOCGETQSTATSV0 means stats struct v0 */ 2884 else 2885 version = pq->version; 2886 error = altq_getqstats(altq, pq->buf, &nbytes, version); 2887 if (error == 0) { 2888 pq->scheduler = altq->scheduler; 2889 pq->nbytes = nbytes; 2890 } 2891 break; 2892 } 2893 #endif /* ALTQ */ 2894 2895 case DIOCBEGINADDRS: { 2896 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 2897 2898 PF_RULES_WLOCK(); 2899 pf_empty_kpool(&V_pf_pabuf); 2900 pp->ticket = ++V_ticket_pabuf; 2901 PF_RULES_WUNLOCK(); 2902 break; 2903 } 2904 2905 case DIOCADDADDR: { 2906 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 2907 struct pf_kpooladdr *pa; 2908 struct pfi_kkif *kif = NULL; 2909 2910 #ifndef INET 2911 if (pp->af == AF_INET) { 2912 error = EAFNOSUPPORT; 2913 break; 2914 } 2915 #endif /* INET */ 2916 #ifndef INET6 2917 if (pp->af == AF_INET6) { 2918 error = EAFNOSUPPORT; 2919 break; 2920 } 2921 #endif /* INET6 */ 2922 if (pp->addr.addr.type != PF_ADDR_ADDRMASK && 2923 pp->addr.addr.type != PF_ADDR_DYNIFTL && 2924 pp->addr.addr.type != PF_ADDR_TABLE) { 2925 error = EINVAL; 2926 break; 2927 } 2928 if (pp->addr.addr.p.dyn != NULL) { 2929 error = EINVAL; 2930 break; 2931 } 2932 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK); 2933 pf_pooladdr_to_kpooladdr(&pp->addr, pa); 2934 if (pa->ifname[0]) 2935 kif = pf_kkif_create(M_WAITOK); 2936 PF_RULES_WLOCK(); 2937 if (pp->ticket != V_ticket_pabuf) { 2938 PF_RULES_WUNLOCK(); 2939 if (pa->ifname[0]) 2940 pf_kkif_free(kif); 2941 free(pa, M_PFRULE); 2942 error = EBUSY; 2943 break; 2944 } 2945 if (pa->ifname[0]) { 2946 pa->kif = pfi_kkif_attach(kif, pa->ifname); 2947 pfi_kkif_ref(pa->kif); 2948 } else 2949 pa->kif = NULL; 2950 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error = 2951 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) { 2952 if (pa->ifname[0]) 2953 pfi_kkif_unref(pa->kif); 2954 PF_RULES_WUNLOCK(); 2955 free(pa, M_PFRULE); 2956 break; 2957 } 2958 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries); 2959 PF_RULES_WUNLOCK(); 2960 break; 2961 } 2962 2963 case DIOCGETADDRS: { 2964 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 2965 struct pf_kpool *pool; 2966 struct pf_kpooladdr *pa; 2967 2968 PF_RULES_RLOCK(); 2969 pp->nr = 0; 2970 pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action, 2971 pp->r_num, 0, 1, 0); 2972 if (pool == NULL) { 2973 PF_RULES_RUNLOCK(); 2974 error = EBUSY; 2975 break; 2976 } 2977 TAILQ_FOREACH(pa, &pool->list, entries) 2978 pp->nr++; 2979 PF_RULES_RUNLOCK(); 2980 break; 2981 } 2982 2983 case DIOCGETADDR: { 2984 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 2985 struct pf_kpool *pool; 2986 struct pf_kpooladdr *pa; 2987 u_int32_t nr = 0; 2988 2989 PF_RULES_RLOCK(); 2990 pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action, 2991 pp->r_num, 0, 1, 1); 2992 if (pool == NULL) { 2993 PF_RULES_RUNLOCK(); 2994 error = EBUSY; 2995 break; 2996 } 2997 pa = TAILQ_FIRST(&pool->list); 2998 while ((pa != NULL) && (nr < pp->nr)) { 2999 pa = TAILQ_NEXT(pa, entries); 3000 nr++; 3001 } 3002 if (pa == NULL) { 3003 PF_RULES_RUNLOCK(); 3004 error = EBUSY; 3005 break; 3006 } 3007 pf_kpooladdr_to_pooladdr(pa, &pp->addr); 3008 pf_addr_copyout(&pp->addr.addr); 3009 PF_RULES_RUNLOCK(); 3010 break; 3011 } 3012 3013 case DIOCCHANGEADDR: { 3014 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr; 3015 struct pf_kpool *pool; 3016 struct pf_kpooladdr *oldpa = NULL, *newpa = NULL; 3017 struct pf_kruleset *ruleset; 3018 struct pfi_kkif *kif = NULL; 3019 3020 if (pca->action < PF_CHANGE_ADD_HEAD || 3021 pca->action > PF_CHANGE_REMOVE) { 3022 error = EINVAL; 3023 break; 3024 } 3025 if (pca->addr.addr.type != PF_ADDR_ADDRMASK && 3026 pca->addr.addr.type != PF_ADDR_DYNIFTL && 3027 pca->addr.addr.type != PF_ADDR_TABLE) { 3028 error = EINVAL; 3029 break; 3030 } 3031 if (pca->addr.addr.p.dyn != NULL) { 3032 error = EINVAL; 3033 break; 3034 } 3035 3036 if (pca->action != PF_CHANGE_REMOVE) { 3037 #ifndef INET 3038 if (pca->af == AF_INET) { 3039 error = EAFNOSUPPORT; 3040 break; 3041 } 3042 #endif /* INET */ 3043 #ifndef INET6 3044 if (pca->af == AF_INET6) { 3045 error = EAFNOSUPPORT; 3046 break; 3047 } 3048 #endif /* INET6 */ 3049 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK); 3050 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr)); 3051 if (newpa->ifname[0]) 3052 kif = pf_kkif_create(M_WAITOK); 3053 newpa->kif = NULL; 3054 } 3055 #define ERROUT(x) { error = (x); goto DIOCCHANGEADDR_error; } 3056 PF_RULES_WLOCK(); 3057 ruleset = pf_find_kruleset(pca->anchor); 3058 if (ruleset == NULL) 3059 ERROUT(EBUSY); 3060 3061 pool = pf_get_kpool(pca->anchor, pca->ticket, pca->r_action, 3062 pca->r_num, pca->r_last, 1, 1); 3063 if (pool == NULL) 3064 ERROUT(EBUSY); 3065 3066 if (pca->action != PF_CHANGE_REMOVE) { 3067 if (newpa->ifname[0]) { 3068 newpa->kif = pfi_kkif_attach(kif, newpa->ifname); 3069 pfi_kkif_ref(newpa->kif); 3070 kif = NULL; 3071 } 3072 3073 switch (newpa->addr.type) { 3074 case PF_ADDR_DYNIFTL: 3075 error = pfi_dynaddr_setup(&newpa->addr, 3076 pca->af); 3077 break; 3078 case PF_ADDR_TABLE: 3079 newpa->addr.p.tbl = pfr_attach_table(ruleset, 3080 newpa->addr.v.tblname); 3081 if (newpa->addr.p.tbl == NULL) 3082 error = ENOMEM; 3083 break; 3084 } 3085 if (error) 3086 goto DIOCCHANGEADDR_error; 3087 } 3088 3089 switch (pca->action) { 3090 case PF_CHANGE_ADD_HEAD: 3091 oldpa = TAILQ_FIRST(&pool->list); 3092 break; 3093 case PF_CHANGE_ADD_TAIL: 3094 oldpa = TAILQ_LAST(&pool->list, pf_kpalist); 3095 break; 3096 default: 3097 oldpa = TAILQ_FIRST(&pool->list); 3098 for (int i = 0; oldpa && i < pca->nr; i++) 3099 oldpa = TAILQ_NEXT(oldpa, entries); 3100 3101 if (oldpa == NULL) 3102 ERROUT(EINVAL); 3103 } 3104 3105 if (pca->action == PF_CHANGE_REMOVE) { 3106 TAILQ_REMOVE(&pool->list, oldpa, entries); 3107 switch (oldpa->addr.type) { 3108 case PF_ADDR_DYNIFTL: 3109 pfi_dynaddr_remove(oldpa->addr.p.dyn); 3110 break; 3111 case PF_ADDR_TABLE: 3112 pfr_detach_table(oldpa->addr.p.tbl); 3113 break; 3114 } 3115 if (oldpa->kif) 3116 pfi_kkif_unref(oldpa->kif); 3117 free(oldpa, M_PFRULE); 3118 } else { 3119 if (oldpa == NULL) 3120 TAILQ_INSERT_TAIL(&pool->list, newpa, entries); 3121 else if (pca->action == PF_CHANGE_ADD_HEAD || 3122 pca->action == PF_CHANGE_ADD_BEFORE) 3123 TAILQ_INSERT_BEFORE(oldpa, newpa, entries); 3124 else 3125 TAILQ_INSERT_AFTER(&pool->list, oldpa, 3126 newpa, entries); 3127 } 3128 3129 pool->cur = TAILQ_FIRST(&pool->list); 3130 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af); 3131 PF_RULES_WUNLOCK(); 3132 break; 3133 3134 #undef ERROUT 3135 DIOCCHANGEADDR_error: 3136 if (newpa != NULL) { 3137 if (newpa->kif) 3138 pfi_kkif_unref(newpa->kif); 3139 free(newpa, M_PFRULE); 3140 } 3141 PF_RULES_WUNLOCK(); 3142 if (kif != NULL) 3143 pf_kkif_free(kif); 3144 break; 3145 } 3146 3147 case DIOCGETRULESETS: { 3148 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr; 3149 struct pf_kruleset *ruleset; 3150 struct pf_kanchor *anchor; 3151 3152 PF_RULES_RLOCK(); 3153 pr->path[sizeof(pr->path) - 1] = 0; 3154 if ((ruleset = pf_find_kruleset(pr->path)) == NULL) { 3155 PF_RULES_RUNLOCK(); 3156 error = ENOENT; 3157 break; 3158 } 3159 pr->nr = 0; 3160 if (ruleset->anchor == NULL) { 3161 /* XXX kludge for pf_main_ruleset */ 3162 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) 3163 if (anchor->parent == NULL) 3164 pr->nr++; 3165 } else { 3166 RB_FOREACH(anchor, pf_kanchor_node, 3167 &ruleset->anchor->children) 3168 pr->nr++; 3169 } 3170 PF_RULES_RUNLOCK(); 3171 break; 3172 } 3173 3174 case DIOCGETRULESET: { 3175 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr; 3176 struct pf_kruleset *ruleset; 3177 struct pf_kanchor *anchor; 3178 u_int32_t nr = 0; 3179 3180 PF_RULES_RLOCK(); 3181 pr->path[sizeof(pr->path) - 1] = 0; 3182 if ((ruleset = pf_find_kruleset(pr->path)) == NULL) { 3183 PF_RULES_RUNLOCK(); 3184 error = ENOENT; 3185 break; 3186 } 3187 pr->name[0] = 0; 3188 if (ruleset->anchor == NULL) { 3189 /* XXX kludge for pf_main_ruleset */ 3190 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) 3191 if (anchor->parent == NULL && nr++ == pr->nr) { 3192 strlcpy(pr->name, anchor->name, 3193 sizeof(pr->name)); 3194 break; 3195 } 3196 } else { 3197 RB_FOREACH(anchor, pf_kanchor_node, 3198 &ruleset->anchor->children) 3199 if (nr++ == pr->nr) { 3200 strlcpy(pr->name, anchor->name, 3201 sizeof(pr->name)); 3202 break; 3203 } 3204 } 3205 if (!pr->name[0]) 3206 error = EBUSY; 3207 PF_RULES_RUNLOCK(); 3208 break; 3209 } 3210 3211 case DIOCRCLRTABLES: { 3212 struct pfioc_table *io = (struct pfioc_table *)addr; 3213 3214 if (io->pfrio_esize != 0) { 3215 error = ENODEV; 3216 break; 3217 } 3218 PF_RULES_WLOCK(); 3219 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel, 3220 io->pfrio_flags | PFR_FLAG_USERIOCTL); 3221 PF_RULES_WUNLOCK(); 3222 break; 3223 } 3224 3225 case DIOCRADDTABLES: { 3226 struct pfioc_table *io = (struct pfioc_table *)addr; 3227 struct pfr_table *pfrts; 3228 size_t totlen; 3229 3230 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3231 error = ENODEV; 3232 break; 3233 } 3234 3235 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 3236 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 3237 error = ENOMEM; 3238 break; 3239 } 3240 3241 totlen = io->pfrio_size * sizeof(struct pfr_table); 3242 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3243 M_TEMP, M_WAITOK); 3244 error = copyin(io->pfrio_buffer, pfrts, totlen); 3245 if (error) { 3246 free(pfrts, M_TEMP); 3247 break; 3248 } 3249 PF_RULES_WLOCK(); 3250 error = pfr_add_tables(pfrts, io->pfrio_size, 3251 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3252 PF_RULES_WUNLOCK(); 3253 free(pfrts, M_TEMP); 3254 break; 3255 } 3256 3257 case DIOCRDELTABLES: { 3258 struct pfioc_table *io = (struct pfioc_table *)addr; 3259 struct pfr_table *pfrts; 3260 size_t totlen; 3261 3262 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3263 error = ENODEV; 3264 break; 3265 } 3266 3267 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 3268 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 3269 error = ENOMEM; 3270 break; 3271 } 3272 3273 totlen = io->pfrio_size * sizeof(struct pfr_table); 3274 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3275 M_TEMP, M_WAITOK); 3276 error = copyin(io->pfrio_buffer, pfrts, totlen); 3277 if (error) { 3278 free(pfrts, M_TEMP); 3279 break; 3280 } 3281 PF_RULES_WLOCK(); 3282 error = pfr_del_tables(pfrts, io->pfrio_size, 3283 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3284 PF_RULES_WUNLOCK(); 3285 free(pfrts, M_TEMP); 3286 break; 3287 } 3288 3289 case DIOCRGETTABLES: { 3290 struct pfioc_table *io = (struct pfioc_table *)addr; 3291 struct pfr_table *pfrts; 3292 size_t totlen; 3293 int n; 3294 3295 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3296 error = ENODEV; 3297 break; 3298 } 3299 PF_RULES_RLOCK(); 3300 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 3301 if (n < 0) { 3302 PF_RULES_RUNLOCK(); 3303 error = EINVAL; 3304 break; 3305 } 3306 io->pfrio_size = min(io->pfrio_size, n); 3307 3308 totlen = io->pfrio_size * sizeof(struct pfr_table); 3309 3310 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3311 M_TEMP, M_NOWAIT); 3312 if (pfrts == NULL) { 3313 error = ENOMEM; 3314 PF_RULES_RUNLOCK(); 3315 break; 3316 } 3317 error = pfr_get_tables(&io->pfrio_table, pfrts, 3318 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3319 PF_RULES_RUNLOCK(); 3320 if (error == 0) 3321 error = copyout(pfrts, io->pfrio_buffer, totlen); 3322 free(pfrts, M_TEMP); 3323 break; 3324 } 3325 3326 case DIOCRGETTSTATS: { 3327 struct pfioc_table *io = (struct pfioc_table *)addr; 3328 struct pfr_tstats *pfrtstats; 3329 size_t totlen; 3330 int n; 3331 3332 if (io->pfrio_esize != sizeof(struct pfr_tstats)) { 3333 error = ENODEV; 3334 break; 3335 } 3336 PF_RULES_WLOCK(); 3337 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 3338 if (n < 0) { 3339 PF_RULES_WUNLOCK(); 3340 error = EINVAL; 3341 break; 3342 } 3343 io->pfrio_size = min(io->pfrio_size, n); 3344 3345 totlen = io->pfrio_size * sizeof(struct pfr_tstats); 3346 pfrtstats = mallocarray(io->pfrio_size, 3347 sizeof(struct pfr_tstats), M_TEMP, M_NOWAIT); 3348 if (pfrtstats == NULL) { 3349 error = ENOMEM; 3350 PF_RULES_WUNLOCK(); 3351 break; 3352 } 3353 error = pfr_get_tstats(&io->pfrio_table, pfrtstats, 3354 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3355 PF_RULES_WUNLOCK(); 3356 if (error == 0) 3357 error = copyout(pfrtstats, io->pfrio_buffer, totlen); 3358 free(pfrtstats, M_TEMP); 3359 break; 3360 } 3361 3362 case DIOCRCLRTSTATS: { 3363 struct pfioc_table *io = (struct pfioc_table *)addr; 3364 struct pfr_table *pfrts; 3365 size_t totlen; 3366 3367 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3368 error = ENODEV; 3369 break; 3370 } 3371 3372 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 3373 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 3374 /* We used to count tables and use the minimum required 3375 * size, so we didn't fail on overly large requests. 3376 * Keep doing so. */ 3377 io->pfrio_size = pf_ioctl_maxcount; 3378 break; 3379 } 3380 3381 totlen = io->pfrio_size * sizeof(struct pfr_table); 3382 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3383 M_TEMP, M_NOWAIT); 3384 if (pfrts == NULL) { 3385 error = ENOMEM; 3386 break; 3387 } 3388 error = copyin(io->pfrio_buffer, pfrts, totlen); 3389 if (error) { 3390 free(pfrts, M_TEMP); 3391 break; 3392 } 3393 3394 PF_RULES_WLOCK(); 3395 error = pfr_clr_tstats(pfrts, io->pfrio_size, 3396 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3397 PF_RULES_WUNLOCK(); 3398 free(pfrts, M_TEMP); 3399 break; 3400 } 3401 3402 case DIOCRSETTFLAGS: { 3403 struct pfioc_table *io = (struct pfioc_table *)addr; 3404 struct pfr_table *pfrts; 3405 size_t totlen; 3406 int n; 3407 3408 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3409 error = ENODEV; 3410 break; 3411 } 3412 3413 PF_RULES_RLOCK(); 3414 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 3415 if (n < 0) { 3416 PF_RULES_RUNLOCK(); 3417 error = EINVAL; 3418 break; 3419 } 3420 3421 io->pfrio_size = min(io->pfrio_size, n); 3422 PF_RULES_RUNLOCK(); 3423 3424 totlen = io->pfrio_size * sizeof(struct pfr_table); 3425 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3426 M_TEMP, M_WAITOK); 3427 error = copyin(io->pfrio_buffer, pfrts, totlen); 3428 if (error) { 3429 free(pfrts, M_TEMP); 3430 break; 3431 } 3432 PF_RULES_WLOCK(); 3433 error = pfr_set_tflags(pfrts, io->pfrio_size, 3434 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange, 3435 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3436 PF_RULES_WUNLOCK(); 3437 free(pfrts, M_TEMP); 3438 break; 3439 } 3440 3441 case DIOCRCLRADDRS: { 3442 struct pfioc_table *io = (struct pfioc_table *)addr; 3443 3444 if (io->pfrio_esize != 0) { 3445 error = ENODEV; 3446 break; 3447 } 3448 PF_RULES_WLOCK(); 3449 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel, 3450 io->pfrio_flags | PFR_FLAG_USERIOCTL); 3451 PF_RULES_WUNLOCK(); 3452 break; 3453 } 3454 3455 case DIOCRADDADDRS: { 3456 struct pfioc_table *io = (struct pfioc_table *)addr; 3457 struct pfr_addr *pfras; 3458 size_t totlen; 3459 3460 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3461 error = ENODEV; 3462 break; 3463 } 3464 if (io->pfrio_size < 0 || 3465 io->pfrio_size > pf_ioctl_maxcount || 3466 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3467 error = EINVAL; 3468 break; 3469 } 3470 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3471 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3472 M_TEMP, M_NOWAIT); 3473 if (! pfras) { 3474 error = ENOMEM; 3475 break; 3476 } 3477 error = copyin(io->pfrio_buffer, pfras, totlen); 3478 if (error) { 3479 free(pfras, M_TEMP); 3480 break; 3481 } 3482 PF_RULES_WLOCK(); 3483 error = pfr_add_addrs(&io->pfrio_table, pfras, 3484 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags | 3485 PFR_FLAG_USERIOCTL); 3486 PF_RULES_WUNLOCK(); 3487 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 3488 error = copyout(pfras, io->pfrio_buffer, totlen); 3489 free(pfras, M_TEMP); 3490 break; 3491 } 3492 3493 case DIOCRDELADDRS: { 3494 struct pfioc_table *io = (struct pfioc_table *)addr; 3495 struct pfr_addr *pfras; 3496 size_t totlen; 3497 3498 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3499 error = ENODEV; 3500 break; 3501 } 3502 if (io->pfrio_size < 0 || 3503 io->pfrio_size > pf_ioctl_maxcount || 3504 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3505 error = EINVAL; 3506 break; 3507 } 3508 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3509 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3510 M_TEMP, M_NOWAIT); 3511 if (! pfras) { 3512 error = ENOMEM; 3513 break; 3514 } 3515 error = copyin(io->pfrio_buffer, pfras, totlen); 3516 if (error) { 3517 free(pfras, M_TEMP); 3518 break; 3519 } 3520 PF_RULES_WLOCK(); 3521 error = pfr_del_addrs(&io->pfrio_table, pfras, 3522 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags | 3523 PFR_FLAG_USERIOCTL); 3524 PF_RULES_WUNLOCK(); 3525 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 3526 error = copyout(pfras, io->pfrio_buffer, totlen); 3527 free(pfras, M_TEMP); 3528 break; 3529 } 3530 3531 case DIOCRSETADDRS: { 3532 struct pfioc_table *io = (struct pfioc_table *)addr; 3533 struct pfr_addr *pfras; 3534 size_t totlen, count; 3535 3536 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3537 error = ENODEV; 3538 break; 3539 } 3540 if (io->pfrio_size < 0 || io->pfrio_size2 < 0) { 3541 error = EINVAL; 3542 break; 3543 } 3544 count = max(io->pfrio_size, io->pfrio_size2); 3545 if (count > pf_ioctl_maxcount || 3546 WOULD_OVERFLOW(count, sizeof(struct pfr_addr))) { 3547 error = EINVAL; 3548 break; 3549 } 3550 totlen = count * sizeof(struct pfr_addr); 3551 pfras = mallocarray(count, sizeof(struct pfr_addr), M_TEMP, 3552 M_NOWAIT); 3553 if (! pfras) { 3554 error = ENOMEM; 3555 break; 3556 } 3557 error = copyin(io->pfrio_buffer, pfras, totlen); 3558 if (error) { 3559 free(pfras, M_TEMP); 3560 break; 3561 } 3562 PF_RULES_WLOCK(); 3563 error = pfr_set_addrs(&io->pfrio_table, pfras, 3564 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd, 3565 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags | 3566 PFR_FLAG_USERIOCTL, 0); 3567 PF_RULES_WUNLOCK(); 3568 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 3569 error = copyout(pfras, io->pfrio_buffer, totlen); 3570 free(pfras, M_TEMP); 3571 break; 3572 } 3573 3574 case DIOCRGETADDRS: { 3575 struct pfioc_table *io = (struct pfioc_table *)addr; 3576 struct pfr_addr *pfras; 3577 size_t totlen; 3578 3579 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3580 error = ENODEV; 3581 break; 3582 } 3583 if (io->pfrio_size < 0 || 3584 io->pfrio_size > pf_ioctl_maxcount || 3585 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3586 error = EINVAL; 3587 break; 3588 } 3589 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3590 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3591 M_TEMP, M_NOWAIT); 3592 if (! pfras) { 3593 error = ENOMEM; 3594 break; 3595 } 3596 PF_RULES_RLOCK(); 3597 error = pfr_get_addrs(&io->pfrio_table, pfras, 3598 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3599 PF_RULES_RUNLOCK(); 3600 if (error == 0) 3601 error = copyout(pfras, io->pfrio_buffer, totlen); 3602 free(pfras, M_TEMP); 3603 break; 3604 } 3605 3606 case DIOCRGETASTATS: { 3607 struct pfioc_table *io = (struct pfioc_table *)addr; 3608 struct pfr_astats *pfrastats; 3609 size_t totlen; 3610 3611 if (io->pfrio_esize != sizeof(struct pfr_astats)) { 3612 error = ENODEV; 3613 break; 3614 } 3615 if (io->pfrio_size < 0 || 3616 io->pfrio_size > pf_ioctl_maxcount || 3617 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_astats))) { 3618 error = EINVAL; 3619 break; 3620 } 3621 totlen = io->pfrio_size * sizeof(struct pfr_astats); 3622 pfrastats = mallocarray(io->pfrio_size, 3623 sizeof(struct pfr_astats), M_TEMP, M_NOWAIT); 3624 if (! pfrastats) { 3625 error = ENOMEM; 3626 break; 3627 } 3628 PF_RULES_RLOCK(); 3629 error = pfr_get_astats(&io->pfrio_table, pfrastats, 3630 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3631 PF_RULES_RUNLOCK(); 3632 if (error == 0) 3633 error = copyout(pfrastats, io->pfrio_buffer, totlen); 3634 free(pfrastats, M_TEMP); 3635 break; 3636 } 3637 3638 case DIOCRCLRASTATS: { 3639 struct pfioc_table *io = (struct pfioc_table *)addr; 3640 struct pfr_addr *pfras; 3641 size_t totlen; 3642 3643 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3644 error = ENODEV; 3645 break; 3646 } 3647 if (io->pfrio_size < 0 || 3648 io->pfrio_size > pf_ioctl_maxcount || 3649 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3650 error = EINVAL; 3651 break; 3652 } 3653 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3654 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3655 M_TEMP, M_NOWAIT); 3656 if (! pfras) { 3657 error = ENOMEM; 3658 break; 3659 } 3660 error = copyin(io->pfrio_buffer, pfras, totlen); 3661 if (error) { 3662 free(pfras, M_TEMP); 3663 break; 3664 } 3665 PF_RULES_WLOCK(); 3666 error = pfr_clr_astats(&io->pfrio_table, pfras, 3667 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags | 3668 PFR_FLAG_USERIOCTL); 3669 PF_RULES_WUNLOCK(); 3670 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 3671 error = copyout(pfras, io->pfrio_buffer, totlen); 3672 free(pfras, M_TEMP); 3673 break; 3674 } 3675 3676 case DIOCRTSTADDRS: { 3677 struct pfioc_table *io = (struct pfioc_table *)addr; 3678 struct pfr_addr *pfras; 3679 size_t totlen; 3680 3681 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3682 error = ENODEV; 3683 break; 3684 } 3685 if (io->pfrio_size < 0 || 3686 io->pfrio_size > pf_ioctl_maxcount || 3687 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3688 error = EINVAL; 3689 break; 3690 } 3691 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3692 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3693 M_TEMP, M_NOWAIT); 3694 if (! pfras) { 3695 error = ENOMEM; 3696 break; 3697 } 3698 error = copyin(io->pfrio_buffer, pfras, totlen); 3699 if (error) { 3700 free(pfras, M_TEMP); 3701 break; 3702 } 3703 PF_RULES_RLOCK(); 3704 error = pfr_tst_addrs(&io->pfrio_table, pfras, 3705 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags | 3706 PFR_FLAG_USERIOCTL); 3707 PF_RULES_RUNLOCK(); 3708 if (error == 0) 3709 error = copyout(pfras, io->pfrio_buffer, totlen); 3710 free(pfras, M_TEMP); 3711 break; 3712 } 3713 3714 case DIOCRINADEFINE: { 3715 struct pfioc_table *io = (struct pfioc_table *)addr; 3716 struct pfr_addr *pfras; 3717 size_t totlen; 3718 3719 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3720 error = ENODEV; 3721 break; 3722 } 3723 if (io->pfrio_size < 0 || 3724 io->pfrio_size > pf_ioctl_maxcount || 3725 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3726 error = EINVAL; 3727 break; 3728 } 3729 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3730 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3731 M_TEMP, M_NOWAIT); 3732 if (! pfras) { 3733 error = ENOMEM; 3734 break; 3735 } 3736 error = copyin(io->pfrio_buffer, pfras, totlen); 3737 if (error) { 3738 free(pfras, M_TEMP); 3739 break; 3740 } 3741 PF_RULES_WLOCK(); 3742 error = pfr_ina_define(&io->pfrio_table, pfras, 3743 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr, 3744 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3745 PF_RULES_WUNLOCK(); 3746 free(pfras, M_TEMP); 3747 break; 3748 } 3749 3750 case DIOCOSFPADD: { 3751 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr; 3752 PF_RULES_WLOCK(); 3753 error = pf_osfp_add(io); 3754 PF_RULES_WUNLOCK(); 3755 break; 3756 } 3757 3758 case DIOCOSFPGET: { 3759 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr; 3760 PF_RULES_RLOCK(); 3761 error = pf_osfp_get(io); 3762 PF_RULES_RUNLOCK(); 3763 break; 3764 } 3765 3766 case DIOCXBEGIN: { 3767 struct pfioc_trans *io = (struct pfioc_trans *)addr; 3768 struct pfioc_trans_e *ioes, *ioe; 3769 size_t totlen; 3770 int i; 3771 3772 if (io->esize != sizeof(*ioe)) { 3773 error = ENODEV; 3774 break; 3775 } 3776 if (io->size < 0 || 3777 io->size > pf_ioctl_maxcount || 3778 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 3779 error = EINVAL; 3780 break; 3781 } 3782 totlen = sizeof(struct pfioc_trans_e) * io->size; 3783 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 3784 M_TEMP, M_NOWAIT); 3785 if (! ioes) { 3786 error = ENOMEM; 3787 break; 3788 } 3789 error = copyin(io->array, ioes, totlen); 3790 if (error) { 3791 free(ioes, M_TEMP); 3792 break; 3793 } 3794 PF_RULES_WLOCK(); 3795 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 3796 switch (ioe->rs_num) { 3797 #ifdef ALTQ 3798 case PF_RULESET_ALTQ: 3799 if (ioe->anchor[0]) { 3800 PF_RULES_WUNLOCK(); 3801 free(ioes, M_TEMP); 3802 error = EINVAL; 3803 goto fail; 3804 } 3805 if ((error = pf_begin_altq(&ioe->ticket))) { 3806 PF_RULES_WUNLOCK(); 3807 free(ioes, M_TEMP); 3808 goto fail; 3809 } 3810 break; 3811 #endif /* ALTQ */ 3812 case PF_RULESET_TABLE: 3813 { 3814 struct pfr_table table; 3815 3816 bzero(&table, sizeof(table)); 3817 strlcpy(table.pfrt_anchor, ioe->anchor, 3818 sizeof(table.pfrt_anchor)); 3819 if ((error = pfr_ina_begin(&table, 3820 &ioe->ticket, NULL, 0))) { 3821 PF_RULES_WUNLOCK(); 3822 free(ioes, M_TEMP); 3823 goto fail; 3824 } 3825 break; 3826 } 3827 default: 3828 if ((error = pf_begin_rules(&ioe->ticket, 3829 ioe->rs_num, ioe->anchor))) { 3830 PF_RULES_WUNLOCK(); 3831 free(ioes, M_TEMP); 3832 goto fail; 3833 } 3834 break; 3835 } 3836 } 3837 PF_RULES_WUNLOCK(); 3838 error = copyout(ioes, io->array, totlen); 3839 free(ioes, M_TEMP); 3840 break; 3841 } 3842 3843 case DIOCXROLLBACK: { 3844 struct pfioc_trans *io = (struct pfioc_trans *)addr; 3845 struct pfioc_trans_e *ioe, *ioes; 3846 size_t totlen; 3847 int i; 3848 3849 if (io->esize != sizeof(*ioe)) { 3850 error = ENODEV; 3851 break; 3852 } 3853 if (io->size < 0 || 3854 io->size > pf_ioctl_maxcount || 3855 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 3856 error = EINVAL; 3857 break; 3858 } 3859 totlen = sizeof(struct pfioc_trans_e) * io->size; 3860 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 3861 M_TEMP, M_NOWAIT); 3862 if (! ioes) { 3863 error = ENOMEM; 3864 break; 3865 } 3866 error = copyin(io->array, ioes, totlen); 3867 if (error) { 3868 free(ioes, M_TEMP); 3869 break; 3870 } 3871 PF_RULES_WLOCK(); 3872 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 3873 switch (ioe->rs_num) { 3874 #ifdef ALTQ 3875 case PF_RULESET_ALTQ: 3876 if (ioe->anchor[0]) { 3877 PF_RULES_WUNLOCK(); 3878 free(ioes, M_TEMP); 3879 error = EINVAL; 3880 goto fail; 3881 } 3882 if ((error = pf_rollback_altq(ioe->ticket))) { 3883 PF_RULES_WUNLOCK(); 3884 free(ioes, M_TEMP); 3885 goto fail; /* really bad */ 3886 } 3887 break; 3888 #endif /* ALTQ */ 3889 case PF_RULESET_TABLE: 3890 { 3891 struct pfr_table table; 3892 3893 bzero(&table, sizeof(table)); 3894 strlcpy(table.pfrt_anchor, ioe->anchor, 3895 sizeof(table.pfrt_anchor)); 3896 if ((error = pfr_ina_rollback(&table, 3897 ioe->ticket, NULL, 0))) { 3898 PF_RULES_WUNLOCK(); 3899 free(ioes, M_TEMP); 3900 goto fail; /* really bad */ 3901 } 3902 break; 3903 } 3904 default: 3905 if ((error = pf_rollback_rules(ioe->ticket, 3906 ioe->rs_num, ioe->anchor))) { 3907 PF_RULES_WUNLOCK(); 3908 free(ioes, M_TEMP); 3909 goto fail; /* really bad */ 3910 } 3911 break; 3912 } 3913 } 3914 PF_RULES_WUNLOCK(); 3915 free(ioes, M_TEMP); 3916 break; 3917 } 3918 3919 case DIOCXCOMMIT: { 3920 struct pfioc_trans *io = (struct pfioc_trans *)addr; 3921 struct pfioc_trans_e *ioe, *ioes; 3922 struct pf_kruleset *rs; 3923 size_t totlen; 3924 int i; 3925 3926 if (io->esize != sizeof(*ioe)) { 3927 error = ENODEV; 3928 break; 3929 } 3930 3931 if (io->size < 0 || 3932 io->size > pf_ioctl_maxcount || 3933 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 3934 error = EINVAL; 3935 break; 3936 } 3937 3938 totlen = sizeof(struct pfioc_trans_e) * io->size; 3939 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 3940 M_TEMP, M_NOWAIT); 3941 if (ioes == NULL) { 3942 error = ENOMEM; 3943 break; 3944 } 3945 error = copyin(io->array, ioes, totlen); 3946 if (error) { 3947 free(ioes, M_TEMP); 3948 break; 3949 } 3950 PF_RULES_WLOCK(); 3951 /* First makes sure everything will succeed. */ 3952 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 3953 switch (ioe->rs_num) { 3954 #ifdef ALTQ 3955 case PF_RULESET_ALTQ: 3956 if (ioe->anchor[0]) { 3957 PF_RULES_WUNLOCK(); 3958 free(ioes, M_TEMP); 3959 error = EINVAL; 3960 goto fail; 3961 } 3962 if (!V_altqs_inactive_open || ioe->ticket != 3963 V_ticket_altqs_inactive) { 3964 PF_RULES_WUNLOCK(); 3965 free(ioes, M_TEMP); 3966 error = EBUSY; 3967 goto fail; 3968 } 3969 break; 3970 #endif /* ALTQ */ 3971 case PF_RULESET_TABLE: 3972 rs = pf_find_kruleset(ioe->anchor); 3973 if (rs == NULL || !rs->topen || ioe->ticket != 3974 rs->tticket) { 3975 PF_RULES_WUNLOCK(); 3976 free(ioes, M_TEMP); 3977 error = EBUSY; 3978 goto fail; 3979 } 3980 break; 3981 default: 3982 if (ioe->rs_num < 0 || ioe->rs_num >= 3983 PF_RULESET_MAX) { 3984 PF_RULES_WUNLOCK(); 3985 free(ioes, M_TEMP); 3986 error = EINVAL; 3987 goto fail; 3988 } 3989 rs = pf_find_kruleset(ioe->anchor); 3990 if (rs == NULL || 3991 !rs->rules[ioe->rs_num].inactive.open || 3992 rs->rules[ioe->rs_num].inactive.ticket != 3993 ioe->ticket) { 3994 PF_RULES_WUNLOCK(); 3995 free(ioes, M_TEMP); 3996 error = EBUSY; 3997 goto fail; 3998 } 3999 break; 4000 } 4001 } 4002 /* Now do the commit - no errors should happen here. */ 4003 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 4004 switch (ioe->rs_num) { 4005 #ifdef ALTQ 4006 case PF_RULESET_ALTQ: 4007 if ((error = pf_commit_altq(ioe->ticket))) { 4008 PF_RULES_WUNLOCK(); 4009 free(ioes, M_TEMP); 4010 goto fail; /* really bad */ 4011 } 4012 break; 4013 #endif /* ALTQ */ 4014 case PF_RULESET_TABLE: 4015 { 4016 struct pfr_table table; 4017 4018 bzero(&table, sizeof(table)); 4019 strlcpy(table.pfrt_anchor, ioe->anchor, 4020 sizeof(table.pfrt_anchor)); 4021 if ((error = pfr_ina_commit(&table, 4022 ioe->ticket, NULL, NULL, 0))) { 4023 PF_RULES_WUNLOCK(); 4024 free(ioes, M_TEMP); 4025 goto fail; /* really bad */ 4026 } 4027 break; 4028 } 4029 default: 4030 if ((error = pf_commit_rules(ioe->ticket, 4031 ioe->rs_num, ioe->anchor))) { 4032 PF_RULES_WUNLOCK(); 4033 free(ioes, M_TEMP); 4034 goto fail; /* really bad */ 4035 } 4036 break; 4037 } 4038 } 4039 PF_RULES_WUNLOCK(); 4040 free(ioes, M_TEMP); 4041 break; 4042 } 4043 4044 case DIOCGETSRCNODES: { 4045 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr; 4046 struct pf_srchash *sh; 4047 struct pf_ksrc_node *n; 4048 struct pf_src_node *p, *pstore; 4049 uint32_t i, nr = 0; 4050 4051 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 4052 i++, sh++) { 4053 PF_HASHROW_LOCK(sh); 4054 LIST_FOREACH(n, &sh->nodes, entry) 4055 nr++; 4056 PF_HASHROW_UNLOCK(sh); 4057 } 4058 4059 psn->psn_len = min(psn->psn_len, 4060 sizeof(struct pf_src_node) * nr); 4061 4062 if (psn->psn_len == 0) { 4063 psn->psn_len = sizeof(struct pf_src_node) * nr; 4064 break; 4065 } 4066 4067 nr = 0; 4068 4069 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK | M_ZERO); 4070 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 4071 i++, sh++) { 4072 PF_HASHROW_LOCK(sh); 4073 LIST_FOREACH(n, &sh->nodes, entry) { 4074 4075 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len) 4076 break; 4077 4078 pf_src_node_copy(n, p); 4079 4080 p++; 4081 nr++; 4082 } 4083 PF_HASHROW_UNLOCK(sh); 4084 } 4085 error = copyout(pstore, psn->psn_src_nodes, 4086 sizeof(struct pf_src_node) * nr); 4087 if (error) { 4088 free(pstore, M_TEMP); 4089 break; 4090 } 4091 psn->psn_len = sizeof(struct pf_src_node) * nr; 4092 free(pstore, M_TEMP); 4093 break; 4094 } 4095 4096 case DIOCCLRSRCNODES: { 4097 pf_clear_srcnodes(NULL); 4098 pf_purge_expired_src_nodes(); 4099 break; 4100 } 4101 4102 case DIOCKILLSRCNODES: 4103 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr); 4104 break; 4105 4106 case DIOCSETHOSTID: { 4107 u_int32_t *hostid = (u_int32_t *)addr; 4108 4109 PF_RULES_WLOCK(); 4110 if (*hostid == 0) 4111 V_pf_status.hostid = arc4random(); 4112 else 4113 V_pf_status.hostid = *hostid; 4114 PF_RULES_WUNLOCK(); 4115 break; 4116 } 4117 4118 case DIOCOSFPFLUSH: 4119 PF_RULES_WLOCK(); 4120 pf_osfp_flush(); 4121 PF_RULES_WUNLOCK(); 4122 break; 4123 4124 case DIOCIGETIFACES: { 4125 struct pfioc_iface *io = (struct pfioc_iface *)addr; 4126 struct pfi_kif *ifstore; 4127 size_t bufsiz; 4128 4129 if (io->pfiio_esize != sizeof(struct pfi_kif)) { 4130 error = ENODEV; 4131 break; 4132 } 4133 4134 if (io->pfiio_size < 0 || 4135 io->pfiio_size > pf_ioctl_maxcount || 4136 WOULD_OVERFLOW(io->pfiio_size, sizeof(struct pfi_kif))) { 4137 error = EINVAL; 4138 break; 4139 } 4140 4141 bufsiz = io->pfiio_size * sizeof(struct pfi_kif); 4142 ifstore = mallocarray(io->pfiio_size, sizeof(struct pfi_kif), 4143 M_TEMP, M_NOWAIT); 4144 if (ifstore == NULL) { 4145 error = ENOMEM; 4146 break; 4147 } 4148 4149 PF_RULES_RLOCK(); 4150 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size); 4151 PF_RULES_RUNLOCK(); 4152 error = copyout(ifstore, io->pfiio_buffer, bufsiz); 4153 free(ifstore, M_TEMP); 4154 break; 4155 } 4156 4157 case DIOCSETIFFLAG: { 4158 struct pfioc_iface *io = (struct pfioc_iface *)addr; 4159 4160 PF_RULES_WLOCK(); 4161 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags); 4162 PF_RULES_WUNLOCK(); 4163 break; 4164 } 4165 4166 case DIOCCLRIFFLAG: { 4167 struct pfioc_iface *io = (struct pfioc_iface *)addr; 4168 4169 PF_RULES_WLOCK(); 4170 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags); 4171 PF_RULES_WUNLOCK(); 4172 break; 4173 } 4174 4175 default: 4176 error = ENODEV; 4177 break; 4178 } 4179 fail: 4180 if (sx_xlocked(&pf_ioctl_lock)) 4181 sx_xunlock(&pf_ioctl_lock); 4182 CURVNET_RESTORE(); 4183 4184 return (error); 4185 } 4186 4187 void 4188 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st) 4189 { 4190 bzero(sp, sizeof(struct pfsync_state)); 4191 4192 /* copy from state key */ 4193 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0]; 4194 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1]; 4195 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0]; 4196 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1]; 4197 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0]; 4198 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1]; 4199 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0]; 4200 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1]; 4201 sp->proto = st->key[PF_SK_WIRE]->proto; 4202 sp->af = st->key[PF_SK_WIRE]->af; 4203 4204 /* copy from state */ 4205 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname)); 4206 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr)); 4207 sp->creation = htonl(time_uptime - st->creation); 4208 sp->expire = pf_state_expires(st); 4209 if (sp->expire <= time_uptime) 4210 sp->expire = htonl(0); 4211 else 4212 sp->expire = htonl(sp->expire - time_uptime); 4213 4214 sp->direction = st->direction; 4215 sp->log = st->log; 4216 sp->timeout = st->timeout; 4217 sp->state_flags = st->state_flags; 4218 if (st->src_node) 4219 sp->sync_flags |= PFSYNC_FLAG_SRCNODE; 4220 if (st->nat_src_node) 4221 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE; 4222 4223 sp->id = st->id; 4224 sp->creatorid = st->creatorid; 4225 pf_state_peer_hton(&st->src, &sp->src); 4226 pf_state_peer_hton(&st->dst, &sp->dst); 4227 4228 if (st->rule.ptr == NULL) 4229 sp->rule = htonl(-1); 4230 else 4231 sp->rule = htonl(st->rule.ptr->nr); 4232 if (st->anchor.ptr == NULL) 4233 sp->anchor = htonl(-1); 4234 else 4235 sp->anchor = htonl(st->anchor.ptr->nr); 4236 if (st->nat_rule.ptr == NULL) 4237 sp->nat_rule = htonl(-1); 4238 else 4239 sp->nat_rule = htonl(st->nat_rule.ptr->nr); 4240 4241 pf_state_counter_hton(counter_u64_fetch(st->packets[0]), 4242 sp->packets[0]); 4243 pf_state_counter_hton(counter_u64_fetch(st->packets[1]), 4244 sp->packets[1]); 4245 pf_state_counter_hton(counter_u64_fetch(st->bytes[0]), sp->bytes[0]); 4246 pf_state_counter_hton(counter_u64_fetch(st->bytes[1]), sp->bytes[1]); 4247 4248 } 4249 4250 static void 4251 pf_tbladdr_copyout(struct pf_addr_wrap *aw) 4252 { 4253 struct pfr_ktable *kt; 4254 4255 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type)); 4256 4257 kt = aw->p.tbl; 4258 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 4259 kt = kt->pfrkt_root; 4260 aw->p.tbl = NULL; 4261 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ? 4262 kt->pfrkt_cnt : -1; 4263 } 4264 4265 /* 4266 * XXX - Check for version missmatch!!! 4267 */ 4268 static void 4269 pf_clear_states(void) 4270 { 4271 struct pf_state *s; 4272 u_int i; 4273 4274 for (i = 0; i <= pf_hashmask; i++) { 4275 struct pf_idhash *ih = &V_pf_idhash[i]; 4276 relock: 4277 PF_HASHROW_LOCK(ih); 4278 LIST_FOREACH(s, &ih->states, entry) { 4279 s->timeout = PFTM_PURGE; 4280 /* Don't send out individual delete messages. */ 4281 s->state_flags |= PFSTATE_NOSYNC; 4282 pf_unlink_state(s, PF_ENTER_LOCKED); 4283 goto relock; 4284 } 4285 PF_HASHROW_UNLOCK(ih); 4286 } 4287 } 4288 4289 static int 4290 pf_clear_tables(void) 4291 { 4292 struct pfioc_table io; 4293 int error; 4294 4295 bzero(&io, sizeof(io)); 4296 4297 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel, 4298 io.pfrio_flags); 4299 4300 return (error); 4301 } 4302 4303 static void 4304 pf_clear_srcnodes(struct pf_ksrc_node *n) 4305 { 4306 struct pf_state *s; 4307 int i; 4308 4309 for (i = 0; i <= pf_hashmask; i++) { 4310 struct pf_idhash *ih = &V_pf_idhash[i]; 4311 4312 PF_HASHROW_LOCK(ih); 4313 LIST_FOREACH(s, &ih->states, entry) { 4314 if (n == NULL || n == s->src_node) 4315 s->src_node = NULL; 4316 if (n == NULL || n == s->nat_src_node) 4317 s->nat_src_node = NULL; 4318 } 4319 PF_HASHROW_UNLOCK(ih); 4320 } 4321 4322 if (n == NULL) { 4323 struct pf_srchash *sh; 4324 4325 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 4326 i++, sh++) { 4327 PF_HASHROW_LOCK(sh); 4328 LIST_FOREACH(n, &sh->nodes, entry) { 4329 n->expire = 1; 4330 n->states = 0; 4331 } 4332 PF_HASHROW_UNLOCK(sh); 4333 } 4334 } else { 4335 /* XXX: hash slot should already be locked here. */ 4336 n->expire = 1; 4337 n->states = 0; 4338 } 4339 } 4340 4341 static void 4342 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk) 4343 { 4344 struct pf_ksrc_node_list kill; 4345 4346 LIST_INIT(&kill); 4347 for (int i = 0; i <= pf_srchashmask; i++) { 4348 struct pf_srchash *sh = &V_pf_srchash[i]; 4349 struct pf_ksrc_node *sn, *tmp; 4350 4351 PF_HASHROW_LOCK(sh); 4352 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp) 4353 if (PF_MATCHA(psnk->psnk_src.neg, 4354 &psnk->psnk_src.addr.v.a.addr, 4355 &psnk->psnk_src.addr.v.a.mask, 4356 &sn->addr, sn->af) && 4357 PF_MATCHA(psnk->psnk_dst.neg, 4358 &psnk->psnk_dst.addr.v.a.addr, 4359 &psnk->psnk_dst.addr.v.a.mask, 4360 &sn->raddr, sn->af)) { 4361 pf_unlink_src_node(sn); 4362 LIST_INSERT_HEAD(&kill, sn, entry); 4363 sn->expire = 1; 4364 } 4365 PF_HASHROW_UNLOCK(sh); 4366 } 4367 4368 for (int i = 0; i <= pf_hashmask; i++) { 4369 struct pf_idhash *ih = &V_pf_idhash[i]; 4370 struct pf_state *s; 4371 4372 PF_HASHROW_LOCK(ih); 4373 LIST_FOREACH(s, &ih->states, entry) { 4374 if (s->src_node && s->src_node->expire == 1) 4375 s->src_node = NULL; 4376 if (s->nat_src_node && s->nat_src_node->expire == 1) 4377 s->nat_src_node = NULL; 4378 } 4379 PF_HASHROW_UNLOCK(ih); 4380 } 4381 4382 psnk->psnk_killed = pf_free_src_nodes(&kill); 4383 } 4384 4385 /* 4386 * XXX - Check for version missmatch!!! 4387 */ 4388 4389 /* 4390 * Duplicate pfctl -Fa operation to get rid of as much as we can. 4391 */ 4392 static int 4393 shutdown_pf(void) 4394 { 4395 int error = 0; 4396 u_int32_t t[5]; 4397 char nn = '\0'; 4398 4399 do { 4400 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn)) 4401 != 0) { 4402 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n")); 4403 break; 4404 } 4405 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn)) 4406 != 0) { 4407 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n")); 4408 break; /* XXX: rollback? */ 4409 } 4410 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn)) 4411 != 0) { 4412 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n")); 4413 break; /* XXX: rollback? */ 4414 } 4415 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn)) 4416 != 0) { 4417 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n")); 4418 break; /* XXX: rollback? */ 4419 } 4420 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn)) 4421 != 0) { 4422 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n")); 4423 break; /* XXX: rollback? */ 4424 } 4425 4426 /* XXX: these should always succeed here */ 4427 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn); 4428 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn); 4429 pf_commit_rules(t[2], PF_RULESET_NAT, &nn); 4430 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn); 4431 pf_commit_rules(t[4], PF_RULESET_RDR, &nn); 4432 4433 if ((error = pf_clear_tables()) != 0) 4434 break; 4435 4436 #ifdef ALTQ 4437 if ((error = pf_begin_altq(&t[0])) != 0) { 4438 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n")); 4439 break; 4440 } 4441 pf_commit_altq(t[0]); 4442 #endif 4443 4444 pf_clear_states(); 4445 4446 pf_clear_srcnodes(NULL); 4447 4448 /* status does not use malloced mem so no need to cleanup */ 4449 /* fingerprints and interfaces have their own cleanup code */ 4450 } while(0); 4451 4452 return (error); 4453 } 4454 4455 static pfil_return_t 4456 pf_check_return(int chk, struct mbuf **m) 4457 { 4458 4459 switch (chk) { 4460 case PF_PASS: 4461 if (*m == NULL) 4462 return (PFIL_CONSUMED); 4463 else 4464 return (PFIL_PASS); 4465 break; 4466 default: 4467 if (*m != NULL) { 4468 m_freem(*m); 4469 *m = NULL; 4470 } 4471 return (PFIL_DROPPED); 4472 } 4473 } 4474 4475 #ifdef INET 4476 static pfil_return_t 4477 pf_check_in(struct mbuf **m, struct ifnet *ifp, int flags, 4478 void *ruleset __unused, struct inpcb *inp) 4479 { 4480 int chk; 4481 4482 chk = pf_test(PF_IN, flags, ifp, m, inp); 4483 4484 return (pf_check_return(chk, m)); 4485 } 4486 4487 static pfil_return_t 4488 pf_check_out(struct mbuf **m, struct ifnet *ifp, int flags, 4489 void *ruleset __unused, struct inpcb *inp) 4490 { 4491 int chk; 4492 4493 chk = pf_test(PF_OUT, flags, ifp, m, inp); 4494 4495 return (pf_check_return(chk, m)); 4496 } 4497 #endif 4498 4499 #ifdef INET6 4500 static pfil_return_t 4501 pf_check6_in(struct mbuf **m, struct ifnet *ifp, int flags, 4502 void *ruleset __unused, struct inpcb *inp) 4503 { 4504 int chk; 4505 4506 /* 4507 * In case of loopback traffic IPv6 uses the real interface in 4508 * order to support scoped addresses. In order to support stateful 4509 * filtering we have change this to lo0 as it is the case in IPv4. 4510 */ 4511 CURVNET_SET(ifp->if_vnet); 4512 chk = pf_test6(PF_IN, flags, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp); 4513 CURVNET_RESTORE(); 4514 4515 return (pf_check_return(chk, m)); 4516 } 4517 4518 static pfil_return_t 4519 pf_check6_out(struct mbuf **m, struct ifnet *ifp, int flags, 4520 void *ruleset __unused, struct inpcb *inp) 4521 { 4522 int chk; 4523 4524 CURVNET_SET(ifp->if_vnet); 4525 chk = pf_test6(PF_OUT, flags, ifp, m, inp); 4526 CURVNET_RESTORE(); 4527 4528 return (pf_check_return(chk, m)); 4529 } 4530 #endif /* INET6 */ 4531 4532 #ifdef INET 4533 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_in_hook); 4534 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_out_hook); 4535 #define V_pf_ip4_in_hook VNET(pf_ip4_in_hook) 4536 #define V_pf_ip4_out_hook VNET(pf_ip4_out_hook) 4537 #endif 4538 #ifdef INET6 4539 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_in_hook); 4540 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_out_hook); 4541 #define V_pf_ip6_in_hook VNET(pf_ip6_in_hook) 4542 #define V_pf_ip6_out_hook VNET(pf_ip6_out_hook) 4543 #endif 4544 4545 static int 4546 hook_pf(void) 4547 { 4548 struct pfil_hook_args pha; 4549 struct pfil_link_args pla; 4550 4551 if (V_pf_pfil_hooked) 4552 return (0); 4553 4554 pha.pa_version = PFIL_VERSION; 4555 pha.pa_modname = "pf"; 4556 pha.pa_ruleset = NULL; 4557 4558 pla.pa_version = PFIL_VERSION; 4559 4560 #ifdef INET 4561 pha.pa_type = PFIL_TYPE_IP4; 4562 pha.pa_func = pf_check_in; 4563 pha.pa_flags = PFIL_IN; 4564 pha.pa_rulname = "default-in"; 4565 V_pf_ip4_in_hook = pfil_add_hook(&pha); 4566 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR; 4567 pla.pa_head = V_inet_pfil_head; 4568 pla.pa_hook = V_pf_ip4_in_hook; 4569 (void)pfil_link(&pla); 4570 pha.pa_func = pf_check_out; 4571 pha.pa_flags = PFIL_OUT; 4572 pha.pa_rulname = "default-out"; 4573 V_pf_ip4_out_hook = pfil_add_hook(&pha); 4574 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR; 4575 pla.pa_head = V_inet_pfil_head; 4576 pla.pa_hook = V_pf_ip4_out_hook; 4577 (void)pfil_link(&pla); 4578 #endif 4579 #ifdef INET6 4580 pha.pa_type = PFIL_TYPE_IP6; 4581 pha.pa_func = pf_check6_in; 4582 pha.pa_flags = PFIL_IN; 4583 pha.pa_rulname = "default-in6"; 4584 V_pf_ip6_in_hook = pfil_add_hook(&pha); 4585 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR; 4586 pla.pa_head = V_inet6_pfil_head; 4587 pla.pa_hook = V_pf_ip6_in_hook; 4588 (void)pfil_link(&pla); 4589 pha.pa_func = pf_check6_out; 4590 pha.pa_rulname = "default-out6"; 4591 pha.pa_flags = PFIL_OUT; 4592 V_pf_ip6_out_hook = pfil_add_hook(&pha); 4593 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR; 4594 pla.pa_head = V_inet6_pfil_head; 4595 pla.pa_hook = V_pf_ip6_out_hook; 4596 (void)pfil_link(&pla); 4597 #endif 4598 4599 V_pf_pfil_hooked = 1; 4600 return (0); 4601 } 4602 4603 static int 4604 dehook_pf(void) 4605 { 4606 4607 if (V_pf_pfil_hooked == 0) 4608 return (0); 4609 4610 #ifdef INET 4611 pfil_remove_hook(V_pf_ip4_in_hook); 4612 pfil_remove_hook(V_pf_ip4_out_hook); 4613 #endif 4614 #ifdef INET6 4615 pfil_remove_hook(V_pf_ip6_in_hook); 4616 pfil_remove_hook(V_pf_ip6_out_hook); 4617 #endif 4618 4619 V_pf_pfil_hooked = 0; 4620 return (0); 4621 } 4622 4623 static void 4624 pf_load_vnet(void) 4625 { 4626 V_pf_tag_z = uma_zcreate("pf tags", sizeof(struct pf_tagname), 4627 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 4628 4629 pf_init_tagset(&V_pf_tags, &pf_rule_tag_hashsize, 4630 PF_RULE_TAG_HASH_SIZE_DEFAULT); 4631 #ifdef ALTQ 4632 pf_init_tagset(&V_pf_qids, &pf_queue_tag_hashsize, 4633 PF_QUEUE_TAG_HASH_SIZE_DEFAULT); 4634 #endif 4635 4636 pfattach_vnet(); 4637 V_pf_vnet_active = 1; 4638 } 4639 4640 static int 4641 pf_load(void) 4642 { 4643 int error; 4644 4645 rm_init(&pf_rules_lock, "pf rulesets"); 4646 sx_init(&pf_ioctl_lock, "pf ioctl"); 4647 sx_init(&pf_end_lock, "pf end thread"); 4648 4649 pf_mtag_initialize(); 4650 4651 pf_dev = make_dev(&pf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, PF_NAME); 4652 if (pf_dev == NULL) 4653 return (ENOMEM); 4654 4655 pf_end_threads = 0; 4656 error = kproc_create(pf_purge_thread, NULL, &pf_purge_proc, 0, 0, "pf purge"); 4657 if (error != 0) 4658 return (error); 4659 4660 pfi_initialize(); 4661 4662 return (0); 4663 } 4664 4665 static void 4666 pf_unload_vnet(void) 4667 { 4668 int error; 4669 4670 V_pf_vnet_active = 0; 4671 V_pf_status.running = 0; 4672 error = dehook_pf(); 4673 if (error) { 4674 /* 4675 * Should not happen! 4676 * XXX Due to error code ESRCH, kldunload will show 4677 * a message like 'No such process'. 4678 */ 4679 printf("%s : pfil unregisteration fail\n", __FUNCTION__); 4680 return; 4681 } 4682 4683 PF_RULES_WLOCK(); 4684 shutdown_pf(); 4685 PF_RULES_WUNLOCK(); 4686 4687 swi_remove(V_pf_swi_cookie); 4688 4689 pf_unload_vnet_purge(); 4690 4691 pf_normalize_cleanup(); 4692 PF_RULES_WLOCK(); 4693 pfi_cleanup_vnet(); 4694 PF_RULES_WUNLOCK(); 4695 pfr_cleanup(); 4696 pf_osfp_flush(); 4697 pf_cleanup(); 4698 if (IS_DEFAULT_VNET(curvnet)) 4699 pf_mtag_cleanup(); 4700 4701 pf_cleanup_tagset(&V_pf_tags); 4702 #ifdef ALTQ 4703 pf_cleanup_tagset(&V_pf_qids); 4704 #endif 4705 uma_zdestroy(V_pf_tag_z); 4706 4707 /* Free counters last as we updated them during shutdown. */ 4708 counter_u64_free(V_pf_default_rule.evaluations); 4709 for (int i = 0; i < 2; i++) { 4710 counter_u64_free(V_pf_default_rule.packets[i]); 4711 counter_u64_free(V_pf_default_rule.bytes[i]); 4712 } 4713 counter_u64_free(V_pf_default_rule.states_cur); 4714 counter_u64_free(V_pf_default_rule.states_tot); 4715 counter_u64_free(V_pf_default_rule.src_nodes); 4716 4717 for (int i = 0; i < PFRES_MAX; i++) 4718 counter_u64_free(V_pf_status.counters[i]); 4719 for (int i = 0; i < LCNT_MAX; i++) 4720 counter_u64_free(V_pf_status.lcounters[i]); 4721 for (int i = 0; i < FCNT_MAX; i++) 4722 counter_u64_free(V_pf_status.fcounters[i]); 4723 for (int i = 0; i < SCNT_MAX; i++) 4724 counter_u64_free(V_pf_status.scounters[i]); 4725 } 4726 4727 static void 4728 pf_unload(void) 4729 { 4730 4731 sx_xlock(&pf_end_lock); 4732 pf_end_threads = 1; 4733 while (pf_end_threads < 2) { 4734 wakeup_one(pf_purge_thread); 4735 sx_sleep(pf_purge_proc, &pf_end_lock, 0, "pftmo", 0); 4736 } 4737 sx_xunlock(&pf_end_lock); 4738 4739 if (pf_dev != NULL) 4740 destroy_dev(pf_dev); 4741 4742 pfi_cleanup(); 4743 4744 rm_destroy(&pf_rules_lock); 4745 sx_destroy(&pf_ioctl_lock); 4746 sx_destroy(&pf_end_lock); 4747 } 4748 4749 static void 4750 vnet_pf_init(void *unused __unused) 4751 { 4752 4753 pf_load_vnet(); 4754 } 4755 VNET_SYSINIT(vnet_pf_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD, 4756 vnet_pf_init, NULL); 4757 4758 static void 4759 vnet_pf_uninit(const void *unused __unused) 4760 { 4761 4762 pf_unload_vnet(); 4763 } 4764 SYSUNINIT(pf_unload, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND, pf_unload, NULL); 4765 VNET_SYSUNINIT(vnet_pf_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD, 4766 vnet_pf_uninit, NULL); 4767 4768 static int 4769 pf_modevent(module_t mod, int type, void *data) 4770 { 4771 int error = 0; 4772 4773 switch(type) { 4774 case MOD_LOAD: 4775 error = pf_load(); 4776 break; 4777 case MOD_UNLOAD: 4778 /* Handled in SYSUNINIT(pf_unload) to ensure it's done after 4779 * the vnet_pf_uninit()s */ 4780 break; 4781 default: 4782 error = EINVAL; 4783 break; 4784 } 4785 4786 return (error); 4787 } 4788 4789 static moduledata_t pf_mod = { 4790 "pf", 4791 pf_modevent, 4792 0 4793 }; 4794 4795 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND); 4796 MODULE_VERSION(pf, PF_MODVER); 4797