1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Daniel Hartmeier 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials provided 16 * with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $OpenBSD: pfvar.h,v 1.282 2009/01/29 15:12:28 pyr Exp $ 32 * $FreeBSD$ 33 */ 34 35 #ifndef _NET_PFVAR_H_ 36 #define _NET_PFVAR_H_ 37 38 #include <sys/param.h> 39 #include <sys/queue.h> 40 #include <sys/counter.h> 41 #include <sys/cpuset.h> 42 #include <sys/malloc.h> 43 #include <sys/nv.h> 44 #include <sys/refcount.h> 45 #include <sys/sysctl.h> 46 #include <sys/lock.h> 47 #include <sys/rmlock.h> 48 #include <sys/tree.h> 49 #include <vm/uma.h> 50 51 #include <net/radix.h> 52 #include <netinet/in.h> 53 54 #include <netpfil/pf/pf.h> 55 #include <netpfil/pf/pf_altq.h> 56 #include <netpfil/pf/pf_mtag.h> 57 58 #ifdef _KERNEL 59 60 SYSCTL_DECL(_net_pf); 61 MALLOC_DECLARE(M_PFHASH); 62 63 struct pfi_dynaddr { 64 TAILQ_ENTRY(pfi_dynaddr) entry; 65 struct pf_addr pfid_addr4; 66 struct pf_addr pfid_mask4; 67 struct pf_addr pfid_addr6; 68 struct pf_addr pfid_mask6; 69 struct pfr_ktable *pfid_kt; 70 struct pfi_kkif *pfid_kif; 71 int pfid_net; /* mask or 128 */ 72 int pfid_acnt4; /* address count IPv4 */ 73 int pfid_acnt6; /* address count IPv6 */ 74 sa_family_t pfid_af; /* rule af */ 75 u_int8_t pfid_iflags; /* PFI_AFLAG_* */ 76 }; 77 78 /* 79 * Address manipulation macros 80 */ 81 #define HTONL(x) (x) = htonl((__uint32_t)(x)) 82 #define HTONS(x) (x) = htons((__uint16_t)(x)) 83 #define NTOHL(x) (x) = ntohl((__uint32_t)(x)) 84 #define NTOHS(x) (x) = ntohs((__uint16_t)(x)) 85 86 #define PF_NAME "pf" 87 88 #define PF_HASHROW_ASSERT(h) mtx_assert(&(h)->lock, MA_OWNED) 89 #define PF_HASHROW_LOCK(h) mtx_lock(&(h)->lock) 90 #define PF_HASHROW_UNLOCK(h) mtx_unlock(&(h)->lock) 91 92 #define PF_STATE_LOCK(s) \ 93 do { \ 94 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(s)]; \ 95 PF_HASHROW_LOCK(_ih); \ 96 } while (0) 97 98 #define PF_STATE_UNLOCK(s) \ 99 do { \ 100 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH((s))]; \ 101 PF_HASHROW_UNLOCK(_ih); \ 102 } while (0) 103 104 #ifdef INVARIANTS 105 #define PF_STATE_LOCK_ASSERT(s) \ 106 do { \ 107 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(s)]; \ 108 PF_HASHROW_ASSERT(_ih); \ 109 } while (0) 110 #else /* !INVARIANTS */ 111 #define PF_STATE_LOCK_ASSERT(s) do {} while (0) 112 #endif /* INVARIANTS */ 113 114 extern struct mtx pf_unlnkdrules_mtx; 115 #define PF_UNLNKDRULES_LOCK() mtx_lock(&pf_unlnkdrules_mtx) 116 #define PF_UNLNKDRULES_UNLOCK() mtx_unlock(&pf_unlnkdrules_mtx) 117 118 extern struct rmlock pf_rules_lock; 119 #define PF_RULES_RLOCK_TRACKER struct rm_priotracker _pf_rules_tracker 120 #define PF_RULES_RLOCK() rm_rlock(&pf_rules_lock, &_pf_rules_tracker) 121 #define PF_RULES_RUNLOCK() rm_runlock(&pf_rules_lock, &_pf_rules_tracker) 122 #define PF_RULES_WLOCK() rm_wlock(&pf_rules_lock) 123 #define PF_RULES_WUNLOCK() rm_wunlock(&pf_rules_lock) 124 #define PF_RULES_ASSERT() rm_assert(&pf_rules_lock, RA_LOCKED) 125 #define PF_RULES_RASSERT() rm_assert(&pf_rules_lock, RA_RLOCKED) 126 #define PF_RULES_WASSERT() rm_assert(&pf_rules_lock, RA_WLOCKED) 127 128 extern struct sx pf_end_lock; 129 130 #define PF_MODVER 1 131 #define PFLOG_MODVER 1 132 #define PFSYNC_MODVER 1 133 134 #define PFLOG_MINVER 1 135 #define PFLOG_PREFVER PFLOG_MODVER 136 #define PFLOG_MAXVER 1 137 #define PFSYNC_MINVER 1 138 #define PFSYNC_PREFVER PFSYNC_MODVER 139 #define PFSYNC_MAXVER 1 140 141 #ifdef INET 142 #ifndef INET6 143 #define PF_INET_ONLY 144 #endif /* ! INET6 */ 145 #endif /* INET */ 146 147 #ifdef INET6 148 #ifndef INET 149 #define PF_INET6_ONLY 150 #endif /* ! INET */ 151 #endif /* INET6 */ 152 153 #ifdef INET 154 #ifdef INET6 155 #define PF_INET_INET6 156 #endif /* INET6 */ 157 #endif /* INET */ 158 159 #else 160 161 #define PF_INET_INET6 162 163 #endif /* _KERNEL */ 164 165 /* Both IPv4 and IPv6 */ 166 #ifdef PF_INET_INET6 167 168 #define PF_AEQ(a, b, c) \ 169 ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \ 170 (c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \ 171 (a)->addr32[2] == (b)->addr32[2] && \ 172 (a)->addr32[1] == (b)->addr32[1] && \ 173 (a)->addr32[0] == (b)->addr32[0])) \ 174 175 #define PF_ANEQ(a, b, c) \ 176 ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \ 177 (c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \ 178 (a)->addr32[1] != (b)->addr32[1] || \ 179 (a)->addr32[2] != (b)->addr32[2] || \ 180 (a)->addr32[3] != (b)->addr32[3]))) \ 181 182 #define PF_AZERO(a, c) \ 183 ((c == AF_INET && !(a)->addr32[0]) || \ 184 (c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \ 185 !(a)->addr32[2] && !(a)->addr32[3] )) \ 186 187 #define PF_MATCHA(n, a, m, b, f) \ 188 pf_match_addr(n, a, m, b, f) 189 190 #define PF_ACPY(a, b, f) \ 191 pf_addrcpy(a, b, f) 192 193 #define PF_AINC(a, f) \ 194 pf_addr_inc(a, f) 195 196 #define PF_POOLMASK(a, b, c, d, f) \ 197 pf_poolmask(a, b, c, d, f) 198 199 #else 200 201 /* Just IPv6 */ 202 203 #ifdef PF_INET6_ONLY 204 205 #define PF_AEQ(a, b, c) \ 206 ((a)->addr32[3] == (b)->addr32[3] && \ 207 (a)->addr32[2] == (b)->addr32[2] && \ 208 (a)->addr32[1] == (b)->addr32[1] && \ 209 (a)->addr32[0] == (b)->addr32[0]) \ 210 211 #define PF_ANEQ(a, b, c) \ 212 ((a)->addr32[3] != (b)->addr32[3] || \ 213 (a)->addr32[2] != (b)->addr32[2] || \ 214 (a)->addr32[1] != (b)->addr32[1] || \ 215 (a)->addr32[0] != (b)->addr32[0]) \ 216 217 #define PF_AZERO(a, c) \ 218 (!(a)->addr32[0] && \ 219 !(a)->addr32[1] && \ 220 !(a)->addr32[2] && \ 221 !(a)->addr32[3] ) \ 222 223 #define PF_MATCHA(n, a, m, b, f) \ 224 pf_match_addr(n, a, m, b, f) 225 226 #define PF_ACPY(a, b, f) \ 227 pf_addrcpy(a, b, f) 228 229 #define PF_AINC(a, f) \ 230 pf_addr_inc(a, f) 231 232 #define PF_POOLMASK(a, b, c, d, f) \ 233 pf_poolmask(a, b, c, d, f) 234 235 #else 236 237 /* Just IPv4 */ 238 #ifdef PF_INET_ONLY 239 240 #define PF_AEQ(a, b, c) \ 241 ((a)->addr32[0] == (b)->addr32[0]) 242 243 #define PF_ANEQ(a, b, c) \ 244 ((a)->addr32[0] != (b)->addr32[0]) 245 246 #define PF_AZERO(a, c) \ 247 (!(a)->addr32[0]) 248 249 #define PF_MATCHA(n, a, m, b, f) \ 250 pf_match_addr(n, a, m, b, f) 251 252 #define PF_ACPY(a, b, f) \ 253 (a)->v4.s_addr = (b)->v4.s_addr 254 255 #define PF_AINC(a, f) \ 256 do { \ 257 (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \ 258 } while (0) 259 260 #define PF_POOLMASK(a, b, c, d, f) \ 261 do { \ 262 (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \ 263 (((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \ 264 } while (0) 265 266 #endif /* PF_INET_ONLY */ 267 #endif /* PF_INET6_ONLY */ 268 #endif /* PF_INET_INET6 */ 269 270 /* 271 * XXX callers not FIB-aware in our version of pf yet. 272 * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio. 273 */ 274 #define PF_MISMATCHAW(aw, x, af, neg, ifp, rtid) \ 275 ( \ 276 (((aw)->type == PF_ADDR_NOROUTE && \ 277 pf_routable((x), (af), NULL, (rtid))) || \ 278 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \ 279 pf_routable((x), (af), (ifp), (rtid))) || \ 280 ((aw)->type == PF_ADDR_TABLE && \ 281 !pfr_match_addr((aw)->p.tbl, (x), (af))) || \ 282 ((aw)->type == PF_ADDR_DYNIFTL && \ 283 !pfi_match_addr((aw)->p.dyn, (x), (af))) || \ 284 ((aw)->type == PF_ADDR_RANGE && \ 285 !pf_match_addr_range(&(aw)->v.a.addr, \ 286 &(aw)->v.a.mask, (x), (af))) || \ 287 ((aw)->type == PF_ADDR_ADDRMASK && \ 288 !PF_AZERO(&(aw)->v.a.mask, (af)) && \ 289 !PF_MATCHA(0, &(aw)->v.a.addr, \ 290 &(aw)->v.a.mask, (x), (af))))) != \ 291 (neg) \ 292 ) 293 294 #define PF_ALGNMNT(off) (((off) % 2) == 0) 295 296 #ifdef _KERNEL 297 298 struct pf_kpooladdr { 299 struct pf_addr_wrap addr; 300 TAILQ_ENTRY(pf_kpooladdr) entries; 301 char ifname[IFNAMSIZ]; 302 struct pfi_kkif *kif; 303 }; 304 305 TAILQ_HEAD(pf_kpalist, pf_kpooladdr); 306 307 struct pf_kpool { 308 struct pf_kpalist list; 309 struct pf_kpooladdr *cur; 310 struct pf_poolhashkey key; 311 struct pf_addr counter; 312 struct pf_mape_portset mape; 313 int tblidx; 314 u_int16_t proxy_port[2]; 315 u_int8_t opts; 316 }; 317 318 union pf_krule_ptr { 319 struct pf_krule *ptr; 320 u_int32_t nr; 321 }; 322 323 struct pf_krule { 324 struct pf_rule_addr src; 325 struct pf_rule_addr dst; 326 union pf_krule_ptr skip[PF_SKIP_COUNT]; 327 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 328 char ifname[IFNAMSIZ]; 329 char qname[PF_QNAME_SIZE]; 330 char pqname[PF_QNAME_SIZE]; 331 char tagname[PF_TAG_NAME_SIZE]; 332 char match_tagname[PF_TAG_NAME_SIZE]; 333 334 char overload_tblname[PF_TABLE_NAME_SIZE]; 335 336 TAILQ_ENTRY(pf_krule) entries; 337 struct pf_kpool rpool; 338 339 counter_u64_t evaluations; 340 counter_u64_t packets[2]; 341 counter_u64_t bytes[2]; 342 343 struct pfi_kkif *kif; 344 struct pf_kanchor *anchor; 345 struct pfr_ktable *overload_tbl; 346 347 pf_osfp_t os_fingerprint; 348 349 int rtableid; 350 u_int32_t timeout[PFTM_MAX]; 351 u_int32_t max_states; 352 u_int32_t max_src_nodes; 353 u_int32_t max_src_states; 354 u_int32_t max_src_conn; 355 struct { 356 u_int32_t limit; 357 u_int32_t seconds; 358 } max_src_conn_rate; 359 u_int32_t qid; 360 u_int32_t pqid; 361 u_int32_t nr; 362 u_int32_t prob; 363 uid_t cuid; 364 pid_t cpid; 365 366 counter_u64_t states_cur; 367 counter_u64_t states_tot; 368 counter_u64_t src_nodes; 369 370 u_int16_t return_icmp; 371 u_int16_t return_icmp6; 372 u_int16_t max_mss; 373 u_int16_t tag; 374 u_int16_t match_tag; 375 u_int16_t scrub_flags; 376 377 struct pf_rule_uid uid; 378 struct pf_rule_gid gid; 379 380 u_int32_t rule_flag; 381 uint32_t rule_ref; 382 u_int8_t action; 383 u_int8_t direction; 384 u_int8_t log; 385 u_int8_t logif; 386 u_int8_t quick; 387 u_int8_t ifnot; 388 u_int8_t match_tag_not; 389 u_int8_t natpass; 390 391 u_int8_t keep_state; 392 sa_family_t af; 393 u_int8_t proto; 394 u_int8_t type; 395 u_int8_t code; 396 u_int8_t flags; 397 u_int8_t flagset; 398 u_int8_t min_ttl; 399 u_int8_t allow_opts; 400 u_int8_t rt; 401 u_int8_t return_ttl; 402 u_int8_t tos; 403 u_int8_t set_tos; 404 u_int8_t anchor_relative; 405 u_int8_t anchor_wildcard; 406 407 u_int8_t flush; 408 u_int8_t prio; 409 u_int8_t set_prio[2]; 410 411 struct { 412 struct pf_addr addr; 413 u_int16_t port; 414 } divert; 415 }; 416 417 struct pf_ksrc_node { 418 LIST_ENTRY(pf_ksrc_node) entry; 419 struct pf_addr addr; 420 struct pf_addr raddr; 421 union pf_krule_ptr rule; 422 struct pfi_kkif *kif; 423 counter_u64_t bytes[2]; 424 counter_u64_t packets[2]; 425 u_int32_t states; 426 u_int32_t conn; 427 struct pf_threshold conn_rate; 428 u_int32_t creation; 429 u_int32_t expire; 430 sa_family_t af; 431 u_int8_t ruletype; 432 }; 433 #endif 434 435 struct pf_state_scrub { 436 struct timeval pfss_last; /* time received last packet */ 437 u_int32_t pfss_tsecr; /* last echoed timestamp */ 438 u_int32_t pfss_tsval; /* largest timestamp */ 439 u_int32_t pfss_tsval0; /* original timestamp */ 440 u_int16_t pfss_flags; 441 #define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */ 442 #define PFSS_PAWS 0x0010 /* stricter PAWS checks */ 443 #define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */ 444 #define PFSS_DATA_TS 0x0040 /* timestamp on data packets */ 445 #define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */ 446 u_int8_t pfss_ttl; /* stashed TTL */ 447 u_int8_t pad; 448 u_int32_t pfss_ts_mod; /* timestamp modulation */ 449 }; 450 451 struct pf_state_host { 452 struct pf_addr addr; 453 u_int16_t port; 454 u_int16_t pad; 455 }; 456 457 struct pf_state_peer { 458 struct pf_state_scrub *scrub; /* state is scrubbed */ 459 u_int32_t seqlo; /* Max sequence number sent */ 460 u_int32_t seqhi; /* Max the other end ACKd + win */ 461 u_int32_t seqdiff; /* Sequence number modulator */ 462 u_int16_t max_win; /* largest window (pre scaling) */ 463 u_int16_t mss; /* Maximum segment size option */ 464 u_int8_t state; /* active state level */ 465 u_int8_t wscale; /* window scaling factor */ 466 u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */ 467 u_int8_t pad[1]; 468 }; 469 470 /* Keep synced with struct pf_state_key. */ 471 struct pf_state_key_cmp { 472 struct pf_addr addr[2]; 473 u_int16_t port[2]; 474 sa_family_t af; 475 u_int8_t proto; 476 u_int8_t pad[2]; 477 }; 478 479 struct pf_state_key { 480 struct pf_addr addr[2]; 481 u_int16_t port[2]; 482 sa_family_t af; 483 u_int8_t proto; 484 u_int8_t pad[2]; 485 486 LIST_ENTRY(pf_state_key) entry; 487 TAILQ_HEAD(, pf_state) states[2]; 488 }; 489 490 /* Keep synced with struct pf_state. */ 491 struct pf_state_cmp { 492 u_int64_t id; 493 u_int32_t creatorid; 494 u_int8_t direction; 495 u_int8_t pad[3]; 496 }; 497 498 #define PFSTATE_ALLOWOPTS 0x01 499 #define PFSTATE_SLOPPY 0x02 500 /* was PFSTATE_PFLOW 0x04 */ 501 #define PFSTATE_NOSYNC 0x08 502 #define PFSTATE_ACK 0x10 503 #define PFSTATE_SETPRIO 0x0200 504 #define PFSTATE_SETMASK (PFSTATE_SETPRIO) 505 506 #ifdef _KERNEL 507 struct pf_state { 508 u_int64_t id; 509 u_int32_t creatorid; 510 u_int8_t direction; 511 u_int8_t pad[3]; 512 513 u_int refs; 514 TAILQ_ENTRY(pf_state) sync_list; 515 TAILQ_ENTRY(pf_state) key_list[2]; 516 LIST_ENTRY(pf_state) entry; 517 struct pf_state_peer src; 518 struct pf_state_peer dst; 519 union pf_krule_ptr rule; 520 union pf_krule_ptr anchor; 521 union pf_krule_ptr nat_rule; 522 struct pf_addr rt_addr; 523 struct pf_state_key *key[2]; /* addresses stack and wire */ 524 struct pfi_kkif *kif; 525 struct pfi_kkif *orig_kif; /* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */ 526 struct pfi_kkif *rt_kif; 527 struct pf_ksrc_node *src_node; 528 struct pf_ksrc_node *nat_src_node; 529 counter_u64_t packets[2]; 530 counter_u64_t bytes[2]; 531 u_int32_t creation; 532 u_int32_t expire; 533 u_int32_t pfsync_time; 534 u_int16_t tag; 535 u_int8_t log; 536 u_int8_t state_flags; 537 u_int8_t timeout; 538 u_int8_t sync_state; /* PFSYNC_S_x */ 539 540 /* XXX */ 541 u_int8_t sync_updates; 542 u_int8_t _tail[3]; 543 }; 544 #endif 545 546 /* 547 * Unified state structures for pulling states out of the kernel 548 * used by pfsync(4) and the pf(4) ioctl. 549 */ 550 struct pfsync_state_scrub { 551 u_int16_t pfss_flags; 552 u_int8_t pfss_ttl; /* stashed TTL */ 553 #define PFSYNC_SCRUB_FLAG_VALID 0x01 554 u_int8_t scrub_flag; 555 u_int32_t pfss_ts_mod; /* timestamp modulation */ 556 } __packed; 557 558 struct pfsync_state_peer { 559 struct pfsync_state_scrub scrub; /* state is scrubbed */ 560 u_int32_t seqlo; /* Max sequence number sent */ 561 u_int32_t seqhi; /* Max the other end ACKd + win */ 562 u_int32_t seqdiff; /* Sequence number modulator */ 563 u_int16_t max_win; /* largest window (pre scaling) */ 564 u_int16_t mss; /* Maximum segment size option */ 565 u_int8_t state; /* active state level */ 566 u_int8_t wscale; /* window scaling factor */ 567 u_int8_t pad[6]; 568 } __packed; 569 570 struct pfsync_state_key { 571 struct pf_addr addr[2]; 572 u_int16_t port[2]; 573 }; 574 575 struct pfsync_state { 576 u_int64_t id; 577 char ifname[IFNAMSIZ]; 578 struct pfsync_state_key key[2]; 579 struct pfsync_state_peer src; 580 struct pfsync_state_peer dst; 581 struct pf_addr rt_addr; 582 u_int32_t rule; 583 u_int32_t anchor; 584 u_int32_t nat_rule; 585 u_int32_t creation; 586 u_int32_t expire; 587 u_int32_t packets[2][2]; 588 u_int32_t bytes[2][2]; 589 u_int32_t creatorid; 590 sa_family_t af; 591 u_int8_t proto; 592 u_int8_t direction; 593 u_int8_t __spare[2]; 594 u_int8_t log; 595 u_int8_t state_flags; 596 u_int8_t timeout; 597 u_int8_t sync_flags; 598 u_int8_t updates; 599 } __packed; 600 601 #ifdef _KERNEL 602 /* pfsync */ 603 typedef int pfsync_state_import_t(struct pfsync_state *, u_int8_t); 604 typedef void pfsync_insert_state_t(struct pf_state *); 605 typedef void pfsync_update_state_t(struct pf_state *); 606 typedef void pfsync_delete_state_t(struct pf_state *); 607 typedef void pfsync_clear_states_t(u_int32_t, const char *); 608 typedef int pfsync_defer_t(struct pf_state *, struct mbuf *); 609 typedef void pfsync_detach_ifnet_t(struct ifnet *); 610 611 VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr); 612 #define V_pfsync_state_import_ptr VNET(pfsync_state_import_ptr) 613 VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr); 614 #define V_pfsync_insert_state_ptr VNET(pfsync_insert_state_ptr) 615 VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr); 616 #define V_pfsync_update_state_ptr VNET(pfsync_update_state_ptr) 617 VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr); 618 #define V_pfsync_delete_state_ptr VNET(pfsync_delete_state_ptr) 619 VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr); 620 #define V_pfsync_clear_states_ptr VNET(pfsync_clear_states_ptr) 621 VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr); 622 #define V_pfsync_defer_ptr VNET(pfsync_defer_ptr) 623 extern pfsync_detach_ifnet_t *pfsync_detach_ifnet_ptr; 624 625 void pfsync_state_export(struct pfsync_state *, 626 struct pf_state *); 627 628 /* pflog */ 629 struct pf_kruleset; 630 struct pf_pdesc; 631 typedef int pflog_packet_t(struct pfi_kkif *, struct mbuf *, sa_family_t, 632 u_int8_t, u_int8_t, struct pf_krule *, struct pf_krule *, 633 struct pf_kruleset *, struct pf_pdesc *, int); 634 extern pflog_packet_t *pflog_packet_ptr; 635 636 #endif /* _KERNEL */ 637 638 #define PFSYNC_FLAG_SRCNODE 0x04 639 #define PFSYNC_FLAG_NATSRCNODE 0x08 640 641 /* for copies to/from network byte order */ 642 /* ioctl interface also uses network byte order */ 643 #define pf_state_peer_hton(s,d) do { \ 644 (d)->seqlo = htonl((s)->seqlo); \ 645 (d)->seqhi = htonl((s)->seqhi); \ 646 (d)->seqdiff = htonl((s)->seqdiff); \ 647 (d)->max_win = htons((s)->max_win); \ 648 (d)->mss = htons((s)->mss); \ 649 (d)->state = (s)->state; \ 650 (d)->wscale = (s)->wscale; \ 651 if ((s)->scrub) { \ 652 (d)->scrub.pfss_flags = \ 653 htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP); \ 654 (d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl; \ 655 (d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\ 656 (d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID; \ 657 } \ 658 } while (0) 659 660 #define pf_state_peer_ntoh(s,d) do { \ 661 (d)->seqlo = ntohl((s)->seqlo); \ 662 (d)->seqhi = ntohl((s)->seqhi); \ 663 (d)->seqdiff = ntohl((s)->seqdiff); \ 664 (d)->max_win = ntohs((s)->max_win); \ 665 (d)->mss = ntohs((s)->mss); \ 666 (d)->state = (s)->state; \ 667 (d)->wscale = (s)->wscale; \ 668 if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && \ 669 (d)->scrub != NULL) { \ 670 (d)->scrub->pfss_flags = \ 671 ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP; \ 672 (d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl; \ 673 (d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\ 674 } \ 675 } while (0) 676 677 #define pf_state_counter_hton(s,d) do { \ 678 d[0] = htonl((s>>32)&0xffffffff); \ 679 d[1] = htonl(s&0xffffffff); \ 680 } while (0) 681 682 #define pf_state_counter_from_pfsync(s) \ 683 (((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1])) 684 685 #define pf_state_counter_ntoh(s,d) do { \ 686 d = ntohl(s[0]); \ 687 d = d<<32; \ 688 d += ntohl(s[1]); \ 689 } while (0) 690 691 TAILQ_HEAD(pf_krulequeue, pf_krule); 692 693 struct pf_kanchor; 694 695 struct pf_kruleset { 696 struct { 697 struct pf_krulequeue queues[2]; 698 struct { 699 struct pf_krulequeue *ptr; 700 struct pf_krule **ptr_array; 701 u_int32_t rcount; 702 u_int32_t ticket; 703 int open; 704 } active, inactive; 705 } rules[PF_RULESET_MAX]; 706 struct pf_kanchor *anchor; 707 u_int32_t tticket; 708 int tables; 709 int topen; 710 }; 711 712 RB_HEAD(pf_kanchor_global, pf_kanchor); 713 RB_HEAD(pf_kanchor_node, pf_kanchor); 714 struct pf_kanchor { 715 RB_ENTRY(pf_kanchor) entry_global; 716 RB_ENTRY(pf_kanchor) entry_node; 717 struct pf_kanchor *parent; 718 struct pf_kanchor_node children; 719 char name[PF_ANCHOR_NAME_SIZE]; 720 char path[MAXPATHLEN]; 721 struct pf_kruleset ruleset; 722 int refcnt; /* anchor rules */ 723 int match; /* XXX: used for pfctl black magic */ 724 }; 725 RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare); 726 RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare); 727 728 #define PF_RESERVED_ANCHOR "_pf" 729 730 #define PFR_TFLAG_PERSIST 0x00000001 731 #define PFR_TFLAG_CONST 0x00000002 732 #define PFR_TFLAG_ACTIVE 0x00000004 733 #define PFR_TFLAG_INACTIVE 0x00000008 734 #define PFR_TFLAG_REFERENCED 0x00000010 735 #define PFR_TFLAG_REFDANCHOR 0x00000020 736 #define PFR_TFLAG_COUNTERS 0x00000040 737 /* Adjust masks below when adding flags. */ 738 #define PFR_TFLAG_USRMASK (PFR_TFLAG_PERSIST | \ 739 PFR_TFLAG_CONST | \ 740 PFR_TFLAG_COUNTERS) 741 #define PFR_TFLAG_SETMASK (PFR_TFLAG_ACTIVE | \ 742 PFR_TFLAG_INACTIVE | \ 743 PFR_TFLAG_REFERENCED | \ 744 PFR_TFLAG_REFDANCHOR) 745 #define PFR_TFLAG_ALLMASK (PFR_TFLAG_PERSIST | \ 746 PFR_TFLAG_CONST | \ 747 PFR_TFLAG_ACTIVE | \ 748 PFR_TFLAG_INACTIVE | \ 749 PFR_TFLAG_REFERENCED | \ 750 PFR_TFLAG_REFDANCHOR | \ 751 PFR_TFLAG_COUNTERS) 752 753 struct pf_kanchor_stackframe; 754 755 struct pfr_table { 756 char pfrt_anchor[MAXPATHLEN]; 757 char pfrt_name[PF_TABLE_NAME_SIZE]; 758 u_int32_t pfrt_flags; 759 u_int8_t pfrt_fback; 760 }; 761 762 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED, 763 PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE, 764 PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX }; 765 766 struct pfr_addr { 767 union { 768 struct in_addr _pfra_ip4addr; 769 struct in6_addr _pfra_ip6addr; 770 } pfra_u; 771 u_int8_t pfra_af; 772 u_int8_t pfra_net; 773 u_int8_t pfra_not; 774 u_int8_t pfra_fback; 775 }; 776 #define pfra_ip4addr pfra_u._pfra_ip4addr 777 #define pfra_ip6addr pfra_u._pfra_ip6addr 778 779 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX }; 780 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX }; 781 enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX }; 782 #define PFR_NUM_COUNTERS (PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX) 783 #define PFR_OP_XPASS PFR_OP_ADDR_MAX 784 785 struct pfr_astats { 786 struct pfr_addr pfras_a; 787 u_int64_t pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 788 u_int64_t pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 789 long pfras_tzero; 790 }; 791 792 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX }; 793 794 struct pfr_tstats { 795 struct pfr_table pfrts_t; 796 u_int64_t pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 797 u_int64_t pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 798 u_int64_t pfrts_match; 799 u_int64_t pfrts_nomatch; 800 long pfrts_tzero; 801 int pfrts_cnt; 802 int pfrts_refcnt[PFR_REFCNT_MAX]; 803 }; 804 805 struct pfr_ktstats { 806 struct pfr_table pfrts_t; 807 counter_u64_t pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 808 counter_u64_t pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 809 counter_u64_t pfrkts_match; 810 counter_u64_t pfrkts_nomatch; 811 long pfrkts_tzero; 812 int pfrkts_cnt; 813 int pfrkts_refcnt[PFR_REFCNT_MAX]; 814 }; 815 #define pfrts_name pfrts_t.pfrt_name 816 #define pfrts_flags pfrts_t.pfrt_flags 817 818 #ifndef _SOCKADDR_UNION_DEFINED 819 #define _SOCKADDR_UNION_DEFINED 820 union sockaddr_union { 821 struct sockaddr sa; 822 struct sockaddr_in sin; 823 struct sockaddr_in6 sin6; 824 }; 825 #endif /* _SOCKADDR_UNION_DEFINED */ 826 827 struct pfr_kcounters { 828 counter_u64_t pfrkc_counters; 829 long pfrkc_tzero; 830 }; 831 #define pfr_kentry_counter(kc, dir, op, t) \ 832 ((kc)->pfrkc_counters + \ 833 (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t)) 834 835 #ifdef _KERNEL 836 SLIST_HEAD(pfr_kentryworkq, pfr_kentry); 837 struct pfr_kentry { 838 struct radix_node pfrke_node[2]; 839 union sockaddr_union pfrke_sa; 840 SLIST_ENTRY(pfr_kentry) pfrke_workq; 841 struct pfr_kcounters pfrke_counters; 842 u_int8_t pfrke_af; 843 u_int8_t pfrke_net; 844 u_int8_t pfrke_not; 845 u_int8_t pfrke_mark; 846 }; 847 848 SLIST_HEAD(pfr_ktableworkq, pfr_ktable); 849 RB_HEAD(pfr_ktablehead, pfr_ktable); 850 struct pfr_ktable { 851 struct pfr_ktstats pfrkt_kts; 852 RB_ENTRY(pfr_ktable) pfrkt_tree; 853 SLIST_ENTRY(pfr_ktable) pfrkt_workq; 854 struct radix_node_head *pfrkt_ip4; 855 struct radix_node_head *pfrkt_ip6; 856 struct pfr_ktable *pfrkt_shadow; 857 struct pfr_ktable *pfrkt_root; 858 struct pf_kruleset *pfrkt_rs; 859 long pfrkt_larg; 860 int pfrkt_nflags; 861 }; 862 #define pfrkt_t pfrkt_kts.pfrts_t 863 #define pfrkt_name pfrkt_t.pfrt_name 864 #define pfrkt_anchor pfrkt_t.pfrt_anchor 865 #define pfrkt_ruleset pfrkt_t.pfrt_ruleset 866 #define pfrkt_flags pfrkt_t.pfrt_flags 867 #define pfrkt_cnt pfrkt_kts.pfrkts_cnt 868 #define pfrkt_refcnt pfrkt_kts.pfrkts_refcnt 869 #define pfrkt_packets pfrkt_kts.pfrkts_packets 870 #define pfrkt_bytes pfrkt_kts.pfrkts_bytes 871 #define pfrkt_match pfrkt_kts.pfrkts_match 872 #define pfrkt_nomatch pfrkt_kts.pfrkts_nomatch 873 #define pfrkt_tzero pfrkt_kts.pfrkts_tzero 874 #endif 875 876 #ifdef _KERNEL 877 struct pfi_kkif { 878 char pfik_name[IFNAMSIZ]; 879 union { 880 RB_ENTRY(pfi_kkif) _pfik_tree; 881 LIST_ENTRY(pfi_kkif) _pfik_list; 882 } _pfik_glue; 883 #define pfik_tree _pfik_glue._pfik_tree 884 #define pfik_list _pfik_glue._pfik_list 885 counter_u64_t pfik_packets[2][2][2]; 886 counter_u64_t pfik_bytes[2][2][2]; 887 u_int32_t pfik_tzero; 888 u_int pfik_flags; 889 struct ifnet *pfik_ifp; 890 struct ifg_group *pfik_group; 891 u_int pfik_rulerefs; 892 TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs; 893 }; 894 #endif 895 896 #define PFI_IFLAG_REFS 0x0001 /* has state references */ 897 #define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */ 898 899 #ifdef _KERNEL 900 struct pf_pdesc { 901 struct { 902 int done; 903 uid_t uid; 904 gid_t gid; 905 } lookup; 906 u_int64_t tot_len; /* Make Mickey money */ 907 union { 908 struct tcphdr *tcp; 909 struct udphdr *udp; 910 struct icmp *icmp; 911 #ifdef INET6 912 struct icmp6_hdr *icmp6; 913 #endif /* INET6 */ 914 void *any; 915 } hdr; 916 917 struct pf_krule *nat_rule; /* nat/rdr rule applied to packet */ 918 struct pf_addr *src; /* src address */ 919 struct pf_addr *dst; /* dst address */ 920 u_int16_t *sport; 921 u_int16_t *dport; 922 struct pf_mtag *pf_mtag; 923 924 u_int32_t p_len; /* total length of payload */ 925 926 u_int16_t *ip_sum; 927 u_int16_t *proto_sum; 928 u_int16_t flags; /* Let SCRUB trigger behavior in 929 * state code. Easier than tags */ 930 #define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */ 931 #define PFDESC_IP_REAS 0x0002 /* IP frags would've been reassembled */ 932 sa_family_t af; 933 u_int8_t proto; 934 u_int8_t tos; 935 u_int8_t dir; /* direction */ 936 u_int8_t sidx; /* key index for source */ 937 u_int8_t didx; /* key index for destination */ 938 }; 939 #endif 940 941 /* flags for RDR options */ 942 #define PF_DPORT_RANGE 0x01 /* Dest port uses range */ 943 #define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */ 944 945 /* UDP state enumeration */ 946 #define PFUDPS_NO_TRAFFIC 0 947 #define PFUDPS_SINGLE 1 948 #define PFUDPS_MULTIPLE 2 949 950 #define PFUDPS_NSTATES 3 /* number of state levels */ 951 952 #define PFUDPS_NAMES { \ 953 "NO_TRAFFIC", \ 954 "SINGLE", \ 955 "MULTIPLE", \ 956 NULL \ 957 } 958 959 /* Other protocol state enumeration */ 960 #define PFOTHERS_NO_TRAFFIC 0 961 #define PFOTHERS_SINGLE 1 962 #define PFOTHERS_MULTIPLE 2 963 964 #define PFOTHERS_NSTATES 3 /* number of state levels */ 965 966 #define PFOTHERS_NAMES { \ 967 "NO_TRAFFIC", \ 968 "SINGLE", \ 969 "MULTIPLE", \ 970 NULL \ 971 } 972 973 #define ACTION_SET(a, x) \ 974 do { \ 975 if ((a) != NULL) \ 976 *(a) = (x); \ 977 } while (0) 978 979 #define REASON_SET(a, x) \ 980 do { \ 981 if ((a) != NULL) \ 982 *(a) = (x); \ 983 if (x < PFRES_MAX) \ 984 counter_u64_add(V_pf_status.counters[x], 1); \ 985 } while (0) 986 987 struct pf_kstatus { 988 counter_u64_t counters[PFRES_MAX]; /* reason for passing/dropping */ 989 counter_u64_t lcounters[LCNT_MAX]; /* limit counters */ 990 counter_u64_t fcounters[FCNT_MAX]; /* state operation counters */ 991 counter_u64_t scounters[SCNT_MAX]; /* src_node operation counters */ 992 uint32_t states; 993 uint32_t src_nodes; 994 uint32_t running; 995 uint32_t since; 996 uint32_t debug; 997 uint32_t hostid; 998 char ifname[IFNAMSIZ]; 999 uint8_t pf_chksum[PF_MD5_DIGEST_LENGTH]; 1000 bool keep_counters; 1001 }; 1002 1003 struct pf_divert { 1004 union { 1005 struct in_addr ipv4; 1006 struct in6_addr ipv6; 1007 } addr; 1008 u_int16_t port; 1009 }; 1010 1011 #define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */ 1012 #define PFR_KENTRY_HIWAT 200000 /* Number of table entries */ 1013 1014 /* 1015 * Limit the length of the fragment queue traversal. Remember 1016 * search entry points based on the fragment offset. 1017 */ 1018 #define PF_FRAG_ENTRY_POINTS 16 1019 1020 /* 1021 * The number of entries in the fragment queue must be limited 1022 * to avoid DoS by linear seaching. Instead of a global limit, 1023 * use a limit per entry point. For large packets these sum up. 1024 */ 1025 #define PF_FRAG_ENTRY_LIMIT 64 1026 1027 /* 1028 * ioctl parameter structures 1029 */ 1030 1031 struct pfioc_pooladdr { 1032 u_int32_t action; 1033 u_int32_t ticket; 1034 u_int32_t nr; 1035 u_int32_t r_num; 1036 u_int8_t r_action; 1037 u_int8_t r_last; 1038 u_int8_t af; 1039 char anchor[MAXPATHLEN]; 1040 struct pf_pooladdr addr; 1041 }; 1042 1043 struct pfioc_rule { 1044 u_int32_t action; 1045 u_int32_t ticket; 1046 u_int32_t pool_ticket; 1047 u_int32_t nr; 1048 char anchor[MAXPATHLEN]; 1049 char anchor_call[MAXPATHLEN]; 1050 struct pf_rule rule; 1051 }; 1052 1053 struct pfioc_natlook { 1054 struct pf_addr saddr; 1055 struct pf_addr daddr; 1056 struct pf_addr rsaddr; 1057 struct pf_addr rdaddr; 1058 u_int16_t sport; 1059 u_int16_t dport; 1060 u_int16_t rsport; 1061 u_int16_t rdport; 1062 sa_family_t af; 1063 u_int8_t proto; 1064 u_int8_t direction; 1065 }; 1066 1067 struct pfioc_state { 1068 struct pfsync_state state; 1069 }; 1070 1071 struct pfioc_src_node_kill { 1072 sa_family_t psnk_af; 1073 struct pf_rule_addr psnk_src; 1074 struct pf_rule_addr psnk_dst; 1075 u_int psnk_killed; 1076 }; 1077 1078 #ifdef _KERNEL 1079 struct pf_kstate_kill { 1080 struct pf_state_cmp psk_pfcmp; 1081 sa_family_t psk_af; 1082 int psk_proto; 1083 struct pf_rule_addr psk_src; 1084 struct pf_rule_addr psk_dst; 1085 struct pf_rule_addr psk_rt_addr; 1086 char psk_ifname[IFNAMSIZ]; 1087 char psk_label[PF_RULE_LABEL_SIZE]; 1088 u_int psk_killed; 1089 bool psk_kill_match; 1090 }; 1091 #endif 1092 1093 struct pfioc_state_kill { 1094 struct pf_state_cmp psk_pfcmp; 1095 sa_family_t psk_af; 1096 int psk_proto; 1097 struct pf_rule_addr psk_src; 1098 struct pf_rule_addr psk_dst; 1099 char psk_ifname[IFNAMSIZ]; 1100 char psk_label[PF_RULE_LABEL_SIZE]; 1101 u_int psk_killed; 1102 }; 1103 1104 struct pfioc_states { 1105 int ps_len; 1106 union { 1107 caddr_t psu_buf; 1108 struct pfsync_state *psu_states; 1109 } ps_u; 1110 #define ps_buf ps_u.psu_buf 1111 #define ps_states ps_u.psu_states 1112 }; 1113 1114 struct pfioc_src_nodes { 1115 int psn_len; 1116 union { 1117 caddr_t psu_buf; 1118 struct pf_src_node *psu_src_nodes; 1119 } psn_u; 1120 #define psn_buf psn_u.psu_buf 1121 #define psn_src_nodes psn_u.psu_src_nodes 1122 }; 1123 1124 struct pfioc_if { 1125 char ifname[IFNAMSIZ]; 1126 }; 1127 1128 struct pfioc_tm { 1129 int timeout; 1130 int seconds; 1131 }; 1132 1133 struct pfioc_limit { 1134 int index; 1135 unsigned limit; 1136 }; 1137 1138 struct pfioc_altq_v0 { 1139 u_int32_t action; 1140 u_int32_t ticket; 1141 u_int32_t nr; 1142 struct pf_altq_v0 altq; 1143 }; 1144 1145 struct pfioc_altq_v1 { 1146 u_int32_t action; 1147 u_int32_t ticket; 1148 u_int32_t nr; 1149 /* 1150 * Placed here so code that only uses the above parameters can be 1151 * written entirely in terms of the v0 or v1 type. 1152 */ 1153 u_int32_t version; 1154 struct pf_altq_v1 altq; 1155 }; 1156 1157 /* 1158 * Latest version of struct pfioc_altq_vX. This must move in lock-step with 1159 * the latest version of struct pf_altq_vX as it has that struct as a 1160 * member. 1161 */ 1162 #define PFIOC_ALTQ_VERSION PF_ALTQ_VERSION 1163 1164 struct pfioc_qstats_v0 { 1165 u_int32_t ticket; 1166 u_int32_t nr; 1167 void *buf; 1168 int nbytes; 1169 u_int8_t scheduler; 1170 }; 1171 1172 struct pfioc_qstats_v1 { 1173 u_int32_t ticket; 1174 u_int32_t nr; 1175 void *buf; 1176 int nbytes; 1177 u_int8_t scheduler; 1178 /* 1179 * Placed here so code that only uses the above parameters can be 1180 * written entirely in terms of the v0 or v1 type. 1181 */ 1182 u_int32_t version; /* Requested version of stats struct */ 1183 }; 1184 1185 /* Latest version of struct pfioc_qstats_vX */ 1186 #define PFIOC_QSTATS_VERSION 1 1187 1188 struct pfioc_ruleset { 1189 u_int32_t nr; 1190 char path[MAXPATHLEN]; 1191 char name[PF_ANCHOR_NAME_SIZE]; 1192 }; 1193 1194 #define PF_RULESET_ALTQ (PF_RULESET_MAX) 1195 #define PF_RULESET_TABLE (PF_RULESET_MAX+1) 1196 struct pfioc_trans { 1197 int size; /* number of elements */ 1198 int esize; /* size of each element in bytes */ 1199 struct pfioc_trans_e { 1200 int rs_num; 1201 char anchor[MAXPATHLEN]; 1202 u_int32_t ticket; 1203 } *array; 1204 }; 1205 1206 #define PFR_FLAG_ATOMIC 0x00000001 /* unused */ 1207 #define PFR_FLAG_DUMMY 0x00000002 1208 #define PFR_FLAG_FEEDBACK 0x00000004 1209 #define PFR_FLAG_CLSTATS 0x00000008 1210 #define PFR_FLAG_ADDRSTOO 0x00000010 1211 #define PFR_FLAG_REPLACE 0x00000020 1212 #define PFR_FLAG_ALLRSETS 0x00000040 1213 #define PFR_FLAG_ALLMASK 0x0000007F 1214 #ifdef _KERNEL 1215 #define PFR_FLAG_USERIOCTL 0x10000000 1216 #endif 1217 1218 struct pfioc_table { 1219 struct pfr_table pfrio_table; 1220 void *pfrio_buffer; 1221 int pfrio_esize; 1222 int pfrio_size; 1223 int pfrio_size2; 1224 int pfrio_nadd; 1225 int pfrio_ndel; 1226 int pfrio_nchange; 1227 int pfrio_flags; 1228 u_int32_t pfrio_ticket; 1229 }; 1230 #define pfrio_exists pfrio_nadd 1231 #define pfrio_nzero pfrio_nadd 1232 #define pfrio_nmatch pfrio_nadd 1233 #define pfrio_naddr pfrio_size2 1234 #define pfrio_setflag pfrio_size2 1235 #define pfrio_clrflag pfrio_nadd 1236 1237 struct pfioc_iface { 1238 char pfiio_name[IFNAMSIZ]; 1239 void *pfiio_buffer; 1240 int pfiio_esize; 1241 int pfiio_size; 1242 int pfiio_nzero; 1243 int pfiio_flags; 1244 }; 1245 1246 /* 1247 * ioctl operations 1248 */ 1249 1250 #define DIOCSTART _IO ('D', 1) 1251 #define DIOCSTOP _IO ('D', 2) 1252 #define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule) 1253 #define DIOCADDRULENV _IOWR('D', 4, struct pfioc_nv) 1254 #define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule) 1255 #define DIOCGETRULE _IOWR('D', 7, struct pfioc_rule) 1256 #define DIOCGETRULENV _IOWR('D', 7, struct pfioc_nv) 1257 /* XXX cut 8 - 17 */ 1258 #define DIOCCLRSTATES _IOWR('D', 18, struct pfioc_state_kill) 1259 #define DIOCCLRSTATESNV _IOWR('D', 18, struct pfioc_nv) 1260 #define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state) 1261 #define DIOCGETSTATENV _IOWR('D', 19, struct pfioc_nv) 1262 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if) 1263 #define DIOCGETSTATUS _IOWR('D', 21, struct pf_status) 1264 #define DIOCCLRSTATUS _IO ('D', 22) 1265 #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook) 1266 #define DIOCSETDEBUG _IOWR('D', 24, u_int32_t) 1267 #define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states) 1268 #define DIOCGETSTATESNV _IOWR('D', 25, struct pfioc_nv) 1269 #define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule) 1270 /* XXX cut 26 - 28 */ 1271 #define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm) 1272 #define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm) 1273 #define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state) 1274 #define DIOCCLRRULECTRS _IO ('D', 38) 1275 #define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit) 1276 #define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit) 1277 #define DIOCKILLSTATES _IOWR('D', 41, struct pfioc_state_kill) 1278 #define DIOCKILLSTATESNV _IOWR('D', 41, struct pfioc_nv) 1279 #define DIOCSTARTALTQ _IO ('D', 42) 1280 #define DIOCSTOPALTQ _IO ('D', 43) 1281 #define DIOCADDALTQV0 _IOWR('D', 45, struct pfioc_altq_v0) 1282 #define DIOCADDALTQV1 _IOWR('D', 45, struct pfioc_altq_v1) 1283 #define DIOCGETALTQSV0 _IOWR('D', 47, struct pfioc_altq_v0) 1284 #define DIOCGETALTQSV1 _IOWR('D', 47, struct pfioc_altq_v1) 1285 #define DIOCGETALTQV0 _IOWR('D', 48, struct pfioc_altq_v0) 1286 #define DIOCGETALTQV1 _IOWR('D', 48, struct pfioc_altq_v1) 1287 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0) 1288 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1) 1289 #define DIOCGETQSTATSV0 _IOWR('D', 50, struct pfioc_qstats_v0) 1290 #define DIOCGETQSTATSV1 _IOWR('D', 50, struct pfioc_qstats_v1) 1291 #define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr) 1292 #define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr) 1293 #define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr) 1294 #define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr) 1295 #define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr) 1296 /* XXX cut 55 - 57 */ 1297 #define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset) 1298 #define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset) 1299 #define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table) 1300 #define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table) 1301 #define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table) 1302 #define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table) 1303 #define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table) 1304 #define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table) 1305 #define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table) 1306 #define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table) 1307 #define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table) 1308 #define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table) 1309 #define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table) 1310 #define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table) 1311 #define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table) 1312 #define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table) 1313 #define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table) 1314 #define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table) 1315 #define DIOCOSFPFLUSH _IO('D', 78) 1316 #define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl) 1317 #define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl) 1318 #define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans) 1319 #define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans) 1320 #define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans) 1321 #define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes) 1322 #define DIOCCLRSRCNODES _IO('D', 85) 1323 #define DIOCSETHOSTID _IOWR('D', 86, u_int32_t) 1324 #define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface) 1325 #define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface) 1326 #define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface) 1327 #define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill) 1328 #define DIOCKEEPCOUNTERS _IOWR('D', 92, struct pfioc_nv) 1329 1330 struct pf_ifspeed_v0 { 1331 char ifname[IFNAMSIZ]; 1332 u_int32_t baudrate; 1333 }; 1334 1335 struct pf_ifspeed_v1 { 1336 char ifname[IFNAMSIZ]; 1337 u_int32_t baudrate32; 1338 /* layout identical to struct pf_ifspeed_v0 up to this point */ 1339 u_int64_t baudrate; 1340 }; 1341 1342 /* Latest version of struct pf_ifspeed_vX */ 1343 #define PF_IFSPEED_VERSION 1 1344 1345 #define DIOCGIFSPEEDV0 _IOWR('D', 92, struct pf_ifspeed_v0) 1346 #define DIOCGIFSPEEDV1 _IOWR('D', 92, struct pf_ifspeed_v1) 1347 1348 /* 1349 * Compatibility and convenience macros 1350 */ 1351 #ifndef _KERNEL 1352 #ifdef PFIOC_USE_LATEST 1353 /* 1354 * Maintaining in-tree consumers of the ioctl interface is easier when that 1355 * code can be written in terms old names that refer to the latest interface 1356 * version as that reduces the required changes in the consumers to those 1357 * that are functionally necessary to accommodate a new interface version. 1358 */ 1359 #define pfioc_altq __CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION) 1360 #define pfioc_qstats __CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION) 1361 #define pf_ifspeed __CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION) 1362 1363 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION) 1364 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION) 1365 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION) 1366 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION) 1367 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION) 1368 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION) 1369 #else 1370 /* 1371 * When building out-of-tree code that is written for the old interface, 1372 * such as may exist in ports for example, resolve the old struct tags and 1373 * ioctl command names to the v0 versions. 1374 */ 1375 #define pfioc_altq __CONCAT(pfioc_altq_v, 0) 1376 #define pfioc_qstats __CONCAT(pfioc_qstats_v, 0) 1377 #define pf_ifspeed __CONCAT(pf_ifspeed_v, 0) 1378 1379 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, 0) 1380 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, 0) 1381 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, 0) 1382 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, 0) 1383 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, 0) 1384 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, 0) 1385 #endif /* PFIOC_USE_LATEST */ 1386 #endif /* _KERNEL */ 1387 1388 #ifdef _KERNEL 1389 LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node); 1390 struct pf_srchash { 1391 struct pf_ksrc_node_list nodes; 1392 struct mtx lock; 1393 }; 1394 1395 struct pf_keyhash { 1396 LIST_HEAD(, pf_state_key) keys; 1397 struct mtx lock; 1398 }; 1399 1400 struct pf_idhash { 1401 LIST_HEAD(, pf_state) states; 1402 struct mtx lock; 1403 }; 1404 1405 extern u_long pf_hashmask; 1406 extern u_long pf_srchashmask; 1407 #define PF_HASHSIZ (131072) 1408 #define PF_SRCHASHSIZ (PF_HASHSIZ/4) 1409 VNET_DECLARE(struct pf_keyhash *, pf_keyhash); 1410 VNET_DECLARE(struct pf_idhash *, pf_idhash); 1411 #define V_pf_keyhash VNET(pf_keyhash) 1412 #define V_pf_idhash VNET(pf_idhash) 1413 VNET_DECLARE(struct pf_srchash *, pf_srchash); 1414 #define V_pf_srchash VNET(pf_srchash) 1415 1416 #define PF_IDHASH(s) (be64toh((s)->id) % (pf_hashmask + 1)) 1417 1418 VNET_DECLARE(void *, pf_swi_cookie); 1419 #define V_pf_swi_cookie VNET(pf_swi_cookie) 1420 VNET_DECLARE(struct intr_event *, pf_swi_ie); 1421 #define V_pf_swi_ie VNET(pf_swi_ie) 1422 1423 VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]); 1424 #define V_pf_stateid VNET(pf_stateid) 1425 1426 TAILQ_HEAD(pf_altqqueue, pf_altq); 1427 VNET_DECLARE(struct pf_altqqueue, pf_altqs[4]); 1428 #define V_pf_altqs VNET(pf_altqs) 1429 VNET_DECLARE(struct pf_kpalist, pf_pabuf); 1430 #define V_pf_pabuf VNET(pf_pabuf) 1431 1432 VNET_DECLARE(u_int32_t, ticket_altqs_active); 1433 #define V_ticket_altqs_active VNET(ticket_altqs_active) 1434 VNET_DECLARE(u_int32_t, ticket_altqs_inactive); 1435 #define V_ticket_altqs_inactive VNET(ticket_altqs_inactive) 1436 VNET_DECLARE(int, altqs_inactive_open); 1437 #define V_altqs_inactive_open VNET(altqs_inactive_open) 1438 VNET_DECLARE(u_int32_t, ticket_pabuf); 1439 #define V_ticket_pabuf VNET(ticket_pabuf) 1440 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_active); 1441 #define V_pf_altqs_active VNET(pf_altqs_active) 1442 VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_active); 1443 #define V_pf_altq_ifs_active VNET(pf_altq_ifs_active) 1444 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_inactive); 1445 #define V_pf_altqs_inactive VNET(pf_altqs_inactive) 1446 VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_inactive); 1447 #define V_pf_altq_ifs_inactive VNET(pf_altq_ifs_inactive) 1448 1449 VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules); 1450 #define V_pf_unlinked_rules VNET(pf_unlinked_rules) 1451 1452 void pf_initialize(void); 1453 void pf_mtag_initialize(void); 1454 void pf_mtag_cleanup(void); 1455 void pf_cleanup(void); 1456 1457 struct pf_mtag *pf_get_mtag(struct mbuf *); 1458 1459 extern void pf_calc_skip_steps(struct pf_krulequeue *); 1460 #ifdef ALTQ 1461 extern void pf_altq_ifnet_event(struct ifnet *, int); 1462 #endif 1463 VNET_DECLARE(uma_zone_t, pf_state_z); 1464 #define V_pf_state_z VNET(pf_state_z) 1465 VNET_DECLARE(uma_zone_t, pf_state_key_z); 1466 #define V_pf_state_key_z VNET(pf_state_key_z) 1467 VNET_DECLARE(uma_zone_t, pf_state_scrub_z); 1468 #define V_pf_state_scrub_z VNET(pf_state_scrub_z) 1469 1470 extern void pf_purge_thread(void *); 1471 extern void pf_unload_vnet_purge(void); 1472 extern void pf_intr(void *); 1473 extern void pf_purge_expired_src_nodes(void); 1474 1475 extern int pf_unlink_state(struct pf_state *, u_int); 1476 #define PF_ENTER_LOCKED 0x00000001 1477 #define PF_RETURN_LOCKED 0x00000002 1478 extern int pf_state_insert(struct pfi_kkif *, 1479 struct pfi_kkif *, 1480 struct pf_state_key *, 1481 struct pf_state_key *, 1482 struct pf_state *); 1483 extern void pf_free_state(struct pf_state *); 1484 1485 static __inline void 1486 pf_ref_state(struct pf_state *s) 1487 { 1488 1489 refcount_acquire(&s->refs); 1490 } 1491 1492 static __inline int 1493 pf_release_state(struct pf_state *s) 1494 { 1495 1496 if (refcount_release(&s->refs)) { 1497 pf_free_state(s); 1498 return (1); 1499 } else 1500 return (0); 1501 } 1502 1503 extern struct pf_state *pf_find_state_byid(uint64_t, uint32_t); 1504 extern struct pf_state *pf_find_state_all(struct pf_state_key_cmp *, 1505 u_int, int *); 1506 extern struct pf_ksrc_node *pf_find_src_node(struct pf_addr *, 1507 struct pf_krule *, sa_family_t, int); 1508 extern void pf_unlink_src_node(struct pf_ksrc_node *); 1509 extern u_int pf_free_src_nodes(struct pf_ksrc_node_list *); 1510 extern void pf_print_state(struct pf_state *); 1511 extern void pf_print_flags(u_int8_t); 1512 extern u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t, 1513 u_int8_t); 1514 extern u_int16_t pf_proto_cksum_fixup(struct mbuf *, u_int16_t, 1515 u_int16_t, u_int16_t, u_int8_t); 1516 1517 VNET_DECLARE(struct ifnet *, sync_ifp); 1518 #define V_sync_ifp VNET(sync_ifp); 1519 VNET_DECLARE(struct pf_krule, pf_default_rule); 1520 #define V_pf_default_rule VNET(pf_default_rule) 1521 extern void pf_addrcpy(struct pf_addr *, struct pf_addr *, 1522 u_int8_t); 1523 void pf_free_rule(struct pf_krule *); 1524 1525 #ifdef INET 1526 int pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *); 1527 int pf_normalize_ip(struct mbuf **, int, struct pfi_kkif *, u_short *, 1528 struct pf_pdesc *); 1529 #endif /* INET */ 1530 1531 #ifdef INET6 1532 int pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *); 1533 int pf_normalize_ip6(struct mbuf **, int, struct pfi_kkif *, u_short *, 1534 struct pf_pdesc *); 1535 void pf_poolmask(struct pf_addr *, struct pf_addr*, 1536 struct pf_addr *, struct pf_addr *, u_int8_t); 1537 void pf_addr_inc(struct pf_addr *, sa_family_t); 1538 int pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *); 1539 #endif /* INET6 */ 1540 1541 u_int32_t pf_new_isn(struct pf_state *); 1542 void *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *, 1543 sa_family_t); 1544 void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t); 1545 void pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t, 1546 u_int8_t); 1547 void pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t); 1548 void pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t, 1549 bool, u_int8_t); 1550 void pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t, 1551 bool, u_int8_t); 1552 void pf_send_deferred_syn(struct pf_state *); 1553 int pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *, 1554 struct pf_addr *, sa_family_t); 1555 int pf_match_addr_range(struct pf_addr *, struct pf_addr *, 1556 struct pf_addr *, sa_family_t); 1557 int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t); 1558 1559 void pf_normalize_init(void); 1560 void pf_normalize_cleanup(void); 1561 int pf_normalize_tcp(int, struct pfi_kkif *, struct mbuf *, int, int, void *, 1562 struct pf_pdesc *); 1563 void pf_normalize_tcp_cleanup(struct pf_state *); 1564 int pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *, 1565 struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *); 1566 int pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *, 1567 u_short *, struct tcphdr *, struct pf_state *, 1568 struct pf_state_peer *, struct pf_state_peer *, int *); 1569 u_int32_t 1570 pf_state_expires(const struct pf_state *); 1571 void pf_purge_expired_fragments(void); 1572 void pf_purge_fragments(uint32_t); 1573 int pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *, 1574 int); 1575 int pf_socket_lookup(int, struct pf_pdesc *, struct mbuf *); 1576 struct pf_state_key *pf_alloc_state_key(int); 1577 void pfr_initialize(void); 1578 void pfr_cleanup(void); 1579 int pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t); 1580 void pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t, 1581 u_int64_t, int, int, int); 1582 int pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t); 1583 void pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *); 1584 struct pfr_ktable * 1585 pfr_attach_table(struct pf_kruleset *, char *); 1586 void pfr_detach_table(struct pfr_ktable *); 1587 int pfr_clr_tables(struct pfr_table *, int *, int); 1588 int pfr_add_tables(struct pfr_table *, int, int *, int); 1589 int pfr_del_tables(struct pfr_table *, int, int *, int); 1590 int pfr_table_count(struct pfr_table *, int); 1591 int pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int); 1592 int pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int); 1593 int pfr_clr_tstats(struct pfr_table *, int, int *, int); 1594 int pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int); 1595 int pfr_clr_addrs(struct pfr_table *, int *, int); 1596 int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long); 1597 int pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 1598 int); 1599 int pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 1600 int); 1601 int pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 1602 int *, int *, int *, int, u_int32_t); 1603 int pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int); 1604 int pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int); 1605 int pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *, 1606 int); 1607 int pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 1608 int); 1609 int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int); 1610 int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int); 1611 int pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int); 1612 int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *, 1613 int *, u_int32_t, int); 1614 1615 MALLOC_DECLARE(PFI_MTYPE); 1616 VNET_DECLARE(struct pfi_kkif *, pfi_all); 1617 #define V_pfi_all VNET(pfi_all) 1618 1619 void pfi_initialize(void); 1620 void pfi_initialize_vnet(void); 1621 void pfi_cleanup(void); 1622 void pfi_cleanup_vnet(void); 1623 void pfi_kkif_ref(struct pfi_kkif *); 1624 void pfi_kkif_unref(struct pfi_kkif *); 1625 struct pfi_kkif *pfi_kkif_find(const char *); 1626 struct pfi_kkif *pfi_kkif_attach(struct pfi_kkif *, const char *); 1627 int pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *); 1628 void pfi_kkif_purge(void); 1629 int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *, 1630 sa_family_t); 1631 int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t); 1632 void pfi_dynaddr_remove(struct pfi_dynaddr *); 1633 void pfi_dynaddr_copyout(struct pf_addr_wrap *); 1634 void pfi_update_status(const char *, struct pf_status *); 1635 void pfi_get_ifaces(const char *, struct pfi_kif *, int *); 1636 int pfi_set_flags(const char *, int); 1637 int pfi_clear_flags(const char *, int); 1638 1639 int pf_match_tag(struct mbuf *, struct pf_krule *, int *, int); 1640 int pf_tag_packet(struct mbuf *, struct pf_pdesc *, int); 1641 int pf_addr_cmp(struct pf_addr *, struct pf_addr *, 1642 sa_family_t); 1643 void pf_qid2qname(u_int32_t, char *); 1644 1645 VNET_DECLARE(struct pf_kstatus, pf_status); 1646 #define V_pf_status VNET(pf_status) 1647 1648 struct pf_limit { 1649 uma_zone_t zone; 1650 u_int limit; 1651 }; 1652 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 1653 #define V_pf_limits VNET(pf_limits) 1654 1655 #endif /* _KERNEL */ 1656 1657 #ifdef _KERNEL 1658 VNET_DECLARE(struct pf_kanchor_global, pf_anchors); 1659 #define V_pf_anchors VNET(pf_anchors) 1660 VNET_DECLARE(struct pf_kanchor, pf_main_anchor); 1661 #define V_pf_main_anchor VNET(pf_main_anchor) 1662 #define pf_main_ruleset V_pf_main_anchor.ruleset 1663 1664 void pf_init_kruleset(struct pf_kruleset *); 1665 int pf_kanchor_setup(struct pf_krule *, 1666 const struct pf_kruleset *, const char *); 1667 int pf_kanchor_nvcopyout(const struct pf_kruleset *, 1668 const struct pf_krule *, nvlist_t *); 1669 int pf_kanchor_copyout(const struct pf_kruleset *, 1670 const struct pf_krule *, struct pfioc_rule *); 1671 void pf_kanchor_remove(struct pf_krule *); 1672 void pf_remove_if_empty_kruleset(struct pf_kruleset *); 1673 struct pf_kruleset *pf_find_kruleset(const char *); 1674 struct pf_kruleset *pf_find_or_create_kruleset(const char *); 1675 void pf_rs_initialize(void); 1676 1677 void pf_krule_free(struct pf_krule *); 1678 #endif 1679 1680 /* The fingerprint functions can be linked into userland programs (tcpdump) */ 1681 int pf_osfp_add(struct pf_osfp_ioctl *); 1682 #ifdef _KERNEL 1683 struct pf_osfp_enlist * 1684 pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int, 1685 const struct tcphdr *); 1686 #endif /* _KERNEL */ 1687 void pf_osfp_flush(void); 1688 int pf_osfp_get(struct pf_osfp_ioctl *); 1689 int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t); 1690 1691 #ifdef _KERNEL 1692 void pf_print_host(struct pf_addr *, u_int16_t, u_int8_t); 1693 1694 void pf_step_into_anchor(struct pf_kanchor_stackframe *, int *, 1695 struct pf_kruleset **, int, struct pf_krule **, 1696 struct pf_krule **, int *); 1697 int pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *, 1698 struct pf_kruleset **, int, struct pf_krule **, 1699 struct pf_krule **, int *); 1700 1701 int pf_map_addr(u_int8_t, struct pf_krule *, 1702 struct pf_addr *, struct pf_addr *, 1703 struct pf_addr *, struct pf_ksrc_node **); 1704 struct pf_krule *pf_get_translation(struct pf_pdesc *, struct mbuf *, 1705 int, int, struct pfi_kkif *, struct pf_ksrc_node **, 1706 struct pf_state_key **, struct pf_state_key **, 1707 struct pf_addr *, struct pf_addr *, 1708 uint16_t, uint16_t, struct pf_kanchor_stackframe *); 1709 1710 struct pf_state_key *pf_state_key_setup(struct pf_pdesc *, struct pf_addr *, 1711 struct pf_addr *, u_int16_t, u_int16_t); 1712 struct pf_state_key *pf_state_key_clone(struct pf_state_key *); 1713 1714 struct pfi_kkif *pf_kkif_create(int); 1715 void pf_kkif_free(struct pfi_kkif *); 1716 void pf_kkif_zero(struct pfi_kkif *); 1717 #endif /* _KERNEL */ 1718 1719 #endif /* _NET_PFVAR_H_ */ 1720