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