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