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