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