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