2 * Copyright (C) 1995-2001, 2003 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
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 $
9 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
10 * Use is subject to license terms.
17 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
20 #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
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)
26 #define SIOCADNAT _IOW(r, 60, struct ipfobj)
27 #define SIOCRMNAT _IOW(r, 61, struct ipfobj)
28 #define SIOCGNATS _IOWR(r, 62, struct ipfobj)
29 #define SIOCGNATL _IOWR(r, 63, struct ipfobj)
32 #undef LARGE_NAT /* define this if you're setting up a system to NAT
33 * LARGE numbers of networks/hosts - i.e. in the
34 * hundreds or thousands. In such a case, you should
35 * also change the RDR_SIZE and NAT_SIZE below to more
36 * appropriate sizes. The figures below were used for
37 * a setup with 1000-2000 networks to NAT.
41 # define NAT_SIZE 2047
48 # define RDR_SIZE 2047
55 # define HOSTMAP_SIZE 8191
57 # define HOSTMAP_SIZE 2047
62 * This is newly introduced and for the sake of "least surprise", the numbers
63 * present aren't what we'd normally use for creating a proper hash table.
66 # define NAT_TABLE_MAX 180000
68 # define NAT_TABLE_MAX 30000
73 # define NAT_TABLE_SZ 16383
75 # define NAT_TABLE_SZ 2047
79 #define APR_LABELLEN 16
82 #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */
90 struct nat
**nat_pnext
;
91 struct nat
*nat_hnext
[2];
92 struct nat
**nat_phnext
[2];
93 struct hostmap
*nat_hm
;
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 */
104 u_32_t nat_sumd
[2]; /* ip checksum delta for data segment */
105 u_32_t nat_ipsumd
; /* ip checksum delta for ip header */
106 u_32_t nat_mssclamp
; /* if != zero clamp MSS to this */
109 i6addr_t nat_oip6
; /* other ip */
110 U_QUAD_T nat_pkts
[2];
111 U_QUAD_T nat_bytes
[2];
118 u_short nat_oport
; /* other port */
120 u_char nat_p
; /* protocol for NAT */
122 int nat_ref
; /* reference count */
124 char nat_ifnames
[2][LIFNAMSIZ
];
125 int nat_rev
; /* 0 = forward, 1 = reverse */
127 int nat_redir
; /* copy of in_redir */
130 #define nat_inip nat_inip6.in4
131 #define nat_outip nat_outip6.in4
132 #define nat_oip nat_oip6.in4
133 #define nat_age nat_tqe.tqe_die
134 #define nat_inport nat_un.nat_unt.ts_sport
135 #define nat_outport nat_un.nat_unt.ts_dport
136 #define nat_type nat_un.nat_uni.ici_type
137 #define nat_seq nat_un.nat_uni.ici_seq
138 #define nat_id nat_un.nat_uni.ici_id
139 #define nat_tcpstate nat_tqe.tqe_state
144 #define NAT_INBOUND 0
145 #define NAT_OUTBOUND 1
148 * Definitions for nat_flags
150 #define NAT_TCP 0x0001 /* IPN_TCP */
151 #define NAT_UDP 0x0002 /* IPN_UDP */
152 #define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */
153 #define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */
154 #define NAT_SEARCH 0x0010
155 #define NAT_SLAVE 0x0020 /* Slave connection for a proxy */
156 #define NAT_NOTRULEPORT 0x0040
158 #define NAT_TCPUDP (NAT_TCP|NAT_UDP)
159 #define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR)
160 #define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
161 #define NAT_FROMRULE (NAT_TCP|NAT_UDP)
163 /* 0x0100 reserved for FI_W_SPORT */
164 /* 0x0200 reserved for FI_W_DPORT */
165 /* 0x0400 reserved for FI_W_SADDR */
166 /* 0x0800 reserved for FI_W_DADDR */
167 /* 0x1000 reserved for FI_W_NEWFR */
168 /* 0x2000 reserved for SI_CLONE */
169 /* 0x4000 reserved for SI_CLONED */
170 /* 0x8000 reserved for SI_IGNOREPKT */
172 #define NAT_DEBUG 0x800000
174 typedef struct ipnat
{
175 struct ipnat
*in_next
; /* NAT rule list next */
176 struct ipnat
*in_rnext
; /* rdr rule hash next */
177 struct ipnat
**in_prnext
; /* prior rdr next ptr */
178 struct ipnat
*in_mnext
; /* map rule hash next */
179 struct ipnat
**in_pmnext
; /* prior map next ptr */
180 struct ipftq
*in_tqehead
[2];
189 int in_flineno
; /* conf. file line number */
193 /* From here to the end is covered by IPN_CMPSIZ */
195 u_32_t in_mssclamp
; /* if != 0 clamp MSS to this */
197 int in_redir
; /* see below for values */
198 int in_p
; /* protocol. */
204 u_short in_ppip
; /* ports per IP. */
205 u_short in_ippip
; /* IP #'s per IP# */
206 char in_ifnames
[2][LIFNAMSIZ
];
207 char in_plabel
[APR_LABELLEN
]; /* proxy label. */
211 #define in_pmin in_port[0] /* Also holds static redir port */
212 #define in_pmax in_port[1]
213 #define in_nextip in_next6.in4
214 #define in_nip in_next6.in4.s_addr
215 #define in_inip in_in[0].in4.s_addr
216 #define in_inmsk in_in[1].in4.s_addr
217 #define in_outip in_out[0].in4.s_addr
218 #define in_outmsk in_out[1].in4.s_addr
219 #define in_srcip in_src[0].in4.s_addr
220 #define in_srcmsk in_src[1].in4.s_addr
221 #define in_scmp in_tuc.ftu_scmp
222 #define in_dcmp in_tuc.ftu_dcmp
223 #define in_stop in_tuc.ftu_stop
224 #define in_dtop in_tuc.ftu_dtop
225 #define in_sport in_tuc.ftu_sport
226 #define in_dport in_tuc.ftu_dport
229 * Bit definitions for in_flags
231 #define IPN_ANY 0x00000
232 #define IPN_TCP 0x00001
233 #define IPN_UDP 0x00002
234 #define IPN_TCPUDP (IPN_TCP|IPN_UDP)
235 #define IPN_ICMPERR 0x00004
236 #define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR)
237 #define IPN_ICMPQUERY 0x00008
238 #define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
239 #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
240 #define IPN_AUTOPORTMAP 0x00010
241 #define IPN_IPRANGE 0x00020
242 #define IPN_FILTER 0x00040
243 #define IPN_SPLIT 0x00080
244 #define IPN_ROUNDR 0x00100
245 #define IPN_NOTSRC 0x04000
246 #define IPN_NOTDST 0x08000
247 #define IPN_DYNSRCIP 0x10000 /* dynamic src IP# */
248 #define IPN_DYNDSTIP 0x20000 /* dynamic dst IP# */
249 #define IPN_DELETE 0x40000
250 #define IPN_STICKY 0x80000
251 #define IPN_FRAG 0x100000
252 #define IPN_FIXEDDPORT 0x200000
253 #define IPN_FINDFORWARD 0x400000
254 #define IPN_IN 0x800000
255 #define IPN_SEQUENTIAL 0x1000000
256 #define IPN_USERFLAGS (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\
257 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\
258 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY|\
262 * Values for in_redir
265 #define NAT_REDIRECT 0x02
266 #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT)
267 #define NAT_MAPBLK 0x04
269 #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */
270 #define USABLE_PORTS (65536 - MAPBLK_MINPORT)
272 #define IPN_CMPSIZ (sizeof (ipnat_t) - offsetof(ipnat_t, in_flags))
274 typedef struct natlookup
{
275 i6addr_t nl_inipaddr
;
276 i6addr_t nl_outipaddr
;
277 i6addr_t nl_realipaddr
;
285 #define nl_inip nl_inipaddr.in4
286 #define nl_outip nl_outipaddr.in4
287 #define nl_realip nl_realipaddr.in4
288 #define nl_inip6 nl_inipaddr.in6
289 #define nl_outip6 nl_outipaddr.in6
290 #define nl_realip6 nl_realipaddr.in6
293 typedef struct nat_save
{
296 struct ipnat ipn_ipnat
;
297 struct frentry ipn_fr
;
302 #define ipn_rule ipn_nat.nat_fr
304 typedef struct natget
{
311 typedef struct nattrpnt
{
312 struct in_addr tr_dstip
; /* real destination IP# */
313 struct in_addr tr_srcip
; /* real source IP# */
314 struct in_addr tr_locip
; /* local source IP# */
317 u_short tr_dstport
; /* real destination port# */
318 u_short tr_srcport
; /* real source port# */
319 u_short tr_locport
; /* local source port# */
320 struct nattrpnt
*tr_hnext
;
321 struct nattrpnt
**tr_phnext
;
322 struct nattrpnt
*tr_next
;
323 struct nattrpnt
**tr_pnext
; /* previous next */
326 #define TN_CMPSIZ offsetof(nattrpnt_t, tr_hnext)
330 * This structure gets used to help NAT sessions keep the same NAT rule (and
331 * thus translation for IP address) when:
332 * (a) round-robin redirects are in use
333 * (b) different IP add
335 typedef struct hostmap
{
336 struct hostmap
*hm_next
;
337 struct hostmap
**hm_pnext
;
338 struct hostmap
*hm_hnext
;
339 struct hostmap
**hm_phnext
;
340 struct ipnat
*hm_ipnat
;
349 #define hm_srcip hm_src.in4
350 #define hm_dstip hm_dst.in4
351 #define hm_mapip hm_map.in4
352 #define hm_srcip6 hm_src.in6
353 #define hm_dstip6 hm_dst.in6
354 #define hm_mapip6 hm_map.in6
358 * Structure used to pass information in to nat_newmap and nat_newrdr.
360 typedef struct natinfo
{
364 struct in_addr nai_ip
;
372 typedef struct natstat
{
384 hostmap_t
**ns_maptable
;
395 nattrpnt_t
*ns_trpntlist
;
396 hostmap_t
*ns_maplist
;
397 u_long
*ns_bucketlen
[2];
399 u_long ns_uncreate
[2][2];
402 typedef struct natlog
{
406 u_short nlg_origport
;
411 U_QUAD_T nlg_pkts
[2];
412 U_QUAD_T nlg_bytes
[2];
418 #define NL_NEWMAP NAT_MAP
419 #define NL_NEWRDR NAT_REDIRECT
420 #define NL_NEWBIMAP NAT_BIMAP
421 #define NL_NEWBLOCK NAT_MAPBLK
422 #define NL_DESTROY 0xfffc
423 #define NL_CLONE 0xfffd
424 #define NL_FLUSH 0xfffe
425 #define NL_EXPIRE 0xffff
427 #define NAT_HASH_FN(k, l, m) (((k) + ((k) >> 12) + l) % (m))
428 #define NAT_HASH_FN6(k, l, m) ((((u_32_t *)(k))[3] \
429 + (((u_32_t *)(k))[3] >> 12) \
430 + (((u_32_t *)(k))[2]) \
431 + (((u_32_t *)(k))[2] >> 12) \
432 + (((u_32_t *)(k))[1]) \
433 + (((u_32_t *)(k))[1] >> 12) \
434 + (((u_32_t *)(k))[0]) \
435 + (((u_32_t *)(k))[0] >> 12) \
438 #define LONG_SUM(in) (((in) & 0xffff) + ((in) >> 16))
439 #define LONG_SUM6(in) (LONG_SUM(ntohl(((u_32_t *)(in))[0])) + \
440 LONG_SUM(ntohl(((u_32_t *)(in))[1])) + \
441 LONG_SUM(ntohl(((u_32_t *)(in))[2])) + \
442 LONG_SUM(ntohl(((u_32_t *)(in))[3])))
444 #define CALC_SUMD(s1, s2, sd) { \
445 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
446 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
448 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
449 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
450 /* Because ~1 == -2, We really need ~1 == -1 */ \
451 if ((s1) > (s2)) (s2)--; \
452 (sd) = (s2) - (s1); \
453 (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
455 #define NAT_SYSSPACE 0x80000000
456 #define NAT_LOCKHELD 0x40000000
458 extern void fr_natsync
__P((void *, ipf_stack_t
*));
459 extern void fr_nataddrsync
__P((int, void *, void *, ipf_stack_t
*));
460 extern void fr_natifpsync
__P((int, int, void *, char *, ipf_stack_t
*));
462 #if defined(__OpenBSD__)
463 extern void nat_ifdetach
__P((void *, ipf_stack_t
*));
465 extern int fr_nat_ioctl
__P((caddr_t
, ioctlcmd_t
, int, int, void *, ipf_stack_t
*));
466 extern int fr_natinit
__P((ipf_stack_t
*));
467 extern nat_t
*nat_new
__P((fr_info_t
*, ipnat_t
*, nat_t
**, u_int
, int));
468 extern nat_t
*nat_outlookup
__P((fr_info_t
*, u_int
, u_int
, struct in_addr
,
470 extern void fix_datacksum
__P((u_short
*, u_32_t
));
471 extern nat_t
*nat_inlookup
__P((fr_info_t
*, u_int
, u_int
, struct in_addr
,
473 extern nat_t
*nat_tnlookup
__P((fr_info_t
*, int));
474 extern nat_t
*nat_maplookup
__P((void *, u_int
, struct in_addr
,
476 extern nat_t
*nat_lookupredir
__P((natlookup_t
*, ipf_stack_t
*));
477 extern nat_t
*nat_icmperrorlookup
__P((fr_info_t
*, int));
478 extern nat_t
*nat_icmperror
__P((fr_info_t
*, u_int
*, int));
479 extern int nat_delete
__P((struct nat
*, int, ipf_stack_t
*));
480 extern int nat_insert
__P((nat_t
*, int, ipf_stack_t
*));
482 extern int fr_checknatout
__P((fr_info_t
*, u_32_t
*));
483 extern int fr_natout
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
484 extern int fr_checknatin
__P((fr_info_t
*, u_32_t
*));
485 extern int fr_natin
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
486 extern void fr_natunload
__P((ipf_stack_t
*));
487 extern void nat_uncreate
__P((fr_info_t
*));
488 extern void fr_natexpire
__P((ipf_stack_t
*));
489 extern void nat_log
__P((struct nat
*, u_int
, ipf_stack_t
*));
490 extern void fix_incksum
__P((u_short
*, u_32_t
));
491 extern void fix_outcksum
__P((u_short
*, u_32_t
));
492 extern void fr_ipnatderef
__P((ipnat_t
**, ipf_stack_t
*));
493 extern void fr_natderef
__P((nat_t
**, ipf_stack_t
*));
494 extern u_short
*nat_proto
__P((fr_info_t
*, nat_t
*, u_int
));
495 extern void nat_update
__P((fr_info_t
*, nat_t
*, ipnat_t
*));
496 extern void fr_setnatqueue
__P((nat_t
*, int, ipf_stack_t
*));
497 extern void fr_hostmapdel
__P((hostmap_t
**));
499 extern void fr_natifindexsync
__P((void *, void *, ipf_stack_t
*));
502 extern nat_t
*fr_natclone
__P((fr_info_t
*, nat_t
*));
503 extern void nat_delrdr
__P((struct ipnat
*));
504 extern void nat_delnat
__P((struct ipnat
*));
505 extern int nat_wildok
__P((nat_t
*, int, int, int, int));
506 extern void nat_calc_chksum_diffs
__P((nat_t
*));
509 extern void nat6_addnat
__P((ipnat_t
*, ipf_stack_t
*));
510 extern void nat6_addrdr
__P((ipnat_t
*, ipf_stack_t
*));
511 extern nat_t
*nat6_new
__P((fr_info_t
*, ipnat_t
*, nat_t
**, u_int
, int));
512 extern nat_t
*nat6_outlookup
__P((fr_info_t
*, u_int
, u_int
,
513 struct in6_addr
*, struct in6_addr
*));
514 extern nat_t
*nat6_inlookup
__P((fr_info_t
*, u_int
, u_int
,
515 struct in6_addr
*, struct in6_addr
*));
516 extern nat_t
*nat6_lookupredir
__P((natlookup_t
*, ipf_stack_t
*));
517 extern nat_t
*nat6_icmperrorlookup
__P((fr_info_t
*, int));
518 extern nat_t
*nat6_icmperror
__P((fr_info_t
*, u_int
*, int));
519 extern int nat6_insert
__P((nat_t
*, int, ipf_stack_t
*));
520 extern int fr_checknat6out
__P((fr_info_t
*, u_32_t
*));
521 extern int fr_nat6out
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
522 extern int fr_checknat6in
__P((fr_info_t
*, u_32_t
*));
523 extern int fr_nat6in
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
526 #endif /* __IP_NAT_H__ */