xref: /freebsd-11-stable/sys/netpfil/ipfw/ip_fw_private.h (revision df69b3666ccf5e9f019ae0de8c93c2849b5b8225)
1 /*-
2  * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #ifndef _IPFW2_PRIVATE_H
29 #define _IPFW2_PRIVATE_H
30 
31 /*
32  * Internal constants and data structures used by ipfw components
33  * and not meant to be exported outside the kernel.
34  */
35 
36 #ifdef _KERNEL
37 
38 /*
39  * For platforms that do not have SYSCTL support, we wrap the
40  * SYSCTL_* into a function (one per file) to collect the values
41  * into an array at module initialization. The wrapping macros,
42  * SYSBEGIN() and SYSEND, are empty in the default case.
43  */
44 #ifndef SYSBEGIN
45 #define SYSBEGIN(x)
46 #endif
47 #ifndef SYSEND
48 #define SYSEND
49 #endif
50 
51 /* Return values from ipfw_chk() */
52 enum {
53 	IP_FW_PASS = 0,
54 	IP_FW_DENY,
55 	IP_FW_DIVERT,
56 	IP_FW_TEE,
57 	IP_FW_DUMMYNET,
58 	IP_FW_NETGRAPH,
59 	IP_FW_NGTEE,
60 	IP_FW_NAT,
61 	IP_FW_REASS,
62 	IP_FW_NAT64,
63 };
64 
65 /*
66  * Structure for collecting parameters to dummynet for ip6_output forwarding
67  */
68 struct _ip6dn_args {
69        struct ip6_pktopts *opt_or;
70        int flags_or;
71        struct ip6_moptions *im6o_or;
72        struct ifnet *origifp_or;
73        struct ifnet *ifp_or;
74        struct sockaddr_in6 dst_or;
75        u_long mtu_or;
76 };
77 
78 
79 /*
80  * Arguments for calling ipfw_chk() and dummynet_io(). We put them
81  * all into a structure because this way it is easier and more
82  * efficient to pass variables around and extend the interface.
83  */
84 struct ip_fw_args {
85 	uint32_t		flags;
86 #define	IPFW_ARGS_ETHER		0x0001	/* has valid ethernet header	*/
87 #define	IPFW_ARGS_NH4		0x0002	/* has IPv4 next hop in hopstore */
88 #define	IPFW_ARGS_NH6		0x0004	/* has IPv6 next hop in hopstore */
89 #define	IPFW_ARGS_NH4PTR	0x0008	/* has IPv4 next hop in next_hop */
90 #define	IPFW_ARGS_NH6PTR	0x0010	/* has IPv6 next hop in next_hop6 */
91 #define	IPFW_ARGS_REF		0x0020	/* has valid ipfw_rule_ref	*/
92 	/*
93 	 * On return, it points to the matching rule.
94 	 * On entry, rule.slot > 0 means the info is valid and
95 	 * contains the starting rule for an ipfw search.
96 	 * If chain_id == chain->id && slot >0 then jump to that slot.
97 	 * Otherwise, we locate the first rule >= rulenum:rule_id
98 	 */
99 	struct ipfw_rule_ref	rule;	/* match/restart info		*/
100 
101 	struct ifnet		*oif;	/* output interface		*/
102 	struct inpcb		*inp;
103 	union {
104 		/*
105 		 * We don't support forwarding on layer2, thus we can
106 		 * keep eh pointer in this union.
107 		 * next_hop[6] pointers can be used to point to next hop
108 		 * stored in rule's opcode to avoid copying into hopstore.
109 		 * Also, it is expected that all 0x1-0x10 flags are mutually
110 		 * exclusive.
111 		 */
112 		struct ether_header	*eh;	/* for bridged packets	*/
113 		struct sockaddr_in	*next_hop;
114 		struct sockaddr_in6	*next_hop6;
115 		/* ipfw next hop storage */
116 		struct sockaddr_in	hopstore;
117 		struct ip_fw_nh6 {
118 			struct in6_addr sin6_addr;
119 			uint32_t	sin6_scope_id;
120 			uint16_t	sin6_port;
121 		} hopstore6;
122 	};
123 
124 	struct mbuf		*m;	/* the mbuf chain		*/
125 	struct ipfw_flow_id	f_id;	/* grabbed from IP header	*/
126 };
127 
128 MALLOC_DECLARE(M_IPFW);
129 
130 /*
131  * Hooks sometime need to know the direction of the packet
132  * (divert, dummynet, netgraph, ...)
133  * We use a generic definition here, with bit0-1 indicating the
134  * direction, bit 2 indicating layer2 or 3, bit 3-4 indicating the
135  * specific protocol
136  * indicating the protocol (if necessary)
137  */
138 enum {
139 	DIR_MASK =	0x3,
140 	DIR_OUT =	0,
141 	DIR_IN =	1,
142 	DIR_FWD =	2,
143 	DIR_DROP =	3,
144 	PROTO_LAYER2 =	0x4, /* set for layer 2 */
145 	/* PROTO_DEFAULT = 0, */
146 	PROTO_IPV4 =	0x08,
147 	PROTO_IPV6 =	0x10,
148 	PROTO_IFB =	0x0c, /* layer2 + ifbridge */
149    /*	PROTO_OLDBDG =	0x14, unused, old bridge */
150 };
151 
152 /* wrapper for freeing a packet, in case we need to do more work */
153 #ifndef FREE_PKT
154 #if defined(__linux__) || defined(_WIN32)
155 #define FREE_PKT(m)	netisr_dispatch(-1, m)
156 #else
157 #define FREE_PKT(m)	m_freem(m)
158 #endif
159 #endif /* !FREE_PKT */
160 
161 /*
162  * Function definitions.
163  */
164 int ipfw_chk(struct ip_fw_args *args);
165 struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *,
166     u_int32_t, u_int32_t, int);
167 
168 /* attach (arg = 1) or detach (arg = 0) hooks */
169 int ipfw_attach_hooks(int);
170 #ifdef NOTYET
171 void ipfw_nat_destroy(void);
172 #endif
173 
174 /* In ip_fw_log.c */
175 struct ip;
176 struct ip_fw_chain;
177 
178 void ipfw_bpf_init(int);
179 void ipfw_bpf_uninit(int);
180 void ipfw_bpf_mtap2(void *, u_int, struct mbuf *);
181 void ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen,
182     struct ip_fw_args *args, struct mbuf *m, struct ifnet *oif,
183     u_short offset, uint32_t tablearg, struct ip *ip);
184 VNET_DECLARE(u_int64_t, norule_counter);
185 #define	V_norule_counter	VNET(norule_counter)
186 VNET_DECLARE(int, verbose_limit);
187 #define	V_verbose_limit		VNET(verbose_limit)
188 
189 /* In ip_fw_dynamic.c */
190 struct sockopt_data;
191 
192 enum { /* result for matching dynamic rules */
193 	MATCH_REVERSE = 0,
194 	MATCH_FORWARD,
195 	MATCH_NONE,
196 	MATCH_UNKNOWN,
197 };
198 
199 /*
200  * Macro to determine that we need to do or redo dynamic state lookup.
201  * direction == MATCH_UNKNOWN means that this is first lookup, then we need
202  * to do lookup.
203  * Otherwise check the state name, if previous lookup was for "any" name,
204  * this means there is no state with specific name. Thus no need to do
205  * lookup. If previous name was not "any", redo lookup for specific name.
206  */
207 #define	DYN_LOOKUP_NEEDED(p, cmd)	\
208     ((p)->direction == MATCH_UNKNOWN ||	\
209 	((p)->kidx != 0 && (p)->kidx != (cmd)->arg1))
210 #define	DYN_INFO_INIT(p)	do {	\
211 	(p)->direction = MATCH_UNKNOWN;	\
212 	(p)->kidx = 0;			\
213 } while (0)
214 struct ipfw_dyn_info {
215 	uint16_t	direction;	/* match direction */
216 	uint16_t	kidx;		/* state name kidx */
217 	uint32_t	hashval;	/* hash value */
218 	uint32_t	version;	/* bucket version */
219 	uint32_t	f_pos;
220 };
221 int ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule,
222     const ipfw_insn_limit *cmd, const struct ip_fw_args *args,
223     const void *ulp, int pktlen, struct ipfw_dyn_info *info,
224     uint32_t tablearg);
225 struct ip_fw *ipfw_dyn_lookup_state(const struct ip_fw_args *args,
226     const void *ulp, int pktlen, const ipfw_insn *cmd,
227     struct ipfw_dyn_info *info);
228 
229 int ipfw_is_dyn_rule(struct ip_fw *rule);
230 void ipfw_expire_dyn_states(struct ip_fw_chain *, ipfw_range_tlv *);
231 void ipfw_get_dynamic(struct ip_fw_chain *chain, char **bp, const char *ep);
232 int ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd);
233 
234 void ipfw_dyn_init(struct ip_fw_chain *);	/* per-vnet initialization */
235 void ipfw_dyn_uninit(int);	/* per-vnet deinitialization */
236 int ipfw_dyn_len(void);
237 uint32_t ipfw_dyn_get_count(uint32_t *, int *);
238 void ipfw_dyn_reset_eaction(struct ip_fw_chain *ch, uint16_t eaction_id,
239     uint16_t default_id, uint16_t instance_id);
240 
241 /* common variables */
242 VNET_DECLARE(int, fw_one_pass);
243 #define	V_fw_one_pass		VNET(fw_one_pass)
244 
245 VNET_DECLARE(int, fw_verbose);
246 #define	V_fw_verbose		VNET(fw_verbose)
247 
248 VNET_DECLARE(struct ip_fw_chain, layer3_chain);
249 #define	V_layer3_chain		VNET(layer3_chain)
250 
251 VNET_DECLARE(int, ipfw_vnet_ready);
252 #define	V_ipfw_vnet_ready	VNET(ipfw_vnet_ready)
253 
254 VNET_DECLARE(u_int32_t, set_disable);
255 #define	V_set_disable		VNET(set_disable)
256 
257 VNET_DECLARE(int, autoinc_step);
258 #define V_autoinc_step		VNET(autoinc_step)
259 
260 VNET_DECLARE(unsigned int, fw_tables_max);
261 #define V_fw_tables_max		VNET(fw_tables_max)
262 
263 VNET_DECLARE(unsigned int, fw_tables_sets);
264 #define V_fw_tables_sets	VNET(fw_tables_sets)
265 
266 struct tables_config;
267 
268 #ifdef _KERNEL
269 /*
270  * Here we have the structure representing an ipfw rule.
271  *
272  * It starts with a general area
273  * followed by an array of one or more instructions, which the code
274  * accesses as an array of 32-bit values.
275  *
276  * Given a rule pointer  r:
277  *
278  *  r->cmd		is the start of the first instruction.
279  *  ACTION_PTR(r)	is the start of the first action (things to do
280  *			once a rule matched).
281  */
282 
283 struct ip_fw {
284 	uint16_t	act_ofs;	/* offset of action in 32-bit units */
285 	uint16_t	cmd_len;	/* # of 32-bit words in cmd	*/
286 	uint16_t	rulenum;	/* rule number			*/
287 	uint8_t		set;		/* rule set (0..31)		*/
288 	uint8_t		flags;		/* currently unused		*/
289 	counter_u64_t	cntr;		/* Pointer to rule counters	*/
290 	uint32_t	timestamp;	/* tv_sec of last match		*/
291 	uint32_t	id;		/* rule id			*/
292 	uint32_t	cached_id;	/* used by jump_fast		*/
293 	uint32_t	cached_pos;	/* used by jump_fast		*/
294 	uint32_t	refcnt;		/* number of references		*/
295 
296 	struct ip_fw	*next;		/* linked list of deleted rules */
297 	ipfw_insn	cmd[1];		/* storage for commands		*/
298 };
299 
300 #define	IPFW_RULE_CNTR_SIZE	(2 * sizeof(uint64_t))
301 
302 #endif
303 
304 struct ip_fw_chain {
305 	struct ip_fw	**map;		/* array of rule ptrs to ease lookup */
306 	uint32_t	id;		/* ruleset id */
307 	int		n_rules;	/* number of static rules */
308 	void		*tablestate;	/* runtime table info */
309 	void		*valuestate;	/* runtime table value info */
310 	int		*idxmap;	/* skipto array of rules */
311 	void		**srvstate;	/* runtime service mappings */
312 #if defined( __linux__ ) || defined( _WIN32 )
313 	spinlock_t rwmtx;
314 #endif
315 	int		static_len;	/* total len of static rules (v0) */
316 	uint32_t	gencnt;		/* NAT generation count */
317 	LIST_HEAD(nat_list, cfg_nat) nat;       /* list of nat entries */
318 	struct ip_fw	*default_rule;
319 	struct tables_config *tblcfg;	/* tables module data */
320 	void		*ifcfg;		/* interface module data */
321 	int		*idxmap_back;	/* standby skipto array of rules */
322 	struct namedobj_instance	*srvmap; /* cfg name->number mappings */
323 #if defined( __linux__ ) || defined( _WIN32 )
324 	spinlock_t uh_lock;
325 #else
326 	struct rwlock	uh_lock;	/* lock for upper half */
327 #endif
328 };
329 
330 /* 64-byte structure representing multi-field table value */
331 struct table_value {
332 	uint32_t	tag;		/* O_TAG/O_TAGGED */
333 	uint32_t	pipe;		/* O_PIPE/O_QUEUE */
334 	uint16_t	divert;		/* O_DIVERT/O_TEE */
335 	uint16_t	skipto;		/* skipto, CALLRET */
336 	uint32_t	netgraph;	/* O_NETGRAPH/O_NGTEE */
337 	uint32_t	fib;		/* O_SETFIB */
338 	uint32_t	nat;		/* O_NAT */
339 	uint32_t	nh4;
340 	uint8_t		dscp;
341 	uint8_t		spare0;
342 	uint16_t	spare1;
343 	/* -- 32 bytes -- */
344 	struct in6_addr	nh6;
345 	uint32_t	limit;		/* O_LIMIT */
346 	uint32_t	zoneid;		/* scope zone id for nh6 */
347 	uint64_t	refcnt;		/* Number of references */
348 };
349 
350 
351 struct named_object {
352 	TAILQ_ENTRY(named_object)	nn_next;	/* namehash */
353 	TAILQ_ENTRY(named_object)	nv_next;	/* valuehash */
354 	char			*name;	/* object name */
355 	uint16_t		etlv;	/* Export TLV id */
356 	uint8_t			subtype;/* object subtype within class */
357 	uint8_t			set;	/* set object belongs to */
358 	uint16_t		kidx;	/* object kernel index */
359 	uint16_t		spare;
360 	uint32_t		ocnt;	/* object counter for internal use */
361 	uint32_t		refcnt;	/* number of references */
362 };
363 TAILQ_HEAD(namedobjects_head, named_object);
364 
365 struct sockopt;	/* used by tcp_var.h */
366 struct sockopt_data {
367 	caddr_t		kbuf;		/* allocated buffer */
368 	size_t		ksize;		/* given buffer size */
369 	size_t		koff;		/* data already used */
370 	size_t		kavail;		/* number of bytes available */
371 	size_t		ktotal;		/* total bytes pushed */
372 	struct sockopt	*sopt;		/* socket data */
373 	caddr_t		sopt_val;	/* sopt user buffer */
374 	size_t		valsize;	/* original data size */
375 };
376 
377 struct ipfw_ifc;
378 
379 typedef void (ipfw_ifc_cb)(struct ip_fw_chain *ch, void *cbdata,
380     uint16_t ifindex);
381 
382 struct ipfw_iface {
383 	struct named_object	no;
384 	char ifname[64];
385 	int resolved;
386 	uint16_t ifindex;
387 	uint16_t spare;
388 	uint64_t gencnt;
389 	TAILQ_HEAD(, ipfw_ifc)	consumers;
390 };
391 
392 struct ipfw_ifc {
393 	TAILQ_ENTRY(ipfw_ifc)	next;
394 	struct ipfw_iface	*iface;
395 	ipfw_ifc_cb		*cb;
396 	void			*cbdata;
397 };
398 
399 /* Macro for working with various counters */
400 #define	IPFW_INC_RULE_COUNTER(_cntr, _bytes)	do {	\
401 	counter_u64_add((_cntr)->cntr, 1);		\
402 	counter_u64_add((_cntr)->cntr + 1, _bytes);	\
403 	if ((_cntr)->timestamp != time_uptime)		\
404 		(_cntr)->timestamp = time_uptime;	\
405 	} while (0)
406 
407 #define	IPFW_INC_DYN_COUNTER(_cntr, _bytes)	do {		\
408 	(_cntr)->pcnt++;				\
409 	(_cntr)->bcnt += _bytes;			\
410 	} while (0)
411 
412 #define	IPFW_ZERO_RULE_COUNTER(_cntr) do {		\
413 	counter_u64_zero((_cntr)->cntr);		\
414 	counter_u64_zero((_cntr)->cntr + 1);		\
415 	(_cntr)->timestamp = 0;				\
416 	} while (0)
417 
418 #define	IPFW_ZERO_DYN_COUNTER(_cntr) do {		\
419 	(_cntr)->pcnt = 0;				\
420 	(_cntr)->bcnt = 0;				\
421 	} while (0)
422 
423 #define	TARG_VAL(ch, k, f)	((struct table_value *)((ch)->valuestate))[k].f
424 #define	IP_FW_ARG_TABLEARG(ch, a, f)	\
425 	(((a) == IP_FW_TARG) ? TARG_VAL(ch, tablearg, f) : (a))
426 /*
427  * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c
428  * so the variable and the macros must be here.
429  */
430 
431 #if defined( __linux__ ) || defined( _WIN32 )
432 #define	IPFW_LOCK_INIT(_chain) do {			\
433 	rw_init(&(_chain)->rwmtx, "IPFW static rules");	\
434 	rw_init(&(_chain)->uh_lock, "IPFW UH lock");	\
435 	} while (0)
436 
437 #define	IPFW_LOCK_DESTROY(_chain) do {			\
438 	rw_destroy(&(_chain)->rwmtx);			\
439 	rw_destroy(&(_chain)->uh_lock);			\
440 	} while (0)
441 
442 #define	IPFW_RLOCK_ASSERT(_chain)	rw_assert(&(_chain)->rwmtx, RA_RLOCKED)
443 #define	IPFW_WLOCK_ASSERT(_chain)	rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
444 
445 #define	IPFW_RLOCK_TRACKER
446 #define	IPFW_RLOCK(p)			rw_rlock(&(p)->rwmtx)
447 #define	IPFW_RUNLOCK(p)			rw_runlock(&(p)->rwmtx)
448 #define	IPFW_WLOCK(p)			rw_wlock(&(p)->rwmtx)
449 #define	IPFW_WUNLOCK(p)			rw_wunlock(&(p)->rwmtx)
450 #define	IPFW_PF_RLOCK(p)		IPFW_RLOCK(p)
451 #define	IPFW_PF_RUNLOCK(p)		IPFW_RUNLOCK(p)
452 #else /* FreeBSD */
453 #define	IPFW_LOCK_INIT(_chain) do {			\
454 	rw_init(&(_chain)->uh_lock, "IPFW UH lock");	\
455 	} while (0)
456 
457 #define	IPFW_LOCK_DESTROY(_chain) do {			\
458 	rw_destroy(&(_chain)->uh_lock);			\
459 	} while (0)
460 
461 #define	IPFW_RLOCK_ASSERT(_chain)	rm_assert(&V_pfil_lock, RA_RLOCKED)
462 #define	IPFW_WLOCK_ASSERT(_chain)	rm_assert(&V_pfil_lock, RA_WLOCKED)
463 
464 #define	IPFW_RLOCK_TRACKER		struct rm_priotracker _tracker
465 #define	IPFW_RLOCK(p)			rm_rlock(&V_pfil_lock, &_tracker)
466 #define	IPFW_RUNLOCK(p)			rm_runlock(&V_pfil_lock, &_tracker)
467 #define	IPFW_WLOCK(p)			rm_wlock(&V_pfil_lock)
468 #define	IPFW_WUNLOCK(p)			rm_wunlock(&V_pfil_lock)
469 #define	IPFW_PF_RLOCK(p)
470 #define	IPFW_PF_RUNLOCK(p)
471 #endif
472 
473 #define	IPFW_UH_RLOCK_ASSERT(_chain)	rw_assert(&(_chain)->uh_lock, RA_RLOCKED)
474 #define	IPFW_UH_WLOCK_ASSERT(_chain)	rw_assert(&(_chain)->uh_lock, RA_WLOCKED)
475 #define	IPFW_UH_UNLOCK_ASSERT(_chain)	rw_assert(&(_chain)->uh_lock, RA_UNLOCKED)
476 
477 #define IPFW_UH_RLOCK(p) rw_rlock(&(p)->uh_lock)
478 #define IPFW_UH_RUNLOCK(p) rw_runlock(&(p)->uh_lock)
479 #define IPFW_UH_WLOCK(p) rw_wlock(&(p)->uh_lock)
480 #define IPFW_UH_WUNLOCK(p) rw_wunlock(&(p)->uh_lock)
481 
482 struct obj_idx {
483 	uint16_t	uidx;	/* internal index supplied by userland */
484 	uint16_t	kidx;	/* kernel object index */
485 	uint16_t	off;	/* tlv offset from rule end in 4-byte words */
486 	uint8_t		spare;
487 	uint8_t		type;	/* object type within its category */
488 };
489 
490 struct rule_check_info {
491 	uint16_t	flags;		/* rule-specific check flags */
492 	uint16_t	object_opcodes;	/* num of opcodes referencing objects */
493 	uint16_t	urule_numoff;	/* offset of rulenum in bytes */
494 	uint8_t		version;	/* rule version */
495 	uint8_t		spare;
496 	ipfw_obj_ctlv	*ctlv;		/* name TLV containter */
497 	struct ip_fw	*krule;		/* resulting rule pointer */
498 	caddr_t		urule;		/* original rule pointer */
499 	struct obj_idx	obuf[8];	/* table references storage */
500 };
501 
502 /* Legacy interface support */
503 /*
504  * FreeBSD 8 export rule format
505  */
506 struct ip_fw_rule0 {
507 	struct ip_fw	*x_next;	/* linked list of rules		*/
508 	struct ip_fw	*next_rule;	/* ptr to next [skipto] rule	*/
509 	/* 'next_rule' is used to pass up 'set_disable' status		*/
510 
511 	uint16_t	act_ofs;	/* offset of action in 32-bit units */
512 	uint16_t	cmd_len;	/* # of 32-bit words in cmd	*/
513 	uint16_t	rulenum;	/* rule number			*/
514 	uint8_t		set;		/* rule set (0..31)		*/
515 	uint8_t		_pad;		/* padding			*/
516 	uint32_t	id;		/* rule id */
517 
518 	/* These fields are present in all rules.			*/
519 	uint64_t	pcnt;		/* Packet counter		*/
520 	uint64_t	bcnt;		/* Byte counter			*/
521 	uint32_t	timestamp;	/* tv_sec of last match		*/
522 
523 	ipfw_insn	cmd[1];		/* storage for commands		*/
524 };
525 
526 struct ip_fw_bcounter0 {
527 	uint64_t	pcnt;		/* Packet counter		*/
528 	uint64_t	bcnt;		/* Byte counter			*/
529 	uint32_t	timestamp;	/* tv_sec of last match		*/
530 };
531 
532 /* Kernel rule length */
533 /*
534  * RULE _K_ SIZE _V_ ->
535  * get kernel size from userland rool version _V_.
536  * RULE _U_ SIZE _V_ ->
537  * get user size version _V_ from kernel rule
538  * RULESIZE _V_ ->
539  * get user size rule length
540  */
541 /* FreeBSD8 <> current kernel format */
542 #define	RULEUSIZE0(r)	(sizeof(struct ip_fw_rule0) + (r)->cmd_len * 4 - 4)
543 #define	RULEKSIZE0(r)	roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
544 /* FreeBSD11 <> current kernel format */
545 #define	RULEUSIZE1(r)	(roundup2(sizeof(struct ip_fw_rule) + \
546     (r)->cmd_len * 4 - 4, 8))
547 #define	RULEKSIZE1(r)	roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
548 
549 /*
550  * Tables/Objects index rewriting code
551  */
552 
553 /* Default and maximum number of ipfw tables/objects. */
554 #define	IPFW_TABLES_MAX		65536
555 #define	IPFW_TABLES_DEFAULT	128
556 #define	IPFW_OBJECTS_MAX	65536
557 #define	IPFW_OBJECTS_DEFAULT	1024
558 
559 #define	CHAIN_TO_SRV(ch)	((ch)->srvmap)
560 #define	SRV_OBJECT(ch, idx)	((ch)->srvstate[(idx)])
561 
562 struct tid_info {
563 	uint32_t	set;	/* table set */
564 	uint16_t	uidx;	/* table index */
565 	uint8_t		type;	/* table type */
566 	uint8_t		atype;
567 	uint8_t		spare;
568 	int		tlen;	/* Total TLV size block */
569 	void		*tlvs;	/* Pointer to first TLV */
570 };
571 
572 /*
573  * Classifier callback. Checks if @cmd opcode contains kernel object reference.
574  * If true, returns its index and type.
575  * Returns 0 if match is found, 1 overwise.
576  */
577 typedef int (ipfw_obj_rw_cl)(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype);
578 /*
579  * Updater callback. Sets kernel object reference index to @puidx
580  */
581 typedef void (ipfw_obj_rw_upd)(ipfw_insn *cmd, uint16_t puidx);
582 /*
583  * Finder callback. Tries to find named object by name (specified via @ti).
584  * Stores found named object pointer in @pno.
585  * If object was not found, NULL is stored.
586  *
587  * Return 0 if input data was valid.
588  */
589 typedef int (ipfw_obj_fname_cb)(struct ip_fw_chain *ch,
590     struct tid_info *ti, struct named_object **pno);
591 /*
592  * Another finder callback. Tries to findex named object by kernel index.
593  *
594  * Returns pointer to named object or NULL.
595  */
596 typedef struct named_object *(ipfw_obj_fidx_cb)(struct ip_fw_chain *ch,
597     uint16_t kidx);
598 /*
599  * Object creator callback. Tries to create object specified by @ti.
600  * Stores newly-allocated object index in @pkidx.
601  *
602  * Returns 0 on success.
603  */
604 typedef int (ipfw_obj_create_cb)(struct ip_fw_chain *ch, struct tid_info *ti,
605     uint16_t *pkidx);
606 /*
607  * Object destroy callback. Intended to free resources allocated by
608  * create_object callback.
609  */
610 typedef void (ipfw_obj_destroy_cb)(struct ip_fw_chain *ch,
611     struct named_object *no);
612 /*
613  * Sets handler callback. Handles moving and swaping set of named object.
614  *  SWAP_ALL moves all named objects from set `set' to `new_set' and vise versa;
615  *  TEST_ALL checks that there aren't any named object with conflicting names;
616  *  MOVE_ALL moves all named objects from set `set' to `new_set';
617  *  COUNT_ONE used to count number of references used by object with kidx `set';
618  *  TEST_ONE checks that named object with kidx `set' can be moved to `new_set`;
619  *  MOVE_ONE moves named object with kidx `set' to set `new_set'.
620  */
621 enum ipfw_sets_cmd {
622 	SWAP_ALL = 0, TEST_ALL, MOVE_ALL, COUNT_ONE, TEST_ONE, MOVE_ONE
623 };
624 typedef int (ipfw_obj_sets_cb)(struct ip_fw_chain *ch,
625     uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
626 
627 
628 struct opcode_obj_rewrite {
629 	uint32_t		opcode;		/* Opcode to act upon */
630 	uint32_t		etlv;		/* Relevant export TLV id  */
631 	ipfw_obj_rw_cl		*classifier;	/* Check if rewrite is needed */
632 	ipfw_obj_rw_upd		*update;	/* update cmd with new value */
633 	ipfw_obj_fname_cb	*find_byname;	/* Find named object by name */
634 	ipfw_obj_fidx_cb	*find_bykidx;	/* Find named object by kidx */
635 	ipfw_obj_create_cb	*create_object;	/* Create named object */
636 	ipfw_obj_destroy_cb	*destroy_object;/* Destroy named object */
637 	ipfw_obj_sets_cb	*manage_sets;	/* Swap or move sets */
638 };
639 
640 #define	IPFW_ADD_OBJ_REWRITER(f, c)	do {	\
641 	if ((f) != 0) 				\
642 		ipfw_add_obj_rewriter(c,	\
643 		    sizeof(c) / sizeof(c[0]));	\
644 	} while(0)
645 #define	IPFW_DEL_OBJ_REWRITER(l, c)	do {	\
646 	if ((l) != 0) 				\
647 		ipfw_del_obj_rewriter(c,	\
648 		    sizeof(c) / sizeof(c[0]));	\
649 	} while(0)
650 
651 /* In ip_fw_iface.c */
652 int ipfw_iface_init(void);
653 void ipfw_iface_destroy(void);
654 void vnet_ipfw_iface_destroy(struct ip_fw_chain *ch);
655 int ipfw_iface_ref(struct ip_fw_chain *ch, char *name,
656     struct ipfw_ifc *ic);
657 void ipfw_iface_unref(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
658 void ipfw_iface_add_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
659 void ipfw_iface_del_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
660 
661 /* In ip_fw_sockopt.c */
662 void ipfw_init_skipto_cache(struct ip_fw_chain *chain);
663 void ipfw_destroy_skipto_cache(struct ip_fw_chain *chain);
664 int ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id);
665 int ipfw_ctl3(struct sockopt *sopt);
666 int ipfw_add_protected_rule(struct ip_fw_chain *chain, struct ip_fw *rule,
667     int locked);
668 void ipfw_reap_add(struct ip_fw_chain *chain, struct ip_fw **head,
669     struct ip_fw *rule);
670 void ipfw_reap_rules(struct ip_fw *head);
671 void ipfw_init_counters(void);
672 void ipfw_destroy_counters(void);
673 struct ip_fw *ipfw_alloc_rule(struct ip_fw_chain *chain, size_t rulesize);
674 void ipfw_free_rule(struct ip_fw *rule);
675 int ipfw_match_range(struct ip_fw *rule, ipfw_range_tlv *rt);
676 int ipfw_mark_object_kidx(uint32_t *bmask, uint16_t etlv, uint16_t kidx);
677 ipfw_insn *ipfw_get_action(struct ip_fw *);
678 
679 typedef int (sopt_handler_f)(struct ip_fw_chain *ch,
680     ip_fw3_opheader *op3, struct sockopt_data *sd);
681 struct ipfw_sopt_handler {
682 	uint16_t	opcode;
683 	uint8_t		version;
684 	uint8_t		dir;
685 	sopt_handler_f	*handler;
686 	uint64_t	refcnt;
687 };
688 #define	HDIR_SET	0x01	/* Handler is used to set some data */
689 #define	HDIR_GET	0x02	/* Handler is used to retrieve data */
690 #define	HDIR_BOTH	HDIR_GET|HDIR_SET
691 
692 void ipfw_init_sopt_handler(void);
693 void ipfw_destroy_sopt_handler(void);
694 void ipfw_add_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
695 int ipfw_del_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
696 caddr_t ipfw_get_sopt_space(struct sockopt_data *sd, size_t needed);
697 caddr_t ipfw_get_sopt_header(struct sockopt_data *sd, size_t needed);
698 #define	IPFW_ADD_SOPT_HANDLER(f, c)	do {	\
699 	if ((f) != 0) 				\
700 		ipfw_add_sopt_handler(c,	\
701 		    sizeof(c) / sizeof(c[0]));	\
702 	} while(0)
703 #define	IPFW_DEL_SOPT_HANDLER(l, c)	do {	\
704 	if ((l) != 0) 				\
705 		ipfw_del_sopt_handler(c,	\
706 		    sizeof(c) / sizeof(c[0]));	\
707 	} while(0)
708 
709 struct namedobj_instance;
710 typedef int (objhash_cb_t)(struct namedobj_instance *ni, struct named_object *,
711     void *arg);
712 typedef uint32_t (objhash_hash_f)(struct namedobj_instance *ni, const void *key,
713     uint32_t kopt);
714 typedef int (objhash_cmp_f)(struct named_object *no, const void *key,
715     uint32_t kopt);
716 struct namedobj_instance *ipfw_objhash_create(uint32_t items);
717 void ipfw_objhash_destroy(struct namedobj_instance *);
718 void ipfw_objhash_bitmap_alloc(uint32_t items, void **idx, int *pblocks);
719 void ipfw_objhash_bitmap_merge(struct namedobj_instance *ni,
720     void **idx, int *blocks);
721 void ipfw_objhash_bitmap_swap(struct namedobj_instance *ni,
722     void **idx, int *blocks);
723 void ipfw_objhash_bitmap_free(void *idx, int blocks);
724 void ipfw_objhash_set_hashf(struct namedobj_instance *ni, objhash_hash_f *f);
725 struct named_object *ipfw_objhash_lookup_name(struct namedobj_instance *ni,
726     uint32_t set, char *name);
727 struct named_object *ipfw_objhash_lookup_name_type(struct namedobj_instance *ni,
728     uint32_t set, uint32_t type, const char *name);
729 struct named_object *ipfw_objhash_lookup_kidx(struct namedobj_instance *ni,
730     uint16_t idx);
731 int ipfw_objhash_same_name(struct namedobj_instance *ni, struct named_object *a,
732     struct named_object *b);
733 void ipfw_objhash_add(struct namedobj_instance *ni, struct named_object *no);
734 void ipfw_objhash_del(struct namedobj_instance *ni, struct named_object *no);
735 uint32_t ipfw_objhash_count(struct namedobj_instance *ni);
736 uint32_t ipfw_objhash_count_type(struct namedobj_instance *ni, uint16_t type);
737 int ipfw_objhash_foreach(struct namedobj_instance *ni, objhash_cb_t *f,
738     void *arg);
739 int ipfw_objhash_foreach_type(struct namedobj_instance *ni, objhash_cb_t *f,
740     void *arg, uint16_t type);
741 int ipfw_objhash_free_idx(struct namedobj_instance *ni, uint16_t idx);
742 int ipfw_objhash_alloc_idx(void *n, uint16_t *pidx);
743 void ipfw_objhash_set_funcs(struct namedobj_instance *ni,
744     objhash_hash_f *hash_f, objhash_cmp_f *cmp_f);
745 int ipfw_objhash_find_type(struct namedobj_instance *ni, struct tid_info *ti,
746     uint32_t etlv, struct named_object **pno);
747 void ipfw_export_obj_ntlv(struct named_object *no, ipfw_obj_ntlv *ntlv);
748 ipfw_obj_ntlv *ipfw_find_name_tlv_type(void *tlvs, int len, uint16_t uidx,
749     uint32_t etlv);
750 void ipfw_init_obj_rewriter(void);
751 void ipfw_destroy_obj_rewriter(void);
752 void ipfw_add_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
753 int ipfw_del_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
754 
755 int create_objects_compat(struct ip_fw_chain *ch, ipfw_insn *cmd,
756     struct obj_idx *oib, struct obj_idx *pidx, struct tid_info *ti);
757 void update_opcode_kidx(ipfw_insn *cmd, uint16_t idx);
758 int classify_opcode_kidx(ipfw_insn *cmd, uint16_t *puidx);
759 void ipfw_init_srv(struct ip_fw_chain *ch);
760 void ipfw_destroy_srv(struct ip_fw_chain *ch);
761 int ipfw_check_object_name_generic(const char *name);
762 int ipfw_obj_manage_sets(struct namedobj_instance *ni, uint16_t type,
763     uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
764 
765 /* In ip_fw_eaction.c */
766 typedef int (ipfw_eaction_t)(struct ip_fw_chain *ch, struct ip_fw_args *args,
767     ipfw_insn *cmd, int *done);
768 int ipfw_eaction_init(struct ip_fw_chain *ch, int first);
769 void ipfw_eaction_uninit(struct ip_fw_chain *ch, int last);
770 
771 uint16_t ipfw_add_eaction(struct ip_fw_chain *ch, ipfw_eaction_t handler,
772     const char *name);
773 int ipfw_del_eaction(struct ip_fw_chain *ch, uint16_t eaction_id);
774 int ipfw_run_eaction(struct ip_fw_chain *ch, struct ip_fw_args *args,
775     ipfw_insn *cmd, int *done);
776 int ipfw_reset_eaction(struct ip_fw_chain *ch, struct ip_fw *rule,
777     uint16_t eaction_id, uint16_t default_id, uint16_t instance_id);
778 int ipfw_reset_eaction_instance(struct ip_fw_chain *ch, uint16_t eaction_id,
779     uint16_t instance_id);
780 
781 /* In ip_fw_table.c */
782 struct table_info;
783 
784 typedef int (table_lookup_t)(struct table_info *ti, void *key, uint32_t keylen,
785     uint32_t *val);
786 
787 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
788     void *paddr, uint32_t *val);
789 struct named_object *ipfw_objhash_lookup_table_kidx(struct ip_fw_chain *ch,
790     uint16_t kidx);
791 int ipfw_ref_table(struct ip_fw_chain *ch, ipfw_obj_ntlv *ntlv, uint16_t *kidx);
792 void ipfw_unref_table(struct ip_fw_chain *ch, uint16_t kidx);
793 int ipfw_init_tables(struct ip_fw_chain *ch, int first);
794 int ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables);
795 int ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int nsets);
796 void ipfw_destroy_tables(struct ip_fw_chain *ch, int last);
797 
798 /* In ip_fw_nat.c -- XXX to be moved to ip_var.h */
799 
800 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
801 
802 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
803 typedef int ipfw_nat_cfg_t(struct sockopt *);
804 
805 VNET_DECLARE(int, ipfw_nat_ready);
806 #define	V_ipfw_nat_ready	VNET(ipfw_nat_ready)
807 #define	IPFW_NAT_LOADED	(V_ipfw_nat_ready)
808 
809 extern ipfw_nat_t *ipfw_nat_ptr;
810 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
811 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
812 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
813 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
814 
815 /* Helper functions for IP checksum adjustment */
816 static __inline uint16_t
cksum_add(uint16_t sum,uint16_t a)817 cksum_add(uint16_t sum, uint16_t a)
818 {
819 	uint16_t res;
820 
821 	res = sum + a;
822 	return (res + (res < a));
823 }
824 
825 static __inline uint16_t
cksum_adjust(uint16_t oldsum,uint16_t old,uint16_t new)826 cksum_adjust(uint16_t oldsum, uint16_t old, uint16_t new)
827 {
828 
829 	return (~cksum_add(cksum_add(~oldsum, ~old), new));
830 }
831 
832 #endif /* _KERNEL */
833 #endif /* _IPFW2_PRIVATE_H */
834