]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/pfvar.h
if_epair: fix race condition on multi-core systems
[FreeBSD/FreeBSD.git] / sys / net / pfvar.h
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/epoch.h>
43 #include <sys/malloc.h>
44 #include <sys/nv.h>
45 #include <sys/refcount.h>
46 #include <sys/sdt.h>
47 #include <sys/sysctl.h>
48 #include <sys/smp.h>
49 #include <sys/lock.h>
50 #include <sys/rmlock.h>
51 #include <sys/tree.h>
52 #include <sys/seqc.h>
53 #include <vm/uma.h>
54
55 #include <net/if.h>
56 #include <net/ethernet.h>
57 #include <net/radix.h>
58 #include <netinet/in.h>
59 #ifdef _KERNEL
60 #include <netinet/ip.h>
61 #include <netinet/tcp.h>
62 #include <netinet/udp.h>
63 #include <netinet/ip_icmp.h>
64 #include <netinet/icmp6.h>
65 #endif
66
67 #include <netpfil/pf/pf.h>
68 #include <netpfil/pf/pf_altq.h>
69 #include <netpfil/pf/pf_mtag.h>
70
71 #ifdef _KERNEL
72
73 #if defined(__arm__)
74 #define PF_WANT_32_TO_64_COUNTER
75 #endif
76
77 /*
78  * A hybrid of 32-bit and 64-bit counters which can be used on platforms where
79  * counter(9) is very expensive.
80  *
81  * As 32-bit counters are expected to overflow, a periodic job sums them up to
82  * a saved 64-bit state. Fetching the value still walks all CPUs to get the most
83  * current snapshot.
84  */
85 #ifdef PF_WANT_32_TO_64_COUNTER
86 struct pf_counter_u64_pcpu {
87         u_int32_t current;
88         u_int32_t snapshot;
89 };
90
91 struct pf_counter_u64 {
92         struct pf_counter_u64_pcpu *pfcu64_pcpu;
93         u_int64_t pfcu64_value;
94         seqc_t  pfcu64_seqc;
95 };
96
97 static inline int
98 pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags)
99 {
100
101         pfcu64->pfcu64_value = 0;
102         pfcu64->pfcu64_seqc = 0;
103         pfcu64->pfcu64_pcpu = uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO);
104         if (__predict_false(pfcu64->pfcu64_pcpu == NULL))
105                 return (ENOMEM);
106         return (0);
107 }
108
109 static inline void
110 pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64)
111 {
112
113         uma_zfree_pcpu(pcpu_zone_8, pfcu64->pfcu64_pcpu);
114 }
115
116 static inline void
117 pf_counter_u64_critical_enter(void)
118 {
119
120         critical_enter();
121 }
122
123 static inline void
124 pf_counter_u64_critical_exit(void)
125 {
126
127         critical_exit();
128 }
129
130 static inline void
131 pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n)
132 {
133         struct pf_counter_u64_pcpu *pcpu;
134         u_int32_t val;
135
136         MPASS(curthread->td_critnest > 0);
137         pcpu = zpcpu_get(pfcu64->pfcu64_pcpu);
138         val = atomic_load_int(&pcpu->current);
139         atomic_store_int(&pcpu->current, val + n);
140 }
141
142 static inline void
143 pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n)
144 {
145
146         critical_enter();
147         pf_counter_u64_add_protected(pfcu64, n);
148         critical_exit();
149 }
150
151 static inline u_int64_t
152 pf_counter_u64_periodic(struct pf_counter_u64 *pfcu64)
153 {
154         struct pf_counter_u64_pcpu *pcpu;
155         u_int64_t sum;
156         u_int32_t val;
157         int cpu;
158
159         MPASS(curthread->td_critnest > 0);
160         seqc_write_begin(&pfcu64->pfcu64_seqc);
161         sum = pfcu64->pfcu64_value;
162         CPU_FOREACH(cpu) {
163                 pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
164                 val = atomic_load_int(&pcpu->current);
165                 sum += (uint32_t)(val - pcpu->snapshot);
166                 pcpu->snapshot = val;
167         }
168         pfcu64->pfcu64_value = sum;
169         seqc_write_end(&pfcu64->pfcu64_seqc);
170         return (sum);
171 }
172
173 static inline u_int64_t
174 pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64)
175 {
176         struct pf_counter_u64_pcpu *pcpu;
177         u_int64_t sum;
178         seqc_t seqc;
179         int cpu;
180
181         for (;;) {
182                 seqc = seqc_read(&pfcu64->pfcu64_seqc);
183                 sum = 0;
184                 CPU_FOREACH(cpu) {
185                         pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
186                         sum += (uint32_t)(atomic_load_int(&pcpu->current) -pcpu->snapshot);
187                 }
188                 sum += pfcu64->pfcu64_value;
189                 if (seqc_consistent(&pfcu64->pfcu64_seqc, seqc))
190                         break;
191         }
192         return (sum);
193 }
194
195 static inline void
196 pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64)
197 {
198         struct pf_counter_u64_pcpu *pcpu;
199         int cpu;
200
201         MPASS(curthread->td_critnest > 0);
202         seqc_write_begin(&pfcu64->pfcu64_seqc);
203         CPU_FOREACH(cpu) {
204                 pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
205                 pcpu->snapshot = atomic_load_int(&pcpu->current);
206         }
207         pfcu64->pfcu64_value = 0;
208         seqc_write_end(&pfcu64->pfcu64_seqc);
209 }
210
211 static inline void
212 pf_counter_u64_zero(struct pf_counter_u64 *pfcu64)
213 {
214
215         critical_enter();
216         pf_counter_u64_zero_protected(pfcu64);
217         critical_exit();
218 }
219 #else
220 struct pf_counter_u64 {
221         counter_u64_t counter;
222 };
223
224 static inline int
225 pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags)
226 {
227
228         pfcu64->counter = counter_u64_alloc(flags);
229         if (__predict_false(pfcu64->counter == NULL))
230                 return (ENOMEM);
231         return (0);
232 }
233
234 static inline void
235 pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64)
236 {
237
238         counter_u64_free(pfcu64->counter);
239 }
240
241 static inline void
242 pf_counter_u64_critical_enter(void)
243 {
244
245 }
246
247 static inline void
248 pf_counter_u64_critical_exit(void)
249 {
250
251 }
252
253 static inline void
254 pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n)
255 {
256
257         counter_u64_add(pfcu64->counter, n);
258 }
259
260 static inline void
261 pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n)
262 {
263
264         pf_counter_u64_add_protected(pfcu64, n);
265 }
266
267 static inline u_int64_t
268 pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64)
269 {
270
271         return (counter_u64_fetch(pfcu64->counter));
272 }
273
274 static inline void
275 pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64)
276 {
277
278         counter_u64_zero(pfcu64->counter);
279 }
280
281 static inline void
282 pf_counter_u64_zero(struct pf_counter_u64 *pfcu64)
283 {
284
285         pf_counter_u64_zero_protected(pfcu64);
286 }
287 #endif
288
289 SYSCTL_DECL(_net_pf);
290 MALLOC_DECLARE(M_PFHASH);
291
292 SDT_PROVIDER_DECLARE(pf);
293
294 struct pfi_dynaddr {
295         TAILQ_ENTRY(pfi_dynaddr)         entry;
296         struct pf_addr                   pfid_addr4;
297         struct pf_addr                   pfid_mask4;
298         struct pf_addr                   pfid_addr6;
299         struct pf_addr                   pfid_mask6;
300         struct pfr_ktable               *pfid_kt;
301         struct pfi_kkif                 *pfid_kif;
302         int                              pfid_net;      /* mask or 128 */
303         int                              pfid_acnt4;    /* address count IPv4 */
304         int                              pfid_acnt6;    /* address count IPv6 */
305         sa_family_t                      pfid_af;       /* rule af */
306         u_int8_t                         pfid_iflags;   /* PFI_AFLAG_* */
307 };
308
309 /*
310  * Address manipulation macros
311  */
312 #define HTONL(x)        (x) = htonl((__uint32_t)(x))
313 #define HTONS(x)        (x) = htons((__uint16_t)(x))
314 #define NTOHL(x)        (x) = ntohl((__uint32_t)(x))
315 #define NTOHS(x)        (x) = ntohs((__uint16_t)(x))
316
317 #define PF_NAME         "pf"
318
319 #define PF_HASHROW_ASSERT(h)    mtx_assert(&(h)->lock, MA_OWNED)
320 #define PF_HASHROW_LOCK(h)      mtx_lock(&(h)->lock)
321 #define PF_HASHROW_UNLOCK(h)    mtx_unlock(&(h)->lock)
322
323 #ifdef INVARIANTS
324 #define PF_STATE_LOCK(s)                                                \
325         do {                                                            \
326                 struct pf_kstate *_s = (s);                             \
327                 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];    \
328                 MPASS(_s->lock == &_ih->lock);                          \
329                 mtx_lock(_s->lock);                                     \
330         } while (0)
331 #define PF_STATE_UNLOCK(s)                                              \
332         do {                                                            \
333                 struct pf_kstate *_s = (s);                             \
334                 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];    \
335                 MPASS(_s->lock == &_ih->lock);                          \
336                 mtx_unlock(_s->lock);                                   \
337         } while (0)
338 #else
339 #define PF_STATE_LOCK(s)        mtx_lock(s->lock)
340 #define PF_STATE_UNLOCK(s)      mtx_unlock(s->lock)
341 #endif
342
343 #ifdef INVARIANTS
344 #define PF_STATE_LOCK_ASSERT(s)                                         \
345         do {                                                            \
346                 struct pf_kstate *_s = (s);                             \
347                 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];    \
348                 MPASS(_s->lock == &_ih->lock);                          \
349                 PF_HASHROW_ASSERT(_ih);                                 \
350         } while (0)
351 #else /* !INVARIANTS */
352 #define PF_STATE_LOCK_ASSERT(s)         do {} while (0)
353 #endif /* INVARIANTS */
354
355 extern struct mtx_padalign pf_unlnkdrules_mtx;
356 #define PF_UNLNKDRULES_LOCK()   mtx_lock(&pf_unlnkdrules_mtx)
357 #define PF_UNLNKDRULES_UNLOCK() mtx_unlock(&pf_unlnkdrules_mtx)
358 #define PF_UNLNKDRULES_ASSERT() mtx_assert(&pf_unlnkdrules_mtx, MA_OWNED)
359
360 extern struct rmlock pf_rules_lock;
361 #define PF_RULES_RLOCK_TRACKER  struct rm_priotracker _pf_rules_tracker
362 #define PF_RULES_RLOCK()        rm_rlock(&pf_rules_lock, &_pf_rules_tracker)
363 #define PF_RULES_RUNLOCK()      rm_runlock(&pf_rules_lock, &_pf_rules_tracker)
364 #define PF_RULES_WLOCK()        rm_wlock(&pf_rules_lock)
365 #define PF_RULES_WUNLOCK()      rm_wunlock(&pf_rules_lock)
366 #define PF_RULES_WOWNED()       rm_wowned(&pf_rules_lock)
367 #define PF_RULES_ASSERT()       rm_assert(&pf_rules_lock, RA_LOCKED)
368 #define PF_RULES_RASSERT()      rm_assert(&pf_rules_lock, RA_RLOCKED)
369 #define PF_RULES_WASSERT()      rm_assert(&pf_rules_lock, RA_WLOCKED)
370
371 extern struct mtx_padalign pf_table_stats_lock;
372 #define PF_TABLE_STATS_LOCK()   mtx_lock(&pf_table_stats_lock)
373 #define PF_TABLE_STATS_UNLOCK() mtx_unlock(&pf_table_stats_lock)
374 #define PF_TABLE_STATS_OWNED()  mtx_owned(&pf_table_stats_lock)
375 #define PF_TABLE_STATS_ASSERT() mtx_assert(&pf_rules_lock, MA_OWNED)
376
377 extern struct sx pf_end_lock;
378
379 #define PF_MODVER       1
380 #define PFLOG_MODVER    1
381 #define PFSYNC_MODVER   1
382
383 #define PFLOG_MINVER    1
384 #define PFLOG_PREFVER   PFLOG_MODVER
385 #define PFLOG_MAXVER    1
386 #define PFSYNC_MINVER   1
387 #define PFSYNC_PREFVER  PFSYNC_MODVER
388 #define PFSYNC_MAXVER   1
389
390 #ifdef INET
391 #ifndef INET6
392 #define PF_INET_ONLY
393 #endif /* ! INET6 */
394 #endif /* INET */
395
396 #ifdef INET6
397 #ifndef INET
398 #define PF_INET6_ONLY
399 #endif /* ! INET */
400 #endif /* INET6 */
401
402 #ifdef INET
403 #ifdef INET6
404 #define PF_INET_INET6
405 #endif /* INET6 */
406 #endif /* INET */
407
408 #else
409
410 #define PF_INET_INET6
411
412 #endif /* _KERNEL */
413
414 /* Both IPv4 and IPv6 */
415 #ifdef PF_INET_INET6
416
417 #define PF_AEQ(a, b, c) \
418         ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
419         (c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \
420         (a)->addr32[2] == (b)->addr32[2] && \
421         (a)->addr32[1] == (b)->addr32[1] && \
422         (a)->addr32[0] == (b)->addr32[0])) \
423
424 #define PF_ANEQ(a, b, c) \
425         ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
426         (c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \
427         (a)->addr32[1] != (b)->addr32[1] || \
428         (a)->addr32[2] != (b)->addr32[2] || \
429         (a)->addr32[3] != (b)->addr32[3]))) \
430
431 #define PF_AZERO(a, c) \
432         ((c == AF_INET && !(a)->addr32[0]) || \
433         (c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \
434         !(a)->addr32[2] && !(a)->addr32[3] )) \
435
436 #define PF_MATCHA(n, a, m, b, f) \
437         pf_match_addr(n, a, m, b, f)
438
439 #define PF_ACPY(a, b, f) \
440         pf_addrcpy(a, b, f)
441
442 #define PF_AINC(a, f) \
443         pf_addr_inc(a, f)
444
445 #define PF_POOLMASK(a, b, c, d, f) \
446         pf_poolmask(a, b, c, d, f)
447
448 #else
449
450 /* Just IPv6 */
451
452 #ifdef PF_INET6_ONLY
453
454 #define PF_AEQ(a, b, c) \
455         ((a)->addr32[3] == (b)->addr32[3] && \
456         (a)->addr32[2] == (b)->addr32[2] && \
457         (a)->addr32[1] == (b)->addr32[1] && \
458         (a)->addr32[0] == (b)->addr32[0]) \
459
460 #define PF_ANEQ(a, b, c) \
461         ((a)->addr32[3] != (b)->addr32[3] || \
462         (a)->addr32[2] != (b)->addr32[2] || \
463         (a)->addr32[1] != (b)->addr32[1] || \
464         (a)->addr32[0] != (b)->addr32[0]) \
465
466 #define PF_AZERO(a, c) \
467         (!(a)->addr32[0] && \
468         !(a)->addr32[1] && \
469         !(a)->addr32[2] && \
470         !(a)->addr32[3] ) \
471
472 #define PF_MATCHA(n, a, m, b, f) \
473         pf_match_addr(n, a, m, b, f)
474
475 #define PF_ACPY(a, b, f) \
476         pf_addrcpy(a, b, f)
477
478 #define PF_AINC(a, f) \
479         pf_addr_inc(a, f)
480
481 #define PF_POOLMASK(a, b, c, d, f) \
482         pf_poolmask(a, b, c, d, f)
483
484 #else
485
486 /* Just IPv4 */
487 #ifdef PF_INET_ONLY
488
489 #define PF_AEQ(a, b, c) \
490         ((a)->addr32[0] == (b)->addr32[0])
491
492 #define PF_ANEQ(a, b, c) \
493         ((a)->addr32[0] != (b)->addr32[0])
494
495 #define PF_AZERO(a, c) \
496         (!(a)->addr32[0])
497
498 #define PF_MATCHA(n, a, m, b, f) \
499         pf_match_addr(n, a, m, b, f)
500
501 #define PF_ACPY(a, b, f) \
502         (a)->v4.s_addr = (b)->v4.s_addr
503
504 #define PF_AINC(a, f) \
505         do { \
506                 (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
507         } while (0)
508
509 #define PF_POOLMASK(a, b, c, d, f) \
510         do { \
511                 (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
512                 (((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
513         } while (0)
514
515 #endif /* PF_INET_ONLY */
516 #endif /* PF_INET6_ONLY */
517 #endif /* PF_INET_INET6 */
518
519 /*
520  * XXX callers not FIB-aware in our version of pf yet.
521  * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio.
522  */
523 #define PF_MISMATCHAW(aw, x, af, neg, ifp, rtid)                        \
524         (                                                               \
525                 (((aw)->type == PF_ADDR_NOROUTE &&                      \
526                     pf_routable((x), (af), NULL, (rtid))) ||            \
527                 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL &&  \
528                     pf_routable((x), (af), (ifp), (rtid))) ||           \
529                 ((aw)->type == PF_ADDR_TABLE &&                         \
530                     !pfr_match_addr((aw)->p.tbl, (x), (af))) ||         \
531                 ((aw)->type == PF_ADDR_DYNIFTL &&                       \
532                     !pfi_match_addr((aw)->p.dyn, (x), (af))) ||         \
533                 ((aw)->type == PF_ADDR_RANGE &&                         \
534                     !pf_match_addr_range(&(aw)->v.a.addr,               \
535                     &(aw)->v.a.mask, (x), (af))) ||                     \
536                 ((aw)->type == PF_ADDR_ADDRMASK &&                      \
537                     !PF_AZERO(&(aw)->v.a.mask, (af)) &&                 \
538                     !PF_MATCHA(0, &(aw)->v.a.addr,                      \
539                     &(aw)->v.a.mask, (x), (af))))) !=                   \
540                 (neg)                                                   \
541         )
542
543 #define PF_ALGNMNT(off) (((off) % 2) == 0)
544
545 #ifdef _KERNEL
546
547 struct pf_kpooladdr {
548         struct pf_addr_wrap              addr;
549         TAILQ_ENTRY(pf_kpooladdr)        entries;
550         char                             ifname[IFNAMSIZ];
551         struct pfi_kkif                 *kif;
552 };
553
554 TAILQ_HEAD(pf_kpalist, pf_kpooladdr);
555
556 struct pf_kpool {
557         struct mtx               mtx;
558         struct pf_kpalist        list;
559         struct pf_kpooladdr     *cur;
560         struct pf_poolhashkey    key;
561         struct pf_addr           counter;
562         struct pf_mape_portset   mape;
563         int                      tblidx;
564         u_int16_t                proxy_port[2];
565         u_int8_t                 opts;
566 };
567
568 struct pf_rule_actions {
569         uint16_t         qid;
570         uint16_t         pqid;
571         uint16_t         dnpipe;
572         uint16_t         dnrpipe;       /* Reverse direction pipe */
573         uint32_t         flags;
574 };
575
576 union pf_keth_rule_ptr {
577         struct pf_keth_rule     *ptr;
578         uint32_t                nr;
579 };
580
581 struct pf_keth_rule_addr {
582         uint8_t addr[ETHER_ADDR_LEN];
583         uint8_t mask[ETHER_ADDR_LEN];
584         bool neg;
585         uint8_t isset;
586 };
587
588 struct pf_keth_anchor;
589
590 TAILQ_HEAD(pf_keth_ruleq, pf_keth_rule);
591
592 struct pf_keth_ruleset {
593         struct pf_keth_ruleq             rules[2];
594         struct pf_keth_rules {
595                 struct pf_keth_ruleq    *rules;
596                 int                      open;
597                 uint32_t                 ticket;
598         } active, inactive;
599         struct epoch_context     epoch_ctx;
600         struct vnet             *vnet;
601         struct pf_keth_anchor   *anchor;
602 };
603
604 RB_HEAD(pf_keth_anchor_global, pf_keth_anchor);
605 RB_HEAD(pf_keth_anchor_node, pf_keth_anchor);
606 struct pf_keth_anchor {
607         RB_ENTRY(pf_keth_anchor)         entry_node;
608         RB_ENTRY(pf_keth_anchor)         entry_global;
609         struct pf_keth_anchor           *parent;
610         struct pf_keth_anchor_node       children;
611         char                             name[PF_ANCHOR_NAME_SIZE];
612         char                             path[MAXPATHLEN];
613         struct pf_keth_ruleset           ruleset;
614         int                              refcnt;        /* anchor rules */
615         uint8_t                          anchor_relative;
616         uint8_t                          anchor_wildcard;
617 };
618 RB_PROTOTYPE(pf_keth_anchor_node, pf_keth_anchor, entry_node,
619     pf_keth_anchor_compare);
620 RB_PROTOTYPE(pf_keth_anchor_global, pf_keth_anchor, entry_global,
621     pf_keth_anchor_compare);
622
623 struct pf_keth_rule {
624 #define PFE_SKIP_IFP            0
625 #define PFE_SKIP_DIR            1
626 #define PFE_SKIP_PROTO          2
627 #define PFE_SKIP_SRC_ADDR       3
628 #define PFE_SKIP_DST_ADDR       4
629 #define PFE_SKIP_COUNT          5
630         union pf_keth_rule_ptr   skip[PFE_SKIP_COUNT];
631
632         TAILQ_ENTRY(pf_keth_rule)       entries;
633
634         struct pf_keth_anchor   *anchor;
635         u_int8_t                 anchor_relative;
636         u_int8_t                 anchor_wildcard;
637
638         uint32_t                 nr;
639
640         bool                     quick;
641
642         /* Filter */
643         char                     ifname[IFNAMSIZ];
644         struct pfi_kkif         *kif;
645         bool                     ifnot;
646         uint8_t                  direction;
647         uint16_t                 proto;
648         struct pf_keth_rule_addr src, dst;
649         struct pf_rule_addr      ipsrc, ipdst;
650
651         /* Stats */
652         counter_u64_t            evaluations;
653         counter_u64_t            packets[2];
654         counter_u64_t            bytes[2];
655
656         /* Action */
657         char                     qname[PF_QNAME_SIZE];
658         int                      qid;
659         char                     tagname[PF_TAG_NAME_SIZE];
660         uint16_t                 tag;
661         uint8_t                  action;
662         uint16_t                 dnpipe;
663         uint32_t                 dnflags;
664 };
665
666 union pf_krule_ptr {
667         struct pf_krule         *ptr;
668         u_int32_t                nr;
669 };
670
671 struct pf_krule {
672         struct pf_rule_addr      src;
673         struct pf_rule_addr      dst;
674         union pf_krule_ptr       skip[PF_SKIP_COUNT];
675         char                     label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
676         uint32_t                 ridentifier;
677         char                     ifname[IFNAMSIZ];
678         char                     qname[PF_QNAME_SIZE];
679         char                     pqname[PF_QNAME_SIZE];
680         char                     tagname[PF_TAG_NAME_SIZE];
681         char                     match_tagname[PF_TAG_NAME_SIZE];
682
683         char                     overload_tblname[PF_TABLE_NAME_SIZE];
684
685         TAILQ_ENTRY(pf_krule)    entries;
686         struct pf_kpool          rpool;
687
688         struct pf_counter_u64    evaluations;
689         struct pf_counter_u64    packets[2];
690         struct pf_counter_u64    bytes[2];
691
692         struct pfi_kkif         *kif;
693         struct pf_kanchor       *anchor;
694         struct pfr_ktable       *overload_tbl;
695
696         pf_osfp_t                os_fingerprint;
697
698         int                      rtableid;
699         u_int32_t                timeout[PFTM_MAX];
700         u_int32_t                max_states;
701         u_int32_t                max_src_nodes;
702         u_int32_t                max_src_states;
703         u_int32_t                max_src_conn;
704         struct {
705                 u_int32_t               limit;
706                 u_int32_t               seconds;
707         }                        max_src_conn_rate;
708         u_int16_t                qid;
709         u_int16_t                pqid;
710         u_int16_t                dnpipe;
711         u_int16_t                dnrpipe;
712         u_int32_t                free_flags;
713         u_int32_t                nr;
714         u_int32_t                prob;
715         uid_t                    cuid;
716         pid_t                    cpid;
717
718         counter_u64_t            states_cur;
719         counter_u64_t            states_tot;
720         counter_u64_t            src_nodes;
721
722         u_int16_t                return_icmp;
723         u_int16_t                return_icmp6;
724         u_int16_t                max_mss;
725         u_int16_t                tag;
726         u_int16_t                match_tag;
727         u_int16_t                scrub_flags;
728
729         struct pf_rule_uid       uid;
730         struct pf_rule_gid       gid;
731
732         u_int32_t                rule_flag;
733         uint32_t                 rule_ref;
734         u_int8_t                 action;
735         u_int8_t                 direction;
736         u_int8_t                 log;
737         u_int8_t                 logif;
738         u_int8_t                 quick;
739         u_int8_t                 ifnot;
740         u_int8_t                 match_tag_not;
741         u_int8_t                 natpass;
742
743         u_int8_t                 keep_state;
744         sa_family_t              af;
745         u_int8_t                 proto;
746         u_int8_t                 type;
747         u_int8_t                 code;
748         u_int8_t                 flags;
749         u_int8_t                 flagset;
750         u_int8_t                 min_ttl;
751         u_int8_t                 allow_opts;
752         u_int8_t                 rt;
753         u_int8_t                 return_ttl;
754         u_int8_t                 tos;
755         u_int8_t                 set_tos;
756         u_int8_t                 anchor_relative;
757         u_int8_t                 anchor_wildcard;
758
759         u_int8_t                 flush;
760         u_int8_t                 prio;
761         u_int8_t                 set_prio[2];
762
763         struct {
764                 struct pf_addr          addr;
765                 u_int16_t               port;
766         }                       divert;
767
768 #ifdef PF_WANT_32_TO_64_COUNTER
769         LIST_ENTRY(pf_krule)     allrulelist;
770         bool                     allrulelinked;
771 #endif
772 };
773
774 struct pf_ksrc_node {
775         LIST_ENTRY(pf_ksrc_node) entry;
776         struct pf_addr   addr;
777         struct pf_addr   raddr;
778         union pf_krule_ptr rule;
779         struct pfi_kkif *kif;
780         counter_u64_t    bytes[2];
781         counter_u64_t    packets[2];
782         u_int32_t        states;
783         u_int32_t        conn;
784         struct pf_threshold     conn_rate;
785         u_int32_t        creation;
786         u_int32_t        expire;
787         sa_family_t      af;
788         u_int8_t         ruletype;
789 };
790 #endif
791
792 struct pf_state_scrub {
793         struct timeval  pfss_last;      /* time received last packet    */
794         u_int32_t       pfss_tsecr;     /* last echoed timestamp        */
795         u_int32_t       pfss_tsval;     /* largest timestamp            */
796         u_int32_t       pfss_tsval0;    /* original timestamp           */
797         u_int16_t       pfss_flags;
798 #define PFSS_TIMESTAMP  0x0001          /* modulate timestamp           */
799 #define PFSS_PAWS       0x0010          /* stricter PAWS checks         */
800 #define PFSS_PAWS_IDLED 0x0020          /* was idle too long.  no PAWS  */
801 #define PFSS_DATA_TS    0x0040          /* timestamp on data packets    */
802 #define PFSS_DATA_NOTS  0x0080          /* no timestamp on data packets */
803         u_int8_t        pfss_ttl;       /* stashed TTL                  */
804         u_int8_t        pad;
805         u_int32_t       pfss_ts_mod;    /* timestamp modulation         */
806 };
807
808 struct pf_state_host {
809         struct pf_addr  addr;
810         u_int16_t       port;
811         u_int16_t       pad;
812 };
813
814 struct pf_state_peer {
815         struct pf_state_scrub   *scrub; /* state is scrubbed            */
816         u_int32_t       seqlo;          /* Max sequence number sent     */
817         u_int32_t       seqhi;          /* Max the other end ACKd + win */
818         u_int32_t       seqdiff;        /* Sequence number modulator    */
819         u_int16_t       max_win;        /* largest window (pre scaling) */
820         u_int16_t       mss;            /* Maximum segment size option  */
821         u_int8_t        state;          /* active state level           */
822         u_int8_t        wscale;         /* window scaling factor        */
823         u_int8_t        tcp_est;        /* Did we reach TCPS_ESTABLISHED */
824         u_int8_t        pad[1];
825 };
826
827 /* Keep synced with struct pf_state_key. */
828 struct pf_state_key_cmp {
829         struct pf_addr   addr[2];
830         u_int16_t        port[2];
831         sa_family_t      af;
832         u_int8_t         proto;
833         u_int8_t         pad[2];
834 };
835
836 struct pf_state_key {
837         struct pf_addr   addr[2];
838         u_int16_t        port[2];
839         sa_family_t      af;
840         u_int8_t         proto;
841         u_int8_t         pad[2];
842
843         LIST_ENTRY(pf_state_key) entry;
844         TAILQ_HEAD(, pf_kstate)  states[2];
845 };
846
847 /* Keep synced with struct pf_kstate. */
848 struct pf_state_cmp {
849         u_int64_t                id;
850         u_int32_t                creatorid;
851         u_int8_t                 direction;
852         u_int8_t                 pad[3];
853 };
854
855 #define PFSTATE_ALLOWOPTS       0x01
856 #define PFSTATE_SLOPPY          0x02
857 /*  was PFSTATE_PFLOW           0x04 */
858 #define PFSTATE_NOSYNC          0x08
859 #define PFSTATE_ACK             0x10
860 #define PFRULE_DN_IS_PIPE       0x40
861 #define PFRULE_DN_IS_QUEUE      0x80
862 #define PFSTATE_SETPRIO         0x0200
863 #define PFSTATE_SETMASK   (PFSTATE_SETPRIO)
864
865 struct pf_state_scrub_export {
866         uint16_t        pfss_flags;
867         uint8_t         pfss_ttl;       /* stashed TTL          */
868 #define PF_SCRUB_FLAG_VALID             0x01
869         uint8_t         scrub_flag;
870         uint32_t        pfss_ts_mod;    /* timestamp modulation */
871 };
872
873 struct pf_state_key_export {
874         struct pf_addr   addr[2];
875         uint16_t         port[2];
876 };
877
878 struct pf_state_peer_export {
879         struct pf_state_scrub_export    scrub;  /* state is scrubbed    */
880         uint32_t        seqlo;          /* Max sequence number sent     */
881         uint32_t        seqhi;          /* Max the other end ACKd + win */
882         uint32_t        seqdiff;        /* Sequence number modulator    */
883         uint16_t        max_win;        /* largest window (pre scaling) */
884         uint16_t        mss;            /* Maximum segment size option  */
885         uint8_t         state;          /* active state level           */
886         uint8_t         wscale;         /* window scaling factor        */
887         uint8_t         dummy[6];
888 };
889 _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect");
890
891 struct pf_state_export {
892         uint64_t         version;
893 #define PF_STATE_VERSION        20210706
894         uint64_t         id;
895         char             ifname[IFNAMSIZ];
896         char             orig_ifname[IFNAMSIZ];
897         struct pf_state_key_export       key[2];
898         struct pf_state_peer_export      src;
899         struct pf_state_peer_export      dst;
900         struct pf_addr   rt_addr;
901         uint32_t         rule;
902         uint32_t         anchor;
903         uint32_t         nat_rule;
904         uint32_t         creation;
905         uint32_t         expire;
906         uint32_t         spare0;
907         uint64_t         packets[2];
908         uint64_t         bytes[2];
909         uint32_t         creatorid;
910         uint32_t         spare1;
911         sa_family_t      af;
912         uint8_t          proto;
913         uint8_t          direction;
914         uint8_t          log;
915         uint8_t          state_flags;
916         uint8_t          timeout;
917         uint8_t          sync_flags;
918         uint8_t          updates;
919
920         uint8_t          spare[112];
921 };
922 _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect");
923
924 #ifdef _KERNEL
925 struct pf_kstate {
926         /*
927          * Area shared with pf_state_cmp
928          */
929         u_int64_t                id;
930         u_int32_t                creatorid;
931         u_int8_t                 direction;
932         u_int8_t                 pad[3];
933         /*
934          * end of the area
935          */
936
937         u_int8_t                 state_flags;
938         u_int8_t                 timeout;
939         u_int8_t                 sync_state; /* PFSYNC_S_x */
940         u_int8_t                 sync_updates; /* XXX */
941         u_int                    refs;
942         struct mtx              *lock;
943         TAILQ_ENTRY(pf_kstate)   sync_list;
944         TAILQ_ENTRY(pf_kstate)   key_list[2];
945         LIST_ENTRY(pf_kstate)    entry;
946         struct pf_state_peer     src;
947         struct pf_state_peer     dst;
948         union pf_krule_ptr       rule;
949         union pf_krule_ptr       anchor;
950         union pf_krule_ptr       nat_rule;
951         struct pf_addr           rt_addr;
952         struct pf_state_key     *key[2];        /* addresses stack and wire  */
953         struct pfi_kkif         *kif;
954         struct pfi_kkif         *orig_kif;      /* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */
955         struct pfi_kkif         *rt_kif;
956         struct pf_ksrc_node     *src_node;
957         struct pf_ksrc_node     *nat_src_node;
958         u_int64_t                packets[2];
959         u_int64_t                bytes[2];
960         u_int32_t                creation;
961         u_int32_t                expire;
962         u_int32_t                pfsync_time;
963         u_int16_t                qid;
964         u_int16_t                pqid;
965         u_int16_t                dnpipe;
966         u_int16_t                dnrpipe;
967         u_int16_t                tag;
968         u_int8_t                 log;
969 };
970
971 /*
972  * Size <= fits 13 objects per page on LP64. Try to not grow the struct beyond that.
973  */
974 _Static_assert(sizeof(struct pf_kstate) <= 312, "pf_kstate size crosses 312 bytes");
975 #endif
976
977 /*
978  * Unified state structures for pulling states out of the kernel
979  * used by pfsync(4) and the pf(4) ioctl.
980  */
981 struct pfsync_state_scrub {
982         u_int16_t       pfss_flags;
983         u_int8_t        pfss_ttl;       /* stashed TTL          */
984 #define PFSYNC_SCRUB_FLAG_VALID         0x01
985         u_int8_t        scrub_flag;
986         u_int32_t       pfss_ts_mod;    /* timestamp modulation */
987 } __packed;
988
989 struct pfsync_state_peer {
990         struct pfsync_state_scrub scrub;        /* state is scrubbed    */
991         u_int32_t       seqlo;          /* Max sequence number sent     */
992         u_int32_t       seqhi;          /* Max the other end ACKd + win */
993         u_int32_t       seqdiff;        /* Sequence number modulator    */
994         u_int16_t       max_win;        /* largest window (pre scaling) */
995         u_int16_t       mss;            /* Maximum segment size option  */
996         u_int8_t        state;          /* active state level           */
997         u_int8_t        wscale;         /* window scaling factor        */
998         u_int8_t        pad[6];
999 } __packed;
1000
1001 struct pfsync_state_key {
1002         struct pf_addr   addr[2];
1003         u_int16_t        port[2];
1004 };
1005
1006 struct pfsync_state {
1007         u_int64_t        id;
1008         char             ifname[IFNAMSIZ];
1009         struct pfsync_state_key key[2];
1010         struct pfsync_state_peer src;
1011         struct pfsync_state_peer dst;
1012         struct pf_addr   rt_addr;
1013         u_int32_t        rule;
1014         u_int32_t        anchor;
1015         u_int32_t        nat_rule;
1016         u_int32_t        creation;
1017         u_int32_t        expire;
1018         u_int32_t        packets[2][2];
1019         u_int32_t        bytes[2][2];
1020         u_int32_t        creatorid;
1021         sa_family_t      af;
1022         u_int8_t         proto;
1023         u_int8_t         direction;
1024         u_int8_t         __spare[2];
1025         u_int8_t         log;
1026         u_int8_t         state_flags;
1027         u_int8_t         timeout;
1028         u_int8_t         sync_flags;
1029         u_int8_t         updates;
1030 } __packed;
1031
1032 #ifdef _KERNEL
1033 /* pfsync */
1034 typedef int             pfsync_state_import_t(struct pfsync_state *, u_int8_t);
1035 typedef void            pfsync_insert_state_t(struct pf_kstate *);
1036 typedef void            pfsync_update_state_t(struct pf_kstate *);
1037 typedef void            pfsync_delete_state_t(struct pf_kstate *);
1038 typedef void            pfsync_clear_states_t(u_int32_t, const char *);
1039 typedef int             pfsync_defer_t(struct pf_kstate *, struct mbuf *);
1040 typedef void            pfsync_detach_ifnet_t(struct ifnet *);
1041
1042 VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr);
1043 #define V_pfsync_state_import_ptr       VNET(pfsync_state_import_ptr)
1044 VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
1045 #define V_pfsync_insert_state_ptr       VNET(pfsync_insert_state_ptr)
1046 VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr);
1047 #define V_pfsync_update_state_ptr       VNET(pfsync_update_state_ptr)
1048 VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
1049 #define V_pfsync_delete_state_ptr       VNET(pfsync_delete_state_ptr)
1050 VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
1051 #define V_pfsync_clear_states_ptr       VNET(pfsync_clear_states_ptr)
1052 VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr);
1053 #define V_pfsync_defer_ptr              VNET(pfsync_defer_ptr)
1054 extern pfsync_detach_ifnet_t    *pfsync_detach_ifnet_ptr;
1055
1056 void                    pfsync_state_export(struct pfsync_state *,
1057                             struct pf_kstate *);
1058 void                    pf_state_export(struct pf_state_export *,
1059                             struct pf_kstate *);
1060
1061 /* pflog */
1062 struct pf_kruleset;
1063 struct pf_pdesc;
1064 typedef int pflog_packet_t(struct pfi_kkif *, struct mbuf *, sa_family_t,
1065     u_int8_t, u_int8_t, struct pf_krule *, struct pf_krule *,
1066     struct pf_kruleset *, struct pf_pdesc *, int);
1067 extern pflog_packet_t           *pflog_packet_ptr;
1068
1069 #endif /* _KERNEL */
1070
1071 #define PFSYNC_FLAG_SRCNODE     0x04
1072 #define PFSYNC_FLAG_NATSRCNODE  0x08
1073
1074 /* for copies to/from network byte order */
1075 /* ioctl interface also uses network byte order */
1076 #define pf_state_peer_hton(s,d) do {            \
1077         (d)->seqlo = htonl((s)->seqlo);         \
1078         (d)->seqhi = htonl((s)->seqhi);         \
1079         (d)->seqdiff = htonl((s)->seqdiff);     \
1080         (d)->max_win = htons((s)->max_win);     \
1081         (d)->mss = htons((s)->mss);             \
1082         (d)->state = (s)->state;                \
1083         (d)->wscale = (s)->wscale;              \
1084         if ((s)->scrub) {                                               \
1085                 (d)->scrub.pfss_flags =                                 \
1086                     htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP);     \
1087                 (d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl;             \
1088                 (d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\
1089                 (d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID;        \
1090         }                                                               \
1091 } while (0)
1092
1093 #define pf_state_peer_ntoh(s,d) do {            \
1094         (d)->seqlo = ntohl((s)->seqlo);         \
1095         (d)->seqhi = ntohl((s)->seqhi);         \
1096         (d)->seqdiff = ntohl((s)->seqdiff);     \
1097         (d)->max_win = ntohs((s)->max_win);     \
1098         (d)->mss = ntohs((s)->mss);             \
1099         (d)->state = (s)->state;                \
1100         (d)->wscale = (s)->wscale;              \
1101         if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID &&         \
1102             (d)->scrub != NULL) {                                       \
1103                 (d)->scrub->pfss_flags =                                \
1104                     ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP;      \
1105                 (d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl;             \
1106                 (d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\
1107         }                                                               \
1108 } while (0)
1109
1110 #define pf_state_counter_hton(s,d) do {                         \
1111         d[0] = htonl((s>>32)&0xffffffff);                       \
1112         d[1] = htonl(s&0xffffffff);                             \
1113 } while (0)
1114
1115 #define pf_state_counter_from_pfsync(s)                         \
1116         (((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1]))
1117
1118 #define pf_state_counter_ntoh(s,d) do {                         \
1119         d = ntohl(s[0]);                                        \
1120         d = d<<32;                                              \
1121         d += ntohl(s[1]);                                       \
1122 } while (0)
1123
1124 TAILQ_HEAD(pf_krulequeue, pf_krule);
1125
1126 struct pf_kanchor;
1127
1128 struct pf_kruleset {
1129         struct {
1130                 struct pf_krulequeue     queues[2];
1131                 struct {
1132                         struct pf_krulequeue    *ptr;
1133                         struct pf_krule         **ptr_array;
1134                         u_int32_t                rcount;
1135                         u_int32_t                ticket;
1136                         int                      open;
1137                 }                        active, inactive;
1138         }                        rules[PF_RULESET_MAX];
1139         struct pf_kanchor       *anchor;
1140         u_int32_t                tticket;
1141         int                      tables;
1142         int                      topen;
1143 };
1144
1145 RB_HEAD(pf_kanchor_global, pf_kanchor);
1146 RB_HEAD(pf_kanchor_node, pf_kanchor);
1147 struct pf_kanchor {
1148         RB_ENTRY(pf_kanchor)     entry_global;
1149         RB_ENTRY(pf_kanchor)     entry_node;
1150         struct pf_kanchor       *parent;
1151         struct pf_kanchor_node   children;
1152         char                     name[PF_ANCHOR_NAME_SIZE];
1153         char                     path[MAXPATHLEN];
1154         struct pf_kruleset       ruleset;
1155         int                      refcnt;        /* anchor rules */
1156 };
1157 RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare);
1158 RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare);
1159
1160 #define PF_RESERVED_ANCHOR      "_pf"
1161
1162 #define PFR_TFLAG_PERSIST       0x00000001
1163 #define PFR_TFLAG_CONST         0x00000002
1164 #define PFR_TFLAG_ACTIVE        0x00000004
1165 #define PFR_TFLAG_INACTIVE      0x00000008
1166 #define PFR_TFLAG_REFERENCED    0x00000010
1167 #define PFR_TFLAG_REFDANCHOR    0x00000020
1168 #define PFR_TFLAG_COUNTERS      0x00000040
1169 /* Adjust masks below when adding flags. */
1170 #define PFR_TFLAG_USRMASK       (PFR_TFLAG_PERSIST      | \
1171                                  PFR_TFLAG_CONST        | \
1172                                  PFR_TFLAG_COUNTERS)
1173 #define PFR_TFLAG_SETMASK       (PFR_TFLAG_ACTIVE       | \
1174                                  PFR_TFLAG_INACTIVE     | \
1175                                  PFR_TFLAG_REFERENCED   | \
1176                                  PFR_TFLAG_REFDANCHOR)
1177 #define PFR_TFLAG_ALLMASK       (PFR_TFLAG_PERSIST      | \
1178                                  PFR_TFLAG_CONST        | \
1179                                  PFR_TFLAG_ACTIVE       | \
1180                                  PFR_TFLAG_INACTIVE     | \
1181                                  PFR_TFLAG_REFERENCED   | \
1182                                  PFR_TFLAG_REFDANCHOR   | \
1183                                  PFR_TFLAG_COUNTERS)
1184
1185 struct pf_kanchor_stackframe;
1186 struct pf_keth_anchor_stackframe;
1187
1188 struct pfr_table {
1189         char                     pfrt_anchor[MAXPATHLEN];
1190         char                     pfrt_name[PF_TABLE_NAME_SIZE];
1191         u_int32_t                pfrt_flags;
1192         u_int8_t                 pfrt_fback;
1193 };
1194
1195 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
1196         PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
1197         PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX };
1198
1199 struct pfr_addr {
1200         union {
1201                 struct in_addr   _pfra_ip4addr;
1202                 struct in6_addr  _pfra_ip6addr;
1203         }                pfra_u;
1204         u_int8_t         pfra_af;
1205         u_int8_t         pfra_net;
1206         u_int8_t         pfra_not;
1207         u_int8_t         pfra_fback;
1208 };
1209 #define pfra_ip4addr    pfra_u._pfra_ip4addr
1210 #define pfra_ip6addr    pfra_u._pfra_ip6addr
1211
1212 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
1213 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
1214 enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX };
1215 #define PFR_NUM_COUNTERS        (PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX)
1216 #define PFR_OP_XPASS    PFR_OP_ADDR_MAX
1217
1218 struct pfr_astats {
1219         struct pfr_addr  pfras_a;
1220         u_int64_t        pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1221         u_int64_t        pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1222         long             pfras_tzero;
1223 };
1224
1225 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
1226
1227 struct pfr_tstats {
1228         struct pfr_table pfrts_t;
1229         u_int64_t        pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1230         u_int64_t        pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1231         u_int64_t        pfrts_match;
1232         u_int64_t        pfrts_nomatch;
1233         long             pfrts_tzero;
1234         int              pfrts_cnt;
1235         int              pfrts_refcnt[PFR_REFCNT_MAX];
1236 };
1237
1238 #ifdef _KERNEL
1239
1240 struct pfr_kstate_counter {
1241         counter_u64_t   pkc_pcpu;
1242         u_int64_t       pkc_zero;
1243 };
1244
1245 static inline int
1246 pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags)
1247 {
1248
1249         pfrc->pkc_zero = 0;
1250         pfrc->pkc_pcpu = counter_u64_alloc(flags);
1251         if (pfrc->pkc_pcpu == NULL)
1252                 return (ENOMEM);
1253         return (0);
1254 }
1255
1256 static inline void
1257 pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc)
1258 {
1259
1260         counter_u64_free(pfrc->pkc_pcpu);
1261 }
1262
1263 static inline u_int64_t
1264 pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc)
1265 {
1266         u_int64_t c;
1267
1268         c = counter_u64_fetch(pfrc->pkc_pcpu);
1269         c -= pfrc->pkc_zero;
1270         return (c);
1271 }
1272
1273 static inline void
1274 pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc)
1275 {
1276         u_int64_t c;
1277
1278         c = counter_u64_fetch(pfrc->pkc_pcpu);
1279         pfrc->pkc_zero = c;
1280 }
1281
1282 static inline void
1283 pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n)
1284 {
1285
1286         counter_u64_add(pfrc->pkc_pcpu, n);
1287 }
1288
1289 struct pfr_ktstats {
1290         struct pfr_table pfrts_t;
1291         struct pfr_kstate_counter        pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1292         struct pfr_kstate_counter        pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1293         struct pfr_kstate_counter        pfrkts_match;
1294         struct pfr_kstate_counter        pfrkts_nomatch;
1295         long             pfrkts_tzero;
1296         int              pfrkts_cnt;
1297         int              pfrkts_refcnt[PFR_REFCNT_MAX];
1298 };
1299
1300 #endif /* _KERNEL */
1301
1302 #define pfrts_name      pfrts_t.pfrt_name
1303 #define pfrts_flags     pfrts_t.pfrt_flags
1304
1305 #ifndef _SOCKADDR_UNION_DEFINED
1306 #define _SOCKADDR_UNION_DEFINED
1307 union sockaddr_union {
1308         struct sockaddr         sa;
1309         struct sockaddr_in      sin;
1310         struct sockaddr_in6     sin6;
1311 };
1312 #endif /* _SOCKADDR_UNION_DEFINED */
1313
1314 struct pfr_kcounters {
1315         counter_u64_t            pfrkc_counters;
1316         long                     pfrkc_tzero;
1317 };
1318 #define pfr_kentry_counter(kc, dir, op, t)              \
1319         ((kc)->pfrkc_counters +                         \
1320             (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t))
1321
1322 #ifdef _KERNEL
1323 SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
1324 struct pfr_kentry {
1325         struct radix_node        pfrke_node[2];
1326         union sockaddr_union     pfrke_sa;
1327         SLIST_ENTRY(pfr_kentry)  pfrke_workq;
1328         struct pfr_kcounters     pfrke_counters;
1329         u_int8_t                 pfrke_af;
1330         u_int8_t                 pfrke_net;
1331         u_int8_t                 pfrke_not;
1332         u_int8_t                 pfrke_mark;
1333 };
1334
1335 SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
1336 RB_HEAD(pfr_ktablehead, pfr_ktable);
1337 struct pfr_ktable {
1338         struct pfr_ktstats       pfrkt_kts;
1339         RB_ENTRY(pfr_ktable)     pfrkt_tree;
1340         SLIST_ENTRY(pfr_ktable)  pfrkt_workq;
1341         struct radix_node_head  *pfrkt_ip4;
1342         struct radix_node_head  *pfrkt_ip6;
1343         struct pfr_ktable       *pfrkt_shadow;
1344         struct pfr_ktable       *pfrkt_root;
1345         struct pf_kruleset      *pfrkt_rs;
1346         long                     pfrkt_larg;
1347         int                      pfrkt_nflags;
1348 };
1349 #define pfrkt_t         pfrkt_kts.pfrts_t
1350 #define pfrkt_name      pfrkt_t.pfrt_name
1351 #define pfrkt_anchor    pfrkt_t.pfrt_anchor
1352 #define pfrkt_ruleset   pfrkt_t.pfrt_ruleset
1353 #define pfrkt_flags     pfrkt_t.pfrt_flags
1354 #define pfrkt_cnt       pfrkt_kts.pfrkts_cnt
1355 #define pfrkt_refcnt    pfrkt_kts.pfrkts_refcnt
1356 #define pfrkt_packets   pfrkt_kts.pfrkts_packets
1357 #define pfrkt_bytes     pfrkt_kts.pfrkts_bytes
1358 #define pfrkt_match     pfrkt_kts.pfrkts_match
1359 #define pfrkt_nomatch   pfrkt_kts.pfrkts_nomatch
1360 #define pfrkt_tzero     pfrkt_kts.pfrkts_tzero
1361 #endif
1362
1363 #ifdef _KERNEL
1364 struct pfi_kkif {
1365         char                             pfik_name[IFNAMSIZ];
1366         union {
1367                 RB_ENTRY(pfi_kkif)       _pfik_tree;
1368                 LIST_ENTRY(pfi_kkif)     _pfik_list;
1369         } _pfik_glue;
1370 #define pfik_tree       _pfik_glue._pfik_tree
1371 #define pfik_list       _pfik_glue._pfik_list
1372         struct pf_counter_u64            pfik_packets[2][2][2];
1373         struct pf_counter_u64            pfik_bytes[2][2][2];
1374         u_int32_t                        pfik_tzero;
1375         u_int                            pfik_flags;
1376         struct ifnet                    *pfik_ifp;
1377         struct ifg_group                *pfik_group;
1378         u_int                            pfik_rulerefs;
1379         TAILQ_HEAD(, pfi_dynaddr)        pfik_dynaddrs;
1380 #ifdef PF_WANT_32_TO_64_COUNTER
1381         LIST_ENTRY(pfi_kkif)             pfik_allkiflist;
1382 #endif
1383 };
1384 #endif
1385
1386 #define PFI_IFLAG_REFS          0x0001  /* has state references */
1387 #define PFI_IFLAG_SKIP          0x0100  /* skip filtering on interface */
1388
1389 #ifdef _KERNEL
1390 struct pf_pdesc {
1391         struct {
1392                 int      done;
1393                 uid_t    uid;
1394                 gid_t    gid;
1395         }                lookup;
1396         u_int64_t        tot_len;       /* Make Mickey money */
1397         union pf_headers {
1398                 struct tcphdr           tcp;
1399                 struct udphdr           udp;
1400                 struct icmp             icmp;
1401 #ifdef INET6
1402                 struct icmp6_hdr        icmp6;
1403 #endif /* INET6 */
1404                 char any[0];
1405         } hdr;
1406
1407         struct pf_krule *nat_rule;      /* nat/rdr rule applied to packet */
1408         struct pf_addr  *src;           /* src address */
1409         struct pf_addr  *dst;           /* dst address */
1410         u_int16_t *sport;
1411         u_int16_t *dport;
1412         struct pf_mtag  *pf_mtag;
1413         struct pf_rule_actions  act;
1414
1415         u_int32_t        p_len;         /* total length of payload */
1416
1417         u_int16_t       *ip_sum;
1418         u_int16_t       *proto_sum;
1419         u_int16_t        flags;         /* Let SCRUB trigger behavior in
1420                                          * state code. Easier than tags */
1421 #define PFDESC_TCP_NORM 0x0001          /* TCP shall be statefully scrubbed */
1422 #define PFDESC_IP_REAS  0x0002          /* IP frags would've been reassembled */
1423         sa_family_t      af;
1424         u_int8_t         proto;
1425         u_int8_t         tos;
1426         u_int8_t         dir;           /* direction */
1427         u_int8_t         sidx;          /* key index for source */
1428         u_int8_t         didx;          /* key index for destination */
1429 };
1430 #endif
1431
1432 /* flags for RDR options */
1433 #define PF_DPORT_RANGE  0x01            /* Dest port uses range */
1434 #define PF_RPORT_RANGE  0x02            /* RDR'ed port uses range */
1435
1436 /* UDP state enumeration */
1437 #define PFUDPS_NO_TRAFFIC       0
1438 #define PFUDPS_SINGLE           1
1439 #define PFUDPS_MULTIPLE         2
1440
1441 #define PFUDPS_NSTATES          3       /* number of state levels */
1442
1443 #define PFUDPS_NAMES { \
1444         "NO_TRAFFIC", \
1445         "SINGLE", \
1446         "MULTIPLE", \
1447         NULL \
1448 }
1449
1450 /* Other protocol state enumeration */
1451 #define PFOTHERS_NO_TRAFFIC     0
1452 #define PFOTHERS_SINGLE         1
1453 #define PFOTHERS_MULTIPLE       2
1454
1455 #define PFOTHERS_NSTATES        3       /* number of state levels */
1456
1457 #define PFOTHERS_NAMES { \
1458         "NO_TRAFFIC", \
1459         "SINGLE", \
1460         "MULTIPLE", \
1461         NULL \
1462 }
1463
1464 #define ACTION_SET(a, x) \
1465         do { \
1466                 if ((a) != NULL) \
1467                         *(a) = (x); \
1468         } while (0)
1469
1470 #define REASON_SET(a, x) \
1471         do { \
1472                 if ((a) != NULL) \
1473                         *(a) = (x); \
1474                 if (x < PFRES_MAX) \
1475                         counter_u64_add(V_pf_status.counters[x], 1); \
1476         } while (0)
1477
1478 enum pf_syncookies_mode {
1479         PF_SYNCOOKIES_NEVER = 0,
1480         PF_SYNCOOKIES_ALWAYS = 1,
1481         PF_SYNCOOKIES_ADAPTIVE = 2,
1482         PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE
1483 };
1484
1485 #define PF_SYNCOOKIES_HIWATPCT  25
1486 #define PF_SYNCOOKIES_LOWATPCT  (PF_SYNCOOKIES_HIWATPCT / 2)
1487
1488 #ifdef _KERNEL
1489 struct pf_kstatus {
1490         counter_u64_t   counters[PFRES_MAX]; /* reason for passing/dropping */
1491         counter_u64_t   lcounters[KLCNT_MAX]; /* limit counters */
1492         struct pf_counter_u64   fcounters[FCNT_MAX]; /* state operation counters */
1493         counter_u64_t   scounters[SCNT_MAX]; /* src_node operation counters */
1494         uint32_t        states;
1495         uint32_t        src_nodes;
1496         uint32_t        running;
1497         uint32_t        since;
1498         uint32_t        debug;
1499         uint32_t        hostid;
1500         char            ifname[IFNAMSIZ];
1501         uint8_t         pf_chksum[PF_MD5_DIGEST_LENGTH];
1502         bool            keep_counters;
1503         enum pf_syncookies_mode syncookies_mode;
1504         bool            syncookies_active;
1505         uint64_t        syncookies_inflight[2];
1506         uint32_t        states_halfopen;
1507 };
1508 #endif
1509
1510 struct pf_divert {
1511         union {
1512                 struct in_addr  ipv4;
1513                 struct in6_addr ipv6;
1514         }               addr;
1515         u_int16_t       port;
1516 };
1517
1518 #define PFFRAG_FRENT_HIWAT      5000    /* Number of fragment entries */
1519 #define PFR_KENTRY_HIWAT        200000  /* Number of table entries */
1520
1521 /*
1522  * Limit the length of the fragment queue traversal.  Remember
1523  * search entry points based on the fragment offset.
1524  */
1525 #define PF_FRAG_ENTRY_POINTS            16
1526
1527 /*
1528  * The number of entries in the fragment queue must be limited
1529  * to avoid DoS by linear seaching.  Instead of a global limit,
1530  * use a limit per entry point.  For large packets these sum up.
1531  */
1532 #define PF_FRAG_ENTRY_LIMIT             64
1533
1534 /*
1535  * ioctl parameter structures
1536  */
1537
1538 struct pfioc_pooladdr {
1539         u_int32_t                action;
1540         u_int32_t                ticket;
1541         u_int32_t                nr;
1542         u_int32_t                r_num;
1543         u_int8_t                 r_action;
1544         u_int8_t                 r_last;
1545         u_int8_t                 af;
1546         char                     anchor[MAXPATHLEN];
1547         struct pf_pooladdr       addr;
1548 };
1549
1550 struct pfioc_rule {
1551         u_int32_t        action;
1552         u_int32_t        ticket;
1553         u_int32_t        pool_ticket;
1554         u_int32_t        nr;
1555         char             anchor[MAXPATHLEN];
1556         char             anchor_call[MAXPATHLEN];
1557         struct pf_rule   rule;
1558 };
1559
1560 struct pfioc_natlook {
1561         struct pf_addr   saddr;
1562         struct pf_addr   daddr;
1563         struct pf_addr   rsaddr;
1564         struct pf_addr   rdaddr;
1565         u_int16_t        sport;
1566         u_int16_t        dport;
1567         u_int16_t        rsport;
1568         u_int16_t        rdport;
1569         sa_family_t      af;
1570         u_int8_t         proto;
1571         u_int8_t         direction;
1572 };
1573
1574 struct pfioc_state {
1575         struct pfsync_state     state;
1576 };
1577
1578 struct pfioc_src_node_kill {
1579         sa_family_t psnk_af;
1580         struct pf_rule_addr psnk_src;
1581         struct pf_rule_addr psnk_dst;
1582         u_int               psnk_killed;
1583 };
1584
1585 #ifdef _KERNEL
1586 struct pf_kstate_kill {
1587         struct pf_state_cmp     psk_pfcmp;
1588         sa_family_t             psk_af;
1589         int                     psk_proto;
1590         struct pf_rule_addr     psk_src;
1591         struct pf_rule_addr     psk_dst;
1592         struct pf_rule_addr     psk_rt_addr;
1593         char                    psk_ifname[IFNAMSIZ];
1594         char                    psk_label[PF_RULE_LABEL_SIZE];
1595         u_int                   psk_killed;
1596         bool                    psk_kill_match;
1597 };
1598 #endif
1599
1600 struct pfioc_state_kill {
1601         struct pf_state_cmp     psk_pfcmp;
1602         sa_family_t             psk_af;
1603         int                     psk_proto;
1604         struct pf_rule_addr     psk_src;
1605         struct pf_rule_addr     psk_dst;
1606         char                    psk_ifname[IFNAMSIZ];
1607         char                    psk_label[PF_RULE_LABEL_SIZE];
1608         u_int                   psk_killed;
1609 };
1610
1611 struct pfioc_states {
1612         int     ps_len;
1613         union {
1614                 caddr_t                  psu_buf;
1615                 struct pfsync_state     *psu_states;
1616         } ps_u;
1617 #define ps_buf          ps_u.psu_buf
1618 #define ps_states       ps_u.psu_states
1619 };
1620
1621 struct pfioc_states_v2 {
1622         int             ps_len;
1623         uint64_t        ps_req_version;
1624         union {
1625                 caddr_t                  psu_buf;
1626                 struct pf_state_export  *psu_states;
1627         } ps_u;
1628 #define ps_buf          ps_u.psu_buf
1629 #define ps_states       ps_u.psu_states
1630 };
1631
1632 struct pfioc_src_nodes {
1633         int     psn_len;
1634         union {
1635                 caddr_t          psu_buf;
1636                 struct pf_src_node      *psu_src_nodes;
1637         } psn_u;
1638 #define psn_buf         psn_u.psu_buf
1639 #define psn_src_nodes   psn_u.psu_src_nodes
1640 };
1641
1642 struct pfioc_if {
1643         char             ifname[IFNAMSIZ];
1644 };
1645
1646 struct pfioc_tm {
1647         int              timeout;
1648         int              seconds;
1649 };
1650
1651 struct pfioc_limit {
1652         int              index;
1653         unsigned         limit;
1654 };
1655
1656 struct pfioc_altq_v0 {
1657         u_int32_t        action;
1658         u_int32_t        ticket;
1659         u_int32_t        nr;
1660         struct pf_altq_v0 altq;
1661 };
1662
1663 struct pfioc_altq_v1 {
1664         u_int32_t        action;
1665         u_int32_t        ticket;
1666         u_int32_t        nr;
1667         /*
1668          * Placed here so code that only uses the above parameters can be
1669          * written entirely in terms of the v0 or v1 type.
1670          */
1671         u_int32_t        version;
1672         struct pf_altq_v1 altq;
1673 };
1674
1675 /*
1676  * Latest version of struct pfioc_altq_vX.  This must move in lock-step with
1677  * the latest version of struct pf_altq_vX as it has that struct as a
1678  * member.
1679  */
1680 #define PFIOC_ALTQ_VERSION      PF_ALTQ_VERSION
1681
1682 struct pfioc_qstats_v0 {
1683         u_int32_t        ticket;
1684         u_int32_t        nr;
1685         void            *buf;
1686         int              nbytes;
1687         u_int8_t         scheduler;
1688 };
1689
1690 struct pfioc_qstats_v1 {
1691         u_int32_t        ticket;
1692         u_int32_t        nr;
1693         void            *buf;
1694         int              nbytes;
1695         u_int8_t         scheduler;
1696         /*
1697          * Placed here so code that only uses the above parameters can be
1698          * written entirely in terms of the v0 or v1 type.
1699          */
1700         u_int32_t        version;  /* Requested version of stats struct */
1701 };
1702
1703 /* Latest version of struct pfioc_qstats_vX */
1704 #define PFIOC_QSTATS_VERSION    1
1705
1706 struct pfioc_ruleset {
1707         u_int32_t        nr;
1708         char             path[MAXPATHLEN];
1709         char             name[PF_ANCHOR_NAME_SIZE];
1710 };
1711
1712 #define PF_RULESET_ALTQ         (PF_RULESET_MAX)
1713 #define PF_RULESET_TABLE        (PF_RULESET_MAX+1)
1714 #define PF_RULESET_ETH          (PF_RULESET_MAX+2)
1715 struct pfioc_trans {
1716         int              size;  /* number of elements */
1717         int              esize; /* size of each element in bytes */
1718         struct pfioc_trans_e {
1719                 int             rs_num;
1720                 char            anchor[MAXPATHLEN];
1721                 u_int32_t       ticket;
1722         }               *array;
1723 };
1724
1725 #define PFR_FLAG_ATOMIC         0x00000001      /* unused */
1726 #define PFR_FLAG_DUMMY          0x00000002
1727 #define PFR_FLAG_FEEDBACK       0x00000004
1728 #define PFR_FLAG_CLSTATS        0x00000008
1729 #define PFR_FLAG_ADDRSTOO       0x00000010
1730 #define PFR_FLAG_REPLACE        0x00000020
1731 #define PFR_FLAG_ALLRSETS       0x00000040
1732 #define PFR_FLAG_ALLMASK        0x0000007F
1733 #ifdef _KERNEL
1734 #define PFR_FLAG_USERIOCTL      0x10000000
1735 #endif
1736
1737 struct pfioc_table {
1738         struct pfr_table         pfrio_table;
1739         void                    *pfrio_buffer;
1740         int                      pfrio_esize;
1741         int                      pfrio_size;
1742         int                      pfrio_size2;
1743         int                      pfrio_nadd;
1744         int                      pfrio_ndel;
1745         int                      pfrio_nchange;
1746         int                      pfrio_flags;
1747         u_int32_t                pfrio_ticket;
1748 };
1749 #define pfrio_exists    pfrio_nadd
1750 #define pfrio_nzero     pfrio_nadd
1751 #define pfrio_nmatch    pfrio_nadd
1752 #define pfrio_naddr     pfrio_size2
1753 #define pfrio_setflag   pfrio_size2
1754 #define pfrio_clrflag   pfrio_nadd
1755
1756 struct pfioc_iface {
1757         char     pfiio_name[IFNAMSIZ];
1758         void    *pfiio_buffer;
1759         int      pfiio_esize;
1760         int      pfiio_size;
1761         int      pfiio_nzero;
1762         int      pfiio_flags;
1763 };
1764
1765 /*
1766  * ioctl operations
1767  */
1768
1769 #define DIOCSTART       _IO  ('D',  1)
1770 #define DIOCSTOP        _IO  ('D',  2)
1771 #define DIOCADDRULE     _IOWR('D',  4, struct pfioc_rule)
1772 #define DIOCADDRULENV   _IOWR('D',  4, struct pfioc_nv)
1773 #define DIOCGETRULES    _IOWR('D',  6, struct pfioc_rule)
1774 #define DIOCGETRULE     _IOWR('D',  7, struct pfioc_rule)
1775 #define DIOCGETRULENV   _IOWR('D',  7, struct pfioc_nv)
1776 /* XXX cut 8 - 17 */
1777 #define DIOCCLRSTATES   _IOWR('D', 18, struct pfioc_state_kill)
1778 #define DIOCCLRSTATESNV _IOWR('D', 18, struct pfioc_nv)
1779 #define DIOCGETSTATE    _IOWR('D', 19, struct pfioc_state)
1780 #define DIOCGETSTATENV  _IOWR('D', 19, struct pfioc_nv)
1781 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
1782 #define DIOCGETSTATUS   _IOWR('D', 21, struct pf_status)
1783 #define DIOCGETSTATUSNV _IOWR('D', 21, struct pfioc_nv)
1784 #define DIOCCLRSTATUS   _IO  ('D', 22)
1785 #define DIOCNATLOOK     _IOWR('D', 23, struct pfioc_natlook)
1786 #define DIOCSETDEBUG    _IOWR('D', 24, u_int32_t)
1787 #define DIOCGETSTATES   _IOWR('D', 25, struct pfioc_states)
1788 #define DIOCCHANGERULE  _IOWR('D', 26, struct pfioc_rule)
1789 /* XXX cut 26 - 28 */
1790 #define DIOCSETTIMEOUT  _IOWR('D', 29, struct pfioc_tm)
1791 #define DIOCGETTIMEOUT  _IOWR('D', 30, struct pfioc_tm)
1792 #define DIOCADDSTATE    _IOWR('D', 37, struct pfioc_state)
1793 #define DIOCCLRRULECTRS _IO  ('D', 38)
1794 #define DIOCGETLIMIT    _IOWR('D', 39, struct pfioc_limit)
1795 #define DIOCSETLIMIT    _IOWR('D', 40, struct pfioc_limit)
1796 #define DIOCKILLSTATES  _IOWR('D', 41, struct pfioc_state_kill)
1797 #define DIOCKILLSTATESNV        _IOWR('D', 41, struct pfioc_nv)
1798 #define DIOCSTARTALTQ   _IO  ('D', 42)
1799 #define DIOCSTOPALTQ    _IO  ('D', 43)
1800 #define DIOCADDALTQV0   _IOWR('D', 45, struct pfioc_altq_v0)
1801 #define DIOCADDALTQV1   _IOWR('D', 45, struct pfioc_altq_v1)
1802 #define DIOCGETALTQSV0  _IOWR('D', 47, struct pfioc_altq_v0)
1803 #define DIOCGETALTQSV1  _IOWR('D', 47, struct pfioc_altq_v1)
1804 #define DIOCGETALTQV0   _IOWR('D', 48, struct pfioc_altq_v0)
1805 #define DIOCGETALTQV1   _IOWR('D', 48, struct pfioc_altq_v1)
1806 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0)
1807 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1)
1808 #define DIOCGETQSTATSV0 _IOWR('D', 50, struct pfioc_qstats_v0)
1809 #define DIOCGETQSTATSV1 _IOWR('D', 50, struct pfioc_qstats_v1)
1810 #define DIOCBEGINADDRS  _IOWR('D', 51, struct pfioc_pooladdr)
1811 #define DIOCADDADDR     _IOWR('D', 52, struct pfioc_pooladdr)
1812 #define DIOCGETADDRS    _IOWR('D', 53, struct pfioc_pooladdr)
1813 #define DIOCGETADDR     _IOWR('D', 54, struct pfioc_pooladdr)
1814 #define DIOCCHANGEADDR  _IOWR('D', 55, struct pfioc_pooladdr)
1815 /* XXX cut 55 - 57 */
1816 #define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset)
1817 #define DIOCGETRULESET  _IOWR('D', 59, struct pfioc_ruleset)
1818 #define DIOCRCLRTABLES  _IOWR('D', 60, struct pfioc_table)
1819 #define DIOCRADDTABLES  _IOWR('D', 61, struct pfioc_table)
1820 #define DIOCRDELTABLES  _IOWR('D', 62, struct pfioc_table)
1821 #define DIOCRGETTABLES  _IOWR('D', 63, struct pfioc_table)
1822 #define DIOCRGETTSTATS  _IOWR('D', 64, struct pfioc_table)
1823 #define DIOCRCLRTSTATS  _IOWR('D', 65, struct pfioc_table)
1824 #define DIOCRCLRADDRS   _IOWR('D', 66, struct pfioc_table)
1825 #define DIOCRADDADDRS   _IOWR('D', 67, struct pfioc_table)
1826 #define DIOCRDELADDRS   _IOWR('D', 68, struct pfioc_table)
1827 #define DIOCRSETADDRS   _IOWR('D', 69, struct pfioc_table)
1828 #define DIOCRGETADDRS   _IOWR('D', 70, struct pfioc_table)
1829 #define DIOCRGETASTATS  _IOWR('D', 71, struct pfioc_table)
1830 #define DIOCRCLRASTATS  _IOWR('D', 72, struct pfioc_table)
1831 #define DIOCRTSTADDRS   _IOWR('D', 73, struct pfioc_table)
1832 #define DIOCRSETTFLAGS  _IOWR('D', 74, struct pfioc_table)
1833 #define DIOCRINADEFINE  _IOWR('D', 77, struct pfioc_table)
1834 #define DIOCOSFPFLUSH   _IO('D', 78)
1835 #define DIOCOSFPADD     _IOWR('D', 79, struct pf_osfp_ioctl)
1836 #define DIOCOSFPGET     _IOWR('D', 80, struct pf_osfp_ioctl)
1837 #define DIOCXBEGIN      _IOWR('D', 81, struct pfioc_trans)
1838 #define DIOCXCOMMIT     _IOWR('D', 82, struct pfioc_trans)
1839 #define DIOCXROLLBACK   _IOWR('D', 83, struct pfioc_trans)
1840 #define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes)
1841 #define DIOCCLRSRCNODES _IO('D', 85)
1842 #define DIOCSETHOSTID   _IOWR('D', 86, u_int32_t)
1843 #define DIOCIGETIFACES  _IOWR('D', 87, struct pfioc_iface)
1844 #define DIOCSETIFFLAG   _IOWR('D', 89, struct pfioc_iface)
1845 #define DIOCCLRIFFLAG   _IOWR('D', 90, struct pfioc_iface)
1846 #define DIOCKILLSRCNODES        _IOWR('D', 91, struct pfioc_src_node_kill)
1847 #define DIOCGIFSPEEDV0  _IOWR('D', 92, struct pf_ifspeed_v0)
1848 #define DIOCGIFSPEEDV1  _IOWR('D', 92, struct pf_ifspeed_v1)
1849 #define DIOCGETSTATESV2 _IOWR('D', 93, struct pfioc_states_v2)
1850 #define DIOCGETSYNCOOKIES       _IOWR('D', 94, struct pfioc_nv)
1851 #define DIOCSETSYNCOOKIES       _IOWR('D', 95, struct pfioc_nv)
1852 #define DIOCKEEPCOUNTERS        _IOWR('D', 96, struct pfioc_nv)
1853 #define DIOCKEEPCOUNTERS_FREEBSD13      _IOWR('D', 92, struct pfioc_nv)
1854 #define DIOCADDETHRULE          _IOWR('D', 97, struct pfioc_nv)
1855 #define DIOCGETETHRULE          _IOWR('D', 98, struct pfioc_nv)
1856 #define DIOCGETETHRULES         _IOWR('D', 99, struct pfioc_nv)
1857
1858 struct pf_ifspeed_v0 {
1859         char                    ifname[IFNAMSIZ];
1860         u_int32_t               baudrate;
1861 };
1862
1863 struct pf_ifspeed_v1 {
1864         char                    ifname[IFNAMSIZ];
1865         u_int32_t               baudrate32;
1866         /* layout identical to struct pf_ifspeed_v0 up to this point */
1867         u_int64_t               baudrate;
1868 };
1869
1870 /* Latest version of struct pf_ifspeed_vX */
1871 #define PF_IFSPEED_VERSION      1
1872
1873 /*
1874  * Compatibility and convenience macros
1875  */
1876 #ifndef _KERNEL
1877 #ifdef PFIOC_USE_LATEST
1878 /*
1879  * Maintaining in-tree consumers of the ioctl interface is easier when that
1880  * code can be written in terms old names that refer to the latest interface
1881  * version as that reduces the required changes in the consumers to those
1882  * that are functionally necessary to accommodate a new interface version.
1883  */
1884 #define pfioc_altq      __CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION)
1885 #define pfioc_qstats    __CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION)
1886 #define pf_ifspeed      __CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION)
1887
1888 #define DIOCADDALTQ     __CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION)
1889 #define DIOCGETALTQS    __CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION)
1890 #define DIOCGETALTQ     __CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION)
1891 #define DIOCCHANGEALTQ  __CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION)
1892 #define DIOCGETQSTATS   __CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION)
1893 #define DIOCGIFSPEED    __CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION)
1894 #else
1895 /*
1896  * When building out-of-tree code that is written for the old interface,
1897  * such as may exist in ports for example, resolve the old struct tags and
1898  * ioctl command names to the v0 versions.
1899  */
1900 #define pfioc_altq      __CONCAT(pfioc_altq_v, 0)
1901 #define pfioc_qstats    __CONCAT(pfioc_qstats_v, 0)
1902 #define pf_ifspeed      __CONCAT(pf_ifspeed_v, 0)
1903
1904 #define DIOCADDALTQ     __CONCAT(DIOCADDALTQV, 0)
1905 #define DIOCGETALTQS    __CONCAT(DIOCGETALTQSV, 0)
1906 #define DIOCGETALTQ     __CONCAT(DIOCGETALTQV, 0)
1907 #define DIOCCHANGEALTQ  __CONCAT(DIOCCHANGEALTQV, 0)
1908 #define DIOCGETQSTATS   __CONCAT(DIOCGETQSTATSV, 0)
1909 #define DIOCGIFSPEED    __CONCAT(DIOCGIFSPEEDV, 0)
1910 #endif /* PFIOC_USE_LATEST */
1911 #endif /* _KERNEL */
1912
1913 #ifdef _KERNEL
1914 LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node);
1915 struct pf_srchash {
1916         struct pf_ksrc_node_list                nodes;
1917         struct mtx                      lock;
1918 };
1919
1920 struct pf_keyhash {
1921         LIST_HEAD(, pf_state_key)       keys;
1922         struct mtx                      lock;
1923 };
1924
1925 struct pf_idhash {
1926         LIST_HEAD(, pf_kstate)          states;
1927         struct mtx                      lock;
1928 };
1929
1930 extern u_long           pf_ioctl_maxcount;
1931 extern u_long           pf_hashmask;
1932 extern u_long           pf_srchashmask;
1933 #define PF_HASHSIZ      (131072)
1934 #define PF_SRCHASHSIZ   (PF_HASHSIZ/4)
1935 VNET_DECLARE(struct pf_keyhash *, pf_keyhash);
1936 VNET_DECLARE(struct pf_idhash *, pf_idhash);
1937 #define V_pf_keyhash    VNET(pf_keyhash)
1938 #define V_pf_idhash     VNET(pf_idhash)
1939 VNET_DECLARE(struct pf_srchash *, pf_srchash);
1940 #define V_pf_srchash    VNET(pf_srchash)
1941
1942 #define PF_IDHASH(s)    (be64toh((s)->id) % (pf_hashmask + 1))
1943
1944 VNET_DECLARE(void *, pf_swi_cookie);
1945 #define V_pf_swi_cookie VNET(pf_swi_cookie)
1946 VNET_DECLARE(struct intr_event *, pf_swi_ie);
1947 #define V_pf_swi_ie     VNET(pf_swi_ie)
1948
1949 VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]);
1950 #define V_pf_stateid    VNET(pf_stateid)
1951
1952 TAILQ_HEAD(pf_altqqueue, pf_altq);
1953 VNET_DECLARE(struct pf_altqqueue,        pf_altqs[4]);
1954 #define V_pf_altqs                       VNET(pf_altqs)
1955 VNET_DECLARE(struct pf_kpalist,          pf_pabuf);
1956 #define V_pf_pabuf                       VNET(pf_pabuf)
1957
1958 VNET_DECLARE(u_int32_t,                  ticket_altqs_active);
1959 #define V_ticket_altqs_active            VNET(ticket_altqs_active)
1960 VNET_DECLARE(u_int32_t,                  ticket_altqs_inactive);
1961 #define V_ticket_altqs_inactive          VNET(ticket_altqs_inactive)
1962 VNET_DECLARE(int,                        altqs_inactive_open);
1963 #define V_altqs_inactive_open            VNET(altqs_inactive_open)
1964 VNET_DECLARE(u_int32_t,                  ticket_pabuf);
1965 #define V_ticket_pabuf                   VNET(ticket_pabuf)
1966 VNET_DECLARE(struct pf_altqqueue *,      pf_altqs_active);
1967 #define V_pf_altqs_active                VNET(pf_altqs_active)
1968 VNET_DECLARE(struct pf_altqqueue *,      pf_altq_ifs_active);
1969 #define V_pf_altq_ifs_active             VNET(pf_altq_ifs_active)
1970 VNET_DECLARE(struct pf_altqqueue *,      pf_altqs_inactive);
1971 #define V_pf_altqs_inactive              VNET(pf_altqs_inactive)
1972 VNET_DECLARE(struct pf_altqqueue *,      pf_altq_ifs_inactive);
1973 #define V_pf_altq_ifs_inactive           VNET(pf_altq_ifs_inactive)
1974
1975 VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules);
1976 #define V_pf_unlinked_rules     VNET(pf_unlinked_rules)
1977
1978 #ifdef PF_WANT_32_TO_64_COUNTER
1979 LIST_HEAD(allkiflist_head, pfi_kkif);
1980 VNET_DECLARE(struct allkiflist_head, pf_allkiflist);
1981 #define V_pf_allkiflist     VNET(pf_allkiflist)
1982 VNET_DECLARE(size_t, pf_allkifcount);
1983 #define V_pf_allkifcount     VNET(pf_allkifcount)
1984 VNET_DECLARE(struct pfi_kkif *, pf_kifmarker);
1985 #define V_pf_kifmarker     VNET(pf_kifmarker)
1986
1987 LIST_HEAD(allrulelist_head, pf_krule);
1988 VNET_DECLARE(struct allrulelist_head, pf_allrulelist);
1989 #define V_pf_allrulelist     VNET(pf_allrulelist)
1990 VNET_DECLARE(size_t, pf_allrulecount);
1991 #define V_pf_allrulecount     VNET(pf_allrulecount)
1992 VNET_DECLARE(struct pf_krule *, pf_rulemarker);
1993 #define V_pf_rulemarker     VNET(pf_rulemarker)
1994 #endif
1995
1996 void                             pf_initialize(void);
1997 void                             pf_mtag_initialize(void);
1998 void                             pf_mtag_cleanup(void);
1999 void                             pf_cleanup(void);
2000
2001 struct pf_mtag                  *pf_get_mtag(struct mbuf *);
2002
2003 extern void                      pf_calc_skip_steps(struct pf_krulequeue *);
2004 #ifdef ALTQ
2005 extern  void                     pf_altq_ifnet_event(struct ifnet *, int);
2006 #endif
2007 VNET_DECLARE(uma_zone_t,         pf_state_z);
2008 #define V_pf_state_z             VNET(pf_state_z)
2009 VNET_DECLARE(uma_zone_t,         pf_state_key_z);
2010 #define V_pf_state_key_z         VNET(pf_state_key_z)
2011 VNET_DECLARE(uma_zone_t,         pf_state_scrub_z);
2012 #define V_pf_state_scrub_z       VNET(pf_state_scrub_z)
2013
2014 extern void                      pf_purge_thread(void *);
2015 extern void                      pf_unload_vnet_purge(void);
2016 extern void                      pf_intr(void *);
2017 extern void                      pf_purge_expired_src_nodes(void);
2018
2019 extern int                       pf_unlink_state(struct pf_kstate *);
2020 extern int                       pf_state_insert(struct pfi_kkif *,
2021                                     struct pfi_kkif *,
2022                                     struct pf_state_key *,
2023                                     struct pf_state_key *,
2024                                     struct pf_kstate *);
2025 extern struct pf_kstate         *pf_alloc_state(int);
2026 extern void                      pf_free_state(struct pf_kstate *);
2027
2028 static __inline void
2029 pf_ref_state(struct pf_kstate *s)
2030 {
2031
2032         refcount_acquire(&s->refs);
2033 }
2034
2035 static __inline int
2036 pf_release_state(struct pf_kstate *s)
2037 {
2038
2039         if (refcount_release(&s->refs)) {
2040                 pf_free_state(s);
2041                 return (1);
2042         } else
2043                 return (0);
2044 }
2045
2046 static __inline int
2047 pf_release_staten(struct pf_kstate *s, u_int n)
2048 {
2049
2050         if (refcount_releasen(&s->refs, n)) {
2051                 pf_free_state(s);
2052                 return (1);
2053         } else
2054                 return (0);
2055 }
2056
2057 extern struct pf_kstate         *pf_find_state_byid(uint64_t, uint32_t);
2058 extern struct pf_kstate         *pf_find_state_all(struct pf_state_key_cmp *,
2059                                     u_int, int *);
2060 extern bool                     pf_find_state_all_exists(struct pf_state_key_cmp *,
2061                                     u_int);
2062 extern struct pf_ksrc_node      *pf_find_src_node(struct pf_addr *,
2063                                     struct pf_krule *, sa_family_t, int);
2064 extern void                      pf_unlink_src_node(struct pf_ksrc_node *);
2065 extern u_int                     pf_free_src_nodes(struct pf_ksrc_node_list *);
2066 extern void                      pf_print_state(struct pf_kstate *);
2067 extern void                      pf_print_flags(u_int8_t);
2068 extern u_int16_t                 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
2069                                     u_int8_t);
2070 extern u_int16_t                 pf_proto_cksum_fixup(struct mbuf *, u_int16_t,
2071                                     u_int16_t, u_int16_t, u_int8_t);
2072
2073 VNET_DECLARE(struct ifnet *,             sync_ifp);
2074 #define V_sync_ifp                       VNET(sync_ifp);
2075 VNET_DECLARE(struct pf_krule,            pf_default_rule);
2076 #define V_pf_default_rule                 VNET(pf_default_rule)
2077 extern void                      pf_addrcpy(struct pf_addr *, struct pf_addr *,
2078                                     u_int8_t);
2079 void                            pf_free_rule(struct pf_krule *);
2080
2081 int     pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
2082 #ifdef INET
2083 int     pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
2084 int     pf_normalize_ip(struct mbuf **, int, struct pfi_kkif *, u_short *,
2085             struct pf_pdesc *);
2086 #endif /* INET */
2087
2088 #ifdef INET6
2089 int     pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
2090 int     pf_normalize_ip6(struct mbuf **, int, struct pfi_kkif *, u_short *,
2091             struct pf_pdesc *);
2092 void    pf_poolmask(struct pf_addr *, struct pf_addr*,
2093             struct pf_addr *, struct pf_addr *, u_int8_t);
2094 void    pf_addr_inc(struct pf_addr *, sa_family_t);
2095 int     pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *);
2096 #endif /* INET6 */
2097
2098 u_int32_t       pf_new_isn(struct pf_kstate *);
2099 void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
2100             sa_family_t);
2101 void    pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
2102 void    pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t,
2103             u_int8_t);
2104 void    pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t);
2105 void    pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t,
2106             bool, u_int8_t);
2107 void    pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t,
2108     bool, u_int8_t);
2109 void    pf_send_deferred_syn(struct pf_kstate *);
2110 int     pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
2111             struct pf_addr *, sa_family_t);
2112 int     pf_match_addr_range(struct pf_addr *, struct pf_addr *,
2113             struct pf_addr *, sa_family_t);
2114 int     pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
2115
2116 void    pf_normalize_init(void);
2117 void    pf_normalize_cleanup(void);
2118 int     pf_normalize_tcp(int, struct pfi_kkif *, struct mbuf *, int, int, void *,
2119             struct pf_pdesc *);
2120 void    pf_normalize_tcp_cleanup(struct pf_kstate *);
2121 int     pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
2122             struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
2123 int     pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
2124             u_short *, struct tcphdr *, struct pf_kstate *,
2125             struct pf_state_peer *, struct pf_state_peer *, int *);
2126 u_int32_t
2127         pf_state_expires(const struct pf_kstate *);
2128 void    pf_purge_expired_fragments(void);
2129 void    pf_purge_fragments(uint32_t);
2130 int     pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *,
2131             int);
2132 int     pf_socket_lookup(int, struct pf_pdesc *, struct mbuf *);
2133 struct pf_state_key *pf_alloc_state_key(int);
2134 void    pfr_initialize(void);
2135 void    pfr_cleanup(void);
2136 int     pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
2137 void    pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
2138             u_int64_t, int, int, int);
2139 int     pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t);
2140 void    pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
2141 struct pfr_ktable *
2142         pfr_attach_table(struct pf_kruleset *, char *);
2143 void    pfr_detach_table(struct pfr_ktable *);
2144 int     pfr_clr_tables(struct pfr_table *, int *, int);
2145 int     pfr_add_tables(struct pfr_table *, int, int *, int);
2146 int     pfr_del_tables(struct pfr_table *, int, int *, int);
2147 int     pfr_table_count(struct pfr_table *, int);
2148 int     pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
2149 int     pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
2150 int     pfr_clr_tstats(struct pfr_table *, int, int *, int);
2151 int     pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
2152 int     pfr_clr_addrs(struct pfr_table *, int *, int);
2153 int     pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
2154 int     pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
2155             int);
2156 int     pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
2157             int);
2158 int     pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
2159             int *, int *, int *, int, u_int32_t);
2160 int     pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
2161 int     pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
2162 int     pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
2163             int);
2164 int     pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
2165             int);
2166 int     pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
2167 int     pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
2168 int     pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
2169 int     pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
2170             int *, u_int32_t, int);
2171
2172 MALLOC_DECLARE(PFI_MTYPE);
2173 VNET_DECLARE(struct pfi_kkif *,          pfi_all);
2174 #define V_pfi_all                        VNET(pfi_all)
2175
2176 void             pfi_initialize(void);
2177 void             pfi_initialize_vnet(void);
2178 void             pfi_cleanup(void);
2179 void             pfi_cleanup_vnet(void);
2180 void             pfi_kkif_ref(struct pfi_kkif *);
2181 void             pfi_kkif_unref(struct pfi_kkif *);
2182 struct pfi_kkif *pfi_kkif_find(const char *);
2183 struct pfi_kkif *pfi_kkif_attach(struct pfi_kkif *, const char *);
2184 int              pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *);
2185 void             pfi_kkif_purge(void);
2186 int              pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
2187                     sa_family_t);
2188 int              pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
2189 void             pfi_dynaddr_remove(struct pfi_dynaddr *);
2190 void             pfi_dynaddr_copyout(struct pf_addr_wrap *);
2191 void             pfi_update_status(const char *, struct pf_status *);
2192 void             pfi_get_ifaces(const char *, struct pfi_kif *, int *);
2193 int              pfi_set_flags(const char *, int);
2194 int              pfi_clear_flags(const char *, int);
2195
2196 int              pf_match_tag(struct mbuf *, struct pf_krule *, int *, int);
2197 int              pf_tag_packet(struct mbuf *, struct pf_pdesc *, int);
2198 int              pf_addr_cmp(struct pf_addr *, struct pf_addr *,
2199                     sa_family_t);
2200
2201 u_int16_t        pf_get_mss(struct mbuf *, int, u_int16_t, sa_family_t);
2202 u_int8_t         pf_get_wscale(struct mbuf *, int, u_int16_t, sa_family_t);
2203 struct mbuf     *pf_build_tcp(const struct pf_krule *, sa_family_t,
2204                     const struct pf_addr *, const struct pf_addr *,
2205                     u_int16_t, u_int16_t, u_int32_t, u_int32_t,
2206                     u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
2207                     u_int16_t);
2208 void             pf_send_tcp(const struct pf_krule *, sa_family_t,
2209                             const struct pf_addr *, const struct pf_addr *,
2210                             u_int16_t, u_int16_t, u_int32_t, u_int32_t,
2211                             u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
2212                             u_int16_t);
2213
2214 void                     pf_syncookies_init(void);
2215 void                     pf_syncookies_cleanup(void);
2216 int                      pf_get_syncookies(struct pfioc_nv *);
2217 int                      pf_set_syncookies(struct pfioc_nv *);
2218 int                      pf_synflood_check(struct pf_pdesc *);
2219 void                     pf_syncookie_send(struct mbuf *m, int off,
2220                             struct pf_pdesc *);
2221 u_int8_t                 pf_syncookie_validate(struct pf_pdesc *);
2222 struct mbuf *            pf_syncookie_recreate_syn(uint8_t, int,
2223                             struct pf_pdesc *);
2224
2225 VNET_DECLARE(struct pf_kstatus, pf_status);
2226 #define V_pf_status     VNET(pf_status)
2227
2228 struct pf_limit {
2229         uma_zone_t      zone;
2230         u_int           limit;
2231 };
2232 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
2233 #define V_pf_limits VNET(pf_limits)
2234
2235 #endif /* _KERNEL */
2236
2237 #ifdef _KERNEL
2238 VNET_DECLARE(struct pf_kanchor_global,           pf_anchors);
2239 #define V_pf_anchors                             VNET(pf_anchors)
2240 VNET_DECLARE(struct pf_kanchor,                  pf_main_anchor);
2241 #define V_pf_main_anchor                         VNET(pf_main_anchor)
2242 VNET_DECLARE(struct pf_keth_anchor_global,       pf_keth_anchors);
2243 #define V_pf_keth_anchors                        VNET(pf_keth_anchors)
2244 #define pf_main_ruleset V_pf_main_anchor.ruleset
2245
2246 VNET_DECLARE(struct pf_keth_anchor,              pf_main_keth_anchor);
2247 #define V_pf_main_keth_anchor                    VNET(pf_main_keth_anchor)
2248 VNET_DECLARE(struct pf_keth_ruleset*,            pf_keth);
2249 #define V_pf_keth                                VNET(pf_keth)
2250
2251 void                     pf_init_kruleset(struct pf_kruleset *);
2252 void                     pf_init_keth(struct pf_keth_ruleset *);
2253 int                      pf_kanchor_setup(struct pf_krule *,
2254                             const struct pf_kruleset *, const char *);
2255 int                      pf_kanchor_nvcopyout(const struct pf_kruleset *,
2256                             const struct pf_krule *, nvlist_t *);
2257 int                      pf_kanchor_copyout(const struct pf_kruleset *,
2258                             const struct pf_krule *, struct pfioc_rule *);
2259 void                     pf_kanchor_remove(struct pf_krule *);
2260 void                     pf_remove_if_empty_kruleset(struct pf_kruleset *);
2261 struct pf_kruleset      *pf_find_kruleset(const char *);
2262 struct pf_kruleset      *pf_find_or_create_kruleset(const char *);
2263 void                     pf_rs_initialize(void);
2264
2265
2266 struct pf_krule         *pf_krule_alloc(void);
2267
2268 void                     pf_remove_if_empty_keth_ruleset(
2269                             struct pf_keth_ruleset *);
2270 struct pf_keth_ruleset  *pf_find_keth_ruleset(const char *);
2271 struct pf_keth_anchor   *pf_find_keth_anchor(const char *);
2272 int                      pf_keth_anchor_setup(struct pf_keth_rule *,
2273                             const struct pf_keth_ruleset *, const char *);
2274 int                      pf_keth_anchor_nvcopyout(
2275                             const struct pf_keth_ruleset *,
2276                             const struct pf_keth_rule *, nvlist_t *);
2277 struct pf_keth_ruleset  *pf_find_or_create_keth_ruleset(const char *);
2278 void                     pf_keth_anchor_remove(struct pf_keth_rule *);
2279
2280 void                     pf_krule_free(struct pf_krule *);
2281 #endif
2282
2283 /* The fingerprint functions can be linked into userland programs (tcpdump) */
2284 int     pf_osfp_add(struct pf_osfp_ioctl *);
2285 #ifdef _KERNEL
2286 struct pf_osfp_enlist *
2287         pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
2288             const struct tcphdr *);
2289 #endif /* _KERNEL */
2290 void    pf_osfp_flush(void);
2291 int     pf_osfp_get(struct pf_osfp_ioctl *);
2292 int     pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
2293
2294 #ifdef _KERNEL
2295 void                     pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
2296
2297 void                     pf_step_into_anchor(struct pf_kanchor_stackframe *, int *,
2298                             struct pf_kruleset **, int, struct pf_krule **,
2299                             struct pf_krule **, int *);
2300 int                      pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *,
2301                             struct pf_kruleset **, int, struct pf_krule **,
2302                             struct pf_krule **, int *);
2303 void                     pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *,
2304                             int *, struct pf_keth_ruleset **,
2305                             struct pf_keth_rule **, struct pf_keth_rule **,
2306                             int *);
2307 int                      pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *,
2308                             int *, struct pf_keth_ruleset **,
2309                             struct pf_keth_rule **, struct pf_keth_rule **,
2310                             int *);
2311
2312 int                      pf_map_addr(u_int8_t, struct pf_krule *,
2313                             struct pf_addr *, struct pf_addr *,
2314                             struct pf_addr *, struct pf_ksrc_node **);
2315 struct pf_krule         *pf_get_translation(struct pf_pdesc *, struct mbuf *,
2316                             int, int, struct pfi_kkif *, struct pf_ksrc_node **,
2317                             struct pf_state_key **, struct pf_state_key **,
2318                             struct pf_addr *, struct pf_addr *,
2319                             uint16_t, uint16_t, struct pf_kanchor_stackframe *);
2320
2321 struct pf_state_key     *pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
2322                             struct pf_addr *, u_int16_t, u_int16_t);
2323 struct pf_state_key     *pf_state_key_clone(struct pf_state_key *);
2324
2325 struct pfi_kkif         *pf_kkif_create(int);
2326 void                     pf_kkif_free(struct pfi_kkif *);
2327 void                     pf_kkif_zero(struct pfi_kkif *);
2328 #endif /* _KERNEL */
2329
2330 #endif /* _NET_PFVAR_H_ */