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