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/nv.h> 65 #include <sys/proc.h> 66 #include <sys/sdt.h> 67 #include <sys/smp.h> 68 #include <sys/socket.h> 69 #include <sys/sysctl.h> 70 #include <sys/md5.h> 71 #include <sys/ucred.h> 72 73 #include <net/if.h> 74 #include <net/if_var.h> 75 #include <net/vnet.h> 76 #include <net/route.h> 77 #include <net/pfil.h> 78 #include <net/pfvar.h> 79 #include <net/if_pfsync.h> 80 #include <net/if_pflog.h> 81 82 #include <netinet/in.h> 83 #include <netinet/ip.h> 84 #include <netinet/ip_var.h> 85 #include <netinet6/ip6_var.h> 86 #include <netinet/ip_icmp.h> 87 #include <netpfil/pf/pf_nv.h> 88 89 #ifdef INET6 90 #include <netinet/ip6.h> 91 #endif /* INET6 */ 92 93 #ifdef ALTQ 94 #include <net/altq/altq.h> 95 #endif 96 97 SDT_PROBE_DEFINE3(pf, ioctl, ioctl, error, "int", "int", "int"); 98 SDT_PROBE_DEFINE3(pf, ioctl, function, error, "char *", "int", "int"); 99 SDT_PROBE_DEFINE2(pf, ioctl, addrule, error, "int", "int"); 100 SDT_PROBE_DEFINE2(pf, ioctl, nvchk, error, "int", "int"); 101 102 static struct pf_kpool *pf_get_kpool(const char *, u_int32_t, u_int8_t, 103 u_int32_t, u_int8_t, u_int8_t, u_int8_t); 104 105 static void pf_mv_kpool(struct pf_kpalist *, struct pf_kpalist *); 106 static void pf_empty_kpool(struct pf_kpalist *); 107 static int pfioctl(struct cdev *, u_long, caddr_t, int, 108 struct thread *); 109 static int pf_begin_eth(uint32_t *, const char *); 110 static void pf_rollback_eth_cb(struct epoch_context *); 111 static int pf_rollback_eth(uint32_t, const char *); 112 static int pf_commit_eth(uint32_t, const char *); 113 static void pf_free_eth_rule(struct pf_keth_rule *); 114 #ifdef ALTQ 115 static int pf_begin_altq(u_int32_t *); 116 static int pf_rollback_altq(u_int32_t); 117 static int pf_commit_altq(u_int32_t); 118 static int pf_enable_altq(struct pf_altq *); 119 static int pf_disable_altq(struct pf_altq *); 120 static uint16_t pf_qname2qid(const char *); 121 static void pf_qid_unref(uint16_t); 122 #endif /* ALTQ */ 123 static int pf_begin_rules(u_int32_t *, int, const char *); 124 static int pf_rollback_rules(u_int32_t, int, char *); 125 static int pf_setup_pfsync_matching(struct pf_kruleset *); 126 static void pf_hash_rule_rolling(MD5_CTX *, struct pf_krule *); 127 static void pf_hash_rule(struct pf_krule *); 128 static void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *); 129 static int pf_commit_rules(u_int32_t, int, char *); 130 static int pf_addr_setup(struct pf_kruleset *, 131 struct pf_addr_wrap *, sa_family_t); 132 static void pf_addr_copyout(struct pf_addr_wrap *); 133 static void pf_src_node_copy(const struct pf_ksrc_node *, 134 struct pf_src_node *); 135 #ifdef ALTQ 136 static int pf_export_kaltq(struct pf_altq *, 137 struct pfioc_altq_v1 *, size_t); 138 static int pf_import_kaltq(struct pfioc_altq_v1 *, 139 struct pf_altq *, size_t); 140 #endif /* ALTQ */ 141 142 VNET_DEFINE(struct pf_krule, pf_default_rule); 143 144 static __inline int pf_krule_compare(struct pf_krule *, 145 struct pf_krule *); 146 147 RB_GENERATE(pf_krule_global, pf_krule, entry_global, pf_krule_compare); 148 149 #ifdef ALTQ 150 VNET_DEFINE_STATIC(int, pf_altq_running); 151 #define V_pf_altq_running VNET(pf_altq_running) 152 #endif 153 154 #define TAGID_MAX 50000 155 struct pf_tagname { 156 TAILQ_ENTRY(pf_tagname) namehash_entries; 157 TAILQ_ENTRY(pf_tagname) taghash_entries; 158 char name[PF_TAG_NAME_SIZE]; 159 uint16_t tag; 160 int ref; 161 }; 162 163 struct pf_tagset { 164 TAILQ_HEAD(, pf_tagname) *namehash; 165 TAILQ_HEAD(, pf_tagname) *taghash; 166 unsigned int mask; 167 uint32_t seed; 168 BITSET_DEFINE(, TAGID_MAX) avail; 169 }; 170 171 VNET_DEFINE(struct pf_tagset, pf_tags); 172 #define V_pf_tags VNET(pf_tags) 173 static unsigned int pf_rule_tag_hashsize; 174 #define PF_RULE_TAG_HASH_SIZE_DEFAULT 128 175 SYSCTL_UINT(_net_pf, OID_AUTO, rule_tag_hashsize, CTLFLAG_RDTUN, 176 &pf_rule_tag_hashsize, PF_RULE_TAG_HASH_SIZE_DEFAULT, 177 "Size of pf(4) rule tag hashtable"); 178 179 #ifdef ALTQ 180 VNET_DEFINE(struct pf_tagset, pf_qids); 181 #define V_pf_qids VNET(pf_qids) 182 static unsigned int pf_queue_tag_hashsize; 183 #define PF_QUEUE_TAG_HASH_SIZE_DEFAULT 128 184 SYSCTL_UINT(_net_pf, OID_AUTO, queue_tag_hashsize, CTLFLAG_RDTUN, 185 &pf_queue_tag_hashsize, PF_QUEUE_TAG_HASH_SIZE_DEFAULT, 186 "Size of pf(4) queue tag hashtable"); 187 #endif 188 VNET_DEFINE(uma_zone_t, pf_tag_z); 189 #define V_pf_tag_z VNET(pf_tag_z) 190 static MALLOC_DEFINE(M_PFALTQ, "pf_altq", "pf(4) altq configuration db"); 191 static MALLOC_DEFINE(M_PFRULE, "pf_rule", "pf(4) rules"); 192 193 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE) 194 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE 195 #endif 196 197 static void pf_init_tagset(struct pf_tagset *, unsigned int *, 198 unsigned int); 199 static void pf_cleanup_tagset(struct pf_tagset *); 200 static uint16_t tagname2hashindex(const struct pf_tagset *, const char *); 201 static uint16_t tag2hashindex(const struct pf_tagset *, uint16_t); 202 static u_int16_t tagname2tag(struct pf_tagset *, const char *); 203 static u_int16_t pf_tagname2tag(const char *); 204 static void tag_unref(struct pf_tagset *, u_int16_t); 205 206 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x 207 208 struct cdev *pf_dev; 209 210 /* 211 * XXX - These are new and need to be checked when moveing to a new version 212 */ 213 static void pf_clear_all_states(void); 214 static unsigned int pf_clear_states(const struct pf_kstate_kill *); 215 static void pf_killstates(struct pf_kstate_kill *, 216 unsigned int *); 217 static int pf_killstates_row(struct pf_kstate_kill *, 218 struct pf_idhash *); 219 static int pf_killstates_nv(struct pfioc_nv *); 220 static int pf_clearstates_nv(struct pfioc_nv *); 221 static int pf_getstate(struct pfioc_nv *); 222 static int pf_getstatus(struct pfioc_nv *); 223 static int pf_clear_tables(void); 224 static void pf_clear_srcnodes(struct pf_ksrc_node *); 225 static void pf_kill_srcnodes(struct pfioc_src_node_kill *); 226 static int pf_keepcounters(struct pfioc_nv *); 227 static void pf_tbladdr_copyout(struct pf_addr_wrap *); 228 229 /* 230 * Wrapper functions for pfil(9) hooks 231 */ 232 static pfil_return_t pf_eth_check_in(struct mbuf **m, struct ifnet *ifp, 233 int flags, void *ruleset __unused, struct inpcb *inp); 234 static pfil_return_t pf_eth_check_out(struct mbuf **m, struct ifnet *ifp, 235 int flags, void *ruleset __unused, struct inpcb *inp); 236 #ifdef INET 237 static pfil_return_t pf_check_in(struct mbuf **m, struct ifnet *ifp, 238 int flags, void *ruleset __unused, struct inpcb *inp); 239 static pfil_return_t pf_check_out(struct mbuf **m, struct ifnet *ifp, 240 int flags, void *ruleset __unused, struct inpcb *inp); 241 #endif 242 #ifdef INET6 243 static pfil_return_t pf_check6_in(struct mbuf **m, struct ifnet *ifp, 244 int flags, void *ruleset __unused, struct inpcb *inp); 245 static pfil_return_t pf_check6_out(struct mbuf **m, struct ifnet *ifp, 246 int flags, void *ruleset __unused, struct inpcb *inp); 247 #endif 248 249 static void hook_pf_eth(void); 250 static void hook_pf(void); 251 static void dehook_pf_eth(void); 252 static void dehook_pf(void); 253 static int shutdown_pf(void); 254 static int pf_load(void); 255 static void pf_unload(void); 256 257 static struct cdevsw pf_cdevsw = { 258 .d_ioctl = pfioctl, 259 .d_name = PF_NAME, 260 .d_version = D_VERSION, 261 }; 262 263 volatile VNET_DEFINE_STATIC(int, pf_pfil_hooked); 264 #define V_pf_pfil_hooked VNET(pf_pfil_hooked) 265 volatile VNET_DEFINE_STATIC(int, pf_pfil_eth_hooked); 266 #define V_pf_pfil_eth_hooked VNET(pf_pfil_eth_hooked) 267 268 /* 269 * We need a flag that is neither hooked nor running to know when 270 * the VNET is "valid". We primarily need this to control (global) 271 * external event, e.g., eventhandlers. 272 */ 273 VNET_DEFINE(int, pf_vnet_active); 274 #define V_pf_vnet_active VNET(pf_vnet_active) 275 276 int pf_end_threads; 277 struct proc *pf_purge_proc; 278 279 struct rmlock pf_rules_lock; 280 struct sx pf_ioctl_lock; 281 struct sx pf_end_lock; 282 283 /* pfsync */ 284 VNET_DEFINE(pfsync_state_import_t *, pfsync_state_import_ptr); 285 VNET_DEFINE(pfsync_insert_state_t *, pfsync_insert_state_ptr); 286 VNET_DEFINE(pfsync_update_state_t *, pfsync_update_state_ptr); 287 VNET_DEFINE(pfsync_delete_state_t *, pfsync_delete_state_ptr); 288 VNET_DEFINE(pfsync_clear_states_t *, pfsync_clear_states_ptr); 289 VNET_DEFINE(pfsync_defer_t *, pfsync_defer_ptr); 290 pfsync_detach_ifnet_t *pfsync_detach_ifnet_ptr; 291 292 /* pflog */ 293 pflog_packet_t *pflog_packet_ptr = NULL; 294 295 /* 296 * Copy a user-provided string, returning an error if truncation would occur. 297 * Avoid scanning past "sz" bytes in the source string since there's no 298 * guarantee that it's nul-terminated. 299 */ 300 static int 301 pf_user_strcpy(char *dst, const char *src, size_t sz) 302 { 303 if (strnlen(src, sz) == sz) 304 return (EINVAL); 305 (void)strlcpy(dst, src, sz); 306 return (0); 307 } 308 309 static void 310 pfattach_vnet(void) 311 { 312 u_int32_t *my_timeout = V_pf_default_rule.timeout; 313 314 pf_initialize(); 315 pfr_initialize(); 316 pfi_initialize_vnet(); 317 pf_normalize_init(); 318 pf_syncookies_init(); 319 320 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT; 321 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT; 322 323 RB_INIT(&V_pf_anchors); 324 pf_init_kruleset(&pf_main_ruleset); 325 326 pf_init_keth(V_pf_keth); 327 328 /* default rule should never be garbage collected */ 329 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next; 330 #ifdef PF_DEFAULT_TO_DROP 331 V_pf_default_rule.action = PF_DROP; 332 #else 333 V_pf_default_rule.action = PF_PASS; 334 #endif 335 V_pf_default_rule.nr = -1; 336 V_pf_default_rule.rtableid = -1; 337 338 pf_counter_u64_init(&V_pf_default_rule.evaluations, M_WAITOK); 339 for (int i = 0; i < 2; i++) { 340 pf_counter_u64_init(&V_pf_default_rule.packets[i], M_WAITOK); 341 pf_counter_u64_init(&V_pf_default_rule.bytes[i], M_WAITOK); 342 } 343 V_pf_default_rule.states_cur = counter_u64_alloc(M_WAITOK); 344 V_pf_default_rule.states_tot = counter_u64_alloc(M_WAITOK); 345 V_pf_default_rule.src_nodes = counter_u64_alloc(M_WAITOK); 346 347 #ifdef PF_WANT_32_TO_64_COUNTER 348 V_pf_kifmarker = malloc(sizeof(*V_pf_kifmarker), PFI_MTYPE, M_WAITOK | M_ZERO); 349 V_pf_rulemarker = malloc(sizeof(*V_pf_rulemarker), M_PFRULE, M_WAITOK | M_ZERO); 350 PF_RULES_WLOCK(); 351 LIST_INSERT_HEAD(&V_pf_allkiflist, V_pf_kifmarker, pfik_allkiflist); 352 LIST_INSERT_HEAD(&V_pf_allrulelist, &V_pf_default_rule, allrulelist); 353 V_pf_allrulecount++; 354 LIST_INSERT_HEAD(&V_pf_allrulelist, V_pf_rulemarker, allrulelist); 355 PF_RULES_WUNLOCK(); 356 #endif 357 358 /* initialize default timeouts */ 359 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL; 360 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL; 361 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL; 362 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL; 363 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL; 364 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL; 365 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL; 366 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL; 367 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL; 368 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL; 369 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL; 370 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL; 371 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL; 372 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL; 373 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL; 374 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL; 375 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL; 376 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL; 377 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START; 378 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END; 379 380 bzero(&V_pf_status, sizeof(V_pf_status)); 381 V_pf_status.debug = PF_DEBUG_URGENT; 382 383 V_pf_pfil_hooked = 0; 384 V_pf_pfil_eth_hooked = 0; 385 386 /* XXX do our best to avoid a conflict */ 387 V_pf_status.hostid = arc4random(); 388 389 for (int i = 0; i < PFRES_MAX; i++) 390 V_pf_status.counters[i] = counter_u64_alloc(M_WAITOK); 391 for (int i = 0; i < KLCNT_MAX; i++) 392 V_pf_status.lcounters[i] = counter_u64_alloc(M_WAITOK); 393 for (int i = 0; i < FCNT_MAX; i++) 394 pf_counter_u64_init(&V_pf_status.fcounters[i], M_WAITOK); 395 for (int i = 0; i < SCNT_MAX; i++) 396 V_pf_status.scounters[i] = counter_u64_alloc(M_WAITOK); 397 398 if (swi_add(&V_pf_swi_ie, "pf send", pf_intr, curvnet, SWI_NET, 399 INTR_MPSAFE, &V_pf_swi_cookie) != 0) 400 /* XXXGL: leaked all above. */ 401 return; 402 } 403 404 static struct pf_kpool * 405 pf_get_kpool(const char *anchor, u_int32_t ticket, u_int8_t rule_action, 406 u_int32_t rule_number, u_int8_t r_last, u_int8_t active, 407 u_int8_t check_ticket) 408 { 409 struct pf_kruleset *ruleset; 410 struct pf_krule *rule; 411 int rs_num; 412 413 ruleset = pf_find_kruleset(anchor); 414 if (ruleset == NULL) 415 return (NULL); 416 rs_num = pf_get_ruleset_number(rule_action); 417 if (rs_num >= PF_RULESET_MAX) 418 return (NULL); 419 if (active) { 420 if (check_ticket && ticket != 421 ruleset->rules[rs_num].active.ticket) 422 return (NULL); 423 if (r_last) 424 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr, 425 pf_krulequeue); 426 else 427 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr); 428 } else { 429 if (check_ticket && ticket != 430 ruleset->rules[rs_num].inactive.ticket) 431 return (NULL); 432 if (r_last) 433 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr, 434 pf_krulequeue); 435 else 436 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr); 437 } 438 if (!r_last) { 439 while ((rule != NULL) && (rule->nr != rule_number)) 440 rule = TAILQ_NEXT(rule, entries); 441 } 442 if (rule == NULL) 443 return (NULL); 444 445 return (&rule->rpool); 446 } 447 448 static void 449 pf_mv_kpool(struct pf_kpalist *poola, struct pf_kpalist *poolb) 450 { 451 struct pf_kpooladdr *mv_pool_pa; 452 453 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) { 454 TAILQ_REMOVE(poola, mv_pool_pa, entries); 455 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries); 456 } 457 } 458 459 static void 460 pf_empty_kpool(struct pf_kpalist *poola) 461 { 462 struct pf_kpooladdr *pa; 463 464 while ((pa = TAILQ_FIRST(poola)) != NULL) { 465 switch (pa->addr.type) { 466 case PF_ADDR_DYNIFTL: 467 pfi_dynaddr_remove(pa->addr.p.dyn); 468 break; 469 case PF_ADDR_TABLE: 470 /* XXX: this could be unfinished pooladdr on pabuf */ 471 if (pa->addr.p.tbl != NULL) 472 pfr_detach_table(pa->addr.p.tbl); 473 break; 474 } 475 if (pa->kif) 476 pfi_kkif_unref(pa->kif); 477 TAILQ_REMOVE(poola, pa, entries); 478 free(pa, M_PFRULE); 479 } 480 } 481 482 static void 483 pf_unlink_rule_locked(struct pf_krulequeue *rulequeue, struct pf_krule *rule) 484 { 485 486 PF_RULES_WASSERT(); 487 PF_UNLNKDRULES_ASSERT(); 488 489 TAILQ_REMOVE(rulequeue, rule, entries); 490 491 rule->rule_ref |= PFRULE_REFS; 492 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries); 493 } 494 495 static void 496 pf_unlink_rule(struct pf_krulequeue *rulequeue, struct pf_krule *rule) 497 { 498 499 PF_RULES_WASSERT(); 500 501 PF_UNLNKDRULES_LOCK(); 502 pf_unlink_rule_locked(rulequeue, rule); 503 PF_UNLNKDRULES_UNLOCK(); 504 } 505 506 static void 507 pf_free_eth_rule(struct pf_keth_rule *rule) 508 { 509 PF_RULES_WASSERT(); 510 511 if (rule == NULL) 512 return; 513 514 if (rule->tag) 515 tag_unref(&V_pf_tags, rule->tag); 516 #ifdef ALTQ 517 pf_qid_unref(rule->qid); 518 #endif 519 520 if (rule->kif) 521 pfi_kkif_unref(rule->kif); 522 523 counter_u64_free(rule->evaluations); 524 for (int i = 0; i < 2; i++) { 525 counter_u64_free(rule->packets[i]); 526 counter_u64_free(rule->bytes[i]); 527 } 528 pf_keth_anchor_remove(rule); 529 530 free(rule, M_PFRULE); 531 } 532 533 void 534 pf_free_rule(struct pf_krule *rule) 535 { 536 537 PF_RULES_WASSERT(); 538 PF_CONFIG_ASSERT(); 539 540 if (rule->tag) 541 tag_unref(&V_pf_tags, rule->tag); 542 if (rule->match_tag) 543 tag_unref(&V_pf_tags, rule->match_tag); 544 #ifdef ALTQ 545 if (rule->pqid != rule->qid) 546 pf_qid_unref(rule->pqid); 547 pf_qid_unref(rule->qid); 548 #endif 549 switch (rule->src.addr.type) { 550 case PF_ADDR_DYNIFTL: 551 pfi_dynaddr_remove(rule->src.addr.p.dyn); 552 break; 553 case PF_ADDR_TABLE: 554 pfr_detach_table(rule->src.addr.p.tbl); 555 break; 556 } 557 switch (rule->dst.addr.type) { 558 case PF_ADDR_DYNIFTL: 559 pfi_dynaddr_remove(rule->dst.addr.p.dyn); 560 break; 561 case PF_ADDR_TABLE: 562 pfr_detach_table(rule->dst.addr.p.tbl); 563 break; 564 } 565 if (rule->overload_tbl) 566 pfr_detach_table(rule->overload_tbl); 567 if (rule->kif) 568 pfi_kkif_unref(rule->kif); 569 pf_kanchor_remove(rule); 570 pf_empty_kpool(&rule->rpool.list); 571 572 pf_krule_free(rule); 573 } 574 575 static void 576 pf_init_tagset(struct pf_tagset *ts, unsigned int *tunable_size, 577 unsigned int default_size) 578 { 579 unsigned int i; 580 unsigned int hashsize; 581 582 if (*tunable_size == 0 || !powerof2(*tunable_size)) 583 *tunable_size = default_size; 584 585 hashsize = *tunable_size; 586 ts->namehash = mallocarray(hashsize, sizeof(*ts->namehash), M_PFHASH, 587 M_WAITOK); 588 ts->taghash = mallocarray(hashsize, sizeof(*ts->taghash), M_PFHASH, 589 M_WAITOK); 590 ts->mask = hashsize - 1; 591 ts->seed = arc4random(); 592 for (i = 0; i < hashsize; i++) { 593 TAILQ_INIT(&ts->namehash[i]); 594 TAILQ_INIT(&ts->taghash[i]); 595 } 596 BIT_FILL(TAGID_MAX, &ts->avail); 597 } 598 599 static void 600 pf_cleanup_tagset(struct pf_tagset *ts) 601 { 602 unsigned int i; 603 unsigned int hashsize; 604 struct pf_tagname *t, *tmp; 605 606 /* 607 * Only need to clean up one of the hashes as each tag is hashed 608 * into each table. 609 */ 610 hashsize = ts->mask + 1; 611 for (i = 0; i < hashsize; i++) 612 TAILQ_FOREACH_SAFE(t, &ts->namehash[i], namehash_entries, tmp) 613 uma_zfree(V_pf_tag_z, t); 614 615 free(ts->namehash, M_PFHASH); 616 free(ts->taghash, M_PFHASH); 617 } 618 619 static uint16_t 620 tagname2hashindex(const struct pf_tagset *ts, const char *tagname) 621 { 622 size_t len; 623 624 len = strnlen(tagname, PF_TAG_NAME_SIZE - 1); 625 return (murmur3_32_hash(tagname, len, ts->seed) & ts->mask); 626 } 627 628 static uint16_t 629 tag2hashindex(const struct pf_tagset *ts, uint16_t tag) 630 { 631 632 return (tag & ts->mask); 633 } 634 635 static u_int16_t 636 tagname2tag(struct pf_tagset *ts, const char *tagname) 637 { 638 struct pf_tagname *tag; 639 u_int32_t index; 640 u_int16_t new_tagid; 641 642 PF_RULES_WASSERT(); 643 644 index = tagname2hashindex(ts, tagname); 645 TAILQ_FOREACH(tag, &ts->namehash[index], namehash_entries) 646 if (strcmp(tagname, tag->name) == 0) { 647 tag->ref++; 648 return (tag->tag); 649 } 650 651 /* 652 * new entry 653 * 654 * to avoid fragmentation, we do a linear search from the beginning 655 * and take the first free slot we find. 656 */ 657 new_tagid = BIT_FFS(TAGID_MAX, &ts->avail); 658 /* 659 * Tags are 1-based, with valid tags in the range [1..TAGID_MAX]. 660 * BIT_FFS() returns a 1-based bit number, with 0 indicating no bits 661 * set. It may also return a bit number greater than TAGID_MAX due 662 * to rounding of the number of bits in the vector up to a multiple 663 * of the vector word size at declaration/allocation time. 664 */ 665 if ((new_tagid == 0) || (new_tagid > TAGID_MAX)) 666 return (0); 667 668 /* Mark the tag as in use. Bits are 0-based for BIT_CLR() */ 669 BIT_CLR(TAGID_MAX, new_tagid - 1, &ts->avail); 670 671 /* allocate and fill new struct pf_tagname */ 672 tag = uma_zalloc(V_pf_tag_z, M_NOWAIT); 673 if (tag == NULL) 674 return (0); 675 strlcpy(tag->name, tagname, sizeof(tag->name)); 676 tag->tag = new_tagid; 677 tag->ref = 1; 678 679 /* Insert into namehash */ 680 TAILQ_INSERT_TAIL(&ts->namehash[index], tag, namehash_entries); 681 682 /* Insert into taghash */ 683 index = tag2hashindex(ts, new_tagid); 684 TAILQ_INSERT_TAIL(&ts->taghash[index], tag, taghash_entries); 685 686 return (tag->tag); 687 } 688 689 static void 690 tag_unref(struct pf_tagset *ts, u_int16_t tag) 691 { 692 struct pf_tagname *t; 693 uint16_t index; 694 695 PF_RULES_WASSERT(); 696 697 index = tag2hashindex(ts, tag); 698 TAILQ_FOREACH(t, &ts->taghash[index], taghash_entries) 699 if (tag == t->tag) { 700 if (--t->ref == 0) { 701 TAILQ_REMOVE(&ts->taghash[index], t, 702 taghash_entries); 703 index = tagname2hashindex(ts, t->name); 704 TAILQ_REMOVE(&ts->namehash[index], t, 705 namehash_entries); 706 /* Bits are 0-based for BIT_SET() */ 707 BIT_SET(TAGID_MAX, tag - 1, &ts->avail); 708 uma_zfree(V_pf_tag_z, t); 709 } 710 break; 711 } 712 } 713 714 static uint16_t 715 pf_tagname2tag(const char *tagname) 716 { 717 return (tagname2tag(&V_pf_tags, tagname)); 718 } 719 720 static int 721 pf_begin_eth(uint32_t *ticket, const char *anchor) 722 { 723 struct pf_keth_rule *rule, *tmp; 724 struct pf_keth_ruleset *rs; 725 726 PF_RULES_WASSERT(); 727 728 rs = pf_find_or_create_keth_ruleset(anchor); 729 if (rs == NULL) 730 return (EINVAL); 731 732 if (rs->inactive.open) 733 /* We may be waiting for NET_EPOCH_CALL(pf_rollback_eth_cb) to 734 * finish. */ 735 return (EBUSY); 736 737 /* Purge old inactive rules. */ 738 TAILQ_FOREACH_SAFE(rule, rs->inactive.rules, entries, 739 tmp) { 740 TAILQ_REMOVE(rs->inactive.rules, rule, 741 entries); 742 pf_free_eth_rule(rule); 743 } 744 745 *ticket = ++rs->inactive.ticket; 746 rs->inactive.open = 1; 747 748 return (0); 749 } 750 751 static void 752 pf_rollback_eth_cb(struct epoch_context *ctx) 753 { 754 struct pf_keth_ruleset *rs; 755 756 rs = __containerof(ctx, struct pf_keth_ruleset, epoch_ctx); 757 758 CURVNET_SET(rs->vnet); 759 760 PF_RULES_WLOCK(); 761 pf_rollback_eth(rs->inactive.ticket, 762 rs->anchor ? rs->anchor->path : ""); 763 PF_RULES_WUNLOCK(); 764 765 CURVNET_RESTORE(); 766 } 767 768 static int 769 pf_rollback_eth(uint32_t ticket, const char *anchor) 770 { 771 struct pf_keth_rule *rule, *tmp; 772 struct pf_keth_ruleset *rs; 773 774 PF_RULES_WASSERT(); 775 776 rs = pf_find_keth_ruleset(anchor); 777 if (rs == NULL) 778 return (EINVAL); 779 780 if (!rs->inactive.open || 781 ticket != rs->inactive.ticket) 782 return (0); 783 784 /* Purge old inactive rules. */ 785 TAILQ_FOREACH_SAFE(rule, rs->inactive.rules, entries, 786 tmp) { 787 TAILQ_REMOVE(rs->inactive.rules, rule, entries); 788 pf_free_eth_rule(rule); 789 } 790 791 rs->inactive.open = 0; 792 793 pf_remove_if_empty_keth_ruleset(rs); 794 795 return (0); 796 } 797 798 #define PF_SET_SKIP_STEPS(i) \ 799 do { \ 800 while (head[i] != cur) { \ 801 head[i]->skip[i].ptr = cur; \ 802 head[i] = TAILQ_NEXT(head[i], entries); \ 803 } \ 804 } while (0) 805 806 static void 807 pf_eth_calc_skip_steps(struct pf_keth_ruleq *rules) 808 { 809 struct pf_keth_rule *cur, *prev, *head[PFE_SKIP_COUNT]; 810 int i; 811 812 cur = TAILQ_FIRST(rules); 813 prev = cur; 814 for (i = 0; i < PFE_SKIP_COUNT; ++i) 815 head[i] = cur; 816 while (cur != NULL) { 817 if (cur->kif != prev->kif || cur->ifnot != prev->ifnot) 818 PF_SET_SKIP_STEPS(PFE_SKIP_IFP); 819 if (cur->direction != prev->direction) 820 PF_SET_SKIP_STEPS(PFE_SKIP_DIR); 821 if (cur->proto != prev->proto) 822 PF_SET_SKIP_STEPS(PFE_SKIP_PROTO); 823 if (memcmp(&cur->src, &prev->src, sizeof(cur->src)) != 0) 824 PF_SET_SKIP_STEPS(PFE_SKIP_SRC_ADDR); 825 if (memcmp(&cur->dst, &prev->dst, sizeof(cur->dst)) != 0) 826 PF_SET_SKIP_STEPS(PFE_SKIP_DST_ADDR); 827 828 prev = cur; 829 cur = TAILQ_NEXT(cur, entries); 830 } 831 for (i = 0; i < PFE_SKIP_COUNT; ++i) 832 PF_SET_SKIP_STEPS(i); 833 } 834 835 static int 836 pf_commit_eth(uint32_t ticket, const char *anchor) 837 { 838 struct pf_keth_ruleq *rules; 839 struct pf_keth_ruleset *rs; 840 841 rs = pf_find_keth_ruleset(anchor); 842 if (rs == NULL) { 843 return (EINVAL); 844 } 845 846 if (!rs->inactive.open || 847 ticket != rs->inactive.ticket) 848 return (EBUSY); 849 850 PF_RULES_WASSERT(); 851 852 pf_eth_calc_skip_steps(rs->inactive.rules); 853 854 rules = rs->active.rules; 855 ck_pr_store_ptr(&rs->active.rules, rs->inactive.rules); 856 rs->inactive.rules = rules; 857 rs->inactive.ticket = rs->active.ticket; 858 859 /* Clean up inactive rules (i.e. previously active rules), only when 860 * we're sure they're no longer used. */ 861 NET_EPOCH_CALL(pf_rollback_eth_cb, &rs->epoch_ctx); 862 863 return (0); 864 } 865 866 #ifdef ALTQ 867 static uint16_t 868 pf_qname2qid(const char *qname) 869 { 870 return (tagname2tag(&V_pf_qids, qname)); 871 } 872 873 static void 874 pf_qid_unref(uint16_t qid) 875 { 876 tag_unref(&V_pf_qids, qid); 877 } 878 879 static int 880 pf_begin_altq(u_int32_t *ticket) 881 { 882 struct pf_altq *altq, *tmp; 883 int error = 0; 884 885 PF_RULES_WASSERT(); 886 887 /* Purge the old altq lists */ 888 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) { 889 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 890 /* detach and destroy the discipline */ 891 error = altq_remove(altq); 892 } 893 free(altq, M_PFALTQ); 894 } 895 TAILQ_INIT(V_pf_altq_ifs_inactive); 896 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) { 897 pf_qid_unref(altq->qid); 898 free(altq, M_PFALTQ); 899 } 900 TAILQ_INIT(V_pf_altqs_inactive); 901 if (error) 902 return (error); 903 *ticket = ++V_ticket_altqs_inactive; 904 V_altqs_inactive_open = 1; 905 return (0); 906 } 907 908 static int 909 pf_rollback_altq(u_int32_t ticket) 910 { 911 struct pf_altq *altq, *tmp; 912 int error = 0; 913 914 PF_RULES_WASSERT(); 915 916 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive) 917 return (0); 918 /* Purge the old altq lists */ 919 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) { 920 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 921 /* detach and destroy the discipline */ 922 error = altq_remove(altq); 923 } 924 free(altq, M_PFALTQ); 925 } 926 TAILQ_INIT(V_pf_altq_ifs_inactive); 927 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) { 928 pf_qid_unref(altq->qid); 929 free(altq, M_PFALTQ); 930 } 931 TAILQ_INIT(V_pf_altqs_inactive); 932 V_altqs_inactive_open = 0; 933 return (error); 934 } 935 936 static int 937 pf_commit_altq(u_int32_t ticket) 938 { 939 struct pf_altqqueue *old_altqs, *old_altq_ifs; 940 struct pf_altq *altq, *tmp; 941 int err, error = 0; 942 943 PF_RULES_WASSERT(); 944 945 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive) 946 return (EBUSY); 947 948 /* swap altqs, keep the old. */ 949 old_altqs = V_pf_altqs_active; 950 old_altq_ifs = V_pf_altq_ifs_active; 951 V_pf_altqs_active = V_pf_altqs_inactive; 952 V_pf_altq_ifs_active = V_pf_altq_ifs_inactive; 953 V_pf_altqs_inactive = old_altqs; 954 V_pf_altq_ifs_inactive = old_altq_ifs; 955 V_ticket_altqs_active = V_ticket_altqs_inactive; 956 957 /* Attach new disciplines */ 958 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 959 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 960 /* attach the discipline */ 961 error = altq_pfattach(altq); 962 if (error == 0 && V_pf_altq_running) 963 error = pf_enable_altq(altq); 964 if (error != 0) 965 return (error); 966 } 967 } 968 969 /* Purge the old altq lists */ 970 TAILQ_FOREACH_SAFE(altq, V_pf_altq_ifs_inactive, entries, tmp) { 971 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 972 /* detach and destroy the discipline */ 973 if (V_pf_altq_running) 974 error = pf_disable_altq(altq); 975 err = altq_pfdetach(altq); 976 if (err != 0 && error == 0) 977 error = err; 978 err = altq_remove(altq); 979 if (err != 0 && error == 0) 980 error = err; 981 } 982 free(altq, M_PFALTQ); 983 } 984 TAILQ_INIT(V_pf_altq_ifs_inactive); 985 TAILQ_FOREACH_SAFE(altq, V_pf_altqs_inactive, entries, tmp) { 986 pf_qid_unref(altq->qid); 987 free(altq, M_PFALTQ); 988 } 989 TAILQ_INIT(V_pf_altqs_inactive); 990 991 V_altqs_inactive_open = 0; 992 return (error); 993 } 994 995 static int 996 pf_enable_altq(struct pf_altq *altq) 997 { 998 struct ifnet *ifp; 999 struct tb_profile tb; 1000 int error = 0; 1001 1002 if ((ifp = ifunit(altq->ifname)) == NULL) 1003 return (EINVAL); 1004 1005 if (ifp->if_snd.altq_type != ALTQT_NONE) 1006 error = altq_enable(&ifp->if_snd); 1007 1008 /* set tokenbucket regulator */ 1009 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) { 1010 tb.rate = altq->ifbandwidth; 1011 tb.depth = altq->tbrsize; 1012 error = tbr_set(&ifp->if_snd, &tb); 1013 } 1014 1015 return (error); 1016 } 1017 1018 static int 1019 pf_disable_altq(struct pf_altq *altq) 1020 { 1021 struct ifnet *ifp; 1022 struct tb_profile tb; 1023 int error; 1024 1025 if ((ifp = ifunit(altq->ifname)) == NULL) 1026 return (EINVAL); 1027 1028 /* 1029 * when the discipline is no longer referenced, it was overridden 1030 * by a new one. if so, just return. 1031 */ 1032 if (altq->altq_disc != ifp->if_snd.altq_disc) 1033 return (0); 1034 1035 error = altq_disable(&ifp->if_snd); 1036 1037 if (error == 0) { 1038 /* clear tokenbucket regulator */ 1039 tb.rate = 0; 1040 error = tbr_set(&ifp->if_snd, &tb); 1041 } 1042 1043 return (error); 1044 } 1045 1046 static int 1047 pf_altq_ifnet_event_add(struct ifnet *ifp, int remove, u_int32_t ticket, 1048 struct pf_altq *altq) 1049 { 1050 struct ifnet *ifp1; 1051 int error = 0; 1052 1053 /* Deactivate the interface in question */ 1054 altq->local_flags &= ~PFALTQ_FLAG_IF_REMOVED; 1055 if ((ifp1 = ifunit(altq->ifname)) == NULL || 1056 (remove && ifp1 == ifp)) { 1057 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED; 1058 } else { 1059 error = altq_add(ifp1, altq); 1060 1061 if (ticket != V_ticket_altqs_inactive) 1062 error = EBUSY; 1063 1064 if (error) 1065 free(altq, M_PFALTQ); 1066 } 1067 1068 return (error); 1069 } 1070 1071 void 1072 pf_altq_ifnet_event(struct ifnet *ifp, int remove) 1073 { 1074 struct pf_altq *a1, *a2, *a3; 1075 u_int32_t ticket; 1076 int error = 0; 1077 1078 /* 1079 * No need to re-evaluate the configuration for events on interfaces 1080 * that do not support ALTQ, as it's not possible for such 1081 * interfaces to be part of the configuration. 1082 */ 1083 if (!ALTQ_IS_READY(&ifp->if_snd)) 1084 return; 1085 1086 /* Interrupt userland queue modifications */ 1087 if (V_altqs_inactive_open) 1088 pf_rollback_altq(V_ticket_altqs_inactive); 1089 1090 /* Start new altq ruleset */ 1091 if (pf_begin_altq(&ticket)) 1092 return; 1093 1094 /* Copy the current active set */ 1095 TAILQ_FOREACH(a1, V_pf_altq_ifs_active, entries) { 1096 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT); 1097 if (a2 == NULL) { 1098 error = ENOMEM; 1099 break; 1100 } 1101 bcopy(a1, a2, sizeof(struct pf_altq)); 1102 1103 error = pf_altq_ifnet_event_add(ifp, remove, ticket, a2); 1104 if (error) 1105 break; 1106 1107 TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, a2, entries); 1108 } 1109 if (error) 1110 goto out; 1111 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) { 1112 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT); 1113 if (a2 == NULL) { 1114 error = ENOMEM; 1115 break; 1116 } 1117 bcopy(a1, a2, sizeof(struct pf_altq)); 1118 1119 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) { 1120 error = EBUSY; 1121 free(a2, M_PFALTQ); 1122 break; 1123 } 1124 a2->altq_disc = NULL; 1125 TAILQ_FOREACH(a3, V_pf_altq_ifs_inactive, entries) { 1126 if (strncmp(a3->ifname, a2->ifname, 1127 IFNAMSIZ) == 0) { 1128 a2->altq_disc = a3->altq_disc; 1129 break; 1130 } 1131 } 1132 error = pf_altq_ifnet_event_add(ifp, remove, ticket, a2); 1133 if (error) 1134 break; 1135 1136 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries); 1137 } 1138 1139 out: 1140 if (error != 0) 1141 pf_rollback_altq(ticket); 1142 else 1143 pf_commit_altq(ticket); 1144 } 1145 #endif /* ALTQ */ 1146 1147 static int 1148 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor) 1149 { 1150 struct pf_krule_global *tree; 1151 struct pf_kruleset *rs; 1152 struct pf_krule *rule; 1153 1154 PF_RULES_WASSERT(); 1155 1156 if (rs_num < 0 || rs_num >= PF_RULESET_MAX) 1157 return (EINVAL); 1158 tree = malloc(sizeof(struct pf_krule_global), M_TEMP, M_NOWAIT); 1159 if (tree == NULL) 1160 return (ENOMEM); 1161 RB_INIT(tree); 1162 rs = pf_find_or_create_kruleset(anchor); 1163 if (rs == NULL) { 1164 free(tree, M_TEMP); 1165 return (EINVAL); 1166 } 1167 free(rs->rules[rs_num].inactive.tree, M_TEMP); 1168 rs->rules[rs_num].inactive.tree = tree; 1169 1170 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) { 1171 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule); 1172 rs->rules[rs_num].inactive.rcount--; 1173 } 1174 *ticket = ++rs->rules[rs_num].inactive.ticket; 1175 rs->rules[rs_num].inactive.open = 1; 1176 return (0); 1177 } 1178 1179 static int 1180 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor) 1181 { 1182 struct pf_kruleset *rs; 1183 struct pf_krule *rule; 1184 1185 PF_RULES_WASSERT(); 1186 1187 if (rs_num < 0 || rs_num >= PF_RULESET_MAX) 1188 return (EINVAL); 1189 rs = pf_find_kruleset(anchor); 1190 if (rs == NULL || !rs->rules[rs_num].inactive.open || 1191 rs->rules[rs_num].inactive.ticket != ticket) 1192 return (0); 1193 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) { 1194 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule); 1195 rs->rules[rs_num].inactive.rcount--; 1196 } 1197 rs->rules[rs_num].inactive.open = 0; 1198 return (0); 1199 } 1200 1201 #define PF_MD5_UPD(st, elm) \ 1202 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm)) 1203 1204 #define PF_MD5_UPD_STR(st, elm) \ 1205 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm)) 1206 1207 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \ 1208 (stor) = htonl((st)->elm); \ 1209 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\ 1210 } while (0) 1211 1212 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \ 1213 (stor) = htons((st)->elm); \ 1214 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\ 1215 } while (0) 1216 1217 static void 1218 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr) 1219 { 1220 PF_MD5_UPD(pfr, addr.type); 1221 switch (pfr->addr.type) { 1222 case PF_ADDR_DYNIFTL: 1223 PF_MD5_UPD(pfr, addr.v.ifname); 1224 PF_MD5_UPD(pfr, addr.iflags); 1225 break; 1226 case PF_ADDR_TABLE: 1227 PF_MD5_UPD(pfr, addr.v.tblname); 1228 break; 1229 case PF_ADDR_ADDRMASK: 1230 /* XXX ignore af? */ 1231 PF_MD5_UPD(pfr, addr.v.a.addr.addr32); 1232 PF_MD5_UPD(pfr, addr.v.a.mask.addr32); 1233 break; 1234 } 1235 1236 PF_MD5_UPD(pfr, port[0]); 1237 PF_MD5_UPD(pfr, port[1]); 1238 PF_MD5_UPD(pfr, neg); 1239 PF_MD5_UPD(pfr, port_op); 1240 } 1241 1242 static void 1243 pf_hash_rule_rolling(MD5_CTX *ctx, struct pf_krule *rule) 1244 { 1245 u_int16_t x; 1246 u_int32_t y; 1247 1248 pf_hash_rule_addr(ctx, &rule->src); 1249 pf_hash_rule_addr(ctx, &rule->dst); 1250 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) 1251 PF_MD5_UPD_STR(rule, label[i]); 1252 PF_MD5_UPD_STR(rule, ifname); 1253 PF_MD5_UPD_STR(rule, match_tagname); 1254 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */ 1255 PF_MD5_UPD_HTONL(rule, os_fingerprint, y); 1256 PF_MD5_UPD_HTONL(rule, prob, y); 1257 PF_MD5_UPD_HTONL(rule, uid.uid[0], y); 1258 PF_MD5_UPD_HTONL(rule, uid.uid[1], y); 1259 PF_MD5_UPD(rule, uid.op); 1260 PF_MD5_UPD_HTONL(rule, gid.gid[0], y); 1261 PF_MD5_UPD_HTONL(rule, gid.gid[1], y); 1262 PF_MD5_UPD(rule, gid.op); 1263 PF_MD5_UPD_HTONL(rule, rule_flag, y); 1264 PF_MD5_UPD(rule, action); 1265 PF_MD5_UPD(rule, direction); 1266 PF_MD5_UPD(rule, af); 1267 PF_MD5_UPD(rule, quick); 1268 PF_MD5_UPD(rule, ifnot); 1269 PF_MD5_UPD(rule, match_tag_not); 1270 PF_MD5_UPD(rule, natpass); 1271 PF_MD5_UPD(rule, keep_state); 1272 PF_MD5_UPD(rule, proto); 1273 PF_MD5_UPD(rule, type); 1274 PF_MD5_UPD(rule, code); 1275 PF_MD5_UPD(rule, flags); 1276 PF_MD5_UPD(rule, flagset); 1277 PF_MD5_UPD(rule, allow_opts); 1278 PF_MD5_UPD(rule, rt); 1279 PF_MD5_UPD(rule, tos); 1280 if (rule->anchor != NULL) 1281 PF_MD5_UPD_STR(rule, anchor->path); 1282 } 1283 1284 static void 1285 pf_hash_rule(struct pf_krule *rule) 1286 { 1287 MD5_CTX ctx; 1288 1289 MD5Init(&ctx); 1290 pf_hash_rule_rolling(&ctx, rule); 1291 MD5Final(rule->md5sum, &ctx); 1292 } 1293 1294 static int 1295 pf_krule_compare(struct pf_krule *a, struct pf_krule *b) 1296 { 1297 1298 return (memcmp(a->md5sum, b->md5sum, PF_MD5_DIGEST_LENGTH)); 1299 } 1300 1301 static int 1302 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor) 1303 { 1304 struct pf_kruleset *rs; 1305 struct pf_krule *rule, **old_array, *old_rule; 1306 struct pf_krulequeue *old_rules; 1307 struct pf_krule_global *old_tree; 1308 int error; 1309 u_int32_t old_rcount; 1310 1311 PF_RULES_WASSERT(); 1312 1313 if (rs_num < 0 || rs_num >= PF_RULESET_MAX) 1314 return (EINVAL); 1315 rs = pf_find_kruleset(anchor); 1316 if (rs == NULL || !rs->rules[rs_num].inactive.open || 1317 ticket != rs->rules[rs_num].inactive.ticket) 1318 return (EBUSY); 1319 1320 /* Calculate checksum for the main ruleset */ 1321 if (rs == &pf_main_ruleset) { 1322 error = pf_setup_pfsync_matching(rs); 1323 if (error != 0) 1324 return (error); 1325 } 1326 1327 /* Swap rules, keep the old. */ 1328 old_rules = rs->rules[rs_num].active.ptr; 1329 old_rcount = rs->rules[rs_num].active.rcount; 1330 old_array = rs->rules[rs_num].active.ptr_array; 1331 old_tree = rs->rules[rs_num].active.tree; 1332 1333 rs->rules[rs_num].active.ptr = 1334 rs->rules[rs_num].inactive.ptr; 1335 rs->rules[rs_num].active.ptr_array = 1336 rs->rules[rs_num].inactive.ptr_array; 1337 rs->rules[rs_num].active.tree = 1338 rs->rules[rs_num].inactive.tree; 1339 rs->rules[rs_num].active.rcount = 1340 rs->rules[rs_num].inactive.rcount; 1341 1342 /* Attempt to preserve counter information. */ 1343 if (V_pf_status.keep_counters && old_tree != NULL) { 1344 TAILQ_FOREACH(rule, rs->rules[rs_num].active.ptr, 1345 entries) { 1346 old_rule = RB_FIND(pf_krule_global, old_tree, rule); 1347 if (old_rule == NULL) { 1348 continue; 1349 } 1350 pf_counter_u64_critical_enter(); 1351 pf_counter_u64_add_protected(&rule->evaluations, 1352 pf_counter_u64_fetch(&old_rule->evaluations)); 1353 pf_counter_u64_add_protected(&rule->packets[0], 1354 pf_counter_u64_fetch(&old_rule->packets[0])); 1355 pf_counter_u64_add_protected(&rule->packets[1], 1356 pf_counter_u64_fetch(&old_rule->packets[1])); 1357 pf_counter_u64_add_protected(&rule->bytes[0], 1358 pf_counter_u64_fetch(&old_rule->bytes[0])); 1359 pf_counter_u64_add_protected(&rule->bytes[1], 1360 pf_counter_u64_fetch(&old_rule->bytes[1])); 1361 pf_counter_u64_critical_exit(); 1362 } 1363 } 1364 1365 rs->rules[rs_num].inactive.ptr = old_rules; 1366 rs->rules[rs_num].inactive.ptr_array = old_array; 1367 rs->rules[rs_num].inactive.tree = NULL; 1368 rs->rules[rs_num].inactive.rcount = old_rcount; 1369 1370 rs->rules[rs_num].active.ticket = 1371 rs->rules[rs_num].inactive.ticket; 1372 pf_calc_skip_steps(rs->rules[rs_num].active.ptr); 1373 1374 /* Purge the old rule list. */ 1375 PF_UNLNKDRULES_LOCK(); 1376 while ((rule = TAILQ_FIRST(old_rules)) != NULL) 1377 pf_unlink_rule_locked(old_rules, rule); 1378 PF_UNLNKDRULES_UNLOCK(); 1379 if (rs->rules[rs_num].inactive.ptr_array) 1380 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP); 1381 rs->rules[rs_num].inactive.ptr_array = NULL; 1382 rs->rules[rs_num].inactive.rcount = 0; 1383 rs->rules[rs_num].inactive.open = 0; 1384 pf_remove_if_empty_kruleset(rs); 1385 free(old_tree, M_TEMP); 1386 1387 return (0); 1388 } 1389 1390 static int 1391 pf_setup_pfsync_matching(struct pf_kruleset *rs) 1392 { 1393 MD5_CTX ctx; 1394 struct pf_krule *rule; 1395 int rs_cnt; 1396 u_int8_t digest[PF_MD5_DIGEST_LENGTH]; 1397 1398 MD5Init(&ctx); 1399 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) { 1400 /* XXX PF_RULESET_SCRUB as well? */ 1401 if (rs_cnt == PF_RULESET_SCRUB) 1402 continue; 1403 1404 if (rs->rules[rs_cnt].inactive.ptr_array) 1405 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP); 1406 rs->rules[rs_cnt].inactive.ptr_array = NULL; 1407 1408 if (rs->rules[rs_cnt].inactive.rcount) { 1409 rs->rules[rs_cnt].inactive.ptr_array = 1410 malloc(sizeof(caddr_t) * 1411 rs->rules[rs_cnt].inactive.rcount, 1412 M_TEMP, M_NOWAIT); 1413 1414 if (!rs->rules[rs_cnt].inactive.ptr_array) 1415 return (ENOMEM); 1416 } 1417 1418 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr, 1419 entries) { 1420 pf_hash_rule_rolling(&ctx, rule); 1421 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule; 1422 } 1423 } 1424 1425 MD5Final(digest, &ctx); 1426 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum)); 1427 return (0); 1428 } 1429 1430 static int 1431 pf_addr_setup(struct pf_kruleset *ruleset, struct pf_addr_wrap *addr, 1432 sa_family_t af) 1433 { 1434 int error = 0; 1435 1436 switch (addr->type) { 1437 case PF_ADDR_TABLE: 1438 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname); 1439 if (addr->p.tbl == NULL) 1440 error = ENOMEM; 1441 break; 1442 case PF_ADDR_DYNIFTL: 1443 error = pfi_dynaddr_setup(addr, af); 1444 break; 1445 } 1446 1447 return (error); 1448 } 1449 1450 static void 1451 pf_addr_copyout(struct pf_addr_wrap *addr) 1452 { 1453 1454 switch (addr->type) { 1455 case PF_ADDR_DYNIFTL: 1456 pfi_dynaddr_copyout(addr); 1457 break; 1458 case PF_ADDR_TABLE: 1459 pf_tbladdr_copyout(addr); 1460 break; 1461 } 1462 } 1463 1464 static void 1465 pf_src_node_copy(const struct pf_ksrc_node *in, struct pf_src_node *out) 1466 { 1467 int secs = time_uptime, diff; 1468 1469 bzero(out, sizeof(struct pf_src_node)); 1470 1471 bcopy(&in->addr, &out->addr, sizeof(struct pf_addr)); 1472 bcopy(&in->raddr, &out->raddr, sizeof(struct pf_addr)); 1473 1474 if (in->rule.ptr != NULL) 1475 out->rule.nr = in->rule.ptr->nr; 1476 1477 for (int i = 0; i < 2; i++) { 1478 out->bytes[i] = counter_u64_fetch(in->bytes[i]); 1479 out->packets[i] = counter_u64_fetch(in->packets[i]); 1480 } 1481 1482 out->states = in->states; 1483 out->conn = in->conn; 1484 out->af = in->af; 1485 out->ruletype = in->ruletype; 1486 1487 out->creation = secs - in->creation; 1488 if (out->expire > secs) 1489 out->expire -= secs; 1490 else 1491 out->expire = 0; 1492 1493 /* Adjust the connection rate estimate. */ 1494 diff = secs - in->conn_rate.last; 1495 if (diff >= in->conn_rate.seconds) 1496 out->conn_rate.count = 0; 1497 else 1498 out->conn_rate.count -= 1499 in->conn_rate.count * diff / 1500 in->conn_rate.seconds; 1501 } 1502 1503 #ifdef ALTQ 1504 /* 1505 * Handle export of struct pf_kaltq to user binaries that may be using any 1506 * version of struct pf_altq. 1507 */ 1508 static int 1509 pf_export_kaltq(struct pf_altq *q, struct pfioc_altq_v1 *pa, size_t ioc_size) 1510 { 1511 u_int32_t version; 1512 1513 if (ioc_size == sizeof(struct pfioc_altq_v0)) 1514 version = 0; 1515 else 1516 version = pa->version; 1517 1518 if (version > PFIOC_ALTQ_VERSION) 1519 return (EINVAL); 1520 1521 #define ASSIGN(x) exported_q->x = q->x 1522 #define COPY(x) \ 1523 bcopy(&q->x, &exported_q->x, min(sizeof(q->x), sizeof(exported_q->x))) 1524 #define SATU16(x) (u_int32_t)uqmin((x), USHRT_MAX) 1525 #define SATU32(x) (u_int32_t)uqmin((x), UINT_MAX) 1526 1527 switch (version) { 1528 case 0: { 1529 struct pf_altq_v0 *exported_q = 1530 &((struct pfioc_altq_v0 *)pa)->altq; 1531 1532 COPY(ifname); 1533 1534 ASSIGN(scheduler); 1535 ASSIGN(tbrsize); 1536 exported_q->tbrsize = SATU16(q->tbrsize); 1537 exported_q->ifbandwidth = SATU32(q->ifbandwidth); 1538 1539 COPY(qname); 1540 COPY(parent); 1541 ASSIGN(parent_qid); 1542 exported_q->bandwidth = SATU32(q->bandwidth); 1543 ASSIGN(priority); 1544 ASSIGN(local_flags); 1545 1546 ASSIGN(qlimit); 1547 ASSIGN(flags); 1548 1549 if (q->scheduler == ALTQT_HFSC) { 1550 #define ASSIGN_OPT(x) exported_q->pq_u.hfsc_opts.x = q->pq_u.hfsc_opts.x 1551 #define ASSIGN_OPT_SATU32(x) exported_q->pq_u.hfsc_opts.x = \ 1552 SATU32(q->pq_u.hfsc_opts.x) 1553 1554 ASSIGN_OPT_SATU32(rtsc_m1); 1555 ASSIGN_OPT(rtsc_d); 1556 ASSIGN_OPT_SATU32(rtsc_m2); 1557 1558 ASSIGN_OPT_SATU32(lssc_m1); 1559 ASSIGN_OPT(lssc_d); 1560 ASSIGN_OPT_SATU32(lssc_m2); 1561 1562 ASSIGN_OPT_SATU32(ulsc_m1); 1563 ASSIGN_OPT(ulsc_d); 1564 ASSIGN_OPT_SATU32(ulsc_m2); 1565 1566 ASSIGN_OPT(flags); 1567 1568 #undef ASSIGN_OPT 1569 #undef ASSIGN_OPT_SATU32 1570 } else 1571 COPY(pq_u); 1572 1573 ASSIGN(qid); 1574 break; 1575 } 1576 case 1: { 1577 struct pf_altq_v1 *exported_q = 1578 &((struct pfioc_altq_v1 *)pa)->altq; 1579 1580 COPY(ifname); 1581 1582 ASSIGN(scheduler); 1583 ASSIGN(tbrsize); 1584 ASSIGN(ifbandwidth); 1585 1586 COPY(qname); 1587 COPY(parent); 1588 ASSIGN(parent_qid); 1589 ASSIGN(bandwidth); 1590 ASSIGN(priority); 1591 ASSIGN(local_flags); 1592 1593 ASSIGN(qlimit); 1594 ASSIGN(flags); 1595 COPY(pq_u); 1596 1597 ASSIGN(qid); 1598 break; 1599 } 1600 default: 1601 panic("%s: unhandled struct pfioc_altq version", __func__); 1602 break; 1603 } 1604 1605 #undef ASSIGN 1606 #undef COPY 1607 #undef SATU16 1608 #undef SATU32 1609 1610 return (0); 1611 } 1612 1613 /* 1614 * Handle import to struct pf_kaltq of struct pf_altq from user binaries 1615 * that may be using any version of it. 1616 */ 1617 static int 1618 pf_import_kaltq(struct pfioc_altq_v1 *pa, struct pf_altq *q, size_t ioc_size) 1619 { 1620 u_int32_t version; 1621 1622 if (ioc_size == sizeof(struct pfioc_altq_v0)) 1623 version = 0; 1624 else 1625 version = pa->version; 1626 1627 if (version > PFIOC_ALTQ_VERSION) 1628 return (EINVAL); 1629 1630 #define ASSIGN(x) q->x = imported_q->x 1631 #define COPY(x) \ 1632 bcopy(&imported_q->x, &q->x, min(sizeof(imported_q->x), sizeof(q->x))) 1633 1634 switch (version) { 1635 case 0: { 1636 struct pf_altq_v0 *imported_q = 1637 &((struct pfioc_altq_v0 *)pa)->altq; 1638 1639 COPY(ifname); 1640 1641 ASSIGN(scheduler); 1642 ASSIGN(tbrsize); /* 16-bit -> 32-bit */ 1643 ASSIGN(ifbandwidth); /* 32-bit -> 64-bit */ 1644 1645 COPY(qname); 1646 COPY(parent); 1647 ASSIGN(parent_qid); 1648 ASSIGN(bandwidth); /* 32-bit -> 64-bit */ 1649 ASSIGN(priority); 1650 ASSIGN(local_flags); 1651 1652 ASSIGN(qlimit); 1653 ASSIGN(flags); 1654 1655 if (imported_q->scheduler == ALTQT_HFSC) { 1656 #define ASSIGN_OPT(x) q->pq_u.hfsc_opts.x = imported_q->pq_u.hfsc_opts.x 1657 1658 /* 1659 * The m1 and m2 parameters are being copied from 1660 * 32-bit to 64-bit. 1661 */ 1662 ASSIGN_OPT(rtsc_m1); 1663 ASSIGN_OPT(rtsc_d); 1664 ASSIGN_OPT(rtsc_m2); 1665 1666 ASSIGN_OPT(lssc_m1); 1667 ASSIGN_OPT(lssc_d); 1668 ASSIGN_OPT(lssc_m2); 1669 1670 ASSIGN_OPT(ulsc_m1); 1671 ASSIGN_OPT(ulsc_d); 1672 ASSIGN_OPT(ulsc_m2); 1673 1674 ASSIGN_OPT(flags); 1675 1676 #undef ASSIGN_OPT 1677 } else 1678 COPY(pq_u); 1679 1680 ASSIGN(qid); 1681 break; 1682 } 1683 case 1: { 1684 struct pf_altq_v1 *imported_q = 1685 &((struct pfioc_altq_v1 *)pa)->altq; 1686 1687 COPY(ifname); 1688 1689 ASSIGN(scheduler); 1690 ASSIGN(tbrsize); 1691 ASSIGN(ifbandwidth); 1692 1693 COPY(qname); 1694 COPY(parent); 1695 ASSIGN(parent_qid); 1696 ASSIGN(bandwidth); 1697 ASSIGN(priority); 1698 ASSIGN(local_flags); 1699 1700 ASSIGN(qlimit); 1701 ASSIGN(flags); 1702 COPY(pq_u); 1703 1704 ASSIGN(qid); 1705 break; 1706 } 1707 default: 1708 panic("%s: unhandled struct pfioc_altq version", __func__); 1709 break; 1710 } 1711 1712 #undef ASSIGN 1713 #undef COPY 1714 1715 return (0); 1716 } 1717 1718 static struct pf_altq * 1719 pf_altq_get_nth_active(u_int32_t n) 1720 { 1721 struct pf_altq *altq; 1722 u_int32_t nr; 1723 1724 nr = 0; 1725 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 1726 if (nr == n) 1727 return (altq); 1728 nr++; 1729 } 1730 1731 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) { 1732 if (nr == n) 1733 return (altq); 1734 nr++; 1735 } 1736 1737 return (NULL); 1738 } 1739 #endif /* ALTQ */ 1740 1741 struct pf_krule * 1742 pf_krule_alloc(void) 1743 { 1744 struct pf_krule *rule; 1745 1746 rule = malloc(sizeof(struct pf_krule), M_PFRULE, M_WAITOK | M_ZERO); 1747 mtx_init(&rule->rpool.mtx, "pf_krule_pool", NULL, MTX_DEF); 1748 return (rule); 1749 } 1750 1751 void 1752 pf_krule_free(struct pf_krule *rule) 1753 { 1754 #ifdef PF_WANT_32_TO_64_COUNTER 1755 bool wowned; 1756 #endif 1757 1758 if (rule == NULL) 1759 return; 1760 1761 #ifdef PF_WANT_32_TO_64_COUNTER 1762 if (rule->allrulelinked) { 1763 wowned = PF_RULES_WOWNED(); 1764 if (!wowned) 1765 PF_RULES_WLOCK(); 1766 LIST_REMOVE(rule, allrulelist); 1767 V_pf_allrulecount--; 1768 if (!wowned) 1769 PF_RULES_WUNLOCK(); 1770 } 1771 #endif 1772 1773 pf_counter_u64_deinit(&rule->evaluations); 1774 for (int i = 0; i < 2; i++) { 1775 pf_counter_u64_deinit(&rule->packets[i]); 1776 pf_counter_u64_deinit(&rule->bytes[i]); 1777 } 1778 counter_u64_free(rule->states_cur); 1779 counter_u64_free(rule->states_tot); 1780 counter_u64_free(rule->src_nodes); 1781 1782 mtx_destroy(&rule->rpool.mtx); 1783 free(rule, M_PFRULE); 1784 } 1785 1786 static void 1787 pf_kpooladdr_to_pooladdr(const struct pf_kpooladdr *kpool, 1788 struct pf_pooladdr *pool) 1789 { 1790 1791 bzero(pool, sizeof(*pool)); 1792 bcopy(&kpool->addr, &pool->addr, sizeof(pool->addr)); 1793 strlcpy(pool->ifname, kpool->ifname, sizeof(pool->ifname)); 1794 } 1795 1796 static int 1797 pf_pooladdr_to_kpooladdr(const struct pf_pooladdr *pool, 1798 struct pf_kpooladdr *kpool) 1799 { 1800 int ret; 1801 1802 bzero(kpool, sizeof(*kpool)); 1803 bcopy(&pool->addr, &kpool->addr, sizeof(kpool->addr)); 1804 ret = pf_user_strcpy(kpool->ifname, pool->ifname, 1805 sizeof(kpool->ifname)); 1806 return (ret); 1807 } 1808 1809 static void 1810 pf_kpool_to_pool(const struct pf_kpool *kpool, struct pf_pool *pool) 1811 { 1812 bzero(pool, sizeof(*pool)); 1813 1814 bcopy(&kpool->key, &pool->key, sizeof(pool->key)); 1815 bcopy(&kpool->counter, &pool->counter, sizeof(pool->counter)); 1816 1817 pool->tblidx = kpool->tblidx; 1818 pool->proxy_port[0] = kpool->proxy_port[0]; 1819 pool->proxy_port[1] = kpool->proxy_port[1]; 1820 pool->opts = kpool->opts; 1821 } 1822 1823 static void 1824 pf_pool_to_kpool(const struct pf_pool *pool, struct pf_kpool *kpool) 1825 { 1826 _Static_assert(sizeof(pool->key) == sizeof(kpool->key), ""); 1827 _Static_assert(sizeof(pool->counter) == sizeof(kpool->counter), ""); 1828 1829 bcopy(&pool->key, &kpool->key, sizeof(kpool->key)); 1830 bcopy(&pool->counter, &kpool->counter, sizeof(kpool->counter)); 1831 1832 kpool->tblidx = pool->tblidx; 1833 kpool->proxy_port[0] = pool->proxy_port[0]; 1834 kpool->proxy_port[1] = pool->proxy_port[1]; 1835 kpool->opts = pool->opts; 1836 } 1837 1838 static void 1839 pf_krule_to_rule(const struct pf_krule *krule, struct pf_rule *rule) 1840 { 1841 1842 bzero(rule, sizeof(*rule)); 1843 1844 bcopy(&krule->src, &rule->src, sizeof(rule->src)); 1845 bcopy(&krule->dst, &rule->dst, sizeof(rule->dst)); 1846 1847 for (int i = 0; i < PF_SKIP_COUNT; ++i) { 1848 if (rule->skip[i].ptr == NULL) 1849 rule->skip[i].nr = -1; 1850 else 1851 rule->skip[i].nr = krule->skip[i].ptr->nr; 1852 } 1853 1854 strlcpy(rule->label, krule->label[0], sizeof(rule->label)); 1855 strlcpy(rule->ifname, krule->ifname, sizeof(rule->ifname)); 1856 strlcpy(rule->qname, krule->qname, sizeof(rule->qname)); 1857 strlcpy(rule->pqname, krule->pqname, sizeof(rule->pqname)); 1858 strlcpy(rule->tagname, krule->tagname, sizeof(rule->tagname)); 1859 strlcpy(rule->match_tagname, krule->match_tagname, 1860 sizeof(rule->match_tagname)); 1861 strlcpy(rule->overload_tblname, krule->overload_tblname, 1862 sizeof(rule->overload_tblname)); 1863 1864 pf_kpool_to_pool(&krule->rpool, &rule->rpool); 1865 1866 rule->evaluations = pf_counter_u64_fetch(&krule->evaluations); 1867 for (int i = 0; i < 2; i++) { 1868 rule->packets[i] = pf_counter_u64_fetch(&krule->packets[i]); 1869 rule->bytes[i] = pf_counter_u64_fetch(&krule->bytes[i]); 1870 } 1871 1872 /* kif, anchor, overload_tbl are not copied over. */ 1873 1874 rule->os_fingerprint = krule->os_fingerprint; 1875 1876 rule->rtableid = krule->rtableid; 1877 bcopy(krule->timeout, rule->timeout, sizeof(krule->timeout)); 1878 rule->max_states = krule->max_states; 1879 rule->max_src_nodes = krule->max_src_nodes; 1880 rule->max_src_states = krule->max_src_states; 1881 rule->max_src_conn = krule->max_src_conn; 1882 rule->max_src_conn_rate.limit = krule->max_src_conn_rate.limit; 1883 rule->max_src_conn_rate.seconds = krule->max_src_conn_rate.seconds; 1884 rule->qid = krule->qid; 1885 rule->pqid = krule->pqid; 1886 rule->nr = krule->nr; 1887 rule->prob = krule->prob; 1888 rule->cuid = krule->cuid; 1889 rule->cpid = krule->cpid; 1890 1891 rule->return_icmp = krule->return_icmp; 1892 rule->return_icmp6 = krule->return_icmp6; 1893 rule->max_mss = krule->max_mss; 1894 rule->tag = krule->tag; 1895 rule->match_tag = krule->match_tag; 1896 rule->scrub_flags = krule->scrub_flags; 1897 1898 bcopy(&krule->uid, &rule->uid, sizeof(krule->uid)); 1899 bcopy(&krule->gid, &rule->gid, sizeof(krule->gid)); 1900 1901 rule->rule_flag = krule->rule_flag; 1902 rule->action = krule->action; 1903 rule->direction = krule->direction; 1904 rule->log = krule->log; 1905 rule->logif = krule->logif; 1906 rule->quick = krule->quick; 1907 rule->ifnot = krule->ifnot; 1908 rule->match_tag_not = krule->match_tag_not; 1909 rule->natpass = krule->natpass; 1910 1911 rule->keep_state = krule->keep_state; 1912 rule->af = krule->af; 1913 rule->proto = krule->proto; 1914 rule->type = krule->type; 1915 rule->code = krule->code; 1916 rule->flags = krule->flags; 1917 rule->flagset = krule->flagset; 1918 rule->min_ttl = krule->min_ttl; 1919 rule->allow_opts = krule->allow_opts; 1920 rule->rt = krule->rt; 1921 rule->return_ttl = krule->return_ttl; 1922 rule->tos = krule->tos; 1923 rule->set_tos = krule->set_tos; 1924 rule->anchor_relative = krule->anchor_relative; 1925 rule->anchor_wildcard = krule->anchor_wildcard; 1926 1927 rule->flush = krule->flush; 1928 rule->prio = krule->prio; 1929 rule->set_prio[0] = krule->set_prio[0]; 1930 rule->set_prio[1] = krule->set_prio[1]; 1931 1932 bcopy(&krule->divert, &rule->divert, sizeof(krule->divert)); 1933 1934 rule->u_states_cur = counter_u64_fetch(krule->states_cur); 1935 rule->u_states_tot = counter_u64_fetch(krule->states_tot); 1936 rule->u_src_nodes = counter_u64_fetch(krule->src_nodes); 1937 } 1938 1939 static int 1940 pf_rule_to_krule(const struct pf_rule *rule, struct pf_krule *krule) 1941 { 1942 int ret; 1943 1944 #ifndef INET 1945 if (rule->af == AF_INET) { 1946 return (EAFNOSUPPORT); 1947 } 1948 #endif /* INET */ 1949 #ifndef INET6 1950 if (rule->af == AF_INET6) { 1951 return (EAFNOSUPPORT); 1952 } 1953 #endif /* INET6 */ 1954 1955 ret = pf_check_rule_addr(&rule->src); 1956 if (ret != 0) 1957 return (ret); 1958 ret = pf_check_rule_addr(&rule->dst); 1959 if (ret != 0) 1960 return (ret); 1961 1962 bcopy(&rule->src, &krule->src, sizeof(rule->src)); 1963 bcopy(&rule->dst, &krule->dst, sizeof(rule->dst)); 1964 1965 ret = pf_user_strcpy(krule->label[0], rule->label, sizeof(rule->label)); 1966 if (ret != 0) 1967 return (ret); 1968 ret = pf_user_strcpy(krule->ifname, rule->ifname, sizeof(rule->ifname)); 1969 if (ret != 0) 1970 return (ret); 1971 ret = pf_user_strcpy(krule->qname, rule->qname, sizeof(rule->qname)); 1972 if (ret != 0) 1973 return (ret); 1974 ret = pf_user_strcpy(krule->pqname, rule->pqname, sizeof(rule->pqname)); 1975 if (ret != 0) 1976 return (ret); 1977 ret = pf_user_strcpy(krule->tagname, rule->tagname, 1978 sizeof(rule->tagname)); 1979 if (ret != 0) 1980 return (ret); 1981 ret = pf_user_strcpy(krule->match_tagname, rule->match_tagname, 1982 sizeof(rule->match_tagname)); 1983 if (ret != 0) 1984 return (ret); 1985 ret = pf_user_strcpy(krule->overload_tblname, rule->overload_tblname, 1986 sizeof(rule->overload_tblname)); 1987 if (ret != 0) 1988 return (ret); 1989 1990 pf_pool_to_kpool(&rule->rpool, &krule->rpool); 1991 1992 /* Don't allow userspace to set evaulations, packets or bytes. */ 1993 /* kif, anchor, overload_tbl are not copied over. */ 1994 1995 krule->os_fingerprint = rule->os_fingerprint; 1996 1997 krule->rtableid = rule->rtableid; 1998 bcopy(rule->timeout, krule->timeout, sizeof(krule->timeout)); 1999 krule->max_states = rule->max_states; 2000 krule->max_src_nodes = rule->max_src_nodes; 2001 krule->max_src_states = rule->max_src_states; 2002 krule->max_src_conn = rule->max_src_conn; 2003 krule->max_src_conn_rate.limit = rule->max_src_conn_rate.limit; 2004 krule->max_src_conn_rate.seconds = rule->max_src_conn_rate.seconds; 2005 krule->qid = rule->qid; 2006 krule->pqid = rule->pqid; 2007 krule->nr = rule->nr; 2008 krule->prob = rule->prob; 2009 krule->cuid = rule->cuid; 2010 krule->cpid = rule->cpid; 2011 2012 krule->return_icmp = rule->return_icmp; 2013 krule->return_icmp6 = rule->return_icmp6; 2014 krule->max_mss = rule->max_mss; 2015 krule->tag = rule->tag; 2016 krule->match_tag = rule->match_tag; 2017 krule->scrub_flags = rule->scrub_flags; 2018 2019 bcopy(&rule->uid, &krule->uid, sizeof(krule->uid)); 2020 bcopy(&rule->gid, &krule->gid, sizeof(krule->gid)); 2021 2022 krule->rule_flag = rule->rule_flag; 2023 krule->action = rule->action; 2024 krule->direction = rule->direction; 2025 krule->log = rule->log; 2026 krule->logif = rule->logif; 2027 krule->quick = rule->quick; 2028 krule->ifnot = rule->ifnot; 2029 krule->match_tag_not = rule->match_tag_not; 2030 krule->natpass = rule->natpass; 2031 2032 krule->keep_state = rule->keep_state; 2033 krule->af = rule->af; 2034 krule->proto = rule->proto; 2035 krule->type = rule->type; 2036 krule->code = rule->code; 2037 krule->flags = rule->flags; 2038 krule->flagset = rule->flagset; 2039 krule->min_ttl = rule->min_ttl; 2040 krule->allow_opts = rule->allow_opts; 2041 krule->rt = rule->rt; 2042 krule->return_ttl = rule->return_ttl; 2043 krule->tos = rule->tos; 2044 krule->set_tos = rule->set_tos; 2045 2046 krule->flush = rule->flush; 2047 krule->prio = rule->prio; 2048 krule->set_prio[0] = rule->set_prio[0]; 2049 krule->set_prio[1] = rule->set_prio[1]; 2050 2051 bcopy(&rule->divert, &krule->divert, sizeof(krule->divert)); 2052 2053 return (0); 2054 } 2055 2056 static int 2057 pf_state_kill_to_kstate_kill(const struct pfioc_state_kill *psk, 2058 struct pf_kstate_kill *kill) 2059 { 2060 int ret; 2061 2062 bzero(kill, sizeof(*kill)); 2063 2064 bcopy(&psk->psk_pfcmp, &kill->psk_pfcmp, sizeof(kill->psk_pfcmp)); 2065 kill->psk_af = psk->psk_af; 2066 kill->psk_proto = psk->psk_proto; 2067 bcopy(&psk->psk_src, &kill->psk_src, sizeof(kill->psk_src)); 2068 bcopy(&psk->psk_dst, &kill->psk_dst, sizeof(kill->psk_dst)); 2069 ret = pf_user_strcpy(kill->psk_ifname, psk->psk_ifname, 2070 sizeof(kill->psk_ifname)); 2071 if (ret != 0) 2072 return (ret); 2073 ret = pf_user_strcpy(kill->psk_label, psk->psk_label, 2074 sizeof(kill->psk_label)); 2075 if (ret != 0) 2076 return (ret); 2077 2078 return (0); 2079 } 2080 2081 static int 2082 pf_ioctl_addrule(struct pf_krule *rule, uint32_t ticket, 2083 uint32_t pool_ticket, const char *anchor, const char *anchor_call, 2084 struct thread *td) 2085 { 2086 struct pf_kruleset *ruleset; 2087 struct pf_krule *tail; 2088 struct pf_kpooladdr *pa; 2089 struct pfi_kkif *kif = NULL; 2090 int rs_num; 2091 int error = 0; 2092 2093 if ((rule->return_icmp >> 8) > ICMP_MAXTYPE) { 2094 error = EINVAL; 2095 goto errout_unlocked; 2096 } 2097 2098 #define ERROUT(x) ERROUT_FUNCTION(errout, x) 2099 2100 if (rule->ifname[0]) 2101 kif = pf_kkif_create(M_WAITOK); 2102 pf_counter_u64_init(&rule->evaluations, M_WAITOK); 2103 for (int i = 0; i < 2; i++) { 2104 pf_counter_u64_init(&rule->packets[i], M_WAITOK); 2105 pf_counter_u64_init(&rule->bytes[i], M_WAITOK); 2106 } 2107 rule->states_cur = counter_u64_alloc(M_WAITOK); 2108 rule->states_tot = counter_u64_alloc(M_WAITOK); 2109 rule->src_nodes = counter_u64_alloc(M_WAITOK); 2110 rule->cuid = td->td_ucred->cr_ruid; 2111 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0; 2112 TAILQ_INIT(&rule->rpool.list); 2113 2114 PF_CONFIG_LOCK(); 2115 PF_RULES_WLOCK(); 2116 #ifdef PF_WANT_32_TO_64_COUNTER 2117 LIST_INSERT_HEAD(&V_pf_allrulelist, rule, allrulelist); 2118 MPASS(!rule->allrulelinked); 2119 rule->allrulelinked = true; 2120 V_pf_allrulecount++; 2121 #endif 2122 ruleset = pf_find_kruleset(anchor); 2123 if (ruleset == NULL) 2124 ERROUT(EINVAL); 2125 rs_num = pf_get_ruleset_number(rule->action); 2126 if (rs_num >= PF_RULESET_MAX) 2127 ERROUT(EINVAL); 2128 if (ticket != ruleset->rules[rs_num].inactive.ticket) { 2129 DPFPRINTF(PF_DEBUG_MISC, 2130 ("ticket: %d != [%d]%d\n", ticket, rs_num, 2131 ruleset->rules[rs_num].inactive.ticket)); 2132 ERROUT(EBUSY); 2133 } 2134 if (pool_ticket != V_ticket_pabuf) { 2135 DPFPRINTF(PF_DEBUG_MISC, 2136 ("pool_ticket: %d != %d\n", pool_ticket, 2137 V_ticket_pabuf)); 2138 ERROUT(EBUSY); 2139 } 2140 2141 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr, 2142 pf_krulequeue); 2143 if (tail) 2144 rule->nr = tail->nr + 1; 2145 else 2146 rule->nr = 0; 2147 if (rule->ifname[0]) { 2148 rule->kif = pfi_kkif_attach(kif, rule->ifname); 2149 kif = NULL; 2150 pfi_kkif_ref(rule->kif); 2151 } else 2152 rule->kif = NULL; 2153 2154 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs) 2155 error = EBUSY; 2156 2157 #ifdef ALTQ 2158 /* set queue IDs */ 2159 if (rule->qname[0] != 0) { 2160 if ((rule->qid = pf_qname2qid(rule->qname)) == 0) 2161 error = EBUSY; 2162 else if (rule->pqname[0] != 0) { 2163 if ((rule->pqid = 2164 pf_qname2qid(rule->pqname)) == 0) 2165 error = EBUSY; 2166 } else 2167 rule->pqid = rule->qid; 2168 } 2169 #endif 2170 if (rule->tagname[0]) 2171 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0) 2172 error = EBUSY; 2173 if (rule->match_tagname[0]) 2174 if ((rule->match_tag = 2175 pf_tagname2tag(rule->match_tagname)) == 0) 2176 error = EBUSY; 2177 if (rule->rt && !rule->direction) 2178 error = EINVAL; 2179 if (!rule->log) 2180 rule->logif = 0; 2181 if (rule->logif >= PFLOGIFS_MAX) 2182 error = EINVAL; 2183 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af)) 2184 error = ENOMEM; 2185 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af)) 2186 error = ENOMEM; 2187 if (pf_kanchor_setup(rule, ruleset, anchor_call)) 2188 error = EINVAL; 2189 if (rule->scrub_flags & PFSTATE_SETPRIO && 2190 (rule->set_prio[0] > PF_PRIO_MAX || 2191 rule->set_prio[1] > PF_PRIO_MAX)) 2192 error = EINVAL; 2193 TAILQ_FOREACH(pa, &V_pf_pabuf, entries) 2194 if (pa->addr.type == PF_ADDR_TABLE) { 2195 pa->addr.p.tbl = pfr_attach_table(ruleset, 2196 pa->addr.v.tblname); 2197 if (pa->addr.p.tbl == NULL) 2198 error = ENOMEM; 2199 } 2200 2201 rule->overload_tbl = NULL; 2202 if (rule->overload_tblname[0]) { 2203 if ((rule->overload_tbl = pfr_attach_table(ruleset, 2204 rule->overload_tblname)) == NULL) 2205 error = EINVAL; 2206 else 2207 rule->overload_tbl->pfrkt_flags |= 2208 PFR_TFLAG_ACTIVE; 2209 } 2210 2211 pf_mv_kpool(&V_pf_pabuf, &rule->rpool.list); 2212 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) || 2213 (rule->action == PF_BINAT)) && rule->anchor == NULL) || 2214 (rule->rt > PF_NOPFROUTE)) && 2215 (TAILQ_FIRST(&rule->rpool.list) == NULL)) 2216 error = EINVAL; 2217 2218 if (error) { 2219 pf_free_rule(rule); 2220 rule = NULL; 2221 ERROUT(error); 2222 } 2223 2224 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list); 2225 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr, 2226 rule, entries); 2227 ruleset->rules[rs_num].inactive.rcount++; 2228 2229 PF_RULES_WUNLOCK(); 2230 pf_hash_rule(rule); 2231 if (RB_INSERT(pf_krule_global, ruleset->rules[rs_num].inactive.tree, rule) != NULL) { 2232 PF_RULES_WLOCK(); 2233 pf_free_rule(rule); 2234 rule = NULL; 2235 error = EINVAL; 2236 ERROUT(error); 2237 } 2238 PF_CONFIG_UNLOCK(); 2239 2240 return (0); 2241 2242 #undef ERROUT 2243 errout: 2244 PF_RULES_WUNLOCK(); 2245 PF_CONFIG_UNLOCK(); 2246 errout_unlocked: 2247 pf_kkif_free(kif); 2248 pf_krule_free(rule); 2249 return (error); 2250 } 2251 2252 static bool 2253 pf_label_match(const struct pf_krule *rule, const char *label) 2254 { 2255 int i = 0; 2256 2257 while (*rule->label[i]) { 2258 if (strcmp(rule->label[i], label) == 0) 2259 return (true); 2260 i++; 2261 } 2262 2263 return (false); 2264 } 2265 2266 static unsigned int 2267 pf_kill_matching_state(struct pf_state_key_cmp *key, int dir) 2268 { 2269 struct pf_kstate *s; 2270 int more = 0; 2271 2272 s = pf_find_state_all(key, dir, &more); 2273 if (s == NULL) 2274 return (0); 2275 2276 if (more) { 2277 PF_STATE_UNLOCK(s); 2278 return (0); 2279 } 2280 2281 pf_unlink_state(s); 2282 return (1); 2283 } 2284 2285 static int 2286 pf_killstates_row(struct pf_kstate_kill *psk, struct pf_idhash *ih) 2287 { 2288 struct pf_kstate *s; 2289 struct pf_state_key *sk; 2290 struct pf_addr *srcaddr, *dstaddr; 2291 struct pf_state_key_cmp match_key; 2292 int idx, killed = 0; 2293 unsigned int dir; 2294 u_int16_t srcport, dstport; 2295 struct pfi_kkif *kif; 2296 2297 relock_DIOCKILLSTATES: 2298 PF_HASHROW_LOCK(ih); 2299 LIST_FOREACH(s, &ih->states, entry) { 2300 /* For floating states look at the original kif. */ 2301 kif = s->kif == V_pfi_all ? s->orig_kif : s->kif; 2302 2303 sk = s->key[PF_SK_WIRE]; 2304 if (s->direction == PF_OUT) { 2305 srcaddr = &sk->addr[1]; 2306 dstaddr = &sk->addr[0]; 2307 srcport = sk->port[1]; 2308 dstport = sk->port[0]; 2309 } else { 2310 srcaddr = &sk->addr[0]; 2311 dstaddr = &sk->addr[1]; 2312 srcport = sk->port[0]; 2313 dstport = sk->port[1]; 2314 } 2315 2316 if (psk->psk_af && sk->af != psk->psk_af) 2317 continue; 2318 2319 if (psk->psk_proto && psk->psk_proto != sk->proto) 2320 continue; 2321 2322 if (! PF_MATCHA(psk->psk_src.neg, &psk->psk_src.addr.v.a.addr, 2323 &psk->psk_src.addr.v.a.mask, srcaddr, sk->af)) 2324 continue; 2325 2326 if (! PF_MATCHA(psk->psk_dst.neg, &psk->psk_dst.addr.v.a.addr, 2327 &psk->psk_dst.addr.v.a.mask, dstaddr, sk->af)) 2328 continue; 2329 2330 if (! PF_MATCHA(psk->psk_rt_addr.neg, 2331 &psk->psk_rt_addr.addr.v.a.addr, 2332 &psk->psk_rt_addr.addr.v.a.mask, 2333 &s->rt_addr, sk->af)) 2334 continue; 2335 2336 if (psk->psk_src.port_op != 0 && 2337 ! pf_match_port(psk->psk_src.port_op, 2338 psk->psk_src.port[0], psk->psk_src.port[1], srcport)) 2339 continue; 2340 2341 if (psk->psk_dst.port_op != 0 && 2342 ! pf_match_port(psk->psk_dst.port_op, 2343 psk->psk_dst.port[0], psk->psk_dst.port[1], dstport)) 2344 continue; 2345 2346 if (psk->psk_label[0] && 2347 ! pf_label_match(s->rule.ptr, psk->psk_label)) 2348 continue; 2349 2350 if (psk->psk_ifname[0] && strcmp(psk->psk_ifname, 2351 kif->pfik_name)) 2352 continue; 2353 2354 if (psk->psk_kill_match) { 2355 /* Create the key to find matching states, with lock 2356 * held. */ 2357 2358 bzero(&match_key, sizeof(match_key)); 2359 2360 if (s->direction == PF_OUT) { 2361 dir = PF_IN; 2362 idx = PF_SK_STACK; 2363 } else { 2364 dir = PF_OUT; 2365 idx = PF_SK_WIRE; 2366 } 2367 2368 match_key.af = s->key[idx]->af; 2369 match_key.proto = s->key[idx]->proto; 2370 PF_ACPY(&match_key.addr[0], 2371 &s->key[idx]->addr[1], match_key.af); 2372 match_key.port[0] = s->key[idx]->port[1]; 2373 PF_ACPY(&match_key.addr[1], 2374 &s->key[idx]->addr[0], match_key.af); 2375 match_key.port[1] = s->key[idx]->port[0]; 2376 } 2377 2378 pf_unlink_state(s); 2379 killed++; 2380 2381 if (psk->psk_kill_match) 2382 killed += pf_kill_matching_state(&match_key, dir); 2383 2384 goto relock_DIOCKILLSTATES; 2385 } 2386 PF_HASHROW_UNLOCK(ih); 2387 2388 return (killed); 2389 } 2390 2391 static int 2392 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 2393 { 2394 int error = 0; 2395 PF_RULES_RLOCK_TRACKER; 2396 2397 #define ERROUT_IOCTL(target, x) \ 2398 do { \ 2399 error = (x); \ 2400 SDT_PROBE3(pf, ioctl, ioctl, error, cmd, error, __LINE__); \ 2401 goto target; \ 2402 } while (0) 2403 2404 2405 /* XXX keep in sync with switch() below */ 2406 if (securelevel_gt(td->td_ucred, 2)) 2407 switch (cmd) { 2408 case DIOCGETRULES: 2409 case DIOCGETRULE: 2410 case DIOCGETRULENV: 2411 case DIOCGETADDRS: 2412 case DIOCGETADDR: 2413 case DIOCGETSTATE: 2414 case DIOCGETSTATENV: 2415 case DIOCSETSTATUSIF: 2416 case DIOCGETSTATUS: 2417 case DIOCGETSTATUSNV: 2418 case DIOCCLRSTATUS: 2419 case DIOCNATLOOK: 2420 case DIOCSETDEBUG: 2421 case DIOCGETSTATES: 2422 case DIOCGETSTATESV2: 2423 case DIOCGETTIMEOUT: 2424 case DIOCCLRRULECTRS: 2425 case DIOCGETLIMIT: 2426 case DIOCGETALTQSV0: 2427 case DIOCGETALTQSV1: 2428 case DIOCGETALTQV0: 2429 case DIOCGETALTQV1: 2430 case DIOCGETQSTATSV0: 2431 case DIOCGETQSTATSV1: 2432 case DIOCGETRULESETS: 2433 case DIOCGETRULESET: 2434 case DIOCRGETTABLES: 2435 case DIOCRGETTSTATS: 2436 case DIOCRCLRTSTATS: 2437 case DIOCRCLRADDRS: 2438 case DIOCRADDADDRS: 2439 case DIOCRDELADDRS: 2440 case DIOCRSETADDRS: 2441 case DIOCRGETADDRS: 2442 case DIOCRGETASTATS: 2443 case DIOCRCLRASTATS: 2444 case DIOCRTSTADDRS: 2445 case DIOCOSFPGET: 2446 case DIOCGETSRCNODES: 2447 case DIOCCLRSRCNODES: 2448 case DIOCGETSYNCOOKIES: 2449 case DIOCIGETIFACES: 2450 case DIOCGIFSPEEDV0: 2451 case DIOCGIFSPEEDV1: 2452 case DIOCSETIFFLAG: 2453 case DIOCCLRIFFLAG: 2454 case DIOCGETETHRULES: 2455 case DIOCGETETHRULE: 2456 break; 2457 case DIOCRCLRTABLES: 2458 case DIOCRADDTABLES: 2459 case DIOCRDELTABLES: 2460 case DIOCRSETTFLAGS: 2461 if (((struct pfioc_table *)addr)->pfrio_flags & 2462 PFR_FLAG_DUMMY) 2463 break; /* dummy operation ok */ 2464 return (EPERM); 2465 default: 2466 return (EPERM); 2467 } 2468 2469 if (!(flags & FWRITE)) 2470 switch (cmd) { 2471 case DIOCGETRULES: 2472 case DIOCGETADDRS: 2473 case DIOCGETADDR: 2474 case DIOCGETSTATE: 2475 case DIOCGETSTATENV: 2476 case DIOCGETSTATUS: 2477 case DIOCGETSTATUSNV: 2478 case DIOCGETSTATES: 2479 case DIOCGETSTATESV2: 2480 case DIOCGETTIMEOUT: 2481 case DIOCGETLIMIT: 2482 case DIOCGETALTQSV0: 2483 case DIOCGETALTQSV1: 2484 case DIOCGETALTQV0: 2485 case DIOCGETALTQV1: 2486 case DIOCGETQSTATSV0: 2487 case DIOCGETQSTATSV1: 2488 case DIOCGETRULESETS: 2489 case DIOCGETRULESET: 2490 case DIOCNATLOOK: 2491 case DIOCRGETTABLES: 2492 case DIOCRGETTSTATS: 2493 case DIOCRGETADDRS: 2494 case DIOCRGETASTATS: 2495 case DIOCRTSTADDRS: 2496 case DIOCOSFPGET: 2497 case DIOCGETSRCNODES: 2498 case DIOCGETSYNCOOKIES: 2499 case DIOCIGETIFACES: 2500 case DIOCGIFSPEEDV1: 2501 case DIOCGIFSPEEDV0: 2502 case DIOCGETRULENV: 2503 case DIOCGETETHRULES: 2504 case DIOCGETETHRULE: 2505 break; 2506 case DIOCRCLRTABLES: 2507 case DIOCRADDTABLES: 2508 case DIOCRDELTABLES: 2509 case DIOCRCLRTSTATS: 2510 case DIOCRCLRADDRS: 2511 case DIOCRADDADDRS: 2512 case DIOCRDELADDRS: 2513 case DIOCRSETADDRS: 2514 case DIOCRSETTFLAGS: 2515 if (((struct pfioc_table *)addr)->pfrio_flags & 2516 PFR_FLAG_DUMMY) { 2517 flags |= FWRITE; /* need write lock for dummy */ 2518 break; /* dummy operation ok */ 2519 } 2520 return (EACCES); 2521 case DIOCGETRULE: 2522 if (((struct pfioc_rule *)addr)->action == 2523 PF_GET_CLR_CNTR) 2524 return (EACCES); 2525 break; 2526 default: 2527 return (EACCES); 2528 } 2529 2530 CURVNET_SET(TD_TO_VNET(td)); 2531 2532 switch (cmd) { 2533 case DIOCSTART: 2534 sx_xlock(&pf_ioctl_lock); 2535 if (V_pf_status.running) 2536 error = EEXIST; 2537 else { 2538 int cpu; 2539 2540 hook_pf(); 2541 if (! TAILQ_EMPTY(V_pf_keth->active.rules)) 2542 hook_pf_eth(); 2543 V_pf_status.running = 1; 2544 V_pf_status.since = time_second; 2545 2546 CPU_FOREACH(cpu) 2547 V_pf_stateid[cpu] = time_second; 2548 2549 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n")); 2550 } 2551 break; 2552 2553 case DIOCSTOP: 2554 sx_xlock(&pf_ioctl_lock); 2555 if (!V_pf_status.running) 2556 error = ENOENT; 2557 else { 2558 V_pf_status.running = 0; 2559 dehook_pf(); 2560 dehook_pf_eth(); 2561 V_pf_status.since = time_second; 2562 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n")); 2563 } 2564 break; 2565 2566 case DIOCGETETHRULES: { 2567 struct pfioc_nv *nv = (struct pfioc_nv *)addr; 2568 nvlist_t *nvl; 2569 void *packed; 2570 struct pf_keth_rule *tail; 2571 struct pf_keth_ruleset *rs; 2572 u_int32_t ticket, nr; 2573 const char *anchor = ""; 2574 2575 nvl = NULL; 2576 packed = NULL; 2577 2578 #define ERROUT(x) do { error = (x); goto DIOCGETETHRULES_error; } while (0) 2579 2580 if (nv->len > pf_ioctl_maxcount) 2581 ERROUT(ENOMEM); 2582 2583 /* Copy the request in */ 2584 packed = malloc(nv->len, M_NVLIST, M_WAITOK); 2585 if (packed == NULL) 2586 ERROUT(ENOMEM); 2587 2588 error = copyin(nv->data, packed, nv->len); 2589 if (error) 2590 ERROUT(error); 2591 2592 nvl = nvlist_unpack(packed, nv->len, 0); 2593 if (nvl == NULL) 2594 ERROUT(EBADMSG); 2595 2596 if (! nvlist_exists_string(nvl, "anchor")) 2597 ERROUT(EBADMSG); 2598 2599 anchor = nvlist_get_string(nvl, "anchor"); 2600 2601 rs = pf_find_keth_ruleset(anchor); 2602 2603 nvlist_destroy(nvl); 2604 nvl = NULL; 2605 free(packed, M_NVLIST); 2606 packed = NULL; 2607 2608 if (rs == NULL) 2609 ERROUT(ENOENT); 2610 2611 /* Reply */ 2612 nvl = nvlist_create(0); 2613 if (nvl == NULL) 2614 ERROUT(ENOMEM); 2615 2616 PF_RULES_RLOCK(); 2617 2618 ticket = rs->active.ticket; 2619 tail = TAILQ_LAST(rs->active.rules, pf_keth_ruleq); 2620 if (tail) 2621 nr = tail->nr + 1; 2622 else 2623 nr = 0; 2624 2625 PF_RULES_RUNLOCK(); 2626 2627 nvlist_add_number(nvl, "ticket", ticket); 2628 nvlist_add_number(nvl, "nr", nr); 2629 2630 packed = nvlist_pack(nvl, &nv->len); 2631 if (packed == NULL) 2632 ERROUT(ENOMEM); 2633 2634 if (nv->size == 0) 2635 ERROUT(0); 2636 else if (nv->size < nv->len) 2637 ERROUT(ENOSPC); 2638 2639 error = copyout(packed, nv->data, nv->len); 2640 2641 #undef ERROUT 2642 DIOCGETETHRULES_error: 2643 free(packed, M_NVLIST); 2644 nvlist_destroy(nvl); 2645 break; 2646 } 2647 2648 case DIOCGETETHRULE: { 2649 struct epoch_tracker et; 2650 struct pfioc_nv *nv = (struct pfioc_nv *)addr; 2651 nvlist_t *nvl = NULL; 2652 void *nvlpacked = NULL; 2653 struct pf_keth_rule *rule = NULL; 2654 struct pf_keth_ruleset *rs; 2655 u_int32_t ticket, nr; 2656 bool clear = false; 2657 const char *anchor; 2658 2659 #define ERROUT(x) do { error = (x); goto DIOCGETETHRULE_error; } while (0) 2660 2661 nvlpacked = malloc(nv->len, M_TEMP, M_WAITOK); 2662 if (nvlpacked == NULL) 2663 ERROUT(ENOMEM); 2664 2665 error = copyin(nv->data, nvlpacked, nv->len); 2666 if (error) 2667 ERROUT(error); 2668 2669 nvl = nvlist_unpack(nvlpacked, nv->len, 0); 2670 if (! nvlist_exists_number(nvl, "ticket")) 2671 ERROUT(EBADMSG); 2672 ticket = nvlist_get_number(nvl, "ticket"); 2673 if (! nvlist_exists_string(nvl, "anchor")) 2674 ERROUT(EBADMSG); 2675 anchor = nvlist_get_string(nvl, "anchor"); 2676 2677 if (nvlist_exists_bool(nvl, "clear")) 2678 clear = nvlist_get_bool(nvl, "clear"); 2679 2680 if (clear && !(flags & FWRITE)) 2681 ERROUT(EACCES); 2682 2683 if (! nvlist_exists_number(nvl, "nr")) 2684 ERROUT(EBADMSG); 2685 nr = nvlist_get_number(nvl, "nr"); 2686 2687 PF_RULES_RLOCK(); 2688 rs = pf_find_keth_ruleset(anchor); 2689 if (rs == NULL) { 2690 PF_RULES_RUNLOCK(); 2691 ERROUT(ENOENT); 2692 } 2693 if (ticket != rs->active.ticket) { 2694 PF_RULES_RUNLOCK(); 2695 ERROUT(EBUSY); 2696 } 2697 2698 nvlist_destroy(nvl); 2699 nvl = NULL; 2700 free(nvlpacked, M_TEMP); 2701 nvlpacked = NULL; 2702 2703 nvl = nvlist_create(0); 2704 2705 rule = TAILQ_FIRST(rs->active.rules); 2706 while ((rule != NULL) && (rule->nr != nr)) 2707 rule = TAILQ_NEXT(rule, entries); 2708 if (rule == NULL) { 2709 PF_RULES_RUNLOCK(); 2710 ERROUT(ENOENT); 2711 } 2712 /* Make sure rule can't go away. */ 2713 NET_EPOCH_ENTER(et); 2714 PF_RULES_RUNLOCK(); 2715 nvl = pf_keth_rule_to_nveth_rule(rule); 2716 if (pf_keth_anchor_nvcopyout(rs, rule, nvl)) 2717 ERROUT(EBUSY); 2718 NET_EPOCH_EXIT(et); 2719 if (nvl == NULL) 2720 ERROUT(ENOMEM); 2721 2722 nvlpacked = nvlist_pack(nvl, &nv->len); 2723 if (nvlpacked == NULL) 2724 ERROUT(ENOMEM); 2725 2726 if (nv->size == 0) 2727 ERROUT(0); 2728 else if (nv->size < nv->len) 2729 ERROUT(ENOSPC); 2730 2731 error = copyout(nvlpacked, nv->data, nv->len); 2732 if (error == 0 && clear) { 2733 counter_u64_zero(rule->evaluations); 2734 for (int i = 0; i < 2; i++) { 2735 counter_u64_zero(rule->packets[i]); 2736 counter_u64_zero(rule->bytes[i]); 2737 } 2738 } 2739 2740 #undef ERROUT 2741 DIOCGETETHRULE_error: 2742 free(nvlpacked, M_TEMP); 2743 nvlist_destroy(nvl); 2744 break; 2745 } 2746 2747 case DIOCADDETHRULE: { 2748 struct pfioc_nv *nv = (struct pfioc_nv *)addr; 2749 nvlist_t *nvl = NULL; 2750 void *nvlpacked = NULL; 2751 struct pf_keth_rule *rule = NULL, *tail = NULL; 2752 struct pf_keth_ruleset *ruleset = NULL; 2753 struct pfi_kkif *kif = NULL; 2754 const char *anchor = "", *anchor_call = ""; 2755 2756 #define ERROUT(x) do { error = (x); goto DIOCADDETHRULE_error; } while (0) 2757 2758 nvlpacked = malloc(nv->len, M_TEMP, M_WAITOK); 2759 if (nvlpacked == NULL) 2760 ERROUT(ENOMEM); 2761 2762 error = copyin(nv->data, nvlpacked, nv->len); 2763 if (error) 2764 ERROUT(error); 2765 2766 nvl = nvlist_unpack(nvlpacked, nv->len, 0); 2767 if (nvl == NULL) 2768 ERROUT(EBADMSG); 2769 2770 if (! nvlist_exists_number(nvl, "ticket")) 2771 ERROUT(EBADMSG); 2772 2773 if (nvlist_exists_string(nvl, "anchor")) 2774 anchor = nvlist_get_string(nvl, "anchor"); 2775 if (nvlist_exists_string(nvl, "anchor_call")) 2776 anchor_call = nvlist_get_string(nvl, "anchor_call"); 2777 2778 ruleset = pf_find_keth_ruleset(anchor); 2779 if (ruleset == NULL) 2780 ERROUT(EINVAL); 2781 2782 if (nvlist_get_number(nvl, "ticket") != 2783 ruleset->inactive.ticket) { 2784 DPFPRINTF(PF_DEBUG_MISC, 2785 ("ticket: %d != %d\n", 2786 (u_int32_t)nvlist_get_number(nvl, "ticket"), 2787 ruleset->inactive.ticket)); 2788 ERROUT(EBUSY); 2789 } 2790 2791 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK); 2792 if (rule == NULL) 2793 ERROUT(ENOMEM); 2794 2795 error = pf_nveth_rule_to_keth_rule(nvl, rule); 2796 if (error != 0) 2797 ERROUT(error); 2798 2799 if (rule->ifname[0]) 2800 kif = pf_kkif_create(M_WAITOK); 2801 rule->evaluations = counter_u64_alloc(M_WAITOK); 2802 for (int i = 0; i < 2; i++) { 2803 rule->packets[i] = counter_u64_alloc(M_WAITOK); 2804 rule->bytes[i] = counter_u64_alloc(M_WAITOK); 2805 } 2806 2807 PF_RULES_WLOCK(); 2808 2809 if (rule->ifname[0]) { 2810 rule->kif = pfi_kkif_attach(kif, rule->ifname); 2811 pfi_kkif_ref(rule->kif); 2812 } else 2813 rule->kif = NULL; 2814 2815 #ifdef ALTQ 2816 /* set queue IDs */ 2817 if (rule->qname[0] != 0) { 2818 if ((rule->qid = pf_qname2qid(rule->qname)) == 0) 2819 error = EBUSY; 2820 else 2821 rule->qid = rule->qid; 2822 } 2823 #endif 2824 if (rule->tagname[0]) 2825 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0) 2826 error = EBUSY; 2827 2828 if (error) { 2829 pf_free_eth_rule(rule); 2830 PF_RULES_WUNLOCK(); 2831 ERROUT(error); 2832 } 2833 2834 if (pf_keth_anchor_setup(rule, ruleset, anchor_call)) { 2835 pf_free_eth_rule(rule); 2836 PF_RULES_WUNLOCK(); 2837 ERROUT(EINVAL); 2838 } 2839 2840 tail = TAILQ_LAST(ruleset->inactive.rules, pf_keth_ruleq); 2841 if (tail) 2842 rule->nr = tail->nr + 1; 2843 else 2844 rule->nr = 0; 2845 2846 TAILQ_INSERT_TAIL(ruleset->inactive.rules, rule, entries); 2847 2848 PF_RULES_WUNLOCK(); 2849 2850 #undef ERROUT 2851 DIOCADDETHRULE_error: 2852 nvlist_destroy(nvl); 2853 free(nvlpacked, M_TEMP); 2854 break; 2855 } 2856 2857 case DIOCADDRULENV: { 2858 struct pfioc_nv *nv = (struct pfioc_nv *)addr; 2859 nvlist_t *nvl = NULL; 2860 void *nvlpacked = NULL; 2861 struct pf_krule *rule = NULL; 2862 const char *anchor = "", *anchor_call = ""; 2863 uint32_t ticket = 0, pool_ticket = 0; 2864 2865 #define ERROUT(x) ERROUT_IOCTL(DIOCADDRULENV_error, x) 2866 2867 if (nv->len > pf_ioctl_maxcount) 2868 ERROUT(ENOMEM); 2869 2870 nvlpacked = malloc(nv->len, M_TEMP, M_WAITOK); 2871 error = copyin(nv->data, nvlpacked, nv->len); 2872 if (error) 2873 ERROUT(error); 2874 2875 nvl = nvlist_unpack(nvlpacked, nv->len, 0); 2876 if (nvl == NULL) 2877 ERROUT(EBADMSG); 2878 2879 if (! nvlist_exists_number(nvl, "ticket")) 2880 ERROUT(EINVAL); 2881 ticket = nvlist_get_number(nvl, "ticket"); 2882 2883 if (! nvlist_exists_number(nvl, "pool_ticket")) 2884 ERROUT(EINVAL); 2885 pool_ticket = nvlist_get_number(nvl, "pool_ticket"); 2886 2887 if (! nvlist_exists_nvlist(nvl, "rule")) 2888 ERROUT(EINVAL); 2889 2890 rule = pf_krule_alloc(); 2891 error = pf_nvrule_to_krule(nvlist_get_nvlist(nvl, "rule"), 2892 rule); 2893 if (error) 2894 ERROUT(error); 2895 2896 if (nvlist_exists_string(nvl, "anchor")) 2897 anchor = nvlist_get_string(nvl, "anchor"); 2898 if (nvlist_exists_string(nvl, "anchor_call")) 2899 anchor_call = nvlist_get_string(nvl, "anchor_call"); 2900 2901 if ((error = nvlist_error(nvl))) 2902 ERROUT(error); 2903 2904 /* Frees rule on error */ 2905 error = pf_ioctl_addrule(rule, ticket, pool_ticket, anchor, 2906 anchor_call, td); 2907 2908 nvlist_destroy(nvl); 2909 free(nvlpacked, M_TEMP); 2910 break; 2911 #undef ERROUT 2912 DIOCADDRULENV_error: 2913 pf_krule_free(rule); 2914 nvlist_destroy(nvl); 2915 free(nvlpacked, M_TEMP); 2916 2917 break; 2918 } 2919 case DIOCADDRULE: { 2920 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 2921 struct pf_krule *rule; 2922 2923 rule = pf_krule_alloc(); 2924 error = pf_rule_to_krule(&pr->rule, rule); 2925 if (error != 0) { 2926 pf_krule_free(rule); 2927 break; 2928 } 2929 2930 pr->anchor[sizeof(pr->anchor) - 1] = 0; 2931 2932 /* Frees rule on error */ 2933 error = pf_ioctl_addrule(rule, pr->ticket, pr->pool_ticket, 2934 pr->anchor, pr->anchor_call, td); 2935 break; 2936 } 2937 2938 case DIOCGETRULES: { 2939 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 2940 struct pf_kruleset *ruleset; 2941 struct pf_krule *tail; 2942 int rs_num; 2943 2944 pr->anchor[sizeof(pr->anchor) - 1] = 0; 2945 2946 PF_RULES_WLOCK(); 2947 ruleset = pf_find_kruleset(pr->anchor); 2948 if (ruleset == NULL) { 2949 PF_RULES_WUNLOCK(); 2950 error = EINVAL; 2951 break; 2952 } 2953 rs_num = pf_get_ruleset_number(pr->rule.action); 2954 if (rs_num >= PF_RULESET_MAX) { 2955 PF_RULES_WUNLOCK(); 2956 error = EINVAL; 2957 break; 2958 } 2959 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr, 2960 pf_krulequeue); 2961 if (tail) 2962 pr->nr = tail->nr + 1; 2963 else 2964 pr->nr = 0; 2965 pr->ticket = ruleset->rules[rs_num].active.ticket; 2966 PF_RULES_WUNLOCK(); 2967 break; 2968 } 2969 2970 case DIOCGETRULE: { 2971 struct pfioc_rule *pr = (struct pfioc_rule *)addr; 2972 struct pf_kruleset *ruleset; 2973 struct pf_krule *rule; 2974 int rs_num; 2975 2976 pr->anchor[sizeof(pr->anchor) - 1] = 0; 2977 2978 PF_RULES_WLOCK(); 2979 ruleset = pf_find_kruleset(pr->anchor); 2980 if (ruleset == NULL) { 2981 PF_RULES_WUNLOCK(); 2982 error = EINVAL; 2983 break; 2984 } 2985 rs_num = pf_get_ruleset_number(pr->rule.action); 2986 if (rs_num >= PF_RULESET_MAX) { 2987 PF_RULES_WUNLOCK(); 2988 error = EINVAL; 2989 break; 2990 } 2991 if (pr->ticket != ruleset->rules[rs_num].active.ticket) { 2992 PF_RULES_WUNLOCK(); 2993 error = EBUSY; 2994 break; 2995 } 2996 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr); 2997 while ((rule != NULL) && (rule->nr != pr->nr)) 2998 rule = TAILQ_NEXT(rule, entries); 2999 if (rule == NULL) { 3000 PF_RULES_WUNLOCK(); 3001 error = EBUSY; 3002 break; 3003 } 3004 3005 pf_krule_to_rule(rule, &pr->rule); 3006 3007 if (pf_kanchor_copyout(ruleset, rule, pr)) { 3008 PF_RULES_WUNLOCK(); 3009 error = EBUSY; 3010 break; 3011 } 3012 pf_addr_copyout(&pr->rule.src.addr); 3013 pf_addr_copyout(&pr->rule.dst.addr); 3014 3015 if (pr->action == PF_GET_CLR_CNTR) { 3016 pf_counter_u64_zero(&rule->evaluations); 3017 for (int i = 0; i < 2; i++) { 3018 pf_counter_u64_zero(&rule->packets[i]); 3019 pf_counter_u64_zero(&rule->bytes[i]); 3020 } 3021 counter_u64_zero(rule->states_tot); 3022 } 3023 PF_RULES_WUNLOCK(); 3024 break; 3025 } 3026 3027 case DIOCGETRULENV: { 3028 struct pfioc_nv *nv = (struct pfioc_nv *)addr; 3029 nvlist_t *nvrule = NULL; 3030 nvlist_t *nvl = NULL; 3031 struct pf_kruleset *ruleset; 3032 struct pf_krule *rule; 3033 void *nvlpacked = NULL; 3034 int rs_num, nr; 3035 bool clear_counter = false; 3036 3037 #define ERROUT(x) ERROUT_IOCTL(DIOCGETRULENV_error, x) 3038 3039 if (nv->len > pf_ioctl_maxcount) 3040 ERROUT(ENOMEM); 3041 3042 /* Copy the request in */ 3043 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK); 3044 if (nvlpacked == NULL) 3045 ERROUT(ENOMEM); 3046 3047 error = copyin(nv->data, nvlpacked, nv->len); 3048 if (error) 3049 ERROUT(error); 3050 3051 nvl = nvlist_unpack(nvlpacked, nv->len, 0); 3052 if (nvl == NULL) 3053 ERROUT(EBADMSG); 3054 3055 if (! nvlist_exists_string(nvl, "anchor")) 3056 ERROUT(EBADMSG); 3057 if (! nvlist_exists_number(nvl, "ruleset")) 3058 ERROUT(EBADMSG); 3059 if (! nvlist_exists_number(nvl, "ticket")) 3060 ERROUT(EBADMSG); 3061 if (! nvlist_exists_number(nvl, "nr")) 3062 ERROUT(EBADMSG); 3063 3064 if (nvlist_exists_bool(nvl, "clear_counter")) 3065 clear_counter = nvlist_get_bool(nvl, "clear_counter"); 3066 3067 if (clear_counter && !(flags & FWRITE)) 3068 ERROUT(EACCES); 3069 3070 nr = nvlist_get_number(nvl, "nr"); 3071 3072 PF_RULES_WLOCK(); 3073 ruleset = pf_find_kruleset(nvlist_get_string(nvl, "anchor")); 3074 if (ruleset == NULL) { 3075 PF_RULES_WUNLOCK(); 3076 ERROUT(ENOENT); 3077 } 3078 3079 rs_num = pf_get_ruleset_number(nvlist_get_number(nvl, "ruleset")); 3080 if (rs_num >= PF_RULESET_MAX) { 3081 PF_RULES_WUNLOCK(); 3082 ERROUT(EINVAL); 3083 } 3084 3085 if (nvlist_get_number(nvl, "ticket") != 3086 ruleset->rules[rs_num].active.ticket) { 3087 PF_RULES_WUNLOCK(); 3088 ERROUT(EBUSY); 3089 } 3090 3091 if ((error = nvlist_error(nvl))) { 3092 PF_RULES_WUNLOCK(); 3093 ERROUT(error); 3094 } 3095 3096 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr); 3097 while ((rule != NULL) && (rule->nr != nr)) 3098 rule = TAILQ_NEXT(rule, entries); 3099 if (rule == NULL) { 3100 PF_RULES_WUNLOCK(); 3101 ERROUT(EBUSY); 3102 } 3103 3104 nvrule = pf_krule_to_nvrule(rule); 3105 3106 nvlist_destroy(nvl); 3107 nvl = nvlist_create(0); 3108 if (nvl == NULL) { 3109 PF_RULES_WUNLOCK(); 3110 ERROUT(ENOMEM); 3111 } 3112 nvlist_add_number(nvl, "nr", nr); 3113 nvlist_add_nvlist(nvl, "rule", nvrule); 3114 nvlist_destroy(nvrule); 3115 nvrule = NULL; 3116 if (pf_kanchor_nvcopyout(ruleset, rule, nvl)) { 3117 PF_RULES_WUNLOCK(); 3118 ERROUT(EBUSY); 3119 } 3120 3121 free(nvlpacked, M_NVLIST); 3122 nvlpacked = nvlist_pack(nvl, &nv->len); 3123 if (nvlpacked == NULL) { 3124 PF_RULES_WUNLOCK(); 3125 ERROUT(ENOMEM); 3126 } 3127 3128 if (nv->size == 0) { 3129 PF_RULES_WUNLOCK(); 3130 ERROUT(0); 3131 } 3132 else if (nv->size < nv->len) { 3133 PF_RULES_WUNLOCK(); 3134 ERROUT(ENOSPC); 3135 } 3136 3137 if (clear_counter) { 3138 pf_counter_u64_zero(&rule->evaluations); 3139 for (int i = 0; i < 2; i++) { 3140 pf_counter_u64_zero(&rule->packets[i]); 3141 pf_counter_u64_zero(&rule->bytes[i]); 3142 } 3143 counter_u64_zero(rule->states_tot); 3144 } 3145 PF_RULES_WUNLOCK(); 3146 3147 error = copyout(nvlpacked, nv->data, nv->len); 3148 3149 #undef ERROUT 3150 DIOCGETRULENV_error: 3151 free(nvlpacked, M_NVLIST); 3152 nvlist_destroy(nvrule); 3153 nvlist_destroy(nvl); 3154 3155 break; 3156 } 3157 3158 case DIOCCHANGERULE: { 3159 struct pfioc_rule *pcr = (struct pfioc_rule *)addr; 3160 struct pf_kruleset *ruleset; 3161 struct pf_krule *oldrule = NULL, *newrule = NULL; 3162 struct pfi_kkif *kif = NULL; 3163 struct pf_kpooladdr *pa; 3164 u_int32_t nr = 0; 3165 int rs_num; 3166 3167 pcr->anchor[sizeof(pcr->anchor) - 1] = 0; 3168 3169 if (pcr->action < PF_CHANGE_ADD_HEAD || 3170 pcr->action > PF_CHANGE_GET_TICKET) { 3171 error = EINVAL; 3172 break; 3173 } 3174 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) { 3175 error = EINVAL; 3176 break; 3177 } 3178 3179 if (pcr->action != PF_CHANGE_REMOVE) { 3180 newrule = pf_krule_alloc(); 3181 error = pf_rule_to_krule(&pcr->rule, newrule); 3182 if (error != 0) { 3183 free(newrule, M_PFRULE); 3184 break; 3185 } 3186 3187 if (newrule->ifname[0]) 3188 kif = pf_kkif_create(M_WAITOK); 3189 pf_counter_u64_init(&newrule->evaluations, M_WAITOK); 3190 for (int i = 0; i < 2; i++) { 3191 pf_counter_u64_init(&newrule->packets[i], M_WAITOK); 3192 pf_counter_u64_init(&newrule->bytes[i], M_WAITOK); 3193 } 3194 newrule->states_cur = counter_u64_alloc(M_WAITOK); 3195 newrule->states_tot = counter_u64_alloc(M_WAITOK); 3196 newrule->src_nodes = counter_u64_alloc(M_WAITOK); 3197 newrule->cuid = td->td_ucred->cr_ruid; 3198 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0; 3199 TAILQ_INIT(&newrule->rpool.list); 3200 } 3201 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; } 3202 3203 PF_RULES_WLOCK(); 3204 #ifdef PF_WANT_32_TO_64_COUNTER 3205 if (newrule != NULL) { 3206 LIST_INSERT_HEAD(&V_pf_allrulelist, newrule, allrulelist); 3207 newrule->allrulelinked = true; 3208 V_pf_allrulecount++; 3209 } 3210 #endif 3211 3212 if (!(pcr->action == PF_CHANGE_REMOVE || 3213 pcr->action == PF_CHANGE_GET_TICKET) && 3214 pcr->pool_ticket != V_ticket_pabuf) 3215 ERROUT(EBUSY); 3216 3217 ruleset = pf_find_kruleset(pcr->anchor); 3218 if (ruleset == NULL) 3219 ERROUT(EINVAL); 3220 3221 rs_num = pf_get_ruleset_number(pcr->rule.action); 3222 if (rs_num >= PF_RULESET_MAX) 3223 ERROUT(EINVAL); 3224 3225 if (pcr->action == PF_CHANGE_GET_TICKET) { 3226 pcr->ticket = ++ruleset->rules[rs_num].active.ticket; 3227 ERROUT(0); 3228 } else if (pcr->ticket != 3229 ruleset->rules[rs_num].active.ticket) 3230 ERROUT(EINVAL); 3231 3232 if (pcr->action != PF_CHANGE_REMOVE) { 3233 if (newrule->ifname[0]) { 3234 newrule->kif = pfi_kkif_attach(kif, 3235 newrule->ifname); 3236 kif = NULL; 3237 pfi_kkif_ref(newrule->kif); 3238 } else 3239 newrule->kif = NULL; 3240 3241 if (newrule->rtableid > 0 && 3242 newrule->rtableid >= rt_numfibs) 3243 error = EBUSY; 3244 3245 #ifdef ALTQ 3246 /* set queue IDs */ 3247 if (newrule->qname[0] != 0) { 3248 if ((newrule->qid = 3249 pf_qname2qid(newrule->qname)) == 0) 3250 error = EBUSY; 3251 else if (newrule->pqname[0] != 0) { 3252 if ((newrule->pqid = 3253 pf_qname2qid(newrule->pqname)) == 0) 3254 error = EBUSY; 3255 } else 3256 newrule->pqid = newrule->qid; 3257 } 3258 #endif /* ALTQ */ 3259 if (newrule->tagname[0]) 3260 if ((newrule->tag = 3261 pf_tagname2tag(newrule->tagname)) == 0) 3262 error = EBUSY; 3263 if (newrule->match_tagname[0]) 3264 if ((newrule->match_tag = pf_tagname2tag( 3265 newrule->match_tagname)) == 0) 3266 error = EBUSY; 3267 if (newrule->rt && !newrule->direction) 3268 error = EINVAL; 3269 if (!newrule->log) 3270 newrule->logif = 0; 3271 if (newrule->logif >= PFLOGIFS_MAX) 3272 error = EINVAL; 3273 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af)) 3274 error = ENOMEM; 3275 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af)) 3276 error = ENOMEM; 3277 if (pf_kanchor_setup(newrule, ruleset, pcr->anchor_call)) 3278 error = EINVAL; 3279 TAILQ_FOREACH(pa, &V_pf_pabuf, entries) 3280 if (pa->addr.type == PF_ADDR_TABLE) { 3281 pa->addr.p.tbl = 3282 pfr_attach_table(ruleset, 3283 pa->addr.v.tblname); 3284 if (pa->addr.p.tbl == NULL) 3285 error = ENOMEM; 3286 } 3287 3288 newrule->overload_tbl = NULL; 3289 if (newrule->overload_tblname[0]) { 3290 if ((newrule->overload_tbl = pfr_attach_table( 3291 ruleset, newrule->overload_tblname)) == 3292 NULL) 3293 error = EINVAL; 3294 else 3295 newrule->overload_tbl->pfrkt_flags |= 3296 PFR_TFLAG_ACTIVE; 3297 } 3298 3299 pf_mv_kpool(&V_pf_pabuf, &newrule->rpool.list); 3300 if (((((newrule->action == PF_NAT) || 3301 (newrule->action == PF_RDR) || 3302 (newrule->action == PF_BINAT) || 3303 (newrule->rt > PF_NOPFROUTE)) && 3304 !newrule->anchor)) && 3305 (TAILQ_FIRST(&newrule->rpool.list) == NULL)) 3306 error = EINVAL; 3307 3308 if (error) { 3309 pf_free_rule(newrule); 3310 PF_RULES_WUNLOCK(); 3311 break; 3312 } 3313 3314 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list); 3315 } 3316 pf_empty_kpool(&V_pf_pabuf); 3317 3318 if (pcr->action == PF_CHANGE_ADD_HEAD) 3319 oldrule = TAILQ_FIRST( 3320 ruleset->rules[rs_num].active.ptr); 3321 else if (pcr->action == PF_CHANGE_ADD_TAIL) 3322 oldrule = TAILQ_LAST( 3323 ruleset->rules[rs_num].active.ptr, pf_krulequeue); 3324 else { 3325 oldrule = TAILQ_FIRST( 3326 ruleset->rules[rs_num].active.ptr); 3327 while ((oldrule != NULL) && (oldrule->nr != pcr->nr)) 3328 oldrule = TAILQ_NEXT(oldrule, entries); 3329 if (oldrule == NULL) { 3330 if (newrule != NULL) 3331 pf_free_rule(newrule); 3332 PF_RULES_WUNLOCK(); 3333 error = EINVAL; 3334 break; 3335 } 3336 } 3337 3338 if (pcr->action == PF_CHANGE_REMOVE) { 3339 pf_unlink_rule(ruleset->rules[rs_num].active.ptr, 3340 oldrule); 3341 ruleset->rules[rs_num].active.rcount--; 3342 } else { 3343 if (oldrule == NULL) 3344 TAILQ_INSERT_TAIL( 3345 ruleset->rules[rs_num].active.ptr, 3346 newrule, entries); 3347 else if (pcr->action == PF_CHANGE_ADD_HEAD || 3348 pcr->action == PF_CHANGE_ADD_BEFORE) 3349 TAILQ_INSERT_BEFORE(oldrule, newrule, entries); 3350 else 3351 TAILQ_INSERT_AFTER( 3352 ruleset->rules[rs_num].active.ptr, 3353 oldrule, newrule, entries); 3354 ruleset->rules[rs_num].active.rcount++; 3355 } 3356 3357 nr = 0; 3358 TAILQ_FOREACH(oldrule, 3359 ruleset->rules[rs_num].active.ptr, entries) 3360 oldrule->nr = nr++; 3361 3362 ruleset->rules[rs_num].active.ticket++; 3363 3364 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr); 3365 pf_remove_if_empty_kruleset(ruleset); 3366 3367 PF_RULES_WUNLOCK(); 3368 break; 3369 3370 #undef ERROUT 3371 DIOCCHANGERULE_error: 3372 PF_RULES_WUNLOCK(); 3373 pf_krule_free(newrule); 3374 pf_kkif_free(kif); 3375 break; 3376 } 3377 3378 case DIOCCLRSTATES: { 3379 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr; 3380 struct pf_kstate_kill kill; 3381 3382 error = pf_state_kill_to_kstate_kill(psk, &kill); 3383 if (error) 3384 break; 3385 3386 psk->psk_killed = pf_clear_states(&kill); 3387 break; 3388 } 3389 3390 case DIOCCLRSTATESNV: { 3391 error = pf_clearstates_nv((struct pfioc_nv *)addr); 3392 break; 3393 } 3394 3395 case DIOCKILLSTATES: { 3396 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr; 3397 struct pf_kstate_kill kill; 3398 3399 error = pf_state_kill_to_kstate_kill(psk, &kill); 3400 if (error) 3401 break; 3402 3403 psk->psk_killed = 0; 3404 pf_killstates(&kill, &psk->psk_killed); 3405 break; 3406 } 3407 3408 case DIOCKILLSTATESNV: { 3409 error = pf_killstates_nv((struct pfioc_nv *)addr); 3410 break; 3411 } 3412 3413 case DIOCADDSTATE: { 3414 struct pfioc_state *ps = (struct pfioc_state *)addr; 3415 struct pfsync_state *sp = &ps->state; 3416 3417 if (sp->timeout >= PFTM_MAX) { 3418 error = EINVAL; 3419 break; 3420 } 3421 if (V_pfsync_state_import_ptr != NULL) { 3422 PF_RULES_RLOCK(); 3423 error = V_pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL); 3424 PF_RULES_RUNLOCK(); 3425 } else 3426 error = EOPNOTSUPP; 3427 break; 3428 } 3429 3430 case DIOCGETSTATE: { 3431 struct pfioc_state *ps = (struct pfioc_state *)addr; 3432 struct pf_kstate *s; 3433 3434 s = pf_find_state_byid(ps->state.id, ps->state.creatorid); 3435 if (s == NULL) { 3436 error = ENOENT; 3437 break; 3438 } 3439 3440 pfsync_state_export(&ps->state, s); 3441 PF_STATE_UNLOCK(s); 3442 break; 3443 } 3444 3445 case DIOCGETSTATENV: { 3446 error = pf_getstate((struct pfioc_nv *)addr); 3447 break; 3448 } 3449 3450 case DIOCGETSTATES: { 3451 struct pfioc_states *ps = (struct pfioc_states *)addr; 3452 struct pf_kstate *s; 3453 struct pfsync_state *pstore, *p; 3454 int i, nr; 3455 size_t slice_count = 16, count; 3456 void *out; 3457 3458 if (ps->ps_len <= 0) { 3459 nr = uma_zone_get_cur(V_pf_state_z); 3460 ps->ps_len = sizeof(struct pfsync_state) * nr; 3461 break; 3462 } 3463 3464 out = ps->ps_states; 3465 pstore = mallocarray(slice_count, 3466 sizeof(struct pfsync_state), M_TEMP, M_WAITOK | M_ZERO); 3467 nr = 0; 3468 3469 for (i = 0; i <= pf_hashmask; i++) { 3470 struct pf_idhash *ih = &V_pf_idhash[i]; 3471 3472 DIOCGETSTATES_retry: 3473 p = pstore; 3474 3475 if (LIST_EMPTY(&ih->states)) 3476 continue; 3477 3478 PF_HASHROW_LOCK(ih); 3479 count = 0; 3480 LIST_FOREACH(s, &ih->states, entry) { 3481 if (s->timeout == PFTM_UNLINKED) 3482 continue; 3483 count++; 3484 } 3485 3486 if (count > slice_count) { 3487 PF_HASHROW_UNLOCK(ih); 3488 free(pstore, M_TEMP); 3489 slice_count = count * 2; 3490 pstore = mallocarray(slice_count, 3491 sizeof(struct pfsync_state), M_TEMP, 3492 M_WAITOK | M_ZERO); 3493 goto DIOCGETSTATES_retry; 3494 } 3495 3496 if ((nr+count) * sizeof(*p) > ps->ps_len) { 3497 PF_HASHROW_UNLOCK(ih); 3498 goto DIOCGETSTATES_full; 3499 } 3500 3501 LIST_FOREACH(s, &ih->states, entry) { 3502 if (s->timeout == PFTM_UNLINKED) 3503 continue; 3504 3505 pfsync_state_export(p, s); 3506 p++; 3507 nr++; 3508 } 3509 PF_HASHROW_UNLOCK(ih); 3510 error = copyout(pstore, out, 3511 sizeof(struct pfsync_state) * count); 3512 if (error) 3513 break; 3514 out = ps->ps_states + nr; 3515 } 3516 DIOCGETSTATES_full: 3517 ps->ps_len = sizeof(struct pfsync_state) * nr; 3518 free(pstore, M_TEMP); 3519 3520 break; 3521 } 3522 3523 case DIOCGETSTATESV2: { 3524 struct pfioc_states_v2 *ps = (struct pfioc_states_v2 *)addr; 3525 struct pf_kstate *s; 3526 struct pf_state_export *pstore, *p; 3527 int i, nr; 3528 size_t slice_count = 16, count; 3529 void *out; 3530 3531 if (ps->ps_req_version > PF_STATE_VERSION) { 3532 error = ENOTSUP; 3533 break; 3534 } 3535 3536 if (ps->ps_len <= 0) { 3537 nr = uma_zone_get_cur(V_pf_state_z); 3538 ps->ps_len = sizeof(struct pf_state_export) * nr; 3539 break; 3540 } 3541 3542 out = ps->ps_states; 3543 pstore = mallocarray(slice_count, 3544 sizeof(struct pf_state_export), M_TEMP, M_WAITOK | M_ZERO); 3545 nr = 0; 3546 3547 for (i = 0; i <= pf_hashmask; i++) { 3548 struct pf_idhash *ih = &V_pf_idhash[i]; 3549 3550 DIOCGETSTATESV2_retry: 3551 p = pstore; 3552 3553 if (LIST_EMPTY(&ih->states)) 3554 continue; 3555 3556 PF_HASHROW_LOCK(ih); 3557 count = 0; 3558 LIST_FOREACH(s, &ih->states, entry) { 3559 if (s->timeout == PFTM_UNLINKED) 3560 continue; 3561 count++; 3562 } 3563 3564 if (count > slice_count) { 3565 PF_HASHROW_UNLOCK(ih); 3566 free(pstore, M_TEMP); 3567 slice_count = count * 2; 3568 pstore = mallocarray(slice_count, 3569 sizeof(struct pf_state_export), M_TEMP, 3570 M_WAITOK | M_ZERO); 3571 goto DIOCGETSTATESV2_retry; 3572 } 3573 3574 if ((nr+count) * sizeof(*p) > ps->ps_len) { 3575 PF_HASHROW_UNLOCK(ih); 3576 goto DIOCGETSTATESV2_full; 3577 } 3578 3579 LIST_FOREACH(s, &ih->states, entry) { 3580 if (s->timeout == PFTM_UNLINKED) 3581 continue; 3582 3583 pf_state_export(p, s); 3584 p++; 3585 nr++; 3586 } 3587 PF_HASHROW_UNLOCK(ih); 3588 error = copyout(pstore, out, 3589 sizeof(struct pf_state_export) * count); 3590 if (error) 3591 break; 3592 out = ps->ps_states + nr; 3593 } 3594 DIOCGETSTATESV2_full: 3595 ps->ps_len = nr * sizeof(struct pf_state_export); 3596 free(pstore, M_TEMP); 3597 3598 break; 3599 } 3600 3601 case DIOCGETSTATUS: { 3602 struct pf_status *s = (struct pf_status *)addr; 3603 3604 PF_RULES_RLOCK(); 3605 s->running = V_pf_status.running; 3606 s->since = V_pf_status.since; 3607 s->debug = V_pf_status.debug; 3608 s->hostid = V_pf_status.hostid; 3609 s->states = V_pf_status.states; 3610 s->src_nodes = V_pf_status.src_nodes; 3611 3612 for (int i = 0; i < PFRES_MAX; i++) 3613 s->counters[i] = 3614 counter_u64_fetch(V_pf_status.counters[i]); 3615 for (int i = 0; i < LCNT_MAX; i++) 3616 s->lcounters[i] = 3617 counter_u64_fetch(V_pf_status.lcounters[i]); 3618 for (int i = 0; i < FCNT_MAX; i++) 3619 s->fcounters[i] = 3620 pf_counter_u64_fetch(&V_pf_status.fcounters[i]); 3621 for (int i = 0; i < SCNT_MAX; i++) 3622 s->scounters[i] = 3623 counter_u64_fetch(V_pf_status.scounters[i]); 3624 3625 bcopy(V_pf_status.ifname, s->ifname, IFNAMSIZ); 3626 bcopy(V_pf_status.pf_chksum, s->pf_chksum, 3627 PF_MD5_DIGEST_LENGTH); 3628 3629 pfi_update_status(s->ifname, s); 3630 PF_RULES_RUNLOCK(); 3631 break; 3632 } 3633 3634 case DIOCGETSTATUSNV: { 3635 error = pf_getstatus((struct pfioc_nv *)addr); 3636 break; 3637 } 3638 3639 case DIOCSETSTATUSIF: { 3640 struct pfioc_if *pi = (struct pfioc_if *)addr; 3641 3642 if (pi->ifname[0] == 0) { 3643 bzero(V_pf_status.ifname, IFNAMSIZ); 3644 break; 3645 } 3646 PF_RULES_WLOCK(); 3647 error = pf_user_strcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ); 3648 PF_RULES_WUNLOCK(); 3649 break; 3650 } 3651 3652 case DIOCCLRSTATUS: { 3653 PF_RULES_WLOCK(); 3654 for (int i = 0; i < PFRES_MAX; i++) 3655 counter_u64_zero(V_pf_status.counters[i]); 3656 for (int i = 0; i < FCNT_MAX; i++) 3657 pf_counter_u64_zero(&V_pf_status.fcounters[i]); 3658 for (int i = 0; i < SCNT_MAX; i++) 3659 counter_u64_zero(V_pf_status.scounters[i]); 3660 for (int i = 0; i < KLCNT_MAX; i++) 3661 counter_u64_zero(V_pf_status.lcounters[i]); 3662 V_pf_status.since = time_second; 3663 if (*V_pf_status.ifname) 3664 pfi_update_status(V_pf_status.ifname, NULL); 3665 PF_RULES_WUNLOCK(); 3666 break; 3667 } 3668 3669 case DIOCNATLOOK: { 3670 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr; 3671 struct pf_state_key *sk; 3672 struct pf_kstate *state; 3673 struct pf_state_key_cmp key; 3674 int m = 0, direction = pnl->direction; 3675 int sidx, didx; 3676 3677 /* NATLOOK src and dst are reversed, so reverse sidx/didx */ 3678 sidx = (direction == PF_IN) ? 1 : 0; 3679 didx = (direction == PF_IN) ? 0 : 1; 3680 3681 if (!pnl->proto || 3682 PF_AZERO(&pnl->saddr, pnl->af) || 3683 PF_AZERO(&pnl->daddr, pnl->af) || 3684 ((pnl->proto == IPPROTO_TCP || 3685 pnl->proto == IPPROTO_UDP) && 3686 (!pnl->dport || !pnl->sport))) 3687 error = EINVAL; 3688 else { 3689 bzero(&key, sizeof(key)); 3690 key.af = pnl->af; 3691 key.proto = pnl->proto; 3692 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af); 3693 key.port[sidx] = pnl->sport; 3694 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af); 3695 key.port[didx] = pnl->dport; 3696 3697 state = pf_find_state_all(&key, direction, &m); 3698 if (state == NULL) { 3699 error = ENOENT; 3700 } else { 3701 if (m > 1) { 3702 PF_STATE_UNLOCK(state); 3703 error = E2BIG; /* more than one state */ 3704 } else { 3705 sk = state->key[sidx]; 3706 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af); 3707 pnl->rsport = sk->port[sidx]; 3708 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af); 3709 pnl->rdport = sk->port[didx]; 3710 PF_STATE_UNLOCK(state); 3711 } 3712 } 3713 } 3714 break; 3715 } 3716 3717 case DIOCSETTIMEOUT: { 3718 struct pfioc_tm *pt = (struct pfioc_tm *)addr; 3719 int old; 3720 3721 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX || 3722 pt->seconds < 0) { 3723 error = EINVAL; 3724 break; 3725 } 3726 PF_RULES_WLOCK(); 3727 old = V_pf_default_rule.timeout[pt->timeout]; 3728 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0) 3729 pt->seconds = 1; 3730 V_pf_default_rule.timeout[pt->timeout] = pt->seconds; 3731 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old) 3732 wakeup(pf_purge_thread); 3733 pt->seconds = old; 3734 PF_RULES_WUNLOCK(); 3735 break; 3736 } 3737 3738 case DIOCGETTIMEOUT: { 3739 struct pfioc_tm *pt = (struct pfioc_tm *)addr; 3740 3741 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) { 3742 error = EINVAL; 3743 break; 3744 } 3745 PF_RULES_RLOCK(); 3746 pt->seconds = V_pf_default_rule.timeout[pt->timeout]; 3747 PF_RULES_RUNLOCK(); 3748 break; 3749 } 3750 3751 case DIOCGETLIMIT: { 3752 struct pfioc_limit *pl = (struct pfioc_limit *)addr; 3753 3754 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) { 3755 error = EINVAL; 3756 break; 3757 } 3758 PF_RULES_RLOCK(); 3759 pl->limit = V_pf_limits[pl->index].limit; 3760 PF_RULES_RUNLOCK(); 3761 break; 3762 } 3763 3764 case DIOCSETLIMIT: { 3765 struct pfioc_limit *pl = (struct pfioc_limit *)addr; 3766 int old_limit; 3767 3768 PF_RULES_WLOCK(); 3769 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX || 3770 V_pf_limits[pl->index].zone == NULL) { 3771 PF_RULES_WUNLOCK(); 3772 error = EINVAL; 3773 break; 3774 } 3775 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit); 3776 old_limit = V_pf_limits[pl->index].limit; 3777 V_pf_limits[pl->index].limit = pl->limit; 3778 pl->limit = old_limit; 3779 PF_RULES_WUNLOCK(); 3780 break; 3781 } 3782 3783 case DIOCSETDEBUG: { 3784 u_int32_t *level = (u_int32_t *)addr; 3785 3786 PF_RULES_WLOCK(); 3787 V_pf_status.debug = *level; 3788 PF_RULES_WUNLOCK(); 3789 break; 3790 } 3791 3792 case DIOCCLRRULECTRS: { 3793 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */ 3794 struct pf_kruleset *ruleset = &pf_main_ruleset; 3795 struct pf_krule *rule; 3796 3797 PF_RULES_WLOCK(); 3798 TAILQ_FOREACH(rule, 3799 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) { 3800 pf_counter_u64_zero(&rule->evaluations); 3801 for (int i = 0; i < 2; i++) { 3802 pf_counter_u64_zero(&rule->packets[i]); 3803 pf_counter_u64_zero(&rule->bytes[i]); 3804 } 3805 } 3806 PF_RULES_WUNLOCK(); 3807 break; 3808 } 3809 3810 case DIOCGIFSPEEDV0: 3811 case DIOCGIFSPEEDV1: { 3812 struct pf_ifspeed_v1 *psp = (struct pf_ifspeed_v1 *)addr; 3813 struct pf_ifspeed_v1 ps; 3814 struct ifnet *ifp; 3815 3816 if (psp->ifname[0] == '\0') { 3817 error = EINVAL; 3818 break; 3819 } 3820 3821 error = pf_user_strcpy(ps.ifname, psp->ifname, IFNAMSIZ); 3822 if (error != 0) 3823 break; 3824 ifp = ifunit(ps.ifname); 3825 if (ifp != NULL) { 3826 psp->baudrate32 = 3827 (u_int32_t)uqmin(ifp->if_baudrate, UINT_MAX); 3828 if (cmd == DIOCGIFSPEEDV1) 3829 psp->baudrate = ifp->if_baudrate; 3830 } else { 3831 error = EINVAL; 3832 } 3833 break; 3834 } 3835 3836 #ifdef ALTQ 3837 case DIOCSTARTALTQ: { 3838 struct pf_altq *altq; 3839 3840 PF_RULES_WLOCK(); 3841 /* enable all altq interfaces on active list */ 3842 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 3843 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 3844 error = pf_enable_altq(altq); 3845 if (error != 0) 3846 break; 3847 } 3848 } 3849 if (error == 0) 3850 V_pf_altq_running = 1; 3851 PF_RULES_WUNLOCK(); 3852 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n")); 3853 break; 3854 } 3855 3856 case DIOCSTOPALTQ: { 3857 struct pf_altq *altq; 3858 3859 PF_RULES_WLOCK(); 3860 /* disable all altq interfaces on active list */ 3861 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) { 3862 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) { 3863 error = pf_disable_altq(altq); 3864 if (error != 0) 3865 break; 3866 } 3867 } 3868 if (error == 0) 3869 V_pf_altq_running = 0; 3870 PF_RULES_WUNLOCK(); 3871 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n")); 3872 break; 3873 } 3874 3875 case DIOCADDALTQV0: 3876 case DIOCADDALTQV1: { 3877 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 3878 struct pf_altq *altq, *a; 3879 struct ifnet *ifp; 3880 3881 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK | M_ZERO); 3882 error = pf_import_kaltq(pa, altq, IOCPARM_LEN(cmd)); 3883 if (error) 3884 break; 3885 altq->local_flags = 0; 3886 3887 PF_RULES_WLOCK(); 3888 if (pa->ticket != V_ticket_altqs_inactive) { 3889 PF_RULES_WUNLOCK(); 3890 free(altq, M_PFALTQ); 3891 error = EBUSY; 3892 break; 3893 } 3894 3895 /* 3896 * if this is for a queue, find the discipline and 3897 * copy the necessary fields 3898 */ 3899 if (altq->qname[0] != 0) { 3900 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) { 3901 PF_RULES_WUNLOCK(); 3902 error = EBUSY; 3903 free(altq, M_PFALTQ); 3904 break; 3905 } 3906 altq->altq_disc = NULL; 3907 TAILQ_FOREACH(a, V_pf_altq_ifs_inactive, entries) { 3908 if (strncmp(a->ifname, altq->ifname, 3909 IFNAMSIZ) == 0) { 3910 altq->altq_disc = a->altq_disc; 3911 break; 3912 } 3913 } 3914 } 3915 3916 if ((ifp = ifunit(altq->ifname)) == NULL) 3917 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED; 3918 else 3919 error = altq_add(ifp, altq); 3920 3921 if (error) { 3922 PF_RULES_WUNLOCK(); 3923 free(altq, M_PFALTQ); 3924 break; 3925 } 3926 3927 if (altq->qname[0] != 0) 3928 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries); 3929 else 3930 TAILQ_INSERT_TAIL(V_pf_altq_ifs_inactive, altq, entries); 3931 /* version error check done on import above */ 3932 pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd)); 3933 PF_RULES_WUNLOCK(); 3934 break; 3935 } 3936 3937 case DIOCGETALTQSV0: 3938 case DIOCGETALTQSV1: { 3939 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 3940 struct pf_altq *altq; 3941 3942 PF_RULES_RLOCK(); 3943 pa->nr = 0; 3944 TAILQ_FOREACH(altq, V_pf_altq_ifs_active, entries) 3945 pa->nr++; 3946 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) 3947 pa->nr++; 3948 pa->ticket = V_ticket_altqs_active; 3949 PF_RULES_RUNLOCK(); 3950 break; 3951 } 3952 3953 case DIOCGETALTQV0: 3954 case DIOCGETALTQV1: { 3955 struct pfioc_altq_v1 *pa = (struct pfioc_altq_v1 *)addr; 3956 struct pf_altq *altq; 3957 3958 PF_RULES_RLOCK(); 3959 if (pa->ticket != V_ticket_altqs_active) { 3960 PF_RULES_RUNLOCK(); 3961 error = EBUSY; 3962 break; 3963 } 3964 altq = pf_altq_get_nth_active(pa->nr); 3965 if (altq == NULL) { 3966 PF_RULES_RUNLOCK(); 3967 error = EBUSY; 3968 break; 3969 } 3970 pf_export_kaltq(altq, pa, IOCPARM_LEN(cmd)); 3971 PF_RULES_RUNLOCK(); 3972 break; 3973 } 3974 3975 case DIOCCHANGEALTQV0: 3976 case DIOCCHANGEALTQV1: 3977 /* CHANGEALTQ not supported yet! */ 3978 error = ENODEV; 3979 break; 3980 3981 case DIOCGETQSTATSV0: 3982 case DIOCGETQSTATSV1: { 3983 struct pfioc_qstats_v1 *pq = (struct pfioc_qstats_v1 *)addr; 3984 struct pf_altq *altq; 3985 int nbytes; 3986 u_int32_t version; 3987 3988 PF_RULES_RLOCK(); 3989 if (pq->ticket != V_ticket_altqs_active) { 3990 PF_RULES_RUNLOCK(); 3991 error = EBUSY; 3992 break; 3993 } 3994 nbytes = pq->nbytes; 3995 altq = pf_altq_get_nth_active(pq->nr); 3996 if (altq == NULL) { 3997 PF_RULES_RUNLOCK(); 3998 error = EBUSY; 3999 break; 4000 } 4001 4002 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) { 4003 PF_RULES_RUNLOCK(); 4004 error = ENXIO; 4005 break; 4006 } 4007 PF_RULES_RUNLOCK(); 4008 if (cmd == DIOCGETQSTATSV0) 4009 version = 0; /* DIOCGETQSTATSV0 means stats struct v0 */ 4010 else 4011 version = pq->version; 4012 error = altq_getqstats(altq, pq->buf, &nbytes, version); 4013 if (error == 0) { 4014 pq->scheduler = altq->scheduler; 4015 pq->nbytes = nbytes; 4016 } 4017 break; 4018 } 4019 #endif /* ALTQ */ 4020 4021 case DIOCBEGINADDRS: { 4022 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 4023 4024 PF_RULES_WLOCK(); 4025 pf_empty_kpool(&V_pf_pabuf); 4026 pp->ticket = ++V_ticket_pabuf; 4027 PF_RULES_WUNLOCK(); 4028 break; 4029 } 4030 4031 case DIOCADDADDR: { 4032 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 4033 struct pf_kpooladdr *pa; 4034 struct pfi_kkif *kif = NULL; 4035 4036 #ifndef INET 4037 if (pp->af == AF_INET) { 4038 error = EAFNOSUPPORT; 4039 break; 4040 } 4041 #endif /* INET */ 4042 #ifndef INET6 4043 if (pp->af == AF_INET6) { 4044 error = EAFNOSUPPORT; 4045 break; 4046 } 4047 #endif /* INET6 */ 4048 if (pp->addr.addr.type != PF_ADDR_ADDRMASK && 4049 pp->addr.addr.type != PF_ADDR_DYNIFTL && 4050 pp->addr.addr.type != PF_ADDR_TABLE) { 4051 error = EINVAL; 4052 break; 4053 } 4054 if (pp->addr.addr.p.dyn != NULL) { 4055 error = EINVAL; 4056 break; 4057 } 4058 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK); 4059 error = pf_pooladdr_to_kpooladdr(&pp->addr, pa); 4060 if (error != 0) 4061 break; 4062 if (pa->ifname[0]) 4063 kif = pf_kkif_create(M_WAITOK); 4064 PF_RULES_WLOCK(); 4065 if (pp->ticket != V_ticket_pabuf) { 4066 PF_RULES_WUNLOCK(); 4067 if (pa->ifname[0]) 4068 pf_kkif_free(kif); 4069 free(pa, M_PFRULE); 4070 error = EBUSY; 4071 break; 4072 } 4073 if (pa->ifname[0]) { 4074 pa->kif = pfi_kkif_attach(kif, pa->ifname); 4075 kif = NULL; 4076 pfi_kkif_ref(pa->kif); 4077 } else 4078 pa->kif = NULL; 4079 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error = 4080 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) { 4081 if (pa->ifname[0]) 4082 pfi_kkif_unref(pa->kif); 4083 PF_RULES_WUNLOCK(); 4084 free(pa, M_PFRULE); 4085 break; 4086 } 4087 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries); 4088 PF_RULES_WUNLOCK(); 4089 break; 4090 } 4091 4092 case DIOCGETADDRS: { 4093 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 4094 struct pf_kpool *pool; 4095 struct pf_kpooladdr *pa; 4096 4097 pp->anchor[sizeof(pp->anchor) - 1] = 0; 4098 pp->nr = 0; 4099 4100 PF_RULES_RLOCK(); 4101 pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action, 4102 pp->r_num, 0, 1, 0); 4103 if (pool == NULL) { 4104 PF_RULES_RUNLOCK(); 4105 error = EBUSY; 4106 break; 4107 } 4108 TAILQ_FOREACH(pa, &pool->list, entries) 4109 pp->nr++; 4110 PF_RULES_RUNLOCK(); 4111 break; 4112 } 4113 4114 case DIOCGETADDR: { 4115 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr; 4116 struct pf_kpool *pool; 4117 struct pf_kpooladdr *pa; 4118 u_int32_t nr = 0; 4119 4120 pp->anchor[sizeof(pp->anchor) - 1] = 0; 4121 4122 PF_RULES_RLOCK(); 4123 pool = pf_get_kpool(pp->anchor, pp->ticket, pp->r_action, 4124 pp->r_num, 0, 1, 1); 4125 if (pool == NULL) { 4126 PF_RULES_RUNLOCK(); 4127 error = EBUSY; 4128 break; 4129 } 4130 pa = TAILQ_FIRST(&pool->list); 4131 while ((pa != NULL) && (nr < pp->nr)) { 4132 pa = TAILQ_NEXT(pa, entries); 4133 nr++; 4134 } 4135 if (pa == NULL) { 4136 PF_RULES_RUNLOCK(); 4137 error = EBUSY; 4138 break; 4139 } 4140 pf_kpooladdr_to_pooladdr(pa, &pp->addr); 4141 pf_addr_copyout(&pp->addr.addr); 4142 PF_RULES_RUNLOCK(); 4143 break; 4144 } 4145 4146 case DIOCCHANGEADDR: { 4147 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr; 4148 struct pf_kpool *pool; 4149 struct pf_kpooladdr *oldpa = NULL, *newpa = NULL; 4150 struct pf_kruleset *ruleset; 4151 struct pfi_kkif *kif = NULL; 4152 4153 pca->anchor[sizeof(pca->anchor) - 1] = 0; 4154 4155 if (pca->action < PF_CHANGE_ADD_HEAD || 4156 pca->action > PF_CHANGE_REMOVE) { 4157 error = EINVAL; 4158 break; 4159 } 4160 if (pca->addr.addr.type != PF_ADDR_ADDRMASK && 4161 pca->addr.addr.type != PF_ADDR_DYNIFTL && 4162 pca->addr.addr.type != PF_ADDR_TABLE) { 4163 error = EINVAL; 4164 break; 4165 } 4166 if (pca->addr.addr.p.dyn != NULL) { 4167 error = EINVAL; 4168 break; 4169 } 4170 4171 if (pca->action != PF_CHANGE_REMOVE) { 4172 #ifndef INET 4173 if (pca->af == AF_INET) { 4174 error = EAFNOSUPPORT; 4175 break; 4176 } 4177 #endif /* INET */ 4178 #ifndef INET6 4179 if (pca->af == AF_INET6) { 4180 error = EAFNOSUPPORT; 4181 break; 4182 } 4183 #endif /* INET6 */ 4184 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK); 4185 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr)); 4186 if (newpa->ifname[0]) 4187 kif = pf_kkif_create(M_WAITOK); 4188 newpa->kif = NULL; 4189 } 4190 #define ERROUT(x) ERROUT_IOCTL(DIOCCHANGEADDR_error, x) 4191 PF_RULES_WLOCK(); 4192 ruleset = pf_find_kruleset(pca->anchor); 4193 if (ruleset == NULL) 4194 ERROUT(EBUSY); 4195 4196 pool = pf_get_kpool(pca->anchor, pca->ticket, pca->r_action, 4197 pca->r_num, pca->r_last, 1, 1); 4198 if (pool == NULL) 4199 ERROUT(EBUSY); 4200 4201 if (pca->action != PF_CHANGE_REMOVE) { 4202 if (newpa->ifname[0]) { 4203 newpa->kif = pfi_kkif_attach(kif, newpa->ifname); 4204 pfi_kkif_ref(newpa->kif); 4205 kif = NULL; 4206 } 4207 4208 switch (newpa->addr.type) { 4209 case PF_ADDR_DYNIFTL: 4210 error = pfi_dynaddr_setup(&newpa->addr, 4211 pca->af); 4212 break; 4213 case PF_ADDR_TABLE: 4214 newpa->addr.p.tbl = pfr_attach_table(ruleset, 4215 newpa->addr.v.tblname); 4216 if (newpa->addr.p.tbl == NULL) 4217 error = ENOMEM; 4218 break; 4219 } 4220 if (error) 4221 goto DIOCCHANGEADDR_error; 4222 } 4223 4224 switch (pca->action) { 4225 case PF_CHANGE_ADD_HEAD: 4226 oldpa = TAILQ_FIRST(&pool->list); 4227 break; 4228 case PF_CHANGE_ADD_TAIL: 4229 oldpa = TAILQ_LAST(&pool->list, pf_kpalist); 4230 break; 4231 default: 4232 oldpa = TAILQ_FIRST(&pool->list); 4233 for (int i = 0; oldpa && i < pca->nr; i++) 4234 oldpa = TAILQ_NEXT(oldpa, entries); 4235 4236 if (oldpa == NULL) 4237 ERROUT(EINVAL); 4238 } 4239 4240 if (pca->action == PF_CHANGE_REMOVE) { 4241 TAILQ_REMOVE(&pool->list, oldpa, entries); 4242 switch (oldpa->addr.type) { 4243 case PF_ADDR_DYNIFTL: 4244 pfi_dynaddr_remove(oldpa->addr.p.dyn); 4245 break; 4246 case PF_ADDR_TABLE: 4247 pfr_detach_table(oldpa->addr.p.tbl); 4248 break; 4249 } 4250 if (oldpa->kif) 4251 pfi_kkif_unref(oldpa->kif); 4252 free(oldpa, M_PFRULE); 4253 } else { 4254 if (oldpa == NULL) 4255 TAILQ_INSERT_TAIL(&pool->list, newpa, entries); 4256 else if (pca->action == PF_CHANGE_ADD_HEAD || 4257 pca->action == PF_CHANGE_ADD_BEFORE) 4258 TAILQ_INSERT_BEFORE(oldpa, newpa, entries); 4259 else 4260 TAILQ_INSERT_AFTER(&pool->list, oldpa, 4261 newpa, entries); 4262 } 4263 4264 pool->cur = TAILQ_FIRST(&pool->list); 4265 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af); 4266 PF_RULES_WUNLOCK(); 4267 break; 4268 4269 #undef ERROUT 4270 DIOCCHANGEADDR_error: 4271 if (newpa != NULL) { 4272 if (newpa->kif) 4273 pfi_kkif_unref(newpa->kif); 4274 free(newpa, M_PFRULE); 4275 } 4276 PF_RULES_WUNLOCK(); 4277 pf_kkif_free(kif); 4278 break; 4279 } 4280 4281 case DIOCGETRULESETS: { 4282 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr; 4283 struct pf_kruleset *ruleset; 4284 struct pf_kanchor *anchor; 4285 4286 pr->path[sizeof(pr->path) - 1] = 0; 4287 4288 PF_RULES_RLOCK(); 4289 if ((ruleset = pf_find_kruleset(pr->path)) == NULL) { 4290 PF_RULES_RUNLOCK(); 4291 error = ENOENT; 4292 break; 4293 } 4294 pr->nr = 0; 4295 if (ruleset->anchor == NULL) { 4296 /* XXX kludge for pf_main_ruleset */ 4297 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) 4298 if (anchor->parent == NULL) 4299 pr->nr++; 4300 } else { 4301 RB_FOREACH(anchor, pf_kanchor_node, 4302 &ruleset->anchor->children) 4303 pr->nr++; 4304 } 4305 PF_RULES_RUNLOCK(); 4306 break; 4307 } 4308 4309 case DIOCGETRULESET: { 4310 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr; 4311 struct pf_kruleset *ruleset; 4312 struct pf_kanchor *anchor; 4313 u_int32_t nr = 0; 4314 4315 pr->path[sizeof(pr->path) - 1] = 0; 4316 4317 PF_RULES_RLOCK(); 4318 if ((ruleset = pf_find_kruleset(pr->path)) == NULL) { 4319 PF_RULES_RUNLOCK(); 4320 error = ENOENT; 4321 break; 4322 } 4323 pr->name[0] = 0; 4324 if (ruleset->anchor == NULL) { 4325 /* XXX kludge for pf_main_ruleset */ 4326 RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) 4327 if (anchor->parent == NULL && nr++ == pr->nr) { 4328 strlcpy(pr->name, anchor->name, 4329 sizeof(pr->name)); 4330 break; 4331 } 4332 } else { 4333 RB_FOREACH(anchor, pf_kanchor_node, 4334 &ruleset->anchor->children) 4335 if (nr++ == pr->nr) { 4336 strlcpy(pr->name, anchor->name, 4337 sizeof(pr->name)); 4338 break; 4339 } 4340 } 4341 if (!pr->name[0]) 4342 error = EBUSY; 4343 PF_RULES_RUNLOCK(); 4344 break; 4345 } 4346 4347 case DIOCRCLRTABLES: { 4348 struct pfioc_table *io = (struct pfioc_table *)addr; 4349 4350 if (io->pfrio_esize != 0) { 4351 error = ENODEV; 4352 break; 4353 } 4354 PF_RULES_WLOCK(); 4355 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel, 4356 io->pfrio_flags | PFR_FLAG_USERIOCTL); 4357 PF_RULES_WUNLOCK(); 4358 break; 4359 } 4360 4361 case DIOCRADDTABLES: { 4362 struct pfioc_table *io = (struct pfioc_table *)addr; 4363 struct pfr_table *pfrts; 4364 size_t totlen; 4365 4366 if (io->pfrio_esize != sizeof(struct pfr_table)) { 4367 error = ENODEV; 4368 break; 4369 } 4370 4371 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 4372 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 4373 error = ENOMEM; 4374 break; 4375 } 4376 4377 totlen = io->pfrio_size * sizeof(struct pfr_table); 4378 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 4379 M_TEMP, M_WAITOK); 4380 error = copyin(io->pfrio_buffer, pfrts, totlen); 4381 if (error) { 4382 free(pfrts, M_TEMP); 4383 break; 4384 } 4385 PF_RULES_WLOCK(); 4386 error = pfr_add_tables(pfrts, io->pfrio_size, 4387 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4388 PF_RULES_WUNLOCK(); 4389 free(pfrts, M_TEMP); 4390 break; 4391 } 4392 4393 case DIOCRDELTABLES: { 4394 struct pfioc_table *io = (struct pfioc_table *)addr; 4395 struct pfr_table *pfrts; 4396 size_t totlen; 4397 4398 if (io->pfrio_esize != sizeof(struct pfr_table)) { 4399 error = ENODEV; 4400 break; 4401 } 4402 4403 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 4404 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 4405 error = ENOMEM; 4406 break; 4407 } 4408 4409 totlen = io->pfrio_size * sizeof(struct pfr_table); 4410 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 4411 M_TEMP, M_WAITOK); 4412 error = copyin(io->pfrio_buffer, pfrts, totlen); 4413 if (error) { 4414 free(pfrts, M_TEMP); 4415 break; 4416 } 4417 PF_RULES_WLOCK(); 4418 error = pfr_del_tables(pfrts, io->pfrio_size, 4419 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4420 PF_RULES_WUNLOCK(); 4421 free(pfrts, M_TEMP); 4422 break; 4423 } 4424 4425 case DIOCRGETTABLES: { 4426 struct pfioc_table *io = (struct pfioc_table *)addr; 4427 struct pfr_table *pfrts; 4428 size_t totlen; 4429 int n; 4430 4431 if (io->pfrio_esize != sizeof(struct pfr_table)) { 4432 error = ENODEV; 4433 break; 4434 } 4435 PF_RULES_RLOCK(); 4436 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 4437 if (n < 0) { 4438 PF_RULES_RUNLOCK(); 4439 error = EINVAL; 4440 break; 4441 } 4442 io->pfrio_size = min(io->pfrio_size, n); 4443 4444 totlen = io->pfrio_size * sizeof(struct pfr_table); 4445 4446 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 4447 M_TEMP, M_NOWAIT | M_ZERO); 4448 if (pfrts == NULL) { 4449 error = ENOMEM; 4450 PF_RULES_RUNLOCK(); 4451 break; 4452 } 4453 error = pfr_get_tables(&io->pfrio_table, pfrts, 4454 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4455 PF_RULES_RUNLOCK(); 4456 if (error == 0) 4457 error = copyout(pfrts, io->pfrio_buffer, totlen); 4458 free(pfrts, M_TEMP); 4459 break; 4460 } 4461 4462 case DIOCRGETTSTATS: { 4463 struct pfioc_table *io = (struct pfioc_table *)addr; 4464 struct pfr_tstats *pfrtstats; 4465 size_t totlen; 4466 int n; 4467 4468 if (io->pfrio_esize != sizeof(struct pfr_tstats)) { 4469 error = ENODEV; 4470 break; 4471 } 4472 PF_TABLE_STATS_LOCK(); 4473 PF_RULES_RLOCK(); 4474 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 4475 if (n < 0) { 4476 PF_RULES_RUNLOCK(); 4477 PF_TABLE_STATS_UNLOCK(); 4478 error = EINVAL; 4479 break; 4480 } 4481 io->pfrio_size = min(io->pfrio_size, n); 4482 4483 totlen = io->pfrio_size * sizeof(struct pfr_tstats); 4484 pfrtstats = mallocarray(io->pfrio_size, 4485 sizeof(struct pfr_tstats), M_TEMP, M_NOWAIT | M_ZERO); 4486 if (pfrtstats == NULL) { 4487 error = ENOMEM; 4488 PF_RULES_RUNLOCK(); 4489 PF_TABLE_STATS_UNLOCK(); 4490 break; 4491 } 4492 error = pfr_get_tstats(&io->pfrio_table, pfrtstats, 4493 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4494 PF_RULES_RUNLOCK(); 4495 PF_TABLE_STATS_UNLOCK(); 4496 if (error == 0) 4497 error = copyout(pfrtstats, io->pfrio_buffer, totlen); 4498 free(pfrtstats, M_TEMP); 4499 break; 4500 } 4501 4502 case DIOCRCLRTSTATS: { 4503 struct pfioc_table *io = (struct pfioc_table *)addr; 4504 struct pfr_table *pfrts; 4505 size_t totlen; 4506 4507 if (io->pfrio_esize != sizeof(struct pfr_table)) { 4508 error = ENODEV; 4509 break; 4510 } 4511 4512 if (io->pfrio_size < 0 || io->pfrio_size > pf_ioctl_maxcount || 4513 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_table))) { 4514 /* We used to count tables and use the minimum required 4515 * size, so we didn't fail on overly large requests. 4516 * Keep doing so. */ 4517 io->pfrio_size = pf_ioctl_maxcount; 4518 break; 4519 } 4520 4521 totlen = io->pfrio_size * sizeof(struct pfr_table); 4522 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 4523 M_TEMP, M_WAITOK); 4524 error = copyin(io->pfrio_buffer, pfrts, totlen); 4525 if (error) { 4526 free(pfrts, M_TEMP); 4527 break; 4528 } 4529 4530 PF_TABLE_STATS_LOCK(); 4531 PF_RULES_RLOCK(); 4532 error = pfr_clr_tstats(pfrts, io->pfrio_size, 4533 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4534 PF_RULES_RUNLOCK(); 4535 PF_TABLE_STATS_UNLOCK(); 4536 free(pfrts, M_TEMP); 4537 break; 4538 } 4539 4540 case DIOCRSETTFLAGS: { 4541 struct pfioc_table *io = (struct pfioc_table *)addr; 4542 struct pfr_table *pfrts; 4543 size_t totlen; 4544 int n; 4545 4546 if (io->pfrio_esize != sizeof(struct pfr_table)) { 4547 error = ENODEV; 4548 break; 4549 } 4550 4551 PF_RULES_RLOCK(); 4552 n = pfr_table_count(&io->pfrio_table, io->pfrio_flags); 4553 if (n < 0) { 4554 PF_RULES_RUNLOCK(); 4555 error = EINVAL; 4556 break; 4557 } 4558 4559 io->pfrio_size = min(io->pfrio_size, n); 4560 PF_RULES_RUNLOCK(); 4561 4562 totlen = io->pfrio_size * sizeof(struct pfr_table); 4563 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table), 4564 M_TEMP, M_WAITOK); 4565 error = copyin(io->pfrio_buffer, pfrts, totlen); 4566 if (error) { 4567 free(pfrts, M_TEMP); 4568 break; 4569 } 4570 PF_RULES_WLOCK(); 4571 error = pfr_set_tflags(pfrts, io->pfrio_size, 4572 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange, 4573 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4574 PF_RULES_WUNLOCK(); 4575 free(pfrts, M_TEMP); 4576 break; 4577 } 4578 4579 case DIOCRCLRADDRS: { 4580 struct pfioc_table *io = (struct pfioc_table *)addr; 4581 4582 if (io->pfrio_esize != 0) { 4583 error = ENODEV; 4584 break; 4585 } 4586 PF_RULES_WLOCK(); 4587 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel, 4588 io->pfrio_flags | PFR_FLAG_USERIOCTL); 4589 PF_RULES_WUNLOCK(); 4590 break; 4591 } 4592 4593 case DIOCRADDADDRS: { 4594 struct pfioc_table *io = (struct pfioc_table *)addr; 4595 struct pfr_addr *pfras; 4596 size_t totlen; 4597 4598 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 4599 error = ENODEV; 4600 break; 4601 } 4602 if (io->pfrio_size < 0 || 4603 io->pfrio_size > pf_ioctl_maxcount || 4604 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 4605 error = EINVAL; 4606 break; 4607 } 4608 totlen = io->pfrio_size * sizeof(struct pfr_addr); 4609 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 4610 M_TEMP, M_WAITOK); 4611 error = copyin(io->pfrio_buffer, pfras, totlen); 4612 if (error) { 4613 free(pfras, M_TEMP); 4614 break; 4615 } 4616 PF_RULES_WLOCK(); 4617 error = pfr_add_addrs(&io->pfrio_table, pfras, 4618 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags | 4619 PFR_FLAG_USERIOCTL); 4620 PF_RULES_WUNLOCK(); 4621 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 4622 error = copyout(pfras, io->pfrio_buffer, totlen); 4623 free(pfras, M_TEMP); 4624 break; 4625 } 4626 4627 case DIOCRDELADDRS: { 4628 struct pfioc_table *io = (struct pfioc_table *)addr; 4629 struct pfr_addr *pfras; 4630 size_t totlen; 4631 4632 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 4633 error = ENODEV; 4634 break; 4635 } 4636 if (io->pfrio_size < 0 || 4637 io->pfrio_size > pf_ioctl_maxcount || 4638 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 4639 error = EINVAL; 4640 break; 4641 } 4642 totlen = io->pfrio_size * sizeof(struct pfr_addr); 4643 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 4644 M_TEMP, M_WAITOK); 4645 error = copyin(io->pfrio_buffer, pfras, totlen); 4646 if (error) { 4647 free(pfras, M_TEMP); 4648 break; 4649 } 4650 PF_RULES_WLOCK(); 4651 error = pfr_del_addrs(&io->pfrio_table, pfras, 4652 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags | 4653 PFR_FLAG_USERIOCTL); 4654 PF_RULES_WUNLOCK(); 4655 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 4656 error = copyout(pfras, io->pfrio_buffer, totlen); 4657 free(pfras, M_TEMP); 4658 break; 4659 } 4660 4661 case DIOCRSETADDRS: { 4662 struct pfioc_table *io = (struct pfioc_table *)addr; 4663 struct pfr_addr *pfras; 4664 size_t totlen, count; 4665 4666 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 4667 error = ENODEV; 4668 break; 4669 } 4670 if (io->pfrio_size < 0 || io->pfrio_size2 < 0) { 4671 error = EINVAL; 4672 break; 4673 } 4674 count = max(io->pfrio_size, io->pfrio_size2); 4675 if (count > pf_ioctl_maxcount || 4676 WOULD_OVERFLOW(count, sizeof(struct pfr_addr))) { 4677 error = EINVAL; 4678 break; 4679 } 4680 totlen = count * sizeof(struct pfr_addr); 4681 pfras = mallocarray(count, sizeof(struct pfr_addr), M_TEMP, 4682 M_WAITOK); 4683 error = copyin(io->pfrio_buffer, pfras, totlen); 4684 if (error) { 4685 free(pfras, M_TEMP); 4686 break; 4687 } 4688 PF_RULES_WLOCK(); 4689 error = pfr_set_addrs(&io->pfrio_table, pfras, 4690 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd, 4691 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags | 4692 PFR_FLAG_USERIOCTL, 0); 4693 PF_RULES_WUNLOCK(); 4694 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 4695 error = copyout(pfras, io->pfrio_buffer, totlen); 4696 free(pfras, M_TEMP); 4697 break; 4698 } 4699 4700 case DIOCRGETADDRS: { 4701 struct pfioc_table *io = (struct pfioc_table *)addr; 4702 struct pfr_addr *pfras; 4703 size_t totlen; 4704 4705 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 4706 error = ENODEV; 4707 break; 4708 } 4709 if (io->pfrio_size < 0 || 4710 io->pfrio_size > pf_ioctl_maxcount || 4711 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 4712 error = EINVAL; 4713 break; 4714 } 4715 totlen = io->pfrio_size * sizeof(struct pfr_addr); 4716 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 4717 M_TEMP, M_WAITOK | M_ZERO); 4718 PF_RULES_RLOCK(); 4719 error = pfr_get_addrs(&io->pfrio_table, pfras, 4720 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4721 PF_RULES_RUNLOCK(); 4722 if (error == 0) 4723 error = copyout(pfras, io->pfrio_buffer, totlen); 4724 free(pfras, M_TEMP); 4725 break; 4726 } 4727 4728 case DIOCRGETASTATS: { 4729 struct pfioc_table *io = (struct pfioc_table *)addr; 4730 struct pfr_astats *pfrastats; 4731 size_t totlen; 4732 4733 if (io->pfrio_esize != sizeof(struct pfr_astats)) { 4734 error = ENODEV; 4735 break; 4736 } 4737 if (io->pfrio_size < 0 || 4738 io->pfrio_size > pf_ioctl_maxcount || 4739 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_astats))) { 4740 error = EINVAL; 4741 break; 4742 } 4743 totlen = io->pfrio_size * sizeof(struct pfr_astats); 4744 pfrastats = mallocarray(io->pfrio_size, 4745 sizeof(struct pfr_astats), M_TEMP, M_WAITOK | M_ZERO); 4746 PF_RULES_RLOCK(); 4747 error = pfr_get_astats(&io->pfrio_table, pfrastats, 4748 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4749 PF_RULES_RUNLOCK(); 4750 if (error == 0) 4751 error = copyout(pfrastats, io->pfrio_buffer, totlen); 4752 free(pfrastats, M_TEMP); 4753 break; 4754 } 4755 4756 case DIOCRCLRASTATS: { 4757 struct pfioc_table *io = (struct pfioc_table *)addr; 4758 struct pfr_addr *pfras; 4759 size_t totlen; 4760 4761 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 4762 error = ENODEV; 4763 break; 4764 } 4765 if (io->pfrio_size < 0 || 4766 io->pfrio_size > pf_ioctl_maxcount || 4767 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 4768 error = EINVAL; 4769 break; 4770 } 4771 totlen = io->pfrio_size * sizeof(struct pfr_addr); 4772 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 4773 M_TEMP, M_WAITOK); 4774 error = copyin(io->pfrio_buffer, pfras, totlen); 4775 if (error) { 4776 free(pfras, M_TEMP); 4777 break; 4778 } 4779 PF_RULES_WLOCK(); 4780 error = pfr_clr_astats(&io->pfrio_table, pfras, 4781 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags | 4782 PFR_FLAG_USERIOCTL); 4783 PF_RULES_WUNLOCK(); 4784 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK) 4785 error = copyout(pfras, io->pfrio_buffer, totlen); 4786 free(pfras, M_TEMP); 4787 break; 4788 } 4789 4790 case DIOCRTSTADDRS: { 4791 struct pfioc_table *io = (struct pfioc_table *)addr; 4792 struct pfr_addr *pfras; 4793 size_t totlen; 4794 4795 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 4796 error = ENODEV; 4797 break; 4798 } 4799 if (io->pfrio_size < 0 || 4800 io->pfrio_size > pf_ioctl_maxcount || 4801 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 4802 error = EINVAL; 4803 break; 4804 } 4805 totlen = io->pfrio_size * sizeof(struct pfr_addr); 4806 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 4807 M_TEMP, M_WAITOK); 4808 error = copyin(io->pfrio_buffer, pfras, totlen); 4809 if (error) { 4810 free(pfras, M_TEMP); 4811 break; 4812 } 4813 PF_RULES_RLOCK(); 4814 error = pfr_tst_addrs(&io->pfrio_table, pfras, 4815 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags | 4816 PFR_FLAG_USERIOCTL); 4817 PF_RULES_RUNLOCK(); 4818 if (error == 0) 4819 error = copyout(pfras, io->pfrio_buffer, totlen); 4820 free(pfras, M_TEMP); 4821 break; 4822 } 4823 4824 case DIOCRINADEFINE: { 4825 struct pfioc_table *io = (struct pfioc_table *)addr; 4826 struct pfr_addr *pfras; 4827 size_t totlen; 4828 4829 if (io->pfrio_esize != sizeof(struct pfr_addr)) { 4830 error = ENODEV; 4831 break; 4832 } 4833 if (io->pfrio_size < 0 || 4834 io->pfrio_size > pf_ioctl_maxcount || 4835 WOULD_OVERFLOW(io->pfrio_size, sizeof(struct pfr_addr))) { 4836 error = EINVAL; 4837 break; 4838 } 4839 totlen = io->pfrio_size * sizeof(struct pfr_addr); 4840 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr), 4841 M_TEMP, M_WAITOK); 4842 error = copyin(io->pfrio_buffer, pfras, totlen); 4843 if (error) { 4844 free(pfras, M_TEMP); 4845 break; 4846 } 4847 PF_RULES_WLOCK(); 4848 error = pfr_ina_define(&io->pfrio_table, pfras, 4849 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr, 4850 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL); 4851 PF_RULES_WUNLOCK(); 4852 free(pfras, M_TEMP); 4853 break; 4854 } 4855 4856 case DIOCOSFPADD: { 4857 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr; 4858 PF_RULES_WLOCK(); 4859 error = pf_osfp_add(io); 4860 PF_RULES_WUNLOCK(); 4861 break; 4862 } 4863 4864 case DIOCOSFPGET: { 4865 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr; 4866 PF_RULES_RLOCK(); 4867 error = pf_osfp_get(io); 4868 PF_RULES_RUNLOCK(); 4869 break; 4870 } 4871 4872 case DIOCXBEGIN: { 4873 struct pfioc_trans *io = (struct pfioc_trans *)addr; 4874 struct pfioc_trans_e *ioes, *ioe; 4875 size_t totlen; 4876 int i; 4877 4878 if (io->esize != sizeof(*ioe)) { 4879 error = ENODEV; 4880 break; 4881 } 4882 if (io->size < 0 || 4883 io->size > pf_ioctl_maxcount || 4884 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 4885 error = EINVAL; 4886 break; 4887 } 4888 totlen = sizeof(struct pfioc_trans_e) * io->size; 4889 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 4890 M_TEMP, M_WAITOK); 4891 error = copyin(io->array, ioes, totlen); 4892 if (error) { 4893 free(ioes, M_TEMP); 4894 break; 4895 } 4896 PF_RULES_WLOCK(); 4897 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 4898 ioe->anchor[sizeof(ioe->anchor) - 1] = '\0'; 4899 switch (ioe->rs_num) { 4900 case PF_RULESET_ETH: 4901 if ((error = pf_begin_eth(&ioe->ticket, ioe->anchor))) { 4902 PF_RULES_WUNLOCK(); 4903 free(ioes, M_TEMP); 4904 goto fail; 4905 } 4906 break; 4907 #ifdef ALTQ 4908 case PF_RULESET_ALTQ: 4909 if (ioe->anchor[0]) { 4910 PF_RULES_WUNLOCK(); 4911 free(ioes, M_TEMP); 4912 error = EINVAL; 4913 goto fail; 4914 } 4915 if ((error = pf_begin_altq(&ioe->ticket))) { 4916 PF_RULES_WUNLOCK(); 4917 free(ioes, M_TEMP); 4918 goto fail; 4919 } 4920 break; 4921 #endif /* ALTQ */ 4922 case PF_RULESET_TABLE: 4923 { 4924 struct pfr_table table; 4925 4926 bzero(&table, sizeof(table)); 4927 strlcpy(table.pfrt_anchor, ioe->anchor, 4928 sizeof(table.pfrt_anchor)); 4929 if ((error = pfr_ina_begin(&table, 4930 &ioe->ticket, NULL, 0))) { 4931 PF_RULES_WUNLOCK(); 4932 free(ioes, M_TEMP); 4933 goto fail; 4934 } 4935 break; 4936 } 4937 default: 4938 if ((error = pf_begin_rules(&ioe->ticket, 4939 ioe->rs_num, ioe->anchor))) { 4940 PF_RULES_WUNLOCK(); 4941 free(ioes, M_TEMP); 4942 goto fail; 4943 } 4944 break; 4945 } 4946 } 4947 PF_RULES_WUNLOCK(); 4948 error = copyout(ioes, io->array, totlen); 4949 free(ioes, M_TEMP); 4950 break; 4951 } 4952 4953 case DIOCXROLLBACK: { 4954 struct pfioc_trans *io = (struct pfioc_trans *)addr; 4955 struct pfioc_trans_e *ioe, *ioes; 4956 size_t totlen; 4957 int i; 4958 4959 if (io->esize != sizeof(*ioe)) { 4960 error = ENODEV; 4961 break; 4962 } 4963 if (io->size < 0 || 4964 io->size > pf_ioctl_maxcount || 4965 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 4966 error = EINVAL; 4967 break; 4968 } 4969 totlen = sizeof(struct pfioc_trans_e) * io->size; 4970 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 4971 M_TEMP, M_WAITOK); 4972 error = copyin(io->array, ioes, totlen); 4973 if (error) { 4974 free(ioes, M_TEMP); 4975 break; 4976 } 4977 PF_RULES_WLOCK(); 4978 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 4979 ioe->anchor[sizeof(ioe->anchor) - 1] = '\0'; 4980 switch (ioe->rs_num) { 4981 case PF_RULESET_ETH: 4982 if ((error = pf_rollback_eth(ioe->ticket, 4983 ioe->anchor))) { 4984 PF_RULES_WUNLOCK(); 4985 free(ioes, M_TEMP); 4986 goto fail; /* really bad */ 4987 } 4988 break; 4989 #ifdef ALTQ 4990 case PF_RULESET_ALTQ: 4991 if (ioe->anchor[0]) { 4992 PF_RULES_WUNLOCK(); 4993 free(ioes, M_TEMP); 4994 error = EINVAL; 4995 goto fail; 4996 } 4997 if ((error = pf_rollback_altq(ioe->ticket))) { 4998 PF_RULES_WUNLOCK(); 4999 free(ioes, M_TEMP); 5000 goto fail; /* really bad */ 5001 } 5002 break; 5003 #endif /* ALTQ */ 5004 case PF_RULESET_TABLE: 5005 { 5006 struct pfr_table table; 5007 5008 bzero(&table, sizeof(table)); 5009 strlcpy(table.pfrt_anchor, ioe->anchor, 5010 sizeof(table.pfrt_anchor)); 5011 if ((error = pfr_ina_rollback(&table, 5012 ioe->ticket, NULL, 0))) { 5013 PF_RULES_WUNLOCK(); 5014 free(ioes, M_TEMP); 5015 goto fail; /* really bad */ 5016 } 5017 break; 5018 } 5019 default: 5020 if ((error = pf_rollback_rules(ioe->ticket, 5021 ioe->rs_num, ioe->anchor))) { 5022 PF_RULES_WUNLOCK(); 5023 free(ioes, M_TEMP); 5024 goto fail; /* really bad */ 5025 } 5026 break; 5027 } 5028 } 5029 PF_RULES_WUNLOCK(); 5030 free(ioes, M_TEMP); 5031 break; 5032 } 5033 5034 case DIOCXCOMMIT: { 5035 struct pfioc_trans *io = (struct pfioc_trans *)addr; 5036 struct pfioc_trans_e *ioe, *ioes; 5037 struct pf_kruleset *rs; 5038 struct pf_keth_ruleset *ers; 5039 size_t totlen; 5040 int i; 5041 5042 if (io->esize != sizeof(*ioe)) { 5043 error = ENODEV; 5044 break; 5045 } 5046 5047 if (io->size < 0 || 5048 io->size > pf_ioctl_maxcount || 5049 WOULD_OVERFLOW(io->size, sizeof(struct pfioc_trans_e))) { 5050 error = EINVAL; 5051 break; 5052 } 5053 5054 totlen = sizeof(struct pfioc_trans_e) * io->size; 5055 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e), 5056 M_TEMP, M_WAITOK); 5057 error = copyin(io->array, ioes, totlen); 5058 if (error) { 5059 free(ioes, M_TEMP); 5060 break; 5061 } 5062 PF_RULES_WLOCK(); 5063 /* First makes sure everything will succeed. */ 5064 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 5065 ioe->anchor[sizeof(ioe->anchor) - 1] = 0; 5066 switch (ioe->rs_num) { 5067 case PF_RULESET_ETH: 5068 ers = pf_find_keth_ruleset(ioe->anchor); 5069 if (ers == NULL || ioe->ticket == 0 || 5070 ioe->ticket != ers->inactive.ticket) { 5071 PF_RULES_WUNLOCK(); 5072 free(ioes, M_TEMP); 5073 error = EINVAL; 5074 goto fail; 5075 } 5076 break; 5077 #ifdef ALTQ 5078 case PF_RULESET_ALTQ: 5079 if (ioe->anchor[0]) { 5080 PF_RULES_WUNLOCK(); 5081 free(ioes, M_TEMP); 5082 error = EINVAL; 5083 goto fail; 5084 } 5085 if (!V_altqs_inactive_open || ioe->ticket != 5086 V_ticket_altqs_inactive) { 5087 PF_RULES_WUNLOCK(); 5088 free(ioes, M_TEMP); 5089 error = EBUSY; 5090 goto fail; 5091 } 5092 break; 5093 #endif /* ALTQ */ 5094 case PF_RULESET_TABLE: 5095 rs = pf_find_kruleset(ioe->anchor); 5096 if (rs == NULL || !rs->topen || ioe->ticket != 5097 rs->tticket) { 5098 PF_RULES_WUNLOCK(); 5099 free(ioes, M_TEMP); 5100 error = EBUSY; 5101 goto fail; 5102 } 5103 break; 5104 default: 5105 if (ioe->rs_num < 0 || ioe->rs_num >= 5106 PF_RULESET_MAX) { 5107 PF_RULES_WUNLOCK(); 5108 free(ioes, M_TEMP); 5109 error = EINVAL; 5110 goto fail; 5111 } 5112 rs = pf_find_kruleset(ioe->anchor); 5113 if (rs == NULL || 5114 !rs->rules[ioe->rs_num].inactive.open || 5115 rs->rules[ioe->rs_num].inactive.ticket != 5116 ioe->ticket) { 5117 PF_RULES_WUNLOCK(); 5118 free(ioes, M_TEMP); 5119 error = EBUSY; 5120 goto fail; 5121 } 5122 break; 5123 } 5124 } 5125 /* Now do the commit - no errors should happen here. */ 5126 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) { 5127 switch (ioe->rs_num) { 5128 case PF_RULESET_ETH: 5129 if ((error = pf_commit_eth(ioe->ticket, ioe->anchor))) { 5130 PF_RULES_WUNLOCK(); 5131 free(ioes, M_TEMP); 5132 goto fail; /* really bad */ 5133 } 5134 break; 5135 #ifdef ALTQ 5136 case PF_RULESET_ALTQ: 5137 if ((error = pf_commit_altq(ioe->ticket))) { 5138 PF_RULES_WUNLOCK(); 5139 free(ioes, M_TEMP); 5140 goto fail; /* really bad */ 5141 } 5142 break; 5143 #endif /* ALTQ */ 5144 case PF_RULESET_TABLE: 5145 { 5146 struct pfr_table table; 5147 5148 bzero(&table, sizeof(table)); 5149 (void)strlcpy(table.pfrt_anchor, ioe->anchor, 5150 sizeof(table.pfrt_anchor)); 5151 if ((error = pfr_ina_commit(&table, 5152 ioe->ticket, NULL, NULL, 0))) { 5153 PF_RULES_WUNLOCK(); 5154 free(ioes, M_TEMP); 5155 goto fail; /* really bad */ 5156 } 5157 break; 5158 } 5159 default: 5160 if ((error = pf_commit_rules(ioe->ticket, 5161 ioe->rs_num, ioe->anchor))) { 5162 PF_RULES_WUNLOCK(); 5163 free(ioes, M_TEMP); 5164 goto fail; /* really bad */ 5165 } 5166 break; 5167 } 5168 } 5169 PF_RULES_WUNLOCK(); 5170 5171 /* Only hook into EtherNet taffic if we've got rules for it. */ 5172 if (! TAILQ_EMPTY(V_pf_keth->active.rules)) 5173 hook_pf_eth(); 5174 else 5175 dehook_pf_eth(); 5176 5177 free(ioes, M_TEMP); 5178 break; 5179 } 5180 5181 case DIOCGETSRCNODES: { 5182 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr; 5183 struct pf_srchash *sh; 5184 struct pf_ksrc_node *n; 5185 struct pf_src_node *p, *pstore; 5186 uint32_t i, nr = 0; 5187 5188 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 5189 i++, sh++) { 5190 PF_HASHROW_LOCK(sh); 5191 LIST_FOREACH(n, &sh->nodes, entry) 5192 nr++; 5193 PF_HASHROW_UNLOCK(sh); 5194 } 5195 5196 psn->psn_len = min(psn->psn_len, 5197 sizeof(struct pf_src_node) * nr); 5198 5199 if (psn->psn_len == 0) { 5200 psn->psn_len = sizeof(struct pf_src_node) * nr; 5201 break; 5202 } 5203 5204 nr = 0; 5205 5206 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK | M_ZERO); 5207 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 5208 i++, sh++) { 5209 PF_HASHROW_LOCK(sh); 5210 LIST_FOREACH(n, &sh->nodes, entry) { 5211 5212 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len) 5213 break; 5214 5215 pf_src_node_copy(n, p); 5216 5217 p++; 5218 nr++; 5219 } 5220 PF_HASHROW_UNLOCK(sh); 5221 } 5222 error = copyout(pstore, psn->psn_src_nodes, 5223 sizeof(struct pf_src_node) * nr); 5224 if (error) { 5225 free(pstore, M_TEMP); 5226 break; 5227 } 5228 psn->psn_len = sizeof(struct pf_src_node) * nr; 5229 free(pstore, M_TEMP); 5230 break; 5231 } 5232 5233 case DIOCCLRSRCNODES: { 5234 pf_clear_srcnodes(NULL); 5235 pf_purge_expired_src_nodes(); 5236 break; 5237 } 5238 5239 case DIOCKILLSRCNODES: 5240 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr); 5241 break; 5242 5243 #ifdef COMPAT_FREEBSD13 5244 case DIOCKEEPCOUNTERS_FREEBSD13: 5245 #endif 5246 case DIOCKEEPCOUNTERS: 5247 error = pf_keepcounters((struct pfioc_nv *)addr); 5248 break; 5249 5250 case DIOCGETSYNCOOKIES: 5251 error = pf_get_syncookies((struct pfioc_nv *)addr); 5252 break; 5253 5254 case DIOCSETSYNCOOKIES: 5255 error = pf_set_syncookies((struct pfioc_nv *)addr); 5256 break; 5257 5258 case DIOCSETHOSTID: { 5259 u_int32_t *hostid = (u_int32_t *)addr; 5260 5261 PF_RULES_WLOCK(); 5262 if (*hostid == 0) 5263 V_pf_status.hostid = arc4random(); 5264 else 5265 V_pf_status.hostid = *hostid; 5266 PF_RULES_WUNLOCK(); 5267 break; 5268 } 5269 5270 case DIOCOSFPFLUSH: 5271 PF_RULES_WLOCK(); 5272 pf_osfp_flush(); 5273 PF_RULES_WUNLOCK(); 5274 break; 5275 5276 case DIOCIGETIFACES: { 5277 struct pfioc_iface *io = (struct pfioc_iface *)addr; 5278 struct pfi_kif *ifstore; 5279 size_t bufsiz; 5280 5281 if (io->pfiio_esize != sizeof(struct pfi_kif)) { 5282 error = ENODEV; 5283 break; 5284 } 5285 5286 if (io->pfiio_size < 0 || 5287 io->pfiio_size > pf_ioctl_maxcount || 5288 WOULD_OVERFLOW(io->pfiio_size, sizeof(struct pfi_kif))) { 5289 error = EINVAL; 5290 break; 5291 } 5292 5293 bufsiz = io->pfiio_size * sizeof(struct pfi_kif); 5294 ifstore = mallocarray(io->pfiio_size, sizeof(struct pfi_kif), 5295 M_TEMP, M_WAITOK | M_ZERO); 5296 5297 PF_RULES_RLOCK(); 5298 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size); 5299 PF_RULES_RUNLOCK(); 5300 error = copyout(ifstore, io->pfiio_buffer, bufsiz); 5301 free(ifstore, M_TEMP); 5302 break; 5303 } 5304 5305 case DIOCSETIFFLAG: { 5306 struct pfioc_iface *io = (struct pfioc_iface *)addr; 5307 5308 PF_RULES_WLOCK(); 5309 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags); 5310 PF_RULES_WUNLOCK(); 5311 break; 5312 } 5313 5314 case DIOCCLRIFFLAG: { 5315 struct pfioc_iface *io = (struct pfioc_iface *)addr; 5316 5317 PF_RULES_WLOCK(); 5318 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags); 5319 PF_RULES_WUNLOCK(); 5320 break; 5321 } 5322 5323 default: 5324 error = ENODEV; 5325 break; 5326 } 5327 fail: 5328 if (sx_xlocked(&pf_ioctl_lock)) 5329 sx_xunlock(&pf_ioctl_lock); 5330 CURVNET_RESTORE(); 5331 5332 #undef ERROUT_IOCTL 5333 5334 return (error); 5335 } 5336 5337 void 5338 pfsync_state_export(struct pfsync_state *sp, struct pf_kstate *st) 5339 { 5340 bzero(sp, sizeof(struct pfsync_state)); 5341 5342 /* copy from state key */ 5343 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0]; 5344 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1]; 5345 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0]; 5346 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1]; 5347 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0]; 5348 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1]; 5349 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0]; 5350 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1]; 5351 sp->proto = st->key[PF_SK_WIRE]->proto; 5352 sp->af = st->key[PF_SK_WIRE]->af; 5353 5354 /* copy from state */ 5355 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname)); 5356 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr)); 5357 sp->creation = htonl(time_uptime - st->creation); 5358 sp->expire = pf_state_expires(st); 5359 if (sp->expire <= time_uptime) 5360 sp->expire = htonl(0); 5361 else 5362 sp->expire = htonl(sp->expire - time_uptime); 5363 5364 sp->direction = st->direction; 5365 sp->log = st->log; 5366 sp->timeout = st->timeout; 5367 sp->state_flags = st->state_flags; 5368 if (st->src_node) 5369 sp->sync_flags |= PFSYNC_FLAG_SRCNODE; 5370 if (st->nat_src_node) 5371 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE; 5372 5373 sp->id = st->id; 5374 sp->creatorid = st->creatorid; 5375 pf_state_peer_hton(&st->src, &sp->src); 5376 pf_state_peer_hton(&st->dst, &sp->dst); 5377 5378 if (st->rule.ptr == NULL) 5379 sp->rule = htonl(-1); 5380 else 5381 sp->rule = htonl(st->rule.ptr->nr); 5382 if (st->anchor.ptr == NULL) 5383 sp->anchor = htonl(-1); 5384 else 5385 sp->anchor = htonl(st->anchor.ptr->nr); 5386 if (st->nat_rule.ptr == NULL) 5387 sp->nat_rule = htonl(-1); 5388 else 5389 sp->nat_rule = htonl(st->nat_rule.ptr->nr); 5390 5391 pf_state_counter_hton(st->packets[0], sp->packets[0]); 5392 pf_state_counter_hton(st->packets[1], sp->packets[1]); 5393 pf_state_counter_hton(st->bytes[0], sp->bytes[0]); 5394 pf_state_counter_hton(st->bytes[1], sp->bytes[1]); 5395 } 5396 5397 void 5398 pf_state_export(struct pf_state_export *sp, struct pf_kstate *st) 5399 { 5400 bzero(sp, sizeof(*sp)); 5401 5402 sp->version = PF_STATE_VERSION; 5403 5404 /* copy from state key */ 5405 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0]; 5406 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1]; 5407 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0]; 5408 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1]; 5409 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0]; 5410 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1]; 5411 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0]; 5412 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1]; 5413 sp->proto = st->key[PF_SK_WIRE]->proto; 5414 sp->af = st->key[PF_SK_WIRE]->af; 5415 5416 /* copy from state */ 5417 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname)); 5418 strlcpy(sp->orig_ifname, st->orig_kif->pfik_name, 5419 sizeof(sp->orig_ifname)); 5420 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr)); 5421 sp->creation = htonl(time_uptime - st->creation); 5422 sp->expire = pf_state_expires(st); 5423 if (sp->expire <= time_uptime) 5424 sp->expire = htonl(0); 5425 else 5426 sp->expire = htonl(sp->expire - time_uptime); 5427 5428 sp->direction = st->direction; 5429 sp->log = st->log; 5430 sp->timeout = st->timeout; 5431 sp->state_flags = st->state_flags; 5432 if (st->src_node) 5433 sp->sync_flags |= PFSYNC_FLAG_SRCNODE; 5434 if (st->nat_src_node) 5435 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE; 5436 5437 sp->id = st->id; 5438 sp->creatorid = st->creatorid; 5439 pf_state_peer_hton(&st->src, &sp->src); 5440 pf_state_peer_hton(&st->dst, &sp->dst); 5441 5442 if (st->rule.ptr == NULL) 5443 sp->rule = htonl(-1); 5444 else 5445 sp->rule = htonl(st->rule.ptr->nr); 5446 if (st->anchor.ptr == NULL) 5447 sp->anchor = htonl(-1); 5448 else 5449 sp->anchor = htonl(st->anchor.ptr->nr); 5450 if (st->nat_rule.ptr == NULL) 5451 sp->nat_rule = htonl(-1); 5452 else 5453 sp->nat_rule = htonl(st->nat_rule.ptr->nr); 5454 5455 sp->packets[0] = st->packets[0]; 5456 sp->packets[1] = st->packets[1]; 5457 sp->bytes[0] = st->bytes[0]; 5458 sp->bytes[1] = st->bytes[1]; 5459 } 5460 5461 static void 5462 pf_tbladdr_copyout(struct pf_addr_wrap *aw) 5463 { 5464 struct pfr_ktable *kt; 5465 5466 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type)); 5467 5468 kt = aw->p.tbl; 5469 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 5470 kt = kt->pfrkt_root; 5471 aw->p.tbl = NULL; 5472 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ? 5473 kt->pfrkt_cnt : -1; 5474 } 5475 5476 static int 5477 pf_add_status_counters(nvlist_t *nvl, const char *name, counter_u64_t *counters, 5478 size_t number, char **names) 5479 { 5480 nvlist_t *nvc; 5481 5482 nvc = nvlist_create(0); 5483 if (nvc == NULL) 5484 return (ENOMEM); 5485 5486 for (int i = 0; i < number; i++) { 5487 nvlist_append_number_array(nvc, "counters", 5488 counter_u64_fetch(counters[i])); 5489 nvlist_append_string_array(nvc, "names", 5490 names[i]); 5491 nvlist_append_number_array(nvc, "ids", 5492 i); 5493 } 5494 nvlist_add_nvlist(nvl, name, nvc); 5495 nvlist_destroy(nvc); 5496 5497 return (0); 5498 } 5499 5500 static int 5501 pf_getstatus(struct pfioc_nv *nv) 5502 { 5503 nvlist_t *nvl = NULL, *nvc = NULL; 5504 void *nvlpacked = NULL; 5505 int error; 5506 struct pf_status s; 5507 char *pf_reasons[PFRES_MAX+1] = PFRES_NAMES; 5508 char *pf_lcounter[KLCNT_MAX+1] = KLCNT_NAMES; 5509 char *pf_fcounter[FCNT_MAX+1] = FCNT_NAMES; 5510 PF_RULES_RLOCK_TRACKER; 5511 5512 #define ERROUT(x) ERROUT_FUNCTION(errout, x) 5513 5514 PF_RULES_RLOCK(); 5515 5516 nvl = nvlist_create(0); 5517 if (nvl == NULL) 5518 ERROUT(ENOMEM); 5519 5520 nvlist_add_bool(nvl, "running", V_pf_status.running); 5521 nvlist_add_number(nvl, "since", V_pf_status.since); 5522 nvlist_add_number(nvl, "debug", V_pf_status.debug); 5523 nvlist_add_number(nvl, "hostid", V_pf_status.hostid); 5524 nvlist_add_number(nvl, "states", V_pf_status.states); 5525 nvlist_add_number(nvl, "src_nodes", V_pf_status.src_nodes); 5526 5527 /* counters */ 5528 error = pf_add_status_counters(nvl, "counters", V_pf_status.counters, 5529 PFRES_MAX, pf_reasons); 5530 if (error != 0) 5531 ERROUT(error); 5532 5533 /* lcounters */ 5534 error = pf_add_status_counters(nvl, "lcounters", V_pf_status.lcounters, 5535 KLCNT_MAX, pf_lcounter); 5536 if (error != 0) 5537 ERROUT(error); 5538 5539 /* fcounters */ 5540 nvc = nvlist_create(0); 5541 if (nvc == NULL) 5542 ERROUT(ENOMEM); 5543 5544 for (int i = 0; i < FCNT_MAX; i++) { 5545 nvlist_append_number_array(nvc, "counters", 5546 pf_counter_u64_fetch(&V_pf_status.fcounters[i])); 5547 nvlist_append_string_array(nvc, "names", 5548 pf_fcounter[i]); 5549 nvlist_append_number_array(nvc, "ids", 5550 i); 5551 } 5552 nvlist_add_nvlist(nvl, "fcounters", nvc); 5553 nvlist_destroy(nvc); 5554 nvc = NULL; 5555 5556 /* scounters */ 5557 error = pf_add_status_counters(nvl, "scounters", V_pf_status.scounters, 5558 SCNT_MAX, pf_fcounter); 5559 if (error != 0) 5560 ERROUT(error); 5561 5562 nvlist_add_string(nvl, "ifname", V_pf_status.ifname); 5563 nvlist_add_binary(nvl, "chksum", V_pf_status.pf_chksum, 5564 PF_MD5_DIGEST_LENGTH); 5565 5566 pfi_update_status(V_pf_status.ifname, &s); 5567 5568 /* pcounters / bcounters */ 5569 for (int i = 0; i < 2; i++) { 5570 for (int j = 0; j < 2; j++) { 5571 for (int k = 0; k < 2; k++) { 5572 nvlist_append_number_array(nvl, "pcounters", 5573 s.pcounters[i][j][k]); 5574 } 5575 nvlist_append_number_array(nvl, "bcounters", 5576 s.bcounters[i][j]); 5577 } 5578 } 5579 5580 nvlpacked = nvlist_pack(nvl, &nv->len); 5581 if (nvlpacked == NULL) 5582 ERROUT(ENOMEM); 5583 5584 if (nv->size == 0) 5585 ERROUT(0); 5586 else if (nv->size < nv->len) 5587 ERROUT(ENOSPC); 5588 5589 PF_RULES_RUNLOCK(); 5590 error = copyout(nvlpacked, nv->data, nv->len); 5591 goto done; 5592 5593 #undef ERROUT 5594 errout: 5595 PF_RULES_RUNLOCK(); 5596 done: 5597 free(nvlpacked, M_NVLIST); 5598 nvlist_destroy(nvc); 5599 nvlist_destroy(nvl); 5600 5601 return (error); 5602 } 5603 5604 /* 5605 * XXX - Check for version missmatch!!! 5606 */ 5607 static void 5608 pf_clear_all_states(void) 5609 { 5610 struct pf_kstate *s; 5611 u_int i; 5612 5613 for (i = 0; i <= pf_hashmask; i++) { 5614 struct pf_idhash *ih = &V_pf_idhash[i]; 5615 relock: 5616 PF_HASHROW_LOCK(ih); 5617 LIST_FOREACH(s, &ih->states, entry) { 5618 s->timeout = PFTM_PURGE; 5619 /* Don't send out individual delete messages. */ 5620 s->state_flags |= PFSTATE_NOSYNC; 5621 pf_unlink_state(s); 5622 goto relock; 5623 } 5624 PF_HASHROW_UNLOCK(ih); 5625 } 5626 } 5627 5628 static int 5629 pf_clear_tables(void) 5630 { 5631 struct pfioc_table io; 5632 int error; 5633 5634 bzero(&io, sizeof(io)); 5635 5636 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel, 5637 io.pfrio_flags); 5638 5639 return (error); 5640 } 5641 5642 static void 5643 pf_clear_srcnodes(struct pf_ksrc_node *n) 5644 { 5645 struct pf_kstate *s; 5646 int i; 5647 5648 for (i = 0; i <= pf_hashmask; i++) { 5649 struct pf_idhash *ih = &V_pf_idhash[i]; 5650 5651 PF_HASHROW_LOCK(ih); 5652 LIST_FOREACH(s, &ih->states, entry) { 5653 if (n == NULL || n == s->src_node) 5654 s->src_node = NULL; 5655 if (n == NULL || n == s->nat_src_node) 5656 s->nat_src_node = NULL; 5657 } 5658 PF_HASHROW_UNLOCK(ih); 5659 } 5660 5661 if (n == NULL) { 5662 struct pf_srchash *sh; 5663 5664 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask; 5665 i++, sh++) { 5666 PF_HASHROW_LOCK(sh); 5667 LIST_FOREACH(n, &sh->nodes, entry) { 5668 n->expire = 1; 5669 n->states = 0; 5670 } 5671 PF_HASHROW_UNLOCK(sh); 5672 } 5673 } else { 5674 /* XXX: hash slot should already be locked here. */ 5675 n->expire = 1; 5676 n->states = 0; 5677 } 5678 } 5679 5680 static void 5681 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk) 5682 { 5683 struct pf_ksrc_node_list kill; 5684 5685 LIST_INIT(&kill); 5686 for (int i = 0; i <= pf_srchashmask; i++) { 5687 struct pf_srchash *sh = &V_pf_srchash[i]; 5688 struct pf_ksrc_node *sn, *tmp; 5689 5690 PF_HASHROW_LOCK(sh); 5691 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp) 5692 if (PF_MATCHA(psnk->psnk_src.neg, 5693 &psnk->psnk_src.addr.v.a.addr, 5694 &psnk->psnk_src.addr.v.a.mask, 5695 &sn->addr, sn->af) && 5696 PF_MATCHA(psnk->psnk_dst.neg, 5697 &psnk->psnk_dst.addr.v.a.addr, 5698 &psnk->psnk_dst.addr.v.a.mask, 5699 &sn->raddr, sn->af)) { 5700 pf_unlink_src_node(sn); 5701 LIST_INSERT_HEAD(&kill, sn, entry); 5702 sn->expire = 1; 5703 } 5704 PF_HASHROW_UNLOCK(sh); 5705 } 5706 5707 for (int i = 0; i <= pf_hashmask; i++) { 5708 struct pf_idhash *ih = &V_pf_idhash[i]; 5709 struct pf_kstate *s; 5710 5711 PF_HASHROW_LOCK(ih); 5712 LIST_FOREACH(s, &ih->states, entry) { 5713 if (s->src_node && s->src_node->expire == 1) 5714 s->src_node = NULL; 5715 if (s->nat_src_node && s->nat_src_node->expire == 1) 5716 s->nat_src_node = NULL; 5717 } 5718 PF_HASHROW_UNLOCK(ih); 5719 } 5720 5721 psnk->psnk_killed = pf_free_src_nodes(&kill); 5722 } 5723 5724 static int 5725 pf_keepcounters(struct pfioc_nv *nv) 5726 { 5727 nvlist_t *nvl = NULL; 5728 void *nvlpacked = NULL; 5729 int error = 0; 5730 5731 #define ERROUT(x) ERROUT_FUNCTION(on_error, x) 5732 5733 if (nv->len > pf_ioctl_maxcount) 5734 ERROUT(ENOMEM); 5735 5736 nvlpacked = malloc(nv->len, M_TEMP, M_WAITOK); 5737 if (nvlpacked == NULL) 5738 ERROUT(ENOMEM); 5739 5740 error = copyin(nv->data, nvlpacked, nv->len); 5741 if (error) 5742 ERROUT(error); 5743 5744 nvl = nvlist_unpack(nvlpacked, nv->len, 0); 5745 if (nvl == NULL) 5746 ERROUT(EBADMSG); 5747 5748 if (! nvlist_exists_bool(nvl, "keep_counters")) 5749 ERROUT(EBADMSG); 5750 5751 V_pf_status.keep_counters = nvlist_get_bool(nvl, "keep_counters"); 5752 5753 on_error: 5754 nvlist_destroy(nvl); 5755 free(nvlpacked, M_TEMP); 5756 return (error); 5757 } 5758 5759 static unsigned int 5760 pf_clear_states(const struct pf_kstate_kill *kill) 5761 { 5762 struct pf_state_key_cmp match_key; 5763 struct pf_kstate *s; 5764 struct pfi_kkif *kif; 5765 int idx; 5766 unsigned int killed = 0, dir; 5767 5768 for (unsigned int i = 0; i <= pf_hashmask; i++) { 5769 struct pf_idhash *ih = &V_pf_idhash[i]; 5770 5771 relock_DIOCCLRSTATES: 5772 PF_HASHROW_LOCK(ih); 5773 LIST_FOREACH(s, &ih->states, entry) { 5774 /* For floating states look at the original kif. */ 5775 kif = s->kif == V_pfi_all ? s->orig_kif : s->kif; 5776 5777 if (kill->psk_ifname[0] && 5778 strcmp(kill->psk_ifname, 5779 kif->pfik_name)) 5780 continue; 5781 5782 if (kill->psk_kill_match) { 5783 bzero(&match_key, sizeof(match_key)); 5784 5785 if (s->direction == PF_OUT) { 5786 dir = PF_IN; 5787 idx = PF_SK_STACK; 5788 } else { 5789 dir = PF_OUT; 5790 idx = PF_SK_WIRE; 5791 } 5792 5793 match_key.af = s->key[idx]->af; 5794 match_key.proto = s->key[idx]->proto; 5795 PF_ACPY(&match_key.addr[0], 5796 &s->key[idx]->addr[1], match_key.af); 5797 match_key.port[0] = s->key[idx]->port[1]; 5798 PF_ACPY(&match_key.addr[1], 5799 &s->key[idx]->addr[0], match_key.af); 5800 match_key.port[1] = s->key[idx]->port[0]; 5801 } 5802 5803 /* 5804 * Don't send out individual 5805 * delete messages. 5806 */ 5807 s->state_flags |= PFSTATE_NOSYNC; 5808 pf_unlink_state(s); 5809 killed++; 5810 5811 if (kill->psk_kill_match) 5812 killed += pf_kill_matching_state(&match_key, 5813 dir); 5814 5815 goto relock_DIOCCLRSTATES; 5816 } 5817 PF_HASHROW_UNLOCK(ih); 5818 } 5819 5820 if (V_pfsync_clear_states_ptr != NULL) 5821 V_pfsync_clear_states_ptr(V_pf_status.hostid, kill->psk_ifname); 5822 5823 return (killed); 5824 } 5825 5826 static void 5827 pf_killstates(struct pf_kstate_kill *kill, unsigned int *killed) 5828 { 5829 struct pf_kstate *s; 5830 5831 if (kill->psk_pfcmp.id) { 5832 if (kill->psk_pfcmp.creatorid == 0) 5833 kill->psk_pfcmp.creatorid = V_pf_status.hostid; 5834 if ((s = pf_find_state_byid(kill->psk_pfcmp.id, 5835 kill->psk_pfcmp.creatorid))) { 5836 pf_unlink_state(s); 5837 *killed = 1; 5838 } 5839 return; 5840 } 5841 5842 for (unsigned int i = 0; i <= pf_hashmask; i++) 5843 *killed += pf_killstates_row(kill, &V_pf_idhash[i]); 5844 5845 return; 5846 } 5847 5848 static int 5849 pf_killstates_nv(struct pfioc_nv *nv) 5850 { 5851 struct pf_kstate_kill kill; 5852 nvlist_t *nvl = NULL; 5853 void *nvlpacked = NULL; 5854 int error = 0; 5855 unsigned int killed = 0; 5856 5857 #define ERROUT(x) ERROUT_FUNCTION(on_error, x) 5858 5859 if (nv->len > pf_ioctl_maxcount) 5860 ERROUT(ENOMEM); 5861 5862 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK); 5863 if (nvlpacked == NULL) 5864 ERROUT(ENOMEM); 5865 5866 error = copyin(nv->data, nvlpacked, nv->len); 5867 if (error) 5868 ERROUT(error); 5869 5870 nvl = nvlist_unpack(nvlpacked, nv->len, 0); 5871 if (nvl == NULL) 5872 ERROUT(EBADMSG); 5873 5874 error = pf_nvstate_kill_to_kstate_kill(nvl, &kill); 5875 if (error) 5876 ERROUT(error); 5877 5878 pf_killstates(&kill, &killed); 5879 5880 free(nvlpacked, M_NVLIST); 5881 nvlpacked = NULL; 5882 nvlist_destroy(nvl); 5883 nvl = nvlist_create(0); 5884 if (nvl == NULL) 5885 ERROUT(ENOMEM); 5886 5887 nvlist_add_number(nvl, "killed", killed); 5888 5889 nvlpacked = nvlist_pack(nvl, &nv->len); 5890 if (nvlpacked == NULL) 5891 ERROUT(ENOMEM); 5892 5893 if (nv->size == 0) 5894 ERROUT(0); 5895 else if (nv->size < nv->len) 5896 ERROUT(ENOSPC); 5897 5898 error = copyout(nvlpacked, nv->data, nv->len); 5899 5900 on_error: 5901 nvlist_destroy(nvl); 5902 free(nvlpacked, M_NVLIST); 5903 return (error); 5904 } 5905 5906 static int 5907 pf_clearstates_nv(struct pfioc_nv *nv) 5908 { 5909 struct pf_kstate_kill kill; 5910 nvlist_t *nvl = NULL; 5911 void *nvlpacked = NULL; 5912 int error = 0; 5913 unsigned int killed; 5914 5915 #define ERROUT(x) ERROUT_FUNCTION(on_error, x) 5916 5917 if (nv->len > pf_ioctl_maxcount) 5918 ERROUT(ENOMEM); 5919 5920 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK); 5921 if (nvlpacked == NULL) 5922 ERROUT(ENOMEM); 5923 5924 error = copyin(nv->data, nvlpacked, nv->len); 5925 if (error) 5926 ERROUT(error); 5927 5928 nvl = nvlist_unpack(nvlpacked, nv->len, 0); 5929 if (nvl == NULL) 5930 ERROUT(EBADMSG); 5931 5932 error = pf_nvstate_kill_to_kstate_kill(nvl, &kill); 5933 if (error) 5934 ERROUT(error); 5935 5936 killed = pf_clear_states(&kill); 5937 5938 free(nvlpacked, M_NVLIST); 5939 nvlpacked = NULL; 5940 nvlist_destroy(nvl); 5941 nvl = nvlist_create(0); 5942 if (nvl == NULL) 5943 ERROUT(ENOMEM); 5944 5945 nvlist_add_number(nvl, "killed", killed); 5946 5947 nvlpacked = nvlist_pack(nvl, &nv->len); 5948 if (nvlpacked == NULL) 5949 ERROUT(ENOMEM); 5950 5951 if (nv->size == 0) 5952 ERROUT(0); 5953 else if (nv->size < nv->len) 5954 ERROUT(ENOSPC); 5955 5956 error = copyout(nvlpacked, nv->data, nv->len); 5957 5958 #undef ERROUT 5959 on_error: 5960 nvlist_destroy(nvl); 5961 free(nvlpacked, M_NVLIST); 5962 return (error); 5963 } 5964 5965 static int 5966 pf_getstate(struct pfioc_nv *nv) 5967 { 5968 nvlist_t *nvl = NULL, *nvls; 5969 void *nvlpacked = NULL; 5970 struct pf_kstate *s = NULL; 5971 int error = 0; 5972 uint64_t id, creatorid; 5973 5974 #define ERROUT(x) ERROUT_FUNCTION(errout, x) 5975 5976 if (nv->len > pf_ioctl_maxcount) 5977 ERROUT(ENOMEM); 5978 5979 nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK); 5980 if (nvlpacked == NULL) 5981 ERROUT(ENOMEM); 5982 5983 error = copyin(nv->data, nvlpacked, nv->len); 5984 if (error) 5985 ERROUT(error); 5986 5987 nvl = nvlist_unpack(nvlpacked, nv->len, 0); 5988 if (nvl == NULL) 5989 ERROUT(EBADMSG); 5990 5991 PFNV_CHK(pf_nvuint64(nvl, "id", &id)); 5992 PFNV_CHK(pf_nvuint64(nvl, "creatorid", &creatorid)); 5993 5994 s = pf_find_state_byid(id, creatorid); 5995 if (s == NULL) 5996 ERROUT(ENOENT); 5997 5998 free(nvlpacked, M_NVLIST); 5999 nvlpacked = NULL; 6000 nvlist_destroy(nvl); 6001 nvl = nvlist_create(0); 6002 if (nvl == NULL) 6003 ERROUT(ENOMEM); 6004 6005 nvls = pf_state_to_nvstate(s); 6006 if (nvls == NULL) 6007 ERROUT(ENOMEM); 6008 6009 nvlist_add_nvlist(nvl, "state", nvls); 6010 nvlist_destroy(nvls); 6011 6012 nvlpacked = nvlist_pack(nvl, &nv->len); 6013 if (nvlpacked == NULL) 6014 ERROUT(ENOMEM); 6015 6016 if (nv->size == 0) 6017 ERROUT(0); 6018 else if (nv->size < nv->len) 6019 ERROUT(ENOSPC); 6020 6021 error = copyout(nvlpacked, nv->data, nv->len); 6022 6023 #undef ERROUT 6024 errout: 6025 if (s != NULL) 6026 PF_STATE_UNLOCK(s); 6027 free(nvlpacked, M_NVLIST); 6028 nvlist_destroy(nvl); 6029 return (error); 6030 } 6031 6032 /* 6033 * XXX - Check for version missmatch!!! 6034 */ 6035 6036 /* 6037 * Duplicate pfctl -Fa operation to get rid of as much as we can. 6038 */ 6039 static int 6040 shutdown_pf(void) 6041 { 6042 int error = 0; 6043 u_int32_t t[5]; 6044 char nn = '\0'; 6045 6046 do { 6047 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn)) 6048 != 0) { 6049 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n")); 6050 break; 6051 } 6052 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn)) 6053 != 0) { 6054 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n")); 6055 break; /* XXX: rollback? */ 6056 } 6057 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn)) 6058 != 0) { 6059 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n")); 6060 break; /* XXX: rollback? */ 6061 } 6062 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn)) 6063 != 0) { 6064 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n")); 6065 break; /* XXX: rollback? */ 6066 } 6067 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn)) 6068 != 0) { 6069 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n")); 6070 break; /* XXX: rollback? */ 6071 } 6072 6073 /* XXX: these should always succeed here */ 6074 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn); 6075 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn); 6076 pf_commit_rules(t[2], PF_RULESET_NAT, &nn); 6077 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn); 6078 pf_commit_rules(t[4], PF_RULESET_RDR, &nn); 6079 6080 if ((error = pf_clear_tables()) != 0) 6081 break; 6082 6083 if ((error = pf_begin_eth(&t[0], &nn)) != 0) { 6084 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: eth\n")); 6085 break; 6086 } 6087 pf_commit_eth(t[0], &nn); 6088 6089 #ifdef ALTQ 6090 if ((error = pf_begin_altq(&t[0])) != 0) { 6091 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n")); 6092 break; 6093 } 6094 pf_commit_altq(t[0]); 6095 #endif 6096 6097 pf_clear_all_states(); 6098 6099 pf_clear_srcnodes(NULL); 6100 6101 /* status does not use malloced mem so no need to cleanup */ 6102 /* fingerprints and interfaces have their own cleanup code */ 6103 } while(0); 6104 6105 return (error); 6106 } 6107 6108 static pfil_return_t 6109 pf_check_return(int chk, struct mbuf **m) 6110 { 6111 6112 switch (chk) { 6113 case PF_PASS: 6114 if (*m == NULL) 6115 return (PFIL_CONSUMED); 6116 else 6117 return (PFIL_PASS); 6118 break; 6119 default: 6120 if (*m != NULL) { 6121 m_freem(*m); 6122 *m = NULL; 6123 } 6124 return (PFIL_DROPPED); 6125 } 6126 } 6127 6128 static pfil_return_t 6129 pf_eth_check_in(struct mbuf **m, struct ifnet *ifp, int flags, 6130 void *ruleset __unused, struct inpcb *inp) 6131 { 6132 int chk; 6133 6134 chk = pf_test_eth(PF_IN, flags, ifp, m, inp); 6135 6136 return (pf_check_return(chk, m)); 6137 } 6138 6139 static pfil_return_t 6140 pf_eth_check_out(struct mbuf **m, struct ifnet *ifp, int flags, 6141 void *ruleset __unused, struct inpcb *inp) 6142 { 6143 int chk; 6144 6145 chk = pf_test_eth(PF_OUT, flags, ifp, m, inp); 6146 6147 return (pf_check_return(chk, m)); 6148 } 6149 6150 #ifdef INET 6151 static pfil_return_t 6152 pf_check_in(struct mbuf **m, struct ifnet *ifp, int flags, 6153 void *ruleset __unused, struct inpcb *inp) 6154 { 6155 int chk; 6156 6157 chk = pf_test(PF_IN, flags, ifp, m, inp); 6158 6159 return (pf_check_return(chk, m)); 6160 } 6161 6162 static pfil_return_t 6163 pf_check_out(struct mbuf **m, struct ifnet *ifp, int flags, 6164 void *ruleset __unused, struct inpcb *inp) 6165 { 6166 int chk; 6167 6168 chk = pf_test(PF_OUT, flags, ifp, m, inp); 6169 6170 return (pf_check_return(chk, m)); 6171 } 6172 #endif 6173 6174 #ifdef INET6 6175 static pfil_return_t 6176 pf_check6_in(struct mbuf **m, struct ifnet *ifp, int flags, 6177 void *ruleset __unused, struct inpcb *inp) 6178 { 6179 int chk; 6180 6181 /* 6182 * In case of loopback traffic IPv6 uses the real interface in 6183 * order to support scoped addresses. In order to support stateful 6184 * filtering we have change this to lo0 as it is the case in IPv4. 6185 */ 6186 CURVNET_SET(ifp->if_vnet); 6187 chk = pf_test6(PF_IN, flags, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp); 6188 CURVNET_RESTORE(); 6189 6190 return (pf_check_return(chk, m)); 6191 } 6192 6193 static pfil_return_t 6194 pf_check6_out(struct mbuf **m, struct ifnet *ifp, int flags, 6195 void *ruleset __unused, struct inpcb *inp) 6196 { 6197 int chk; 6198 6199 CURVNET_SET(ifp->if_vnet); 6200 chk = pf_test6(PF_OUT, flags, ifp, m, inp); 6201 CURVNET_RESTORE(); 6202 6203 return (pf_check_return(chk, m)); 6204 } 6205 #endif /* INET6 */ 6206 6207 VNET_DEFINE_STATIC(pfil_hook_t, pf_eth_in_hook); 6208 VNET_DEFINE_STATIC(pfil_hook_t, pf_eth_out_hook); 6209 #define V_pf_eth_in_hook VNET(pf_eth_in_hook) 6210 #define V_pf_eth_out_hook VNET(pf_eth_out_hook) 6211 6212 #ifdef INET 6213 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_in_hook); 6214 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip4_out_hook); 6215 #define V_pf_ip4_in_hook VNET(pf_ip4_in_hook) 6216 #define V_pf_ip4_out_hook VNET(pf_ip4_out_hook) 6217 #endif 6218 #ifdef INET6 6219 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_in_hook); 6220 VNET_DEFINE_STATIC(pfil_hook_t, pf_ip6_out_hook); 6221 #define V_pf_ip6_in_hook VNET(pf_ip6_in_hook) 6222 #define V_pf_ip6_out_hook VNET(pf_ip6_out_hook) 6223 #endif 6224 6225 static void 6226 hook_pf_eth(void) 6227 { 6228 struct pfil_hook_args pha; 6229 struct pfil_link_args pla; 6230 int ret __diagused; 6231 6232 if (V_pf_pfil_eth_hooked) 6233 return; 6234 6235 pha.pa_version = PFIL_VERSION; 6236 pha.pa_modname = "pf"; 6237 pha.pa_ruleset = NULL; 6238 6239 pla.pa_version = PFIL_VERSION; 6240 6241 pha.pa_type = PFIL_TYPE_ETHERNET; 6242 pha.pa_func = pf_eth_check_in; 6243 pha.pa_flags = PFIL_IN; 6244 pha.pa_rulname = "eth-in"; 6245 V_pf_eth_in_hook = pfil_add_hook(&pha); 6246 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR; 6247 pla.pa_head = V_link_pfil_head; 6248 pla.pa_hook = V_pf_eth_in_hook; 6249 ret = pfil_link(&pla); 6250 MPASS(ret == 0); 6251 pha.pa_func = pf_eth_check_out; 6252 pha.pa_flags = PFIL_OUT; 6253 pha.pa_rulname = "eth-out"; 6254 V_pf_eth_out_hook = pfil_add_hook(&pha); 6255 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR; 6256 pla.pa_head = V_link_pfil_head; 6257 pla.pa_hook = V_pf_eth_out_hook; 6258 ret = pfil_link(&pla); 6259 MPASS(ret == 0); 6260 6261 V_pf_pfil_eth_hooked = 1; 6262 } 6263 6264 static void 6265 hook_pf(void) 6266 { 6267 struct pfil_hook_args pha; 6268 struct pfil_link_args pla; 6269 int ret; 6270 6271 if (V_pf_pfil_hooked) 6272 return; 6273 6274 pha.pa_version = PFIL_VERSION; 6275 pha.pa_modname = "pf"; 6276 pha.pa_ruleset = NULL; 6277 6278 pla.pa_version = PFIL_VERSION; 6279 6280 #ifdef INET 6281 pha.pa_type = PFIL_TYPE_IP4; 6282 pha.pa_func = pf_check_in; 6283 pha.pa_flags = PFIL_IN; 6284 pha.pa_rulname = "default-in"; 6285 V_pf_ip4_in_hook = pfil_add_hook(&pha); 6286 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR; 6287 pla.pa_head = V_inet_pfil_head; 6288 pla.pa_hook = V_pf_ip4_in_hook; 6289 ret = pfil_link(&pla); 6290 MPASS(ret == 0); 6291 pha.pa_func = pf_check_out; 6292 pha.pa_flags = PFIL_OUT; 6293 pha.pa_rulname = "default-out"; 6294 V_pf_ip4_out_hook = pfil_add_hook(&pha); 6295 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR; 6296 pla.pa_head = V_inet_pfil_head; 6297 pla.pa_hook = V_pf_ip4_out_hook; 6298 ret = pfil_link(&pla); 6299 MPASS(ret == 0); 6300 #endif 6301 #ifdef INET6 6302 pha.pa_type = PFIL_TYPE_IP6; 6303 pha.pa_func = pf_check6_in; 6304 pha.pa_flags = PFIL_IN; 6305 pha.pa_rulname = "default-in6"; 6306 V_pf_ip6_in_hook = pfil_add_hook(&pha); 6307 pla.pa_flags = PFIL_IN | PFIL_HEADPTR | PFIL_HOOKPTR; 6308 pla.pa_head = V_inet6_pfil_head; 6309 pla.pa_hook = V_pf_ip6_in_hook; 6310 ret = pfil_link(&pla); 6311 MPASS(ret == 0); 6312 pha.pa_func = pf_check6_out; 6313 pha.pa_rulname = "default-out6"; 6314 pha.pa_flags = PFIL_OUT; 6315 V_pf_ip6_out_hook = pfil_add_hook(&pha); 6316 pla.pa_flags = PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR; 6317 pla.pa_head = V_inet6_pfil_head; 6318 pla.pa_hook = V_pf_ip6_out_hook; 6319 ret = pfil_link(&pla); 6320 MPASS(ret == 0); 6321 #endif 6322 6323 V_pf_pfil_hooked = 1; 6324 } 6325 6326 static void 6327 dehook_pf_eth(void) 6328 { 6329 6330 if (V_pf_pfil_eth_hooked == 0) 6331 return; 6332 6333 pfil_remove_hook(V_pf_eth_in_hook); 6334 pfil_remove_hook(V_pf_eth_out_hook); 6335 6336 V_pf_pfil_eth_hooked = 0; 6337 } 6338 6339 static void 6340 dehook_pf(void) 6341 { 6342 6343 if (V_pf_pfil_hooked == 0) 6344 return; 6345 6346 #ifdef INET 6347 pfil_remove_hook(V_pf_ip4_in_hook); 6348 pfil_remove_hook(V_pf_ip4_out_hook); 6349 #endif 6350 #ifdef INET6 6351 pfil_remove_hook(V_pf_ip6_in_hook); 6352 pfil_remove_hook(V_pf_ip6_out_hook); 6353 #endif 6354 6355 V_pf_pfil_hooked = 0; 6356 } 6357 6358 static void 6359 pf_load_vnet(void) 6360 { 6361 V_pf_tag_z = uma_zcreate("pf tags", sizeof(struct pf_tagname), 6362 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 6363 6364 pf_init_tagset(&V_pf_tags, &pf_rule_tag_hashsize, 6365 PF_RULE_TAG_HASH_SIZE_DEFAULT); 6366 #ifdef ALTQ 6367 pf_init_tagset(&V_pf_qids, &pf_queue_tag_hashsize, 6368 PF_QUEUE_TAG_HASH_SIZE_DEFAULT); 6369 #endif 6370 6371 V_pf_keth = &V_pf_main_keth_anchor.ruleset; 6372 6373 pfattach_vnet(); 6374 V_pf_vnet_active = 1; 6375 } 6376 6377 static int 6378 pf_load(void) 6379 { 6380 int error; 6381 6382 rm_init_flags(&pf_rules_lock, "pf rulesets", RM_RECURSE); 6383 sx_init(&pf_ioctl_lock, "pf ioctl"); 6384 sx_init(&pf_end_lock, "pf end thread"); 6385 6386 pf_mtag_initialize(); 6387 6388 pf_dev = make_dev(&pf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, PF_NAME); 6389 if (pf_dev == NULL) 6390 return (ENOMEM); 6391 6392 pf_end_threads = 0; 6393 error = kproc_create(pf_purge_thread, NULL, &pf_purge_proc, 0, 0, "pf purge"); 6394 if (error != 0) 6395 return (error); 6396 6397 pfi_initialize(); 6398 6399 return (0); 6400 } 6401 6402 static void 6403 pf_unload_vnet(void) 6404 { 6405 int ret; 6406 6407 V_pf_vnet_active = 0; 6408 V_pf_status.running = 0; 6409 dehook_pf(); 6410 dehook_pf_eth(); 6411 6412 PF_RULES_WLOCK(); 6413 pf_syncookies_cleanup(); 6414 shutdown_pf(); 6415 PF_RULES_WUNLOCK(); 6416 6417 ret = swi_remove(V_pf_swi_cookie); 6418 MPASS(ret == 0); 6419 ret = intr_event_destroy(V_pf_swi_ie); 6420 MPASS(ret == 0); 6421 6422 pf_unload_vnet_purge(); 6423 6424 pf_normalize_cleanup(); 6425 PF_RULES_WLOCK(); 6426 pfi_cleanup_vnet(); 6427 PF_RULES_WUNLOCK(); 6428 pfr_cleanup(); 6429 pf_osfp_flush(); 6430 pf_cleanup(); 6431 if (IS_DEFAULT_VNET(curvnet)) 6432 pf_mtag_cleanup(); 6433 6434 pf_cleanup_tagset(&V_pf_tags); 6435 #ifdef ALTQ 6436 pf_cleanup_tagset(&V_pf_qids); 6437 #endif 6438 uma_zdestroy(V_pf_tag_z); 6439 6440 #ifdef PF_WANT_32_TO_64_COUNTER 6441 PF_RULES_WLOCK(); 6442 LIST_REMOVE(V_pf_kifmarker, pfik_allkiflist); 6443 6444 MPASS(LIST_EMPTY(&V_pf_allkiflist)); 6445 MPASS(V_pf_allkifcount == 0); 6446 6447 LIST_REMOVE(&V_pf_default_rule, allrulelist); 6448 V_pf_allrulecount--; 6449 LIST_REMOVE(V_pf_rulemarker, allrulelist); 6450 6451 /* 6452 * There are known pf rule leaks when running the test suite. 6453 */ 6454 #ifdef notyet 6455 MPASS(LIST_EMPTY(&V_pf_allrulelist)); 6456 MPASS(V_pf_allrulecount == 0); 6457 #endif 6458 6459 PF_RULES_WUNLOCK(); 6460 6461 free(V_pf_kifmarker, PFI_MTYPE); 6462 free(V_pf_rulemarker, M_PFRULE); 6463 #endif 6464 6465 /* Free counters last as we updated them during shutdown. */ 6466 pf_counter_u64_deinit(&V_pf_default_rule.evaluations); 6467 for (int i = 0; i < 2; i++) { 6468 pf_counter_u64_deinit(&V_pf_default_rule.packets[i]); 6469 pf_counter_u64_deinit(&V_pf_default_rule.bytes[i]); 6470 } 6471 counter_u64_free(V_pf_default_rule.states_cur); 6472 counter_u64_free(V_pf_default_rule.states_tot); 6473 counter_u64_free(V_pf_default_rule.src_nodes); 6474 6475 for (int i = 0; i < PFRES_MAX; i++) 6476 counter_u64_free(V_pf_status.counters[i]); 6477 for (int i = 0; i < KLCNT_MAX; i++) 6478 counter_u64_free(V_pf_status.lcounters[i]); 6479 for (int i = 0; i < FCNT_MAX; i++) 6480 pf_counter_u64_deinit(&V_pf_status.fcounters[i]); 6481 for (int i = 0; i < SCNT_MAX; i++) 6482 counter_u64_free(V_pf_status.scounters[i]); 6483 } 6484 6485 static void 6486 pf_unload(void) 6487 { 6488 6489 sx_xlock(&pf_end_lock); 6490 pf_end_threads = 1; 6491 while (pf_end_threads < 2) { 6492 wakeup_one(pf_purge_thread); 6493 sx_sleep(pf_purge_proc, &pf_end_lock, 0, "pftmo", 0); 6494 } 6495 sx_xunlock(&pf_end_lock); 6496 6497 if (pf_dev != NULL) 6498 destroy_dev(pf_dev); 6499 6500 pfi_cleanup(); 6501 6502 rm_destroy(&pf_rules_lock); 6503 sx_destroy(&pf_ioctl_lock); 6504 sx_destroy(&pf_end_lock); 6505 } 6506 6507 static void 6508 vnet_pf_init(void *unused __unused) 6509 { 6510 6511 pf_load_vnet(); 6512 } 6513 VNET_SYSINIT(vnet_pf_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD, 6514 vnet_pf_init, NULL); 6515 6516 static void 6517 vnet_pf_uninit(const void *unused __unused) 6518 { 6519 6520 pf_unload_vnet(); 6521 } 6522 SYSUNINIT(pf_unload, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND, pf_unload, NULL); 6523 VNET_SYSUNINIT(vnet_pf_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD, 6524 vnet_pf_uninit, NULL); 6525 6526 static int 6527 pf_modevent(module_t mod, int type, void *data) 6528 { 6529 int error = 0; 6530 6531 switch(type) { 6532 case MOD_LOAD: 6533 error = pf_load(); 6534 break; 6535 case MOD_UNLOAD: 6536 /* Handled in SYSUNINIT(pf_unload) to ensure it's done after 6537 * the vnet_pf_uninit()s */ 6538 break; 6539 default: 6540 error = EINVAL; 6541 break; 6542 } 6543 6544 return (error); 6545 } 6546 6547 static moduledata_t pf_mod = { 6548 "pf", 6549 pf_modevent, 6550 0 6551 }; 6552 6553 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND); 6554 MODULE_VERSION(pf, PF_MODVER); 6555