1/*
  2 * Copyright (C) 2012 by Darren Reed.
  3 *
  4 * See the IPFILTER.LICENCE file for details on licencing.
  5 *
  6 * @(#)ip_nat.h	1.5 2/4/96
  7 * Id: ip_nat.h,v 2.90.2.20 2007/09/25 08:27:32 darrenr Exp $
  8 */
  9
 10#ifndef	__IP_NAT_H__
 11#define	__IP_NAT_H__
 12
 13#ifndef	SOLARIS
 14# if defined(sun) && defined(__SVR4)
 15#  define	SOLARIS		1
 16# else
 17#  define	SOLARIS		0
 18# endif
 19#endif
 20
 21#define	SIOCADNAT	_IOW('r', 60, struct ipfobj)
 22#define	SIOCRMNAT	_IOW('r', 61, struct ipfobj)
 23#define	SIOCGNATS	_IOWR('r', 62, struct ipfobj)
 24#define	SIOCGNATL	_IOWR('r', 63, struct ipfobj)
 25#define	SIOCPURGENAT	_IOWR('r', 100, struct ipfobj)
 26
 27#undef	LARGE_NAT	/* define	this if you're setting up a system to NAT
 28			 * LARGE numbers of networks/hosts - i.e. in the
 29			 * hundreds or thousands.  In such a case, you should
 30			 * also change the RDR_SIZE and NAT_SIZE below to more
 31			 * appropriate sizes.  The figures below were used for
 32			 * a setup with 1000-2000 networks to NAT.
 33			 */
 34#ifdef NAT_SIZE
 35# define	NAT_SIZE_LARGE		NAT_SIZE
 36# define	NAT_SIZE_NORMAL		NAT_SIZE
 37#else
 38# define	NAT_SIZE_LARGE		2047
 39# define	NAT_SIZE_NORMAL		127
 40#endif
 41#ifdef RDR_SIZE
 42# define	RDR_SIZE_LARGE		RDR_SIZE
 43# define	RDR_SIZE_NORMAL		RDR_SIZE
 44#else
 45# define	RDR_SIZE_LARGE		2047
 46# define	RDR_SIZE_NORMAL		127
 47#endif
 48#ifdef HOSTMAP_SIZE
 49# define	HOSTMAP_SIZE_LARGE	HOSTMAP_SIZE
 50# define	HOSTMAP_SIZE_NORMAL	HOSTMAP_SIZE
 51#else
 52# define	HOSTMAP_SIZE_LARGE	8191
 53# define	HOSTMAP_SIZE_NORMAL	2047
 54#endif
 55/*
 56 * This is newly introduced and for the sake of "least surprise", the numbers
 57 * present aren't what we'd normally use for creating a proper hash table.
 58 */
 59#ifdef NAT_TABLE_MAX
 60# define	NAT_TABLE_MAX_LARGE	NAT_TABLE_MAX
 61# define	NAT_TABLE_MAX_NORMAL	NAT_TABLE_MAX
 62#else
 63# define	NAT_TABLE_MAX_LARGE	180000
 64# define	NAT_TABLE_MAX_NORMAL	30000
 65#endif
 66#ifdef NAT_TABLE_SZ
 67# define	NAT_TABLE_SZ_LARGE	NAT_TABLE_SZ
 68# define	NAT_TABLE_SZ_NORMAL	NAT_TABLE_SZ
 69#else
 70# define	NAT_TABLE_SZ_LARGE	16383
 71# define	NAT_TABLE_SZ_NORMAL	2047
 72#endif
 73#ifndef	APR_LABELLEN
 74#define	APR_LABELLEN	16
 75#endif
 76#define	NAT_HW_CKSUM		0x80000000
 77#define	NAT_HW_CKSUM_PART	0x40000000
 78
 79#define	DEF_NAT_AGE	1200     /* 10 minutes (600 seconds) */
 80
 81struct ipstate;
 82struct ap_session;
 83
 84/*
 85 * This structure is used in the active NAT table and represents an
 86 * active NAT session.
 87 */
 88typedef	struct	nat	{
 89	ipfmutex_t	nat_lock;
 90	struct	nat	*nat_next;
 91	struct	nat	**nat_pnext;
 92	struct	nat	*nat_hnext[2];
 93	struct	nat	**nat_phnext[2];
 94	struct	hostmap	*nat_hm;
 95	void		*nat_data;
 96	struct	nat	**nat_me;
 97	struct	ipstate	*nat_state;
 98	struct	ap_session	*nat_aps;		/* proxy session */
 99	frentry_t	*nat_fr;	/* filter rule ptr if appropriate */
100	struct	ipnat	*nat_ptr;	/* pointer back to the rule */
101	void		*nat_ifps[2];
102	void		*nat_sync;
103	ipftqent_t	nat_tqe;
104	int		nat_mtu[2];
105	u_32_t		nat_flags;
106	u_32_t		nat_sumd[2];	/* ip checksum delta for data segment*/
107	u_32_t		nat_ipsumd;	/* ip checksum delta for ip header */
108	u_32_t		nat_mssclamp;	/* if != zero clamp MSS to this */
109	i6addr_t	nat_odst6;
110	i6addr_t	nat_osrc6;
111	i6addr_t	nat_ndst6;
112	i6addr_t	nat_nsrc6;
113	U_QUAD_T	nat_pkts[2];
114	U_QUAD_T	nat_bytes[2];
115	union	{
116		udpinfo_t	nat_unu;
117		tcpinfo_t	nat_unt;
118		icmpinfo_t	nat_uni;
119		greinfo_t	nat_ugre;
120	} nat_unold, nat_unnew;
121	int		nat_use;
122	int		nat_pr[2];		/* protocol for NAT */
123	int		nat_dir;
124	int		nat_ref;		/* reference count */
125	u_int		nat_hv[2];
126	char		nat_ifnames[2][LIFNAMSIZ];
127	int		nat_rev;		/* 0 = forward, 1 = reverse */
128	int		nat_dlocal;
129	int		nat_v[2];		/* 0 = old, 1 = new */
130	u_int		nat_redir;		/* copy of in_redir */
131} nat_t;
132
133#define	nat_osrcip	nat_osrc6.in4
134#define	nat_odstip	nat_odst6.in4
135#define	nat_nsrcip	nat_nsrc6.in4
136#define	nat_ndstip	nat_ndst6.in4
137#define	nat_osrcaddr	nat_osrc6.in4.s_addr
138#define	nat_odstaddr	nat_odst6.in4.s_addr
139#define	nat_nsrcaddr	nat_nsrc6.in4.s_addr
140#define	nat_ndstaddr	nat_ndst6.in4.s_addr
141#define	nat_age		nat_tqe.tqe_die
142#define	nat_osport	nat_unold.nat_unt.ts_sport
143#define	nat_odport	nat_unold.nat_unt.ts_dport
144#define	nat_nsport	nat_unnew.nat_unt.ts_sport
145#define	nat_ndport	nat_unnew.nat_unt.ts_dport
146#define	nat_oicmpid	nat_unold.nat_uni.ici_id
147#define	nat_nicmpid	nat_unnew.nat_uni.ici_id
148#define	nat_type	nat_unold.nat_uni.ici_type
149#define	nat_oseq	nat_unold.nat_uni.ici_seq
150#define	nat_nseq	nat_unnew.nat_uni.ici_seq
151#define	nat_tcpstate	nat_tqe.tqe_state
152#define	nat_die		nat_tqe.tqe_die
153#define	nat_touched	nat_tqe.tqe_touched
154
155/*
156 * Values for nat_dir
157 */
158#define	NAT_INBOUND	0
159#define	NAT_OUTBOUND	1
160#define	NAT_ENCAPIN	2
161#define	NAT_ENCAPOUT	3
162#define	NAT_DIVERTIN	4
163#define	NAT_DIVERTOUT	5
164
165/*
166 * Definitions for nat_flags
167 */
168#define	NAT_TCP		0x0001	/* IPN_TCP */
169#define	NAT_UDP		0x0002	/* IPN_UDP */
170#define	NAT_ICMPERR	0x0004	/* IPN_ICMPERR */
171#define	NAT_ICMPQUERY	0x0008	/* IPN_ICMPQUERY */
172#define	NAT_SEARCH	0x0010
173#define	NAT_SLAVE	0x0020	/* Slave connection for a proxy */
174#define	NAT_NOTRULEPORT	0x0040	/* Don't use the port # in the NAT rule */
175
176#define	NAT_TCPUDP	(NAT_TCP|NAT_UDP)
177#define	NAT_TCPUDPICMP	(NAT_TCP|NAT_UDP|NAT_ICMPERR)
178#define	NAT_TCPUDPICMPQ	(NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
179#define	NAT_FROMRULE	(NAT_TCP|NAT_UDP)
180
181/* 0x0100 reserved for FI_W_SPORT */
182/* 0x0200 reserved for FI_W_DPORT */
183/* 0x0400 reserved for FI_W_SADDR */
184/* 0x0800 reserved for FI_W_DADDR */
185/* 0x1000 reserved for FI_W_NEWFR */
186/* 0x2000 reserved for SI_CLONE */
187/* 0x4000 reserved for SI_CLONED */
188/* 0x8000 reserved for SI_IGNOREPKT */
189
190#define	NAT_DEBUG	0x800000
191
192typedef	struct nat_addr_s {
193	i6addr_t	na_addr[2];
194	i6addr_t	na_nextaddr;
195	int		na_atype;
196	int		na_function;
197} nat_addr_t;
198
199#define	na_nextip	na_nextaddr.in4.s_addr
200#define	na_nextip6	na_nextaddr.in6
201#define	na_num		na_addr[0].iplookupnum
202#define	na_type		na_addr[0].iplookuptype
203#define	na_subtype	na_addr[0].iplookupsubtype
204#define	na_ptr		na_addr[1].iplookupptr
205#define	na_func		na_addr[1].iplookupfunc
206
207
208/*
209 * This structure represents an actual NAT rule, loaded by ipnat.
210 */
211typedef	struct	ipnat	{
212	ipfmutex_t	in_lock;
213	struct	ipnat	*in_next;		/* NAT rule list next */
214	struct	ipnat	**in_pnext;		/* prior rdr next ptr */
215	struct	ipnat	*in_rnext;		/* rdr rule hash next */
216	struct	ipnat	**in_prnext;		/* prior rdr next ptr */
217	struct	ipnat	*in_mnext;		/* map rule hash next */
218	struct	ipnat	**in_pmnext;		/* prior map next ptr */
219	struct	ipftq	*in_tqehead[2];
220	void		*in_ifps[2];
221	void		*in_apr;
222	char		*in_comment;
223	mb_t		*in_divmp;
224	void		*in_pconf;
225	U_QUAD_T	in_pkts[2];
226	U_QUAD_T	in_bytes[2];
227	u_long		in_space;
228	u_long		in_hits;
229	int		in_size;
230	int		in_use;
231	u_int		in_hv[2];
232	int		in_flineno;		/* conf. file line number */
233	int		in_stepnext;
234	int		in_dlocal;
235	u_short		in_dpnext;
236	u_short		in_spnext;
237	/* From here to the end is covered by IPN_CMPSIZ */
238	u_char		in_v[2];		/* 0 = old, 1 = new */
239	u_32_t		in_flags;
240	u_32_t		in_mssclamp;		/* if != 0 clamp MSS to this */
241	u_int		in_age[2];
242	int		in_redir;		/* see below for values */
243	int		in_pr[2];		/* protocol. */
244	nat_addr_t	in_ndst;
245	nat_addr_t	in_nsrc;
246	nat_addr_t	in_osrc;
247	nat_addr_t	in_odst;
248	frtuc_t		in_tuc;
249	u_short		in_ppip;		/* ports per IP. */
250	u_short		in_ippip;		/* IP #'s per IP# */
251	u_short		in_ndports[2];
252	u_short		in_nsports[2];
253	int		in_ifnames[2];
254	int		in_plabel;	/* proxy label. */
255	int		in_pconfig;	/* proxy label. */
256	ipftag_t	in_tag;
257	int		in_namelen;
258	char		in_names[1];
259} ipnat_t;
260
261/*
262 *      MAP-IN MAP-OUT RDR-IN RDR-OUT
263 * osrc    X   == src  == src    X
264 * odst    X   == dst  == dst    X
265 * nsrc == dst   X       X    == dst
266 * ndst == src   X       X    == src
267 */
268#define	in_dpmin	in_ndports[0]	/* Also holds static redir port */
269#define	in_dpmax	in_ndports[1]
270#define	in_spmin	in_nsports[0]	/* Also holds static redir port */
271#define	in_spmax	in_nsports[1]
272#define	in_ndport	in_ndports[0]
273#define	in_nsport	in_nsports[0]
274#define	in_dipnext	in_ndst.na_nextaddr.in4
275#define	in_dipnext6	in_ndst.na_nextaddr
276#define	in_dnip		in_ndst.na_nextaddr.in4.s_addr
277#define	in_dnip6	in_ndst.na_nextaddr
278#define	in_sipnext	in_nsrc.na_nextaddr.in4
279#define	in_snip		in_nsrc.na_nextaddr.in4.s_addr
280#define	in_snip6	in_nsrc.na_nextaddr
281#define	in_odstip	in_odst.na_addr[0].in4
282#define	in_odstip6	in_odst.na_addr[0]
283#define	in_odstaddr	in_odst.na_addr[0].in4.s_addr
284#define	in_odstmsk	in_odst.na_addr[1].in4.s_addr
285#define	in_odstmsk6	in_odst.na_addr[1]
286#define	in_odstatype	in_odst.na_atype
287#define	in_osrcip	in_osrc.na_addr[0].in4
288#define	in_osrcip6	in_osrc.na_addr[0]
289#define	in_osrcaddr	in_osrc.na_addr[0].in4.s_addr
290#define	in_osrcmsk	in_osrc.na_addr[1].in4.s_addr
291#define	in_osrcmsk6	in_osrc.na_addr[1]
292#define	in_osrcatype	in_osrc.na_atype
293#define	in_ndstip	in_ndst.na_addr[0].in4
294#define	in_ndstip6	in_ndst.na_addr[0]
295#define	in_ndstaddr	in_ndst.na_addr[0].in4.s_addr
296#define	in_ndstmsk	in_ndst.na_addr[1].in4.s_addr
297#define	in_ndstmsk6	in_ndst.na_addr[1]
298#define	in_ndstatype	in_ndst.na_atype
299#define	in_ndstafunc	in_ndst.na_function
300#define	in_nsrcip	in_nsrc.na_addr[0].in4
301#define	in_nsrcip6	in_nsrc.na_addr[0]
302#define	in_nsrcaddr	in_nsrc.na_addr[0].in4.s_addr
303#define	in_nsrcmsk	in_nsrc.na_addr[1].in4.s_addr
304#define	in_nsrcmsk6	in_nsrc.na_addr[1]
305#define	in_nsrcatype	in_nsrc.na_atype
306#define	in_nsrcafunc	in_nsrc.na_function
307#define	in_scmp		in_tuc.ftu_scmp
308#define	in_dcmp		in_tuc.ftu_dcmp
309#define	in_stop		in_tuc.ftu_stop
310#define	in_dtop		in_tuc.ftu_dtop
311#define	in_osport	in_tuc.ftu_sport
312#define	in_odport	in_tuc.ftu_dport
313#define	in_ndstnum	in_ndst.na_addr[0].iplookupnum
314#define	in_ndsttype	in_ndst.na_addr[0].iplookuptype
315#define	in_ndstptr	in_ndst.na_addr[1].iplookupptr
316#define	in_ndstfunc	in_ndst.na_addr[1].iplookupfunc
317#define	in_nsrcnum	in_nsrc.na_addr[0].iplookupnum
318#define	in_nsrctype	in_nsrc.na_addr[0].iplookuptype
319#define	in_nsrcptr	in_nsrc.na_addr[1].iplookupptr
320#define	in_nsrcfunc	in_nsrc.na_addr[1].iplookupfunc
321#define	in_odstnum	in_odst.na_addr[0].iplookupnum
322#define	in_odsttype	in_odst.na_addr[0].iplookuptype
323#define	in_odstptr	in_odst.na_addr[1].iplookupptr
324#define	in_odstfunc	in_odst.na_addr[1].iplookupfunc
325#define	in_osrcnum	in_osrc.na_addr[0].iplookupnum
326#define	in_osrctype	in_osrc.na_addr[0].iplookuptype
327#define	in_osrcptr	in_osrc.na_addr[1].iplookupptr
328#define	in_osrcfunc	in_osrc.na_addr[1].iplookupfunc
329#define	in_icmpidmin	in_nsports[0]
330#define	in_icmpidmax	in_nsports[1]
331
332/*
333 * Bit definitions for in_flags
334 */
335#define	IPN_ANY		0x00000
336#define	IPN_TCP		0x00001
337#define	IPN_UDP		0x00002
338#define	IPN_TCPUDP	(IPN_TCP|IPN_UDP)
339#define	IPN_ICMPERR	0x00004
340#define	IPN_TCPUDPICMP	(IPN_TCP|IPN_UDP|IPN_ICMPERR)
341#define	IPN_ICMPQUERY	0x00008
342#define	IPN_TCPUDPICMPQ	(IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
343#define	IPN_RF		(IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
344#define	IPN_AUTOPORTMAP	0x00010
345#define	IPN_FILTER	0x00020
346#define	IPN_SPLIT	0x00040
347#define	IPN_ROUNDR	0x00080
348#define	IPN_SIPRANGE	0x00100
349#define	IPN_DIPRANGE	0x00200
350#define	IPN_NOTSRC	0x00400
351#define	IPN_NOTDST	0x00800
352#define	IPN_NO		0x01000
353#define	IPN_DYNSRCIP	0x02000	/* dynamic src IP# */
354#define	IPN_DYNDSTIP	0x04000	/* dynamic dst IP# */
355#define	IPN_DELETE	0x08000
356#define	IPN_STICKY	0x10000
357#define	IPN_FRAG	0x20000
358#define	IPN_FIXEDSPORT	0x40000
359#define	IPN_FIXEDDPORT	0x80000
360#define	IPN_FINDFORWARD	0x100000
361#define	IPN_IN		0x200000
362#define	IPN_SEQUENTIAL	0x400000
363#define	IPN_PURGE	0x800000
364#define	IPN_PROXYRULE	0x1000000
365#define	IPN_USERFLAGS	(IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_SIPRANGE|IPN_SPLIT|\
366			 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|IPN_NO|\
367			 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY|\
368			 IPN_DIPRANGE|IPN_SEQUENTIAL|IPN_PURGE)
369
370/*
371 * Values for in_redir
372 */
373#define	NAT_MAP		0x01
374#define	NAT_REDIRECT	0x02
375#define	NAT_BIMAP	(NAT_MAP|NAT_REDIRECT)
376#define	NAT_MAPBLK	0x04
377#define	NAT_REWRITE	0x08
378#define	NAT_ENCAP	0x10
379#define	NAT_DIVERTUDP	0x20
380
381#define	MAPBLK_MINPORT	1024	/* don't use reserved ports for src port */
382#define	USABLE_PORTS	(65536 - MAPBLK_MINPORT)
383
384#define	IPN_CMPSIZ	(sizeof(ipnat_t) - offsetof(ipnat_t, in_v))
385
386typedef	struct	natlookup {
387	i6addr_t	nl_inipaddr;
388	i6addr_t	nl_outipaddr;
389	i6addr_t	nl_realipaddr;
390	int		nl_v;
391	int		nl_flags;
392	u_short		nl_inport;
393	u_short		nl_outport;
394	u_short		nl_realport;
395} natlookup_t;
396
397#define	nl_inip		nl_inipaddr.in4
398#define	nl_outip	nl_outipaddr.in4
399#define	nl_realip	nl_realipaddr.in4
400#define	nl_inip6	nl_inipaddr.in6
401#define	nl_outip6	nl_outipaddr.in6
402#define	nl_realip6	nl_realipaddr.in6
403
404
405typedef struct  nat_save    {
406	void	*ipn_next;
407	struct	nat	ipn_nat;
408	struct	ipnat	ipn_ipnat;
409	struct	frentry ipn_fr;
410	int	ipn_dsize;
411	char	ipn_data[4];
412} nat_save_t;
413
414#define	ipn_rule	ipn_nat.nat_fr
415
416typedef	struct	natget	{
417	void	*ng_ptr;
418	int	ng_sz;
419} natget_t;
420
421
422/*
423 * This structure gets used to help NAT sessions keep the same NAT rule (and
424 * thus translation for IP address) when:
425 * (a) round-robin redirects are in use
426 * (b) different IP add
427 */
428typedef	struct	hostmap	{
429	struct	hostmap	*hm_hnext;
430	struct	hostmap	**hm_phnext;
431	struct	hostmap	*hm_next;
432	struct	hostmap	**hm_pnext;
433	struct	ipnat	*hm_ipnat;
434	i6addr_t	hm_osrcip6;
435	i6addr_t	hm_odstip6;
436	i6addr_t	hm_nsrcip6;
437	i6addr_t	hm_ndstip6;
438	u_32_t		hm_port;
439	int		hm_ref;
440	int		hm_hv;
441	int		hm_v;
442} hostmap_t;
443
444#define	hm_osrcip	hm_osrcip6.in4
445#define	hm_odstip	hm_odstip6.in4
446#define	hm_nsrcip	hm_nsrcip6.in4
447#define	hm_ndstip	hm_ndstip6.in4
448#define	hm_osrc6	hm_osrcip6.in6
449#define	hm_odst6	hm_odstip6.in6
450#define	hm_nsrc6	hm_nsrcip6.in6
451#define	hm_ndst6	hm_ndstip6.in6
452
453
454/*
455 * Structure used to pass information in to nat_newmap and nat_newrdr.
456 */
457typedef struct	natinfo	{
458	ipnat_t		*nai_np;
459	u_32_t		nai_sum1;
460	u_32_t		nai_sum2;
461	struct	in_addr	nai_ip;		/* In host byte order */
462	u_short		nai_port;
463	u_short		nai_nport;
464	u_short		nai_sport;
465	u_short		nai_dport;
466} natinfo_t;
467
468
469typedef	struct nat_stat_side {
470	u_int	*ns_bucketlen;
471	nat_t	**ns_table;
472	u_long	ns_added;
473	u_long	ns_appr_fail;
474	u_long	ns_badnat;
475	u_long	ns_badnatnew;
476	u_long	ns_badnextaddr;
477	u_long	ns_bucket_max;
478	u_long	ns_clone_nomem;
479	u_long	ns_decap_bad;
480	u_long	ns_decap_fail;
481	u_long	ns_decap_pullup;
482	u_long	ns_divert_dup;
483	u_long	ns_divert_exist;
484	u_long	ns_drop;
485	u_long	ns_encap_dup;
486	u_long	ns_encap_pullup;
487	u_long	ns_exhausted;
488	u_long	ns_icmp_address;
489	u_long	ns_icmp_basic;
490	u_long	ns_icmp_mbuf;
491	u_long	ns_icmp_notfound;
492	u_long	ns_icmp_rebuild;
493	u_long	ns_icmp_short;
494	u_long	ns_icmp_size;
495	u_long	ns_ifpaddrfail;
496	u_long	ns_ignored;
497	u_long	ns_insert_fail;
498	u_long	ns_inuse;
499	u_long	ns_log;
500	u_long	ns_lookup_miss;
501	u_long	ns_lookup_nowild;
502	u_long	ns_new_ifpaddr;
503	u_long	ns_memfail;
504	u_long	ns_table_max;
505	u_long	ns_translated;
506	u_long	ns_unfinalised;
507	u_long	ns_wrap;
508	u_long	ns_xlate_null;
509	u_long	ns_xlate_exists;
510	u_long	ns_ipf_proxy_fail;
511	u_long	ns_uncreate[2];
512} nat_stat_side_t;
513
514
515typedef	struct	natstat	{
516	nat_t		*ns_instances;
517	ipnat_t		*ns_list;
518	hostmap_t	*ns_maplist;
519	hostmap_t	**ns_maptable;
520	u_int		ns_active;
521	u_long		ns_addtrpnt;
522	u_long		ns_divert_build;
523	u_long		ns_expire;
524	u_long		ns_flush_all;
525	u_long		ns_flush_closing;
526	u_long		ns_flush_queue;
527	u_long		ns_flush_state;
528	u_long		ns_flush_timeout;
529	u_long		ns_hm_new;
530	u_long		ns_hm_newfail;
531	u_long		ns_hm_addref;
532	u_long		ns_hm_nullnp;
533	u_long		ns_log_ok;
534	u_long		ns_log_fail;
535	u_int		ns_hostmap_sz;
536	u_int		ns_nattab_sz;
537	u_int		ns_nattab_max;
538	u_int		ns_orphans;
539	u_int		ns_rules;
540	u_int		ns_rules_map;
541	u_int		ns_rules_rdr;
542	u_int		ns_rultab_sz;
543	u_int		ns_rdrtab_sz;
544	u_32_t		ns_ticks;
545	u_int		ns_trpntab_sz;
546	u_int		ns_wilds;
547	u_long		ns_proto[256];
548	nat_stat_side_t	ns_side[2];
549#ifdef USE_INET6
550	nat_stat_side_t	ns_side6[2];
551#endif
552} natstat_t;
553
554typedef	struct	natlog {
555	i6addr_t	nl_osrcip;
556	i6addr_t	nl_odstip;
557	i6addr_t	nl_nsrcip;
558	i6addr_t	nl_ndstip;
559	u_short		nl_osrcport;
560	u_short		nl_odstport;
561	u_short		nl_nsrcport;
562	u_short		nl_ndstport;
563	int		nl_action;
564	int		nl_type;
565	int		nl_rule;
566	U_QUAD_T	nl_pkts[2];
567	U_QUAD_T	nl_bytes[2];
568	u_char		nl_p[2];
569	u_char		nl_v[2];
570	u_char		nl_ifnames[2][LIFNAMSIZ];
571} natlog_t;
572
573
574#define	NL_NEW		0
575#define	NL_CLONE	1
576#define	NL_PURGE	0xfffc
577#define	NL_DESTROY	0xfffd
578#define	NL_FLUSH	0xfffe
579#define	NL_EXPIRE	0xffff
580
581#define	NAT_HASH_FN(_k,_l,_m)	(((_k) + ((_k) >> 12) + _l) % (_m))
582#define	NAT_HASH_FN6(_k,_l,_m)	((((u_32_t *)(_k))[3] \
583				 + (((u_32_t *)(_k))[3] >> 12) \
584				 + (((u_32_t *)(_k))[2]) \
585				 + (((u_32_t *)(_k))[2] >> 12) \
586				 + (((u_32_t *)(_k))[1]) \
587				 + (((u_32_t *)(_k))[1] >> 12) \
588				 + (((u_32_t *)(_k))[0]) \
589				 + (((u_32_t *)(_k))[0] >> 12) \
590				 + _l) % (_m))
591
592#define	LONG_SUM(_i)	(((_i) & 0xffff) + ((_i) >> 16))
593#define	LONG_SUM6(_i)	(LONG_SUM(ntohl(((u_32_t *)(_i))[0])) + \
594			 LONG_SUM(ntohl(((u_32_t *)(_i))[1])) + \
595			 LONG_SUM(ntohl(((u_32_t *)(_i))[2])) + \
596			 LONG_SUM(ntohl(((u_32_t *)(_i))[3])))
597
598#define	CALC_SUMD(s1, s2, sd) { \
599			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
600			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
601			    /* Do it twice */ \
602			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
603			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
604			    /* Because ~1 == -2, We really need ~1 == -1 */ \
605			    if ((s1) > (s2)) (s2)--; \
606			    (sd) = (s2) - (s1); \
607			    (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
608
609#define	NAT_SYSSPACE		0x80000000
610#define	NAT_LOCKHELD		0x40000000
611
612/*
613 * This is present in ip_nat.h because it needs to be shared between
614 * ip_nat.c and ip_nat6.c
615 */
616typedef struct ipf_nat_softc_s {
617	ipfmutex_t	ipf_nat_new;
618	ipfmutex_t	ipf_nat_io;
619	int		ipf_nat_doflush;
620	int		ipf_nat_logging;
621	int		ipf_nat_lock;
622	int		ipf_nat_inited;
623	int		ipf_nat_table_wm_high;
624	int		ipf_nat_table_wm_low;
625	u_int		ipf_nat_table_max;
626	u_int		ipf_nat_table_sz;
627	u_int		ipf_nat_maprules_sz;
628	u_int		ipf_nat_rdrrules_sz;
629	u_int		ipf_nat_hostmap_sz;
630	u_int		ipf_nat_maxbucket;
631	u_int		ipf_nat_last_force_flush;
632	u_int		ipf_nat_defage;
633	u_int		ipf_nat_defipage;
634	u_int		ipf_nat_deficmpage;
635	ipf_v4_masktab_t	ipf_nat_map_mask;
636	ipf_v6_masktab_t	ipf_nat6_map_mask;
637	ipf_v4_masktab_t	ipf_nat_rdr_mask;
638	ipf_v6_masktab_t	ipf_nat6_rdr_mask;
639	nat_t		**ipf_nat_table[2];
640	nat_t		*ipf_nat_instances;
641	ipnat_t		*ipf_nat_list;
642	ipnat_t		**ipf_nat_list_tail;
643	ipnat_t		**ipf_nat_map_rules;
644	ipnat_t		**ipf_nat_rdr_rules;
645	ipftq_t		*ipf_nat_utqe;
646	hostmap_t	**ipf_hm_maptable ;
647	hostmap_t	*ipf_hm_maplist ;
648	ipftuneable_t	*ipf_nat_tune;
649	ipftq_t		ipf_nat_udptq;
650	ipftq_t		ipf_nat_udpacktq;
651	ipftq_t		ipf_nat_icmptq;
652	ipftq_t		ipf_nat_icmpacktq;
653	ipftq_t		ipf_nat_iptq;
654	ipftq_t		ipf_nat_pending;
655	ipftq_t		ipf_nat_tcptq[IPF_TCP_NSTATES];
656	natstat_t	ipf_nat_stats;
657} ipf_nat_softc_t ;
658
659#define	ipf_nat_map_max			ipf_nat_map_mask.imt4_max
660#define	ipf_nat_rdr_max			ipf_nat_rdr_mask.imt4_max
661#define	ipf_nat6_map_max		ipf_nat6_map_mask.imt6_max
662#define	ipf_nat6_rdr_max		ipf_nat6_rdr_mask.imt6_max
663#define	ipf_nat_map_active_masks	ipf_nat_map_mask.imt4_active
664#define	ipf_nat_rdr_active_masks	ipf_nat_rdr_mask.imt4_active
665#define	ipf_nat6_map_active_masks	ipf_nat6_map_mask.imt6_active
666#define	ipf_nat6_rdr_active_masks	ipf_nat6_rdr_mask.imt6_active
667
668extern	frentry_t 	ipfnatblock;
669
670extern	void	ipf_fix_datacksum(u_short *, u_32_t);
671extern	void	ipf_fix_incksum(int, u_short *, u_32_t, u_32_t);
672extern	void	ipf_fix_outcksum(int, u_short *, u_32_t, u_32_t);
673
674extern	int	ipf_nat_checkin(fr_info_t *, u_32_t *);
675extern	int	ipf_nat_checkout(fr_info_t *, u_32_t *);
676extern	void	ipf_nat_delete(ipf_main_softc_t *, struct nat *, int);
677extern	void	ipf_nat_deref(ipf_main_softc_t *, nat_t **);
678extern	void	ipf_nat_expire(ipf_main_softc_t *);
679extern	int	ipf_nat_hashtab_add(ipf_main_softc_t *,
680					 ipf_nat_softc_t *, nat_t *);
681extern	void	ipf_nat_hostmapdel(ipf_main_softc_t *, hostmap_t **);
682extern	int	ipf_nat_hostmap_rehash(ipf_main_softc_t *,
683					    ipftuneable_t *, ipftuneval_t *);
684extern	nat_t	*ipf_nat_icmperrorlookup(fr_info_t *, int);
685extern	nat_t	*ipf_nat_icmperror(fr_info_t *, u_int *, int);
686extern	int	ipf_nat_init(void);
687extern	nat_t	*ipf_nat_inlookup(fr_info_t *, u_int, u_int,
688				      struct in_addr, struct in_addr);
689extern	int	ipf_nat_in(fr_info_t *, nat_t *, int, u_32_t);
690extern	int	ipf_nat_insert(ipf_main_softc_t *, ipf_nat_softc_t *,
691				    nat_t *);
692extern	int	ipf_nat_ioctl(ipf_main_softc_t *, caddr_t, ioctlcmd_t,
693				   int, int, void *);
694extern	void	ipf_nat_log(ipf_main_softc_t *, ipf_nat_softc_t *,
695				 struct nat *, u_int);
696extern	nat_t	*ipf_nat_lookupredir(natlookup_t *);
697extern	nat_t	*ipf_nat_maplookup(void *, u_int, struct in_addr,
698				struct in_addr);
699extern	nat_t	*ipf_nat_add(fr_info_t *, ipnat_t *, nat_t **,
700				 u_int, int);
701extern	int	ipf_nat_out(fr_info_t *, nat_t *, int, u_32_t);
702extern	nat_t	*ipf_nat_outlookup(fr_info_t *, u_int, u_int,
703				       struct in_addr, struct in_addr);
704extern	u_short	*ipf_nat_proto(fr_info_t *, nat_t *, u_int);
705extern	void	ipf_nat_rule_deref(ipf_main_softc_t *, ipnat_t **);
706extern	void	ipf_nat_setqueue(ipf_main_softc_t *, ipf_nat_softc_t *,
707				      nat_t *);
708extern	void	ipf_nat_setpending(ipf_main_softc_t *, nat_t *);
709extern	nat_t	*ipf_nat_tnlookup(fr_info_t *, int);
710extern	void	ipf_nat_update(fr_info_t *, nat_t *);
711extern	int	ipf_nat_rehash(ipf_main_softc_t *, ipftuneable_t *,
712				    ipftuneval_t *);
713extern	int	ipf_nat_rehash_rules(ipf_main_softc_t *, ipftuneable_t *,
714					  ipftuneval_t *);
715extern	int	ipf_nat_settimeout(struct ipf_main_softc_s *,
716					ipftuneable_t *, ipftuneval_t *);
717extern	void	ipf_nat_sync(ipf_main_softc_t *, void *);
718
719extern	nat_t	*ipf_nat_clone(fr_info_t *, nat_t *);
720extern	void	ipf_nat_delmap(ipf_nat_softc_t *, ipnat_t *);
721extern	void	ipf_nat_delrdr(ipf_nat_softc_t *, ipnat_t *);
722extern	int	ipf_nat_wildok(nat_t *, int, int, int, int);
723extern	void	ipf_nat_setlock(void *, int);
724extern	void	ipf_nat_load(void);
725extern	void	*ipf_nat_soft_create(ipf_main_softc_t *);
726extern	int	ipf_nat_soft_init(ipf_main_softc_t *, void *);
727extern	void	ipf_nat_soft_destroy(ipf_main_softc_t *, void *);
728extern	int	ipf_nat_soft_fini(ipf_main_softc_t *, void *);
729extern	int	ipf_nat_main_load(void);
730extern	int	ipf_nat_main_unload(void);
731extern	ipftq_t	*ipf_nat_add_tq(ipf_main_softc_t *, int);
732extern	void	ipf_nat_uncreate(fr_info_t *);
733
734#ifdef USE_INET6
735extern	nat_t	*ipf_nat6_add(fr_info_t *, ipnat_t *, nat_t **,
736				u_int, int);
737extern	void	ipf_nat6_addrdr(ipf_nat_softc_t *, ipnat_t *);
738extern	void	ipf_nat6_addmap(ipf_nat_softc_t *, ipnat_t *);
739extern	void	ipf_nat6_addencap(ipf_nat_softc_t *, ipnat_t *);
740extern	int	ipf_nat6_checkout(fr_info_t *, u_32_t *);
741extern	int	ipf_nat6_checkin(fr_info_t *, u_32_t *);
742extern	void	ipf_nat6_delmap(ipf_nat_softc_t *, ipnat_t *);
743extern	void	ipf_nat6_delrdr(ipf_nat_softc_t *, ipnat_t *);
744extern	int	ipf_nat6_finalise(fr_info_t *, nat_t *);
745extern	nat_t	*ipf_nat6_icmperror(fr_info_t *, u_int *, int);
746extern	nat_t	*ipf_nat6_icmperrorlookup(fr_info_t *, int);
747extern	nat_t	*ipf_nat6_inlookup(fr_info_t *, u_int, u_int,
748				struct in6_addr *, struct in6_addr *);
749extern	u_32_t	ipf_nat6_ip6subtract(i6addr_t *, i6addr_t *);
750extern	frentry_t *ipf_nat6_ipfin(fr_info_t *, u_32_t *);
751extern	frentry_t *ipf_nat6_ipfout(fr_info_t *, u_32_t *);
752extern	nat_t	*ipf_nat6_lookupredir(natlookup_t *);
753extern	int	ipf_nat6_newmap(fr_info_t *, nat_t *, natinfo_t *);
754extern	int	ipf_nat6_newrdr(fr_info_t *, nat_t *, natinfo_t *);
755extern	nat_t	*ipf_nat6_outlookup(fr_info_t *, u_int, u_int,
756				struct in6_addr *, struct in6_addr *);
757extern	int	ipf_nat6_newrewrite(fr_info_t *, nat_t *, natinfo_t *);
758extern	int	ipf_nat6_newdivert(fr_info_t *, nat_t *, natinfo_t *);
759extern	int	ipf_nat6_ruleaddrinit(ipf_main_softc_t *, ipf_nat_softc_t *, ipnat_t *);
760
761#endif
762
763
764#endif /* __IP_NAT_H__ */