xref: /netbsd-src/sys/net/npf/npf.h (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /*-
2  * Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This material is based upon work partially supported by The
6  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
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  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * Public NPF interfaces.
32  */
33 
34 #ifndef _NPF_NET_H_
35 #define _NPF_NET_H_
36 
37 #include <sys/param.h>
38 #include <sys/types.h>
39 
40 #define	NPF_VERSION		22
41 
42 #if defined(_NPF_STANDALONE)
43 #include "npf_stand.h"
44 #else
45 #include <sys/ioctl.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/in.h>
48 #endif
49 
50 struct npf;
51 typedef struct npf npf_t;
52 
53 /*
54  * Storage of address (both for IPv4 and IPv6) and netmask.
55  */
56 typedef union {
57 	uint8_t			word8[16];
58 	uint16_t		word16[8];
59 	uint32_t		word32[4];
60 } npf_addr_t;
61 
62 typedef uint8_t			npf_netmask_t;
63 
64 #define	NPF_MAX_NETMASK		(128)
65 #define	NPF_NO_NETMASK		((npf_netmask_t)~0)
66 
67 /* BPF coprocessor. */
68 #if defined(NPF_BPFCOP)
69 #define	NPF_COP_L3		0
70 #define	NPF_COP_TABLE		1
71 
72 #define	BPF_MW_IPVER		0
73 #define	BPF_MW_L4OFF		1
74 #define	BPF_MW_L4PROTO		2
75 #endif
76 /* The number of words used. */
77 #define	NPF_BPF_NWORDS		3
78 
79 /*
80  * In-kernel declarations and definitions.
81  */
82 
83 #if defined(_KERNEL) || defined(_NPF_STANDALONE)
84 
85 #define	NPF_DECISION_BLOCK	0
86 #define	NPF_DECISION_PASS	1
87 
88 #define	NPF_EXT_MODULE(name, req)	\
89     MODULE(MODULE_CLASS_MISC, name, (sizeof(req) - 1) ? ("npf," req) : "npf")
90 
91 #include <net/if.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip6.h>
94 #include <netinet/tcp.h>
95 #include <netinet/udp.h>
96 #include <netinet/ip_icmp.h>
97 #include <netinet/icmp6.h>
98 
99 /*
100  * Network buffer interface.
101  */
102 
103 #define	NBUF_DATAREF_RESET	0x01
104 
105 struct mbuf;
106 struct nbuf;
107 typedef struct nbuf nbuf_t;
108 
109 void		nbuf_init(npf_t *, nbuf_t *, struct mbuf *, const ifnet_t *);
110 void		nbuf_reset(nbuf_t *);
111 struct mbuf *	nbuf_head_mbuf(nbuf_t *);
112 
113 bool		nbuf_flag_p(const nbuf_t *, int);
114 void		nbuf_unset_flag(nbuf_t *, int);
115 
116 void *		nbuf_dataptr(nbuf_t *);
117 size_t		nbuf_offset(const nbuf_t *);
118 void *		nbuf_advance(nbuf_t *, size_t, size_t);
119 
120 void *		nbuf_ensure_contig(nbuf_t *, size_t);
121 void *		nbuf_ensure_writable(nbuf_t *, size_t);
122 
123 bool		nbuf_cksum_barrier(nbuf_t *, int);
124 int		nbuf_add_tag(nbuf_t *, uint32_t);
125 int		nbuf_find_tag(nbuf_t *, uint32_t *);
126 
127 /*
128  * Packet information cache.
129  */
130 
131 #define	NPC_IP4		0x01	/* Indicates IPv4 header. */
132 #define	NPC_IP6		0x02	/* Indicates IPv6 header. */
133 #define	NPC_IPFRAG	0x04	/* IPv4/IPv6 fragment. */
134 #define	NPC_LAYER4	0x08	/* Layer 4 has been fetched. */
135 
136 #define	NPC_TCP		0x10	/* TCP header. */
137 #define	NPC_UDP		0x20	/* UDP header. */
138 #define	NPC_ICMP	0x40	/* ICMP header. */
139 #define	NPC_ICMP_ID	0x80	/* ICMP with query ID. */
140 
141 #define	NPC_ALG_EXEC	0x100	/* ALG execution. */
142 
143 #define	NPC_FMTERR	0x200	/* Format error. */
144 
145 #define	NPC_IP46	(NPC_IP4|NPC_IP6)
146 
147 struct npf_connkey;
148 
149 typedef struct {
150 	/* NPF context, information flags and the nbuf. */
151 	npf_t *			npc_ctx;
152 	uint32_t		npc_info;
153 	nbuf_t *		npc_nbuf;
154 
155 	/*
156 	 * Pointers to the IP source and destination addresses,
157 	 * and the address length (4 for IPv4 or 16 for IPv6).
158 	 */
159 	npf_addr_t *		npc_ips[2];
160 	uint8_t			npc_alen;
161 
162 	/* IP header length and L4 protocol. */
163 	uint32_t		npc_hlen;
164 	uint16_t		npc_proto;
165 
166 	/* IPv4, IPv6. */
167 	union {
168 		struct ip *		v4;
169 		struct ip6_hdr *	v6;
170 	} npc_ip;
171 
172 	/* TCP, UDP, ICMP or other protocols. */
173 	union {
174 		struct tcphdr *		tcp;
175 		struct udphdr *		udp;
176 		struct icmp *		icmp;
177 		struct icmp6_hdr *	icmp6;
178 		void *			hdr;
179 	} npc_l4;
180 
181 	/*
182 	 * Override the connection key, if not NULL.  This affects the
183 	 * behaviour of npf_conn_lookup() and npf_conn_establish().
184 	 * Note: npc_ckey is of npf_connkey_t type.
185 	 */
186 	const void *		npc_ckey;
187 } npf_cache_t;
188 
189 static inline bool
190 npf_iscached(const npf_cache_t *npc, const int inf)
191 {
192 	KASSERT(npc->npc_nbuf != NULL);
193 	return __predict_true((npc->npc_info & inf) != 0);
194 }
195 
196 /*
197  * Misc.
198  */
199 
200 bool		npf_autounload_p(void);
201 
202 #endif	/* _KERNEL */
203 
204 #define	NPF_SRC		0
205 #define	NPF_DST		1
206 
207 /* Rule attributes. */
208 #define	NPF_RULE_PASS			0x00000001
209 #define	NPF_RULE_GROUP			0x00000002
210 #define	NPF_RULE_FINAL			0x00000004
211 #define	NPF_RULE_STATEFUL		0x00000008
212 #define	NPF_RULE_RETRST			0x00000010
213 #define	NPF_RULE_RETICMP		0x00000020
214 #define	NPF_RULE_DYNAMIC		0x00000040
215 #define	NPF_RULE_GSTATEFUL		0x00000080
216 
217 #define	NPF_DYNAMIC_GROUP		(NPF_RULE_GROUP | NPF_RULE_DYNAMIC)
218 
219 #define	NPF_RULE_IN			0x10000000
220 #define	NPF_RULE_OUT			0x20000000
221 #define	NPF_RULE_DIMASK			(NPF_RULE_IN | NPF_RULE_OUT)
222 #define	NPF_RULE_FORW			0x40000000
223 
224 /* Private range of rule attributes (not public and should not be set). */
225 #define	NPF_RULE_PRIVMASK		0x0f000000
226 
227 #define	NPF_RULE_MAXNAMELEN		64
228 #define	NPF_RULE_MAXKEYLEN		32
229 
230 /* Priority values. */
231 #define	NPF_PRI_FIRST			(-2)
232 #define	NPF_PRI_LAST			(-1)
233 
234 /* Types of code. */
235 #define	NPF_CODE_BPF			1
236 
237 /* Address translation types and flags. */
238 #define	NPF_NATIN			1
239 #define	NPF_NATOUT			2
240 
241 #define	NPF_NAT_PORTS			0x01
242 #define	NPF_NAT_PORTMAP			0x02
243 #define	NPF_NAT_STATIC			0x04
244 
245 #define	NPF_NAT_PRIVMASK		0x0f000000
246 
247 #define	NPF_ALGO_NONE			0
248 #define	NPF_ALGO_NETMAP			1
249 #define	NPF_ALGO_IPHASH			2
250 #define	NPF_ALGO_RR			3
251 #define	NPF_ALGO_NPT66			4
252 
253 /* Table types. */
254 #define	NPF_TABLE_IPSET			1
255 #define	NPF_TABLE_LPM			2
256 #define	NPF_TABLE_CONST			3
257 #define	NPF_TABLE_IFADDR		4
258 
259 #define	NPF_TABLE_MAXNAMELEN		32
260 
261 /* Layers. */
262 #define	NPF_LAYER_2			2
263 #define	NPF_LAYER_3			3
264 
265 /*
266  * Flags passed via nbuf tags.
267  */
268 #define	NPF_NTAG_PASS			0x0001
269 
270 /*
271  * Rule commands (non-ioctl).
272  */
273 
274 #define	NPF_CMD_RULE_ADD		1
275 #define	NPF_CMD_RULE_INSERT		2
276 #define	NPF_CMD_RULE_REMOVE		3
277 #define	NPF_CMD_RULE_REMKEY		4
278 #define	NPF_CMD_RULE_LIST		5
279 #define	NPF_CMD_RULE_FLUSH		6
280 
281 /*
282  * NPF ioctl(2): table commands and structures.
283  */
284 
285 #define	NPF_CMD_TABLE_LOOKUP		1
286 #define	NPF_CMD_TABLE_ADD		2
287 #define	NPF_CMD_TABLE_REMOVE		3
288 #define	NPF_CMD_TABLE_LIST		4
289 #define	NPF_CMD_TABLE_FLUSH		5
290 
291 typedef struct npf_ioctl_ent {
292 	int			alen;
293 	npf_addr_t		addr;
294 	npf_netmask_t		mask;
295 } npf_ioctl_ent_t;
296 
297 typedef struct npf_ioctl_buf {
298 	void *			buf;
299 	size_t			len;
300 } npf_ioctl_buf_t;
301 
302 typedef struct npf_ioctl_table {
303 	int			nct_cmd;
304 	const char *		nct_name;
305 	union {
306 		npf_ioctl_ent_t	ent;
307 		npf_ioctl_buf_t	buf;
308 	} nct_data;
309 } npf_ioctl_table_t;
310 
311 /*
312  * IOCTL operations.
313  */
314 
315 #define	IOC_NPF_VERSION		_IOR('N', 100, int)
316 #define	IOC_NPF_SWITCH		_IOW('N', 101, int)
317 #define	IOC_NPF_LOAD		_IOWR('N', 102, nvlist_ref_t)
318 #define	IOC_NPF_TABLE		_IOW('N', 103, struct npf_ioctl_table)
319 #define	IOC_NPF_STATS		_IOW('N', 104, void *)
320 #define	IOC_NPF_SAVE		_IOR('N', 105, nvlist_ref_t)
321 #define	IOC_NPF_RULE		_IOWR('N', 107, nvlist_ref_t)
322 #define	IOC_NPF_CONN_LOOKUP	_IOWR('N', 108, nvlist_ref_t)
323 #define	IOC_NPF_TABLE_REPLACE	_IOWR('N', 109, nvlist_ref_t)
324 
325 /*
326  * NPF error report.
327  */
328 
329 typedef struct {
330 	int64_t		id;
331 	char *		error_msg;
332 	char *		source_file;
333 	unsigned	source_line;
334 } npf_error_t;
335 
336 /*
337  * Statistics counters.
338  */
339 
340 typedef enum {
341 	/* Packets passed. */
342 	NPF_STAT_PASS_DEFAULT,
343 	NPF_STAT_PASS_RULESET,
344 	NPF_STAT_PASS_CONN,
345 	/* Packets blocked. */
346 	NPF_STAT_BLOCK_DEFAULT,
347 	NPF_STAT_BLOCK_RULESET,
348 	/* Connection and NAT entries. */
349 	NPF_STAT_CONN_CREATE,
350 	NPF_STAT_CONN_DESTROY,
351 	NPF_STAT_NAT_CREATE,
352 	NPF_STAT_NAT_DESTROY,
353 	/* Invalid state cases. */
354 	NPF_STAT_INVALID_STATE,
355 	NPF_STAT_INVALID_STATE_TCP1,
356 	NPF_STAT_INVALID_STATE_TCP2,
357 	NPF_STAT_INVALID_STATE_TCP3,
358 	/* Raced packets. */
359 	NPF_STAT_RACE_CONN,
360 	NPF_STAT_RACE_NAT,
361 	/* Fragments. */
362 	NPF_STAT_FRAGMENTS,
363 	NPF_STAT_REASSEMBLY,
364 	NPF_STAT_REASSFAIL,
365 	/* Other errors. */
366 	NPF_STAT_ERROR,
367 	/* nbuf non-contiguous cases. */
368 	NPF_STAT_NBUF_NONCONTIG,
369 	NPF_STAT_NBUF_CONTIG_FAIL,
370 	/* Count (last). */
371 	NPF_STATS_COUNT
372 } npf_stats_t;
373 
374 #define	NPF_STATS_SIZE		(sizeof(uint64_t) * NPF_STATS_COUNT)
375 
376 #endif	/* _NPF_NET_H_ */
377