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