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_rule_to_krule(const struct pf_rule *rule, struct pf_krule *krule) 1562 { 1563 1564 #ifndef INET 1565 if (rule->af == AF_INET) { 1566 return (EAFNOSUPPORT); 1567 } 1568 #endif /* INET */ 1569 #ifndef INET6 1570 if (rule->af == AF_INET6) { 1571 return (EAFNOSUPPORT); 1572 } 1573 #endif /* INET6 */ 1574 1575 if (rule->src.addr.type != PF_ADDR_ADDRMASK && 1576 rule->src.addr.type != PF_ADDR_DYNIFTL && 1577 rule->src.addr.type != PF_ADDR_TABLE) { 1578 return (EINVAL); 1579 } 1580 if (rule->src.addr.p.dyn != NULL) { 1581 return (EINVAL); 1582 } 1583 1584 if (rule->dst.addr.type != PF_ADDR_ADDRMASK && 1585 rule->dst.addr.type != PF_ADDR_DYNIFTL && 1586 rule->dst.addr.type != PF_ADDR_TABLE) { 1587 return (EINVAL); 1588 } 1589 if (rule->dst.addr.p.dyn != NULL) { 1590 return (EINVAL); 1591 } 1592 1593 bzero(krule, sizeof(*krule)); 1594 1595 bcopy(&rule->src, &krule->src, sizeof(rule->src)); 1596 bcopy(&rule->dst, &krule->dst, sizeof(rule->dst)); 1597 1598 strlcpy(krule->label, rule->label, sizeof(rule->label)); 1599 strlcpy(krule->ifname, rule->ifname, sizeof(rule->ifname)); 1600 strlcpy(krule->qname, rule->qname, sizeof(rule->qname)); 1601 strlcpy(krule->pqname, rule->pqname, sizeof(rule->pqname)); 1602 strlcpy(krule->tagname, rule->tagname, sizeof(rule->tagname)); 1603 strlcpy(krule->match_tagname, rule->match_tagname, 1604 sizeof(rule->match_tagname)); 1605 strlcpy(krule->overload_tblname, rule->overload_tblname, 1606 sizeof(rule->overload_tblname)); 1607 1608 bcopy(&rule->rpool, &krule->rpool, sizeof(krule->rpool)); 1609 1610 /* Don't allow userspace to set evaulations, packets or bytes. */ 1611 /* kif, anchor, overload_tbl are not copied over. */ 1612 1613 krule->os_fingerprint = krule->os_fingerprint; 1614 1615 krule->rtableid = rule->rtableid; 1616 bcopy(rule->timeout, krule->timeout, sizeof(krule->timeout)); 1617 krule->max_states = rule->max_states; 1618 krule->max_src_nodes = rule->max_src_nodes; 1619 krule->max_src_states = rule->max_src_states; 1620 krule->max_src_conn = rule->max_src_conn; 1621 krule->max_src_conn_rate.limit = rule->max_src_conn_rate.limit; 1622 krule->max_src_conn_rate.seconds = rule->max_src_conn_rate.seconds; 1623 krule->qid = rule->qid; 1624 krule->pqid = rule->pqid; 1625 krule->rt_listid = rule->rt_listid; 1626 krule->nr = rule->nr; 1627 krule->prob = rule->prob; 1628 krule->cuid = rule->cuid; 1629 krule->cpid = rule->cpid; 1630 1631 krule->return_icmp = rule->return_icmp; 1632 krule->return_icmp6 = rule->return_icmp6; 1633 krule->max_mss = rule->max_mss; 1634 krule->tag = rule->tag; 1635 krule->match_tag = rule->match_tag; 1636 krule->scrub_flags = rule->scrub_flags; 1637 1638 bcopy(&rule->uid, &krule->uid, sizeof(krule->uid)); 1639 bcopy(&rule->gid, &krule->gid, sizeof(krule->gid)); 1640 1641 krule->rule_flag = rule->rule_flag; 1642 krule->action = rule->action; 1643 krule->direction = rule->direction; 1644 krule->log = rule->log; 1645 krule->logif = rule->logif; 1646 krule->quick = rule->quick; 1647 krule->ifnot = rule->ifnot; 1648 krule->match_tag_not = rule->match_tag_not; 1649 krule->natpass = rule->natpass; 1650 1651 krule->keep_state = rule->keep_state; 1652 krule->af = rule->af; 1653 krule->proto = rule->proto; 1654 krule->type = rule->type; 1655 krule->code = rule->code; 1656 krule->flags = rule->flags; 1657 krule->flagset = rule->flagset; 1658 krule->min_ttl = rule->min_ttl; 1659 krule->allow_opts = rule->allow_opts; 1660 krule->rt = rule->rt; 1661 krule->return_ttl = rule->return_ttl; 1662 krule->tos = rule->tos; 1663 krule->set_tos = rule->set_tos; 1664 krule->anchor_relative = rule->anchor_relative; 1665 krule->anchor_wildcard = rule->anchor_wildcard; 1666 1667 krule->flush = rule->flush; 1668 krule->prio = rule->prio; 1669 krule->set_prio[0] = rule->set_prio[0]; 1670 krule->set_prio[1] = rule->set_prio[1]; 1671 1672 bcopy(&rule->divert, &krule->divert, sizeof(krule->divert)); 1673 1674 return (0); 1675 } 1676 1677 static int 1678 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 1679 { 1680 int error = 0; 1681 PF_RULES_RLOCK_TRACKER; 1682 1683 /* XXX keep in sync with switch() below */ 1684 if (securelevel_gt(td->td_ucred, 2)) 1685 switch (cmd) { 1686 case DIOCGETRULES: 1687 case DIOCGETRULE: 1688 case DIOCGETADDRS: 1689 case DIOCGETADDR: 1690 case DIOCGETSTATE: 1691 case DIOCSETSTATUSIF: 1692 case DIOCGETSTATUS: 1693 case DIOCCLRSTATUS: 1694 case DIOCNATLOOK: 1695 case DIOCSETDEBUG: 1696 case DIOCGETSTATES: 1697 case DIOCGETTIMEOUT: 1698 case DIOCCLRRULECTRS: 1699 case DIOCGETLIMIT: 1700 case DIOCGETALTQSV0: 1701 case DIOCGETALTQSV1: 1702 case DIOCGETALTQV0: 1703 case DIOCGETALTQV1: 1704 case DIOCGETQSTATSV0: 1705 case DIOCGETQSTATSV1: 1706 case DIOCGETRULESETS: 1707 case DIOCGETRULESET: 1708 case DIOCRGETTABLES: 1709 case DIOCRGETTSTATS: 1710 case DIOCRCLRTSTATS: 1711 case DIOCRCLRADDRS: 1712 case DIOCRADDADDRS: 1713 case DIOCRDELADDRS: 1714 case DIOCRSETADDRS: 1715 case DIOCRGETADDRS: 1716 case DIOCRGETASTATS: 1717 case DIOCRCLRASTATS: 1718 case DIOCRTSTADDRS: 1719 case DIOCOSFPGET: 1720 case DIOCGETSRCNODES: 1721 case DIOCCLRSRCNODES: 1722 case DIOCIGETIFACES: 1723 case DIOCGIFSPEEDV0: 1724 case DIOCGIFSPEEDV1: 1725 case DIOCSETIFFLAG: 1726 case DIOCCLRIFFLAG: 1727 break; 1728 case DIOCRCLRTABLES: 1729 case DIOCRADDTABLES: 1730 case DIOCRDELTABLES: 1731 case DIOCRSETTFLAGS: 1732 if (((struct pfioc_table *)addr)->pfrio_flags & 1733 PFR_FLAG_DUMMY) 1734 break; /* dummy operation ok */ 1735 return (EPERM); 1736 default: 1737 return (EPERM); 1738 } 1739 1740 if (!(flags & FWRITE)) 1741 switch (cmd) { 1742 case DIOCGETRULES: 1743 case DIOCGETADDRS: 1744 case DIOCGETADDR: 1745 case DIOCGETSTATE: 1746 case DIOCGETSTATUS: 1747 case DIOCGETSTATES: 1748 case DIOCGETTIMEOUT: 1749 case DIOCGETLIMIT: 1750 case DIOCGETALTQSV0: 1751 case DIOCGETALTQSV1: 1752 case DIOCGETALTQV0: 1753 case DIOCGETALTQV1: 1754 case DIOCGETQSTATSV0: 1755 case DIOCGETQSTATSV1: 1756 case DIOCGETRULESETS: 1757 case DIOCGETRULESET: 1758 case DIOCNATLOOK: 1759 case DIOCRGETTABLES: 1760 case DIOCRGETTSTATS: 1761 case DIOCRGETADDRS: 1762 case DIOCRGETASTATS: 1763 case DIOCRTSTADDRS: 1764 case DIOCOSFPGET: 1765 case DIOCGETSRCNODES: 1766 case DIOCIGETIFACES: 1767 case DIOCGIFSPEEDV1: 1768 case DIOCGIFSPEEDV0: 1769 break; 1770 case DIOCRCLRTABLES: 1771 case DIOCRADDTABLES: 1772 case DIOCRDELTABLES: 1773 case DIOCRCLRTSTATS: 1774 case DIOCRCLRADDRS: 1775 case DIOCRADDADDRS: 1776 case DIOCRDELADDRS: 1777 case DIOCRSETADDRS: 1778 case DIOCRSETTFLAGS: 1779 if (((struct pfioc_table *)addr)->pfrio_flags & 1780 PFR_FLAG_DUMMY) { 1781 flags |= FWRITE; /* need write lock for dummy */ 1782 break; /* dummy operation ok */ 1783 } 1784 return (EACCES); 1785 case DIOCGETRULE: 1786 if (((struct pfioc_rule *)addr)->action == 1787 PF_GET_CLR_CNTR) 1788 return (EACCES); 1789 break; 1790 default: 1791 return (EACCES); 1792 } 1793 1794 CURVNET_SET(TD_TO_VNET(td)); 1795 1796 switch (cmd) { 1797 case DIOCSTART: 1798 sx_xlock(&pf_ioctl_lock); 1799 if (V_pf_status.running) 1800 error = EEXIST; 1801 else { 1802 int cpu; 1803 1804 error = hook_pf(); 1805 if (error) { 1806 DPFPRINTF(PF_DEBUG_MISC, 1807 ("pf: pfil registration failed\n")); 1808 break; 1809 } 1810 V_pf_status.running = 1; 1811 V_pf_status.since = time_second; 1812 1813 CPU_FOREACH(cpu) 1814 V_pf_stateid[cpu] = time_second; 1815 1816 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n")); 1817 } 1818 break; 1819 1820 case DIOCSTOP: 1821 sx_xlock(&pf_ioctl_lock); 1822 if (!V_pf_status.running) 1823 error = ENOENT; 1824 else { 1825 V_pf_status.running = 0; 1826 error = dehook_pf(); 1827 if (error) { 1828 V_pf_status.running = 1; 1829 DPFPRINTF(PF_DEBUG_MISC, 1830 ("pf: pfil unregistration failed\n")); 1831 } 1832 V_pf_status.since = time_second; 1833 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n")); 1834 } 1835 break; 1836 1837 case DIOCADDRULE: { 1838 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 1839 struct pf_kruleset *ruleset; 1840 struct pf_krule *rule, *tail; 1841 struct pf_kpooladdr *pa; 1842 struct pfi_kkif *kif = NULL; 1843 int rs_num; 1844 1845 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) { 1846 error = EINVAL; 1847 break; 1848 } 1849 1850 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK); 1851 error = pf_rule_to_krule(&pr->rule, rule); 1852 if (error != 0) { 1853 free(rule, M_PFRULE); 1854 break; 1855 } 1856 1857 if (rule->ifname[0]) 1858 kif = pf_kkif_create(M_WAITOK); 1859 rule->evaluations = counter_u64_alloc(M_WAITOK); 1860 for (int i = 0; i < 2; i++) { 1861 rule->packets[i] = counter_u64_alloc(M_WAITOK); 1862 rule->bytes[i] = counter_u64_alloc(M_WAITOK); 1863 } 1864 rule->states_cur = counter_u64_alloc(M_WAITOK); 1865 rule->states_tot = counter_u64_alloc(M_WAITOK); 1866 rule->src_nodes = counter_u64_alloc(M_WAITOK); 1867 rule->cuid = td->td_ucred->cr_ruid; 1868 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0; 1869 TAILQ_INIT(&rule->rpool.list); 1870 #define ERROUT(x) { error = (x); goto DIOCADDRULE_error; } 1871 1872 PF_RULES_WLOCK(); 1873 pr->anchor[sizeof(pr->anchor) - 1] = 0; 1874 ruleset = pf_find_kruleset(pr->anchor); 1875 if (ruleset == NULL) 1876 ERROUT(EINVAL); 1877 rs_num = pf_get_ruleset_number(pr->rule.action); 1878 if (rs_num >= PF_RULESET_MAX) 1879 ERROUT(EINVAL); 1880 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) { 1881 DPFPRINTF(PF_DEBUG_MISC, 1882 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num, 1883 ruleset->rules[rs_num].inactive.ticket)); 1884 ERROUT(EBUSY); 1885 } 1886 if (pr->pool_ticket != V_ticket_pabuf) { 1887 DPFPRINTF(PF_DEBUG_MISC, 1888 ("pool_ticket: %d != %d\n", pr->pool_ticket, 1889 V_ticket_pabuf)); 1890 ERROUT(EBUSY); 1891 } 1892 1893 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr, 1894 pf_krulequeue); 1895 if (tail) 1896 rule->nr = tail->nr + 1; 1897 else 1898 rule->nr = 0; 1899 if (rule->ifname[0]) { 1900 rule->kif = pfi_kkif_attach(kif, rule->ifname); 1901 pfi_kkif_ref(rule->kif); 1902 } else 1903 rule->kif = NULL; 1904 1905 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs) 1906 error = EBUSY; 1907 1908 #ifdef ALTQ 1909 /* set queue IDs */ 1910 if (rule->qname[0] != 0) { 1911 if ((rule->qid = pf_qname2qid(rule->qname)) == 0) 1912 error = EBUSY; 1913 else if (rule->pqname[0] != 0) { 1914 if ((rule->pqid = 1915 pf_qname2qid(rule->pqname)) == 0) 1916 error = EBUSY; 1917 } else 1918 rule->pqid = rule->qid; 1919 } 1920 #endif 1921 if (rule->tagname[0]) 1922 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0) 1923 error = EBUSY; 1924 if (rule->match_tagname[0]) 1925 if ((rule->match_tag = 1926 pf_tagname2tag(rule->match_tagname)) == 0) 1927 error = EBUSY; 1928 if (rule->rt && !rule->direction) 1929 error = EINVAL; 1930 if (!rule->log) 1931 rule->logif = 0; 1932 if (rule->logif >= PFLOGIFS_MAX) 1933 error = EINVAL; 1934 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af)) 1935 error = ENOMEM; 1936 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af)) 1937 error = ENOMEM; 1938 if (pf_kanchor_setup(rule, ruleset, pr->anchor_call)) 1939 error = EINVAL; 1940 if (rule->scrub_flags & PFSTATE_SETPRIO && 1941 (rule->set_prio[0] > PF_PRIO_MAX || 1942 rule->set_prio[1] > PF_PRIO_MAX)) 1943 error = EINVAL; 1944 TAILQ_FOREACH(pa, &V_pf_pabuf, entries) 1945 if (pa->addr.type == PF_ADDR_TABLE) { 1946 pa->addr.p.tbl = pfr_attach_table(ruleset, 1947 pa->addr.v.tblname); 1948 if (pa->addr.p.tbl == NULL) 1949 error = ENOMEM; 1950 } 1951 1952 rule->overload_tbl = NULL; 1953 if (rule->overload_tblname[0]) { 1954 if ((rule->overload_tbl = pfr_attach_table(ruleset, 1955 rule->overload_tblname)) == NULL) 1956 error = EINVAL; 1957 else 1958 rule->overload_tbl->pfrkt_flags |= 1959 PFR_TFLAG_ACTIVE; 1960 } 1961 1962 pf_mv_kpool(&V_pf_pabuf, &rule->rpool.list); 1963 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) || 1964 (rule->action == PF_BINAT)) && rule->anchor == NULL) || 1965 (rule->rt > PF_NOPFROUTE)) && 1966 (TAILQ_FIRST(&rule->rpool.list) == NULL)) 1967 error = EINVAL; 1968 1969 if (error) { 1970 pf_free_rule(rule); 1971 PF_RULES_WUNLOCK(); 1972 break; 1973 } 1974 1975 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list); 1976 counter_u64_zero(rule->evaluations); 1977 for (int i = 0; i < 2; i++) { 1978 counter_u64_zero(rule->packets[i]); 1979 counter_u64_zero(rule->bytes[i]); 1980 } 1981 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr, 1982 rule, entries); 1983 ruleset->rules[rs_num].inactive.rcount++; 1984 PF_RULES_WUNLOCK(); 1985 break; 1986 1987 #undef ERROUT 1988 DIOCADDRULE_error: 1989 PF_RULES_WUNLOCK(); 1990 counter_u64_free(rule->evaluations); 1991 for (int i = 0; i < 2; i++) { 1992 counter_u64_free(rule->packets[i]); 1993 counter_u64_free(rule->bytes[i]); 1994 } 1995 counter_u64_free(rule->states_cur); 1996 counter_u64_free(rule->states_tot); 1997 counter_u64_free(rule->src_nodes); 1998 free(rule, M_PFRULE); 1999 if (kif) 2000 pf_kkif_free(kif); 2001 break; 2002 } 2003 2004 case DIOCGETRULES: { 2005 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 2006 struct pf_kruleset *ruleset; 2007 struct pf_krule *tail; 2008 int rs_num; 2009 2010 PF_RULES_WLOCK(); 2011 pr->anchor[sizeof(pr->anchor) - 1] = 0; 2012 ruleset = pf_find_kruleset(pr->anchor); 2013 if (ruleset == NULL) { 2014 PF_RULES_WUNLOCK(); 2015 error = EINVAL; 2016 break; 2017 } 2018 rs_num = pf_get_ruleset_number(pr->rule.action); 2019 if (rs_num >= PF_RULESET_MAX) { 2020 PF_RULES_WUNLOCK(); 2021 error = EINVAL; 2022 break; 2023 } 2024 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr, 2025 pf_krulequeue); 2026 if (tail) 2027 pr->nr = tail->nr + 1; 2028 else 2029 pr->nr = 0; 2030 pr->ticket = ruleset->rules[rs_num].active.ticket; 2031 PF_RULES_WUNLOCK(); 2032 break; 2033 } 2034 2035 case DIOCGETRULE: { 2036 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 2037 struct pf_kruleset *ruleset; 2038 struct pf_krule *rule; 2039 int rs_num; 2040 2041 PF_RULES_WLOCK(); 2042 pr->anchor[sizeof(pr->anchor) - 1] = 0; 2043 ruleset = pf_find_kruleset(pr->anchor); 2044 if (ruleset == NULL) { 2045 PF_RULES_WUNLOCK(); 2046 error = EINVAL; 2047 break; 2048 } 2049 rs_num = pf_get_ruleset_number(pr->rule.action); 2050 if (rs_num >= PF_RULESET_MAX) { 2051 PF_RULES_WUNLOCK(); 2052 error = EINVAL; 2053 break; 2054 } 2055 if (pr->ticket != ruleset->rules[rs_num].active.ticket) { 2056 PF_RULES_WUNLOCK(); 2057 error = EBUSY; 2058 break; 2059 } 2060 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr); 2061 while ((rule != NULL) && (rule->nr != pr->nr)) 2062 rule = TAILQ_NEXT(rule, entries); 2063 if (rule == NULL) { 2064 PF_RULES_WUNLOCK(); 2065 error = EBUSY; 2066 break; 2067 } 2068 2069 pf_krule_to_rule(rule, &pr->rule); 2070 2071 if (pf_kanchor_copyout(ruleset, rule, pr)) { 2072 PF_RULES_WUNLOCK(); 2073 error = EBUSY; 2074 break; 2075 } 2076 pf_addr_copyout(&pr->rule.src.addr); 2077 pf_addr_copyout(&pr->rule.dst.addr); 2078 2079 if (pr->action == PF_GET_CLR_CNTR) { 2080 counter_u64_zero(rule->evaluations); 2081 for (int i = 0; i < 2; i++) { 2082 counter_u64_zero(rule->packets[i]); 2083 counter_u64_zero(rule->bytes[i]); 2084 } 2085 counter_u64_zero(rule->states_tot); 2086 } 2087 PF_RULES_WUNLOCK(); 2088 break; 2089 } 2090 2091 case DIOCCHANGERULE: { 2092 struct pfioc_rule *pcr = (struct pfioc_rule *)addr; 2093 struct pf_kruleset *ruleset; 2094 struct pf_krule *oldrule = NULL, *newrule = NULL; 2095 struct pfi_kkif *kif = NULL; 2096 struct pf_kpooladdr *pa; 2097 u_int32_t nr = 0; 2098 int rs_num; 2099 2100 if (pcr->action < PF_CHANGE_ADD_HEAD || 2101 pcr->action > PF_CHANGE_GET_TICKET) { 2102 error = EINVAL; 2103 break; 2104 } 2105 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) { 2106 error = EINVAL; 2107 break; 2108 } 2109 2110 if (pcr->action != PF_CHANGE_REMOVE) { 2111 newrule = malloc(sizeof(*newrule), M_PFRULE, M_WAITOK); 2112 error = pf_rule_to_krule(&pcr->rule, newrule); 2113 if (error != 0) { 2114 free(newrule, M_PFRULE); 2115 break; 2116 } 2117 2118 if (newrule->ifname[0]) 2119 kif = pf_kkif_create(M_WAITOK); 2120 newrule->evaluations = counter_u64_alloc(M_WAITOK); 2121 for (int i = 0; i < 2; i++) { 2122 newrule->packets[i] = 2123 counter_u64_alloc(M_WAITOK); 2124 newrule->bytes[i] = 2125 counter_u64_alloc(M_WAITOK); 2126 } 2127 newrule->states_cur = counter_u64_alloc(M_WAITOK); 2128 newrule->states_tot = counter_u64_alloc(M_WAITOK); 2129 newrule->src_nodes = counter_u64_alloc(M_WAITOK); 2130 newrule->cuid = td->td_ucred->cr_ruid; 2131 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0; 2132 TAILQ_INIT(&newrule->rpool.list); 2133 } 2134 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; } 2135 2136 PF_RULES_WLOCK(); 2137 if (!(pcr->action == PF_CHANGE_REMOVE || 2138 pcr->action == PF_CHANGE_GET_TICKET) && 2139 pcr->pool_ticket != V_ticket_pabuf) 2140 ERROUT(EBUSY); 2141 2142 ruleset = pf_find_kruleset(pcr->anchor); 2143 if (ruleset == NULL) 2144 ERROUT(EINVAL); 2145 2146 rs_num = pf_get_ruleset_number(pcr->rule.action); 2147 if (rs_num >= PF_RULESET_MAX) 2148 ERROUT(EINVAL); 2149 2150 if (pcr->action == PF_CHANGE_GET_TICKET) { 2151 pcr->ticket = ++ruleset->rules[rs_num].active.ticket; 2152 ERROUT(0); 2153 } else if (pcr->ticket != 2154 ruleset->rules[rs_num].active.ticket) 2155 ERROUT(EINVAL); 2156 2157 if (pcr->action != PF_CHANGE_REMOVE) { 2158 if (newrule->ifname[0]) { 2159 newrule->kif = pfi_kkif_attach(kif, 2160 newrule->ifname); 2161 pfi_kkif_ref(newrule->kif); 2162 } else 2163 newrule->kif = NULL; 2164 2165 if (newrule->rtableid > 0 && 2166 newrule->rtableid >= rt_numfibs) 2167 error = EBUSY; 2168 2169 #ifdef ALTQ 2170 /* set queue IDs */ 2171 if (newrule->qname[0] != 0) { 2172 if ((newrule->qid = 2173 pf_qname2qid(newrule->qname)) == 0) 2174 error = EBUSY; 2175 else if (newrule->pqname[0] != 0) { 2176 if ((newrule->pqid = 2177 pf_qname2qid(newrule->pqname)) == 0) 2178 error = EBUSY; 2179 } else 2180 newrule->pqid = newrule->qid; 2181 } 2182 #endif /* ALTQ */ 2183 if (newrule->tagname[0]) 2184 if ((newrule->tag = 2185 pf_tagname2tag(newrule->tagname)) == 0) 2186 error = EBUSY; 2187 if (newrule->match_tagname[0]) 2188 if ((newrule->match_tag = pf_tagname2tag( 2189 newrule->match_tagname)) == 0) 2190 error = EBUSY; 2191 if (newrule->rt && !newrule->direction) 2192 error = EINVAL; 2193 if (!newrule->log) 2194 newrule->logif = 0; 2195 if (newrule->logif >= PFLOGIFS_MAX) 2196 error = EINVAL; 2197 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af)) 2198 error = ENOMEM; 2199 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af)) 2200 error = ENOMEM; 2201 if (pf_kanchor_setup(newrule, ruleset, pcr->anchor_call)) 2202 error = EINVAL; 2203 TAILQ_FOREACH(pa, &V_pf_pabuf, entries) 2204 if (pa->addr.type == PF_ADDR_TABLE) { 2205 pa->addr.p.tbl = 2206 pfr_attach_table(ruleset, 2207 pa->addr.v.tblname); 2208 if (pa->addr.p.tbl == NULL) 2209 error = ENOMEM; 2210 } 2211 2212 newrule->overload_tbl = NULL; 2213 if (newrule->overload_tblname[0]) { 2214 if ((newrule->overload_tbl = pfr_attach_table( 2215 ruleset, newrule->overload_tblname)) == 2216 NULL) 2217 error = EINVAL; 2218 else 2219 newrule->overload_tbl->pfrkt_flags |= 2220 PFR_TFLAG_ACTIVE; 2221 } 2222 2223 pf_mv_kpool(&V_pf_pabuf, &newrule->rpool.list); 2224 if (((((newrule->action == PF_NAT) || 2225 (newrule->action == PF_RDR) || 2226 (newrule->action == PF_BINAT) || 2227 (newrule->rt > PF_NOPFROUTE)) && 2228 !newrule->anchor)) && 2229 (TAILQ_FIRST(&newrule->rpool.list) == NULL)) 2230 error = EINVAL; 2231 2232 if (error) { 2233 pf_free_rule(newrule); 2234 PF_RULES_WUNLOCK(); 2235 break; 2236 } 2237 2238 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list); 2239 } 2240 pf_empty_kpool(&V_pf_pabuf); 2241 2242 if (pcr->action == PF_CHANGE_ADD_HEAD) 2243 oldrule = TAILQ_FIRST( 2244 ruleset->rules[rs_num].active.ptr); 2245 else if (pcr->action == PF_CHANGE_ADD_TAIL) 2246 oldrule = TAILQ_LAST( 2247 ruleset->rules[rs_num].active.ptr, pf_krulequeue); 2248 else { 2249 oldrule = TAILQ_FIRST( 2250 ruleset->rules[rs_num].active.ptr); 2251 while ((oldrule != NULL) && (oldrule->nr != pcr->nr)) 2252 oldrule = TAILQ_NEXT(oldrule, entries); 2253 if (oldrule == NULL) { 2254 if (newrule != NULL) 2255 pf_free_rule(newrule); 2256 PF_RULES_WUNLOCK(); 2257 error = EINVAL; 2258 break; 2259 } 2260 } 2261 2262 if (pcr->action == PF_CHANGE_REMOVE) { 2263 pf_unlink_rule(ruleset->rules[rs_num].active.ptr, 2264 oldrule); 2265 ruleset->rules[rs_num].active.rcount--; 2266 } else { 2267 if (oldrule == NULL) 2268 TAILQ_INSERT_TAIL( 2269 ruleset->rules[rs_num].active.ptr, 2270 newrule, entries); 2271 else if (pcr->action == PF_CHANGE_ADD_HEAD || 2272 pcr->action == PF_CHANGE_ADD_BEFORE) 2273 TAILQ_INSERT_BEFORE(oldrule, newrule, entries); 2274 else 2275 TAILQ_INSERT_AFTER( 2276 ruleset->rules[rs_num].active.ptr, 2277 oldrule, newrule, entries); 2278 ruleset->rules[rs_num].active.rcount++; 2279 } 2280 2281 nr = 0; 2282 TAILQ_FOREACH(oldrule, 2283 ruleset->rules[rs_num].active.ptr, entries) 2284 oldrule->nr = nr++; 2285 2286 ruleset->rules[rs_num].active.ticket++; 2287 2288 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr); 2289 pf_remove_if_empty_kruleset(ruleset); 2290 2291 PF_RULES_WUNLOCK(); 2292 break; 2293 2294 #undef ERROUT 2295 DIOCCHANGERULE_error: 2296 PF_RULES_WUNLOCK(); 2297 if (newrule != NULL) { 2298 counter_u64_free(newrule->evaluations); 2299 for (int i = 0; i < 2; i++) { 2300 counter_u64_free(newrule->packets[i]); 2301 counter_u64_free(newrule->bytes[i]); 2302 } 2303 counter_u64_free(newrule->states_cur); 2304 counter_u64_free(newrule->states_tot); 2305 counter_u64_free(newrule->src_nodes); 2306 free(newrule, M_PFRULE); 2307 } 2308 if (kif != NULL) 2309 pf_kkif_free(kif); 2310 break; 2311 } 2312 2313 case DIOCCLRSTATES: { 2314 struct pf_state *s; 2315 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr; 2316 u_int i, killed = 0; 2317 2318 for (i = 0; i <= pf_hashmask; i++) { 2319 struct pf_idhash *ih = &V_pf_idhash[i]; 2320 2321 relock_DIOCCLRSTATES: 2322 PF_HASHROW_LOCK(ih); 2323 LIST_FOREACH(s, &ih->states, entry) 2324 if (!psk->psk_ifname[0] || 2325 !strcmp(psk->psk_ifname, 2326 s->kif->pfik_name)) { 2327 /* 2328 * Don't send out individual 2329 * delete messages. 2330 */ 2331 s->state_flags |= PFSTATE_NOSYNC; 2332 pf_unlink_state(s, PF_ENTER_LOCKED); 2333 killed++; 2334 goto relock_DIOCCLRSTATES; 2335 } 2336 PF_HASHROW_UNLOCK(ih); 2337 } 2338 psk->psk_killed = killed; 2339 if (V_pfsync_clear_states_ptr != NULL) 2340 V_pfsync_clear_states_ptr(V_pf_status.hostid, psk->psk_ifname); 2341 break; 2342 } 2343 2344 case DIOCKILLSTATES: { 2345 struct pf_state *s; 2346 struct pf_state_key *sk; 2347 struct pf_addr *srcaddr, *dstaddr; 2348 u_int16_t srcport, dstport; 2349 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr; 2350 u_int i, killed = 0; 2351 2352 if (psk->psk_pfcmp.id) { 2353 if (psk->psk_pfcmp.creatorid == 0) 2354 psk->psk_pfcmp.creatorid = V_pf_status.hostid; 2355 if ((s = pf_find_state_byid(psk->psk_pfcmp.id, 2356 psk->psk_pfcmp.creatorid))) { 2357 pf_unlink_state(s, PF_ENTER_LOCKED); 2358 psk->psk_killed = 1; 2359 } 2360 break; 2361 } 2362 2363 for (i = 0; i <= pf_hashmask; i++) { 2364 struct pf_idhash *ih = &V_pf_idhash[i]; 2365 2366 relock_DIOCKILLSTATES: 2367 PF_HASHROW_LOCK(ih); 2368 LIST_FOREACH(s, &ih->states, entry) { 2369 sk = s->key[PF_SK_WIRE]; 2370 if (s->direction == PF_OUT) { 2371 srcaddr = &sk->addr[1]; 2372 dstaddr = &sk->addr[0]; 2373 srcport = sk->port[1]; 2374 dstport = sk->port[0]; 2375 } else { 2376 srcaddr = &sk->addr[0]; 2377 dstaddr = &sk->addr[1]; 2378 srcport = sk->port[0]; 2379 dstport = sk->port[1]; 2380 } 2381 2382 if ((!psk->psk_af || sk->af == psk->psk_af) 2383 && (!psk->psk_proto || psk->psk_proto == 2384 sk->proto) && 2385 PF_MATCHA(psk->psk_src.neg, 2386 &psk->psk_src.addr.v.a.addr, 2387 &psk->psk_src.addr.v.a.mask, 2388 srcaddr, sk->af) && 2389 PF_MATCHA(psk->psk_dst.neg, 2390 &psk->psk_dst.addr.v.a.addr, 2391 &psk->psk_dst.addr.v.a.mask, 2392 dstaddr, sk->af) && 2393 (psk->psk_src.port_op == 0 || 2394 pf_match_port(psk->psk_src.port_op, 2395 psk->psk_src.port[0], psk->psk_src.port[1], 2396 srcport)) && 2397 (psk->psk_dst.port_op == 0 || 2398 pf_match_port(psk->psk_dst.port_op, 2399 psk->psk_dst.port[0], psk->psk_dst.port[1], 2400 dstport)) && 2401 (!psk->psk_label[0] || 2402 (s->rule.ptr->label[0] && 2403 !strcmp(psk->psk_label, 2404 s->rule.ptr->label))) && 2405 (!psk->psk_ifname[0] || 2406 !strcmp(psk->psk_ifname, 2407 s->kif->pfik_name))) { 2408 pf_unlink_state(s, PF_ENTER_LOCKED); 2409 killed++; 2410 goto relock_DIOCKILLSTATES; 2411 } 2412 } 2413 PF_HASHROW_UNLOCK(ih); 2414 } 2415 psk->psk_killed = killed; 2416 break; 2417 } 2418 2419 case DIOCADDSTATE: { 2420 struct pfioc_state *ps = (struct pfioc_state *)addr; 2421 struct pfsync_state *sp = &ps->state; 2422 2423 if (sp->timeout >= PFTM_MAX) { 2424 error = EINVAL; 2425 break; 2426 } 2427 if (V_pfsync_state_import_ptr != NULL) { 2428 PF_RULES_RLOCK(); 2429 error = V_pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL); 2430 PF_RULES_RUNLOCK(); 2431 } else 2432 error = EOPNOTSUPP; 2433 break; 2434 } 2435 2436 case DIOCGETSTATE: { 2437 struct pfioc_state *ps = (struct pfioc_state *)addr; 2438 struct pf_state *s; 2439 2440 s = pf_find_state_byid(ps->state.id, ps->state.creatorid); 2441 if (s == NULL) { 2442 error = ENOENT; 2443 break; 2444 } 2445 2446 pfsync_state_export(&ps->state, s); 2447 PF_STATE_UNLOCK(s); 2448 break; 2449 } 2450 2451 case DIOCGETSTATES: { 2452 struct pfioc_states *ps = (struct pfioc_states *)addr; 2453 struct pf_state *s; 2454 struct pfsync_state *pstore, *p; 2455 int i, nr; 2456 2457 if (ps->ps_len <= 0) { 2458 nr = uma_zone_get_cur(V_pf_state_z); 2459 ps->ps_len = sizeof(struct pfsync_state) * nr; 2460 break; 2461 } 2462 2463 p = pstore = malloc(ps->ps_len, M_TEMP, M_WAITOK | M_ZERO); 2464 nr = 0; 2465 2466 for (i = 0; i <= pf_hashmask; i++) { 2467 struct pf_idhash *ih = &V_pf_idhash[i]; 2468 2469 PF_HASHROW_LOCK(ih); 2470 LIST_FOREACH(s, &ih->states, entry) { 2471 if (s->timeout == PFTM_UNLINKED) 2472 continue; 2473 2474 if ((nr+1) * sizeof(*p) > ps->ps_len) { 2475 PF_HASHROW_UNLOCK(ih); 2476 goto DIOCGETSTATES_full; 2477 } 2478 pfsync_state_export(p, s); 2479 p++; 2480 nr++; 2481 } 2482 PF_HASHROW_UNLOCK(ih); 2483 } 2484 DIOCGETSTATES_full: 2485 error = copyout(pstore, ps->ps_states, 2486 sizeof(struct pfsync_state) * nr); 2487 if (error) { 2488 free(pstore, M_TEMP); 2489 break; 2490 } 2491 ps->ps_len = sizeof(struct pfsync_state) * nr; 2492 free(pstore, M_TEMP); 2493 2494 break; 2495 } 2496 2497 case DIOCGETSTATUS: { 2498 struct pf_status *s = (struct pf_status *)addr; 2499 2500 PF_RULES_RLOCK(); 2501 s->running = V_pf_status.running; 2502 s->since = V_pf_status.since; 2503 s->debug = V_pf_status.debug; 2504 s->hostid = V_pf_status.hostid; 2505 s->states = V_pf_status.states; 2506 s->src_nodes = V_pf_status.src_nodes; 2507 2508 for (int i = 0; i < PFRES_MAX; i++) 2509 s->counters[i] = 2510 counter_u64_fetch(V_pf_status.counters[i]); 2511 for (int i = 0; i < LCNT_MAX; i++) 2512 s->lcounters[i] = 2513 counter_u64_fetch(V_pf_status.lcounters[i]); 2514 for (int i = 0; i < FCNT_MAX; i++) 2515 s->fcounters[i] = 2516 counter_u64_fetch(V_pf_status.fcounters[i]); 2517 for (int i = 0; i < SCNT_MAX; i++) 2518 s->scounters[i] = 2519 counter_u64_fetch(V_pf_status.scounters[i]); 2520 2521 bcopy(V_pf_status.ifname, s->ifname, IFNAMSIZ); 2522 bcopy(V_pf_status.pf_chksum, s->pf_chksum, 2523 PF_MD5_DIGEST_LENGTH); 2524 2525 pfi_update_status(s->ifname, s); 2526 PF_RULES_RUNLOCK(); 2527 break; 2528 } 2529 2530 case DIOCSETSTATUSIF: { 2531 struct pfioc_if *pi = (struct pfioc_if *)addr; 2532 2533 if (pi->ifname[0] == 0) { 2534 bzero(V_pf_status.ifname, IFNAMSIZ); 2535 break; 2536 } 2537 PF_RULES_WLOCK(); 2538 strlcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ); 2539 PF_RULES_WUNLOCK(); 2540 break; 2541 } 2542 2543 case DIOCCLRSTATUS: { 2544 PF_RULES_WLOCK(); 2545 for (int i = 0; i < PFRES_MAX; i++) 2546 counter_u64_zero(V_pf_status.counters[i]); 2547 for (int i = 0; i < FCNT_MAX; i++) 2548 counter_u64_zero(V_pf_status.fcounters[i]); 2549 for (int i = 0; i < SCNT_MAX; i++) 2550 counter_u64_zero(V_pf_status.scounters[i]); 2551 for (int i = 0; i < LCNT_MAX; i++) 2552 counter_u64_zero(V_pf_status.lcounters[i]); 2553 V_pf_status.since = time_second; 2554 if (*V_pf_status.ifname) 2555 pfi_update_status(V_pf_status.ifname, NULL); 2556 PF_RULES_WUNLOCK(); 2557 break; 2558 } 2559 2560 case DIOCNATLOOK: { 2561 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr; 2562 struct pf_state_key *sk; 2563 struct pf_state *state; 2564 struct pf_state_key_cmp key; 2565 int m = 0, direction = pnl->direction; 2566 int sidx, didx; 2567 2568 /* NATLOOK src and dst are reversed, so reverse sidx/didx */ 2569 sidx = (direction == PF_IN) ? 1 : 0; 2570 didx = (direction == PF_IN) ? 0 : 1; 2571 2572 if (!pnl->proto || 2573 PF_AZERO(&pnl->saddr, pnl->af) || 2574 PF_AZERO(&pnl->daddr, pnl->af) || 2575 ((pnl->proto == IPPROTO_TCP || 2576 pnl->proto == IPPROTO_UDP) && 2577 (!pnl->dport || !pnl->sport))) 2578 error = EINVAL; 2579 else { 2580 bzero(&key, sizeof(key)); 2581 key.af = pnl->af; 2582 key.proto = pnl->proto; 2583 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af); 2584 key.port[sidx] = pnl->sport; 2585 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af); 2586 key.port[didx] = pnl->dport; 2587 2588 state = pf_find_state_all(&key, direction, &m); 2589 2590 if (m > 1) 2591 error = E2BIG; /* more than one state */ 2592 else if (state != NULL) { 2593 /* XXXGL: not locked read */ 2594 sk = state->key[sidx]; 2595 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af); 2596 pnl->rsport = sk->port[sidx]; 2597 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af); 2598 pnl->rdport = sk->port[didx]; 2599 } else 2600 error = ENOENT; 2601 } 2602 break; 2603 } 2604 2605 case DIOCSETTIMEOUT: { 2606 struct pfioc_tm *pt = (struct pfioc_tm *)addr; 2607 int old; 2608 2609 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX || 2610 pt->seconds < 0) { 2611 error = EINVAL; 2612 break; 2613 } 2614 PF_RULES_WLOCK(); 2615 old = V_pf_default_rule.timeout[pt->timeout]; 2616 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0) 2617 pt->seconds = 1; 2618 V_pf_default_rule.timeout[pt->timeout] = pt->seconds; 2619 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old) 2620 wakeup(pf_purge_thread); 2621 pt->seconds = old; 2622 PF_RULES_WUNLOCK(); 2623 break; 2624 } 2625 2626 case DIOCGETTIMEOUT: { 2627 struct pfioc_tm *pt = (struct pfioc_tm *)addr; 2628 2629 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) { 2630 error = EINVAL; 2631 break; 2632 } 2633 PF_RULES_RLOCK(); 2634 pt->seconds = V_pf_default_rule.timeout[pt->timeout]; 2635 PF_RULES_RUNLOCK(); 2636 break; 2637 } 2638 2639 case DIOCGETLIMIT: { 2640 struct pfioc_limit *pl = (struct pfioc_limit *)addr; 2641 2642 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) { 2643 error = EINVAL; 2644 break; 2645 } 2646 PF_RULES_RLOCK(); 2647 pl->limit = V_pf_limits[pl->index].limit; 2648 PF_RULES_RUNLOCK(); 2649 break; 2650 } 2651 2652 case DIOCSETLIMIT: { 2653 struct pfioc_limit *pl = (struct pfioc_limit *)addr; 2654 int old_limit; 2655 2656 PF_RULES_WLOCK(); 2657 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX || 2658 V_pf_limits[pl->index].zone == NULL) { 2659 PF_RULES_WUNLOCK(); 2660 error = EINVAL; 2661 break; 2662 } 2663 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit); 2664 old_limit = V_pf_limits[pl->index].limit; 2665 V_pf_limits[pl->index].limit = pl->limit; 2666 pl->limit = old_limit; 2667 PF_RULES_WUNLOCK(); 2668 break; 2669 } 2670 2671 case DIOCSETDEBUG: { 2672 u_int32_t *level = (u_int32_t *)addr; 2673 2674 PF_RULES_WLOCK(); 2675 V_pf_status.debug = *level; 2676 PF_RULES_WUNLOCK(); 2677 break; 2678 } 2679 2680 case DIOCCLRRULECTRS: { 2681 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */ 2682 struct pf_kruleset *ruleset = &pf_main_ruleset; 2683 struct pf_krule *rule; 2684 2685 PF_RULES_WLOCK(); 2686 TAILQ_FOREACH(rule, 2687 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) { 2688 counter_u64_zero(rule->evaluations); 2689 for (int i = 0; i < 2; i++) { 2690 counter_u64_zero(rule->packets[i]); 2691 counter_u64_zero(rule->bytes[i]); 2692 } 2693 } 2694 PF_RULES_WUNLOCK(); 2695 break; 2696 } 2697 2698 case DIOCGIFSPEEDV0: 2699 case DIOCGIFSPEEDV1: { 2700 struct pf_ifspeed_v1 *psp = (struct pf_ifspeed_v1 *)addr; 2701 struct pf_ifspeed_v1 ps; 2702 struct ifnet *ifp; 2703 2704 if (psp->ifname[0] != 0) { 2705 /* Can we completely trust user-land? */ 2706 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ); 2707 ifp = ifunit(ps.ifname); 2708 if (ifp != NULL) { 2709 psp->baudrate32 = 2710 (u_int32_t)uqmin(ifp->if_baudrate, UINT_MAX); 2711 if (cmd == DIOCGIFSPEEDV1) 2712 psp->baudrate = ifp->if_baudrate; 2713 } else 2714 error = EINVAL; 2715 } else 2716 error = EINVAL; 2717 break; 2718 } 2719 2720 #ifdef ALTQ 2721 case DIOCSTARTALTQ: { 2722 struct pf_altq *altq; 2723 2724 PF_RULES_WLOCK(); 2725 /* enable all altq interfaces on active list */ 2726 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 2727 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 2728 error = pf_enable_altq(altq); 2729 if (error != 0) 2730 break; 2731 } 2732 } 2733 if (error == 0) 2734 V_pf_altq_running = 1; 2735 PF_RULES_WUNLOCK(); 2736 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n")); 2737 break; 2738 } 2739 2740 case DIOCSTOPALTQ: { 2741 struct pf_altq *altq; 2742 2743 PF_RULES_WLOCK(); 2744 /* disable all altq interfaces on active list */ 2745 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 2746 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 2747 error = pf_disable_altq(altq); 2748 if (error != 0) 2749 break; 2750 } 2751 } 2752 if (error == 0) 2753 V_pf_altq_running = 0; 2754 PF_RULES_WUNLOCK(); 2755 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n")); 2756 break; 2757 } 2758 2759 case DIOCADDALTQV0: 2760 case DIOCADDALTQV1: { 2761 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 2762 struct pf_altq *altq, *a; 2763 struct ifnet *ifp; 2764 2765 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK | M_ZERO); 2766 error = pf_import_kaltq(pa, altq, IOCPARM_LEN(cmd)); 2767 if (error) 2768 break; 2769 altq->local_flags = 0; 2770 2771 PF_RULES_WLOCK(); 2772 if (pa->ticket != V_ticket_altqs_inactive) { 2773 PF_RULES_WUNLOCK(); 2774 free(altq, M_PFALTQ); 2775 error = EBUSY; 2776 break; 2777 } 2778 2779 /* 2780 * if this is for a queue, find the discipline and 2781 * copy the necessary fields 2782 */ 2783 if (altq->qname[0] != 0) { 2784 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) { 2785 PF_RULES_WUNLOCK(); 2786 error = EBUSY; 2787 free(altq, M_PFALTQ); 2788 break; 2789 } 2790 altq->altq_disc = NULL; 2791 TAILQ_FOREACH(a, V_pf_altq_ifs_inactive, entries) { 2792 if (strncmp(a->ifname, altq->ifname, 2793 IFNAMSIZ) == 0) { 2794 altq->altq_disc = a->altq_disc; 2795 break; 2796 } 2797 } 2798 } 2799 2800 if ((ifp = ifunit(altq->ifname)) == NULL) 2801 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED; 2802 else 2803 error = altq_add(ifp, altq); 2804 2805 if (error) { 2806 PF_RULES_WUNLOCK(); 2807 free(altq, M_PFALTQ); 2808 break; 2809 } 2810 2811 if (altq->qname[0] != 0) 2812 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries); 2813 else 2814 TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, altq, entries); 2815 /* version error check done on import above */ 2816 pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd)); 2817 PF_RULES_WUNLOCK(); 2818 break; 2819 } 2820 2821 case DIOCGETALTQSV0: 2822 case DIOCGETALTQSV1: { 2823 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 2824 struct pf_altq *altq; 2825 2826 PF_RULES_RLOCK(); 2827 pa->nr = 0; 2828 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) 2829 pa->nr++; 2830 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) 2831 pa->nr++; 2832 pa->ticket = V_ticket_altqs_active; 2833 PF_RULES_RUNLOCK(); 2834 break; 2835 } 2836 2837 case DIOCGETALTQV0: 2838 case DIOCGETALTQV1: { 2839 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 2840 struct pf_altq *altq; 2841 2842 PF_RULES_RLOCK(); 2843 if (pa->ticket != V_ticket_altqs_active) { 2844 PF_RULES_RUNLOCK(); 2845 error = EBUSY; 2846 break; 2847 } 2848 altq = pf_altq_get_nth_active(pa->nr); 2849 if (altq == NULL) { 2850 PF_RULES_RUNLOCK(); 2851 error = EBUSY; 2852 break; 2853 } 2854 pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd)); 2855 PF_RULES_RUNLOCK(); 2856 break; 2857 } 2858 2859 case DIOCCHANGEALTQV0: 2860 case DIOCCHANGEALTQV1: 2861 /* CHANGEALTQ not supported yet! */ 2862 error = ENODEV; 2863 break; 2864 2865 case DIOCGETQSTATSV0: 2866 case DIOCGETQSTATSV1: { 2867 struct pfioc_qstats_v1 *pq = (struct pfioc_qstats_v1 *)addr; 2868 struct pf_altq *altq; 2869 int nbytes; 2870 u_int32_t version; 2871 2872 PF_RULES_RLOCK(); 2873 if (pq->ticket != V_ticket_altqs_active) { 2874 PF_RULES_RUNLOCK(); 2875 error = EBUSY; 2876 break; 2877 } 2878 nbytes = pq->nbytes; 2879 altq = pf_altq_get_nth_active(pq->nr); 2880 if (altq == NULL) { 2881 PF_RULES_RUNLOCK(); 2882 error = EBUSY; 2883 break; 2884 } 2885 2886 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) { 2887 PF_RULES_RUNLOCK(); 2888 error = ENXIO; 2889 break; 2890 } 2891 PF_RULES_RUNLOCK(); 2892 if (cmd == DIOCGETQSTATSV0) 2893 version = 0; /* DIOCGETQSTATSV0 means stats struct v0 */ 2894 else 2895 version = pq->version; 2896 error = altq_getqstats(altq, pq->buf, &nbytes, version); 2897 if (error == 0) { 2898 pq->scheduler = altq->scheduler; 2899 pq->nbytes = nbytes; 2900 } 2901 break; 2902 } 2903 #endif /* ALTQ */ 2904 2905 case DIOCBEGINADDRS: { 2906 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 2907 2908 PF_RULES_WLOCK(); 2909 pf_empty_kpool(&V_pf_pabuf); 2910 pp->ticket = ++V_ticket_pabuf; 2911 PF_RULES_WUNLOCK(); 2912 break; 2913 } 2914 2915 case DIOCADDADDR: { 2916 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 2917 struct pf_kpooladdr *pa; 2918 struct pfi_kkif *kif = NULL; 2919 2920 #ifndef INET 2921 if (pp->af == AF_INET) { 2922 error = EAFNOSUPPORT; 2923 break; 2924 } 2925 #endif /* INET */ 2926 #ifndef INET6 2927 if (pp->af == AF_INET6) { 2928 error = EAFNOSUPPORT; 2929 break; 2930 } 2931 #endif /* INET6 */ 2932 if (pp->addr.addr.type != PF_ADDR_ADDRMASK && 2933 pp->addr.addr.type != PF_ADDR_DYNIFTL && 2934 pp->addr.addr.type != PF_ADDR_TABLE) { 2935 error = EINVAL; 2936 break; 2937 } 2938 if (pp->addr.addr.p.dyn != NULL) { 2939 error = EINVAL; 2940 break; 2941 } 2942 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK); 2943 pf_pooladdr_to_kpooladdr(&pp->addr, pa); 2944 if (pa->ifname[0]) 2945 kif = pf_kkif_create(M_WAITOK); 2946 PF_RULES_WLOCK(); 2947 if (pp->ticket != V_ticket_pabuf) { 2948 PF_RULES_WUNLOCK(); 2949 if (pa->ifname[0]) 2950 pf_kkif_free(kif); 2951 free(pa, M_PFRULE); 2952 error = EBUSY; 2953 break; 2954 } 2955 if (pa->ifname[0]) { 2956 pa->kif = pfi_kkif_attach(kif, pa->ifname); 2957 pfi_kkif_ref(pa->kif); 2958 } else 2959 pa->kif = NULL; 2960 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error = 2961 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) { 2962 if (pa->ifname[0]) 2963 pfi_kkif_unref(pa->kif); 2964 PF_RULES_WUNLOCK(); 2965 free(pa, M_PFRULE); 2966 break; 2967 } 2968 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries); 2969 PF_RULES_WUNLOCK(); 2970 break; 2971 } 2972 2973 case DIOCGETADDRS: { 2974 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 2975 struct pf_kpool *pool; 2976 struct pf_kpooladdr *pa; 2977 2978 PF_RULES_RLOCK(); 2979 pp->nr = 0; 2980 pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action, 2981 pp->r_num, 0, 1, 0); 2982 if (pool == NULL) { 2983 PF_RULES_RUNLOCK(); 2984 error = EBUSY; 2985 break; 2986 } 2987 TAILQ_FOREACH(pa, &pool->list, entries) 2988 pp->nr++; 2989 PF_RULES_RUNLOCK(); 2990 break; 2991 } 2992 2993 case DIOCGETADDR: { 2994 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 2995 struct pf_kpool *pool; 2996 struct pf_kpooladdr *pa; 2997 u_int32_t nr = 0; 2998 2999 PF_RULES_RLOCK(); 3000 pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action, 3001 pp->r_num, 0, 1, 1); 3002 if (pool == NULL) { 3003 PF_RULES_RUNLOCK(); 3004 error = EBUSY; 3005 break; 3006 } 3007 pa = TAILQ_FIRST(&pool->list); 3008 while ((pa != NULL) && (nr < pp->nr)) { 3009 pa = TAILQ_NEXT(pa, entries); 3010 nr++; 3011 } 3012 if (pa == NULL) { 3013 PF_RULES_RUNLOCK(); 3014 error = EBUSY; 3015 break; 3016 } 3017 pf_kpooladdr_to_pooladdr(pa, &pp->addr); 3018 pf_addr_copyout(&pp->addr.addr); 3019 PF_RULES_RUNLOCK(); 3020 break; 3021 } 3022 3023 case DIOCCHANGEADDR: { 3024 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr; 3025 struct pf_kpool *pool; 3026 struct pf_kpooladdr *oldpa = NULL, *newpa = NULL; 3027 struct pf_kruleset *ruleset; 3028 struct pfi_kkif *kif = NULL; 3029 3030 if (pca->action < PF_CHANGE_ADD_HEAD || 3031 pca->action > PF_CHANGE_REMOVE) { 3032 error = EINVAL; 3033 break; 3034 } 3035 if (pca->addr.addr.type != PF_ADDR_ADDRMASK && 3036 pca->addr.addr.type != PF_ADDR_DYNIFTL && 3037 pca->addr.addr.type != PF_ADDR_TABLE) { 3038 error = EINVAL; 3039 break; 3040 } 3041 if (pca->addr.addr.p.dyn != NULL) { 3042 error = EINVAL; 3043 break; 3044 } 3045 3046 if (pca->action != PF_CHANGE_REMOVE) { 3047 #ifndef INET 3048 if (pca->af == AF_INET) { 3049 error = EAFNOSUPPORT; 3050 break; 3051 } 3052 #endif /* INET */ 3053 #ifndef INET6 3054 if (pca->af == AF_INET6) { 3055 error = EAFNOSUPPORT; 3056 break; 3057 } 3058 #endif /* INET6 */ 3059 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK); 3060 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr)); 3061 if (newpa->ifname[0]) 3062 kif = pf_kkif_create(M_WAITOK); 3063 newpa->kif = NULL; 3064 } 3065 #define ERROUT(x) { error = (x); goto DIOCCHANGEADDR_error; } 3066 PF_RULES_WLOCK(); 3067 ruleset = pf_find_kruleset(pca->anchor); 3068 if (ruleset == NULL) 3069 ERROUT(EBUSY); 3070 3071 pool = pf_get_kpool(pca->anchor, pca->ticket, pca->r_action, 3072 pca->r_num, pca->r_last, 1, 1); 3073 if (pool == NULL) 3074 ERROUT(EBUSY); 3075 3076 if (pca->action != PF_CHANGE_REMOVE) { 3077 if (newpa->ifname[0]) { 3078 newpa->kif = pfi_kkif_attach(kif, newpa->ifname); 3079 pfi_kkif_ref(newpa->kif); 3080 kif = NULL; 3081 } 3082 3083 switch (newpa->addr.type) { 3084 case PF_ADDR_DYNIFTL: 3085 error = pfi_dynaddr_setup(&newpa->addr, 3086 pca->af); 3087 break; 3088 case PF_ADDR_TABLE: 3089 newpa->addr.p.tbl = pfr_attach_table(ruleset, 3090 newpa->addr.v.tblname); 3091 if (newpa->addr.p.tbl == NULL) 3092 error = ENOMEM; 3093 break; 3094 } 3095 if (error) 3096 goto DIOCCHANGEADDR_error; 3097 } 3098 3099 switch (pca->action) { 3100 case PF_CHANGE_ADD_HEAD: 3101 oldpa = TAILQ_FIRST(&pool->list); 3102 break; 3103 case PF_CHANGE_ADD_TAIL: 3104 oldpa = TAILQ_LAST(&pool->list, pf_kpalist); 3105 break; 3106 default: 3107 oldpa = TAILQ_FIRST(&pool->list); 3108 for (int i = 0; oldpa && i < pca->nr; i++) 3109 oldpa = TAILQ_NEXT(oldpa, entries); 3110 3111 if (oldpa == NULL) 3112 ERROUT(EINVAL); 3113 } 3114 3115 if (pca->action == PF_CHANGE_REMOVE) { 3116 TAILQ_REMOVE(&pool->list, oldpa, entries); 3117 switch (oldpa->addr.type) { 3118 case PF_ADDR_DYNIFTL: 3119 pfi_dynaddr_remove(oldpa->addr.p.dyn); 3120 break; 3121 case PF_ADDR_TABLE: 3122 pfr_detach_table(oldpa->addr.p.tbl); 3123 break; 3124 } 3125 if (oldpa->kif) 3126 pfi_kkif_unref(oldpa->kif); 3127 free(oldpa, M_PFRULE); 3128 } else { 3129 if (oldpa == NULL) 3130 TAILQ_INSERT_TAIL(&pool->list, newpa, entries); 3131 else if (pca->action == PF_CHANGE_ADD_HEAD || 3132 pca->action == PF_CHANGE_ADD_BEFORE) 3133 TAILQ_INSERT_BEFORE(oldpa, newpa, entries); 3134 else 3135 TAILQ_INSERT_AFTER(&pool->list, oldpa, 3136 newpa, entries); 3137 } 3138 3139 pool->cur = TAILQ_FIRST(&pool->list); 3140 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af); 3141 PF_RULES_WUNLOCK(); 3142 break; 3143 3144 #undef ERROUT 3145 DIOCCHANGEADDR_error: 3146 if (newpa != NULL) { 3147 if (newpa->kif) 3148 pfi_kkif_unref(newpa->kif); 3149 free(newpa, M_PFRULE); 3150 } 3151 PF_RULES_WUNLOCK(); 3152 if (kif != NULL) 3153 pf_kkif_free(kif); 3154 break; 3155 } 3156 3157 case DIOCGETRULESETS: { 3158 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr; 3159 struct pf_kruleset *ruleset; 3160 struct pf_kanchor *anchor; 3161 3162 PF_RULES_RLOCK(); 3163 pr->path[sizeof(pr->path) - 1] = 0; 3164 if ((ruleset = pf_find_kruleset(pr->path)) == NULL) { 3165 PF_RULES_RUNLOCK(); 3166 error = ENOENT; 3167 break; 3168 } 3169 pr->nr = 0; 3170 if (ruleset->anchor == NULL) { 3171 /* XXX kludge for pf_main_ruleset */ 3172 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) 3173 if (anchor->parent == NULL) 3174 pr->nr++; 3175 } else { 3176 RB_FOREACH(anchor, pf_kanchor_node, 3177 &ruleset->anchor->children) 3178 pr->nr++; 3179 } 3180 PF_RULES_RUNLOCK(); 3181 break; 3182 } 3183 3184 case DIOCGETRULESET: { 3185 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr; 3186 struct pf_kruleset *ruleset; 3187 struct pf_kanchor *anchor; 3188 u_int32_t nr = 0; 3189 3190 PF_RULES_RLOCK(); 3191 pr->path[sizeof(pr->path) - 1] = 0; 3192 if ((ruleset = pf_find_kruleset(pr->path)) == NULL) { 3193 PF_RULES_RUNLOCK(); 3194 error = ENOENT; 3195 break; 3196 } 3197 pr->name[0] = 0; 3198 if (ruleset->anchor == NULL) { 3199 /* XXX kludge for pf_main_ruleset */ 3200 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) 3201 if (anchor->parent == NULL && nr++ == pr->nr) { 3202 strlcpy(pr->name, anchor->name, 3203 sizeof(pr->name)); 3204 break; 3205 } 3206 } else { 3207 RB_FOREACH(anchor, pf_kanchor_node, 3208 &ruleset->anchor->children) 3209 if (nr++ == pr->nr) { 3210 strlcpy(pr->name, anchor->name, 3211 sizeof(pr->name)); 3212 break; 3213 } 3214 } 3215 if (!pr->name[0]) 3216 error = EBUSY; 3217 PF_RULES_RUNLOCK(); 3218 break; 3219 } 3220 3221 case DIOCRCLRTABLES: { 3222 struct pfioc_table *io = (struct pfioc_table *)addr; 3223 3224 if (io->pfrio_esize != 0) { 3225 error = ENODEV; 3226 break; 3227 } 3228 PF_RULES_WLOCK(); 3229 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel, 3230 io->pfrio_flags | PFR_FLAG_USERIOCTL); 3231 PF_RULES_WUNLOCK(); 3232 break; 3233 } 3234 3235 case DIOCRADDTABLES: { 3236 struct pfioc_table *io = (struct pfioc_table *)addr; 3237 struct pfr_table *pfrts; 3238 size_t totlen; 3239 3240 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3241 error = ENODEV; 3242 break; 3243 } 3244 3245 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 3246 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 3247 error = ENOMEM; 3248 break; 3249 } 3250 3251 totlen = io->pfrio_size * sizeof(struct pfr_table); 3252 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3253 M_TEMP, M_WAITOK); 3254 error = copyin(io->pfrio_buffer, pfrts, totlen); 3255 if (error) { 3256 free(pfrts, M_TEMP); 3257 break; 3258 } 3259 PF_RULES_WLOCK(); 3260 error = pfr_add_tables(pfrts, io->pfrio_size, 3261 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3262 PF_RULES_WUNLOCK(); 3263 free(pfrts, M_TEMP); 3264 break; 3265 } 3266 3267 case DIOCRDELTABLES: { 3268 struct pfioc_table *io = (struct pfioc_table *)addr; 3269 struct pfr_table *pfrts; 3270 size_t totlen; 3271 3272 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3273 error = ENODEV; 3274 break; 3275 } 3276 3277 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 3278 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 3279 error = ENOMEM; 3280 break; 3281 } 3282 3283 totlen = io->pfrio_size * sizeof(struct pfr_table); 3284 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3285 M_TEMP, M_WAITOK); 3286 error = copyin(io->pfrio_buffer, pfrts, totlen); 3287 if (error) { 3288 free(pfrts, M_TEMP); 3289 break; 3290 } 3291 PF_RULES_WLOCK(); 3292 error = pfr_del_tables(pfrts, io->pfrio_size, 3293 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3294 PF_RULES_WUNLOCK(); 3295 free(pfrts, M_TEMP); 3296 break; 3297 } 3298 3299 case DIOCRGETTABLES: { 3300 struct pfioc_table *io = (struct pfioc_table *)addr; 3301 struct pfr_table *pfrts; 3302 size_t totlen; 3303 int n; 3304 3305 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3306 error = ENODEV; 3307 break; 3308 } 3309 PF_RULES_RLOCK(); 3310 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 3311 if (n < 0) { 3312 PF_RULES_RUNLOCK(); 3313 error = EINVAL; 3314 break; 3315 } 3316 io->pfrio_size = min(io->pfrio_size, n); 3317 3318 totlen = io->pfrio_size * sizeof(struct pfr_table); 3319 3320 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3321 M_TEMP, M_NOWAIT); 3322 if (pfrts == NULL) { 3323 error = ENOMEM; 3324 PF_RULES_RUNLOCK(); 3325 break; 3326 } 3327 error = pfr_get_tables(&io->pfrio_table, pfrts, 3328 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3329 PF_RULES_RUNLOCK(); 3330 if (error == 0) 3331 error = copyout(pfrts, io->pfrio_buffer, totlen); 3332 free(pfrts, M_TEMP); 3333 break; 3334 } 3335 3336 case DIOCRGETTSTATS: { 3337 struct pfioc_table *io = (struct pfioc_table *)addr; 3338 struct pfr_tstats *pfrtstats; 3339 size_t totlen; 3340 int n; 3341 3342 if (io->pfrio_esize != sizeof(struct pfr_tstats)) { 3343 error = ENODEV; 3344 break; 3345 } 3346 PF_RULES_WLOCK(); 3347 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 3348 if (n < 0) { 3349 PF_RULES_WUNLOCK(); 3350 error = EINVAL; 3351 break; 3352 } 3353 io->pfrio_size = min(io->pfrio_size, n); 3354 3355 totlen = io->pfrio_size * sizeof(struct pfr_tstats); 3356 pfrtstats = mallocarray(io->pfrio_size, 3357 sizeof(struct pfr_tstats), M_TEMP, M_NOWAIT); 3358 if (pfrtstats == NULL) { 3359 error = ENOMEM; 3360 PF_RULES_WUNLOCK(); 3361 break; 3362 } 3363 error = pfr_get_tstats(&io->pfrio_table, pfrtstats, 3364 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3365 PF_RULES_WUNLOCK(); 3366 if (error == 0) 3367 error = copyout(pfrtstats, io->pfrio_buffer, totlen); 3368 free(pfrtstats, M_TEMP); 3369 break; 3370 } 3371 3372 case DIOCRCLRTSTATS: { 3373 struct pfioc_table *io = (struct pfioc_table *)addr; 3374 struct pfr_table *pfrts; 3375 size_t totlen; 3376 3377 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3378 error = ENODEV; 3379 break; 3380 } 3381 3382 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 3383 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 3384 /* We used to count tables and use the minimum required 3385 * size, so we didn't fail on overly large requests. 3386 * Keep doing so. */ 3387 io->pfrio_size = pf_ioctl_maxcount; 3388 break; 3389 } 3390 3391 totlen = io->pfrio_size * sizeof(struct pfr_table); 3392 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3393 M_TEMP, M_NOWAIT); 3394 if (pfrts == NULL) { 3395 error = ENOMEM; 3396 break; 3397 } 3398 error = copyin(io->pfrio_buffer, pfrts, totlen); 3399 if (error) { 3400 free(pfrts, M_TEMP); 3401 break; 3402 } 3403 3404 PF_RULES_WLOCK(); 3405 error = pfr_clr_tstats(pfrts, io->pfrio_size, 3406 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3407 PF_RULES_WUNLOCK(); 3408 free(pfrts, M_TEMP); 3409 break; 3410 } 3411 3412 case DIOCRSETTFLAGS: { 3413 struct pfioc_table *io = (struct pfioc_table *)addr; 3414 struct pfr_table *pfrts; 3415 size_t totlen; 3416 int n; 3417 3418 if (io->pfrio_esize != sizeof(struct pfr_table)) { 3419 error = ENODEV; 3420 break; 3421 } 3422 3423 PF_RULES_RLOCK(); 3424 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 3425 if (n < 0) { 3426 PF_RULES_RUNLOCK(); 3427 error = EINVAL; 3428 break; 3429 } 3430 3431 io->pfrio_size = min(io->pfrio_size, n); 3432 PF_RULES_RUNLOCK(); 3433 3434 totlen = io->pfrio_size * sizeof(struct pfr_table); 3435 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 3436 M_TEMP, M_WAITOK); 3437 error = copyin(io->pfrio_buffer, pfrts, totlen); 3438 if (error) { 3439 free(pfrts, M_TEMP); 3440 break; 3441 } 3442 PF_RULES_WLOCK(); 3443 error = pfr_set_tflags(pfrts, io->pfrio_size, 3444 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange, 3445 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3446 PF_RULES_WUNLOCK(); 3447 free(pfrts, M_TEMP); 3448 break; 3449 } 3450 3451 case DIOCRCLRADDRS: { 3452 struct pfioc_table *io = (struct pfioc_table *)addr; 3453 3454 if (io->pfrio_esize != 0) { 3455 error = ENODEV; 3456 break; 3457 } 3458 PF_RULES_WLOCK(); 3459 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel, 3460 io->pfrio_flags | PFR_FLAG_USERIOCTL); 3461 PF_RULES_WUNLOCK(); 3462 break; 3463 } 3464 3465 case DIOCRADDADDRS: { 3466 struct pfioc_table *io = (struct pfioc_table *)addr; 3467 struct pfr_addr *pfras; 3468 size_t totlen; 3469 3470 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3471 error = ENODEV; 3472 break; 3473 } 3474 if (io->pfrio_size < 0 || 3475 io->pfrio_size > pf_ioctl_maxcount || 3476 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3477 error = EINVAL; 3478 break; 3479 } 3480 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3481 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3482 M_TEMP, M_NOWAIT); 3483 if (! pfras) { 3484 error = ENOMEM; 3485 break; 3486 } 3487 error = copyin(io->pfrio_buffer, pfras, totlen); 3488 if (error) { 3489 free(pfras, M_TEMP); 3490 break; 3491 } 3492 PF_RULES_WLOCK(); 3493 error = pfr_add_addrs(&io->pfrio_table, pfras, 3494 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags | 3495 PFR_FLAG_USERIOCTL); 3496 PF_RULES_WUNLOCK(); 3497 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 3498 error = copyout(pfras, io->pfrio_buffer, totlen); 3499 free(pfras, M_TEMP); 3500 break; 3501 } 3502 3503 case DIOCRDELADDRS: { 3504 struct pfioc_table *io = (struct pfioc_table *)addr; 3505 struct pfr_addr *pfras; 3506 size_t totlen; 3507 3508 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3509 error = ENODEV; 3510 break; 3511 } 3512 if (io->pfrio_size < 0 || 3513 io->pfrio_size > pf_ioctl_maxcount || 3514 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3515 error = EINVAL; 3516 break; 3517 } 3518 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3519 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3520 M_TEMP, M_NOWAIT); 3521 if (! pfras) { 3522 error = ENOMEM; 3523 break; 3524 } 3525 error = copyin(io->pfrio_buffer, pfras, totlen); 3526 if (error) { 3527 free(pfras, M_TEMP); 3528 break; 3529 } 3530 PF_RULES_WLOCK(); 3531 error = pfr_del_addrs(&io->pfrio_table, pfras, 3532 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags | 3533 PFR_FLAG_USERIOCTL); 3534 PF_RULES_WUNLOCK(); 3535 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 3536 error = copyout(pfras, io->pfrio_buffer, totlen); 3537 free(pfras, M_TEMP); 3538 break; 3539 } 3540 3541 case DIOCRSETADDRS: { 3542 struct pfioc_table *io = (struct pfioc_table *)addr; 3543 struct pfr_addr *pfras; 3544 size_t totlen, count; 3545 3546 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3547 error = ENODEV; 3548 break; 3549 } 3550 if (io->pfrio_size < 0 || io->pfrio_size2 < 0) { 3551 error = EINVAL; 3552 break; 3553 } 3554 count = max(io->pfrio_size, io->pfrio_size2); 3555 if (count > pf_ioctl_maxcount || 3556 WOULD_OVERFLOW(count, sizeof(struct pfr_addr))) { 3557 error = EINVAL; 3558 break; 3559 } 3560 totlen = count * sizeof(struct pfr_addr); 3561 pfras = mallocarray(count, sizeof(struct pfr_addr), M_TEMP, 3562 M_NOWAIT); 3563 if (! pfras) { 3564 error = ENOMEM; 3565 break; 3566 } 3567 error = copyin(io->pfrio_buffer, pfras, totlen); 3568 if (error) { 3569 free(pfras, M_TEMP); 3570 break; 3571 } 3572 PF_RULES_WLOCK(); 3573 error = pfr_set_addrs(&io->pfrio_table, pfras, 3574 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd, 3575 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags | 3576 PFR_FLAG_USERIOCTL, 0); 3577 PF_RULES_WUNLOCK(); 3578 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 3579 error = copyout(pfras, io->pfrio_buffer, totlen); 3580 free(pfras, M_TEMP); 3581 break; 3582 } 3583 3584 case DIOCRGETADDRS: { 3585 struct pfioc_table *io = (struct pfioc_table *)addr; 3586 struct pfr_addr *pfras; 3587 size_t totlen; 3588 3589 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3590 error = ENODEV; 3591 break; 3592 } 3593 if (io->pfrio_size < 0 || 3594 io->pfrio_size > pf_ioctl_maxcount || 3595 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3596 error = EINVAL; 3597 break; 3598 } 3599 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3600 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3601 M_TEMP, M_NOWAIT); 3602 if (! pfras) { 3603 error = ENOMEM; 3604 break; 3605 } 3606 PF_RULES_RLOCK(); 3607 error = pfr_get_addrs(&io->pfrio_table, pfras, 3608 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3609 PF_RULES_RUNLOCK(); 3610 if (error == 0) 3611 error = copyout(pfras, io->pfrio_buffer, totlen); 3612 free(pfras, M_TEMP); 3613 break; 3614 } 3615 3616 case DIOCRGETASTATS: { 3617 struct pfioc_table *io = (struct pfioc_table *)addr; 3618 struct pfr_astats *pfrastats; 3619 size_t totlen; 3620 3621 if (io->pfrio_esize != sizeof(struct pfr_astats)) { 3622 error = ENODEV; 3623 break; 3624 } 3625 if (io->pfrio_size < 0 || 3626 io->pfrio_size > pf_ioctl_maxcount || 3627 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_astats))) { 3628 error = EINVAL; 3629 break; 3630 } 3631 totlen = io->pfrio_size * sizeof(struct pfr_astats); 3632 pfrastats = mallocarray(io->pfrio_size, 3633 sizeof(struct pfr_astats), M_TEMP, M_NOWAIT); 3634 if (! pfrastats) { 3635 error = ENOMEM; 3636 break; 3637 } 3638 PF_RULES_RLOCK(); 3639 error = pfr_get_astats(&io->pfrio_table, pfrastats, 3640 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3641 PF_RULES_RUNLOCK(); 3642 if (error == 0) 3643 error = copyout(pfrastats, io->pfrio_buffer, totlen); 3644 free(pfrastats, M_TEMP); 3645 break; 3646 } 3647 3648 case DIOCRCLRASTATS: { 3649 struct pfioc_table *io = (struct pfioc_table *)addr; 3650 struct pfr_addr *pfras; 3651 size_t totlen; 3652 3653 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3654 error = ENODEV; 3655 break; 3656 } 3657 if (io->pfrio_size < 0 || 3658 io->pfrio_size > pf_ioctl_maxcount || 3659 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3660 error = EINVAL; 3661 break; 3662 } 3663 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3664 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3665 M_TEMP, M_NOWAIT); 3666 if (! pfras) { 3667 error = ENOMEM; 3668 break; 3669 } 3670 error = copyin(io->pfrio_buffer, pfras, totlen); 3671 if (error) { 3672 free(pfras, M_TEMP); 3673 break; 3674 } 3675 PF_RULES_WLOCK(); 3676 error = pfr_clr_astats(&io->pfrio_table, pfras, 3677 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags | 3678 PFR_FLAG_USERIOCTL); 3679 PF_RULES_WUNLOCK(); 3680 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 3681 error = copyout(pfras, io->pfrio_buffer, totlen); 3682 free(pfras, M_TEMP); 3683 break; 3684 } 3685 3686 case DIOCRTSTADDRS: { 3687 struct pfioc_table *io = (struct pfioc_table *)addr; 3688 struct pfr_addr *pfras; 3689 size_t totlen; 3690 3691 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3692 error = ENODEV; 3693 break; 3694 } 3695 if (io->pfrio_size < 0 || 3696 io->pfrio_size > pf_ioctl_maxcount || 3697 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3698 error = EINVAL; 3699 break; 3700 } 3701 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3702 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3703 M_TEMP, M_NOWAIT); 3704 if (! pfras) { 3705 error = ENOMEM; 3706 break; 3707 } 3708 error = copyin(io->pfrio_buffer, pfras, totlen); 3709 if (error) { 3710 free(pfras, M_TEMP); 3711 break; 3712 } 3713 PF_RULES_RLOCK(); 3714 error = pfr_tst_addrs(&io->pfrio_table, pfras, 3715 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags | 3716 PFR_FLAG_USERIOCTL); 3717 PF_RULES_RUNLOCK(); 3718 if (error == 0) 3719 error = copyout(pfras, io->pfrio_buffer, totlen); 3720 free(pfras, M_TEMP); 3721 break; 3722 } 3723 3724 case DIOCRINADEFINE: { 3725 struct pfioc_table *io = (struct pfioc_table *)addr; 3726 struct pfr_addr *pfras; 3727 size_t totlen; 3728 3729 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 3730 error = ENODEV; 3731 break; 3732 } 3733 if (io->pfrio_size < 0 || 3734 io->pfrio_size > pf_ioctl_maxcount || 3735 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 3736 error = EINVAL; 3737 break; 3738 } 3739 totlen = io->pfrio_size * sizeof(struct pfr_addr); 3740 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 3741 M_TEMP, M_NOWAIT); 3742 if (! pfras) { 3743 error = ENOMEM; 3744 break; 3745 } 3746 error = copyin(io->pfrio_buffer, pfras, totlen); 3747 if (error) { 3748 free(pfras, M_TEMP); 3749 break; 3750 } 3751 PF_RULES_WLOCK(); 3752 error = pfr_ina_define(&io->pfrio_table, pfras, 3753 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr, 3754 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL); 3755 PF_RULES_WUNLOCK(); 3756 free(pfras, M_TEMP); 3757 break; 3758 } 3759 3760 case DIOCOSFPADD: { 3761 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr; 3762 PF_RULES_WLOCK(); 3763 error = pf_osfp_add(io); 3764 PF_RULES_WUNLOCK(); 3765 break; 3766 } 3767 3768 case DIOCOSFPGET: { 3769 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr; 3770 PF_RULES_RLOCK(); 3771 error = pf_osfp_get(io); 3772 PF_RULES_RUNLOCK(); 3773 break; 3774 } 3775 3776 case DIOCXBEGIN: { 3777 struct pfioc_trans *io = (struct pfioc_trans *)addr; 3778 struct pfioc_trans_e *ioes, *ioe; 3779 size_t totlen; 3780 int i; 3781 3782 if (io->esize != sizeof(*ioe)) { 3783 error = ENODEV; 3784 break; 3785 } 3786 if (io->size < 0 || 3787 io->size > pf_ioctl_maxcount || 3788 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 3789 error = EINVAL; 3790 break; 3791 } 3792 totlen = sizeof(struct pfioc_trans_e) * io->size; 3793 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 3794 M_TEMP, M_NOWAIT); 3795 if (! ioes) { 3796 error = ENOMEM; 3797 break; 3798 } 3799 error = copyin(io->array, ioes, totlen); 3800 if (error) { 3801 free(ioes, M_TEMP); 3802 break; 3803 } 3804 PF_RULES_WLOCK(); 3805 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 3806 switch (ioe->rs_num) { 3807 #ifdef ALTQ 3808 case PF_RULESET_ALTQ: 3809 if (ioe->anchor[0]) { 3810 PF_RULES_WUNLOCK(); 3811 free(ioes, M_TEMP); 3812 error = EINVAL; 3813 goto fail; 3814 } 3815 if ((error = pf_begin_altq(&ioe->ticket))) { 3816 PF_RULES_WUNLOCK(); 3817 free(ioes, M_TEMP); 3818 goto fail; 3819 } 3820 break; 3821 #endif /* ALTQ */ 3822 case PF_RULESET_TABLE: 3823 { 3824 struct pfr_table table; 3825 3826 bzero(&table, sizeof(table)); 3827 strlcpy(table.pfrt_anchor, ioe->anchor, 3828 sizeof(table.pfrt_anchor)); 3829 if ((error = pfr_ina_begin(&table, 3830 &ioe->ticket, NULL, 0))) { 3831 PF_RULES_WUNLOCK(); 3832 free(ioes, M_TEMP); 3833 goto fail; 3834 } 3835 break; 3836 } 3837 default: 3838 if ((error = pf_begin_rules(&ioe->ticket, 3839 ioe->rs_num, ioe->anchor))) { 3840 PF_RULES_WUNLOCK(); 3841 free(ioes, M_TEMP); 3842 goto fail; 3843 } 3844 break; 3845 } 3846 } 3847 PF_RULES_WUNLOCK(); 3848 error = copyout(ioes, io->array, totlen); 3849 free(ioes, M_TEMP); 3850 break; 3851 } 3852 3853 case DIOCXROLLBACK: { 3854 struct pfioc_trans *io = (struct pfioc_trans *)addr; 3855 struct pfioc_trans_e *ioe, *ioes; 3856 size_t totlen; 3857 int i; 3858 3859 if (io->esize != sizeof(*ioe)) { 3860 error = ENODEV; 3861 break; 3862 } 3863 if (io->size < 0 || 3864 io->size > pf_ioctl_maxcount || 3865 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 3866 error = EINVAL; 3867 break; 3868 } 3869 totlen = sizeof(struct pfioc_trans_e) * io->size; 3870 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 3871 M_TEMP, M_NOWAIT); 3872 if (! ioes) { 3873 error = ENOMEM; 3874 break; 3875 } 3876 error = copyin(io->array, ioes, totlen); 3877 if (error) { 3878 free(ioes, M_TEMP); 3879 break; 3880 } 3881 PF_RULES_WLOCK(); 3882 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 3883 switch (ioe->rs_num) { 3884 #ifdef ALTQ 3885 case PF_RULESET_ALTQ: 3886 if (ioe->anchor[0]) { 3887 PF_RULES_WUNLOCK(); 3888 free(ioes, M_TEMP); 3889 error = EINVAL; 3890 goto fail; 3891 } 3892 if ((error = pf_rollback_altq(ioe->ticket))) { 3893 PF_RULES_WUNLOCK(); 3894 free(ioes, M_TEMP); 3895 goto fail; /* really bad */ 3896 } 3897 break; 3898 #endif /* ALTQ */ 3899 case PF_RULESET_TABLE: 3900 { 3901 struct pfr_table table; 3902 3903 bzero(&table, sizeof(table)); 3904 strlcpy(table.pfrt_anchor, ioe->anchor, 3905 sizeof(table.pfrt_anchor)); 3906 if ((error = pfr_ina_rollback(&table, 3907 ioe->ticket, NULL, 0))) { 3908 PF_RULES_WUNLOCK(); 3909 free(ioes, M_TEMP); 3910 goto fail; /* really bad */ 3911 } 3912 break; 3913 } 3914 default: 3915 if ((error = pf_rollback_rules(ioe->ticket, 3916 ioe->rs_num, ioe->anchor))) { 3917 PF_RULES_WUNLOCK(); 3918 free(ioes, M_TEMP); 3919 goto fail; /* really bad */ 3920 } 3921 break; 3922 } 3923 } 3924 PF_RULES_WUNLOCK(); 3925 free(ioes, M_TEMP); 3926 break; 3927 } 3928 3929 case DIOCXCOMMIT: { 3930 struct pfioc_trans *io = (struct pfioc_trans *)addr; 3931 struct pfioc_trans_e *ioe, *ioes; 3932 struct pf_kruleset *rs; 3933 size_t totlen; 3934 int i; 3935 3936 if (io->esize != sizeof(*ioe)) { 3937 error = ENODEV; 3938 break; 3939 } 3940 3941 if (io->size < 0 || 3942 io->size > pf_ioctl_maxcount || 3943 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 3944 error = EINVAL; 3945 break; 3946 } 3947 3948 totlen = sizeof(struct pfioc_trans_e) * io->size; 3949 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 3950 M_TEMP, M_NOWAIT); 3951 if (ioes == NULL) { 3952 error = ENOMEM; 3953 break; 3954 } 3955 error = copyin(io->array, ioes, totlen); 3956 if (error) { 3957 free(ioes, M_TEMP); 3958 break; 3959 } 3960 PF_RULES_WLOCK(); 3961 /* First makes sure everything will succeed. */ 3962 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 3963 switch (ioe->rs_num) { 3964 #ifdef ALTQ 3965 case PF_RULESET_ALTQ: 3966 if (ioe->anchor[0]) { 3967 PF_RULES_WUNLOCK(); 3968 free(ioes, M_TEMP); 3969 error = EINVAL; 3970 goto fail; 3971 } 3972 if (!V_altqs_inactive_open || ioe->ticket != 3973 V_ticket_altqs_inactive) { 3974 PF_RULES_WUNLOCK(); 3975 free(ioes, M_TEMP); 3976 error = EBUSY; 3977 goto fail; 3978 } 3979 break; 3980 #endif /* ALTQ */ 3981 case PF_RULESET_TABLE: 3982 rs = pf_find_kruleset(ioe->anchor); 3983 if (rs == NULL || !rs->topen || ioe->ticket != 3984 rs->tticket) { 3985 PF_RULES_WUNLOCK(); 3986 free(ioes, M_TEMP); 3987 error = EBUSY; 3988 goto fail; 3989 } 3990 break; 3991 default: 3992 if (ioe->rs_num < 0 || ioe->rs_num >= 3993 PF_RULESET_MAX) { 3994 PF_RULES_WUNLOCK(); 3995 free(ioes, M_TEMP); 3996 error = EINVAL; 3997 goto fail; 3998 } 3999 rs = pf_find_kruleset(ioe->anchor); 4000 if (rs == NULL || 4001 !rs->rules[ioe->rs_num].inactive.open || 4002 rs->rules[ioe->rs_num].inactive.ticket != 4003 ioe->ticket) { 4004 PF_RULES_WUNLOCK(); 4005 free(ioes, M_TEMP); 4006 error = EBUSY; 4007 goto fail; 4008 } 4009 break; 4010 } 4011 } 4012 /* Now do the commit - no errors should happen here. */ 4013 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 4014 switch (ioe->rs_num) { 4015 #ifdef ALTQ 4016 case PF_RULESET_ALTQ: 4017 if ((error = pf_commit_altq(ioe->ticket))) { 4018 PF_RULES_WUNLOCK(); 4019 free(ioes, M_TEMP); 4020 goto fail; /* really bad */ 4021 } 4022 break; 4023 #endif /* ALTQ */ 4024 case PF_RULESET_TABLE: 4025 { 4026 struct pfr_table table; 4027 4028 bzero(&table, sizeof(table)); 4029 strlcpy(table.pfrt_anchor, ioe->anchor, 4030 sizeof(table.pfrt_anchor)); 4031 if ((error = pfr_ina_commit(&table, 4032 ioe->ticket, NULL, NULL, 0))) { 4033 PF_RULES_WUNLOCK(); 4034 free(ioes, M_TEMP); 4035 goto fail; /* really bad */ 4036 } 4037 break; 4038 } 4039 default: 4040 if ((error = pf_commit_rules(ioe->ticket, 4041 ioe->rs_num, ioe->anchor))) { 4042 PF_RULES_WUNLOCK(); 4043 free(ioes, M_TEMP); 4044 goto fail; /* really bad */ 4045 } 4046 break; 4047 } 4048 } 4049 PF_RULES_WUNLOCK(); 4050 free(ioes, M_TEMP); 4051 break; 4052 } 4053 4054 case DIOCGETSRCNODES: { 4055 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr; 4056 struct pf_srchash *sh; 4057 struct pf_ksrc_node *n; 4058 struct pf_src_node *p, *pstore; 4059 uint32_t i, nr = 0; 4060 4061 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 4062 i++, sh++) { 4063 PF_HASHROW_LOCK(sh); 4064 LIST_FOREACH(n, &sh->nodes, entry) 4065 nr++; 4066 PF_HASHROW_UNLOCK(sh); 4067 } 4068 4069 psn->psn_len = min(psn->psn_len, 4070 sizeof(struct pf_src_node) * nr); 4071 4072 if (psn->psn_len == 0) { 4073 psn->psn_len = sizeof(struct pf_src_node) * nr; 4074 break; 4075 } 4076 4077 nr = 0; 4078 4079 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK | M_ZERO); 4080 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 4081 i++, sh++) { 4082 PF_HASHROW_LOCK(sh); 4083 LIST_FOREACH(n, &sh->nodes, entry) { 4084 4085 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len) 4086 break; 4087 4088 pf_src_node_copy(n, p); 4089 4090 p++; 4091 nr++; 4092 } 4093 PF_HASHROW_UNLOCK(sh); 4094 } 4095 error = copyout(pstore, psn->psn_src_nodes, 4096 sizeof(struct pf_src_node) * nr); 4097 if (error) { 4098 free(pstore, M_TEMP); 4099 break; 4100 } 4101 psn->psn_len = sizeof(struct pf_src_node) * nr; 4102 free(pstore, M_TEMP); 4103 break; 4104 } 4105 4106 case DIOCCLRSRCNODES: { 4107 pf_clear_srcnodes(NULL); 4108 pf_purge_expired_src_nodes(); 4109 break; 4110 } 4111 4112 case DIOCKILLSRCNODES: 4113 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr); 4114 break; 4115 4116 case DIOCSETHOSTID: { 4117 u_int32_t *hostid = (u_int32_t *)addr; 4118 4119 PF_RULES_WLOCK(); 4120 if (*hostid == 0) 4121 V_pf_status.hostid = arc4random(); 4122 else 4123 V_pf_status.hostid = *hostid; 4124 PF_RULES_WUNLOCK(); 4125 break; 4126 } 4127 4128 case DIOCOSFPFLUSH: 4129 PF_RULES_WLOCK(); 4130 pf_osfp_flush(); 4131 PF_RULES_WUNLOCK(); 4132 break; 4133 4134 case DIOCIGETIFACES: { 4135 struct pfioc_iface *io = (struct pfioc_iface *)addr; 4136 struct pfi_kif *ifstore; 4137 size_t bufsiz; 4138 4139 if (io->pfiio_esize != sizeof(struct pfi_kif)) { 4140 error = ENODEV; 4141 break; 4142 } 4143 4144 if (io->pfiio_size < 0 || 4145 io->pfiio_size > pf_ioctl_maxcount || 4146 WOULD_OVERFLOW(io->pfiio_size, sizeof(struct pfi_kif))) { 4147 error = EINVAL; 4148 break; 4149 } 4150 4151 bufsiz = io->pfiio_size * sizeof(struct pfi_kif); 4152 ifstore = mallocarray(io->pfiio_size, sizeof(struct pfi_kif), 4153 M_TEMP, M_NOWAIT); 4154 if (ifstore == NULL) { 4155 error = ENOMEM; 4156 break; 4157 } 4158 4159 PF_RULES_RLOCK(); 4160 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size); 4161 PF_RULES_RUNLOCK(); 4162 error = copyout(ifstore, io->pfiio_buffer, bufsiz); 4163 free(ifstore, M_TEMP); 4164 break; 4165 } 4166 4167 case DIOCSETIFFLAG: { 4168 struct pfioc_iface *io = (struct pfioc_iface *)addr; 4169 4170 PF_RULES_WLOCK(); 4171 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags); 4172 PF_RULES_WUNLOCK(); 4173 break; 4174 } 4175 4176 case DIOCCLRIFFLAG: { 4177 struct pfioc_iface *io = (struct pfioc_iface *)addr; 4178 4179 PF_RULES_WLOCK(); 4180 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags); 4181 PF_RULES_WUNLOCK(); 4182 break; 4183 } 4184 4185 default: 4186 error = ENODEV; 4187 break; 4188 } 4189 fail: 4190 if (sx_xlocked(&pf_ioctl_lock)) 4191 sx_xunlock(&pf_ioctl_lock); 4192 CURVNET_RESTORE(); 4193 4194 return (error); 4195 } 4196 4197 void 4198 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st) 4199 { 4200 bzero(sp, sizeof(struct pfsync_state)); 4201 4202 /* copy from state key */ 4203 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0]; 4204 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1]; 4205 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0]; 4206 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1]; 4207 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0]; 4208 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1]; 4209 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0]; 4210 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1]; 4211 sp->proto = st->key[PF_SK_WIRE]->proto; 4212 sp->af = st->key[PF_SK_WIRE]->af; 4213 4214 /* copy from state */ 4215 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname)); 4216 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr)); 4217 sp->creation = htonl(time_uptime - st->creation); 4218 sp->expire = pf_state_expires(st); 4219 if (sp->expire <= time_uptime) 4220 sp->expire = htonl(0); 4221 else 4222 sp->expire = htonl(sp->expire - time_uptime); 4223 4224 sp->direction = st->direction; 4225 sp->log = st->log; 4226 sp->timeout = st->timeout; 4227 sp->state_flags = st->state_flags; 4228 if (st->src_node) 4229 sp->sync_flags |= PFSYNC_FLAG_SRCNODE; 4230 if (st->nat_src_node) 4231 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE; 4232 4233 sp->id = st->id; 4234 sp->creatorid = st->creatorid; 4235 pf_state_peer_hton(&st->src, &sp->src); 4236 pf_state_peer_hton(&st->dst, &sp->dst); 4237 4238 if (st->rule.ptr == NULL) 4239 sp->rule = htonl(-1); 4240 else 4241 sp->rule = htonl(st->rule.ptr->nr); 4242 if (st->anchor.ptr == NULL) 4243 sp->anchor = htonl(-1); 4244 else 4245 sp->anchor = htonl(st->anchor.ptr->nr); 4246 if (st->nat_rule.ptr == NULL) 4247 sp->nat_rule = htonl(-1); 4248 else 4249 sp->nat_rule = htonl(st->nat_rule.ptr->nr); 4250 4251 pf_state_counter_hton(counter_u64_fetch(st->packets[0]), 4252 sp->packets[0]); 4253 pf_state_counter_hton(counter_u64_fetch(st->packets[1]), 4254 sp->packets[1]); 4255 pf_state_counter_hton(counter_u64_fetch(st->bytes[0]), sp->bytes[0]); 4256 pf_state_counter_hton(counter_u64_fetch(st->bytes[1]), sp->bytes[1]); 4257 4258 } 4259 4260 static void 4261 pf_tbladdr_copyout(struct pf_addr_wrap *aw) 4262 { 4263 struct pfr_ktable *kt; 4264 4265 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type)); 4266 4267 kt = aw->p.tbl; 4268 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 4269 kt = kt->pfrkt_root; 4270 aw->p.tbl = NULL; 4271 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ? 4272 kt->pfrkt_cnt : -1; 4273 } 4274 4275 /* 4276 * XXX - Check for version missmatch!!! 4277 */ 4278 static void 4279 pf_clear_states(void) 4280 { 4281 struct pf_state *s; 4282 u_int i; 4283 4284 for (i = 0; i <= pf_hashmask; i++) { 4285 struct pf_idhash *ih = &V_pf_idhash[i]; 4286 relock: 4287 PF_HASHROW_LOCK(ih); 4288 LIST_FOREACH(s, &ih->states, entry) { 4289 s->timeout = PFTM_PURGE; 4290 /* Don't send out individual delete messages. */ 4291 s->state_flags |= PFSTATE_NOSYNC; 4292 pf_unlink_state(s, PF_ENTER_LOCKED); 4293 goto relock; 4294 } 4295 PF_HASHROW_UNLOCK(ih); 4296 } 4297 } 4298 4299 static int 4300 pf_clear_tables(void) 4301 { 4302 struct pfioc_table io; 4303 int error; 4304 4305 bzero(&io, sizeof(io)); 4306 4307 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel, 4308 io.pfrio_flags); 4309 4310 return (error); 4311 } 4312 4313 static void 4314 pf_clear_srcnodes(struct pf_ksrc_node *n) 4315 { 4316 struct pf_state *s; 4317 int i; 4318 4319 for (i = 0; i <= pf_hashmask; i++) { 4320 struct pf_idhash *ih = &V_pf_idhash[i]; 4321 4322 PF_HASHROW_LOCK(ih); 4323 LIST_FOREACH(s, &ih->states, entry) { 4324 if (n == NULL || n == s->src_node) 4325 s->src_node = NULL; 4326 if (n == NULL || n == s->nat_src_node) 4327 s->nat_src_node = NULL; 4328 } 4329 PF_HASHROW_UNLOCK(ih); 4330 } 4331 4332 if (n == NULL) { 4333 struct pf_srchash *sh; 4334 4335 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 4336 i++, sh++) { 4337 PF_HASHROW_LOCK(sh); 4338 LIST_FOREACH(n, &sh->nodes, entry) { 4339 n->expire = 1; 4340 n->states = 0; 4341 } 4342 PF_HASHROW_UNLOCK(sh); 4343 } 4344 } else { 4345 /* XXX: hash slot should already be locked here. */ 4346 n->expire = 1; 4347 n->states = 0; 4348 } 4349 } 4350 4351 static void 4352 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk) 4353 { 4354 struct pf_ksrc_node_list kill; 4355 4356 LIST_INIT(&kill); 4357 for (int i = 0; i <= pf_srchashmask; i++) { 4358 struct pf_srchash *sh = &V_pf_srchash[i]; 4359 struct pf_ksrc_node *sn, *tmp; 4360 4361 PF_HASHROW_LOCK(sh); 4362 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp) 4363 if (PF_MATCHA(psnk->psnk_src.neg, 4364 &psnk->psnk_src.addr.v.a.addr, 4365 &psnk->psnk_src.addr.v.a.mask, 4366 &sn->addr, sn->af) && 4367 PF_MATCHA(psnk->psnk_dst.neg, 4368 &psnk->psnk_dst.addr.v.a.addr, 4369 &psnk->psnk_dst.addr.v.a.mask, 4370 &sn->raddr, sn->af)) { 4371 pf_unlink_src_node(sn); 4372 LIST_INSERT_HEAD(&kill, sn, entry); 4373 sn->expire = 1; 4374 } 4375 PF_HASHROW_UNLOCK(sh); 4376 } 4377 4378 for (int i = 0; i <= pf_hashmask; i++) { 4379 struct pf_idhash *ih = &V_pf_idhash[i]; 4380 struct pf_state *s; 4381 4382 PF_HASHROW_LOCK(ih); 4383 LIST_FOREACH(s, &ih->states, entry) { 4384 if (s->src_node && s->src_node->expire == 1) 4385 s->src_node = NULL; 4386 if (s->nat_src_node && s->nat_src_node->expire == 1) 4387 s->nat_src_node = NULL; 4388 } 4389 PF_HASHROW_UNLOCK(ih); 4390 } 4391 4392 psnk->psnk_killed = pf_free_src_nodes(&kill); 4393 } 4394 4395 /* 4396 * XXX - Check for version missmatch!!! 4397 */ 4398 4399 /* 4400 * Duplicate pfctl -Fa operation to get rid of as much as we can. 4401 */ 4402 static int 4403 shutdown_pf(void) 4404 { 4405 int error = 0; 4406 u_int32_t t[5]; 4407 char nn = '\0'; 4408 4409 do { 4410 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn)) 4411 != 0) { 4412 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n")); 4413 break; 4414 } 4415 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn)) 4416 != 0) { 4417 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n")); 4418 break; /* XXX: rollback? */ 4419 } 4420 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn)) 4421 != 0) { 4422 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n")); 4423 break; /* XXX: rollback? */ 4424 } 4425 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn)) 4426 != 0) { 4427 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n")); 4428 break; /* XXX: rollback? */ 4429 } 4430 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn)) 4431 != 0) { 4432 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n")); 4433 break; /* XXX: rollback? */ 4434 } 4435 4436 /* XXX: these should always succeed here */ 4437 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn); 4438 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn); 4439 pf_commit_rules(t[2], PF_RULESET_NAT, &nn); 4440 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn); 4441 pf_commit_rules(t[4], PF_RULESET_RDR, &nn); 4442 4443 if ((error = pf_clear_tables()) != 0) 4444 break; 4445 4446 #ifdef ALTQ 4447 if ((error = pf_begin_altq(&t[0])) != 0) { 4448 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n")); 4449 break; 4450 } 4451 pf_commit_altq(t[0]); 4452 #endif 4453 4454 pf_clear_states(); 4455 4456 pf_clear_srcnodes(NULL); 4457 4458 /* status does not use malloced mem so no need to cleanup */ 4459 /* fingerprints and interfaces have their own cleanup code */ 4460 } while(0); 4461 4462 return (error); 4463 } 4464 4465 static pfil_return_t 4466 pf_check_return(int chk, struct mbuf **m) 4467 { 4468 4469 switch (chk) { 4470 case PF_PASS: 4471 if (*m == NULL) 4472 return (PFIL_CONSUMED); 4473 else 4474 return (PFIL_PASS); 4475 break; 4476 default: 4477 if (*m != NULL) { 4478 m_freem(*m); 4479 *m = NULL; 4480 } 4481 return (PFIL_DROPPED); 4482 } 4483 } 4484 4485 #ifdef INET 4486 static pfil_return_t 4487 pf_check_in(struct mbuf **m, struct ifnet *ifp, int flags, 4488 void *ruleset __unused, struct inpcb *inp) 4489 { 4490 int chk; 4491 4492 chk = pf_test(PF_IN, flags, ifp, m, inp); 4493 4494 return (pf_check_return(chk, m)); 4495 } 4496 4497 static pfil_return_t 4498 pf_check_out(struct mbuf **m, struct ifnet *ifp, int flags, 4499 void *ruleset __unused, struct inpcb *inp) 4500 { 4501 int chk; 4502 4503 chk = pf_test(PF_OUT, flags, ifp, m, inp); 4504 4505 return (pf_check_return(chk, m)); 4506 } 4507 #endif 4508 4509 #ifdef INET6 4510 static pfil_return_t 4511 pf_check6_in(struct mbuf **m, struct ifnet *ifp, int flags, 4512 void *ruleset __unused, struct inpcb *inp) 4513 { 4514 int chk; 4515 4516 /* 4517 * In case of loopback traffic IPv6 uses the real interface in 4518 * order to support scoped addresses. In order to support stateful 4519 * filtering we have change this to lo0 as it is the case in IPv4. 4520 */ 4521 CURVNET_SET(ifp->if_vnet); 4522 chk = pf_test6(PF_IN, flags, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp); 4523 CURVNET_RESTORE(); 4524 4525 return (pf_check_return(chk, m)); 4526 } 4527 4528 static pfil_return_t 4529 pf_check6_out(struct mbuf **m, struct ifnet *ifp, int flags, 4530 void *ruleset __unused, struct inpcb *inp) 4531 { 4532 int chk; 4533 4534 CURVNET_SET(ifp->if_vnet); 4535 chk = pf_test6(PF_OUT, flags, ifp, m, inp); 4536 CURVNET_RESTORE(); 4537 4538 return (pf_check_return(chk, m)); 4539 } 4540 #endif /* INET6 */ 4541 4542 #ifdef INET 4543 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_in_hook); 4544 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_out_hook); 4545 #define V_pf_ip4_in_hook VNET(pf_ip4_in_hook) 4546 #define V_pf_ip4_out_hook VNET(pf_ip4_out_hook) 4547 #endif 4548 #ifdef INET6 4549 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_in_hook); 4550 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_out_hook); 4551 #define V_pf_ip6_in_hook VNET(pf_ip6_in_hook) 4552 #define V_pf_ip6_out_hook VNET(pf_ip6_out_hook) 4553 #endif 4554 4555 static int 4556 hook_pf(void) 4557 { 4558 struct pfil_hook_args pha; 4559 struct pfil_link_args pla; 4560 4561 if (V_pf_pfil_hooked) 4562 return (0); 4563 4564 pha.pa_version = PFIL_VERSION; 4565 pha.pa_modname = "pf"; 4566 pha.pa_ruleset = NULL; 4567 4568 pla.pa_version = PFIL_VERSION; 4569 4570 #ifdef INET 4571 pha.pa_type = PFIL_TYPE_IP4; 4572 pha.pa_func = pf_check_in; 4573 pha.pa_flags = PFIL_IN; 4574 pha.pa_rulname = "default-in"; 4575 V_pf_ip4_in_hook = pfil_add_hook(&pha); 4576 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR; 4577 pla.pa_head = V_inet_pfil_head; 4578 pla.pa_hook = V_pf_ip4_in_hook; 4579 (void)pfil_link(&pla); 4580 pha.pa_func = pf_check_out; 4581 pha.pa_flags = PFIL_OUT; 4582 pha.pa_rulname = "default-out"; 4583 V_pf_ip4_out_hook = pfil_add_hook(&pha); 4584 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR; 4585 pla.pa_head = V_inet_pfil_head; 4586 pla.pa_hook = V_pf_ip4_out_hook; 4587 (void)pfil_link(&pla); 4588 #endif 4589 #ifdef INET6 4590 pha.pa_type = PFIL_TYPE_IP6; 4591 pha.pa_func = pf_check6_in; 4592 pha.pa_flags = PFIL_IN; 4593 pha.pa_rulname = "default-in6"; 4594 V_pf_ip6_in_hook = pfil_add_hook(&pha); 4595 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR; 4596 pla.pa_head = V_inet6_pfil_head; 4597 pla.pa_hook = V_pf_ip6_in_hook; 4598 (void)pfil_link(&pla); 4599 pha.pa_func = pf_check6_out; 4600 pha.pa_rulname = "default-out6"; 4601 pha.pa_flags = PFIL_OUT; 4602 V_pf_ip6_out_hook = pfil_add_hook(&pha); 4603 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR; 4604 pla.pa_head = V_inet6_pfil_head; 4605 pla.pa_hook = V_pf_ip6_out_hook; 4606 (void)pfil_link(&pla); 4607 #endif 4608 4609 V_pf_pfil_hooked = 1; 4610 return (0); 4611 } 4612 4613 static int 4614 dehook_pf(void) 4615 { 4616 4617 if (V_pf_pfil_hooked == 0) 4618 return (0); 4619 4620 #ifdef INET 4621 pfil_remove_hook(V_pf_ip4_in_hook); 4622 pfil_remove_hook(V_pf_ip4_out_hook); 4623 #endif 4624 #ifdef INET6 4625 pfil_remove_hook(V_pf_ip6_in_hook); 4626 pfil_remove_hook(V_pf_ip6_out_hook); 4627 #endif 4628 4629 V_pf_pfil_hooked = 0; 4630 return (0); 4631 } 4632 4633 static void 4634 pf_load_vnet(void) 4635 { 4636 V_pf_tag_z = uma_zcreate("pf tags", sizeof(struct pf_tagname), 4637 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 4638 4639 pf_init_tagset(&V_pf_tags, &pf_rule_tag_hashsize, 4640 PF_RULE_TAG_HASH_SIZE_DEFAULT); 4641 #ifdef ALTQ 4642 pf_init_tagset(&V_pf_qids, &pf_queue_tag_hashsize, 4643 PF_QUEUE_TAG_HASH_SIZE_DEFAULT); 4644 #endif 4645 4646 pfattach_vnet(); 4647 V_pf_vnet_active = 1; 4648 } 4649 4650 static int 4651 pf_load(void) 4652 { 4653 int error; 4654 4655 rm_init(&pf_rules_lock, "pf rulesets"); 4656 sx_init(&pf_ioctl_lock, "pf ioctl"); 4657 sx_init(&pf_end_lock, "pf end thread"); 4658 4659 pf_mtag_initialize(); 4660 4661 pf_dev = make_dev(&pf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, PF_NAME); 4662 if (pf_dev == NULL) 4663 return (ENOMEM); 4664 4665 pf_end_threads = 0; 4666 error = kproc_create(pf_purge_thread, NULL, &pf_purge_proc, 0, 0, "pf purge"); 4667 if (error != 0) 4668 return (error); 4669 4670 pfi_initialize(); 4671 4672 return (0); 4673 } 4674 4675 static void 4676 pf_unload_vnet(void) 4677 { 4678 int error; 4679 4680 V_pf_vnet_active = 0; 4681 V_pf_status.running = 0; 4682 error = dehook_pf(); 4683 if (error) { 4684 /* 4685 * Should not happen! 4686 * XXX Due to error code ESRCH, kldunload will show 4687 * a message like 'No such process'. 4688 */ 4689 printf("%s : pfil unregisteration fail\n", __FUNCTION__); 4690 return; 4691 } 4692 4693 PF_RULES_WLOCK(); 4694 shutdown_pf(); 4695 PF_RULES_WUNLOCK(); 4696 4697 swi_remove(V_pf_swi_cookie); 4698 4699 pf_unload_vnet_purge(); 4700 4701 pf_normalize_cleanup(); 4702 PF_RULES_WLOCK(); 4703 pfi_cleanup_vnet(); 4704 PF_RULES_WUNLOCK(); 4705 pfr_cleanup(); 4706 pf_osfp_flush(); 4707 pf_cleanup(); 4708 if (IS_DEFAULT_VNET(curvnet)) 4709 pf_mtag_cleanup(); 4710 4711 pf_cleanup_tagset(&V_pf_tags); 4712 #ifdef ALTQ 4713 pf_cleanup_tagset(&V_pf_qids); 4714 #endif 4715 uma_zdestroy(V_pf_tag_z); 4716 4717 /* Free counters last as we updated them during shutdown. */ 4718 counter_u64_free(V_pf_default_rule.evaluations); 4719 for (int i = 0; i < 2; i++) { 4720 counter_u64_free(V_pf_default_rule.packets[i]); 4721 counter_u64_free(V_pf_default_rule.bytes[i]); 4722 } 4723 counter_u64_free(V_pf_default_rule.states_cur); 4724 counter_u64_free(V_pf_default_rule.states_tot); 4725 counter_u64_free(V_pf_default_rule.src_nodes); 4726 4727 for (int i = 0; i < PFRES_MAX; i++) 4728 counter_u64_free(V_pf_status.counters[i]); 4729 for (int i = 0; i < LCNT_MAX; i++) 4730 counter_u64_free(V_pf_status.lcounters[i]); 4731 for (int i = 0; i < FCNT_MAX; i++) 4732 counter_u64_free(V_pf_status.fcounters[i]); 4733 for (int i = 0; i < SCNT_MAX; i++) 4734 counter_u64_free(V_pf_status.scounters[i]); 4735 } 4736 4737 static void 4738 pf_unload(void) 4739 { 4740 4741 sx_xlock(&pf_end_lock); 4742 pf_end_threads = 1; 4743 while (pf_end_threads < 2) { 4744 wakeup_one(pf_purge_thread); 4745 sx_sleep(pf_purge_proc, &pf_end_lock, 0, "pftmo", 0); 4746 } 4747 sx_xunlock(&pf_end_lock); 4748 4749 if (pf_dev != NULL) 4750 destroy_dev(pf_dev); 4751 4752 pfi_cleanup(); 4753 4754 rm_destroy(&pf_rules_lock); 4755 sx_destroy(&pf_ioctl_lock); 4756 sx_destroy(&pf_end_lock); 4757 } 4758 4759 static void 4760 vnet_pf_init(void *unused __unused) 4761 { 4762 4763 pf_load_vnet(); 4764 } 4765 VNET_SYSINIT(vnet_pf_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD, 4766 vnet_pf_init, NULL); 4767 4768 static void 4769 vnet_pf_uninit(const void *unused __unused) 4770 { 4771 4772 pf_unload_vnet(); 4773 } 4774 SYSUNINIT(pf_unload, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND, pf_unload, NULL); 4775 VNET_SYSUNINIT(vnet_pf_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD, 4776 vnet_pf_uninit, NULL); 4777 4778 static int 4779 pf_modevent(module_t mod, int type, void *data) 4780 { 4781 int error = 0; 4782 4783 switch(type) { 4784 case MOD_LOAD: 4785 error = pf_load(); 4786 break; 4787 case MOD_UNLOAD: 4788 /* Handled in SYSUNINIT(pf_unload) to ensure it's done after 4789 * the vnet_pf_uninit()s */ 4790 break; 4791 default: 4792 error = EINVAL; 4793 break; 4794 } 4795 4796 return (error); 4797 } 4798 4799 static moduledata_t pf_mod = { 4800 "pf", 4801 pf_modevent, 4802 0 4803 }; 4804 4805 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND); 4806 MODULE_VERSION(pf, PF_MODVER); 4807