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