1/* $FreeBSD: head/sys/contrib/pf/net/pfvar.h 196372 2009-08-19 00:10:10Z mlaier $ */
2/* $OpenBSD: pfvar.h,v 1.244 2007/02/23 21:31:51 deraadt Exp $ */
3
4/*
5 * Copyright (c) 2001 Daniel Hartmeier
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#ifndef _NET_PFVAR_H_
35#define _NET_PFVAR_H_
36
37#include <sys/param.h>
38#include <sys/types.h>
39#include <sys/queue.h>
40#include <sys/tree.h>
41#ifdef __FreeBSD__
42#include <sys/lock.h>
43#include <sys/sx.h>
44#else
45#include <sys/rwlock.h>
46#endif
47
48#include <net/radix.h>
49#include <net/route.h>
50#ifdef __FreeBSD__
51#include <net/if_clone.h>
52#include <net/pf_mtag.h>
53#include <vm/uma.h>
54#else
55#include <netinet/ip_ipsp.h>
56#endif
57
58#ifdef __FreeBSD__
59#include <netinet/in.h>
60#endif
61
62#include <netinet/tcp_fsm.h>
63
64struct ip;
65struct ip6_hdr;
66#ifdef __FreeBSD__
67struct inpcb;
68#endif
69
70#define PF_TCPS_PROXY_SRC ((TCP_NSTATES)+0)
71#define PF_TCPS_PROXY_DST ((TCP_NSTATES)+1)
72
73#define PF_MD5_DIGEST_LENGTH 16
74#ifdef MD5_DIGEST_LENGTH
75#if PF_MD5_DIGEST_LENGTH != MD5_DIGEST_LENGTH
76#error
77#endif
78#endif
79
80enum { PF_INOUT, PF_IN, PF_OUT };
81enum { PF_LAN_EXT, PF_EXT_GWY, PF_ID };
82enum { PF_PASS, PF_DROP, PF_SCRUB, PF_NOSCRUB, PF_NAT, PF_NONAT,
83 PF_BINAT, PF_NOBINAT, PF_RDR, PF_NORDR, PF_SYNPROXY_DROP };
84enum { PF_RULESET_SCRUB, PF_RULESET_FILTER, PF_RULESET_NAT,
85 PF_RULESET_BINAT, PF_RULESET_RDR, PF_RULESET_MAX };
86enum { PF_OP_NONE, PF_OP_IRG, PF_OP_EQ, PF_OP_NE, PF_OP_LT,
87 PF_OP_LE, PF_OP_GT, PF_OP_GE, PF_OP_XRG, PF_OP_RRG };
88enum { PF_DEBUG_NONE, PF_DEBUG_URGENT, PF_DEBUG_MISC, PF_DEBUG_NOISY };
89enum { PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
90 PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
91 PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
92enum { PF_GET_NONE, PF_GET_CLR_CNTR };
93
94/*
95 * Note about PFTM_*: real indices into pf_rule.timeout[] come before
96 * PFTM_MAX, special cases afterwards. See pf_state_expires().
97 */
98enum { PFTM_TCP_FIRST_PACKET, PFTM_TCP_OPENING, PFTM_TCP_ESTABLISHED,
99 PFTM_TCP_CLOSING, PFTM_TCP_FIN_WAIT, PFTM_TCP_CLOSED,
100 PFTM_UDP_FIRST_PACKET, PFTM_UDP_SINGLE, PFTM_UDP_MULTIPLE,
101 PFTM_ICMP_FIRST_PACKET, PFTM_ICMP_ERROR_REPLY,
102 PFTM_OTHER_FIRST_PACKET, PFTM_OTHER_SINGLE,
103 PFTM_OTHER_MULTIPLE, PFTM_FRAG, PFTM_INTERVAL,
104 PFTM_ADAPTIVE_START, PFTM_ADAPTIVE_END, PFTM_SRC_NODE,
105 PFTM_TS_DIFF, PFTM_MAX, PFTM_PURGE, PFTM_UNLINKED,
106 PFTM_UNTIL_PACKET };
107
108/* PFTM default values */
109#define PFTM_TCP_FIRST_PACKET_VAL 120 /* First TCP packet */
110#define PFTM_TCP_OPENING_VAL 30 /* No response yet */
111#define PFTM_TCP_ESTABLISHED_VAL 24*60*60/* Established */
112#define PFTM_TCP_CLOSING_VAL 15 * 60 /* Half closed */
113#define PFTM_TCP_FIN_WAIT_VAL 45 /* Got both FINs */
114#define PFTM_TCP_CLOSED_VAL 90 /* Got a RST */
115#define PFTM_UDP_FIRST_PACKET_VAL 60 /* First UDP packet */
116#define PFTM_UDP_SINGLE_VAL 30 /* Unidirectional */
117#define PFTM_UDP_MULTIPLE_VAL 60 /* Bidirectional */
118#define PFTM_ICMP_FIRST_PACKET_VAL 20 /* First ICMP packet */
119#define PFTM_ICMP_ERROR_REPLY_VAL 10 /* Got error response */
120#define PFTM_OTHER_FIRST_PACKET_VAL 60 /* First packet */
121#define PFTM_OTHER_SINGLE_VAL 30 /* Unidirectional */
122#define PFTM_OTHER_MULTIPLE_VAL 60 /* Bidirectional */
123#define PFTM_FRAG_VAL 30 /* Fragment expire */
124#define PFTM_INTERVAL_VAL 10 /* Expire interval */
125#define PFTM_SRC_NODE_VAL 0 /* Source tracking */
126#define PFTM_TS_DIFF_VAL 30 /* Allowed TS diff */
127
128enum { PF_NOPFROUTE, PF_FASTROUTE, PF_ROUTETO, PF_DUPTO, PF_REPLYTO };
129enum { PF_LIMIT_STATES, PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
130 PF_LIMIT_TABLES, PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_MAX };
131#define PF_POOL_IDMASK 0x0f
132enum { PF_POOL_NONE, PF_POOL_BITMASK, PF_POOL_RANDOM,
133 PF_POOL_SRCHASH, PF_POOL_ROUNDROBIN };
134enum { PF_ADDR_ADDRMASK, PF_ADDR_NOROUTE, PF_ADDR_DYNIFTL,
135 PF_ADDR_TABLE, PF_ADDR_RTLABEL, PF_ADDR_URPFFAILED };
136#define PF_POOL_TYPEMASK 0x0f
137#define PF_POOL_STICKYADDR 0x20
138#define PF_WSCALE_FLAG 0x80
139#define PF_WSCALE_MASK 0x0f
140
141#define PF_LOG 0x01
142#define PF_LOG_ALL 0x02
143#define PF_LOG_SOCKET_LOOKUP 0x04
144
145struct pf_addr {
146 union {
147 struct in_addr v4;
148 struct in6_addr v6;
149 u_int8_t addr8[16];
150 u_int16_t addr16[8];
151 u_int32_t addr32[4];
152 } pfa; /* 128-bit address */
153#define v4 pfa.v4
154#define v6 pfa.v6
155#define addr8 pfa.addr8
156#define addr16 pfa.addr16
157#define addr32 pfa.addr32
158};
159
160#define PF_TABLE_NAME_SIZE 32
161
162#define PFI_AFLAG_NETWORK 0x01
163#define PFI_AFLAG_BROADCAST 0x02
164#define PFI_AFLAG_PEER 0x04
165#define PFI_AFLAG_MODEMASK 0x07
166#define PFI_AFLAG_NOALIAS 0x08
167
168struct pf_addr_wrap {
169 union {
170 struct {
171 struct pf_addr addr;
172 struct pf_addr mask;
173 } a;
174 char ifname[IFNAMSIZ];
175 char tblname[PF_TABLE_NAME_SIZE];
176#ifdef __FreeBSD__
177#define RTLABEL_LEN 32
178#endif
179 char rtlabelname[RTLABEL_LEN];
180 u_int32_t rtlabel;
181 } v;
182 union {
183 struct pfi_dynaddr *dyn;
184 struct pfr_ktable *tbl;
185 int dyncnt;
186 int tblcnt;
187 } p;
188 u_int8_t type; /* PF_ADDR_* */
189 u_int8_t iflags; /* PFI_AFLAG_* */
190};
191
192#ifdef _KERNEL
193
194struct pfi_dynaddr {
195 TAILQ_ENTRY(pfi_dynaddr) entry;
196 struct pf_addr pfid_addr4;
197 struct pf_addr pfid_mask4;
198 struct pf_addr pfid_addr6;
199 struct pf_addr pfid_mask6;
200 struct pfr_ktable *pfid_kt;
201 struct pfi_kif *pfid_kif;
202 void *pfid_hook_cookie;
203 int pfid_net; /* mask or 128 */
204 int pfid_acnt4; /* address count IPv4 */
205 int pfid_acnt6; /* address count IPv6 */
206 sa_family_t pfid_af; /* rule af */
207 u_int8_t pfid_iflags; /* PFI_AFLAG_* */
208};
209
210/*
211 * Address manipulation macros
212 */
213
214#ifdef __FreeBSD__
215#define splsoftnet() splnet()
216
217#define HTONL(x) (x) = htonl((__uint32_t)(x))
218#define HTONS(x) (x) = htons((__uint16_t)(x))
219#define NTOHL(x) (x) = ntohl((__uint32_t)(x))
220#define NTOHS(x) (x) = ntohs((__uint16_t)(x))
221
222#define PF_NAME "pf"
223
224#define PR_NOWAIT M_NOWAIT
225#define pool_get(p, f) uma_zalloc(*(p), (f))
226#define pool_put(p, o) uma_zfree(*(p), (o))
227
228#define UMA_CREATE(var, type, desc) \
229 var = uma_zcreate(desc, sizeof(type), \
230 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); \
231 if (var == NULL) break
232#define UMA_DESTROY(var) \
233 if(var) uma_zdestroy(var)
234
235extern struct mtx pf_task_mtx;
236
237#define PF_ASSERT(h) mtx_assert(&pf_task_mtx, (h))
238
239#define PF_LOCK() do { \
240 PF_ASSERT(MA_NOTOWNED); \
241 mtx_lock(&pf_task_mtx); \
242} while(0)
243#define PF_UNLOCK() do { \
244 PF_ASSERT(MA_OWNED); \
245 mtx_unlock(&pf_task_mtx); \
246} while(0)
247
248#define PF_COPYIN(uaddr, kaddr, len, r) do { \
249 PF_UNLOCK(); \
250 r = copyin((uaddr), (kaddr), (len)); \
251 PF_LOCK(); \
252} while(0)
253
254#define PF_COPYOUT(kaddr, uaddr, len, r) do { \
255 PF_UNLOCK(); \
256 r = copyout((kaddr), (uaddr), (len)); \
257 PF_LOCK(); \
258} while(0)
259
260extern void init_pf_mutex(void);
261extern void destroy_pf_mutex(void);
262
263#define PF_MODVER 1
264#define PFLOG_MODVER 1
265#define PFSYNC_MODVER 1
266
267#define PFLOG_MINVER 1
268#define PFLOG_PREFVER PFLOG_MODVER
269#define PFLOG_MAXVER 1
270#define PFSYNC_MINVER 1
271#define PFSYNC_PREFVER PFSYNC_MODVER
272#define PFSYNC_MAXVER 1
273#endif /* __FreeBSD__ */
274
275#ifdef INET
276#ifndef INET6
277#define PF_INET_ONLY
278#endif /* ! INET6 */
279#endif /* INET */
280
281#ifdef INET6
282#ifndef INET
283#define PF_INET6_ONLY
284#endif /* ! INET */
285#endif /* INET6 */
286
287#ifdef INET
288#ifdef INET6
289#define PF_INET_INET6
290#endif /* INET6 */
291#endif /* INET */
292
293#else
294
295#define PF_INET_INET6
296
297#endif /* _KERNEL */
298
299/* Both IPv4 and IPv6 */
300#ifdef PF_INET_INET6
301
302#define PF_AEQ(a, b, c) \
303 ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
304 ((a)->addr32[3] == (b)->addr32[3] && \
305 (a)->addr32[2] == (b)->addr32[2] && \
306 (a)->addr32[1] == (b)->addr32[1] && \
307 (a)->addr32[0] == (b)->addr32[0])) \
308
309#define PF_ANEQ(a, b, c) \
310 ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
311 ((a)->addr32[3] != (b)->addr32[3] || \
312 (a)->addr32[2] != (b)->addr32[2] || \
313 (a)->addr32[1] != (b)->addr32[1] || \
314 (a)->addr32[0] != (b)->addr32[0])) \
315
316#define PF_AZERO(a, c) \
317 ((c == AF_INET && !(a)->addr32[0]) || \
318 (!(a)->addr32[0] && !(a)->addr32[1] && \
319 !(a)->addr32[2] && !(a)->addr32[3] )) \
320
321#define PF_MATCHA(n, a, m, b, f) \
322 pf_match_addr(n, a, m, b, f)
323
324#define PF_ACPY(a, b, f) \
325 pf_addrcpy(a, b, f)
326
327#define PF_AINC(a, f) \
328 pf_addr_inc(a, f)
329
330#define PF_POOLMASK(a, b, c, d, f) \
331 pf_poolmask(a, b, c, d, f)
332
333#else
334
335/* Just IPv6 */
336
337#ifdef PF_INET6_ONLY
338
339#define PF_AEQ(a, b, c) \
340 ((a)->addr32[3] == (b)->addr32[3] && \
341 (a)->addr32[2] == (b)->addr32[2] && \
342 (a)->addr32[1] == (b)->addr32[1] && \
343 (a)->addr32[0] == (b)->addr32[0]) \
344
345#define PF_ANEQ(a, b, c) \
346 ((a)->addr32[3] != (b)->addr32[3] || \
347 (a)->addr32[2] != (b)->addr32[2] || \
348 (a)->addr32[1] != (b)->addr32[1] || \
349 (a)->addr32[0] != (b)->addr32[0]) \
350
351#define PF_AZERO(a, c) \
352 (!(a)->addr32[0] && \
353 !(a)->addr32[1] && \
354 !(a)->addr32[2] && \
355 !(a)->addr32[3] ) \
356
357#define PF_MATCHA(n, a, m, b, f) \
358 pf_match_addr(n, a, m, b, f)
359
360#define PF_ACPY(a, b, f) \
361 pf_addrcpy(a, b, f)
362
363#define PF_AINC(a, f) \
364 pf_addr_inc(a, f)
365
366#define PF_POOLMASK(a, b, c, d, f) \
367 pf_poolmask(a, b, c, d, f)
368
369#else
370
371/* Just IPv4 */
372#ifdef PF_INET_ONLY
373
374#define PF_AEQ(a, b, c) \
375 ((a)->addr32[0] == (b)->addr32[0])
376
377#define PF_ANEQ(a, b, c) \
378 ((a)->addr32[0] != (b)->addr32[0])
379
380#define PF_AZERO(a, c) \
381 (!(a)->addr32[0])
382
383#define PF_MATCHA(n, a, m, b, f) \
384 pf_match_addr(n, a, m, b, f)
385
386#define PF_ACPY(a, b, f) \
387 (a)->v4.s_addr = (b)->v4.s_addr
388
389#define PF_AINC(a, f) \
390 do { \
391 (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
392 } while (0)
393
394#define PF_POOLMASK(a, b, c, d, f) \
395 do { \
396 (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
397 (((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
398 } while (0)
399
400#endif /* PF_INET_ONLY */
401#endif /* PF_INET6_ONLY */
402#endif /* PF_INET_INET6 */
403
404#define PF_MISMATCHAW(aw, x, af, neg, ifp) \
405 ( \
406 (((aw)->type == PF_ADDR_NOROUTE && \
407 pf_routable((x), (af), NULL)) || \
408 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \
409 pf_routable((x), (af), (ifp))) || \
410 ((aw)->type == PF_ADDR_RTLABEL && \
411 !pf_rtlabel_match((x), (af), (aw))) || \
412 ((aw)->type == PF_ADDR_TABLE && \
413 !pfr_match_addr((aw)->p.tbl, (x), (af))) || \
414 ((aw)->type == PF_ADDR_DYNIFTL && \
415 !pfi_match_addr((aw)->p.dyn, (x), (af))) || \
416 ((aw)->type == PF_ADDR_ADDRMASK && \
417 !PF_AZERO(&(aw)->v.a.mask, (af)) && \
418 !PF_MATCHA(0, &(aw)->v.a.addr, \
419 &(aw)->v.a.mask, (x), (af))))) != \
420 (neg) \
421 )
422
423
424struct pf_rule_uid {
425 uid_t uid[2];
426 u_int8_t op;
427};
428
429struct pf_rule_gid {
430 uid_t gid[2];
431 u_int8_t op;
432};
433
434struct pf_rule_addr {
435 struct pf_addr_wrap addr;
436 u_int16_t port[2];
437 u_int8_t neg;
438 u_int8_t port_op;
439};
440
441struct pf_pooladdr {
442 struct pf_addr_wrap addr;
443 TAILQ_ENTRY(pf_pooladdr) entries;
444 char ifname[IFNAMSIZ];
445 struct pfi_kif *kif;
446};
447
448TAILQ_HEAD(pf_palist, pf_pooladdr);
449
450struct pf_poolhashkey {
451 union {
452 u_int8_t key8[16];
453 u_int16_t key16[8];
454 u_int32_t key32[4];
455 } pfk; /* 128-bit hash key */
456#define key8 pfk.key8
457#define key16 pfk.key16
458#define key32 pfk.key32
459};
460
461struct pf_pool {
462 struct pf_palist list;
463 struct pf_pooladdr *cur;
464 struct pf_poolhashkey key;
465 struct pf_addr counter;
466 int tblidx;
467 u_int16_t proxy_port[2];
468 u_int8_t port_op;
469 u_int8_t opts;
470};
471
472
473/* A packed Operating System description for fingerprinting */
474typedef u_int32_t pf_osfp_t;
475#define PF_OSFP_ANY ((pf_osfp_t)0)
476#define PF_OSFP_UNKNOWN ((pf_osfp_t)-1)
477#define PF_OSFP_NOMATCH ((pf_osfp_t)-2)
478
479struct pf_osfp_entry {
480 SLIST_ENTRY(pf_osfp_entry) fp_entry;
481 pf_osfp_t fp_os;
482 int fp_enflags;
483#define PF_OSFP_EXPANDED 0x001 /* expanded entry */
484#define PF_OSFP_GENERIC 0x002 /* generic signature */
485#define PF_OSFP_NODETAIL 0x004 /* no p0f details */
486#define PF_OSFP_LEN 32
487 char fp_class_nm[PF_OSFP_LEN];
488 char fp_version_nm[PF_OSFP_LEN];
489 char fp_subtype_nm[PF_OSFP_LEN];
490};
491#define PF_OSFP_ENTRY_EQ(a, b) \
492 ((a)->fp_os == (b)->fp_os && \
493 memcmp((a)->fp_class_nm, (b)->fp_class_nm, PF_OSFP_LEN) == 0 && \
494 memcmp((a)->fp_version_nm, (b)->fp_version_nm, PF_OSFP_LEN) == 0 && \
495 memcmp((a)->fp_subtype_nm, (b)->fp_subtype_nm, PF_OSFP_LEN) == 0)
496
497/* handle pf_osfp_t packing */
498#define _FP_RESERVED_BIT 1 /* For the special negative #defines */
499#define _FP_UNUSED_BITS 1
500#define _FP_CLASS_BITS 10 /* OS Class (Windows, Linux) */
501#define _FP_VERSION_BITS 10 /* OS version (95, 98, NT, 2.4.54, 3.2) */
502#define _FP_SUBTYPE_BITS 10 /* patch level (NT SP4, SP3, ECN patch) */
503#define PF_OSFP_UNPACK(osfp, class, version, subtype) do { \
504 (class) = ((osfp) >> (_FP_VERSION_BITS+_FP_SUBTYPE_BITS)) & \
505 ((1 << _FP_CLASS_BITS) - 1); \
506 (version) = ((osfp) >> _FP_SUBTYPE_BITS) & \
507 ((1 << _FP_VERSION_BITS) - 1);\
508 (subtype) = (osfp) & ((1 << _FP_SUBTYPE_BITS) - 1); \
509} while(0)
510#define PF_OSFP_PACK(osfp, class, version, subtype) do { \
511 (osfp) = ((class) & ((1 << _FP_CLASS_BITS) - 1)) << (_FP_VERSION_BITS \
512 + _FP_SUBTYPE_BITS); \
513 (osfp) |= ((version) & ((1 << _FP_VERSION_BITS) - 1)) << \
514 _FP_SUBTYPE_BITS; \
515 (osfp) |= (subtype) & ((1 << _FP_SUBTYPE_BITS) - 1); \
516} while(0)
517
518/* the fingerprint of an OSes TCP SYN packet */
519typedef u_int64_t pf_tcpopts_t;
520struct pf_os_fingerprint {
521 SLIST_HEAD(pf_osfp_enlist, pf_osfp_entry) fp_oses; /* list of matches */
522 pf_tcpopts_t fp_tcpopts; /* packed TCP options */
523 u_int16_t fp_wsize; /* TCP window size */
524 u_int16_t fp_psize; /* ip->ip_len */
525 u_int16_t fp_mss; /* TCP MSS */
526 u_int16_t fp_flags;
527#define PF_OSFP_WSIZE_MOD 0x0001 /* Window modulus */
528#define PF_OSFP_WSIZE_DC 0x0002 /* Window don't care */
529#define PF_OSFP_WSIZE_MSS 0x0004 /* Window multiple of MSS */
530#define PF_OSFP_WSIZE_MTU 0x0008 /* Window multiple of MTU */
531#define PF_OSFP_PSIZE_MOD 0x0010 /* packet size modulus */
532#define PF_OSFP_PSIZE_DC 0x0020 /* packet size don't care */
533#define PF_OSFP_WSCALE 0x0040 /* TCP window scaling */
534#define PF_OSFP_WSCALE_MOD 0x0080 /* TCP window scale modulus */
535#define PF_OSFP_WSCALE_DC 0x0100 /* TCP window scale dont-care */
536#define PF_OSFP_MSS 0x0200 /* TCP MSS */
537#define PF_OSFP_MSS_MOD 0x0400 /* TCP MSS modulus */
538#define PF_OSFP_MSS_DC 0x0800 /* TCP MSS dont-care */
539#define PF_OSFP_DF 0x1000 /* IPv4 don't fragment bit */
540#define PF_OSFP_TS0 0x2000 /* Zero timestamp */
541#define PF_OSFP_INET6 0x4000 /* IPv6 */
542 u_int8_t fp_optcnt; /* TCP option count */
543 u_int8_t fp_wscale; /* TCP window scaling */
544 u_int8_t fp_ttl; /* IPv4 TTL */
545#define PF_OSFP_MAXTTL_OFFSET 40
546/* TCP options packing */
547#define PF_OSFP_TCPOPT_NOP 0x0 /* TCP NOP option */
548#define PF_OSFP_TCPOPT_WSCALE 0x1 /* TCP window scaling option */
549#define PF_OSFP_TCPOPT_MSS 0x2 /* TCP max segment size opt */
550#define PF_OSFP_TCPOPT_SACK 0x3 /* TCP SACK OK option */
551#define PF_OSFP_TCPOPT_TS 0x4 /* TCP timestamp option */
552#define PF_OSFP_TCPOPT_BITS 3 /* bits used by each option */
553#define PF_OSFP_MAX_OPTS \
554 (sizeof(((struct pf_os_fingerprint *)0)->fp_tcpopts) * 8) \
555 / PF_OSFP_TCPOPT_BITS
556
557 SLIST_ENTRY(pf_os_fingerprint) fp_next;
558};
559
560struct pf_osfp_ioctl {
561 struct pf_osfp_entry fp_os;
562 pf_tcpopts_t fp_tcpopts; /* packed TCP options */
563 u_int16_t fp_wsize; /* TCP window size */
564 u_int16_t fp_psize; /* ip->ip_len */
565 u_int16_t fp_mss; /* TCP MSS */
566 u_int16_t fp_flags;
567 u_int8_t fp_optcnt; /* TCP option count */
568 u_int8_t fp_wscale; /* TCP window scaling */
569 u_int8_t fp_ttl; /* IPv4 TTL */
570
571 int fp_getnum; /* DIOCOSFPGET number */
572};
573
574
575union pf_rule_ptr {
576 struct pf_rule *ptr;
577 u_int32_t nr;
578};
579
580#define PF_ANCHOR_NAME_SIZE 64
581
582struct pf_rule {
583 struct pf_rule_addr src;
584 struct pf_rule_addr dst;
585#define PF_SKIP_IFP 0
586#define PF_SKIP_DIR 1
587#define PF_SKIP_AF 2
588#define PF_SKIP_PROTO 3
589#define PF_SKIP_SRC_ADDR 4
590#define PF_SKIP_SRC_PORT 5
591#define PF_SKIP_DST_ADDR 6
592#define PF_SKIP_DST_PORT 7
593#define PF_SKIP_COUNT 8
594 union pf_rule_ptr skip[PF_SKIP_COUNT];
595#define PF_RULE_LABEL_SIZE 64
596 char label[PF_RULE_LABEL_SIZE];
597#define PF_QNAME_SIZE 64
598 char ifname[IFNAMSIZ];
599 char qname[PF_QNAME_SIZE];
600 char pqname[PF_QNAME_SIZE];
601#define PF_TAG_NAME_SIZE 64
602 char tagname[PF_TAG_NAME_SIZE];
603 char match_tagname[PF_TAG_NAME_SIZE];
604
605 char overload_tblname[PF_TABLE_NAME_SIZE];
606
607 TAILQ_ENTRY(pf_rule) entries;
608 struct pf_pool rpool;
609
610 u_int64_t evaluations;
611 u_int64_t packets[2];
612 u_int64_t bytes[2];
613
614 struct pfi_kif *kif;
615 struct pf_anchor *anchor;
616 struct pfr_ktable *overload_tbl;
617
618 pf_osfp_t os_fingerprint;
619
620 int rtableid;
621 u_int32_t timeout[PFTM_MAX];
622 u_int32_t states;
623 u_int32_t max_states;
624 u_int32_t src_nodes;
625 u_int32_t max_src_nodes;
626 u_int32_t max_src_states;
627 u_int32_t spare1; /* netgraph */
628 u_int32_t max_src_conn;
629 struct {
630 u_int32_t limit;
631 u_int32_t seconds;
632 } max_src_conn_rate;
633 u_int32_t qid;
634 u_int32_t pqid;
635 u_int32_t rt_listid;
636 u_int32_t nr;
637 u_int32_t prob;
638 uid_t cuid;
639 pid_t cpid;
640
641 u_int16_t return_icmp;
642 u_int16_t return_icmp6;
643 u_int16_t max_mss;
644 u_int16_t tag;
645 u_int16_t match_tag;
646 u_int16_t spare2; /* netgraph */
647
648 struct pf_rule_uid uid;
649 struct pf_rule_gid gid;
650
651 u_int32_t rule_flag;
652 u_int8_t action;
653 u_int8_t direction;
654 u_int8_t log;
655 u_int8_t logif;
656 u_int8_t quick;
657 u_int8_t ifnot;
658 u_int8_t match_tag_not;
659 u_int8_t natpass;
660
661#define PF_STATE_NORMAL 0x1
662#define PF_STATE_MODULATE 0x2
663#define PF_STATE_SYNPROXY 0x3
664 u_int8_t keep_state;
665 sa_family_t af;
666 u_int8_t proto;
667 u_int8_t type;
668 u_int8_t code;
669 u_int8_t flags;
670 u_int8_t flagset;
671 u_int8_t min_ttl;
672 u_int8_t allow_opts;
673 u_int8_t rt;
674 u_int8_t return_ttl;
675 u_int8_t tos;
676 u_int8_t anchor_relative;
677 u_int8_t anchor_wildcard;
678
679#define PF_FLUSH 0x01
680#define PF_FLUSH_GLOBAL 0x02
681 u_int8_t flush;
682};
683
684/* rule flags */
685#define PFRULE_DROP 0x0000
686#define PFRULE_RETURNRST 0x0001
687#define PFRULE_FRAGMENT 0x0002
688#define PFRULE_RETURNICMP 0x0004
689#define PFRULE_RETURN 0x0008
690#define PFRULE_NOSYNC 0x0010
691#define PFRULE_SRCTRACK 0x0020 /* track source states */
692#define PFRULE_RULESRCTRACK 0x0040 /* per rule */
693
694/* scrub flags */
695#define PFRULE_NODF 0x0100
696#define PFRULE_FRAGCROP 0x0200 /* non-buffering frag cache */
697#define PFRULE_FRAGDROP 0x0400 /* drop funny fragments */
698#define PFRULE_RANDOMID 0x0800
699#define PFRULE_REASSEMBLE_TCP 0x1000
700
701/* rule flags again */
702#define PFRULE_IFBOUND 0x00010000 /* if-bound */
703
704#define PFSTATE_HIWAT 10000 /* default state table size */
705#define PFSTATE_ADAPT_START 6000 /* default adaptive timeout start */
706#define PFSTATE_ADAPT_END 12000 /* default adaptive timeout end */
707
708
709struct pf_threshold {
710 u_int32_t limit;
711#define PF_THRESHOLD_MULT 1000
712#define PF_THRESHOLD_MAX 0xffffffff / PF_THRESHOLD_MULT
713 u_int32_t seconds;
714 u_int32_t count;
715 u_int32_t last;
716};
717
718struct pf_src_node {
719 RB_ENTRY(pf_src_node) entry;
720 struct pf_addr addr;
721 struct pf_addr raddr;
722 union pf_rule_ptr rule;
723 struct pfi_kif *kif;
724 u_int64_t bytes[2];
725 u_int64_t packets[2];
726 u_int32_t states;
727 u_int32_t conn;
728 struct pf_threshold conn_rate;
729 u_int32_t creation;
730 u_int32_t expire;
731 sa_family_t af;
732 u_int8_t ruletype;
733};
734
735#define PFSNODE_HIWAT 10000 /* default source node table size */
736
737struct pf_state_scrub {
738 struct timeval pfss_last; /* time received last packet */
739 u_int32_t pfss_tsecr; /* last echoed timestamp */
740 u_int32_t pfss_tsval; /* largest timestamp */
741 u_int32_t pfss_tsval0; /* original timestamp */
742 u_int16_t pfss_flags;
743#define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */
744#define PFSS_PAWS 0x0010 /* stricter PAWS checks */
745#define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */
746#define PFSS_DATA_TS 0x0040 /* timestamp on data packets */
747#define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */
748 u_int8_t pfss_ttl; /* stashed TTL */
749 u_int8_t pad;
750 u_int32_t pfss_ts_mod; /* timestamp modulation */
751};
752
753struct pf_state_host {
754 struct pf_addr addr;
755 u_int16_t port;
756 u_int16_t pad;
757};
758
759struct pf_state_peer {
760 u_int32_t seqlo; /* Max sequence number sent */
761 u_int32_t seqhi; /* Max the other end ACKd + win */
762 u_int32_t seqdiff; /* Sequence number modulator */
763 u_int16_t max_win; /* largest window (pre scaling) */
764 u_int8_t state; /* active state level */
765 u_int8_t wscale; /* window scaling factor */
766 u_int16_t mss; /* Maximum segment size option */
767 u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */
768 struct pf_state_scrub *scrub; /* state is scrubbed */
769 u_int8_t pad[3];
770};
771
772TAILQ_HEAD(pf_state_queue, pf_state);
773
774/* keep synced with struct pf_state, used in RB_FIND */
775struct pf_state_cmp {
776 u_int64_t id;
777 u_int32_t creatorid;
778 struct pf_state_host lan;
779 struct pf_state_host gwy;
780 struct pf_state_host ext;
781 sa_family_t af;
782 u_int8_t proto;
783 u_int8_t direction;
784 u_int8_t pad;
785};
786
787struct pf_state {
788 u_int64_t id;
789 u_int32_t creatorid;
790 struct pf_state_host lan;
791 struct pf_state_host gwy;
792 struct pf_state_host ext;
793 sa_family_t af;
794 u_int8_t proto;
795 u_int8_t direction;
796#ifdef __FreeBSD__
797 u_int8_t local_flags;
798#define PFSTATE_EXPIRING 0x01
799#else
800 u_int8_t pad;
801#endif
802 u_int8_t log;
803 u_int8_t allow_opts;
804 u_int8_t timeout;
805 u_int8_t sync_flags;
806#define PFSTATE_NOSYNC 0x01
807#define PFSTATE_FROMSYNC 0x02
808#define PFSTATE_STALE 0x04
809 union {
810 struct {
811 RB_ENTRY(pf_state) entry_lan_ext;
812 RB_ENTRY(pf_state) entry_ext_gwy;
813 RB_ENTRY(pf_state) entry_id;
814 TAILQ_ENTRY(pf_state) entry_list;
815 struct pfi_kif *kif;
816 } s;
817 char ifname[IFNAMSIZ];
818 } u;
819 struct pf_state_peer src;
820 struct pf_state_peer dst;
821 union pf_rule_ptr rule;
822 union pf_rule_ptr anchor;
823 union pf_rule_ptr nat_rule;
824 struct pf_addr rt_addr;
825 struct pfi_kif *rt_kif;
826 struct pf_src_node *src_node;
827 struct pf_src_node *nat_src_node;
828 u_int64_t packets[2];
829 u_int64_t bytes[2];
830 u_int32_t creation;
831 u_int32_t expire;
832 u_int32_t pfsync_time;
833 u_int16_t tag;
834};
835
836TAILQ_HEAD(pf_rulequeue, pf_rule);
837
838struct pf_anchor;
839
840struct pf_ruleset {
841 struct {
842 struct pf_rulequeue queues[2];
843 struct {
844 struct pf_rulequeue *ptr;
845 struct pf_rule **ptr_array;
846 u_int32_t rcount;
847 u_int32_t ticket;
848 int open;
849 } active, inactive;
850 } rules[PF_RULESET_MAX];
851 struct pf_anchor *anchor;
852 u_int32_t tticket;
853 int tables;
854 int topen;
855};
856
857RB_HEAD(pf_anchor_global, pf_anchor);
858RB_HEAD(pf_anchor_node, pf_anchor);
859struct pf_anchor {
860 RB_ENTRY(pf_anchor) entry_global;
861 RB_ENTRY(pf_anchor) entry_node;
862 struct pf_anchor *parent;
863 struct pf_anchor_node children;
864 char name[PF_ANCHOR_NAME_SIZE];
865 char path[MAXPATHLEN];
866 struct pf_ruleset ruleset;
867 int refcnt; /* anchor rules */
868 int match;
869};
870RB_PROTOTYPE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
871RB_PROTOTYPE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
872
873#define PF_RESERVED_ANCHOR "_pf"
874
875#define PFR_TFLAG_PERSIST 0x00000001
876#define PFR_TFLAG_CONST 0x00000002
877#define PFR_TFLAG_ACTIVE 0x00000004
878#define PFR_TFLAG_INACTIVE 0x00000008
879#define PFR_TFLAG_REFERENCED 0x00000010
880#define PFR_TFLAG_REFDANCHOR 0x00000020
881#define PFR_TFLAG_USRMASK 0x00000003
882#define PFR_TFLAG_SETMASK 0x0000003C
883#define PFR_TFLAG_ALLMASK 0x0000003F
884
885struct pfr_table {
886 char pfrt_anchor[MAXPATHLEN];
887 char pfrt_name[PF_TABLE_NAME_SIZE];
888 u_int32_t pfrt_flags;
889 u_int8_t pfrt_fback;
890};
891
892enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
893 PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
894 PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_MAX };
895
896struct pfr_addr {
897 union {
898 struct in_addr _pfra_ip4addr;
899 struct in6_addr _pfra_ip6addr;
900 } pfra_u;
901 u_int8_t pfra_af;
902 u_int8_t pfra_net;
903 u_int8_t pfra_not;
904 u_int8_t pfra_fback;
905};
906#define pfra_ip4addr pfra_u._pfra_ip4addr
907#define pfra_ip6addr pfra_u._pfra_ip6addr
908
909enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
910enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
911#define PFR_OP_XPASS PFR_OP_ADDR_MAX
912
913struct pfr_astats {
914 struct pfr_addr pfras_a;
915 u_int64_t pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
916 u_int64_t pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
917 long pfras_tzero;
918};
919
920enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
921
922struct pfr_tstats {
923 struct pfr_table pfrts_t;
924 u_int64_t pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
925 u_int64_t pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
926 u_int64_t pfrts_match;
927 u_int64_t pfrts_nomatch;
928 long pfrts_tzero;
929 int pfrts_cnt;
930 int pfrts_refcnt[PFR_REFCNT_MAX];
931};
932#define pfrts_name pfrts_t.pfrt_name
933#define pfrts_flags pfrts_t.pfrt_flags
934
935#ifndef _SOCKADDR_UNION_DEFINED
936#define _SOCKADDR_UNION_DEFINED
937union sockaddr_union {
938 struct sockaddr sa;
939 struct sockaddr_in sin;
940 struct sockaddr_in6 sin6;
941};
942#endif /* _SOCKADDR_UNION_DEFINED */
943
944SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
945struct pfr_kentry {
946 struct radix_node pfrke_node[2];
947 union sockaddr_union pfrke_sa;
948 u_int64_t pfrke_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
949 u_int64_t pfrke_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
950 SLIST_ENTRY(pfr_kentry) pfrke_workq;
951 long pfrke_tzero;
952 u_int8_t pfrke_af;
953 u_int8_t pfrke_net;
954 u_int8_t pfrke_not;
955 u_int8_t pfrke_mark;
956 u_int8_t pfrke_intrpool;
957};
958
959SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
960RB_HEAD(pfr_ktablehead, pfr_ktable);
961struct pfr_ktable {
962 struct pfr_tstats pfrkt_ts;
963 RB_ENTRY(pfr_ktable) pfrkt_tree;
964 SLIST_ENTRY(pfr_ktable) pfrkt_workq;
965 struct radix_node_head *pfrkt_ip4;
966 struct radix_node_head *pfrkt_ip6;
967 struct pfr_ktable *pfrkt_shadow;
968 struct pfr_ktable *pfrkt_root;
969 struct pf_ruleset *pfrkt_rs;
970 long pfrkt_larg;
971 int pfrkt_nflags;
972};
973#define pfrkt_t pfrkt_ts.pfrts_t
974#define pfrkt_name pfrkt_t.pfrt_name
975#define pfrkt_anchor pfrkt_t.pfrt_anchor
976#define pfrkt_ruleset pfrkt_t.pfrt_ruleset
977#define pfrkt_flags pfrkt_t.pfrt_flags
978#define pfrkt_cnt pfrkt_ts.pfrts_cnt
979#define pfrkt_refcnt pfrkt_ts.pfrts_refcnt
980#define pfrkt_packets pfrkt_ts.pfrts_packets
981#define pfrkt_bytes pfrkt_ts.pfrts_bytes
982#define pfrkt_match pfrkt_ts.pfrts_match
983#define pfrkt_nomatch pfrkt_ts.pfrts_nomatch
984#define pfrkt_tzero pfrkt_ts.pfrts_tzero
985
986RB_HEAD(pf_state_tree_lan_ext, pf_state);
987RB_PROTOTYPE(pf_state_tree_lan_ext, pf_state,
988 u.s.entry_lan_ext, pf_state_compare_lan_ext);
989
990RB_HEAD(pf_state_tree_ext_gwy, pf_state);
991RB_PROTOTYPE(pf_state_tree_ext_gwy, pf_state,
992 u.s.entry_ext_gwy, pf_state_compare_ext_gwy);
993
994TAILQ_HEAD(pfi_statehead, pfi_kif);
995RB_HEAD(pfi_ifhead, pfi_kif);
996
997/* keep synced with pfi_kif, used in RB_FIND */
998struct pfi_kif_cmp {
999 char pfik_name[IFNAMSIZ];
1000};
1001
1002struct pfi_kif {
1003 char pfik_name[IFNAMSIZ];
1004 RB_ENTRY(pfi_kif) pfik_tree;
1005 u_int64_t pfik_packets[2][2][2];
1006 u_int64_t pfik_bytes[2][2][2];
1007 u_int32_t pfik_tzero;
1008 int pfik_flags;
1009 struct pf_state_tree_lan_ext pfik_lan_ext;
1010 struct pf_state_tree_ext_gwy pfik_ext_gwy;
1011 TAILQ_ENTRY(pfi_kif) pfik_w_states;
1012#ifndef __FreeBSD__
1013 void *pfik_ah_cookie;
1014#endif
1015 struct ifnet *pfik_ifp;
1016 struct ifg_group *pfik_group;
1017 int pfik_states;
1018 int pfik_rules;
1019 TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs;
1020};
1021
1022enum pfi_kif_refs {
1023 PFI_KIF_REF_NONE,
1024 PFI_KIF_REF_STATE,
1025 PFI_KIF_REF_RULE
1026};
1027
1028#define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */
1029/* XXX: revisist */
1030#define PFI_IFLAG_SETABLE_MASK 0x0100 /* setable via DIOC{SET,CLR}IFFLAG */
1031#define PFI_IFLAG_PLACEHOLDER 0x8000 /* placeholder group/interface */
1032
1033struct pf_pdesc {
1034 struct {
1035 int done;
1036 uid_t uid;
1037 gid_t gid;
1038 pid_t pid;
1039 } lookup;
1040 u_int64_t tot_len; /* Make Mickey money */
1041 union {
1042 struct tcphdr *tcp;
1043 struct udphdr *udp;
1044 struct icmp *icmp;
1045#ifdef INET6
1046 struct icmp6_hdr *icmp6;
1047#endif /* INET6 */
1048 void *any;
1049 } hdr;
1050 struct pf_addr baddr; /* address before translation */
1051 struct pf_addr naddr; /* address after translation */
1052 struct pf_rule *nat_rule; /* nat/rdr rule applied to packet */
1053 struct pf_addr *src;
1054 struct pf_addr *dst;
1055 struct ether_header
1056 *eh;
1057 struct pf_mtag *pf_mtag;
1058 u_int16_t *ip_sum;
1059 u_int32_t p_len; /* total length of payload */
1060 u_int16_t flags; /* Let SCRUB trigger behavior in
1061 * state code. Easier than tags */
1062#define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */
1063#define PFDESC_IP_REAS 0x0002 /* IP frags would've been reassembled */
1064 sa_family_t af;
1065 u_int8_t proto;
1066 u_int8_t tos;
1067};
1068
1069/* flags for RDR options */
1070#define PF_DPORT_RANGE 0x01 /* Dest port uses range */
1071#define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */
1072
1073/* Reasons code for passing/dropping a packet */
1074#define PFRES_MATCH 0 /* Explicit match of a rule */
1075#define PFRES_BADOFF 1 /* Bad offset for pull_hdr */
1076#define PFRES_FRAG 2 /* Dropping following fragment */
1077#define PFRES_SHORT 3 /* Dropping short packet */
1078#define PFRES_NORM 4 /* Dropping by normalizer */
1079#define PFRES_MEMORY 5 /* Dropped due to lacking mem */
1080#define PFRES_TS 6 /* Bad TCP Timestamp (RFC1323) */
1081#define PFRES_CONGEST 7 /* Congestion (of ipintrq) */
1082#define PFRES_IPOPTIONS 8 /* IP option */
1083#define PFRES_PROTCKSUM 9 /* Protocol checksum invalid */
1084#define PFRES_BADSTATE 10 /* State mismatch */
1085#define PFRES_STATEINS 11 /* State insertion failure */
1086#define PFRES_MAXSTATES 12 /* State limit */
1087#define PFRES_SRCLIMIT 13 /* Source node/conn limit */
1088#define PFRES_SYNPROXY 14 /* SYN proxy */
1089#define PFRES_MAX 15 /* total+1 */
1090
1091#define PFRES_NAMES { \
1092 "match", \
1093 "bad-offset", \
1094 "fragment", \
1095 "short", \
1096 "normalize", \
1097 "memory", \
1098 "bad-timestamp", \
1099 "congestion", \
1100 "ip-option", \
1101 "proto-cksum", \
1102 "state-mismatch", \
1103 "state-insert", \
1104 "state-limit", \
1105 "src-limit", \
1106 "synproxy", \
1107 NULL \
1108}
1109
1110/* Counters for other things we want to keep track of */
1111#define LCNT_STATES 0 /* states */
1112#define LCNT_SRCSTATES 1 /* max-src-states */
1113#define LCNT_SRCNODES 2 /* max-src-nodes */
1114#define LCNT_SRCCONN 3 /* max-src-conn */
1115#define LCNT_SRCCONNRATE 4 /* max-src-conn-rate */
1116#define LCNT_OVERLOAD_TABLE 5 /* entry added to overload table */
1117#define LCNT_OVERLOAD_FLUSH 6 /* state entries flushed */
1118#define LCNT_MAX 7 /* total+1 */
1119
1120#define LCNT_NAMES { \
1121 "max states per rule", \
1122 "max-src-states", \
1123 "max-src-nodes", \
1124 "max-src-conn", \
1125 "max-src-conn-rate", \
1126 "overload table insertion", \
1127 "overload flush states", \
1128 NULL \
1129}
1130
1131/* UDP state enumeration */
1132#define PFUDPS_NO_TRAFFIC 0
1133#define PFUDPS_SINGLE 1
1134#define PFUDPS_MULTIPLE 2
1135
1136#define PFUDPS_NSTATES 3 /* number of state levels */
1137
1138#define PFUDPS_NAMES { \
1139 "NO_TRAFFIC", \
1140 "SINGLE", \
1141 "MULTIPLE", \
1142 NULL \
1143}
1144
1145/* Other protocol state enumeration */
1146#define PFOTHERS_NO_TRAFFIC 0
1147#define PFOTHERS_SINGLE 1
1148#define PFOTHERS_MULTIPLE 2
1149
1150#define PFOTHERS_NSTATES 3 /* number of state levels */
1151
1152#define PFOTHERS_NAMES { \
1153 "NO_TRAFFIC", \
1154 "SINGLE", \
1155 "MULTIPLE", \
1156 NULL \
1157}
1158
1159#define FCNT_STATE_SEARCH 0
1160#define FCNT_STATE_INSERT 1
1161#define FCNT_STATE_REMOVALS 2
1162#define FCNT_MAX 3
1163
1164#define SCNT_SRC_NODE_SEARCH 0
1165#define SCNT_SRC_NODE_INSERT 1
1166#define SCNT_SRC_NODE_REMOVALS 2
1167#define SCNT_MAX 3
1168
1169#define ACTION_SET(a, x) \
1170 do { \
1171 if ((a) != NULL) \
1172 *(a) = (x); \
1173 } while (0)
1174
1175#define REASON_SET(a, x) \
1176 do { \
1177 if ((a) != NULL) \
1178 *(a) = (x); \
1179 if (x < PFRES_MAX) \
1180 pf_status.counters[x]++; \
1181 } while (0)
1182
1183struct pf_status {
1184 u_int64_t counters[PFRES_MAX];
1185 u_int64_t lcounters[LCNT_MAX]; /* limit counters */
1186 u_int64_t fcounters[FCNT_MAX];
1187 u_int64_t scounters[SCNT_MAX];
1188 u_int64_t pcounters[2][2][3];
1189 u_int64_t bcounters[2][2];
1190 u_int64_t stateid;
1191 u_int32_t running;
1192 u_int32_t states;
1193 u_int32_t src_nodes;
1194 u_int32_t since;
1195 u_int32_t debug;
1196 u_int32_t hostid;
1197 char ifname[IFNAMSIZ];
1198 u_int8_t pf_chksum[PF_MD5_DIGEST_LENGTH];
1199};
1200
1201struct cbq_opts {
1202 u_int minburst;
1203 u_int maxburst;
1204 u_int pktsize;
1205 u_int maxpktsize;
1206 u_int ns_per_byte;
1207 u_int maxidle;
1208 int minidle;
1209 u_int offtime;
1210 int flags;
1211};
1212
1213struct priq_opts {
1214 int flags;
1215};
1216
1217struct hfsc_opts {
1218 /* real-time service curve */
1219 u_int rtsc_m1; /* slope of the 1st segment in bps */
1220 u_int rtsc_d; /* the x-projection of m1 in msec */
1221 u_int rtsc_m2; /* slope of the 2nd segment in bps */
1222 /* link-sharing service curve */
1223 u_int lssc_m1;
1224 u_int lssc_d;
1225 u_int lssc_m2;
1226 /* upper-limit service curve */
1227 u_int ulsc_m1;
1228 u_int ulsc_d;
1229 u_int ulsc_m2;
1230 int flags;
1231};
1232
1233struct pf_altq {
1234 char ifname[IFNAMSIZ];
1235
1236 void *altq_disc; /* discipline-specific state */
1237 TAILQ_ENTRY(pf_altq) entries;
1238
1239 /* scheduler spec */
1240 u_int8_t scheduler; /* scheduler type */
1241 u_int16_t tbrsize; /* tokenbucket regulator size */
1242 u_int32_t ifbandwidth; /* interface bandwidth */
1243
1244 /* queue spec */
1245 char qname[PF_QNAME_SIZE]; /* queue name */
1246 char parent[PF_QNAME_SIZE]; /* parent name */
1247 u_int32_t parent_qid; /* parent queue id */
1248 u_int32_t bandwidth; /* queue bandwidth */
1249 u_int8_t priority; /* priority */
1250#ifdef __FreeBSD__
1251 u_int8_t local_flags; /* dynamic interface */
1252#define PFALTQ_FLAG_IF_REMOVED 0x01
1253#endif
1254 u_int16_t qlimit; /* queue size limit */
1255 u_int16_t flags; /* misc flags */
1256 union {
1257 struct cbq_opts cbq_opts;
1258 struct priq_opts priq_opts;
1259 struct hfsc_opts hfsc_opts;
1260 } pq_u;
1261
1262 u_int32_t qid; /* return value */
1263};
1264
1265#ifndef __FreeBSD__
1266
1267#define PF_TAG_GENERATED 0x01
1268#define PF_TAG_FRAGCACHE 0x02
1269#define PF_TAG_TRANSLATE_LOCALHOST 0x04
1270
1271struct pf_mtag {
1272 void *hdr; /* saved hdr pos in mbuf, for ECN */
1273 u_int rtableid; /* alternate routing table id */
1274 u_int32_t qid; /* queue id */
1275 u_int16_t tag; /* tag id */
1276 u_int8_t flags;
1277 u_int8_t routed;
1278 sa_family_t af; /* for ECN */
1279};
1280#endif
1281
1282struct pf_tag {
1283 u_int16_t tag; /* tag id */
1284};
1285
1286struct pf_tagname {
1287 TAILQ_ENTRY(pf_tagname) entries;
1288 char name[PF_TAG_NAME_SIZE];
1289 u_int16_t tag;
1290 int ref;
1291};
1292
1293#define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */
1294#define PFFRAG_FRAG_HIWAT 1000 /* Number of fragmented packets */
1295#define PFFRAG_FRCENT_HIWAT 50000 /* Number of fragment cache entries */
1296#define PFFRAG_FRCACHE_HIWAT 10000 /* Number of fragment descriptors */
1297
1298#define PFR_KTABLE_HIWAT 1000 /* Number of tables */
1299#define PFR_KENTRY_HIWAT 200000 /* Number of table entries */
1300#define PFR_KENTRY_HIWAT_SMALL 100000 /* Number of table entries (tiny hosts) */
1301
1302/*
1303 * ioctl parameter structures
1304 */
1305
1306struct pfioc_pooladdr {
1307 u_int32_t action;
1308 u_int32_t ticket;
1309 u_int32_t nr;
1310 u_int32_t r_num;
1311 u_int8_t r_action;
1312 u_int8_t r_last;
1313 u_int8_t af;
1314 char anchor[MAXPATHLEN];
1315 struct pf_pooladdr addr;
1316};
1317
1318struct pfioc_rule {
1319 u_int32_t action;
1320 u_int32_t ticket;
1321 u_int32_t pool_ticket;
1322 u_int32_t nr;
1323 char anchor[MAXPATHLEN];
1324 char anchor_call[MAXPATHLEN];
1325 struct pf_rule rule;
1326};
1327
1328struct pfioc_natlook {
1329 struct pf_addr saddr;
1330 struct pf_addr daddr;
1331 struct pf_addr rsaddr;
1332 struct pf_addr rdaddr;
1333 u_int16_t sport;
1334 u_int16_t dport;
1335 u_int16_t rsport;
1336 u_int16_t rdport;
1337 sa_family_t af;
1338 u_int8_t proto;
1339 u_int8_t direction;
1340};
1341
1342struct pfioc_state {
1343 u_int32_t nr;
1344 struct pf_state state;
1345};
1346
1347struct pfioc_src_node_kill {
1348 /* XXX returns the number of src nodes killed in psnk_af */
1349 sa_family_t psnk_af;
1350 struct pf_rule_addr psnk_src;
1351 struct pf_rule_addr psnk_dst;
1352};
1353
1354struct pfioc_state_kill {
1355 /* XXX returns the number of states killed in psk_af */
1356 sa_family_t psk_af;
1357 int psk_proto;
1358 struct pf_rule_addr psk_src;
1359 struct pf_rule_addr psk_dst;
1360 char psk_ifname[IFNAMSIZ];
1361};
1362
1363struct pfioc_states {
1364 int ps_len;
1365 union {
1366 caddr_t psu_buf;
1367 struct pf_state *psu_states;
1368 } ps_u;
1369#define ps_buf ps_u.psu_buf
1370#define ps_states ps_u.psu_states
1371};
1372
1373struct pfioc_src_nodes {
1374 int psn_len;
1375 union {
1376 caddr_t psu_buf;
1377 struct pf_src_node *psu_src_nodes;
1378 } psn_u;
1379#define psn_buf psn_u.psu_buf
1380#define psn_src_nodes psn_u.psu_src_nodes
1381};
1382
1383struct pfioc_if {
1384 char ifname[IFNAMSIZ];
1385};
1386
1387struct pfioc_tm {
1388 int timeout;
1389 int seconds;
1390};
1391
1392struct pfioc_limit {
1393 int index;
1394 unsigned limit;
1395};
1396
1397struct pfioc_altq {
1398 u_int32_t action;
1399 u_int32_t ticket;
1400 u_int32_t nr;
1401 struct pf_altq altq;
1402};
1403
1404struct pfioc_qstats {
1405 u_int32_t ticket;
1406 u_int32_t nr;
1407 void *buf;
1408 int nbytes;
1409 u_int8_t scheduler;
1410};
1411
1412struct pfioc_ruleset {
1413 u_int32_t nr;
1414 char path[MAXPATHLEN];
1415 char name[PF_ANCHOR_NAME_SIZE];
1416};
1417
1418#define PF_RULESET_ALTQ (PF_RULESET_MAX)
1419#define PF_RULESET_TABLE (PF_RULESET_MAX+1)
1420struct pfioc_trans {
1421 int size; /* number of elements */
1422 int esize; /* size of each element in bytes */
1423 struct pfioc_trans_e {
1424 int rs_num;
1425 char anchor[MAXPATHLEN];
1426 u_int32_t ticket;
1427 } *array;
1428};
1429
1430#define PFR_FLAG_ATOMIC 0x00000001
1431#define PFR_FLAG_DUMMY 0x00000002
1432#define PFR_FLAG_FEEDBACK 0x00000004
1433#define PFR_FLAG_CLSTATS 0x00000008
1434#define PFR_FLAG_ADDRSTOO 0x00000010
1435#define PFR_FLAG_REPLACE 0x00000020
1436#define PFR_FLAG_ALLRSETS 0x00000040
1437#define PFR_FLAG_ALLMASK 0x0000007F
1438#ifdef _KERNEL
1439#define PFR_FLAG_USERIOCTL 0x10000000
1440#endif
1441
1442struct pfioc_table {
1443 struct pfr_table pfrio_table;
1444 void *pfrio_buffer;
1445 int pfrio_esize;
1446 int pfrio_size;
1447 int pfrio_size2;
1448 int pfrio_nadd;
1449 int pfrio_ndel;
1450 int pfrio_nchange;
1451 int pfrio_flags;
1452 u_int32_t pfrio_ticket;
1453};
1454#define pfrio_exists pfrio_nadd
1455#define pfrio_nzero pfrio_nadd
1456#define pfrio_nmatch pfrio_nadd
1457#define pfrio_naddr pfrio_size2
1458#define pfrio_setflag pfrio_size2
1459#define pfrio_clrflag pfrio_nadd
1460
1461struct pfioc_iface {
1462 char pfiio_name[IFNAMSIZ];
1463 void *pfiio_buffer;
1464 int pfiio_esize;
1465 int pfiio_size;
1466 int pfiio_nzero;
1467 int pfiio_flags;
1468};
1469
1470
1471/*
1472 * ioctl operations
1473 */
1474
1475#define DIOCSTART _IO ('D', 1)
1476#define DIOCSTOP _IO ('D', 2)
1477#define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule)
1478#define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule)
1479#define DIOCGETRULE _IOWR('D', 7, struct pfioc_rule)
1480/* XXX cut 8 - 17 */
1481#define DIOCCLRSTATES _IOWR('D', 18, struct pfioc_state_kill)
1482#define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state)
1483#define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
1484#define DIOCGETSTATUS _IOWR('D', 21, struct pf_status)
1485#define DIOCCLRSTATUS _IO ('D', 22)
1486#define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook)
1487#define DIOCSETDEBUG _IOWR('D', 24, u_int32_t)
1488#define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states)
1489#define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule)
1490/* XXX cut 26 - 28 */
1491#define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm)
1492#define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm)
1493#define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state)
1494#define DIOCCLRRULECTRS _IO ('D', 38)
1495#define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit)
1496#define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit)
1497#define DIOCKILLSTATES _IOWR('D', 41, struct pfioc_state_kill)
1498#define DIOCSTARTALTQ _IO ('D', 42)
1499#define DIOCSTOPALTQ _IO ('D', 43)
1500#define DIOCADDALTQ _IOWR('D', 45, struct pfioc_altq)
1501#define DIOCGETALTQS _IOWR('D', 47, struct pfioc_altq)
1502#define DIOCGETALTQ _IOWR('D', 48, struct pfioc_altq)
1503#define DIOCCHANGEALTQ _IOWR('D', 49, struct pfioc_altq)
1504#define DIOCGETQSTATS _IOWR('D', 50, struct pfioc_qstats)
1505#define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr)
1506#define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr)
1507#define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr)
1508#define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr)
1509#define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr)
1510/* XXX cut 55 - 57 */
1511#define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset)
1512#define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset)
1513#define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table)
1514#define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table)
1515#define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table)
1516#define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table)
1517#define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table)
1518#define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table)
1519#define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table)
1520#define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table)
1521#define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table)
1522#define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table)
1523#define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table)
1524#define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table)
1525#define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table)
1526#define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table)
1527#define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table)
1528#define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table)
1529#define DIOCOSFPFLUSH _IO('D', 78)
1530#define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl)
1531#define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl)
1532#define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans)
1533#define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans)
1534#define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans)
1535#define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes)
1536#define DIOCCLRSRCNODES _IO('D', 85)
1537#define DIOCSETHOSTID _IOWR('D', 86, u_int32_t)
1538#define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface)
1539#define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface)
1540#define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface)
1541#define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill)
1542#ifdef __FreeBSD__
1543struct pf_ifspeed {
1544 char ifname[IFNAMSIZ];
1545 u_int32_t baudrate;
1546};
1547#define DIOCGIFSPEED _IOWR('D', 92, struct pf_ifspeed)
1548#endif
1549
1550#ifdef _KERNEL
1551RB_HEAD(pf_src_tree, pf_src_node);
1552RB_PROTOTYPE(pf_src_tree, pf_src_node, entry, pf_src_compare);
1553extern struct pf_src_tree tree_src_tracking;
1554
1555RB_HEAD(pf_state_tree_id, pf_state);
1556RB_PROTOTYPE(pf_state_tree_id, pf_state,
1557 entry_id, pf_state_compare_id);
1558extern struct pf_state_tree_id tree_id;
1559extern struct pf_state_queue state_list;
1560
1561TAILQ_HEAD(pf_poolqueue, pf_pool);
1562extern struct pf_poolqueue pf_pools[2];
1563TAILQ_HEAD(pf_altqqueue, pf_altq);
1564extern struct pf_altqqueue pf_altqs[2];
1565extern struct pf_palist pf_pabuf;
1566
1567extern u_int32_t ticket_altqs_active;
1568extern u_int32_t ticket_altqs_inactive;
1569extern int altqs_inactive_open;
1570extern u_int32_t ticket_pabuf;
1571extern struct pf_altqqueue *pf_altqs_active;
1572extern struct pf_altqqueue *pf_altqs_inactive;
1573extern struct pf_poolqueue *pf_pools_active;
1574extern struct pf_poolqueue *pf_pools_inactive;
1575extern int pf_tbladdr_setup(struct pf_ruleset *,
1576 struct pf_addr_wrap *);
1577extern void pf_tbladdr_remove(struct pf_addr_wrap *);
1578extern void pf_tbladdr_copyout(struct pf_addr_wrap *);
1579extern void pf_calc_skip_steps(struct pf_rulequeue *);
1580#ifdef __FreeBSD__
1581#ifdef ALTQ
1582extern void pf_altq_ifnet_event(struct ifnet *, int);
1583#endif
1584extern uma_zone_t pf_src_tree_pl, pf_rule_pl;
1585extern uma_zone_t pf_state_pl, pf_altq_pl, pf_pooladdr_pl;
1586extern uma_zone_t pfr_ktable_pl, pfr_kentry_pl, pfr_kentry_pl2;
1587extern uma_zone_t pf_cache_pl, pf_cent_pl;
1588extern uma_zone_t pf_state_scrub_pl;
1589extern uma_zone_t pfi_addr_pl;
1590#else
1591extern struct pool pf_src_tree_pl, pf_rule_pl;
1592extern struct pool pf_state_pl, pf_altq_pl, pf_pooladdr_pl;
1593extern struct pool pf_state_scrub_pl;
1594#endif
1595extern void pf_purge_thread(void *);
1596#ifdef __FreeBSD__
1597extern int pf_purge_expired_src_nodes(int);
1598extern int pf_purge_expired_states(u_int32_t, int);
1599#else
1600extern void pf_purge_expired_src_nodes(int);
1601extern void pf_purge_expired_states(u_int32_t);
1602#endif
1603extern void pf_unlink_state(struct pf_state *);
1604extern void pf_free_state(struct pf_state *);
1605extern int pf_insert_state(struct pfi_kif *,
1606 struct pf_state *);
1607extern int pf_insert_src_node(struct pf_src_node **,
1608 struct pf_rule *, struct pf_addr *,
1609 sa_family_t);
1610void pf_src_tree_remove_state(struct pf_state *);
1611extern struct pf_state *pf_find_state_byid(struct pf_state_cmp *);
1612extern struct pf_state *pf_find_state_all(struct pf_state_cmp *key,
1613 u_int8_t tree, int *more);
1614extern void pf_print_state(struct pf_state *);
1615extern void pf_print_flags(u_int8_t);
1616extern u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
1617 u_int8_t);
1618
1619extern struct ifnet *sync_ifp;
1620extern struct pf_rule pf_default_rule;
1621extern void pf_addrcpy(struct pf_addr *, struct pf_addr *,
1622 u_int8_t);
1623void pf_rm_rule(struct pf_rulequeue *,
1624 struct pf_rule *);
1625
1626#ifdef INET
1627#ifdef __FreeBSD__
1628int pf_test(int, struct ifnet *, struct mbuf **, struct ether_header *,
1629 struct inpcb *);
1630#else
1631int pf_test(int, struct ifnet *, struct mbuf **, struct ether_header *);
1632#endif
1633#endif /* INET */
1634
1635#ifdef INET6
1636#ifdef __FreeBSD__
1637int pf_test6(int, struct ifnet *, struct mbuf **, struct ether_header *,
1638 struct inpcb *);
1639#else
1640int pf_test6(int, struct ifnet *, struct mbuf **, struct ether_header *);
1641#endif
1642void pf_poolmask(struct pf_addr *, struct pf_addr*,
1643 struct pf_addr *, struct pf_addr *, u_int8_t);
1644void pf_addr_inc(struct pf_addr *, sa_family_t);
1645#endif /* INET6 */
1646
1647#ifdef __FreeBSD__
1648u_int32_t pf_new_isn(struct pf_state *);
1649#endif
1650void *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
1651 sa_family_t);
1652void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
1653int pflog_packet(struct pfi_kif *, struct mbuf *, sa_family_t, u_int8_t,
1654 u_int8_t, struct pf_rule *, struct pf_rule *, struct pf_ruleset *,
1655 struct pf_pdesc *);
1656int pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
1657 struct pf_addr *, sa_family_t);
1658int pf_match(u_int8_t, u_int32_t, u_int32_t, u_int32_t);
1659int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
1660int pf_match_uid(u_int8_t, uid_t, uid_t, uid_t);
1661int pf_match_gid(u_int8_t, gid_t, gid_t, gid_t);
1662
1663void pf_normalize_init(void);
1664int pf_normalize_ip(struct mbuf **, int, struct pfi_kif *, u_short *,
1665 struct pf_pdesc *);
1666int pf_normalize_ip6(struct mbuf **, int, struct pfi_kif *, u_short *,
1667 struct pf_pdesc *);
1668int pf_normalize_tcp(int, struct pfi_kif *, struct mbuf *, int, int, void *,
1669 struct pf_pdesc *);
1670void pf_normalize_tcp_cleanup(struct pf_state *);
1671int pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
1672 struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
1673int pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
1674 u_short *, struct tcphdr *, struct pf_state *,
1675 struct pf_state_peer *, struct pf_state_peer *, int *);
1676u_int32_t
1677 pf_state_expires(const struct pf_state *);
1678void pf_purge_expired_fragments(void);
1679int pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *);
1680int pf_rtlabel_match(struct pf_addr *, sa_family_t, struct pf_addr_wrap *);
1681#ifdef __FreeBSD__
1682int pf_socket_lookup(int, struct pf_pdesc *, struct inpcb *);
1683#else
1684int pf_socket_lookup(int, struct pf_pdesc *);
1685#endif
1686void pfr_initialize(void);
1687int pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
1688void pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
1689 u_int64_t, int, int, int);
1690int pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *,
1691 struct pf_addr **, struct pf_addr **, sa_family_t);
1692void pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
1693struct pfr_ktable *
1694 pfr_attach_table(struct pf_ruleset *, char *);
1695void pfr_detach_table(struct pfr_ktable *);
1696int pfr_clr_tables(struct pfr_table *, int *, int);
1697int pfr_add_tables(struct pfr_table *, int, int *, int);
1698int pfr_del_tables(struct pfr_table *, int, int *, int);
1699int pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
1700int pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
1701int pfr_clr_tstats(struct pfr_table *, int, int *, int);
1702int pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
1703int pfr_clr_addrs(struct pfr_table *, int *, int);
1704int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
1705int pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1706 int);
1707int pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1708 int);
1709int pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1710 int *, int *, int *, int, u_int32_t);
1711int pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
1712int pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
1713int pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
1714 int);
1715int pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1716 int);
1717int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
1718int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
1719int pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
1720int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
1721 int *, u_int32_t, int);
1722
1723extern struct pfi_statehead pfi_statehead;
1724extern struct pfi_kif *pfi_all;
1725
1726void pfi_initialize(void);
1727#ifdef __FreeBSD__
1728void pfi_cleanup(void);
1729#endif
1730struct pfi_kif *pfi_kif_get(const char *);
1731void pfi_kif_ref(struct pfi_kif *, enum pfi_kif_refs);
1732void pfi_kif_unref(struct pfi_kif *, enum pfi_kif_refs);
1733int pfi_kif_match(struct pfi_kif *, struct pfi_kif *);
1734void pfi_attach_ifnet(struct ifnet *);
1735void pfi_detach_ifnet(struct ifnet *);
1736void pfi_attach_ifgroup(struct ifg_group *);
1737void pfi_detach_ifgroup(struct ifg_group *);
1738void pfi_group_change(const char *);
1739int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
1740 sa_family_t);
1741int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
1742void pfi_dynaddr_remove(struct pf_addr_wrap *);
1743void pfi_dynaddr_copyout(struct pf_addr_wrap *);
1744void pfi_fill_oldstatus(struct pf_status *);
1745int pfi_clr_istats(const char *);
1746int pfi_get_ifaces(const char *, struct pfi_kif *, int *);
1747int pfi_set_flags(const char *, int);
1748int pfi_clear_flags(const char *, int);
1749
1750u_int16_t pf_tagname2tag(char *);
1751void pf_tag2tagname(u_int16_t, char *);
1752void pf_tag_ref(u_int16_t);
1753void pf_tag_unref(u_int16_t);
1754int pf_tag_packet(struct mbuf *, struct pf_mtag *, int, int);
1755u_int32_t pf_qname2qid(char *);
1756void pf_qid2qname(u_int32_t, char *);
1757void pf_qid_unref(u_int32_t);
1758#ifndef __FreeBSD__
1759struct pf_mtag *pf_find_mtag(struct mbuf *);
1760struct pf_mtag *pf_get_mtag(struct mbuf *);
1761#endif
1762
1763extern struct pf_status pf_status;
1764
1765#ifdef __FreeBSD__
1766extern uma_zone_t pf_frent_pl, pf_frag_pl;
1767extern struct sx pf_consistency_lock;
1768#else
1769extern struct pool pf_frent_pl, pf_frag_pl;
1770extern struct rwlock pf_consistency_lock;
1771#endif
1772
1773struct pf_pool_limit {
1774 void *pp;
1775 unsigned limit;
1776};
1777extern struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX];
1778
1779#ifdef __FreeBSD__
1780struct pf_frent {
1781 LIST_ENTRY(pf_frent) fr_next;
1782 struct ip *fr_ip;
1783 struct mbuf *fr_m;
1784};
1785
1786struct pf_frcache {
1787 LIST_ENTRY(pf_frcache) fr_next;
1788 uint16_t fr_off;
1789 uint16_t fr_end;
1790};
1791
1792struct pf_fragment {
1793 RB_ENTRY(pf_fragment) fr_entry;
1794 TAILQ_ENTRY(pf_fragment) frag_next;
1795 struct in_addr fr_src;
1796 struct in_addr fr_dst;
1797 u_int8_t fr_p; /* protocol of this fragment */
1798 u_int8_t fr_flags; /* status flags */
1799 u_int16_t fr_id; /* fragment id for reassemble */
1800 u_int16_t fr_max; /* fragment data max */
1801 u_int32_t fr_timeout;
1802#define fr_queue fr_u.fru_queue
1803#define fr_cache fr_u.fru_cache
1804 union {
1805 LIST_HEAD(pf_fragq, pf_frent) fru_queue; /* buffering */
1806 LIST_HEAD(pf_cacheq, pf_frcache) fru_cache; /* non-buf */
1807 } fr_u;
1808};
1809#endif /* (__FreeBSD__) */
1810
1811#endif /* _KERNEL */
1812
1813extern struct pf_anchor_global pf_anchors;
1814extern struct pf_anchor pf_main_anchor;
1815#define pf_main_ruleset pf_main_anchor.ruleset
1816
1817/* these ruleset functions can be linked into userland programs (pfctl) */
1818int pf_get_ruleset_number(u_int8_t);
1819void pf_init_ruleset(struct pf_ruleset *);
1820int pf_anchor_setup(struct pf_rule *,
1821 const struct pf_ruleset *, const char *);
1822int pf_anchor_copyout(const struct pf_ruleset *,
1823 const struct pf_rule *, struct pfioc_rule *);
1824void pf_anchor_remove(struct pf_rule *);
1825void pf_remove_if_empty_ruleset(struct pf_ruleset *);
1826struct pf_anchor *pf_find_anchor(const char *);
1827struct pf_ruleset *pf_find_ruleset(const char *);
1828struct pf_ruleset *pf_find_or_create_ruleset(const char *);
1829void pf_rs_initialize(void);
1830
1831#ifndef __FreeBSD__
1832/* ?!? */
1833#ifdef _KERNEL
1834int pf_anchor_copyout(const struct pf_ruleset *,
1835 const struct pf_rule *, struct pfioc_rule *);
1836void pf_anchor_remove(struct pf_rule *);
1837
1838#endif /* _KERNEL */
1839#endif
1840
1841/* The fingerprint functions can be linked into userland programs (tcpdump) */
1842int pf_osfp_add(struct pf_osfp_ioctl *);
1843#ifdef _KERNEL
1844struct pf_osfp_enlist *
1845 pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
1846 const struct tcphdr *);
1847#endif /* _KERNEL */
1848struct pf_osfp_enlist *
1849 pf_osfp_fingerprint_hdr(const struct ip *, const struct ip6_hdr *,
1850 const struct tcphdr *);
1851void pf_osfp_flush(void);
1852int pf_osfp_get(struct pf_osfp_ioctl *);
1853#ifdef __FreeBSD__
1854int pf_osfp_initialize(void);
1855void pf_osfp_cleanup(void);
1856#else
1857void pf_osfp_initialize(void);
1858#endif
1859int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
1860struct pf_os_fingerprint *
1861 pf_osfp_validate(void);
1862
1863#endif /* _NET_PFVAR_H_ */
2/* $OpenBSD: pfvar.h,v 1.244 2007/02/23 21:31:51 deraadt Exp $ */
3
4/*
5 * Copyright (c) 2001 Daniel Hartmeier
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#ifndef _NET_PFVAR_H_
35#define _NET_PFVAR_H_
36
37#include <sys/param.h>
38#include <sys/types.h>
39#include <sys/queue.h>
40#include <sys/tree.h>
41#ifdef __FreeBSD__
42#include <sys/lock.h>
43#include <sys/sx.h>
44#else
45#include <sys/rwlock.h>
46#endif
47
48#include <net/radix.h>
49#include <net/route.h>
50#ifdef __FreeBSD__
51#include <net/if_clone.h>
52#include <net/pf_mtag.h>
53#include <vm/uma.h>
54#else
55#include <netinet/ip_ipsp.h>
56#endif
57
58#ifdef __FreeBSD__
59#include <netinet/in.h>
60#endif
61
62#include <netinet/tcp_fsm.h>
63
64struct ip;
65struct ip6_hdr;
66#ifdef __FreeBSD__
67struct inpcb;
68#endif
69
70#define PF_TCPS_PROXY_SRC ((TCP_NSTATES)+0)
71#define PF_TCPS_PROXY_DST ((TCP_NSTATES)+1)
72
73#define PF_MD5_DIGEST_LENGTH 16
74#ifdef MD5_DIGEST_LENGTH
75#if PF_MD5_DIGEST_LENGTH != MD5_DIGEST_LENGTH
76#error
77#endif
78#endif
79
80enum { PF_INOUT, PF_IN, PF_OUT };
81enum { PF_LAN_EXT, PF_EXT_GWY, PF_ID };
82enum { PF_PASS, PF_DROP, PF_SCRUB, PF_NOSCRUB, PF_NAT, PF_NONAT,
83 PF_BINAT, PF_NOBINAT, PF_RDR, PF_NORDR, PF_SYNPROXY_DROP };
84enum { PF_RULESET_SCRUB, PF_RULESET_FILTER, PF_RULESET_NAT,
85 PF_RULESET_BINAT, PF_RULESET_RDR, PF_RULESET_MAX };
86enum { PF_OP_NONE, PF_OP_IRG, PF_OP_EQ, PF_OP_NE, PF_OP_LT,
87 PF_OP_LE, PF_OP_GT, PF_OP_GE, PF_OP_XRG, PF_OP_RRG };
88enum { PF_DEBUG_NONE, PF_DEBUG_URGENT, PF_DEBUG_MISC, PF_DEBUG_NOISY };
89enum { PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
90 PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
91 PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
92enum { PF_GET_NONE, PF_GET_CLR_CNTR };
93
94/*
95 * Note about PFTM_*: real indices into pf_rule.timeout[] come before
96 * PFTM_MAX, special cases afterwards. See pf_state_expires().
97 */
98enum { PFTM_TCP_FIRST_PACKET, PFTM_TCP_OPENING, PFTM_TCP_ESTABLISHED,
99 PFTM_TCP_CLOSING, PFTM_TCP_FIN_WAIT, PFTM_TCP_CLOSED,
100 PFTM_UDP_FIRST_PACKET, PFTM_UDP_SINGLE, PFTM_UDP_MULTIPLE,
101 PFTM_ICMP_FIRST_PACKET, PFTM_ICMP_ERROR_REPLY,
102 PFTM_OTHER_FIRST_PACKET, PFTM_OTHER_SINGLE,
103 PFTM_OTHER_MULTIPLE, PFTM_FRAG, PFTM_INTERVAL,
104 PFTM_ADAPTIVE_START, PFTM_ADAPTIVE_END, PFTM_SRC_NODE,
105 PFTM_TS_DIFF, PFTM_MAX, PFTM_PURGE, PFTM_UNLINKED,
106 PFTM_UNTIL_PACKET };
107
108/* PFTM default values */
109#define PFTM_TCP_FIRST_PACKET_VAL 120 /* First TCP packet */
110#define PFTM_TCP_OPENING_VAL 30 /* No response yet */
111#define PFTM_TCP_ESTABLISHED_VAL 24*60*60/* Established */
112#define PFTM_TCP_CLOSING_VAL 15 * 60 /* Half closed */
113#define PFTM_TCP_FIN_WAIT_VAL 45 /* Got both FINs */
114#define PFTM_TCP_CLOSED_VAL 90 /* Got a RST */
115#define PFTM_UDP_FIRST_PACKET_VAL 60 /* First UDP packet */
116#define PFTM_UDP_SINGLE_VAL 30 /* Unidirectional */
117#define PFTM_UDP_MULTIPLE_VAL 60 /* Bidirectional */
118#define PFTM_ICMP_FIRST_PACKET_VAL 20 /* First ICMP packet */
119#define PFTM_ICMP_ERROR_REPLY_VAL 10 /* Got error response */
120#define PFTM_OTHER_FIRST_PACKET_VAL 60 /* First packet */
121#define PFTM_OTHER_SINGLE_VAL 30 /* Unidirectional */
122#define PFTM_OTHER_MULTIPLE_VAL 60 /* Bidirectional */
123#define PFTM_FRAG_VAL 30 /* Fragment expire */
124#define PFTM_INTERVAL_VAL 10 /* Expire interval */
125#define PFTM_SRC_NODE_VAL 0 /* Source tracking */
126#define PFTM_TS_DIFF_VAL 30 /* Allowed TS diff */
127
128enum { PF_NOPFROUTE, PF_FASTROUTE, PF_ROUTETO, PF_DUPTO, PF_REPLYTO };
129enum { PF_LIMIT_STATES, PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
130 PF_LIMIT_TABLES, PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_MAX };
131#define PF_POOL_IDMASK 0x0f
132enum { PF_POOL_NONE, PF_POOL_BITMASK, PF_POOL_RANDOM,
133 PF_POOL_SRCHASH, PF_POOL_ROUNDROBIN };
134enum { PF_ADDR_ADDRMASK, PF_ADDR_NOROUTE, PF_ADDR_DYNIFTL,
135 PF_ADDR_TABLE, PF_ADDR_RTLABEL, PF_ADDR_URPFFAILED };
136#define PF_POOL_TYPEMASK 0x0f
137#define PF_POOL_STICKYADDR 0x20
138#define PF_WSCALE_FLAG 0x80
139#define PF_WSCALE_MASK 0x0f
140
141#define PF_LOG 0x01
142#define PF_LOG_ALL 0x02
143#define PF_LOG_SOCKET_LOOKUP 0x04
144
145struct pf_addr {
146 union {
147 struct in_addr v4;
148 struct in6_addr v6;
149 u_int8_t addr8[16];
150 u_int16_t addr16[8];
151 u_int32_t addr32[4];
152 } pfa; /* 128-bit address */
153#define v4 pfa.v4
154#define v6 pfa.v6
155#define addr8 pfa.addr8
156#define addr16 pfa.addr16
157#define addr32 pfa.addr32
158};
159
160#define PF_TABLE_NAME_SIZE 32
161
162#define PFI_AFLAG_NETWORK 0x01
163#define PFI_AFLAG_BROADCAST 0x02
164#define PFI_AFLAG_PEER 0x04
165#define PFI_AFLAG_MODEMASK 0x07
166#define PFI_AFLAG_NOALIAS 0x08
167
168struct pf_addr_wrap {
169 union {
170 struct {
171 struct pf_addr addr;
172 struct pf_addr mask;
173 } a;
174 char ifname[IFNAMSIZ];
175 char tblname[PF_TABLE_NAME_SIZE];
176#ifdef __FreeBSD__
177#define RTLABEL_LEN 32
178#endif
179 char rtlabelname[RTLABEL_LEN];
180 u_int32_t rtlabel;
181 } v;
182 union {
183 struct pfi_dynaddr *dyn;
184 struct pfr_ktable *tbl;
185 int dyncnt;
186 int tblcnt;
187 } p;
188 u_int8_t type; /* PF_ADDR_* */
189 u_int8_t iflags; /* PFI_AFLAG_* */
190};
191
192#ifdef _KERNEL
193
194struct pfi_dynaddr {
195 TAILQ_ENTRY(pfi_dynaddr) entry;
196 struct pf_addr pfid_addr4;
197 struct pf_addr pfid_mask4;
198 struct pf_addr pfid_addr6;
199 struct pf_addr pfid_mask6;
200 struct pfr_ktable *pfid_kt;
201 struct pfi_kif *pfid_kif;
202 void *pfid_hook_cookie;
203 int pfid_net; /* mask or 128 */
204 int pfid_acnt4; /* address count IPv4 */
205 int pfid_acnt6; /* address count IPv6 */
206 sa_family_t pfid_af; /* rule af */
207 u_int8_t pfid_iflags; /* PFI_AFLAG_* */
208};
209
210/*
211 * Address manipulation macros
212 */
213
214#ifdef __FreeBSD__
215#define splsoftnet() splnet()
216
217#define HTONL(x) (x) = htonl((__uint32_t)(x))
218#define HTONS(x) (x) = htons((__uint16_t)(x))
219#define NTOHL(x) (x) = ntohl((__uint32_t)(x))
220#define NTOHS(x) (x) = ntohs((__uint16_t)(x))
221
222#define PF_NAME "pf"
223
224#define PR_NOWAIT M_NOWAIT
225#define pool_get(p, f) uma_zalloc(*(p), (f))
226#define pool_put(p, o) uma_zfree(*(p), (o))
227
228#define UMA_CREATE(var, type, desc) \
229 var = uma_zcreate(desc, sizeof(type), \
230 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); \
231 if (var == NULL) break
232#define UMA_DESTROY(var) \
233 if(var) uma_zdestroy(var)
234
235extern struct mtx pf_task_mtx;
236
237#define PF_ASSERT(h) mtx_assert(&pf_task_mtx, (h))
238
239#define PF_LOCK() do { \
240 PF_ASSERT(MA_NOTOWNED); \
241 mtx_lock(&pf_task_mtx); \
242} while(0)
243#define PF_UNLOCK() do { \
244 PF_ASSERT(MA_OWNED); \
245 mtx_unlock(&pf_task_mtx); \
246} while(0)
247
248#define PF_COPYIN(uaddr, kaddr, len, r) do { \
249 PF_UNLOCK(); \
250 r = copyin((uaddr), (kaddr), (len)); \
251 PF_LOCK(); \
252} while(0)
253
254#define PF_COPYOUT(kaddr, uaddr, len, r) do { \
255 PF_UNLOCK(); \
256 r = copyout((kaddr), (uaddr), (len)); \
257 PF_LOCK(); \
258} while(0)
259
260extern void init_pf_mutex(void);
261extern void destroy_pf_mutex(void);
262
263#define PF_MODVER 1
264#define PFLOG_MODVER 1
265#define PFSYNC_MODVER 1
266
267#define PFLOG_MINVER 1
268#define PFLOG_PREFVER PFLOG_MODVER
269#define PFLOG_MAXVER 1
270#define PFSYNC_MINVER 1
271#define PFSYNC_PREFVER PFSYNC_MODVER
272#define PFSYNC_MAXVER 1
273#endif /* __FreeBSD__ */
274
275#ifdef INET
276#ifndef INET6
277#define PF_INET_ONLY
278#endif /* ! INET6 */
279#endif /* INET */
280
281#ifdef INET6
282#ifndef INET
283#define PF_INET6_ONLY
284#endif /* ! INET */
285#endif /* INET6 */
286
287#ifdef INET
288#ifdef INET6
289#define PF_INET_INET6
290#endif /* INET6 */
291#endif /* INET */
292
293#else
294
295#define PF_INET_INET6
296
297#endif /* _KERNEL */
298
299/* Both IPv4 and IPv6 */
300#ifdef PF_INET_INET6
301
302#define PF_AEQ(a, b, c) \
303 ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
304 ((a)->addr32[3] == (b)->addr32[3] && \
305 (a)->addr32[2] == (b)->addr32[2] && \
306 (a)->addr32[1] == (b)->addr32[1] && \
307 (a)->addr32[0] == (b)->addr32[0])) \
308
309#define PF_ANEQ(a, b, c) \
310 ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
311 ((a)->addr32[3] != (b)->addr32[3] || \
312 (a)->addr32[2] != (b)->addr32[2] || \
313 (a)->addr32[1] != (b)->addr32[1] || \
314 (a)->addr32[0] != (b)->addr32[0])) \
315
316#define PF_AZERO(a, c) \
317 ((c == AF_INET && !(a)->addr32[0]) || \
318 (!(a)->addr32[0] && !(a)->addr32[1] && \
319 !(a)->addr32[2] && !(a)->addr32[3] )) \
320
321#define PF_MATCHA(n, a, m, b, f) \
322 pf_match_addr(n, a, m, b, f)
323
324#define PF_ACPY(a, b, f) \
325 pf_addrcpy(a, b, f)
326
327#define PF_AINC(a, f) \
328 pf_addr_inc(a, f)
329
330#define PF_POOLMASK(a, b, c, d, f) \
331 pf_poolmask(a, b, c, d, f)
332
333#else
334
335/* Just IPv6 */
336
337#ifdef PF_INET6_ONLY
338
339#define PF_AEQ(a, b, c) \
340 ((a)->addr32[3] == (b)->addr32[3] && \
341 (a)->addr32[2] == (b)->addr32[2] && \
342 (a)->addr32[1] == (b)->addr32[1] && \
343 (a)->addr32[0] == (b)->addr32[0]) \
344
345#define PF_ANEQ(a, b, c) \
346 ((a)->addr32[3] != (b)->addr32[3] || \
347 (a)->addr32[2] != (b)->addr32[2] || \
348 (a)->addr32[1] != (b)->addr32[1] || \
349 (a)->addr32[0] != (b)->addr32[0]) \
350
351#define PF_AZERO(a, c) \
352 (!(a)->addr32[0] && \
353 !(a)->addr32[1] && \
354 !(a)->addr32[2] && \
355 !(a)->addr32[3] ) \
356
357#define PF_MATCHA(n, a, m, b, f) \
358 pf_match_addr(n, a, m, b, f)
359
360#define PF_ACPY(a, b, f) \
361 pf_addrcpy(a, b, f)
362
363#define PF_AINC(a, f) \
364 pf_addr_inc(a, f)
365
366#define PF_POOLMASK(a, b, c, d, f) \
367 pf_poolmask(a, b, c, d, f)
368
369#else
370
371/* Just IPv4 */
372#ifdef PF_INET_ONLY
373
374#define PF_AEQ(a, b, c) \
375 ((a)->addr32[0] == (b)->addr32[0])
376
377#define PF_ANEQ(a, b, c) \
378 ((a)->addr32[0] != (b)->addr32[0])
379
380#define PF_AZERO(a, c) \
381 (!(a)->addr32[0])
382
383#define PF_MATCHA(n, a, m, b, f) \
384 pf_match_addr(n, a, m, b, f)
385
386#define PF_ACPY(a, b, f) \
387 (a)->v4.s_addr = (b)->v4.s_addr
388
389#define PF_AINC(a, f) \
390 do { \
391 (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
392 } while (0)
393
394#define PF_POOLMASK(a, b, c, d, f) \
395 do { \
396 (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
397 (((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
398 } while (0)
399
400#endif /* PF_INET_ONLY */
401#endif /* PF_INET6_ONLY */
402#endif /* PF_INET_INET6 */
403
404#define PF_MISMATCHAW(aw, x, af, neg, ifp) \
405 ( \
406 (((aw)->type == PF_ADDR_NOROUTE && \
407 pf_routable((x), (af), NULL)) || \
408 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \
409 pf_routable((x), (af), (ifp))) || \
410 ((aw)->type == PF_ADDR_RTLABEL && \
411 !pf_rtlabel_match((x), (af), (aw))) || \
412 ((aw)->type == PF_ADDR_TABLE && \
413 !pfr_match_addr((aw)->p.tbl, (x), (af))) || \
414 ((aw)->type == PF_ADDR_DYNIFTL && \
415 !pfi_match_addr((aw)->p.dyn, (x), (af))) || \
416 ((aw)->type == PF_ADDR_ADDRMASK && \
417 !PF_AZERO(&(aw)->v.a.mask, (af)) && \
418 !PF_MATCHA(0, &(aw)->v.a.addr, \
419 &(aw)->v.a.mask, (x), (af))))) != \
420 (neg) \
421 )
422
423
424struct pf_rule_uid {
425 uid_t uid[2];
426 u_int8_t op;
427};
428
429struct pf_rule_gid {
430 uid_t gid[2];
431 u_int8_t op;
432};
433
434struct pf_rule_addr {
435 struct pf_addr_wrap addr;
436 u_int16_t port[2];
437 u_int8_t neg;
438 u_int8_t port_op;
439};
440
441struct pf_pooladdr {
442 struct pf_addr_wrap addr;
443 TAILQ_ENTRY(pf_pooladdr) entries;
444 char ifname[IFNAMSIZ];
445 struct pfi_kif *kif;
446};
447
448TAILQ_HEAD(pf_palist, pf_pooladdr);
449
450struct pf_poolhashkey {
451 union {
452 u_int8_t key8[16];
453 u_int16_t key16[8];
454 u_int32_t key32[4];
455 } pfk; /* 128-bit hash key */
456#define key8 pfk.key8
457#define key16 pfk.key16
458#define key32 pfk.key32
459};
460
461struct pf_pool {
462 struct pf_palist list;
463 struct pf_pooladdr *cur;
464 struct pf_poolhashkey key;
465 struct pf_addr counter;
466 int tblidx;
467 u_int16_t proxy_port[2];
468 u_int8_t port_op;
469 u_int8_t opts;
470};
471
472
473/* A packed Operating System description for fingerprinting */
474typedef u_int32_t pf_osfp_t;
475#define PF_OSFP_ANY ((pf_osfp_t)0)
476#define PF_OSFP_UNKNOWN ((pf_osfp_t)-1)
477#define PF_OSFP_NOMATCH ((pf_osfp_t)-2)
478
479struct pf_osfp_entry {
480 SLIST_ENTRY(pf_osfp_entry) fp_entry;
481 pf_osfp_t fp_os;
482 int fp_enflags;
483#define PF_OSFP_EXPANDED 0x001 /* expanded entry */
484#define PF_OSFP_GENERIC 0x002 /* generic signature */
485#define PF_OSFP_NODETAIL 0x004 /* no p0f details */
486#define PF_OSFP_LEN 32
487 char fp_class_nm[PF_OSFP_LEN];
488 char fp_version_nm[PF_OSFP_LEN];
489 char fp_subtype_nm[PF_OSFP_LEN];
490};
491#define PF_OSFP_ENTRY_EQ(a, b) \
492 ((a)->fp_os == (b)->fp_os && \
493 memcmp((a)->fp_class_nm, (b)->fp_class_nm, PF_OSFP_LEN) == 0 && \
494 memcmp((a)->fp_version_nm, (b)->fp_version_nm, PF_OSFP_LEN) == 0 && \
495 memcmp((a)->fp_subtype_nm, (b)->fp_subtype_nm, PF_OSFP_LEN) == 0)
496
497/* handle pf_osfp_t packing */
498#define _FP_RESERVED_BIT 1 /* For the special negative #defines */
499#define _FP_UNUSED_BITS 1
500#define _FP_CLASS_BITS 10 /* OS Class (Windows, Linux) */
501#define _FP_VERSION_BITS 10 /* OS version (95, 98, NT, 2.4.54, 3.2) */
502#define _FP_SUBTYPE_BITS 10 /* patch level (NT SP4, SP3, ECN patch) */
503#define PF_OSFP_UNPACK(osfp, class, version, subtype) do { \
504 (class) = ((osfp) >> (_FP_VERSION_BITS+_FP_SUBTYPE_BITS)) & \
505 ((1 << _FP_CLASS_BITS) - 1); \
506 (version) = ((osfp) >> _FP_SUBTYPE_BITS) & \
507 ((1 << _FP_VERSION_BITS) - 1);\
508 (subtype) = (osfp) & ((1 << _FP_SUBTYPE_BITS) - 1); \
509} while(0)
510#define PF_OSFP_PACK(osfp, class, version, subtype) do { \
511 (osfp) = ((class) & ((1 << _FP_CLASS_BITS) - 1)) << (_FP_VERSION_BITS \
512 + _FP_SUBTYPE_BITS); \
513 (osfp) |= ((version) & ((1 << _FP_VERSION_BITS) - 1)) << \
514 _FP_SUBTYPE_BITS; \
515 (osfp) |= (subtype) & ((1 << _FP_SUBTYPE_BITS) - 1); \
516} while(0)
517
518/* the fingerprint of an OSes TCP SYN packet */
519typedef u_int64_t pf_tcpopts_t;
520struct pf_os_fingerprint {
521 SLIST_HEAD(pf_osfp_enlist, pf_osfp_entry) fp_oses; /* list of matches */
522 pf_tcpopts_t fp_tcpopts; /* packed TCP options */
523 u_int16_t fp_wsize; /* TCP window size */
524 u_int16_t fp_psize; /* ip->ip_len */
525 u_int16_t fp_mss; /* TCP MSS */
526 u_int16_t fp_flags;
527#define PF_OSFP_WSIZE_MOD 0x0001 /* Window modulus */
528#define PF_OSFP_WSIZE_DC 0x0002 /* Window don't care */
529#define PF_OSFP_WSIZE_MSS 0x0004 /* Window multiple of MSS */
530#define PF_OSFP_WSIZE_MTU 0x0008 /* Window multiple of MTU */
531#define PF_OSFP_PSIZE_MOD 0x0010 /* packet size modulus */
532#define PF_OSFP_PSIZE_DC 0x0020 /* packet size don't care */
533#define PF_OSFP_WSCALE 0x0040 /* TCP window scaling */
534#define PF_OSFP_WSCALE_MOD 0x0080 /* TCP window scale modulus */
535#define PF_OSFP_WSCALE_DC 0x0100 /* TCP window scale dont-care */
536#define PF_OSFP_MSS 0x0200 /* TCP MSS */
537#define PF_OSFP_MSS_MOD 0x0400 /* TCP MSS modulus */
538#define PF_OSFP_MSS_DC 0x0800 /* TCP MSS dont-care */
539#define PF_OSFP_DF 0x1000 /* IPv4 don't fragment bit */
540#define PF_OSFP_TS0 0x2000 /* Zero timestamp */
541#define PF_OSFP_INET6 0x4000 /* IPv6 */
542 u_int8_t fp_optcnt; /* TCP option count */
543 u_int8_t fp_wscale; /* TCP window scaling */
544 u_int8_t fp_ttl; /* IPv4 TTL */
545#define PF_OSFP_MAXTTL_OFFSET 40
546/* TCP options packing */
547#define PF_OSFP_TCPOPT_NOP 0x0 /* TCP NOP option */
548#define PF_OSFP_TCPOPT_WSCALE 0x1 /* TCP window scaling option */
549#define PF_OSFP_TCPOPT_MSS 0x2 /* TCP max segment size opt */
550#define PF_OSFP_TCPOPT_SACK 0x3 /* TCP SACK OK option */
551#define PF_OSFP_TCPOPT_TS 0x4 /* TCP timestamp option */
552#define PF_OSFP_TCPOPT_BITS 3 /* bits used by each option */
553#define PF_OSFP_MAX_OPTS \
554 (sizeof(((struct pf_os_fingerprint *)0)->fp_tcpopts) * 8) \
555 / PF_OSFP_TCPOPT_BITS
556
557 SLIST_ENTRY(pf_os_fingerprint) fp_next;
558};
559
560struct pf_osfp_ioctl {
561 struct pf_osfp_entry fp_os;
562 pf_tcpopts_t fp_tcpopts; /* packed TCP options */
563 u_int16_t fp_wsize; /* TCP window size */
564 u_int16_t fp_psize; /* ip->ip_len */
565 u_int16_t fp_mss; /* TCP MSS */
566 u_int16_t fp_flags;
567 u_int8_t fp_optcnt; /* TCP option count */
568 u_int8_t fp_wscale; /* TCP window scaling */
569 u_int8_t fp_ttl; /* IPv4 TTL */
570
571 int fp_getnum; /* DIOCOSFPGET number */
572};
573
574
575union pf_rule_ptr {
576 struct pf_rule *ptr;
577 u_int32_t nr;
578};
579
580#define PF_ANCHOR_NAME_SIZE 64
581
582struct pf_rule {
583 struct pf_rule_addr src;
584 struct pf_rule_addr dst;
585#define PF_SKIP_IFP 0
586#define PF_SKIP_DIR 1
587#define PF_SKIP_AF 2
588#define PF_SKIP_PROTO 3
589#define PF_SKIP_SRC_ADDR 4
590#define PF_SKIP_SRC_PORT 5
591#define PF_SKIP_DST_ADDR 6
592#define PF_SKIP_DST_PORT 7
593#define PF_SKIP_COUNT 8
594 union pf_rule_ptr skip[PF_SKIP_COUNT];
595#define PF_RULE_LABEL_SIZE 64
596 char label[PF_RULE_LABEL_SIZE];
597#define PF_QNAME_SIZE 64
598 char ifname[IFNAMSIZ];
599 char qname[PF_QNAME_SIZE];
600 char pqname[PF_QNAME_SIZE];
601#define PF_TAG_NAME_SIZE 64
602 char tagname[PF_TAG_NAME_SIZE];
603 char match_tagname[PF_TAG_NAME_SIZE];
604
605 char overload_tblname[PF_TABLE_NAME_SIZE];
606
607 TAILQ_ENTRY(pf_rule) entries;
608 struct pf_pool rpool;
609
610 u_int64_t evaluations;
611 u_int64_t packets[2];
612 u_int64_t bytes[2];
613
614 struct pfi_kif *kif;
615 struct pf_anchor *anchor;
616 struct pfr_ktable *overload_tbl;
617
618 pf_osfp_t os_fingerprint;
619
620 int rtableid;
621 u_int32_t timeout[PFTM_MAX];
622 u_int32_t states;
623 u_int32_t max_states;
624 u_int32_t src_nodes;
625 u_int32_t max_src_nodes;
626 u_int32_t max_src_states;
627 u_int32_t spare1; /* netgraph */
628 u_int32_t max_src_conn;
629 struct {
630 u_int32_t limit;
631 u_int32_t seconds;
632 } max_src_conn_rate;
633 u_int32_t qid;
634 u_int32_t pqid;
635 u_int32_t rt_listid;
636 u_int32_t nr;
637 u_int32_t prob;
638 uid_t cuid;
639 pid_t cpid;
640
641 u_int16_t return_icmp;
642 u_int16_t return_icmp6;
643 u_int16_t max_mss;
644 u_int16_t tag;
645 u_int16_t match_tag;
646 u_int16_t spare2; /* netgraph */
647
648 struct pf_rule_uid uid;
649 struct pf_rule_gid gid;
650
651 u_int32_t rule_flag;
652 u_int8_t action;
653 u_int8_t direction;
654 u_int8_t log;
655 u_int8_t logif;
656 u_int8_t quick;
657 u_int8_t ifnot;
658 u_int8_t match_tag_not;
659 u_int8_t natpass;
660
661#define PF_STATE_NORMAL 0x1
662#define PF_STATE_MODULATE 0x2
663#define PF_STATE_SYNPROXY 0x3
664 u_int8_t keep_state;
665 sa_family_t af;
666 u_int8_t proto;
667 u_int8_t type;
668 u_int8_t code;
669 u_int8_t flags;
670 u_int8_t flagset;
671 u_int8_t min_ttl;
672 u_int8_t allow_opts;
673 u_int8_t rt;
674 u_int8_t return_ttl;
675 u_int8_t tos;
676 u_int8_t anchor_relative;
677 u_int8_t anchor_wildcard;
678
679#define PF_FLUSH 0x01
680#define PF_FLUSH_GLOBAL 0x02
681 u_int8_t flush;
682};
683
684/* rule flags */
685#define PFRULE_DROP 0x0000
686#define PFRULE_RETURNRST 0x0001
687#define PFRULE_FRAGMENT 0x0002
688#define PFRULE_RETURNICMP 0x0004
689#define PFRULE_RETURN 0x0008
690#define PFRULE_NOSYNC 0x0010
691#define PFRULE_SRCTRACK 0x0020 /* track source states */
692#define PFRULE_RULESRCTRACK 0x0040 /* per rule */
693
694/* scrub flags */
695#define PFRULE_NODF 0x0100
696#define PFRULE_FRAGCROP 0x0200 /* non-buffering frag cache */
697#define PFRULE_FRAGDROP 0x0400 /* drop funny fragments */
698#define PFRULE_RANDOMID 0x0800
699#define PFRULE_REASSEMBLE_TCP 0x1000
700
701/* rule flags again */
702#define PFRULE_IFBOUND 0x00010000 /* if-bound */
703
704#define PFSTATE_HIWAT 10000 /* default state table size */
705#define PFSTATE_ADAPT_START 6000 /* default adaptive timeout start */
706#define PFSTATE_ADAPT_END 12000 /* default adaptive timeout end */
707
708
709struct pf_threshold {
710 u_int32_t limit;
711#define PF_THRESHOLD_MULT 1000
712#define PF_THRESHOLD_MAX 0xffffffff / PF_THRESHOLD_MULT
713 u_int32_t seconds;
714 u_int32_t count;
715 u_int32_t last;
716};
717
718struct pf_src_node {
719 RB_ENTRY(pf_src_node) entry;
720 struct pf_addr addr;
721 struct pf_addr raddr;
722 union pf_rule_ptr rule;
723 struct pfi_kif *kif;
724 u_int64_t bytes[2];
725 u_int64_t packets[2];
726 u_int32_t states;
727 u_int32_t conn;
728 struct pf_threshold conn_rate;
729 u_int32_t creation;
730 u_int32_t expire;
731 sa_family_t af;
732 u_int8_t ruletype;
733};
734
735#define PFSNODE_HIWAT 10000 /* default source node table size */
736
737struct pf_state_scrub {
738 struct timeval pfss_last; /* time received last packet */
739 u_int32_t pfss_tsecr; /* last echoed timestamp */
740 u_int32_t pfss_tsval; /* largest timestamp */
741 u_int32_t pfss_tsval0; /* original timestamp */
742 u_int16_t pfss_flags;
743#define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */
744#define PFSS_PAWS 0x0010 /* stricter PAWS checks */
745#define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */
746#define PFSS_DATA_TS 0x0040 /* timestamp on data packets */
747#define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */
748 u_int8_t pfss_ttl; /* stashed TTL */
749 u_int8_t pad;
750 u_int32_t pfss_ts_mod; /* timestamp modulation */
751};
752
753struct pf_state_host {
754 struct pf_addr addr;
755 u_int16_t port;
756 u_int16_t pad;
757};
758
759struct pf_state_peer {
760 u_int32_t seqlo; /* Max sequence number sent */
761 u_int32_t seqhi; /* Max the other end ACKd + win */
762 u_int32_t seqdiff; /* Sequence number modulator */
763 u_int16_t max_win; /* largest window (pre scaling) */
764 u_int8_t state; /* active state level */
765 u_int8_t wscale; /* window scaling factor */
766 u_int16_t mss; /* Maximum segment size option */
767 u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */
768 struct pf_state_scrub *scrub; /* state is scrubbed */
769 u_int8_t pad[3];
770};
771
772TAILQ_HEAD(pf_state_queue, pf_state);
773
774/* keep synced with struct pf_state, used in RB_FIND */
775struct pf_state_cmp {
776 u_int64_t id;
777 u_int32_t creatorid;
778 struct pf_state_host lan;
779 struct pf_state_host gwy;
780 struct pf_state_host ext;
781 sa_family_t af;
782 u_int8_t proto;
783 u_int8_t direction;
784 u_int8_t pad;
785};
786
787struct pf_state {
788 u_int64_t id;
789 u_int32_t creatorid;
790 struct pf_state_host lan;
791 struct pf_state_host gwy;
792 struct pf_state_host ext;
793 sa_family_t af;
794 u_int8_t proto;
795 u_int8_t direction;
796#ifdef __FreeBSD__
797 u_int8_t local_flags;
798#define PFSTATE_EXPIRING 0x01
799#else
800 u_int8_t pad;
801#endif
802 u_int8_t log;
803 u_int8_t allow_opts;
804 u_int8_t timeout;
805 u_int8_t sync_flags;
806#define PFSTATE_NOSYNC 0x01
807#define PFSTATE_FROMSYNC 0x02
808#define PFSTATE_STALE 0x04
809 union {
810 struct {
811 RB_ENTRY(pf_state) entry_lan_ext;
812 RB_ENTRY(pf_state) entry_ext_gwy;
813 RB_ENTRY(pf_state) entry_id;
814 TAILQ_ENTRY(pf_state) entry_list;
815 struct pfi_kif *kif;
816 } s;
817 char ifname[IFNAMSIZ];
818 } u;
819 struct pf_state_peer src;
820 struct pf_state_peer dst;
821 union pf_rule_ptr rule;
822 union pf_rule_ptr anchor;
823 union pf_rule_ptr nat_rule;
824 struct pf_addr rt_addr;
825 struct pfi_kif *rt_kif;
826 struct pf_src_node *src_node;
827 struct pf_src_node *nat_src_node;
828 u_int64_t packets[2];
829 u_int64_t bytes[2];
830 u_int32_t creation;
831 u_int32_t expire;
832 u_int32_t pfsync_time;
833 u_int16_t tag;
834};
835
836TAILQ_HEAD(pf_rulequeue, pf_rule);
837
838struct pf_anchor;
839
840struct pf_ruleset {
841 struct {
842 struct pf_rulequeue queues[2];
843 struct {
844 struct pf_rulequeue *ptr;
845 struct pf_rule **ptr_array;
846 u_int32_t rcount;
847 u_int32_t ticket;
848 int open;
849 } active, inactive;
850 } rules[PF_RULESET_MAX];
851 struct pf_anchor *anchor;
852 u_int32_t tticket;
853 int tables;
854 int topen;
855};
856
857RB_HEAD(pf_anchor_global, pf_anchor);
858RB_HEAD(pf_anchor_node, pf_anchor);
859struct pf_anchor {
860 RB_ENTRY(pf_anchor) entry_global;
861 RB_ENTRY(pf_anchor) entry_node;
862 struct pf_anchor *parent;
863 struct pf_anchor_node children;
864 char name[PF_ANCHOR_NAME_SIZE];
865 char path[MAXPATHLEN];
866 struct pf_ruleset ruleset;
867 int refcnt; /* anchor rules */
868 int match;
869};
870RB_PROTOTYPE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
871RB_PROTOTYPE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
872
873#define PF_RESERVED_ANCHOR "_pf"
874
875#define PFR_TFLAG_PERSIST 0x00000001
876#define PFR_TFLAG_CONST 0x00000002
877#define PFR_TFLAG_ACTIVE 0x00000004
878#define PFR_TFLAG_INACTIVE 0x00000008
879#define PFR_TFLAG_REFERENCED 0x00000010
880#define PFR_TFLAG_REFDANCHOR 0x00000020
881#define PFR_TFLAG_USRMASK 0x00000003
882#define PFR_TFLAG_SETMASK 0x0000003C
883#define PFR_TFLAG_ALLMASK 0x0000003F
884
885struct pfr_table {
886 char pfrt_anchor[MAXPATHLEN];
887 char pfrt_name[PF_TABLE_NAME_SIZE];
888 u_int32_t pfrt_flags;
889 u_int8_t pfrt_fback;
890};
891
892enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
893 PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
894 PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_MAX };
895
896struct pfr_addr {
897 union {
898 struct in_addr _pfra_ip4addr;
899 struct in6_addr _pfra_ip6addr;
900 } pfra_u;
901 u_int8_t pfra_af;
902 u_int8_t pfra_net;
903 u_int8_t pfra_not;
904 u_int8_t pfra_fback;
905};
906#define pfra_ip4addr pfra_u._pfra_ip4addr
907#define pfra_ip6addr pfra_u._pfra_ip6addr
908
909enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
910enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
911#define PFR_OP_XPASS PFR_OP_ADDR_MAX
912
913struct pfr_astats {
914 struct pfr_addr pfras_a;
915 u_int64_t pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
916 u_int64_t pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
917 long pfras_tzero;
918};
919
920enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
921
922struct pfr_tstats {
923 struct pfr_table pfrts_t;
924 u_int64_t pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
925 u_int64_t pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
926 u_int64_t pfrts_match;
927 u_int64_t pfrts_nomatch;
928 long pfrts_tzero;
929 int pfrts_cnt;
930 int pfrts_refcnt[PFR_REFCNT_MAX];
931};
932#define pfrts_name pfrts_t.pfrt_name
933#define pfrts_flags pfrts_t.pfrt_flags
934
935#ifndef _SOCKADDR_UNION_DEFINED
936#define _SOCKADDR_UNION_DEFINED
937union sockaddr_union {
938 struct sockaddr sa;
939 struct sockaddr_in sin;
940 struct sockaddr_in6 sin6;
941};
942#endif /* _SOCKADDR_UNION_DEFINED */
943
944SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
945struct pfr_kentry {
946 struct radix_node pfrke_node[2];
947 union sockaddr_union pfrke_sa;
948 u_int64_t pfrke_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
949 u_int64_t pfrke_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
950 SLIST_ENTRY(pfr_kentry) pfrke_workq;
951 long pfrke_tzero;
952 u_int8_t pfrke_af;
953 u_int8_t pfrke_net;
954 u_int8_t pfrke_not;
955 u_int8_t pfrke_mark;
956 u_int8_t pfrke_intrpool;
957};
958
959SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
960RB_HEAD(pfr_ktablehead, pfr_ktable);
961struct pfr_ktable {
962 struct pfr_tstats pfrkt_ts;
963 RB_ENTRY(pfr_ktable) pfrkt_tree;
964 SLIST_ENTRY(pfr_ktable) pfrkt_workq;
965 struct radix_node_head *pfrkt_ip4;
966 struct radix_node_head *pfrkt_ip6;
967 struct pfr_ktable *pfrkt_shadow;
968 struct pfr_ktable *pfrkt_root;
969 struct pf_ruleset *pfrkt_rs;
970 long pfrkt_larg;
971 int pfrkt_nflags;
972};
973#define pfrkt_t pfrkt_ts.pfrts_t
974#define pfrkt_name pfrkt_t.pfrt_name
975#define pfrkt_anchor pfrkt_t.pfrt_anchor
976#define pfrkt_ruleset pfrkt_t.pfrt_ruleset
977#define pfrkt_flags pfrkt_t.pfrt_flags
978#define pfrkt_cnt pfrkt_ts.pfrts_cnt
979#define pfrkt_refcnt pfrkt_ts.pfrts_refcnt
980#define pfrkt_packets pfrkt_ts.pfrts_packets
981#define pfrkt_bytes pfrkt_ts.pfrts_bytes
982#define pfrkt_match pfrkt_ts.pfrts_match
983#define pfrkt_nomatch pfrkt_ts.pfrts_nomatch
984#define pfrkt_tzero pfrkt_ts.pfrts_tzero
985
986RB_HEAD(pf_state_tree_lan_ext, pf_state);
987RB_PROTOTYPE(pf_state_tree_lan_ext, pf_state,
988 u.s.entry_lan_ext, pf_state_compare_lan_ext);
989
990RB_HEAD(pf_state_tree_ext_gwy, pf_state);
991RB_PROTOTYPE(pf_state_tree_ext_gwy, pf_state,
992 u.s.entry_ext_gwy, pf_state_compare_ext_gwy);
993
994TAILQ_HEAD(pfi_statehead, pfi_kif);
995RB_HEAD(pfi_ifhead, pfi_kif);
996
997/* keep synced with pfi_kif, used in RB_FIND */
998struct pfi_kif_cmp {
999 char pfik_name[IFNAMSIZ];
1000};
1001
1002struct pfi_kif {
1003 char pfik_name[IFNAMSIZ];
1004 RB_ENTRY(pfi_kif) pfik_tree;
1005 u_int64_t pfik_packets[2][2][2];
1006 u_int64_t pfik_bytes[2][2][2];
1007 u_int32_t pfik_tzero;
1008 int pfik_flags;
1009 struct pf_state_tree_lan_ext pfik_lan_ext;
1010 struct pf_state_tree_ext_gwy pfik_ext_gwy;
1011 TAILQ_ENTRY(pfi_kif) pfik_w_states;
1012#ifndef __FreeBSD__
1013 void *pfik_ah_cookie;
1014#endif
1015 struct ifnet *pfik_ifp;
1016 struct ifg_group *pfik_group;
1017 int pfik_states;
1018 int pfik_rules;
1019 TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs;
1020};
1021
1022enum pfi_kif_refs {
1023 PFI_KIF_REF_NONE,
1024 PFI_KIF_REF_STATE,
1025 PFI_KIF_REF_RULE
1026};
1027
1028#define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */
1029/* XXX: revisist */
1030#define PFI_IFLAG_SETABLE_MASK 0x0100 /* setable via DIOC{SET,CLR}IFFLAG */
1031#define PFI_IFLAG_PLACEHOLDER 0x8000 /* placeholder group/interface */
1032
1033struct pf_pdesc {
1034 struct {
1035 int done;
1036 uid_t uid;
1037 gid_t gid;
1038 pid_t pid;
1039 } lookup;
1040 u_int64_t tot_len; /* Make Mickey money */
1041 union {
1042 struct tcphdr *tcp;
1043 struct udphdr *udp;
1044 struct icmp *icmp;
1045#ifdef INET6
1046 struct icmp6_hdr *icmp6;
1047#endif /* INET6 */
1048 void *any;
1049 } hdr;
1050 struct pf_addr baddr; /* address before translation */
1051 struct pf_addr naddr; /* address after translation */
1052 struct pf_rule *nat_rule; /* nat/rdr rule applied to packet */
1053 struct pf_addr *src;
1054 struct pf_addr *dst;
1055 struct ether_header
1056 *eh;
1057 struct pf_mtag *pf_mtag;
1058 u_int16_t *ip_sum;
1059 u_int32_t p_len; /* total length of payload */
1060 u_int16_t flags; /* Let SCRUB trigger behavior in
1061 * state code. Easier than tags */
1062#define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */
1063#define PFDESC_IP_REAS 0x0002 /* IP frags would've been reassembled */
1064 sa_family_t af;
1065 u_int8_t proto;
1066 u_int8_t tos;
1067};
1068
1069/* flags for RDR options */
1070#define PF_DPORT_RANGE 0x01 /* Dest port uses range */
1071#define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */
1072
1073/* Reasons code for passing/dropping a packet */
1074#define PFRES_MATCH 0 /* Explicit match of a rule */
1075#define PFRES_BADOFF 1 /* Bad offset for pull_hdr */
1076#define PFRES_FRAG 2 /* Dropping following fragment */
1077#define PFRES_SHORT 3 /* Dropping short packet */
1078#define PFRES_NORM 4 /* Dropping by normalizer */
1079#define PFRES_MEMORY 5 /* Dropped due to lacking mem */
1080#define PFRES_TS 6 /* Bad TCP Timestamp (RFC1323) */
1081#define PFRES_CONGEST 7 /* Congestion (of ipintrq) */
1082#define PFRES_IPOPTIONS 8 /* IP option */
1083#define PFRES_PROTCKSUM 9 /* Protocol checksum invalid */
1084#define PFRES_BADSTATE 10 /* State mismatch */
1085#define PFRES_STATEINS 11 /* State insertion failure */
1086#define PFRES_MAXSTATES 12 /* State limit */
1087#define PFRES_SRCLIMIT 13 /* Source node/conn limit */
1088#define PFRES_SYNPROXY 14 /* SYN proxy */
1089#define PFRES_MAX 15 /* total+1 */
1090
1091#define PFRES_NAMES { \
1092 "match", \
1093 "bad-offset", \
1094 "fragment", \
1095 "short", \
1096 "normalize", \
1097 "memory", \
1098 "bad-timestamp", \
1099 "congestion", \
1100 "ip-option", \
1101 "proto-cksum", \
1102 "state-mismatch", \
1103 "state-insert", \
1104 "state-limit", \
1105 "src-limit", \
1106 "synproxy", \
1107 NULL \
1108}
1109
1110/* Counters for other things we want to keep track of */
1111#define LCNT_STATES 0 /* states */
1112#define LCNT_SRCSTATES 1 /* max-src-states */
1113#define LCNT_SRCNODES 2 /* max-src-nodes */
1114#define LCNT_SRCCONN 3 /* max-src-conn */
1115#define LCNT_SRCCONNRATE 4 /* max-src-conn-rate */
1116#define LCNT_OVERLOAD_TABLE 5 /* entry added to overload table */
1117#define LCNT_OVERLOAD_FLUSH 6 /* state entries flushed */
1118#define LCNT_MAX 7 /* total+1 */
1119
1120#define LCNT_NAMES { \
1121 "max states per rule", \
1122 "max-src-states", \
1123 "max-src-nodes", \
1124 "max-src-conn", \
1125 "max-src-conn-rate", \
1126 "overload table insertion", \
1127 "overload flush states", \
1128 NULL \
1129}
1130
1131/* UDP state enumeration */
1132#define PFUDPS_NO_TRAFFIC 0
1133#define PFUDPS_SINGLE 1
1134#define PFUDPS_MULTIPLE 2
1135
1136#define PFUDPS_NSTATES 3 /* number of state levels */
1137
1138#define PFUDPS_NAMES { \
1139 "NO_TRAFFIC", \
1140 "SINGLE", \
1141 "MULTIPLE", \
1142 NULL \
1143}
1144
1145/* Other protocol state enumeration */
1146#define PFOTHERS_NO_TRAFFIC 0
1147#define PFOTHERS_SINGLE 1
1148#define PFOTHERS_MULTIPLE 2
1149
1150#define PFOTHERS_NSTATES 3 /* number of state levels */
1151
1152#define PFOTHERS_NAMES { \
1153 "NO_TRAFFIC", \
1154 "SINGLE", \
1155 "MULTIPLE", \
1156 NULL \
1157}
1158
1159#define FCNT_STATE_SEARCH 0
1160#define FCNT_STATE_INSERT 1
1161#define FCNT_STATE_REMOVALS 2
1162#define FCNT_MAX 3
1163
1164#define SCNT_SRC_NODE_SEARCH 0
1165#define SCNT_SRC_NODE_INSERT 1
1166#define SCNT_SRC_NODE_REMOVALS 2
1167#define SCNT_MAX 3
1168
1169#define ACTION_SET(a, x) \
1170 do { \
1171 if ((a) != NULL) \
1172 *(a) = (x); \
1173 } while (0)
1174
1175#define REASON_SET(a, x) \
1176 do { \
1177 if ((a) != NULL) \
1178 *(a) = (x); \
1179 if (x < PFRES_MAX) \
1180 pf_status.counters[x]++; \
1181 } while (0)
1182
1183struct pf_status {
1184 u_int64_t counters[PFRES_MAX];
1185 u_int64_t lcounters[LCNT_MAX]; /* limit counters */
1186 u_int64_t fcounters[FCNT_MAX];
1187 u_int64_t scounters[SCNT_MAX];
1188 u_int64_t pcounters[2][2][3];
1189 u_int64_t bcounters[2][2];
1190 u_int64_t stateid;
1191 u_int32_t running;
1192 u_int32_t states;
1193 u_int32_t src_nodes;
1194 u_int32_t since;
1195 u_int32_t debug;
1196 u_int32_t hostid;
1197 char ifname[IFNAMSIZ];
1198 u_int8_t pf_chksum[PF_MD5_DIGEST_LENGTH];
1199};
1200
1201struct cbq_opts {
1202 u_int minburst;
1203 u_int maxburst;
1204 u_int pktsize;
1205 u_int maxpktsize;
1206 u_int ns_per_byte;
1207 u_int maxidle;
1208 int minidle;
1209 u_int offtime;
1210 int flags;
1211};
1212
1213struct priq_opts {
1214 int flags;
1215};
1216
1217struct hfsc_opts {
1218 /* real-time service curve */
1219 u_int rtsc_m1; /* slope of the 1st segment in bps */
1220 u_int rtsc_d; /* the x-projection of m1 in msec */
1221 u_int rtsc_m2; /* slope of the 2nd segment in bps */
1222 /* link-sharing service curve */
1223 u_int lssc_m1;
1224 u_int lssc_d;
1225 u_int lssc_m2;
1226 /* upper-limit service curve */
1227 u_int ulsc_m1;
1228 u_int ulsc_d;
1229 u_int ulsc_m2;
1230 int flags;
1231};
1232
1233struct pf_altq {
1234 char ifname[IFNAMSIZ];
1235
1236 void *altq_disc; /* discipline-specific state */
1237 TAILQ_ENTRY(pf_altq) entries;
1238
1239 /* scheduler spec */
1240 u_int8_t scheduler; /* scheduler type */
1241 u_int16_t tbrsize; /* tokenbucket regulator size */
1242 u_int32_t ifbandwidth; /* interface bandwidth */
1243
1244 /* queue spec */
1245 char qname[PF_QNAME_SIZE]; /* queue name */
1246 char parent[PF_QNAME_SIZE]; /* parent name */
1247 u_int32_t parent_qid; /* parent queue id */
1248 u_int32_t bandwidth; /* queue bandwidth */
1249 u_int8_t priority; /* priority */
1250#ifdef __FreeBSD__
1251 u_int8_t local_flags; /* dynamic interface */
1252#define PFALTQ_FLAG_IF_REMOVED 0x01
1253#endif
1254 u_int16_t qlimit; /* queue size limit */
1255 u_int16_t flags; /* misc flags */
1256 union {
1257 struct cbq_opts cbq_opts;
1258 struct priq_opts priq_opts;
1259 struct hfsc_opts hfsc_opts;
1260 } pq_u;
1261
1262 u_int32_t qid; /* return value */
1263};
1264
1265#ifndef __FreeBSD__
1266
1267#define PF_TAG_GENERATED 0x01
1268#define PF_TAG_FRAGCACHE 0x02
1269#define PF_TAG_TRANSLATE_LOCALHOST 0x04
1270
1271struct pf_mtag {
1272 void *hdr; /* saved hdr pos in mbuf, for ECN */
1273 u_int rtableid; /* alternate routing table id */
1274 u_int32_t qid; /* queue id */
1275 u_int16_t tag; /* tag id */
1276 u_int8_t flags;
1277 u_int8_t routed;
1278 sa_family_t af; /* for ECN */
1279};
1280#endif
1281
1282struct pf_tag {
1283 u_int16_t tag; /* tag id */
1284};
1285
1286struct pf_tagname {
1287 TAILQ_ENTRY(pf_tagname) entries;
1288 char name[PF_TAG_NAME_SIZE];
1289 u_int16_t tag;
1290 int ref;
1291};
1292
1293#define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */
1294#define PFFRAG_FRAG_HIWAT 1000 /* Number of fragmented packets */
1295#define PFFRAG_FRCENT_HIWAT 50000 /* Number of fragment cache entries */
1296#define PFFRAG_FRCACHE_HIWAT 10000 /* Number of fragment descriptors */
1297
1298#define PFR_KTABLE_HIWAT 1000 /* Number of tables */
1299#define PFR_KENTRY_HIWAT 200000 /* Number of table entries */
1300#define PFR_KENTRY_HIWAT_SMALL 100000 /* Number of table entries (tiny hosts) */
1301
1302/*
1303 * ioctl parameter structures
1304 */
1305
1306struct pfioc_pooladdr {
1307 u_int32_t action;
1308 u_int32_t ticket;
1309 u_int32_t nr;
1310 u_int32_t r_num;
1311 u_int8_t r_action;
1312 u_int8_t r_last;
1313 u_int8_t af;
1314 char anchor[MAXPATHLEN];
1315 struct pf_pooladdr addr;
1316};
1317
1318struct pfioc_rule {
1319 u_int32_t action;
1320 u_int32_t ticket;
1321 u_int32_t pool_ticket;
1322 u_int32_t nr;
1323 char anchor[MAXPATHLEN];
1324 char anchor_call[MAXPATHLEN];
1325 struct pf_rule rule;
1326};
1327
1328struct pfioc_natlook {
1329 struct pf_addr saddr;
1330 struct pf_addr daddr;
1331 struct pf_addr rsaddr;
1332 struct pf_addr rdaddr;
1333 u_int16_t sport;
1334 u_int16_t dport;
1335 u_int16_t rsport;
1336 u_int16_t rdport;
1337 sa_family_t af;
1338 u_int8_t proto;
1339 u_int8_t direction;
1340};
1341
1342struct pfioc_state {
1343 u_int32_t nr;
1344 struct pf_state state;
1345};
1346
1347struct pfioc_src_node_kill {
1348 /* XXX returns the number of src nodes killed in psnk_af */
1349 sa_family_t psnk_af;
1350 struct pf_rule_addr psnk_src;
1351 struct pf_rule_addr psnk_dst;
1352};
1353
1354struct pfioc_state_kill {
1355 /* XXX returns the number of states killed in psk_af */
1356 sa_family_t psk_af;
1357 int psk_proto;
1358 struct pf_rule_addr psk_src;
1359 struct pf_rule_addr psk_dst;
1360 char psk_ifname[IFNAMSIZ];
1361};
1362
1363struct pfioc_states {
1364 int ps_len;
1365 union {
1366 caddr_t psu_buf;
1367 struct pf_state *psu_states;
1368 } ps_u;
1369#define ps_buf ps_u.psu_buf
1370#define ps_states ps_u.psu_states
1371};
1372
1373struct pfioc_src_nodes {
1374 int psn_len;
1375 union {
1376 caddr_t psu_buf;
1377 struct pf_src_node *psu_src_nodes;
1378 } psn_u;
1379#define psn_buf psn_u.psu_buf
1380#define psn_src_nodes psn_u.psu_src_nodes
1381};
1382
1383struct pfioc_if {
1384 char ifname[IFNAMSIZ];
1385};
1386
1387struct pfioc_tm {
1388 int timeout;
1389 int seconds;
1390};
1391
1392struct pfioc_limit {
1393 int index;
1394 unsigned limit;
1395};
1396
1397struct pfioc_altq {
1398 u_int32_t action;
1399 u_int32_t ticket;
1400 u_int32_t nr;
1401 struct pf_altq altq;
1402};
1403
1404struct pfioc_qstats {
1405 u_int32_t ticket;
1406 u_int32_t nr;
1407 void *buf;
1408 int nbytes;
1409 u_int8_t scheduler;
1410};
1411
1412struct pfioc_ruleset {
1413 u_int32_t nr;
1414 char path[MAXPATHLEN];
1415 char name[PF_ANCHOR_NAME_SIZE];
1416};
1417
1418#define PF_RULESET_ALTQ (PF_RULESET_MAX)
1419#define PF_RULESET_TABLE (PF_RULESET_MAX+1)
1420struct pfioc_trans {
1421 int size; /* number of elements */
1422 int esize; /* size of each element in bytes */
1423 struct pfioc_trans_e {
1424 int rs_num;
1425 char anchor[MAXPATHLEN];
1426 u_int32_t ticket;
1427 } *array;
1428};
1429
1430#define PFR_FLAG_ATOMIC 0x00000001
1431#define PFR_FLAG_DUMMY 0x00000002
1432#define PFR_FLAG_FEEDBACK 0x00000004
1433#define PFR_FLAG_CLSTATS 0x00000008
1434#define PFR_FLAG_ADDRSTOO 0x00000010
1435#define PFR_FLAG_REPLACE 0x00000020
1436#define PFR_FLAG_ALLRSETS 0x00000040
1437#define PFR_FLAG_ALLMASK 0x0000007F
1438#ifdef _KERNEL
1439#define PFR_FLAG_USERIOCTL 0x10000000
1440#endif
1441
1442struct pfioc_table {
1443 struct pfr_table pfrio_table;
1444 void *pfrio_buffer;
1445 int pfrio_esize;
1446 int pfrio_size;
1447 int pfrio_size2;
1448 int pfrio_nadd;
1449 int pfrio_ndel;
1450 int pfrio_nchange;
1451 int pfrio_flags;
1452 u_int32_t pfrio_ticket;
1453};
1454#define pfrio_exists pfrio_nadd
1455#define pfrio_nzero pfrio_nadd
1456#define pfrio_nmatch pfrio_nadd
1457#define pfrio_naddr pfrio_size2
1458#define pfrio_setflag pfrio_size2
1459#define pfrio_clrflag pfrio_nadd
1460
1461struct pfioc_iface {
1462 char pfiio_name[IFNAMSIZ];
1463 void *pfiio_buffer;
1464 int pfiio_esize;
1465 int pfiio_size;
1466 int pfiio_nzero;
1467 int pfiio_flags;
1468};
1469
1470
1471/*
1472 * ioctl operations
1473 */
1474
1475#define DIOCSTART _IO ('D', 1)
1476#define DIOCSTOP _IO ('D', 2)
1477#define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule)
1478#define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule)
1479#define DIOCGETRULE _IOWR('D', 7, struct pfioc_rule)
1480/* XXX cut 8 - 17 */
1481#define DIOCCLRSTATES _IOWR('D', 18, struct pfioc_state_kill)
1482#define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state)
1483#define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
1484#define DIOCGETSTATUS _IOWR('D', 21, struct pf_status)
1485#define DIOCCLRSTATUS _IO ('D', 22)
1486#define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook)
1487#define DIOCSETDEBUG _IOWR('D', 24, u_int32_t)
1488#define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states)
1489#define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule)
1490/* XXX cut 26 - 28 */
1491#define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm)
1492#define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm)
1493#define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state)
1494#define DIOCCLRRULECTRS _IO ('D', 38)
1495#define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit)
1496#define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit)
1497#define DIOCKILLSTATES _IOWR('D', 41, struct pfioc_state_kill)
1498#define DIOCSTARTALTQ _IO ('D', 42)
1499#define DIOCSTOPALTQ _IO ('D', 43)
1500#define DIOCADDALTQ _IOWR('D', 45, struct pfioc_altq)
1501#define DIOCGETALTQS _IOWR('D', 47, struct pfioc_altq)
1502#define DIOCGETALTQ _IOWR('D', 48, struct pfioc_altq)
1503#define DIOCCHANGEALTQ _IOWR('D', 49, struct pfioc_altq)
1504#define DIOCGETQSTATS _IOWR('D', 50, struct pfioc_qstats)
1505#define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr)
1506#define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr)
1507#define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr)
1508#define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr)
1509#define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr)
1510/* XXX cut 55 - 57 */
1511#define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset)
1512#define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset)
1513#define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table)
1514#define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table)
1515#define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table)
1516#define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table)
1517#define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table)
1518#define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table)
1519#define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table)
1520#define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table)
1521#define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table)
1522#define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table)
1523#define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table)
1524#define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table)
1525#define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table)
1526#define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table)
1527#define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table)
1528#define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table)
1529#define DIOCOSFPFLUSH _IO('D', 78)
1530#define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl)
1531#define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl)
1532#define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans)
1533#define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans)
1534#define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans)
1535#define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes)
1536#define DIOCCLRSRCNODES _IO('D', 85)
1537#define DIOCSETHOSTID _IOWR('D', 86, u_int32_t)
1538#define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface)
1539#define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface)
1540#define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface)
1541#define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill)
1542#ifdef __FreeBSD__
1543struct pf_ifspeed {
1544 char ifname[IFNAMSIZ];
1545 u_int32_t baudrate;
1546};
1547#define DIOCGIFSPEED _IOWR('D', 92, struct pf_ifspeed)
1548#endif
1549
1550#ifdef _KERNEL
1551RB_HEAD(pf_src_tree, pf_src_node);
1552RB_PROTOTYPE(pf_src_tree, pf_src_node, entry, pf_src_compare);
1553extern struct pf_src_tree tree_src_tracking;
1554
1555RB_HEAD(pf_state_tree_id, pf_state);
1556RB_PROTOTYPE(pf_state_tree_id, pf_state,
1557 entry_id, pf_state_compare_id);
1558extern struct pf_state_tree_id tree_id;
1559extern struct pf_state_queue state_list;
1560
1561TAILQ_HEAD(pf_poolqueue, pf_pool);
1562extern struct pf_poolqueue pf_pools[2];
1563TAILQ_HEAD(pf_altqqueue, pf_altq);
1564extern struct pf_altqqueue pf_altqs[2];
1565extern struct pf_palist pf_pabuf;
1566
1567extern u_int32_t ticket_altqs_active;
1568extern u_int32_t ticket_altqs_inactive;
1569extern int altqs_inactive_open;
1570extern u_int32_t ticket_pabuf;
1571extern struct pf_altqqueue *pf_altqs_active;
1572extern struct pf_altqqueue *pf_altqs_inactive;
1573extern struct pf_poolqueue *pf_pools_active;
1574extern struct pf_poolqueue *pf_pools_inactive;
1575extern int pf_tbladdr_setup(struct pf_ruleset *,
1576 struct pf_addr_wrap *);
1577extern void pf_tbladdr_remove(struct pf_addr_wrap *);
1578extern void pf_tbladdr_copyout(struct pf_addr_wrap *);
1579extern void pf_calc_skip_steps(struct pf_rulequeue *);
1580#ifdef __FreeBSD__
1581#ifdef ALTQ
1582extern void pf_altq_ifnet_event(struct ifnet *, int);
1583#endif
1584extern uma_zone_t pf_src_tree_pl, pf_rule_pl;
1585extern uma_zone_t pf_state_pl, pf_altq_pl, pf_pooladdr_pl;
1586extern uma_zone_t pfr_ktable_pl, pfr_kentry_pl, pfr_kentry_pl2;
1587extern uma_zone_t pf_cache_pl, pf_cent_pl;
1588extern uma_zone_t pf_state_scrub_pl;
1589extern uma_zone_t pfi_addr_pl;
1590#else
1591extern struct pool pf_src_tree_pl, pf_rule_pl;
1592extern struct pool pf_state_pl, pf_altq_pl, pf_pooladdr_pl;
1593extern struct pool pf_state_scrub_pl;
1594#endif
1595extern void pf_purge_thread(void *);
1596#ifdef __FreeBSD__
1597extern int pf_purge_expired_src_nodes(int);
1598extern int pf_purge_expired_states(u_int32_t, int);
1599#else
1600extern void pf_purge_expired_src_nodes(int);
1601extern void pf_purge_expired_states(u_int32_t);
1602#endif
1603extern void pf_unlink_state(struct pf_state *);
1604extern void pf_free_state(struct pf_state *);
1605extern int pf_insert_state(struct pfi_kif *,
1606 struct pf_state *);
1607extern int pf_insert_src_node(struct pf_src_node **,
1608 struct pf_rule *, struct pf_addr *,
1609 sa_family_t);
1610void pf_src_tree_remove_state(struct pf_state *);
1611extern struct pf_state *pf_find_state_byid(struct pf_state_cmp *);
1612extern struct pf_state *pf_find_state_all(struct pf_state_cmp *key,
1613 u_int8_t tree, int *more);
1614extern void pf_print_state(struct pf_state *);
1615extern void pf_print_flags(u_int8_t);
1616extern u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
1617 u_int8_t);
1618
1619extern struct ifnet *sync_ifp;
1620extern struct pf_rule pf_default_rule;
1621extern void pf_addrcpy(struct pf_addr *, struct pf_addr *,
1622 u_int8_t);
1623void pf_rm_rule(struct pf_rulequeue *,
1624 struct pf_rule *);
1625
1626#ifdef INET
1627#ifdef __FreeBSD__
1628int pf_test(int, struct ifnet *, struct mbuf **, struct ether_header *,
1629 struct inpcb *);
1630#else
1631int pf_test(int, struct ifnet *, struct mbuf **, struct ether_header *);
1632#endif
1633#endif /* INET */
1634
1635#ifdef INET6
1636#ifdef __FreeBSD__
1637int pf_test6(int, struct ifnet *, struct mbuf **, struct ether_header *,
1638 struct inpcb *);
1639#else
1640int pf_test6(int, struct ifnet *, struct mbuf **, struct ether_header *);
1641#endif
1642void pf_poolmask(struct pf_addr *, struct pf_addr*,
1643 struct pf_addr *, struct pf_addr *, u_int8_t);
1644void pf_addr_inc(struct pf_addr *, sa_family_t);
1645#endif /* INET6 */
1646
1647#ifdef __FreeBSD__
1648u_int32_t pf_new_isn(struct pf_state *);
1649#endif
1650void *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
1651 sa_family_t);
1652void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
1653int pflog_packet(struct pfi_kif *, struct mbuf *, sa_family_t, u_int8_t,
1654 u_int8_t, struct pf_rule *, struct pf_rule *, struct pf_ruleset *,
1655 struct pf_pdesc *);
1656int pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
1657 struct pf_addr *, sa_family_t);
1658int pf_match(u_int8_t, u_int32_t, u_int32_t, u_int32_t);
1659int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
1660int pf_match_uid(u_int8_t, uid_t, uid_t, uid_t);
1661int pf_match_gid(u_int8_t, gid_t, gid_t, gid_t);
1662
1663void pf_normalize_init(void);
1664int pf_normalize_ip(struct mbuf **, int, struct pfi_kif *, u_short *,
1665 struct pf_pdesc *);
1666int pf_normalize_ip6(struct mbuf **, int, struct pfi_kif *, u_short *,
1667 struct pf_pdesc *);
1668int pf_normalize_tcp(int, struct pfi_kif *, struct mbuf *, int, int, void *,
1669 struct pf_pdesc *);
1670void pf_normalize_tcp_cleanup(struct pf_state *);
1671int pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
1672 struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
1673int pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
1674 u_short *, struct tcphdr *, struct pf_state *,
1675 struct pf_state_peer *, struct pf_state_peer *, int *);
1676u_int32_t
1677 pf_state_expires(const struct pf_state *);
1678void pf_purge_expired_fragments(void);
1679int pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *);
1680int pf_rtlabel_match(struct pf_addr *, sa_family_t, struct pf_addr_wrap *);
1681#ifdef __FreeBSD__
1682int pf_socket_lookup(int, struct pf_pdesc *, struct inpcb *);
1683#else
1684int pf_socket_lookup(int, struct pf_pdesc *);
1685#endif
1686void pfr_initialize(void);
1687int pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
1688void pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
1689 u_int64_t, int, int, int);
1690int pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *,
1691 struct pf_addr **, struct pf_addr **, sa_family_t);
1692void pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
1693struct pfr_ktable *
1694 pfr_attach_table(struct pf_ruleset *, char *);
1695void pfr_detach_table(struct pfr_ktable *);
1696int pfr_clr_tables(struct pfr_table *, int *, int);
1697int pfr_add_tables(struct pfr_table *, int, int *, int);
1698int pfr_del_tables(struct pfr_table *, int, int *, int);
1699int pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
1700int pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
1701int pfr_clr_tstats(struct pfr_table *, int, int *, int);
1702int pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
1703int pfr_clr_addrs(struct pfr_table *, int *, int);
1704int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
1705int pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1706 int);
1707int pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1708 int);
1709int pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1710 int *, int *, int *, int, u_int32_t);
1711int pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
1712int pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
1713int pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
1714 int);
1715int pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
1716 int);
1717int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
1718int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
1719int pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
1720int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
1721 int *, u_int32_t, int);
1722
1723extern struct pfi_statehead pfi_statehead;
1724extern struct pfi_kif *pfi_all;
1725
1726void pfi_initialize(void);
1727#ifdef __FreeBSD__
1728void pfi_cleanup(void);
1729#endif
1730struct pfi_kif *pfi_kif_get(const char *);
1731void pfi_kif_ref(struct pfi_kif *, enum pfi_kif_refs);
1732void pfi_kif_unref(struct pfi_kif *, enum pfi_kif_refs);
1733int pfi_kif_match(struct pfi_kif *, struct pfi_kif *);
1734void pfi_attach_ifnet(struct ifnet *);
1735void pfi_detach_ifnet(struct ifnet *);
1736void pfi_attach_ifgroup(struct ifg_group *);
1737void pfi_detach_ifgroup(struct ifg_group *);
1738void pfi_group_change(const char *);
1739int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
1740 sa_family_t);
1741int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
1742void pfi_dynaddr_remove(struct pf_addr_wrap *);
1743void pfi_dynaddr_copyout(struct pf_addr_wrap *);
1744void pfi_fill_oldstatus(struct pf_status *);
1745int pfi_clr_istats(const char *);
1746int pfi_get_ifaces(const char *, struct pfi_kif *, int *);
1747int pfi_set_flags(const char *, int);
1748int pfi_clear_flags(const char *, int);
1749
1750u_int16_t pf_tagname2tag(char *);
1751void pf_tag2tagname(u_int16_t, char *);
1752void pf_tag_ref(u_int16_t);
1753void pf_tag_unref(u_int16_t);
1754int pf_tag_packet(struct mbuf *, struct pf_mtag *, int, int);
1755u_int32_t pf_qname2qid(char *);
1756void pf_qid2qname(u_int32_t, char *);
1757void pf_qid_unref(u_int32_t);
1758#ifndef __FreeBSD__
1759struct pf_mtag *pf_find_mtag(struct mbuf *);
1760struct pf_mtag *pf_get_mtag(struct mbuf *);
1761#endif
1762
1763extern struct pf_status pf_status;
1764
1765#ifdef __FreeBSD__
1766extern uma_zone_t pf_frent_pl, pf_frag_pl;
1767extern struct sx pf_consistency_lock;
1768#else
1769extern struct pool pf_frent_pl, pf_frag_pl;
1770extern struct rwlock pf_consistency_lock;
1771#endif
1772
1773struct pf_pool_limit {
1774 void *pp;
1775 unsigned limit;
1776};
1777extern struct pf_pool_limit pf_pool_limits[PF_LIMIT_MAX];
1778
1779#ifdef __FreeBSD__
1780struct pf_frent {
1781 LIST_ENTRY(pf_frent) fr_next;
1782 struct ip *fr_ip;
1783 struct mbuf *fr_m;
1784};
1785
1786struct pf_frcache {
1787 LIST_ENTRY(pf_frcache) fr_next;
1788 uint16_t fr_off;
1789 uint16_t fr_end;
1790};
1791
1792struct pf_fragment {
1793 RB_ENTRY(pf_fragment) fr_entry;
1794 TAILQ_ENTRY(pf_fragment) frag_next;
1795 struct in_addr fr_src;
1796 struct in_addr fr_dst;
1797 u_int8_t fr_p; /* protocol of this fragment */
1798 u_int8_t fr_flags; /* status flags */
1799 u_int16_t fr_id; /* fragment id for reassemble */
1800 u_int16_t fr_max; /* fragment data max */
1801 u_int32_t fr_timeout;
1802#define fr_queue fr_u.fru_queue
1803#define fr_cache fr_u.fru_cache
1804 union {
1805 LIST_HEAD(pf_fragq, pf_frent) fru_queue; /* buffering */
1806 LIST_HEAD(pf_cacheq, pf_frcache) fru_cache; /* non-buf */
1807 } fr_u;
1808};
1809#endif /* (__FreeBSD__) */
1810
1811#endif /* _KERNEL */
1812
1813extern struct pf_anchor_global pf_anchors;
1814extern struct pf_anchor pf_main_anchor;
1815#define pf_main_ruleset pf_main_anchor.ruleset
1816
1817/* these ruleset functions can be linked into userland programs (pfctl) */
1818int pf_get_ruleset_number(u_int8_t);
1819void pf_init_ruleset(struct pf_ruleset *);
1820int pf_anchor_setup(struct pf_rule *,
1821 const struct pf_ruleset *, const char *);
1822int pf_anchor_copyout(const struct pf_ruleset *,
1823 const struct pf_rule *, struct pfioc_rule *);
1824void pf_anchor_remove(struct pf_rule *);
1825void pf_remove_if_empty_ruleset(struct pf_ruleset *);
1826struct pf_anchor *pf_find_anchor(const char *);
1827struct pf_ruleset *pf_find_ruleset(const char *);
1828struct pf_ruleset *pf_find_or_create_ruleset(const char *);
1829void pf_rs_initialize(void);
1830
1831#ifndef __FreeBSD__
1832/* ?!? */
1833#ifdef _KERNEL
1834int pf_anchor_copyout(const struct pf_ruleset *,
1835 const struct pf_rule *, struct pfioc_rule *);
1836void pf_anchor_remove(struct pf_rule *);
1837
1838#endif /* _KERNEL */
1839#endif
1840
1841/* The fingerprint functions can be linked into userland programs (tcpdump) */
1842int pf_osfp_add(struct pf_osfp_ioctl *);
1843#ifdef _KERNEL
1844struct pf_osfp_enlist *
1845 pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
1846 const struct tcphdr *);
1847#endif /* _KERNEL */
1848struct pf_osfp_enlist *
1849 pf_osfp_fingerprint_hdr(const struct ip *, const struct ip6_hdr *,
1850 const struct tcphdr *);
1851void pf_osfp_flush(void);
1852int pf_osfp_get(struct pf_osfp_ioctl *);
1853#ifdef __FreeBSD__
1854int pf_osfp_initialize(void);
1855void pf_osfp_cleanup(void);
1856#else
1857void pf_osfp_initialize(void);
1858#endif
1859int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
1860struct pf_os_fingerprint *
1861 pf_osfp_validate(void);
1862
1863#endif /* _NET_PFVAR_H_ */