xref: /freebsd/sys/netpfil/pf/pf.h (revision f078c492a9b57877c723586db26d789cda1b98ea)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Daniel Hartmeier
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  *	$OpenBSD: pfvar.h,v 1.282 2009/01/29 15:12:28 pyr Exp $
32  *	$FreeBSD$
33  */
34 
35 #ifndef	_NET_PF_H_
36 #define	_NET_PF_H_
37 
38 #define	PF_TCPS_PROXY_SRC	((TCP_NSTATES)+0)
39 #define	PF_TCPS_PROXY_DST	((TCP_NSTATES)+1)
40 
41 #define	PF_MD5_DIGEST_LENGTH	16
42 #ifdef MD5_DIGEST_LENGTH
43 #if PF_MD5_DIGEST_LENGTH != MD5_DIGEST_LENGTH
44 #error
45 #endif
46 #endif
47 
48 enum	{ PF_INOUT, PF_IN, PF_OUT };
49 enum	{ PF_PASS, PF_DROP, PF_SCRUB, PF_NOSCRUB, PF_NAT, PF_NONAT,
50 	  PF_BINAT, PF_NOBINAT, PF_RDR, PF_NORDR, PF_SYNPROXY_DROP, PF_DEFER };
51 enum	{ PF_RULESET_SCRUB, PF_RULESET_FILTER, PF_RULESET_NAT,
52 	  PF_RULESET_BINAT, PF_RULESET_RDR, PF_RULESET_MAX };
53 enum	{ PF_OP_NONE, PF_OP_IRG, PF_OP_EQ, PF_OP_NE, PF_OP_LT,
54 	  PF_OP_LE, PF_OP_GT, PF_OP_GE, PF_OP_XRG, PF_OP_RRG };
55 enum	{ PF_DEBUG_NONE, PF_DEBUG_URGENT, PF_DEBUG_MISC, PF_DEBUG_NOISY };
56 enum	{ PF_CHANGE_NONE, PF_CHANGE_ADD_HEAD, PF_CHANGE_ADD_TAIL,
57 	  PF_CHANGE_ADD_BEFORE, PF_CHANGE_ADD_AFTER,
58 	  PF_CHANGE_REMOVE, PF_CHANGE_GET_TICKET };
59 enum	{ PF_GET_NONE, PF_GET_CLR_CNTR };
60 enum	{ PF_SK_WIRE, PF_SK_STACK, PF_SK_BOTH };
61 
62 /*
63  * Note about PFTM_*: real indices into pf_rule.timeout[] come before
64  * PFTM_MAX, special cases afterwards. See pf_state_expires().
65  */
66 enum	{ PFTM_TCP_FIRST_PACKET, PFTM_TCP_OPENING, PFTM_TCP_ESTABLISHED,
67 	  PFTM_TCP_CLOSING, PFTM_TCP_FIN_WAIT, PFTM_TCP_CLOSED,
68 	  PFTM_UDP_FIRST_PACKET, PFTM_UDP_SINGLE, PFTM_UDP_MULTIPLE,
69 	  PFTM_ICMP_FIRST_PACKET, PFTM_ICMP_ERROR_REPLY,
70 	  PFTM_OTHER_FIRST_PACKET, PFTM_OTHER_SINGLE,
71 	  PFTM_OTHER_MULTIPLE, PFTM_FRAG, PFTM_INTERVAL,
72 	  PFTM_ADAPTIVE_START, PFTM_ADAPTIVE_END, PFTM_SRC_NODE,
73 	  PFTM_TS_DIFF, PFTM_MAX, PFTM_PURGE, PFTM_UNLINKED };
74 
75 /* PFTM default values */
76 #define PFTM_TCP_FIRST_PACKET_VAL	120	/* First TCP packet */
77 #define PFTM_TCP_OPENING_VAL		30	/* No response yet */
78 #define PFTM_TCP_ESTABLISHED_VAL	24*60*60/* Established */
79 #define PFTM_TCP_CLOSING_VAL		15 * 60	/* Half closed */
80 #define PFTM_TCP_FIN_WAIT_VAL		45	/* Got both FINs */
81 #define PFTM_TCP_CLOSED_VAL		90	/* Got a RST */
82 #define PFTM_UDP_FIRST_PACKET_VAL	60	/* First UDP packet */
83 #define PFTM_UDP_SINGLE_VAL		30	/* Unidirectional */
84 #define PFTM_UDP_MULTIPLE_VAL		60	/* Bidirectional */
85 #define PFTM_ICMP_FIRST_PACKET_VAL	20	/* First ICMP packet */
86 #define PFTM_ICMP_ERROR_REPLY_VAL	10	/* Got error response */
87 #define PFTM_OTHER_FIRST_PACKET_VAL	60	/* First packet */
88 #define PFTM_OTHER_SINGLE_VAL		30	/* Unidirectional */
89 #define PFTM_OTHER_MULTIPLE_VAL		60	/* Bidirectional */
90 #define PFTM_FRAG_VAL			30	/* Fragment expire */
91 #define PFTM_INTERVAL_VAL		10	/* Expire interval */
92 #define PFTM_SRC_NODE_VAL		0	/* Source tracking */
93 #define PFTM_TS_DIFF_VAL		30	/* Allowed TS diff */
94 
95 enum	{ PF_NOPFROUTE, PF_FASTROUTE, PF_ROUTETO, PF_DUPTO, PF_REPLYTO };
96 enum	{ PF_LIMIT_STATES, PF_LIMIT_SRC_NODES, PF_LIMIT_FRAGS,
97 	  PF_LIMIT_TABLE_ENTRIES, PF_LIMIT_MAX };
98 #define PF_POOL_IDMASK		0x0f
99 enum	{ PF_POOL_NONE, PF_POOL_BITMASK, PF_POOL_RANDOM,
100 	  PF_POOL_SRCHASH, PF_POOL_ROUNDROBIN };
101 enum	{ PF_ADDR_ADDRMASK, PF_ADDR_NOROUTE, PF_ADDR_DYNIFTL,
102 	  PF_ADDR_TABLE, PF_ADDR_URPFFAILED,
103 	  PF_ADDR_RANGE };
104 #define PF_POOL_TYPEMASK	0x0f
105 #define PF_POOL_STICKYADDR	0x20
106 #define	PF_WSCALE_FLAG		0x80
107 #define	PF_WSCALE_MASK		0x0f
108 
109 #define	PF_LOG			0x01
110 #define	PF_LOG_ALL		0x02
111 #define	PF_LOG_SOCKET_LOOKUP	0x04
112 
113 /* Reasons code for passing/dropping a packet */
114 #define PFRES_MATCH	0		/* Explicit match of a rule */
115 #define PFRES_BADOFF	1		/* Bad offset for pull_hdr */
116 #define PFRES_FRAG	2		/* Dropping following fragment */
117 #define PFRES_SHORT	3		/* Dropping short packet */
118 #define PFRES_NORM	4		/* Dropping by normalizer */
119 #define PFRES_MEMORY	5		/* Dropped due to lacking mem */
120 #define PFRES_TS	6		/* Bad TCP Timestamp (RFC1323) */
121 #define PFRES_CONGEST	7		/* Congestion (of ipintrq) */
122 #define PFRES_IPOPTIONS 8		/* IP option */
123 #define PFRES_PROTCKSUM 9		/* Protocol checksum invalid */
124 #define PFRES_BADSTATE	10		/* State mismatch */
125 #define PFRES_STATEINS	11		/* State insertion failure */
126 #define PFRES_MAXSTATES	12		/* State limit */
127 #define PFRES_SRCLIMIT	13		/* Source node/conn limit */
128 #define PFRES_SYNPROXY	14		/* SYN proxy */
129 #define PFRES_MAPFAILED	15		/* pf_map_addr() failed */
130 #define PFRES_MAX	16		/* total+1 */
131 
132 #define PFRES_NAMES { \
133 	"match", \
134 	"bad-offset", \
135 	"fragment", \
136 	"short", \
137 	"normalize", \
138 	"memory", \
139 	"bad-timestamp", \
140 	"congestion", \
141 	"ip-option", \
142 	"proto-cksum", \
143 	"state-mismatch", \
144 	"state-insert", \
145 	"state-limit", \
146 	"src-limit", \
147 	"synproxy", \
148 	"map-failed", \
149 	NULL \
150 }
151 
152 /* Counters for other things we want to keep track of */
153 #define LCNT_STATES		0	/* states */
154 #define LCNT_SRCSTATES		1	/* max-src-states */
155 #define LCNT_SRCNODES		2	/* max-src-nodes */
156 #define LCNT_SRCCONN		3	/* max-src-conn */
157 #define LCNT_SRCCONNRATE	4	/* max-src-conn-rate */
158 #define LCNT_OVERLOAD_TABLE	5	/* entry added to overload table */
159 #define LCNT_OVERLOAD_FLUSH	6	/* state entries flushed */
160 #define LCNT_MAX		7	/* total+1 */
161 
162 #define LCNT_NAMES { \
163 	"max states per rule", \
164 	"max-src-states", \
165 	"max-src-nodes", \
166 	"max-src-conn", \
167 	"max-src-conn-rate", \
168 	"overload table insertion", \
169 	"overload flush states", \
170 	NULL \
171 }
172 
173 /* state operation counters */
174 #define FCNT_STATE_SEARCH	0
175 #define FCNT_STATE_INSERT	1
176 #define FCNT_STATE_REMOVALS	2
177 #define FCNT_MAX		3
178 
179 /* src_node operation counters */
180 #define SCNT_SRC_NODE_SEARCH	0
181 #define SCNT_SRC_NODE_INSERT	1
182 #define SCNT_SRC_NODE_REMOVALS	2
183 #define SCNT_MAX		3
184 
185 #define	PF_TABLE_NAME_SIZE	32
186 #define	PF_QNAME_SIZE		64
187 
188 struct pf_status {
189 	uint64_t	counters[PFRES_MAX];
190 	uint64_t	lcounters[LCNT_MAX];
191 	uint64_t	fcounters[FCNT_MAX];
192 	uint64_t	scounters[SCNT_MAX];
193 	uint64_t	pcounters[2][2][3];
194 	uint64_t	bcounters[2][2];
195 	uint32_t	running;
196 	uint32_t	states;
197 	uint32_t	src_nodes;
198 	uint32_t	since;
199 	uint32_t	debug;
200 	uint32_t	hostid;
201 	char		ifname[IFNAMSIZ];
202 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
203 };
204 
205 #endif	/* _NET_PF_H_ */
206