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