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