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