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