xref: /illumos-gate/usr/src/uts/common/inet/ipf/netinet/ip_nat.h (revision 29e83d4b25fd82feb8e0e0fbe89f7e2a8438533d)
1 /*
2  * Copyright (C) 1995-2001, 2003 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.11 2005/06/18 02:41:32 darrenr Exp $
8  *
9  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
10  * Use is subject to license terms.
11  */
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 #ifndef	__IP_NAT_H__
15 #define	__IP_NAT_H__
16 
17 #ifndef SOLARIS
18 #define	SOLARIS	(defined(sun) && (defined(__svr4__) || defined(__SVR4)))
19 #endif
20 
21 #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
22 #define	SIOCADNAT	_IOW('r', 60, struct ipfobj)
23 #define	SIOCRMNAT	_IOW('r', 61, struct ipfobj)
24 #define	SIOCGNATS	_IOWR('r', 62, struct ipfobj)
25 #define	SIOCGNATL	_IOWR('r', 63, struct ipfobj)
26 #else
27 #define	SIOCADNAT	_IOW(r, 60, struct ipfobj)
28 #define	SIOCRMNAT	_IOW(r, 61, struct ipfobj)
29 #define	SIOCGNATS	_IOWR(r, 62, struct ipfobj)
30 #define	SIOCGNATL	_IOWR(r, 63, struct ipfobj)
31 #endif
32 
33 #undef	LARGE_NAT	/* define	this if you're setting up a system to NAT
34 			 * LARGE numbers of networks/hosts - i.e. in the
35 			 * hundreds or thousands.  In such a case, you should
36 			 * also change the RDR_SIZE and NAT_SIZE below to more
37 			 * appropriate sizes.  The figures below were used for
38 			 * a setup with 1000-2000 networks to NAT.
39 			 */
40 #ifndef NAT_SIZE
41 # ifdef LARGE_NAT
42 #  define	NAT_SIZE	2047
43 # else
44 #  define	NAT_SIZE	127
45 # endif
46 #endif
47 #ifndef RDR_SIZE
48 # ifdef LARGE_NAT
49 #  define	RDR_SIZE	2047
50 # else
51 #  define	RDR_SIZE	127
52 # endif
53 #endif
54 #ifndef HOSTMAP_SIZE
55 # ifdef LARGE_NAT
56 #  define	HOSTMAP_SIZE	8191
57 # else
58 #  define	HOSTMAP_SIZE	2047
59 # endif
60 #endif
61 #ifndef NAT_TABLE_MAX
62 /*
63  * This is newly introduced and for the sake of "least surprise", the numbers
64  * present aren't what we'd normally use for creating a proper hash table.
65  */
66 # ifdef	LARGE_NAT
67 #  define	NAT_TABLE_MAX	180000
68 # else
69 #  define	NAT_TABLE_MAX	30000
70 # endif
71 #endif
72 #ifndef NAT_TABLE_SZ
73 # ifdef LARGE_NAT
74 #  define	NAT_TABLE_SZ	16383
75 # else
76 #  define	NAT_TABLE_SZ	2047
77 # endif
78 #endif
79 #ifndef	APR_LABELLEN
80 #define	APR_LABELLEN	16
81 #endif
82 
83 #define	DEF_NAT_AGE	1200     /* 10 minutes (600 seconds) */
84 
85 /*
86  * Default hi and lo watermarks used with forced flush of nat table.
87  */
88 
89 #define	NAT_FLUSH_HI	95
90 #define	NAT_FLUSH_LO	75
91 
92 /* How full is the nat table? */
93 
94 #define	NAT_TAB_WATER_LEVEL(x)	((x)->ifs_nat_stats.ns_inuse * 100 \
95 				/ (x)->ifs_ipf_nattable_max)
96 
97 struct ipstate;
98 struct ap_session;
99 
100 typedef	struct	nat	{
101 	ipfmutex_t	nat_lock;
102 	struct	nat	*nat_next;
103 	struct	nat	**nat_pnext;
104 	struct	nat	*nat_hnext[2];
105 	struct	nat	**nat_phnext[2];
106 	struct	hostmap	*nat_hm;
107 	void		*nat_data;
108 	struct	nat	**nat_me;
109 	struct	ipstate	*nat_state;
110 	struct	ap_session	*nat_aps;		/* proxy session */
111 	frentry_t	*nat_fr;	/* filter rule ptr if appropriate */
112 	struct	ipnat	*nat_ptr;	/* pointer back to the rule */
113 	void		*nat_ifps[2];
114 	void		*nat_sync;
115 	ipftqent_t	nat_tqe;
116 	u_32_t		nat_flags;
117 	u_32_t		nat_sumd[2];	/* ip checksum delta for data segment*/
118 	u_32_t		nat_ipsumd;	/* ip checksum delta for ip header */
119 	u_32_t		nat_mssclamp;	/* if != zero clamp MSS to this */
120 	i6addr_t	nat_inip6;
121 	i6addr_t	nat_outip6;
122 	i6addr_t	nat_oip6;		/* other ip */
123 	U_QUAD_T	nat_pkts[2];
124 	U_QUAD_T	nat_bytes[2];
125 	union	{
126 		udpinfo_t	nat_unu;
127 		tcpinfo_t	nat_unt;
128 		icmpinfo_t	nat_uni;
129 		greinfo_t	nat_ugre;
130 	} nat_un;
131 	u_short		nat_oport;		/* other port */
132 	u_short		nat_use;
133 	u_char		nat_p;			/* protocol for NAT */
134 	int		nat_dir;
135 	int		nat_ref;		/* reference count */
136 	int		nat_hv[2];
137 	char		nat_ifnames[2][LIFNAMSIZ];
138 	int		nat_rev;		/* 0 = forward, 1 = reverse */
139 	int		nat_redir;		/* copy of in_redir */
140 } nat_t;
141 
142 #define	nat_inip	nat_inip6.in4
143 #define	nat_outip	nat_outip6.in4
144 #define	nat_oip		nat_oip6.in4
145 #define	nat_age		nat_tqe.tqe_die
146 #define	nat_inport	nat_un.nat_unt.ts_sport
147 #define	nat_outport	nat_un.nat_unt.ts_dport
148 #define	nat_type	nat_un.nat_uni.ici_type
149 #define	nat_seq		nat_un.nat_uni.ici_seq
150 #define	nat_id		nat_un.nat_uni.ici_id
151 #define	nat_tcpstate	nat_tqe.tqe_state
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 
160 /*
161  * Definitions for nat_flags
162  */
163 #define	NAT_TCP		0x0001	/* IPN_TCP */
164 #define	NAT_UDP		0x0002	/* IPN_UDP */
165 #define	NAT_ICMPERR	0x0004	/* IPN_ICMPERR */
166 #define	NAT_ICMPQUERY	0x0008	/* IPN_ICMPQUERY */
167 #define	NAT_SEARCH	0x0010
168 #define	NAT_SLAVE	0x0020	/* Slave connection for a proxy */
169 #define	NAT_NOTRULEPORT	0x0040
170 
171 #define	NAT_TCPUDP	(NAT_TCP|NAT_UDP)
172 #define	NAT_TCPUDPICMP	(NAT_TCP|NAT_UDP|NAT_ICMPERR)
173 #define	NAT_TCPUDPICMPQ	(NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
174 #define	NAT_FROMRULE	(NAT_TCP|NAT_UDP)
175 
176 /* 0x0100 reserved for FI_W_SPORT */
177 /* 0x0200 reserved for FI_W_DPORT */
178 /* 0x0400 reserved for FI_W_SADDR */
179 /* 0x0800 reserved for FI_W_DADDR */
180 /* 0x1000 reserved for FI_W_NEWFR */
181 /* 0x2000 reserved for SI_CLONE */
182 /* 0x4000 reserved for SI_CLONED */
183 /* 0x8000 reserved for SI_IGNOREPKT */
184 
185 #define	NAT_DEBUG	0x800000
186 
187 typedef	struct	ipnat	{
188 	struct	ipnat	*in_next;		/* NAT rule list next */
189 	struct	ipnat	*in_rnext;		/* rdr rule hash next */
190 	struct	ipnat	**in_prnext;		/* prior rdr next ptr */
191 	struct	ipnat	*in_mnext;		/* map rule hash next */
192 	struct	ipnat	**in_pmnext;		/* prior map next ptr */
193 	struct	ipftq	*in_tqehead[2];
194 	void		*in_ifps[2];
195 	void		*in_apr;
196 	char		*in_comment;
197 	i6addr_t	in_next6;
198 	u_long		in_space;
199 	u_long		in_hits;
200 	u_int		in_use;
201 	u_int		in_hv;
202 	int		in_flineno;		/* conf. file line number */
203 	u_short		in_pnext;
204 	u_char		in_v;
205 	u_char		in_xxx;
206 	/* From here to the end is covered by IPN_CMPSIZ */
207 	u_32_t		in_flags;
208 	u_32_t		in_mssclamp;		/* if != 0 clamp MSS to this */
209 	u_int		in_age[2];
210 	int		in_redir;		/* see below for values */
211 	int		in_p;			/* protocol. */
212 	i6addr_t	in_in[2];
213 	i6addr_t	in_out[2];
214 	i6addr_t	in_src[2];
215 	frtuc_t		in_tuc;
216 	u_short		in_port[2];
217 	u_short		in_ppip;		/* ports per IP. */
218 	u_short		in_ippip;		/* IP #'s per IP# */
219 	char		in_ifnames[2][LIFNAMSIZ];
220 	char		in_plabel[APR_LABELLEN];	/* proxy label. */
221 	ipftag_t	in_tag;
222 } ipnat_t;
223 
224 #define	in_pmin		in_port[0]	/* Also holds static redir port */
225 #define	in_pmax		in_port[1]
226 #define	in_nextip	in_next6.in4
227 #define	in_nip		in_next6.in4.s_addr
228 #define	in_inip		in_in[0].in4.s_addr
229 #define	in_inmsk	in_in[1].in4.s_addr
230 #define	in_outip	in_out[0].in4.s_addr
231 #define	in_outmsk	in_out[1].in4.s_addr
232 #define	in_srcip	in_src[0].in4.s_addr
233 #define	in_srcmsk	in_src[1].in4.s_addr
234 #define	in_scmp		in_tuc.ftu_scmp
235 #define	in_dcmp		in_tuc.ftu_dcmp
236 #define	in_stop		in_tuc.ftu_stop
237 #define	in_dtop		in_tuc.ftu_dtop
238 #define	in_sport	in_tuc.ftu_sport
239 #define	in_dport	in_tuc.ftu_dport
240 
241 /*
242  * Bit definitions for in_flags
243  */
244 #define	IPN_ANY		0x00000
245 #define	IPN_TCP		0x00001
246 #define	IPN_UDP		0x00002
247 #define	IPN_TCPUDP	(IPN_TCP|IPN_UDP)
248 #define	IPN_ICMPERR	0x00004
249 #define	IPN_TCPUDPICMP	(IPN_TCP|IPN_UDP|IPN_ICMPERR)
250 #define	IPN_ICMPQUERY	0x00008
251 #define	IPN_TCPUDPICMPQ	(IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
252 #define	IPN_RF		(IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
253 #define	IPN_AUTOPORTMAP	0x00010
254 #define	IPN_IPRANGE	0x00020
255 #define	IPN_FILTER	0x00040
256 #define	IPN_SPLIT	0x00080
257 #define	IPN_ROUNDR	0x00100
258 #define	IPN_NOTSRC	0x04000
259 #define	IPN_NOTDST	0x08000
260 #define	IPN_DYNSRCIP	0x10000	/* dynamic src IP# */
261 #define	IPN_DYNDSTIP	0x20000	/* dynamic dst IP# */
262 #define	IPN_DELETE	0x40000
263 #define	IPN_STICKY	0x80000
264 #define	IPN_FRAG	0x100000
265 #define	IPN_FIXEDDPORT	0x200000
266 #define	IPN_FINDFORWARD	0x400000
267 #define	IPN_IN		0x800000
268 #define	IPN_USERFLAGS	(IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\
269 			 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\
270 			 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY)
271 
272 /*
273  * Values for in_redir
274  */
275 #define	NAT_MAP		0x01
276 #define	NAT_REDIRECT	0x02
277 #define	NAT_BIMAP	(NAT_MAP|NAT_REDIRECT)
278 #define	NAT_MAPBLK	0x04
279 
280 #define	MAPBLK_MINPORT	1024	/* don't use reserved ports for src port */
281 #define	USABLE_PORTS	(65536 - MAPBLK_MINPORT)
282 
283 #define	IPN_CMPSIZ	(sizeof(ipnat_t) - offsetof(ipnat_t, in_flags))
284 
285 typedef	struct	natlookup {
286 	struct	in_addr	nl_inip;
287 	struct	in_addr	nl_outip;
288 	struct	in_addr	nl_realip;
289 	int	nl_flags;
290 	u_short	nl_inport;
291 	u_short	nl_outport;
292 	u_short	nl_realport;
293 } natlookup_t;
294 
295 
296 typedef struct  nat_save    {
297 	void	*ipn_next;
298 	struct	nat	ipn_nat;
299 	struct	ipnat	ipn_ipnat;
300 	struct	frentry ipn_fr;
301 	int	ipn_dsize;
302 	char	ipn_data[4];
303 } nat_save_t;
304 
305 #define	ipn_rule	ipn_nat.nat_fr
306 
307 typedef	struct	natget	{
308 	void	*ng_ptr;
309 	int	ng_sz;
310 } natget_t;
311 
312 
313 #undef	tr_flags
314 typedef	struct	nattrpnt	{
315 	struct	in_addr	tr_dstip;	/* real destination IP# */
316 	struct	in_addr	tr_srcip;	/* real source IP# */
317 	struct	in_addr	tr_locip;	/* local source IP# */
318 	u_int	tr_flags;
319 	int	tr_expire;
320 	u_short	tr_dstport;	/* real destination port# */
321 	u_short	tr_srcport;	/* real source port# */
322 	u_short	tr_locport;	/* local source port# */
323 	struct	nattrpnt	*tr_hnext;
324 	struct	nattrpnt	**tr_phnext;
325 	struct	nattrpnt	*tr_next;
326 	struct	nattrpnt	**tr_pnext;	/* previous next */
327 } nattrpnt_t;
328 
329 #define	TN_CMPSIZ	offsetof(nattrpnt_t, tr_hnext)
330 
331 
332 /*
333  * This structure gets used to help NAT sessions keep the same NAT rule (and
334  * thus translation for IP address) when:
335  * (a) round-robin redirects are in use
336  * (b) different IP add
337  */
338 typedef	struct	hostmap	{
339 	struct	hostmap	*hm_next;
340 	struct	hostmap	**hm_pnext;
341 	struct	hostmap	*hm_hnext;
342 	struct	hostmap	**hm_phnext;
343 	struct	ipnat	*hm_ipnat;
344 	struct	in_addr	hm_srcip;
345 	struct	in_addr	hm_dstip;
346 	struct	in_addr	hm_mapip;
347 	u_32_t		hm_port;
348 	int		hm_ref;
349 } hostmap_t;
350 
351 
352 /*
353  * Structure used to pass information in to nat_newmap and nat_newrdr.
354  */
355 typedef struct	natinfo	{
356 	ipnat_t		*nai_np;
357 	u_32_t		nai_sum1;
358 	u_32_t		nai_sum2;
359 	u_32_t		nai_nflags;
360 	u_32_t		nai_flags;
361 	struct	in_addr	nai_ip;
362 	u_short		nai_port;
363 	u_short		nai_nport;
364 	u_short		nai_sport;
365 	u_short		nai_dport;
366 } natinfo_t;
367 
368 
369 typedef	struct	natstat	{
370 	u_long	ns_mapped[2];
371 	u_long	ns_rules;
372 	u_long	ns_added;
373 	u_long	ns_expire;
374 	u_long	ns_inuse;
375 	u_long	ns_logged;
376 	u_long	ns_logfail;
377 	u_long	ns_memfail;
378 	u_long	ns_badnat;
379 	u_long	ns_addtrpnt;
380 	nat_t	**ns_table[2];
381 	hostmap_t **ns_maptable;
382 	ipnat_t	*ns_list;
383 	void	*ns_apslist;
384 	u_int	ns_wilds;
385 	u_int	ns_nattab_sz;
386 	u_int	ns_nattab_max;
387 	u_int	ns_rultab_sz;
388 	u_int	ns_rdrtab_sz;
389 	u_int	ns_trpntab_sz;
390 	u_int	ns_hostmap_sz;
391 	nat_t	*ns_instances;
392 	nattrpnt_t *ns_trpntlist;
393 	hostmap_t *ns_maplist;
394 	u_long	*ns_bucketlen[2];
395 } natstat_t;
396 
397 typedef	struct	natlog {
398 	struct	in_addr	nl_origip;
399 	struct	in_addr	nl_outip;
400 	struct	in_addr	nl_inip;
401 	u_short	nl_origport;
402 	u_short	nl_outport;
403 	u_short	nl_inport;
404 	u_short	nl_type;
405 	int	nl_rule;
406 	U_QUAD_T	nl_pkts[2];
407 	U_QUAD_T	nl_bytes[2];
408 	u_char	nl_p;
409 } natlog_t;
410 
411 
412 #define	NL_NEWMAP	NAT_MAP
413 #define	NL_NEWRDR	NAT_REDIRECT
414 #define	NL_NEWBIMAP	NAT_BIMAP
415 #define	NL_NEWBLOCK	NAT_MAPBLK
416 #define	NL_CLONE	0xfffd
417 #define	NL_FLUSH	0xfffe
418 #define	NL_EXPIRE	0xffff
419 
420 #define	NAT_HASH_FN(k,l,m)	(((k) + ((k) >> 12) + l) % (m))
421 
422 #define	LONG_SUM(in)	(((in) & 0xffff) + ((in) >> 16))
423 
424 #define	CALC_SUMD(s1, s2, sd) { \
425 			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
426 			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
427 			    /* Do it twice */ \
428 			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
429 			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
430 			    /* Because ~1 == -2, We really need ~1 == -1 */ \
431 			    if ((s1) > (s2)) (s2)--; \
432 			    (sd) = (s2) - (s1); \
433 			    (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
434 
435 #define	NAT_SYSSPACE		0x80000000
436 #define	NAT_LOCKHELD		0x40000000
437 
438 extern	void	fr_natsync __P((void *, ipf_stack_t *));
439 extern	void	fr_nataddrsync __P((void *, struct in_addr *, ipf_stack_t *));
440 extern	void	fr_natifpsync __P((int, void *, char *, ipf_stack_t *));
441 
442 #if defined(__OpenBSD__)
443 extern	void	nat_ifdetach __P((void *, ipf_stack_t *));
444 #endif
445 extern	int	fr_nat_ioctl __P((caddr_t, ioctlcmd_t, int, int, void *, ipf_stack_t *));
446 extern	int	fr_natinit __P((ipf_stack_t *));
447 extern	nat_t	*nat_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int));
448 extern	nat_t	*nat_outlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
449 				 struct in_addr));
450 extern	void	fix_datacksum __P((u_short *, u_32_t));
451 extern	nat_t	*nat_inlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
452 				struct in_addr));
453 extern	nat_t	*nat_tnlookup __P((fr_info_t *, int));
454 extern	nat_t	*nat_maplookup __P((void *, u_int, struct in_addr,
455 				struct in_addr));
456 extern	nat_t	*nat_lookupredir __P((natlookup_t *, ipf_stack_t *));
457 extern	nat_t	*nat_icmperrorlookup __P((fr_info_t *, int));
458 extern	nat_t	*nat_icmperror __P((fr_info_t *, u_int *, int));
459 extern	int	nat_insert __P((nat_t *, int, ipf_stack_t *));
460 
461 extern	int	fr_checknatout __P((fr_info_t *, u_32_t *));
462 extern	int	fr_natout __P((fr_info_t *, nat_t *, int, u_32_t));
463 extern	int	fr_checknatin __P((fr_info_t *, u_32_t *));
464 extern	int	fr_natin __P((fr_info_t *, nat_t *, int, u_32_t));
465 extern	void	fr_natunload __P((ipf_stack_t *));
466 extern	void	fr_natexpire __P((ipf_stack_t *));
467 extern	void	nat_log __P((struct nat *, u_int, ipf_stack_t *));
468 extern	void	fix_incksum __P((u_short *, u_32_t));
469 extern	void	fix_outcksum __P((u_short *, u_32_t));
470 extern  void    fr_ipnatderef __P((ipnat_t **, ipf_stack_t *));
471 extern	void	fr_natderef __P((nat_t **, ipf_stack_t *));
472 extern	u_short	*nat_proto __P((fr_info_t *, nat_t *, u_int));
473 extern	void	nat_update __P((fr_info_t *, nat_t *, ipnat_t *));
474 extern	void	fr_setnatqueue __P((nat_t *, int, ipf_stack_t *));
475 extern  void    fr_hostmapderef __P((hostmap_t **));
476 
477 #endif /* __IP_NAT_H__ */
478