]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/pfvar.h
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / net / pfvar.h
1 /*
2 * Copyright (c) 2007-2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* $apfw: git commit b6bf13f8321283cd7ee82b1795e86506084b1b95 $ */
30 /* $OpenBSD: pfvar.h,v 1.259 2007/12/02 12:08:04 pascoe Exp $ */
31
32 /*
33 * Copyright (c) 2001 Daniel Hartmeier
34 * NAT64 - Copyright (c) 2010 Viagenie Inc. (http://www.viagenie.ca)
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 *
41 * - Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * - Redistributions in binary form must reproduce the above
44 * copyright notice, this list of conditions and the following
45 * disclaimer in the documentation and/or other materials provided
46 * with the distribution.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 *
61 */
62
63 #ifndef _NET_PFVAR_H_
64 #define _NET_PFVAR_H_
65
66 #ifdef PRIVATE
67 /*
68 * XXX
69 * XXX Private interfaces. Do not include this file; use pfctl(8) instead.
70 * XXX
71 */
72 #if PF || !defined(KERNEL)
73
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77
78 #include <stdbool.h>
79 #include <sys/param.h>
80 #include <sys/types.h>
81 #include <sys/queue.h>
82 #include <libkern/tree.h>
83
84 #include <net/radix.h>
85 #include <netinet/in.h>
86 #include <net/if_var.h>
87 #ifdef KERNEL
88 #include <kern/kern_types.h>
89 #include <kern/zalloc.h>
90 #include <kern/locks.h>
91
92 #include <machine/endian.h>
93 #include <sys/systm.h>
94 #include <net/pf_pbuf.h>
95
96
97 #if BYTE_ORDER == BIG_ENDIAN
98 #define htobe64(x) (x)
99 #else /* LITTLE ENDIAN */
100 #define htobe64(x) __DARWIN_OSSwapInt64(x)
101 #endif /* LITTLE_ENDIAN */
102
103 #define be64toh(x) htobe64(x)
104
105 extern lck_rw_t *pf_perim_lock;
106 extern lck_mtx_t *pf_lock;
107
108 struct pool {
109 struct zone *pool_zone; /* pointer to backend zone */
110 const char *pool_name; /* name of pool */
111 unsigned int pool_count; /* # of outstanding elements */
112 unsigned int pool_hiwat; /* high watermark */
113 unsigned int pool_limit; /* hard limit */
114 unsigned int pool_fails; /* # of failed allocs due to limit */
115 };
116
117 #define PR_NOWAIT FALSE
118 #define PR_WAITOK TRUE
119
120 __private_extern__ void pool_init(struct pool *, size_t, unsigned int,
121 unsigned int, int, const char *, void *);
122 __private_extern__ void pool_destroy(struct pool *);
123 __private_extern__ void pool_sethiwat(struct pool *, int);
124 __private_extern__ void pool_sethardlimit(struct pool *, int,
125 const char *, int);
126 __private_extern__ void *pool_get(struct pool *, int);
127 __private_extern__ void pool_put(struct pool *, void *);
128 __private_extern__ u_int64_t pf_time_second(void);
129 __private_extern__ u_int64_t pf_calendar_time_second(void);
130 #endif /* KERNEL */
131
132 union sockaddr_union {
133 struct sockaddr sa;
134 struct sockaddr_in sin;
135 struct sockaddr_in6 sin6;
136 };
137
138 #define PF_TCPS_PROXY_SRC ((TCP_NSTATES)+0)
139 #define PF_TCPS_PROXY_DST ((TCP_NSTATES)+1)
140
141 #define PF_MD5_DIGEST_LENGTH 16
142 #ifdef MD5_DIGEST_LENGTH
143 #if PF_MD5_DIGEST_LENGTH != MD5_DIGEST_LENGTH
144 #error
145 #endif /* PF_MD5_DIGEST_LENGTH != MD5_DIGEST_LENGTH */
146 #endif /* MD5_DIGEST_LENGTH */
147
148 #ifdef KERNEL
149 struct ip;
150 struct ip6_hdr;
151 struct tcphdr;
152 struct pf_grev1_hdr;
153 struct pf_esp_hdr;
154 #endif /* KERNEL */
155
156 #define PF_GRE_PPTP_VARIANT 0x01
157
158 enum { PF_INOUT, PF_IN, PF_OUT };
159 enum { PF_PASS, PF_DROP, PF_SCRUB, PF_NOSCRUB, PF_NAT, PF_NONAT,
160 PF_BINAT, PF_NOBINAT, PF_RDR, PF_NORDR, PF_SYNPROXY_DROP,
161 PF_DUMMYNET, PF_NODUMMYNET, PF_NAT64, PF_NONAT64 };
162 enum { PF_RULESET_SCRUB, PF_RULESET_FILTER, PF_RULESET_NAT,
163 PF_RULESET_BINAT, PF_RULESET_RDR, PF_RULESET_DUMMYNET,
164 PF_RULESET_MAX };
165 enum { PF_OP_NONE, PF_OP_IRG, PF_OP_EQ, PF_OP_NE, PF_OP_LT,
166 PF_OP_LE, PF_OP_GT, PF_OP_GE, PF_OP_XRG, PF_OP_RRG };
167 enum { PF_DEBUG_NONE, PF_DEBUG_URGENT, PF_DEBUG_MISC, PF_DEBUG_NOISY };
168 enum { PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
169 PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
170 PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
171 enum { PF_GET_NONE, PF_GET_CLR_CNTR };
172
173 /*
174 * Note about PFTM_*: real indices into pf_rule.timeout[] come before
175 * PFTM_MAX, special cases afterwards. See pf_state_expires().
176 */
177 enum { PFTM_TCP_FIRST_PACKET, PFTM_TCP_OPENING, PFTM_TCP_ESTABLISHED,
178 PFTM_TCP_CLOSING, PFTM_TCP_FIN_WAIT, PFTM_TCP_CLOSED,
179 PFTM_UDP_FIRST_PACKET, PFTM_UDP_SINGLE, PFTM_UDP_MULTIPLE,
180 PFTM_ICMP_FIRST_PACKET, PFTM_ICMP_ERROR_REPLY,
181 PFTM_GREv1_FIRST_PACKET, PFTM_GREv1_INITIATING,
182 PFTM_GREv1_ESTABLISHED, PFTM_ESP_FIRST_PACKET, PFTM_ESP_INITIATING,
183 PFTM_ESP_ESTABLISHED, PFTM_OTHER_FIRST_PACKET, PFTM_OTHER_SINGLE,
184 PFTM_OTHER_MULTIPLE, PFTM_FRAG, PFTM_INTERVAL,
185 PFTM_ADAPTIVE_START, PFTM_ADAPTIVE_END, PFTM_SRC_NODE,
186 PFTM_TS_DIFF, PFTM_MAX, PFTM_PURGE, PFTM_UNLINKED };
187
188 /* PFTM default values */
189 #define PFTM_TCP_FIRST_PACKET_VAL 120 /* First TCP packet */
190 #define PFTM_TCP_OPENING_VAL 30 /* No response yet */
191 #define PFTM_TCP_ESTABLISHED_VAL (24 * 60 * 60) /* Established */
192 #define PFTM_TCP_CLOSING_VAL (15 * 60) /* Half closed */
193 #define PFTM_TCP_FIN_WAIT_VAL 45 /* Got both FINs */
194 #define PFTM_TCP_CLOSED_VAL 90 /* Got a RST */
195 #define PFTM_UDP_FIRST_PACKET_VAL 60 /* First UDP packet */
196 #define PFTM_UDP_SINGLE_VAL 30 /* Unidirectional */
197 #define PFTM_UDP_MULTIPLE_VAL 60 /* Bidirectional */
198 #define PFTM_ICMP_FIRST_PACKET_VAL 20 /* First ICMP packet */
199 #define PFTM_ICMP_ERROR_REPLY_VAL 10 /* Got error response */
200 #define PFTM_GREv1_FIRST_PACKET_VAL 120
201 #define PFTM_GREv1_INITIATING_VAL 30
202 #define PFTM_GREv1_ESTABLISHED_VAL 1800
203 #define PFTM_ESP_FIRST_PACKET_VAL 120
204 #define PFTM_ESP_INITIATING_VAL 30
205 #define PFTM_ESP_ESTABLISHED_VAL 900
206 #define PFTM_OTHER_FIRST_PACKET_VAL 60 /* First packet */
207 #define PFTM_OTHER_SINGLE_VAL 30 /* Unidirectional */
208 #define PFTM_OTHER_MULTIPLE_VAL 60 /* Bidirectional */
209 #define PFTM_FRAG_VAL 30 /* Fragment expire */
210 #define PFTM_INTERVAL_VAL 10 /* Expire interval */
211 #define PFTM_SRC_NODE_VAL 0 /* Source tracking */
212 #define PFTM_TS_DIFF_VAL 30 /* Allowed TS diff */
213
214 enum { PF_NOPFROUTE, PF_FASTROUTE, PF_ROUTETO, PF_DUPTO, PF_REPLYTO };
215 enum { PF_LIMIT_STATES,
216 PF_LIMIT_APP_STATES,
217 PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
218 PF_LIMIT_TABLES, PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_MAX };
219 #define PF_POOL_IDMASK 0x0f
220 enum { PF_POOL_NONE, PF_POOL_BITMASK, PF_POOL_RANDOM,
221 PF_POOL_SRCHASH, PF_POOL_ROUNDROBIN };
222 enum { PF_ADDR_ADDRMASK, PF_ADDR_NOROUTE, PF_ADDR_DYNIFTL,
223 PF_ADDR_TABLE, PF_ADDR_RTLABEL, PF_ADDR_URPFFAILED,
224 PF_ADDR_RANGE };
225 #define PF_POOL_TYPEMASK 0x0f
226 #define PF_POOL_STICKYADDR 0x20
227 #define PF_WSCALE_FLAG 0x80
228 #define PF_WSCALE_MASK 0x0f
229
230 #define PF_LOG 0x01
231 #define PF_LOG_ALL 0x02
232 #define PF_LOG_SOCKET_LOOKUP 0x04
233
234 struct pf_addr {
235 union {
236 struct in_addr _v4addr;
237 struct in6_addr _v6addr;
238 u_int8_t _addr8[16];
239 u_int16_t _addr16[8];
240 u_int32_t _addr32[4];
241 } pfa; /* 128-bit address */
242 #define v4addr pfa._v4addr
243 #define v6addr pfa._v6addr
244 #define addr8 pfa._addr8
245 #define addr16 pfa._addr16
246 #define addr32 pfa._addr32
247 };
248
249 #define PF_TABLE_NAME_SIZE 32
250
251 #define PFI_AFLAG_NETWORK 0x01
252 #define PFI_AFLAG_BROADCAST 0x02
253 #define PFI_AFLAG_PEER 0x04
254 #define PFI_AFLAG_MODEMASK 0x07
255 #define PFI_AFLAG_NOALIAS 0x08
256
257 #ifndef RTLABEL_LEN
258 #define RTLABEL_LEN 32
259 #endif
260
261 struct pf_addr_wrap {
262 union {
263 struct {
264 struct pf_addr addr;
265 struct pf_addr mask;
266 } a;
267 char ifname[IFNAMSIZ];
268 char tblname[PF_TABLE_NAME_SIZE];
269 char rtlabelname[RTLABEL_LEN];
270 u_int32_t rtlabel;
271 } v;
272 union {
273 #ifdef KERNEL
274 struct pfi_dynaddr *dyn __attribute__((aligned(8)));
275 struct pfr_ktable *tbl __attribute__((aligned(8)));
276 #else /* !KERNEL */
277 void *dyn __attribute__((aligned(8)));
278 void *tbl __attribute__((aligned(8)));
279 #endif /* !KERNEL */
280 int dyncnt __attribute__((aligned(8)));
281 int tblcnt __attribute__((aligned(8)));
282 } p __attribute__((aligned(8)));
283 u_int8_t type; /* PF_ADDR_* */
284 u_int8_t iflags; /* PFI_AFLAG_* */
285 };
286
287 struct pf_port_range {
288 u_int16_t port[2];
289 u_int8_t op;
290 };
291
292 union pf_rule_xport {
293 struct pf_port_range range;
294 u_int16_t call_id;
295 u_int32_t spi;
296 };
297
298 #ifdef KERNEL
299 struct pfi_dynaddr {
300 TAILQ_ENTRY(pfi_dynaddr) entry;
301 struct pf_addr pfid_addr4;
302 struct pf_addr pfid_mask4;
303 struct pf_addr pfid_addr6;
304 struct pf_addr pfid_mask6;
305 struct pfr_ktable *pfid_kt;
306 struct pfi_kif *pfid_kif;
307 void *pfid_hook_cookie;
308 int pfid_net; /* mask or 128 */
309 int pfid_acnt4; /* address count IPv4 */
310 int pfid_acnt6; /* address count IPv6 */
311 sa_family_t pfid_af; /* rule af */
312 u_int8_t pfid_iflags; /* PFI_AFLAG_* */
313 };
314
315 /*
316 * Address manipulation macros
317 */
318
319 #if INET
320 #if !INET6
321 #define PF_INET_ONLY
322 #endif /* ! INET6 */
323 #endif /* INET */
324
325 #if INET6
326 #if !INET
327 #define PF_INET6_ONLY
328 #endif /* ! INET */
329 #endif /* INET6 */
330
331 #if INET
332 #if INET6
333 #define PF_INET_INET6
334 #endif /* INET6 */
335 #endif /* INET */
336
337 #else /* !KERNEL */
338
339 #define PF_INET_INET6
340
341 #endif /* !KERNEL */
342
343 /* Both IPv4 and IPv6 */
344 #ifdef PF_INET_INET6
345
346 #define PF_AEQ(a, b, c) \
347 ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
348 ((a)->addr32[3] == (b)->addr32[3] && \
349 (a)->addr32[2] == (b)->addr32[2] && \
350 (a)->addr32[1] == (b)->addr32[1] && \
351 (a)->addr32[0] == (b)->addr32[0])) \
352
353 #define PF_ANEQ(a, b, c) \
354 ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
355 ((a)->addr32[3] != (b)->addr32[3] || \
356 (a)->addr32[2] != (b)->addr32[2] || \
357 (a)->addr32[1] != (b)->addr32[1] || \
358 (a)->addr32[0] != (b)->addr32[0])) \
359
360 #define PF_ALEQ(a, b, c) \
361 ((c == AF_INET && (a)->addr32[0] <= (b)->addr32[0]) || \
362 ((a)->addr32[3] <= (b)->addr32[3] && \
363 (a)->addr32[2] <= (b)->addr32[2] && \
364 (a)->addr32[1] <= (b)->addr32[1] && \
365 (a)->addr32[0] <= (b)->addr32[0])) \
366
367 #define PF_AZERO(a, c) \
368 ((c == AF_INET && !(a)->addr32[0]) || \
369 (!(a)->addr32[0] && !(a)->addr32[1] && \
370 !(a)->addr32[2] && !(a)->addr32[3])) \
371
372 #define PF_MATCHA(n, a, m, b, f) \
373 pf_match_addr(n, a, m, b, f)
374
375 #define PF_ACPY(a, b, f) \
376 pf_addrcpy(a, b, f)
377
378 #define PF_AINC(a, f) \
379 pf_addr_inc(a, f)
380
381 #define PF_POOLMASK(a, b, c, d, f) \
382 pf_poolmask(a, b, c, d, f)
383
384 #else
385
386 /* Just IPv6 */
387
388 #ifdef PF_INET6_ONLY
389
390 #define PF_AEQ(a, b, c) \
391 ((a)->addr32[3] == (b)->addr32[3] && \
392 (a)->addr32[2] == (b)->addr32[2] && \
393 (a)->addr32[1] == (b)->addr32[1] && \
394 (a)->addr32[0] == (b)->addr32[0]) \
395
396 #define PF_ANEQ(a, b, c) \
397 ((a)->addr32[3] != (b)->addr32[3] || \
398 (a)->addr32[2] != (b)->addr32[2] || \
399 (a)->addr32[1] != (b)->addr32[1] || \
400 (a)->addr32[0] != (b)->addr32[0]) \
401
402 #define PF_ALEQ(a, b, c) \
403 ((a)->addr32[3] <= (b)->addr32[3] && \
404 (a)->addr32[2] <= (b)->addr32[2] && \
405 (a)->addr32[1] <= (b)->addr32[1] && \
406 (a)->addr32[0] <= (b)->addr32[0]) \
407
408 #define PF_AZERO(a, c) \
409 (!(a)->addr32[0] && \
410 !(a)->addr32[1] && \
411 !(a)->addr32[2] && \
412 !(a)->addr32[3]) \
413
414 #define PF_MATCHA(n, a, m, b, f) \
415 pf_match_addr(n, a, m, b, f)
416
417 #define PF_ACPY(a, b, f) \
418 pf_addrcpy(a, b, f)
419
420 #define PF_AINC(a, f) \
421 pf_addr_inc(a, f)
422
423 #define PF_POOLMASK(a, b, c, d, f) \
424 pf_poolmask(a, b, c, d, f)
425
426 #else
427
428 /* Just IPv4 */
429 #ifdef PF_INET_ONLY
430
431 #define PF_AEQ(a, b, c) \
432 ((a)->addr32[0] == (b)->addr32[0])
433
434 #define PF_ANEQ(a, b, c) \
435 ((a)->addr32[0] != (b)->addr32[0])
436
437 #define PF_ALEQ(a, b, c) \
438 ((a)->addr32[0] <= (b)->addr32[0])
439
440 #define PF_AZERO(a, c) \
441 (!(a)->addr32[0])
442
443 #define PF_MATCHA(n, a, m, b, f) \
444 pf_match_addr(n, a, m, b, f)
445
446 #define PF_ACPY(a, b, f) \
447 (a)->v4.s_addr = (b)->v4.s_addr
448
449 #define PF_AINC(a, f) \
450 do { \
451 (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
452 } while (0)
453
454 #define PF_POOLMASK(a, b, c, d, f) \
455 do { \
456 (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
457 (((c)->addr32[0] ^ 0xffffffff) & (d)->addr32[0]); \
458 } while (0)
459
460 #endif /* PF_INET_ONLY */
461 #endif /* PF_INET6_ONLY */
462 #endif /* PF_INET_INET6 */
463
464 #ifdef KERNEL
465 #define PF_MISMATCHAW(aw, x, af, neg, ifp) \
466 ( \
467 (((aw)->type == PF_ADDR_NOROUTE && \
468 pf_routable((x), (af), NULL)) || \
469 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \
470 pf_routable((x), (af), (ifp))) || \
471 ((aw)->type == PF_ADDR_RTLABEL && \
472 !pf_rtlabel_match((x), (af), (aw))) || \
473 ((aw)->type == PF_ADDR_TABLE && \
474 !pfr_match_addr((aw)->p.tbl, (x), (af))) || \
475 ((aw)->type == PF_ADDR_DYNIFTL && \
476 !pfi_match_addr((aw)->p.dyn, (x), (af))) || \
477 ((aw)->type == PF_ADDR_RANGE && \
478 !pf_match_addr_range(&(aw)->v.a.addr, \
479 &(aw)->v.a.mask, (x), (af))) || \
480 ((aw)->type == PF_ADDR_ADDRMASK && \
481 !PF_AZERO(&(aw)->v.a.mask, (af)) && \
482 !PF_MATCHA(0, &(aw)->v.a.addr, \
483 &(aw)->v.a.mask, (x), (af))))) != \
484 (neg) \
485 )
486 #endif /* KERNEL */
487
488 struct pf_rule_uid {
489 uid_t uid[2];
490 u_int8_t op;
491 u_int8_t _pad[3];
492 };
493
494 struct pf_rule_gid {
495 uid_t gid[2];
496 u_int8_t op;
497 u_int8_t _pad[3];
498 };
499
500 struct pf_rule_addr {
501 struct pf_addr_wrap addr;
502 union pf_rule_xport xport;
503 u_int8_t neg;
504 };
505
506 struct pf_pooladdr {
507 struct pf_addr_wrap addr;
508 TAILQ_ENTRY(pf_pooladdr) entries;
509 #if !defined(__LP64__)
510 u_int32_t _pad[2];
511 #endif /* !__LP64__ */
512 char ifname[IFNAMSIZ];
513 #ifdef KERNEL
514 struct pfi_kif *kif __attribute__((aligned(8)));
515 #else /* !KERNEL */
516 void *kif __attribute__((aligned(8)));
517 #endif /* !KERNEL */
518 };
519
520 TAILQ_HEAD(pf_palist, pf_pooladdr);
521
522 struct pf_poolhashkey {
523 union {
524 u_int8_t key8[16];
525 u_int16_t key16[8];
526 u_int32_t key32[4];
527 } pfk; /* 128-bit hash key */
528 #define key8 pfk.key8
529 #define key16 pfk.key16
530 #define key32 pfk.key32
531 };
532
533 struct pf_pool {
534 struct pf_palist list;
535 #if !defined(__LP64__)
536 u_int32_t _pad[2];
537 #endif /* !__LP64__ */
538 #ifdef KERNEL
539 struct pf_pooladdr *cur __attribute__((aligned(8)));
540 #else /* !KERNEL */
541 void *cur __attribute__((aligned(8)));
542 #endif /* !KERNEL */
543 struct pf_poolhashkey key __attribute__((aligned(8)));
544 struct pf_addr counter;
545 int tblidx;
546 u_int16_t proxy_port[2];
547 u_int8_t port_op;
548 u_int8_t opts;
549 sa_family_t af;
550 };
551
552
553 /* A packed Operating System description for fingerprinting */
554 typedef u_int32_t pf_osfp_t;
555 #define PF_OSFP_ANY ((pf_osfp_t)0)
556 #define PF_OSFP_UNKNOWN ((pf_osfp_t)-1)
557 #define PF_OSFP_NOMATCH ((pf_osfp_t)-2)
558
559 struct pf_osfp_entry {
560 SLIST_ENTRY(pf_osfp_entry) fp_entry;
561 #if !defined(__LP64__)
562 u_int32_t _pad;
563 #endif /* !__LP64__ */
564 pf_osfp_t fp_os;
565 int fp_enflags;
566 #define PF_OSFP_EXPANDED 0x001 /* expanded entry */
567 #define PF_OSFP_GENERIC 0x002 /* generic signature */
568 #define PF_OSFP_NODETAIL 0x004 /* no p0f details */
569 #define PF_OSFP_LEN 32
570 char fp_class_nm[PF_OSFP_LEN];
571 char fp_version_nm[PF_OSFP_LEN];
572 char fp_subtype_nm[PF_OSFP_LEN];
573 };
574 #define PF_OSFP_ENTRY_EQ(a, b) \
575 ((a)->fp_os == (b)->fp_os && \
576 memcmp((a)->fp_class_nm, (b)->fp_class_nm, PF_OSFP_LEN) == 0 && \
577 memcmp((a)->fp_version_nm, (b)->fp_version_nm, PF_OSFP_LEN) == 0 && \
578 memcmp((a)->fp_subtype_nm, (b)->fp_subtype_nm, PF_OSFP_LEN) == 0)
579
580 /* handle pf_osfp_t packing */
581 #define _FP_RESERVED_BIT 1 /* For the special negative #defines */
582 #define _FP_UNUSED_BITS 1
583 #define _FP_CLASS_BITS 10 /* OS Class (Windows, Linux) */
584 #define _FP_VERSION_BITS 10 /* OS version (95, 98, NT, 2.4.54, 3.2) */
585 #define _FP_SUBTYPE_BITS 10 /* patch level (NT SP4, SP3, ECN patch) */
586 #define PF_OSFP_UNPACK(osfp, class, version, subtype) do { \
587 (class) = ((osfp) >> (_FP_VERSION_BITS+_FP_SUBTYPE_BITS)) & \
588 ((1 << _FP_CLASS_BITS) - 1); \
589 (version) = ((osfp) >> _FP_SUBTYPE_BITS) & \
590 ((1 << _FP_VERSION_BITS) - 1);\
591 (subtype) = (osfp) & ((1 << _FP_SUBTYPE_BITS) - 1); \
592 } while (0)
593 #define PF_OSFP_PACK(osfp, class, version, subtype) do { \
594 (osfp) = ((class) & ((1 << _FP_CLASS_BITS) - 1)) << (_FP_VERSION_BITS \
595 + _FP_SUBTYPE_BITS); \
596 (osfp) |= ((version) & ((1 << _FP_VERSION_BITS) - 1)) << \
597 _FP_SUBTYPE_BITS; \
598 (osfp) |= (subtype) & ((1 << _FP_SUBTYPE_BITS) - 1); \
599 } while (0)
600
601 /* the fingerprint of an OSes TCP SYN packet */
602 typedef u_int64_t pf_tcpopts_t;
603 struct pf_os_fingerprint {
604 SLIST_HEAD(pf_osfp_enlist, pf_osfp_entry) fp_oses; /* list of matches */
605 pf_tcpopts_t fp_tcpopts; /* packed TCP options */
606 u_int16_t fp_wsize; /* TCP window size */
607 u_int16_t fp_psize; /* ip->ip_len */
608 u_int16_t fp_mss; /* TCP MSS */
609 u_int16_t fp_flags;
610 #define PF_OSFP_WSIZE_MOD 0x0001 /* Window modulus */
611 #define PF_OSFP_WSIZE_DC 0x0002 /* Window don't care */
612 #define PF_OSFP_WSIZE_MSS 0x0004 /* Window multiple of MSS */
613 #define PF_OSFP_WSIZE_MTU 0x0008 /* Window multiple of MTU */
614 #define PF_OSFP_PSIZE_MOD 0x0010 /* packet size modulus */
615 #define PF_OSFP_PSIZE_DC 0x0020 /* packet size don't care */
616 #define PF_OSFP_WSCALE 0x0040 /* TCP window scaling */
617 #define PF_OSFP_WSCALE_MOD 0x0080 /* TCP window scale modulus */
618 #define PF_OSFP_WSCALE_DC 0x0100 /* TCP window scale dont-care */
619 #define PF_OSFP_MSS 0x0200 /* TCP MSS */
620 #define PF_OSFP_MSS_MOD 0x0400 /* TCP MSS modulus */
621 #define PF_OSFP_MSS_DC 0x0800 /* TCP MSS dont-care */
622 #define PF_OSFP_DF 0x1000 /* IPv4 don't fragment bit */
623 #define PF_OSFP_TS0 0x2000 /* Zero timestamp */
624 #define PF_OSFP_INET6 0x4000 /* IPv6 */
625 u_int8_t fp_optcnt; /* TCP option count */
626 u_int8_t fp_wscale; /* TCP window scaling */
627 u_int8_t fp_ttl; /* IPv4 TTL */
628 #define PF_OSFP_MAXTTL_OFFSET 40
629 /* TCP options packing */
630 #define PF_OSFP_TCPOPT_NOP 0x0 /* TCP NOP option */
631 #define PF_OSFP_TCPOPT_WSCALE 0x1 /* TCP window scaling option */
632 #define PF_OSFP_TCPOPT_MSS 0x2 /* TCP max segment size opt */
633 #define PF_OSFP_TCPOPT_SACK 0x3 /* TCP SACK OK option */
634 #define PF_OSFP_TCPOPT_TS 0x4 /* TCP timestamp option */
635 #define PF_OSFP_TCPOPT_BITS 3 /* bits used by each option */
636 #define PF_OSFP_MAX_OPTS \
637 (sizeof(((struct pf_os_fingerprint *)0)->fp_tcpopts) * 8) \
638 / PF_OSFP_TCPOPT_BITS
639
640 SLIST_ENTRY(pf_os_fingerprint) fp_next;
641 };
642
643 struct pf_osfp_ioctl {
644 struct pf_osfp_entry fp_os;
645 pf_tcpopts_t fp_tcpopts; /* packed TCP options */
646 u_int16_t fp_wsize; /* TCP window size */
647 u_int16_t fp_psize; /* ip->ip_len */
648 u_int16_t fp_mss; /* TCP MSS */
649 u_int16_t fp_flags;
650 u_int8_t fp_optcnt; /* TCP option count */
651 u_int8_t fp_wscale; /* TCP window scaling */
652 u_int8_t fp_ttl; /* IPv4 TTL */
653
654 int fp_getnum; /* DIOCOSFPGET number */
655 };
656
657
658 union pf_rule_ptr {
659 struct pf_rule *ptr __attribute__((aligned(8)));
660 u_int32_t nr __attribute__((aligned(8)));
661 } __attribute__((aligned(8)));
662
663 #define PF_ANCHOR_NAME_SIZE 64
664
665 struct pf_rule {
666 struct pf_rule_addr src;
667 struct pf_rule_addr dst;
668 #define PF_SKIP_IFP 0
669 #define PF_SKIP_DIR 1
670 #define PF_SKIP_AF 2
671 #define PF_SKIP_PROTO 3
672 #define PF_SKIP_SRC_ADDR 4
673 #define PF_SKIP_SRC_PORT 5
674 #define PF_SKIP_DST_ADDR 6
675 #define PF_SKIP_DST_PORT 7
676 #define PF_SKIP_COUNT 8
677 union pf_rule_ptr skip[PF_SKIP_COUNT];
678 #define PF_RULE_LABEL_SIZE 64
679 char label[PF_RULE_LABEL_SIZE];
680 #define PF_QNAME_SIZE 64
681 char ifname[IFNAMSIZ];
682 char qname[PF_QNAME_SIZE];
683 char pqname[PF_QNAME_SIZE];
684 #define PF_TAG_NAME_SIZE 64
685 char tagname[PF_TAG_NAME_SIZE];
686 char match_tagname[PF_TAG_NAME_SIZE];
687
688 char overload_tblname[PF_TABLE_NAME_SIZE];
689
690 TAILQ_ENTRY(pf_rule) entries;
691 #if !defined(__LP64__)
692 u_int32_t _pad[2];
693 #endif /* !__LP64__ */
694 struct pf_pool rpool;
695
696 u_int64_t evaluations;
697 u_int64_t packets[2];
698 u_int64_t bytes[2];
699
700 u_int64_t ticket;
701 #define PF_OWNER_NAME_SIZE 64
702 char owner[PF_OWNER_NAME_SIZE];
703 u_int32_t priority;
704
705 #ifdef KERNEL
706 struct pfi_kif *kif __attribute__((aligned(8)));
707 #else /* !KERNEL */
708 void *kif __attribute__((aligned(8)));
709 #endif /* !KERNEL */
710 struct pf_anchor *anchor __attribute__((aligned(8)));
711 #ifdef KERNEL
712 struct pfr_ktable *overload_tbl __attribute__((aligned(8)));
713 #else /* !KERNEL */
714 void *overload_tbl __attribute__((aligned(8)));
715 #endif /* !KERNEL */
716
717 pf_osfp_t os_fingerprint __attribute__((aligned(8)));
718
719 unsigned int rtableid;
720 u_int32_t timeout[PFTM_MAX];
721 u_int32_t states;
722 u_int32_t max_states;
723 u_int32_t src_nodes;
724 u_int32_t max_src_nodes;
725 u_int32_t max_src_states;
726 u_int32_t max_src_conn;
727 struct {
728 u_int32_t limit;
729 u_int32_t seconds;
730 } max_src_conn_rate;
731 u_int32_t qid;
732 u_int32_t pqid;
733 u_int32_t rt_listid;
734 u_int32_t nr;
735 u_int32_t prob;
736 uid_t cuid;
737 pid_t cpid;
738
739 u_int16_t return_icmp;
740 u_int16_t return_icmp6;
741 u_int16_t max_mss;
742 u_int16_t tag;
743 u_int16_t match_tag;
744
745 struct pf_rule_uid uid;
746 struct pf_rule_gid gid;
747
748 u_int32_t rule_flag;
749 u_int8_t action;
750 u_int8_t direction;
751 u_int8_t log;
752 u_int8_t logif;
753 u_int8_t quick;
754 u_int8_t ifnot;
755 u_int8_t match_tag_not;
756 u_int8_t natpass;
757
758 #define PF_STATE_NORMAL 0x1
759 #define PF_STATE_MODULATE 0x2
760 #define PF_STATE_SYNPROXY 0x3
761 u_int8_t keep_state;
762 sa_family_t af;
763 u_int8_t proto;
764 u_int8_t type;
765 u_int8_t code;
766 u_int8_t flags;
767 u_int8_t flagset;
768 u_int8_t min_ttl;
769 u_int8_t allow_opts;
770 u_int8_t rt;
771 u_int8_t return_ttl;
772
773 /* service class categories */
774 #define SCIDX_MASK 0x0f
775 #define SC_BE 0x10
776 #define SC_BK_SYS 0x11
777 #define SC_BK 0x12
778 #define SC_RD 0x13
779 #define SC_OAM 0x14
780 #define SC_AV 0x15
781 #define SC_RV 0x16
782 #define SC_VI 0x17
783 #define SC_SIG 0x17
784 #define SC_VO 0x18
785 #define SC_CTL 0x19
786
787 /* diffserve code points */
788 #define DSCP_MASK 0xfc
789 #define DSCP_CUMASK 0x03
790 #define DSCP_EF 0xb8
791 #define DSCP_AF11 0x28
792 #define DSCP_AF12 0x30
793 #define DSCP_AF13 0x38
794 #define DSCP_AF21 0x48
795 #define DSCP_AF22 0x50
796 #define DSCP_AF23 0x58
797 #define DSCP_AF31 0x68
798 #define DSCP_AF32 0x70
799 #define DSCP_AF33 0x78
800 #define DSCP_AF41 0x88
801 #define DSCP_AF42 0x90
802 #define DSCP_AF43 0x98
803 #define AF_CLASSMASK 0xe0
804 #define AF_DROPPRECMASK 0x18
805 u_int8_t tos;
806 u_int8_t anchor_relative;
807 u_int8_t anchor_wildcard;
808
809 #define PF_FLUSH 0x01
810 #define PF_FLUSH_GLOBAL 0x02
811 u_int8_t flush;
812
813 u_int8_t proto_variant;
814 u_int8_t extfilter; /* Filter mode [PF_EXTFILTER_xxx] */
815 u_int8_t extmap; /* Mapping mode [PF_EXTMAP_xxx] */
816 u_int32_t dnpipe;
817 u_int32_t dntype;
818 };
819
820 /* pf device identifiers */
821 #define PFDEV_PF 0
822 #define PFDEV_PFM 1
823 #define PFDEV_MAX 2
824
825 /* rule flags */
826 #define PFRULE_DROP 0x0000
827 #define PFRULE_RETURNRST 0x0001
828 #define PFRULE_FRAGMENT 0x0002
829 #define PFRULE_RETURNICMP 0x0004
830 #define PFRULE_RETURN 0x0008
831 #define PFRULE_NOSYNC 0x0010
832 #define PFRULE_SRCTRACK 0x0020 /* track source states */
833 #define PFRULE_RULESRCTRACK 0x0040 /* per rule */
834
835 /* scrub flags */
836 #define PFRULE_NODF 0x0100
837 #define PFRULE_FRAGCROP 0x0200 /* non-buffering frag cache */
838 #define PFRULE_FRAGDROP 0x0400 /* drop funny fragments */
839 #define PFRULE_RANDOMID 0x0800
840 #define PFRULE_REASSEMBLE_TCP 0x1000
841
842 /* rule flags for TOS/DSCP/service class differentiation */
843 #define PFRULE_TOS 0x2000
844 #define PFRULE_DSCP 0x4000
845 #define PFRULE_SC 0x8000
846
847 /* rule flags again */
848 #define PFRULE_IFBOUND 0x00010000 /* if-bound */
849 #define PFRULE_PFM 0x00020000 /* created by pfm device */
850
851 #define PFSTATE_HIWAT 10000 /* default state table size */
852 #define PFSTATE_ADAPT_START 6000 /* default adaptive timeout start */
853 #define PFSTATE_ADAPT_END 12000 /* default adaptive timeout end */
854
855 #define PFAPPSTATE_HIWAT 10000 /* default same as state table */
856
857 enum pf_extmap {
858 PF_EXTMAP_APD = 1, /* Address-port-dependent mapping */
859 PF_EXTMAP_AD, /* Address-dependent mapping */
860 PF_EXTMAP_EI /* Endpoint-independent mapping */
861 };
862
863 enum pf_extfilter {
864 PF_EXTFILTER_APD = 1, /* Address-port-dependent filtering */
865 PF_EXTFILTER_AD, /* Address-dependent filtering */
866 PF_EXTFILTER_EI /* Endpoint-independent filtering */
867 };
868
869 struct pf_threshold {
870 u_int32_t limit;
871 #define PF_THRESHOLD_MULT 1000
872 #define PF_THRESHOLD_MAX 0xffffffff / PF_THRESHOLD_MULT
873 u_int32_t seconds;
874 u_int32_t count;
875 u_int32_t last;
876 };
877
878 struct pf_src_node {
879 RB_ENTRY(pf_src_node) entry;
880 struct pf_addr addr;
881 struct pf_addr raddr;
882 union pf_rule_ptr rule;
883 #ifdef KERNEL
884 struct pfi_kif *kif;
885 #else /* !KERNEL */
886 void *kif;
887 #endif /* !KERNEL */
888 u_int64_t bytes[2];
889 u_int64_t packets[2];
890 u_int32_t states;
891 u_int32_t conn;
892 struct pf_threshold conn_rate;
893 u_int64_t creation;
894 u_int64_t expire;
895 sa_family_t af;
896 u_int8_t ruletype;
897 };
898
899 #define PFSNODE_HIWAT 10000 /* default source node table size */
900
901 #ifdef KERNEL
902 struct pf_state_scrub {
903 struct timeval pfss_last; /* time received last packet */
904 u_int32_t pfss_tsecr; /* last echoed timestamp */
905 u_int32_t pfss_tsval; /* largest timestamp */
906 u_int32_t pfss_tsval0; /* original timestamp */
907 u_int16_t pfss_flags;
908 #define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */
909 #define PFSS_PAWS 0x0010 /* stricter PAWS checks */
910 #define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */
911 #define PFSS_DATA_TS 0x0040 /* timestamp on data packets */
912 #define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */
913 u_int8_t pfss_ttl; /* stashed TTL */
914 u_int8_t pad;
915 u_int32_t pfss_ts_mod; /* timestamp modulation */
916 };
917 #endif /* KERNEL */
918
919 union pf_state_xport {
920 u_int16_t port;
921 u_int16_t call_id;
922 u_int32_t spi;
923 };
924
925 struct pf_state_host {
926 struct pf_addr addr;
927 union pf_state_xport xport;
928 };
929
930 #ifdef KERNEL
931 struct pf_state_peer {
932 u_int32_t seqlo; /* Max sequence number sent */
933 u_int32_t seqhi; /* Max the other end ACKd + win */
934 u_int32_t seqdiff; /* Sequence number modulator */
935 u_int16_t max_win; /* largest window (pre scaling) */
936 u_int8_t state; /* active state level */
937 u_int8_t wscale; /* window scaling factor */
938 u_int16_t mss; /* Maximum segment size option */
939 u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */
940 struct pf_state_scrub *scrub; /* state is scrubbed */
941 u_int8_t pad[3];
942 };
943
944 TAILQ_HEAD(pf_state_queue, pf_state);
945
946 struct pf_state;
947 struct pf_pdesc;
948 struct pf_app_state;
949
950 typedef void (*pf_app_handler)(struct pf_state *, int, int, struct pf_pdesc *,
951 struct pfi_kif *);
952
953 typedef int (*pf_app_compare)(struct pf_app_state *, struct pf_app_state *);
954
955 struct pf_pptp_state {
956 struct pf_state *grev1_state;
957 };
958
959 struct pf_grev1_state {
960 struct pf_state *pptp_state;
961 };
962
963 struct pf_ike_state {
964 u_int64_t cookie;
965 };
966
967 struct pf_app_state {
968 pf_app_handler handler;
969 pf_app_compare compare_lan_ext;
970 pf_app_compare compare_ext_gwy;
971 union {
972 struct pf_pptp_state pptp;
973 struct pf_grev1_state grev1;
974 struct pf_ike_state ike;
975 } u;
976 };
977
978 /* keep synced with struct pf_state, used in RB_FIND */
979 struct pf_state_key_cmp {
980 struct pf_state_host lan;
981 struct pf_state_host gwy;
982 struct pf_state_host ext_lan;
983 struct pf_state_host ext_gwy;
984 sa_family_t af_lan;
985 sa_family_t af_gwy;
986 u_int8_t proto;
987 u_int8_t direction;
988 u_int8_t proto_variant;
989 struct pf_app_state *app_state;
990 };
991
992 TAILQ_HEAD(pf_statelist, pf_state);
993
994 struct pf_state_key {
995 struct pf_state_host lan;
996 struct pf_state_host gwy;
997 struct pf_state_host ext_lan;
998 struct pf_state_host ext_gwy;
999 sa_family_t af_lan;
1000 sa_family_t af_gwy;
1001 u_int8_t proto;
1002 u_int8_t direction;
1003 u_int8_t proto_variant;
1004 struct pf_app_state *app_state;
1005 u_int32_t flowsrc;
1006 u_int32_t flowhash;
1007
1008 RB_ENTRY(pf_state_key) entry_lan_ext;
1009 RB_ENTRY(pf_state_key) entry_ext_gwy;
1010 struct pf_statelist states;
1011 u_int32_t refcnt;
1012 };
1013
1014
1015 /* keep synced with struct pf_state, used in RB_FIND */
1016 struct pf_state_cmp {
1017 u_int64_t id;
1018 u_int32_t creatorid;
1019 u_int32_t pad;
1020 };
1021
1022 /* flowhash key (12-bytes multiple for performance) */
1023 struct pf_flowhash_key {
1024 struct pf_state_host ap1; /* address+port blob 1 */
1025 struct pf_state_host ap2; /* address+port blob 2 */
1026 u_int32_t af;
1027 u_int32_t proto;
1028 };
1029 #endif /* KERNEL */
1030
1031 struct hook_desc;
1032 TAILQ_HEAD(hook_desc_head, hook_desc);
1033
1034 #ifdef KERNEL
1035 struct pf_state {
1036 u_int64_t id;
1037 u_int32_t creatorid;
1038 u_int32_t pad;
1039
1040 TAILQ_ENTRY(pf_state) entry_list;
1041 TAILQ_ENTRY(pf_state) next;
1042 RB_ENTRY(pf_state) entry_id;
1043 struct pf_state_peer src;
1044 struct pf_state_peer dst;
1045 union pf_rule_ptr rule;
1046 union pf_rule_ptr anchor;
1047 union pf_rule_ptr nat_rule;
1048 struct pf_addr rt_addr;
1049 struct hook_desc_head unlink_hooks;
1050 struct pf_state_key *state_key;
1051 struct pfi_kif *kif;
1052 struct pfi_kif *rt_kif;
1053 struct pf_src_node *src_node;
1054 struct pf_src_node *nat_src_node;
1055 u_int64_t packets[2];
1056 u_int64_t bytes[2];
1057 u_int64_t creation;
1058 u_int64_t expire;
1059 u_int64_t pfsync_time;
1060 u_int16_t tag;
1061 u_int8_t log;
1062 u_int8_t allow_opts;
1063 u_int8_t timeout;
1064 u_int8_t sync_flags;
1065 };
1066 #endif /* KERNEL */
1067
1068 #define PFSTATE_NOSYNC 0x01
1069 #define PFSTATE_FROMSYNC 0x02
1070 #define PFSTATE_STALE 0x04
1071
1072 #define __packed __attribute__((__packed__))
1073
1074 /*
1075 * Unified state structures for pulling states out of the kernel
1076 * used by pfsync(4) and the pf(4) ioctl.
1077 */
1078 struct pfsync_state_scrub {
1079 u_int16_t pfss_flags;
1080 u_int8_t pfss_ttl; /* stashed TTL */
1081 #define PFSYNC_SCRUB_FLAG_VALID 0x01
1082 u_int8_t scrub_flag;
1083 u_int32_t pfss_ts_mod; /* timestamp modulation */
1084 } __packed;
1085
1086 struct pfsync_state_host {
1087 struct pf_addr addr;
1088 union pf_state_xport xport;
1089 u_int16_t pad[2];
1090 } __packed;
1091
1092 struct pfsync_state_peer {
1093 struct pfsync_state_scrub scrub; /* state is scrubbed */
1094 u_int32_t seqlo; /* Max sequence number sent */
1095 u_int32_t seqhi; /* Max the other end ACKd + win */
1096 u_int32_t seqdiff; /* Sequence number modulator */
1097 u_int16_t max_win; /* largest window (pre scaling) */
1098 u_int16_t mss; /* Maximum segment size option */
1099 u_int8_t state; /* active state level */
1100 u_int8_t wscale; /* window scaling factor */
1101 u_int8_t pad[6];
1102 } __packed;
1103
1104 struct pfsync_state {
1105 u_int32_t id[2];
1106 char ifname[IFNAMSIZ];
1107 struct pfsync_state_host lan;
1108 struct pfsync_state_host gwy;
1109 struct pfsync_state_host ext_lan;
1110 struct pfsync_state_host ext_gwy;
1111 struct pfsync_state_peer src;
1112 struct pfsync_state_peer dst;
1113 struct pf_addr rt_addr;
1114 struct hook_desc_head unlink_hooks;
1115 #if !defined(__LP64__)
1116 u_int32_t _pad[2];
1117 #endif /* !__LP64__ */
1118 u_int32_t rule;
1119 u_int32_t anchor;
1120 u_int32_t nat_rule;
1121 u_int64_t creation;
1122 u_int64_t expire;
1123 u_int32_t packets[2][2];
1124 u_int32_t bytes[2][2];
1125 u_int32_t creatorid;
1126 u_int16_t tag;
1127 sa_family_t af_lan;
1128 sa_family_t af_gwy;
1129 u_int8_t proto;
1130 u_int8_t direction;
1131 u_int8_t log;
1132 u_int8_t allow_opts;
1133 u_int8_t timeout;
1134 u_int8_t sync_flags;
1135 u_int8_t updates;
1136 u_int8_t proto_variant;
1137 u_int8_t __pad;
1138 u_int32_t flowhash;
1139 } __packed;
1140
1141 #define PFSYNC_FLAG_COMPRESS 0x01
1142 #define PFSYNC_FLAG_STALE 0x02
1143 #define PFSYNC_FLAG_SRCNODE 0x04
1144 #define PFSYNC_FLAG_NATSRCNODE 0x08
1145
1146 #ifdef KERNEL
1147 /* for copies to/from userland via pf_ioctl() */
1148 #define pf_state_peer_to_pfsync(s, d) do { \
1149 (d)->seqlo = (s)->seqlo; \
1150 (d)->seqhi = (s)->seqhi; \
1151 (d)->seqdiff = (s)->seqdiff; \
1152 (d)->max_win = (s)->max_win; \
1153 (d)->mss = (s)->mss; \
1154 (d)->state = (s)->state; \
1155 (d)->wscale = (s)->wscale; \
1156 if ((s)->scrub) { \
1157 (d)->scrub.pfss_flags = \
1158 (s)->scrub->pfss_flags & PFSS_TIMESTAMP; \
1159 (d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl; \
1160 (d)->scrub.pfss_ts_mod = (s)->scrub->pfss_ts_mod; \
1161 (d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID; \
1162 } \
1163 } while (0)
1164
1165 #define pf_state_peer_from_pfsync(s, d) do { \
1166 (d)->seqlo = (s)->seqlo; \
1167 (d)->seqhi = (s)->seqhi; \
1168 (d)->seqdiff = (s)->seqdiff; \
1169 (d)->max_win = (s)->max_win; \
1170 (d)->mss = ntohs((s)->mss); \
1171 (d)->state = (s)->state; \
1172 (d)->wscale = (s)->wscale; \
1173 if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && \
1174 (d)->scrub != NULL) { \
1175 (d)->scrub->pfss_flags = \
1176 ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP; \
1177 (d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl; \
1178 (d)->scrub->pfss_ts_mod = (s)->scrub.pfss_ts_mod; \
1179 } \
1180 } while (0)
1181 #endif /* KERNEL */
1182
1183 #define pf_state_counter_to_pfsync(s, d) do { \
1184 d[0] = (s>>32)&0xffffffff; \
1185 d[1] = s&0xffffffff; \
1186 } while (0)
1187
1188 #define pf_state_counter_from_pfsync(s) \
1189 (((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1]))
1190
1191
1192
1193 TAILQ_HEAD(pf_rulequeue, pf_rule);
1194
1195 struct pf_anchor;
1196
1197 struct pf_ruleset {
1198 struct {
1199 struct pf_rulequeue queues[2];
1200 struct {
1201 struct pf_rulequeue *ptr;
1202 struct pf_rule **ptr_array;
1203 u_int32_t rcount;
1204 u_int32_t ticket;
1205 int open;
1206 } active, inactive;
1207 } rules[PF_RULESET_MAX];
1208 struct pf_anchor *anchor;
1209 u_int32_t tticket;
1210 int tables;
1211 int topen;
1212 };
1213
1214 RB_HEAD(pf_anchor_global, pf_anchor);
1215 RB_HEAD(pf_anchor_node, pf_anchor);
1216 struct pf_anchor {
1217 RB_ENTRY(pf_anchor) entry_global;
1218 RB_ENTRY(pf_anchor) entry_node;
1219 struct pf_anchor *parent;
1220 struct pf_anchor_node children;
1221 char name[PF_ANCHOR_NAME_SIZE];
1222 char path[MAXPATHLEN];
1223 struct pf_ruleset ruleset;
1224 int refcnt; /* anchor rules */
1225 int match;
1226 char owner[PF_OWNER_NAME_SIZE];
1227 };
1228 #ifdef KERNEL
1229 RB_PROTOTYPE_SC(__private_extern__, pf_anchor_global, pf_anchor, entry_global,
1230 pf_anchor_compare);
1231 RB_PROTOTYPE_SC(__private_extern__, pf_anchor_node, pf_anchor, entry_node,
1232 pf_anchor_compare);
1233 #else /* !KERNEL */
1234 RB_PROTOTYPE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
1235 RB_PROTOTYPE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
1236 #endif /* !KERNEL */
1237
1238 #define PF_RESERVED_ANCHOR "_pf"
1239
1240 #define PFR_TFLAG_PERSIST 0x00000001
1241 #define PFR_TFLAG_CONST 0x00000002
1242 #define PFR_TFLAG_ACTIVE 0x00000004
1243 #define PFR_TFLAG_INACTIVE 0x00000008
1244 #define PFR_TFLAG_REFERENCED 0x00000010
1245 #define PFR_TFLAG_REFDANCHOR 0x00000020
1246 #define PFR_TFLAG_USRMASK 0x00000003
1247 #define PFR_TFLAG_SETMASK 0x0000003C
1248 #define PFR_TFLAG_ALLMASK 0x0000003F
1249
1250 struct pfr_table {
1251 char pfrt_anchor[MAXPATHLEN];
1252 char pfrt_name[PF_TABLE_NAME_SIZE];
1253 u_int32_t pfrt_flags;
1254 u_int8_t pfrt_fback;
1255 };
1256
1257 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
1258 PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
1259 PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_MAX };
1260
1261 struct pfr_addr {
1262 union {
1263 struct in_addr _pfra_ip4addr;
1264 struct in6_addr _pfra_ip6addr;
1265 } pfra_u;
1266 u_int8_t pfra_af;
1267 u_int8_t pfra_net;
1268 u_int8_t pfra_not;
1269 u_int8_t pfra_fback;
1270 };
1271 #define pfra_ip4addr pfra_u._pfra_ip4addr
1272 #define pfra_ip6addr pfra_u._pfra_ip6addr
1273
1274 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
1275 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
1276 #define PFR_OP_XPASS PFR_OP_ADDR_MAX
1277
1278 struct pfr_astats {
1279 struct pfr_addr pfras_a;
1280 #if !defined(__LP64__)
1281 u_int32_t _pad;
1282 #endif /* !__LP64__ */
1283 u_int64_t pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1284 u_int64_t pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1285 u_int64_t pfras_tzero;
1286 };
1287
1288 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
1289
1290 struct pfr_tstats {
1291 struct pfr_table pfrts_t;
1292 u_int64_t pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1293 u_int64_t pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1294 u_int64_t pfrts_match;
1295 u_int64_t pfrts_nomatch;
1296 u_int64_t pfrts_tzero;
1297 int pfrts_cnt;
1298 int pfrts_refcnt[PFR_REFCNT_MAX];
1299 #if !defined(__LP64__)
1300 u_int32_t _pad;
1301 #endif /* !__LP64__ */
1302 };
1303 #define pfrts_name pfrts_t.pfrt_name
1304 #define pfrts_flags pfrts_t.pfrt_flags
1305
1306 #ifdef KERNEL
1307 SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
1308 struct pfr_kentry {
1309 struct radix_node pfrke_node[2];
1310 union sockaddr_union pfrke_sa;
1311 u_int64_t pfrke_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1312 u_int64_t pfrke_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
1313 SLIST_ENTRY(pfr_kentry) pfrke_workq;
1314 u_int64_t pfrke_tzero;
1315 u_int8_t pfrke_af;
1316 u_int8_t pfrke_net;
1317 u_int8_t pfrke_not;
1318 u_int8_t pfrke_mark;
1319 u_int8_t pfrke_intrpool;
1320 };
1321
1322 SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
1323 RB_HEAD(pfr_ktablehead, pfr_ktable);
1324 struct pfr_ktable {
1325 struct pfr_tstats pfrkt_ts;
1326 RB_ENTRY(pfr_ktable) pfrkt_tree;
1327 SLIST_ENTRY(pfr_ktable) pfrkt_workq;
1328 struct radix_node_head *pfrkt_ip4;
1329 struct radix_node_head *pfrkt_ip6;
1330 struct pfr_ktable *pfrkt_shadow;
1331 struct pfr_ktable *pfrkt_root;
1332 struct pf_ruleset *pfrkt_rs;
1333 u_int64_t pfrkt_larg;
1334 u_int32_t pfrkt_nflags;
1335 };
1336 #define pfrkt_t pfrkt_ts.pfrts_t
1337 #define pfrkt_name pfrkt_t.pfrt_name
1338 #define pfrkt_anchor pfrkt_t.pfrt_anchor
1339 #define pfrkt_ruleset pfrkt_t.pfrt_ruleset
1340 #define pfrkt_flags pfrkt_t.pfrt_flags
1341 #define pfrkt_cnt pfrkt_ts.pfrts_cnt
1342 #define pfrkt_refcnt pfrkt_ts.pfrts_refcnt
1343 #define pfrkt_packets pfrkt_ts.pfrts_packets
1344 #define pfrkt_bytes pfrkt_ts.pfrts_bytes
1345 #define pfrkt_match pfrkt_ts.pfrts_match
1346 #define pfrkt_nomatch pfrkt_ts.pfrts_nomatch
1347 #define pfrkt_tzero pfrkt_ts.pfrts_tzero
1348
1349 RB_HEAD(pf_state_tree_lan_ext, pf_state_key);
1350 RB_PROTOTYPE_SC(__private_extern__, pf_state_tree_lan_ext, pf_state_key,
1351 entry_lan_ext, pf_state_compare_lan_ext);
1352
1353 RB_HEAD(pf_state_tree_ext_gwy, pf_state_key);
1354 RB_PROTOTYPE_SC(__private_extern__, pf_state_tree_ext_gwy, pf_state_key,
1355 entry_ext_gwy, pf_state_compare_ext_gwy);
1356
1357 RB_HEAD(pfi_ifhead, pfi_kif);
1358
1359 /* state tables */
1360 extern struct pf_state_tree_lan_ext pf_statetbl_lan_ext;
1361 extern struct pf_state_tree_ext_gwy pf_statetbl_ext_gwy;
1362
1363 /* keep synced with pfi_kif, used in RB_FIND */
1364 struct pfi_kif_cmp {
1365 char pfik_name[IFNAMSIZ];
1366 };
1367
1368 struct pfi_kif {
1369 char pfik_name[IFNAMSIZ];
1370 RB_ENTRY(pfi_kif) pfik_tree;
1371 u_int64_t pfik_packets[2][2][2];
1372 u_int64_t pfik_bytes[2][2][2];
1373 u_int64_t pfik_tzero;
1374 int pfik_flags;
1375 void *pfik_ah_cookie;
1376 struct ifnet *pfik_ifp;
1377 int pfik_states;
1378 int pfik_rules;
1379 TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs;
1380 };
1381
1382 enum pfi_kif_refs {
1383 PFI_KIF_REF_NONE,
1384 PFI_KIF_REF_STATE,
1385 PFI_KIF_REF_RULE
1386 };
1387
1388 struct pfi_uif {
1389 #else /* !KERNEL */
1390 struct pfi_kif {
1391 #endif /* !KERNEL */
1392 char pfik_name[IFNAMSIZ];
1393 u_int64_t pfik_packets[2][2][2];
1394 u_int64_t pfik_bytes[2][2][2];
1395 u_int64_t pfik_tzero;
1396 int pfik_flags;
1397 int pfik_states;
1398 int pfik_rules;
1399 #if !defined(__LP64__)
1400 u_int32_t _pad;
1401 #endif /* !__LP64__ */
1402 };
1403
1404 #define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */
1405
1406 #ifdef KERNEL
1407 struct pf_pdesc {
1408 struct {
1409 int done;
1410 uid_t uid;
1411 gid_t gid;
1412 pid_t pid;
1413 } lookup;
1414 u_int64_t tot_len; /* Make Mickey money */
1415 union {
1416 struct tcphdr *tcp;
1417 struct udphdr *udp;
1418 struct icmp *icmp;
1419 #if INET6
1420 struct icmp6_hdr *icmp6;
1421 #endif /* INET6 */
1422 struct pf_grev1_hdr *grev1;
1423 struct pf_esp_hdr *esp;
1424 void *any;
1425 } hdr;
1426
1427 /* XXX TODO: Change baddr and naddr to *saddr */
1428 struct pf_addr baddr; /* src address before translation */
1429 struct pf_addr bdaddr; /* dst address before translation */
1430 struct pf_addr naddr; /* src address after translation */
1431 struct pf_addr ndaddr; /* dst address after translation */
1432 struct pf_rule *nat_rule; /* nat/rdr rule applied to packet */
1433 struct pf_addr *src;
1434 struct pf_addr *dst;
1435 struct ether_header
1436 *eh;
1437 pbuf_t *mp;
1438 int lmw; /* lazy writable offset */
1439 struct pf_mtag *pf_mtag;
1440 u_int16_t *ip_sum;
1441 u_int32_t off; /* protocol header offset */
1442 u_int32_t hdrlen; /* protocol header length */
1443 u_int32_t p_len; /* total length of payload */
1444 u_int16_t flags; /* Let SCRUB trigger behavior in */
1445 /* state code. Easier than tags */
1446 #define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */
1447 #define PFDESC_IP_REAS 0x0002 /* IP frags would've been reassembled */
1448 #define PFDESC_IP_FRAG 0x0004 /* This is a fragment */
1449 sa_family_t af;
1450 sa_family_t naf; /* address family after translation */
1451 u_int8_t proto;
1452 u_int8_t tos;
1453 u_int8_t ttl;
1454 u_int8_t proto_variant;
1455 mbuf_svc_class_t sc; /* mbuf service class (MBUF_SVC) */
1456 u_int32_t pktflags; /* mbuf packet flags (PKTF) */
1457 u_int32_t flowsrc; /* flow source (FLOWSRC) */
1458 u_int32_t flowhash; /* flow hash to identify the sender */
1459 };
1460 #endif /* KERNEL */
1461
1462 /* flags for RDR options */
1463 #define PF_DPORT_RANGE 0x01 /* Dest port uses range */
1464 #define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */
1465
1466 /* Reasons code for passing/dropping a packet */
1467 #define PFRES_MATCH 0 /* Explicit match of a rule */
1468 #define PFRES_BADOFF 1 /* Bad offset for pull_hdr */
1469 #define PFRES_FRAG 2 /* Dropping following fragment */
1470 #define PFRES_SHORT 3 /* Dropping short packet */
1471 #define PFRES_NORM 4 /* Dropping by normalizer */
1472 #define PFRES_MEMORY 5 /* Dropped due to lacking mem */
1473 #define PFRES_TS 6 /* Bad TCP Timestamp (RFC1323) */
1474 #define PFRES_CONGEST 7 /* Congestion (of ipintrq) */
1475 #define PFRES_IPOPTIONS 8 /* IP option */
1476 #define PFRES_PROTCKSUM 9 /* Protocol checksum invalid */
1477 #define PFRES_BADSTATE 10 /* State mismatch */
1478 #define PFRES_STATEINS 11 /* State insertion failure */
1479 #define PFRES_MAXSTATES 12 /* State limit */
1480 #define PFRES_SRCLIMIT 13 /* Source node/conn limit */
1481 #define PFRES_SYNPROXY 14 /* SYN proxy */
1482 #define PFRES_DUMMYNET 15 /* Dummynet */
1483 #define PFRES_MAX 16 /* total+1 */
1484
1485 #define PFRES_NAMES { \
1486 "match", \
1487 "bad-offset", \
1488 "fragment", \
1489 "short", \
1490 "normalize", \
1491 "memory", \
1492 "bad-timestamp", \
1493 "congestion", \
1494 "ip-option", \
1495 "proto-cksum", \
1496 "state-mismatch", \
1497 "state-insert", \
1498 "state-limit", \
1499 "src-limit", \
1500 "synproxy", \
1501 "dummynet", \
1502 NULL \
1503 }
1504
1505 /* Counters for other things we want to keep track of */
1506 #define LCNT_STATES 0 /* states */
1507 #define LCNT_SRCSTATES 1 /* max-src-states */
1508 #define LCNT_SRCNODES 2 /* max-src-nodes */
1509 #define LCNT_SRCCONN 3 /* max-src-conn */
1510 #define LCNT_SRCCONNRATE 4 /* max-src-conn-rate */
1511 #define LCNT_OVERLOAD_TABLE 5 /* entry added to overload table */
1512 #define LCNT_OVERLOAD_FLUSH 6 /* state entries flushed */
1513 #define LCNT_MAX 7 /* total+1 */
1514
1515 #define LCNT_NAMES { \
1516 "max states per rule", \
1517 "max-src-states", \
1518 "max-src-nodes", \
1519 "max-src-conn", \
1520 "max-src-conn-rate", \
1521 "overload table insertion", \
1522 "overload flush states", \
1523 NULL \
1524 }
1525
1526 /* UDP state enumeration */
1527 #define PFUDPS_NO_TRAFFIC 0
1528 #define PFUDPS_SINGLE 1
1529 #define PFUDPS_MULTIPLE 2
1530
1531 #define PFUDPS_NSTATES 3 /* number of state levels */
1532
1533 #define PFUDPS_NAMES { \
1534 "NO_TRAFFIC", \
1535 "SINGLE", \
1536 "MULTIPLE", \
1537 NULL \
1538 }
1539
1540 /* GREv1 protocol state enumeration */
1541 #define PFGRE1S_NO_TRAFFIC 0
1542 #define PFGRE1S_INITIATING 1
1543 #define PFGRE1S_ESTABLISHED 2
1544
1545 #define PFGRE1S_NSTATES 3 /* number of state levels */
1546
1547 #define PFGRE1S_NAMES { \
1548 "NO_TRAFFIC", \
1549 "INITIATING", \
1550 "ESTABLISHED", \
1551 NULL \
1552 }
1553
1554 #define PFESPS_NO_TRAFFIC 0
1555 #define PFESPS_INITIATING 1
1556 #define PFESPS_ESTABLISHED 2
1557
1558 #define PFESPS_NSTATES 3 /* number of state levels */
1559
1560 #define PFESPS_NAMES { "NO_TRAFFIC", "INITIATING", "ESTABLISHED", NULL }
1561
1562 /* Other protocol state enumeration */
1563 #define PFOTHERS_NO_TRAFFIC 0
1564 #define PFOTHERS_SINGLE 1
1565 #define PFOTHERS_MULTIPLE 2
1566
1567 #define PFOTHERS_NSTATES 3 /* number of state levels */
1568
1569 #define PFOTHERS_NAMES { \
1570 "NO_TRAFFIC", \
1571 "SINGLE", \
1572 "MULTIPLE", \
1573 NULL \
1574 }
1575
1576 #define FCNT_STATE_SEARCH 0
1577 #define FCNT_STATE_INSERT 1
1578 #define FCNT_STATE_REMOVALS 2
1579 #define FCNT_MAX 3
1580
1581 #define SCNT_SRC_NODE_SEARCH 0
1582 #define SCNT_SRC_NODE_INSERT 1
1583 #define SCNT_SRC_NODE_REMOVALS 2
1584 #define SCNT_MAX 3
1585
1586 #ifdef KERNEL
1587 #define ACTION_SET(a, x) \
1588 do { \
1589 if ((a) != NULL) \
1590 *(a) = (x); \
1591 } while (0)
1592
1593 #define REASON_SET(a, x) \
1594 do { \
1595 if ((a) != NULL) \
1596 *(a) = (x); \
1597 if (x < PFRES_MAX) \
1598 pf_status.counters[x]++; \
1599 } while (0)
1600 #endif /* KERNEL */
1601
1602 struct pf_status {
1603 u_int64_t counters[PFRES_MAX];
1604 u_int64_t lcounters[LCNT_MAX]; /* limit counters */
1605 u_int64_t fcounters[FCNT_MAX];
1606 u_int64_t scounters[SCNT_MAX];
1607 u_int64_t pcounters[2][2][3];
1608 u_int64_t bcounters[2][2];
1609 u_int64_t stateid;
1610 u_int32_t running;
1611 u_int32_t states;
1612 u_int32_t src_nodes;
1613 u_int64_t since __attribute__((aligned(8)));
1614 u_int32_t debug;
1615 u_int32_t hostid;
1616 char ifname[IFNAMSIZ];
1617 u_int8_t pf_chksum[PF_MD5_DIGEST_LENGTH];
1618 };
1619
1620 struct cbq_opts {
1621 u_int32_t minburst;
1622 u_int32_t maxburst;
1623 u_int32_t pktsize;
1624 u_int32_t maxpktsize;
1625 u_int32_t ns_per_byte;
1626 u_int32_t maxidle;
1627 int32_t minidle;
1628 u_int32_t offtime;
1629 u_int32_t flags;
1630 };
1631
1632 struct priq_opts {
1633 u_int32_t flags;
1634 };
1635
1636 struct qfq_opts {
1637 u_int32_t flags;
1638 u_int32_t lmax;
1639 };
1640
1641 struct hfsc_opts {
1642 /* real-time service curve */
1643 u_int64_t rtsc_m1; /* slope of the 1st segment in bps */
1644 u_int64_t rtsc_d; /* the x-projection of m1 in msec */
1645 u_int64_t rtsc_m2; /* slope of the 2nd segment in bps */
1646 u_int32_t rtsc_fl; /* service curve flags */
1647 #if !defined(__LP64__)
1648 u_int32_t _pad;
1649 #endif /* !__LP64__ */
1650 /* link-sharing service curve */
1651 u_int64_t lssc_m1;
1652 u_int64_t lssc_d;
1653 u_int64_t lssc_m2;
1654 u_int32_t lssc_fl;
1655 #if !defined(__LP64__)
1656 u_int32_t __pad;
1657 #endif /* !__LP64__ */
1658 /* upper-limit service curve */
1659 u_int64_t ulsc_m1;
1660 u_int64_t ulsc_d;
1661 u_int64_t ulsc_m2;
1662 u_int32_t ulsc_fl;
1663 u_int32_t flags; /* scheduler flags */
1664 };
1665
1666 struct fairq_opts {
1667 u_int32_t nbuckets; /* hash buckets */
1668 u_int32_t flags;
1669 u_int64_t hogs_m1; /* hog detection bandwidth */
1670
1671 /* link-sharing service curve */
1672 u_int64_t lssc_m1;
1673 u_int64_t lssc_d;
1674 u_int64_t lssc_m2;
1675 };
1676
1677 /* bandwidth types */
1678 #define PF_ALTQ_BW_ABSOLUTE 1 /* bw in absolute value (bps) */
1679 #define PF_ALTQ_BW_PERCENT 2 /* bandwidth in percentage */
1680
1681 /* ALTQ rule flags */
1682 #define PF_ALTQF_TBR 0x1 /* enable Token Bucket Regulator */
1683
1684 /* queue rule flags */
1685 #define PF_ALTQ_QRF_WEIGHT 0x1 /* weight instead of priority */
1686
1687 struct pf_altq {
1688 char ifname[IFNAMSIZ];
1689
1690 /* discipline-specific state */
1691 void *altq_disc __attribute__((aligned(8)));
1692 TAILQ_ENTRY(pf_altq) entries __attribute__((aligned(8)));
1693 #if !defined(__LP64__)
1694 u_int32_t _pad[2];
1695 #endif /* !__LP64__ */
1696
1697 u_int32_t aflags; /* ALTQ rule flags */
1698 u_int32_t bwtype; /* bandwidth type */
1699
1700 /* scheduler spec */
1701 u_int32_t scheduler; /* scheduler type */
1702 u_int32_t tbrsize; /* tokenbucket regulator size */
1703 u_int64_t ifbandwidth; /* interface bandwidth */
1704
1705 /* queue spec */
1706 char qname[PF_QNAME_SIZE]; /* queue name */
1707 char parent[PF_QNAME_SIZE]; /* parent name */
1708 u_int32_t parent_qid; /* parent queue id */
1709 u_int32_t qrflags; /* queue rule flags */
1710 union {
1711 u_int32_t priority; /* priority */
1712 u_int32_t weight; /* weight */
1713 };
1714 u_int32_t qlimit; /* queue size limit */
1715 u_int32_t flags; /* misc flags */
1716 #if !defined(__LP64__)
1717 u_int32_t __pad;
1718 #endif /* !__LP64__ */
1719 u_int64_t bandwidth; /* queue bandwidth */
1720 union {
1721 struct cbq_opts cbq_opts;
1722 struct priq_opts priq_opts;
1723 struct hfsc_opts hfsc_opts;
1724 struct fairq_opts fairq_opts;
1725 struct qfq_opts qfq_opts;
1726 } pq_u;
1727
1728 u_int32_t qid; /* return value */
1729 };
1730
1731 struct pf_tagname {
1732 TAILQ_ENTRY(pf_tagname) entries;
1733 char name[PF_TAG_NAME_SIZE];
1734 u_int16_t tag;
1735 int ref;
1736 };
1737
1738 #define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */
1739 #define PFFRAG_FRAG_HIWAT 1000 /* Number of fragmented packets */
1740 #define PFFRAG_FRCENT_HIWAT 50000 /* Number of fragment cache entries */
1741 #define PFFRAG_FRCACHE_HIWAT 10000 /* Number of fragment descriptors */
1742
1743 #define PFR_KTABLE_HIWAT 1000 /* Number of tables */
1744 #define PFR_KENTRY_HIWAT 200000 /* Number of table entries */
1745 #define PFR_KENTRY_HIWAT_SMALL 100000 /* Number of table entries (tiny hosts) */
1746
1747 /*
1748 * ioctl parameter structures
1749 */
1750
1751 struct pfioc_pooladdr {
1752 u_int32_t action;
1753 u_int32_t ticket;
1754 u_int32_t nr;
1755 u_int32_t r_num;
1756 u_int8_t r_action;
1757 u_int8_t r_last;
1758 u_int8_t af;
1759 char anchor[MAXPATHLEN];
1760 struct pf_pooladdr addr;
1761 };
1762
1763 struct pfioc_rule {
1764 u_int32_t action;
1765 u_int32_t ticket;
1766 u_int32_t pool_ticket;
1767 u_int32_t nr;
1768 char anchor[MAXPATHLEN];
1769 char anchor_call[MAXPATHLEN];
1770 struct pf_rule rule;
1771 };
1772
1773 struct pfioc_natlook {
1774 struct pf_addr saddr;
1775 struct pf_addr daddr;
1776 struct pf_addr rsaddr;
1777 struct pf_addr rdaddr;
1778 union pf_state_xport sxport;
1779 union pf_state_xport dxport;
1780 union pf_state_xport rsxport;
1781 union pf_state_xport rdxport;
1782 sa_family_t af;
1783 u_int8_t proto;
1784 u_int8_t proto_variant;
1785 u_int8_t direction;
1786 };
1787
1788 struct pfioc_state {
1789 struct pfsync_state state;
1790 };
1791
1792 struct pfioc_src_node_kill {
1793 /* XXX returns the number of src nodes killed in psnk_af */
1794 sa_family_t psnk_af;
1795 struct pf_rule_addr psnk_src;
1796 struct pf_rule_addr psnk_dst;
1797 };
1798
1799 struct pfioc_state_addr_kill {
1800 struct pf_addr_wrap addr;
1801 u_int8_t reserved_[3];
1802 u_int8_t neg;
1803 union pf_rule_xport xport;
1804 };
1805
1806 struct pfioc_state_kill {
1807 /* XXX returns the number of states killed in psk_af */
1808 sa_family_t psk_af;
1809 u_int8_t psk_proto;
1810 u_int8_t psk_proto_variant;
1811 u_int8_t _pad;
1812 struct pfioc_state_addr_kill psk_src;
1813 struct pfioc_state_addr_kill psk_dst;
1814 char psk_ifname[IFNAMSIZ];
1815 char psk_ownername[PF_OWNER_NAME_SIZE];
1816 };
1817
1818 struct pfioc_states {
1819 int ps_len;
1820 union {
1821 caddr_t psu_buf;
1822 struct pfsync_state *psu_states;
1823 } ps_u __attribute__((aligned(8)));
1824 #define ps_buf ps_u.psu_buf
1825 #define ps_states ps_u.psu_states
1826 };
1827
1828 #ifdef KERNEL
1829 struct pfioc_states_32 {
1830 int ps_len;
1831 union {
1832 user32_addr_t psu_buf;
1833 user32_addr_t psu_states;
1834 } ps_u __attribute__((aligned(8)));
1835 };
1836
1837 struct pfioc_states_64 {
1838 int ps_len;
1839 union {
1840 user64_addr_t psu_buf;
1841 user64_addr_t psu_states;
1842 } ps_u __attribute__((aligned(8)));
1843 };
1844 #endif /* KERNEL */
1845
1846 #define PFTOK_PROCNAME_LEN 64
1847 #pragma pack(1)
1848 struct pfioc_token {
1849 u_int64_t token_value;
1850 u_int64_t timestamp;
1851 pid_t pid;
1852 char proc_name[PFTOK_PROCNAME_LEN];
1853 };
1854 #pragma pack()
1855
1856 struct pfioc_kernel_token {
1857 SLIST_ENTRY(pfioc_kernel_token) next;
1858 struct pfioc_token token;
1859 };
1860
1861 struct pfioc_remove_token {
1862 u_int64_t token_value;
1863 u_int64_t refcount;
1864 };
1865
1866 struct pfioc_tokens {
1867 int size;
1868 union {
1869 caddr_t pgtu_buf;
1870 struct pfioc_token *pgtu_tokens;
1871 } pgt_u __attribute__((aligned(8)));
1872 #define pgt_buf pgt_u.pgtu_buf
1873 #define pgt_tokens pgt_u.pgtu_tokens
1874 };
1875
1876 #ifdef KERNEL
1877 struct pfioc_tokens_32 {
1878 int size;
1879 union {
1880 user32_addr_t pgtu_buf;
1881 user32_addr_t pgtu_tokens;
1882 } pgt_u __attribute__((aligned(8)));
1883 };
1884
1885 struct pfioc_tokens_64 {
1886 int size;
1887 union {
1888 user64_addr_t pgtu_buf;
1889 user64_addr_t pgtu_tokens;
1890 } pgt_u __attribute__((aligned(8)));
1891 };
1892 #endif /* KERNEL */
1893
1894
1895 struct pfioc_src_nodes {
1896 int psn_len;
1897 union {
1898 caddr_t psu_buf;
1899 struct pf_src_node *psu_src_nodes;
1900 } psn_u __attribute__((aligned(8)));
1901 #define psn_buf psn_u.psu_buf
1902 #define psn_src_nodes psn_u.psu_src_nodes
1903 };
1904
1905 #ifdef KERNEL
1906 struct pfioc_src_nodes_32 {
1907 int psn_len;
1908 union {
1909 user32_addr_t psu_buf;
1910 user32_addr_t psu_src_nodes;
1911 } psn_u __attribute__((aligned(8)));
1912 };
1913
1914 struct pfioc_src_nodes_64 {
1915 int psn_len;
1916 union {
1917 user64_addr_t psu_buf;
1918 user64_addr_t psu_src_nodes;
1919 } psn_u __attribute__((aligned(8)));
1920 };
1921 #endif /* KERNEL */
1922
1923 struct pfioc_if {
1924 char ifname[IFNAMSIZ];
1925 };
1926
1927 struct pfioc_tm {
1928 int timeout;
1929 int seconds;
1930 };
1931
1932 struct pfioc_limit {
1933 int index;
1934 unsigned limit;
1935 };
1936
1937 struct pfioc_altq {
1938 u_int32_t action;
1939 u_int32_t ticket;
1940 u_int32_t nr;
1941 struct pf_altq altq __attribute__((aligned(8)));
1942 };
1943
1944 struct pfioc_qstats {
1945 u_int32_t ticket;
1946 u_int32_t nr;
1947 void *buf __attribute__((aligned(8)));
1948 int nbytes __attribute__((aligned(8)));
1949 u_int8_t scheduler;
1950 };
1951
1952 struct pfioc_ruleset {
1953 u_int32_t nr;
1954 char path[MAXPATHLEN];
1955 char name[PF_ANCHOR_NAME_SIZE];
1956 };
1957
1958 #define PF_RULESET_ALTQ (PF_RULESET_MAX)
1959 #define PF_RULESET_TABLE (PF_RULESET_MAX+1)
1960 struct pfioc_trans {
1961 int size; /* number of elements */
1962 int esize; /* size of each element in bytes */
1963 struct pfioc_trans_e {
1964 int rs_num;
1965 char anchor[MAXPATHLEN];
1966 u_int32_t ticket;
1967 } *array __attribute__((aligned(8)));
1968 };
1969
1970 #ifdef KERNEL
1971 struct pfioc_trans_32 {
1972 int size; /* number of elements */
1973 int esize; /* size of each element in bytes */
1974 user32_addr_t array __attribute__((aligned(8)));
1975 };
1976
1977 struct pfioc_trans_64 {
1978 int size; /* number of elements */
1979 int esize; /* size of each element in bytes */
1980 user64_addr_t array __attribute__((aligned(8)));
1981 };
1982 #endif /* KERNEL */
1983
1984
1985 #define PFR_FLAG_ATOMIC 0x00000001
1986 #define PFR_FLAG_DUMMY 0x00000002
1987 #define PFR_FLAG_FEEDBACK 0x00000004
1988 #define PFR_FLAG_CLSTATS 0x00000008
1989 #define PFR_FLAG_ADDRSTOO 0x00000010
1990 #define PFR_FLAG_REPLACE 0x00000020
1991 #define PFR_FLAG_ALLRSETS 0x00000040
1992 #define PFR_FLAG_ALLMASK 0x0000007F
1993 #ifdef KERNEL
1994 #define PFR_FLAG_USERIOCTL 0x10000000
1995 #endif /* KERNEL */
1996
1997 struct pfioc_table {
1998 struct pfr_table pfrio_table;
1999 void *pfrio_buffer __attribute__((aligned(8)));
2000 int pfrio_esize __attribute__((aligned(8)));
2001 int pfrio_size;
2002 int pfrio_size2;
2003 int pfrio_nadd;
2004 int pfrio_ndel;
2005 int pfrio_nchange;
2006 int pfrio_flags;
2007 u_int32_t pfrio_ticket;
2008 };
2009 #define pfrio_exists pfrio_nadd
2010 #define pfrio_nzero pfrio_nadd
2011 #define pfrio_nmatch pfrio_nadd
2012 #define pfrio_naddr pfrio_size2
2013 #define pfrio_setflag pfrio_size2
2014 #define pfrio_clrflag pfrio_nadd
2015
2016 #ifdef KERNEL
2017 struct pfioc_table_32 {
2018 struct pfr_table pfrio_table;
2019 user32_addr_t pfrio_buffer __attribute__((aligned(8)));
2020 int pfrio_esize __attribute__((aligned(8)));
2021 int pfrio_size;
2022 int pfrio_size2;
2023 int pfrio_nadd;
2024 int pfrio_ndel;
2025 int pfrio_nchange;
2026 int pfrio_flags;
2027 u_int32_t pfrio_ticket;
2028 };
2029
2030 struct pfioc_table_64 {
2031 struct pfr_table pfrio_table;
2032 user64_addr_t pfrio_buffer __attribute__((aligned(8)));
2033 int pfrio_esize __attribute__((aligned(8)));
2034 int pfrio_size;
2035 int pfrio_size2;
2036 int pfrio_nadd;
2037 int pfrio_ndel;
2038 int pfrio_nchange;
2039 int pfrio_flags;
2040 u_int32_t pfrio_ticket;
2041 };
2042 #endif /* KERNEL */
2043
2044 struct pfioc_iface {
2045 char pfiio_name[IFNAMSIZ];
2046 void *pfiio_buffer __attribute__((aligned(8)));
2047 int pfiio_esize __attribute__((aligned(8)));
2048 int pfiio_size;
2049 int pfiio_nzero;
2050 int pfiio_flags;
2051 };
2052
2053 #ifdef KERNEL
2054 struct pfioc_iface_32 {
2055 char pfiio_name[IFNAMSIZ];
2056 user32_addr_t pfiio_buffer __attribute__((aligned(8)));
2057 int pfiio_esize __attribute__((aligned(8)));
2058 int pfiio_size;
2059 int pfiio_nzero;
2060 int pfiio_flags;
2061 };
2062
2063 struct pfioc_iface_64 {
2064 char pfiio_name[IFNAMSIZ];
2065 user64_addr_t pfiio_buffer __attribute__((aligned(8)));
2066 int pfiio_esize __attribute__((aligned(8)));
2067 int pfiio_size;
2068 int pfiio_nzero;
2069 int pfiio_flags;
2070 };
2071 #endif /* KERNEL */
2072
2073 struct pf_ifspeed {
2074 char ifname[IFNAMSIZ];
2075 u_int64_t baudrate;
2076 };
2077
2078 /*
2079 * ioctl operations
2080 */
2081
2082 #define DIOCSTART _IO ('D', 1)
2083 #define DIOCSTOP _IO ('D', 2)
2084 #define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule)
2085 #define DIOCGETSTARTERS _IOWR('D', 5, struct pfioc_tokens)
2086 #define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule)
2087 #define DIOCGETRULE _IOWR('D', 7, struct pfioc_rule)
2088 #define DIOCSTARTREF _IOR ('D', 8, u_int64_t)
2089 #define DIOCSTOPREF _IOWR('D', 9, struct pfioc_remove_token)
2090 /* XXX cut 10 - 17 */
2091 #define DIOCCLRSTATES _IOWR('D', 18, struct pfioc_state_kill)
2092 #define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state)
2093 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
2094 #define DIOCGETSTATUS _IOWR('D', 21, struct pf_status)
2095 #define DIOCCLRSTATUS _IO ('D', 22)
2096 #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook)
2097 #define DIOCSETDEBUG _IOWR('D', 24, u_int32_t)
2098 #define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states)
2099 #define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule)
2100 #define DIOCINSERTRULE _IOWR('D', 27, struct pfioc_rule)
2101 #define DIOCDELETERULE _IOWR('D', 28, struct pfioc_rule)
2102 #define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm)
2103 #define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm)
2104 #define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state)
2105 #define DIOCCLRRULECTRS _IO ('D', 38)
2106 #define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit)
2107 #define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit)
2108 #define DIOCKILLSTATES _IOWR('D', 41, struct pfioc_state_kill)
2109 #define DIOCSTARTALTQ _IO ('D', 42)
2110 #define DIOCSTOPALTQ _IO ('D', 43)
2111 #define DIOCADDALTQ _IOWR('D', 45, struct pfioc_altq)
2112 #define DIOCGETALTQS _IOWR('D', 47, struct pfioc_altq)
2113 #define DIOCGETALTQ _IOWR('D', 48, struct pfioc_altq)
2114 #define DIOCCHANGEALTQ _IOWR('D', 49, struct pfioc_altq)
2115 #define DIOCGETQSTATS _IOWR('D', 50, struct pfioc_qstats)
2116 #define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr)
2117 #define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr)
2118 #define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr)
2119 #define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr)
2120 #define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr)
2121 /* XXX cut 55 - 57 */
2122 #define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset)
2123 #define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset)
2124 #define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table)
2125 #define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table)
2126 #define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table)
2127 #define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table)
2128 #define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table)
2129 #define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table)
2130 #define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table)
2131 #define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table)
2132 #define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table)
2133 #define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table)
2134 #define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table)
2135 #define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table)
2136 #define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table)
2137 #define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table)
2138 #define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table)
2139 #define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table)
2140 #define DIOCOSFPFLUSH _IO('D', 78)
2141 #define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl)
2142 #define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl)
2143 #define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans)
2144 #define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans)
2145 #define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans)
2146 #define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes)
2147 #define DIOCCLRSRCNODES _IO('D', 85)
2148 #define DIOCSETHOSTID _IOWR('D', 86, u_int32_t)
2149 #define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface)
2150 #define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface)
2151 #define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface)
2152 #define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill)
2153 #define DIOCGIFSPEED _IOWR('D', 92, struct pf_ifspeed)
2154
2155 #ifdef KERNEL
2156 RB_HEAD(pf_src_tree, pf_src_node);
2157 RB_PROTOTYPE_SC(__private_extern__, pf_src_tree, pf_src_node, entry,
2158 pf_src_compare);
2159 extern struct pf_src_tree tree_src_tracking;
2160
2161 RB_HEAD(pf_state_tree_id, pf_state);
2162 RB_PROTOTYPE_SC(__private_extern__, pf_state_tree_id, pf_state,
2163 entry_id, pf_state_compare_id);
2164 extern struct pf_state_tree_id tree_id;
2165 extern struct pf_state_queue state_list;
2166
2167 TAILQ_HEAD(pf_poolqueue, pf_pool);
2168 extern struct pf_poolqueue pf_pools[2];
2169 extern struct pf_palist pf_pabuf;
2170 extern u_int32_t ticket_pabuf;
2171 extern struct pf_poolqueue *pf_pools_active;
2172 extern struct pf_poolqueue *pf_pools_inactive;
2173
2174 __private_extern__ int pf_tbladdr_setup(struct pf_ruleset *,
2175 struct pf_addr_wrap *);
2176 __private_extern__ void pf_tbladdr_remove(struct pf_addr_wrap *);
2177 __private_extern__ void pf_tbladdr_copyout(struct pf_addr_wrap *);
2178 __private_extern__ void pf_calc_skip_steps(struct pf_rulequeue *);
2179 __private_extern__ u_int32_t pf_calc_state_key_flowhash(struct pf_state_key *);
2180
2181 extern struct pool pf_src_tree_pl, pf_rule_pl;
2182 extern struct pool pf_state_pl, pf_state_key_pl, pf_pooladdr_pl;
2183 extern struct pool pf_state_scrub_pl;
2184 extern struct pool pf_app_state_pl;
2185
2186 extern struct thread *pf_purge_thread;
2187
2188 __private_extern__ void pfinit(void);
2189 __private_extern__ void pf_purge_thread_fn(void *, wait_result_t);
2190 __private_extern__ void pf_purge_expired_src_nodes(void);
2191 __private_extern__ void pf_purge_expired_states(u_int32_t);
2192 __private_extern__ void pf_unlink_state(struct pf_state *);
2193 __private_extern__ void pf_free_state(struct pf_state *);
2194 __private_extern__ int pf_insert_state(struct pfi_kif *, struct pf_state *);
2195 __private_extern__ int pf_insert_src_node(struct pf_src_node **,
2196 struct pf_rule *, struct pf_addr *, sa_family_t);
2197 __private_extern__ void pf_src_tree_remove_state(struct pf_state *);
2198 __private_extern__ struct pf_state *pf_find_state_byid(struct pf_state_cmp *);
2199 __private_extern__ struct pf_state *pf_find_state_all(struct pf_state_key_cmp *,
2200 u_int, int *);
2201 __private_extern__ void pf_print_state(struct pf_state *);
2202 __private_extern__ void pf_print_flags(u_int8_t);
2203 __private_extern__ u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
2204 u_int8_t);
2205
2206 extern struct ifnet *sync_ifp;
2207 extern struct pf_rule pf_default_rule;
2208 __private_extern__ void pf_addrcpy(struct pf_addr *, struct pf_addr *,
2209 u_int8_t);
2210 __private_extern__ void pf_rm_rule(struct pf_rulequeue *, struct pf_rule *);
2211
2212 struct ip_fw_args;
2213
2214 extern boolean_t is_nlc_enabled_glb;
2215 extern boolean_t pf_is_nlc_enabled(void);
2216
2217 #if INET
2218 __private_extern__ int pf_test(int, struct ifnet *, pbuf_t **,
2219 struct ether_header *, struct ip_fw_args *);
2220 __private_extern__ int pf_test_mbuf(int, struct ifnet *, struct mbuf **,
2221 struct ether_header *, struct ip_fw_args *);
2222 #endif /* INET */
2223
2224 #if INET6
2225 __private_extern__ int pf_test6(int, struct ifnet *, pbuf_t **,
2226 struct ether_header *, struct ip_fw_args *);
2227 __private_extern__ int pf_test6_mbuf(int, struct ifnet *, struct mbuf **,
2228 struct ether_header *, struct ip_fw_args *);
2229 __private_extern__ void pf_poolmask(struct pf_addr *, struct pf_addr *,
2230 struct pf_addr *, struct pf_addr *, u_int8_t);
2231 __private_extern__ void pf_addr_inc(struct pf_addr *, sa_family_t);
2232 #endif /* INET6 */
2233
2234 __private_extern__ void *pf_lazy_makewritable(struct pf_pdesc *,
2235 pbuf_t *, int);
2236 __private_extern__ void *pf_pull_hdr(pbuf_t *, int, void *, int,
2237 u_short *, u_short *, sa_family_t);
2238 __private_extern__ void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
2239 __private_extern__ int pflog_packet(struct pfi_kif *, pbuf_t *,
2240 sa_family_t, u_int8_t, u_int8_t, struct pf_rule *, struct pf_rule *,
2241 struct pf_ruleset *, struct pf_pdesc *);
2242 __private_extern__ int pf_match_addr(u_int8_t, struct pf_addr *,
2243 struct pf_addr *, struct pf_addr *, sa_family_t);
2244 __private_extern__ int pf_match_addr_range(struct pf_addr *, struct pf_addr *,
2245 struct pf_addr *, sa_family_t);
2246 __private_extern__ int pf_match(u_int8_t, u_int32_t, u_int32_t, u_int32_t);
2247 __private_extern__ int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
2248 __private_extern__ int pf_match_xport(u_int8_t, u_int8_t, union pf_rule_xport *,
2249 union pf_state_xport *);
2250 __private_extern__ int pf_match_uid(u_int8_t, uid_t, uid_t, uid_t);
2251 __private_extern__ int pf_match_gid(u_int8_t, gid_t, gid_t, gid_t);
2252
2253 __private_extern__ void pf_normalize_init(void);
2254 __private_extern__ int pf_normalize_isempty(void);
2255 __private_extern__ int pf_normalize_ip(pbuf_t *, int, struct pfi_kif *,
2256 u_short *, struct pf_pdesc *);
2257 __private_extern__ int pf_normalize_ip6(pbuf_t *, int, struct pfi_kif *,
2258 u_short *, struct pf_pdesc *);
2259 __private_extern__ int pf_normalize_tcp(int, struct pfi_kif *, pbuf_t *,
2260 int, int, void *, struct pf_pdesc *);
2261 __private_extern__ void pf_normalize_tcp_cleanup(struct pf_state *);
2262 __private_extern__ int pf_normalize_tcp_init(pbuf_t *, int,
2263 struct pf_pdesc *, struct tcphdr *, struct pf_state_peer *,
2264 struct pf_state_peer *);
2265 __private_extern__ int pf_normalize_tcp_stateful(pbuf_t *, int,
2266 struct pf_pdesc *, u_short *, struct tcphdr *, struct pf_state *,
2267 struct pf_state_peer *, struct pf_state_peer *, int *);
2268 __private_extern__ u_int64_t pf_state_expires(const struct pf_state *);
2269 __private_extern__ void pf_purge_expired_fragments(void);
2270 __private_extern__ int pf_routable(struct pf_addr *addr, sa_family_t af,
2271 struct pfi_kif *);
2272 __private_extern__ int pf_rtlabel_match(struct pf_addr *, sa_family_t,
2273 struct pf_addr_wrap *);
2274 __private_extern__ int pf_socket_lookup(int, struct pf_pdesc *);
2275 __private_extern__ struct pf_state_key *pf_alloc_state_key(struct pf_state *,
2276 struct pf_state_key *);
2277 __private_extern__ void pfr_initialize(void);
2278 __private_extern__ int pfr_match_addr(struct pfr_ktable *, struct pf_addr *,
2279 sa_family_t);
2280 __private_extern__ void pfr_update_stats(struct pfr_ktable *, struct pf_addr *,
2281 sa_family_t, u_int64_t, int, int, int);
2282 __private_extern__ int pfr_pool_get(struct pfr_ktable *, int *,
2283 struct pf_addr *, struct pf_addr **, struct pf_addr **, sa_family_t);
2284 __private_extern__ void pfr_dynaddr_update(struct pfr_ktable *,
2285 struct pfi_dynaddr *);
2286 __private_extern__ void pfr_table_copyin_cleanup(struct pfr_table *);
2287 __private_extern__ struct pfr_ktable *pfr_attach_table(struct pf_ruleset *,
2288 char *);
2289 __private_extern__ void pfr_detach_table(struct pfr_ktable *);
2290 __private_extern__ int pfr_clr_tables(struct pfr_table *, int *, int);
2291 __private_extern__ int pfr_add_tables(user_addr_t, int, int *, int);
2292 __private_extern__ int pfr_del_tables(user_addr_t, int, int *, int);
2293 __private_extern__ int pfr_get_tables(struct pfr_table *, user_addr_t,
2294 int *, int);
2295 __private_extern__ int pfr_get_tstats(struct pfr_table *, user_addr_t,
2296 int *, int);
2297 __private_extern__ int pfr_clr_tstats(user_addr_t, int, int *, int);
2298 __private_extern__ int pfr_set_tflags(user_addr_t, int, int, int, int *,
2299 int *, int);
2300 __private_extern__ int pfr_clr_addrs(struct pfr_table *, int *, int);
2301 __private_extern__ int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *,
2302 u_int64_t);
2303 __private_extern__ int pfr_add_addrs(struct pfr_table *, user_addr_t,
2304 int, int *, int);
2305 __private_extern__ int pfr_del_addrs(struct pfr_table *, user_addr_t,
2306 int, int *, int);
2307 __private_extern__ int pfr_set_addrs(struct pfr_table *, user_addr_t,
2308 int, int *, int *, int *, int *, int, u_int32_t);
2309 __private_extern__ int pfr_get_addrs(struct pfr_table *, user_addr_t,
2310 int *, int);
2311 __private_extern__ int pfr_get_astats(struct pfr_table *, user_addr_t,
2312 int *, int);
2313 __private_extern__ int pfr_clr_astats(struct pfr_table *, user_addr_t,
2314 int, int *, int);
2315 __private_extern__ int pfr_tst_addrs(struct pfr_table *, user_addr_t,
2316 int, int *, int);
2317 __private_extern__ int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *,
2318 int);
2319 __private_extern__ int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *,
2320 int);
2321 __private_extern__ int pfr_ina_commit(struct pfr_table *, u_int32_t, int *,
2322 int *, int);
2323 __private_extern__ int pfr_ina_define(struct pfr_table *, user_addr_t,
2324 int, int *, int *, u_int32_t, int);
2325
2326 extern struct pfi_kif *pfi_all;
2327
2328 __private_extern__ void pfi_initialize(void);
2329 __private_extern__ struct pfi_kif *pfi_kif_get(const char *);
2330 __private_extern__ void pfi_kif_ref(struct pfi_kif *, enum pfi_kif_refs);
2331 __private_extern__ void pfi_kif_unref(struct pfi_kif *, enum pfi_kif_refs);
2332 __private_extern__ int pfi_kif_match(struct pfi_kif *, struct pfi_kif *);
2333 __private_extern__ void pfi_attach_ifnet(struct ifnet *);
2334 __private_extern__ void pfi_detach_ifnet(struct ifnet *);
2335 __private_extern__ int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
2336 sa_family_t);
2337 __private_extern__ int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
2338 __private_extern__ void pfi_dynaddr_remove(struct pf_addr_wrap *);
2339 __private_extern__ void pfi_dynaddr_copyout(struct pf_addr_wrap *);
2340 __private_extern__ void pfi_update_status(const char *, struct pf_status *);
2341 __private_extern__ int pfi_get_ifaces(const char *, user_addr_t, int *);
2342 __private_extern__ int pfi_set_flags(const char *, int);
2343 __private_extern__ int pfi_clear_flags(const char *, int);
2344
2345 __private_extern__ u_int16_t pf_tagname2tag(char *);
2346 __private_extern__ void pf_tag2tagname(u_int16_t, char *);
2347 __private_extern__ void pf_tag_ref(u_int16_t);
2348 __private_extern__ void pf_tag_unref(u_int16_t);
2349 __private_extern__ int pf_tag_packet(pbuf_t *, struct pf_mtag *,
2350 int, unsigned int, struct pf_pdesc *);
2351 __private_extern__ void pf_step_into_anchor(int *, struct pf_ruleset **, int,
2352 struct pf_rule **, struct pf_rule **, int *);
2353 __private_extern__ int pf_step_out_of_anchor(int *, struct pf_ruleset **, int,
2354 struct pf_rule **, struct pf_rule **, int *);
2355 __private_extern__ u_int32_t pf_qname2qid(char *);
2356 __private_extern__ void pf_qid2qname(u_int32_t, char *);
2357 __private_extern__ void pf_qid_unref(u_int32_t);
2358
2359 extern struct pf_status pf_status;
2360 extern struct pool pf_frent_pl, pf_frag_pl;
2361
2362 struct pf_pool_limit {
2363 void *pp;
2364 unsigned limit;
2365 };
2366 extern struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX];
2367
2368 __private_extern__ int pf_af_hook(struct ifnet *, struct mbuf **,
2369 struct mbuf **, unsigned int, int, struct ip_fw_args *);
2370 __private_extern__ int pf_ifaddr_hook(struct ifnet *);
2371 __private_extern__ void pf_ifnet_hook(struct ifnet *, int);
2372
2373 /*
2374 * The following are defined with "private extern" storage class for
2375 * kernel, and "extern" for user-space.
2376 */
2377 extern struct pf_anchor_global pf_anchors;
2378 extern struct pf_anchor pf_main_anchor;
2379 #define pf_main_ruleset pf_main_anchor.ruleset
2380
2381 extern int pf_is_enabled;
2382 extern int16_t pf_nat64_configured;
2383 #define PF_IS_ENABLED (pf_is_enabled != 0)
2384 extern u_int32_t pf_hash_seed;
2385
2386 /* these ruleset functions can be linked into userland programs (pfctl) */
2387 __private_extern__ int pf_get_ruleset_number(u_int8_t);
2388 __private_extern__ void pf_init_ruleset(struct pf_ruleset *);
2389 __private_extern__ int pf_anchor_setup(struct pf_rule *,
2390 const struct pf_ruleset *, const char *);
2391 __private_extern__ int pf_anchor_copyout(const struct pf_ruleset *,
2392 const struct pf_rule *, struct pfioc_rule *);
2393 __private_extern__ void pf_anchor_remove(struct pf_rule *);
2394 __private_extern__ void pf_remove_if_empty_ruleset(struct pf_ruleset *);
2395 __private_extern__ struct pf_anchor *pf_find_anchor(const char *);
2396 __private_extern__ struct pf_ruleset *pf_find_ruleset(const char *);
2397 __private_extern__ struct pf_ruleset *pf_find_ruleset_with_owner(const char *,
2398 const char *, int, int *);
2399 __private_extern__ struct pf_ruleset *pf_find_or_create_ruleset(const char *);
2400 __private_extern__ void pf_rs_initialize(void);
2401
2402 __private_extern__ int pf_osfp_add(struct pf_osfp_ioctl *);
2403 __private_extern__ struct pf_osfp_enlist *pf_osfp_fingerprint(struct pf_pdesc *,
2404 pbuf_t *, int, const struct tcphdr *);
2405 __private_extern__ struct pf_osfp_enlist *pf_osfp_fingerprint_hdr(
2406 const struct ip *, const struct ip6_hdr *, const struct tcphdr *);
2407 __private_extern__ void pf_osfp_flush(void);
2408 __private_extern__ int pf_osfp_get(struct pf_osfp_ioctl *);
2409 __private_extern__ void pf_osfp_initialize(void);
2410 __private_extern__ int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
2411 __private_extern__ struct pf_os_fingerprint *pf_osfp_validate(void);
2412 __private_extern__ struct pf_mtag *pf_find_mtag(struct mbuf *);
2413 __private_extern__ struct pf_mtag *pf_find_mtag_pbuf(pbuf_t *);
2414 __private_extern__ struct pf_mtag *pf_get_mtag(struct mbuf *);
2415 __private_extern__ struct pf_mtag *pf_get_mtag_pbuf(pbuf_t *);
2416 #else /* !KERNEL */
2417 extern struct pf_anchor_global pf_anchors;
2418 extern struct pf_anchor pf_main_anchor;
2419 #define pf_main_ruleset pf_main_anchor.ruleset
2420
2421 /* these ruleset functions can be linked into userland programs (pfctl) */
2422 extern int pf_get_ruleset_number(u_int8_t);
2423 extern void pf_init_ruleset(struct pf_ruleset *);
2424 extern int pf_anchor_setup(struct pf_rule *, const struct pf_ruleset *,
2425 const char *);
2426 extern int pf_anchor_copyout(const struct pf_ruleset *, const struct pf_rule *,
2427 struct pfioc_rule *);
2428 extern void pf_anchor_remove(struct pf_rule *);
2429 extern void pf_remove_if_empty_ruleset(struct pf_ruleset *);
2430 extern struct pf_anchor *pf_find_anchor(const char *);
2431 extern struct pf_ruleset *pf_find_ruleset(const char *);
2432 extern struct pf_ruleset *pf_find_ruleset_with_owner(const char *,
2433 const char *, int, int *);
2434 extern struct pf_ruleset *pf_find_or_create_ruleset(const char *);
2435 extern void pf_rs_initialize(void);
2436 #endif /* !KERNEL */
2437
2438 #ifdef __cplusplus
2439 }
2440 #endif
2441 #endif /* PF || !KERNEL */
2442 #endif /* PRIVATE */
2443 #endif /* _NET_PFVAR_H_ */