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