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