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