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