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