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.
19 #if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
25 #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
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)
31 #define SIOCADNAT _IOW(r, 60, struct ipfobj)
32 #define SIOCRMNAT _IOW(r, 61, struct ipfobj)
33 #define SIOCGNATS _IOWR(r, 62, struct ipfobj)
34 #define SIOCGNATL _IOWR(r, 63, struct ipfobj)
37 #undef LARGE_NAT /* define this if you're setting up a system to NAT
38 * LARGE numbers of networks/hosts - i.e. in the
39 * hundreds or thousands. In such a case, you should
40 * also change the RDR_SIZE and NAT_SIZE below to more
41 * appropriate sizes. The figures below were used for
42 * a setup with 1000-2000 networks to NAT.
46 # define NAT_SIZE 2047
53 # define RDR_SIZE 2047
60 # define HOSTMAP_SIZE 8191
62 # define HOSTMAP_SIZE 2047
67 * This is newly introduced and for the sake of "least surprise", the numbers
68 * present aren't what we'd normally use for creating a proper hash table.
71 # define NAT_TABLE_MAX 180000
73 # define NAT_TABLE_MAX 30000
78 # define NAT_TABLE_SZ 16383
80 # define NAT_TABLE_SZ 2047
84 #define APR_LABELLEN 16
87 #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */
95 struct nat
**nat_pnext
;
96 struct nat
*nat_hnext
[2];
97 struct nat
**nat_phnext
[2];
98 struct hostmap
*nat_hm
;
101 struct ipstate
*nat_state
;
102 struct ap_session
*nat_aps
; /* proxy session */
103 frentry_t
*nat_fr
; /* filter rule ptr if appropriate */
104 struct ipnat
*nat_ptr
; /* pointer back to the rule */
109 u_32_t nat_sumd
[2]; /* ip checksum delta for data segment */
110 u_32_t nat_ipsumd
; /* ip checksum delta for ip header */
111 u_32_t nat_mssclamp
; /* if != zero clamp MSS to this */
114 i6addr_t nat_oip6
; /* other ip */
115 U_QUAD_T nat_pkts
[2];
116 U_QUAD_T nat_bytes
[2];
123 u_short nat_oport
; /* other port */
125 u_char nat_p
; /* protocol for NAT */
127 int nat_ref
; /* reference count */
129 char nat_ifnames
[2][LIFNAMSIZ
];
130 int nat_rev
; /* 0 = forward, 1 = reverse */
132 int nat_redir
; /* copy of in_redir */
135 #define nat_inip nat_inip6.in4
136 #define nat_outip nat_outip6.in4
137 #define nat_oip nat_oip6.in4
138 #define nat_age nat_tqe.tqe_die
139 #define nat_inport nat_un.nat_unt.ts_sport
140 #define nat_outport nat_un.nat_unt.ts_dport
141 #define nat_type nat_un.nat_uni.ici_type
142 #define nat_seq nat_un.nat_uni.ici_seq
143 #define nat_id nat_un.nat_uni.ici_id
144 #define nat_tcpstate nat_tqe.tqe_state
149 #define NAT_INBOUND 0
150 #define NAT_OUTBOUND 1
153 * Definitions for nat_flags
155 #define NAT_TCP 0x0001 /* IPN_TCP */
156 #define NAT_UDP 0x0002 /* IPN_UDP */
157 #define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */
158 #define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */
159 #define NAT_SEARCH 0x0010
160 #define NAT_SLAVE 0x0020 /* Slave connection for a proxy */
161 #define NAT_NOTRULEPORT 0x0040
163 #define NAT_TCPUDP (NAT_TCP|NAT_UDP)
164 #define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR)
165 #define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
166 #define NAT_FROMRULE (NAT_TCP|NAT_UDP)
168 /* 0x0100 reserved for FI_W_SPORT */
169 /* 0x0200 reserved for FI_W_DPORT */
170 /* 0x0400 reserved for FI_W_SADDR */
171 /* 0x0800 reserved for FI_W_DADDR */
172 /* 0x1000 reserved for FI_W_NEWFR */
173 /* 0x2000 reserved for SI_CLONE */
174 /* 0x4000 reserved for SI_CLONED */
175 /* 0x8000 reserved for SI_IGNOREPKT */
177 #define NAT_DEBUG 0x800000
179 typedef struct ipnat
{
180 struct ipnat
*in_next
; /* NAT rule list next */
181 struct ipnat
*in_rnext
; /* rdr rule hash next */
182 struct ipnat
**in_prnext
; /* prior rdr next ptr */
183 struct ipnat
*in_mnext
; /* map rule hash next */
184 struct ipnat
**in_pmnext
; /* prior map next ptr */
185 struct ipftq
*in_tqehead
[2];
194 int in_flineno
; /* conf. file line number */
198 /* From here to the end is covered by IPN_CMPSIZ */
200 u_32_t in_mssclamp
; /* if != 0 clamp MSS to this */
202 int in_redir
; /* see below for values */
203 int in_p
; /* protocol. */
209 u_short in_ppip
; /* ports per IP. */
210 u_short in_ippip
; /* IP #'s per IP# */
211 char in_ifnames
[2][LIFNAMSIZ
];
212 char in_plabel
[APR_LABELLEN
]; /* proxy label. */
216 #define in_pmin in_port[0] /* Also holds static redir port */
217 #define in_pmax in_port[1]
218 #define in_nextip in_next6.in4
219 #define in_nip in_next6.in4.s_addr
220 #define in_inip in_in[0].in4.s_addr
221 #define in_inmsk in_in[1].in4.s_addr
222 #define in_outip in_out[0].in4.s_addr
223 #define in_outmsk in_out[1].in4.s_addr
224 #define in_srcip in_src[0].in4.s_addr
225 #define in_srcmsk in_src[1].in4.s_addr
226 #define in_scmp in_tuc.ftu_scmp
227 #define in_dcmp in_tuc.ftu_dcmp
228 #define in_stop in_tuc.ftu_stop
229 #define in_dtop in_tuc.ftu_dtop
230 #define in_sport in_tuc.ftu_sport
231 #define in_dport in_tuc.ftu_dport
234 * Bit definitions for in_flags
236 #define IPN_ANY 0x00000
237 #define IPN_TCP 0x00001
238 #define IPN_UDP 0x00002
239 #define IPN_TCPUDP (IPN_TCP|IPN_UDP)
240 #define IPN_ICMPERR 0x00004
241 #define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR)
242 #define IPN_ICMPQUERY 0x00008
243 #define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
244 #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
245 #define IPN_AUTOPORTMAP 0x00010
246 #define IPN_IPRANGE 0x00020
247 #define IPN_FILTER 0x00040
248 #define IPN_SPLIT 0x00080
249 #define IPN_ROUNDR 0x00100
250 #define IPN_NOTSRC 0x04000
251 #define IPN_NOTDST 0x08000
252 #define IPN_DYNSRCIP 0x10000 /* dynamic src IP# */
253 #define IPN_DYNDSTIP 0x20000 /* dynamic dst IP# */
254 #define IPN_DELETE 0x40000
255 #define IPN_STICKY 0x80000
256 #define IPN_FRAG 0x100000
257 #define IPN_FIXEDDPORT 0x200000
258 #define IPN_FINDFORWARD 0x400000
259 #define IPN_IN 0x800000
260 #define IPN_SEQUENTIAL 0x1000000
261 #define IPN_USERFLAGS (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\
262 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\
263 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY|\
267 * Values for in_redir
270 #define NAT_REDIRECT 0x02
271 #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT)
272 #define NAT_MAPBLK 0x04
274 #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */
275 #define USABLE_PORTS (65536 - MAPBLK_MINPORT)
277 #define IPN_CMPSIZ (sizeof (ipnat_t) - offsetof(ipnat_t, in_flags))
279 typedef struct natlookup
{
280 i6addr_t nl_inipaddr
;
281 i6addr_t nl_outipaddr
;
282 i6addr_t nl_realipaddr
;
290 #define nl_inip nl_inipaddr.in4
291 #define nl_outip nl_outipaddr.in4
292 #define nl_realip nl_realipaddr.in4
293 #define nl_inip6 nl_inipaddr.in6
294 #define nl_outip6 nl_outipaddr.in6
295 #define nl_realip6 nl_realipaddr.in6
298 typedef struct nat_save
{
301 struct ipnat ipn_ipnat
;
302 struct frentry ipn_fr
;
307 #define ipn_rule ipn_nat.nat_fr
309 typedef struct natget
{
316 typedef struct nattrpnt
{
317 struct in_addr tr_dstip
; /* real destination IP# */
318 struct in_addr tr_srcip
; /* real source IP# */
319 struct in_addr tr_locip
; /* local source IP# */
322 u_short tr_dstport
; /* real destination port# */
323 u_short tr_srcport
; /* real source port# */
324 u_short tr_locport
; /* local source port# */
325 struct nattrpnt
*tr_hnext
;
326 struct nattrpnt
**tr_phnext
;
327 struct nattrpnt
*tr_next
;
328 struct nattrpnt
**tr_pnext
; /* previous next */
331 #define TN_CMPSIZ offsetof(nattrpnt_t, tr_hnext)
335 * This structure gets used to help NAT sessions keep the same NAT rule (and
336 * thus translation for IP address) when:
337 * (a) round-robin redirects are in use
338 * (b) different IP add
340 typedef struct hostmap
{
341 struct hostmap
*hm_next
;
342 struct hostmap
**hm_pnext
;
343 struct hostmap
*hm_hnext
;
344 struct hostmap
**hm_phnext
;
345 struct ipnat
*hm_ipnat
;
354 #define hm_srcip hm_src.in4
355 #define hm_dstip hm_dst.in4
356 #define hm_mapip hm_map.in4
357 #define hm_srcip6 hm_src.in6
358 #define hm_dstip6 hm_dst.in6
359 #define hm_mapip6 hm_map.in6
363 * Structure used to pass information in to nat_newmap and nat_newrdr.
365 typedef struct natinfo
{
369 struct in_addr nai_ip
;
377 typedef struct natstat
{
389 hostmap_t
**ns_maptable
;
400 nattrpnt_t
*ns_trpntlist
;
401 hostmap_t
*ns_maplist
;
402 u_long
*ns_bucketlen
[2];
404 u_long ns_uncreate
[2][2];
407 typedef struct natlog
{
411 u_short nlg_origport
;
416 U_QUAD_T nlg_pkts
[2];
417 U_QUAD_T nlg_bytes
[2];
423 #define NL_NEWMAP NAT_MAP
424 #define NL_NEWRDR NAT_REDIRECT
425 #define NL_NEWBIMAP NAT_BIMAP
426 #define NL_NEWBLOCK NAT_MAPBLK
427 #define NL_DESTROY 0xfffc
428 #define NL_CLONE 0xfffd
429 #define NL_FLUSH 0xfffe
430 #define NL_EXPIRE 0xffff
432 #define NAT_HASH_FN(k, l, m) (((k) + ((k) >> 12) + l) % (m))
433 #define NAT_HASH_FN6(k, l, m) ((((u_32_t *)(k))[3] \
434 + (((u_32_t *)(k))[3] >> 12) \
435 + (((u_32_t *)(k))[2]) \
436 + (((u_32_t *)(k))[2] >> 12) \
437 + (((u_32_t *)(k))[1]) \
438 + (((u_32_t *)(k))[1] >> 12) \
439 + (((u_32_t *)(k))[0]) \
440 + (((u_32_t *)(k))[0] >> 12) \
443 #define LONG_SUM(in) (((in) & 0xffff) + ((in) >> 16))
444 #define LONG_SUM6(in) (LONG_SUM(ntohl(((u_32_t *)(in))[0])) + \
445 LONG_SUM(ntohl(((u_32_t *)(in))[1])) + \
446 LONG_SUM(ntohl(((u_32_t *)(in))[2])) + \
447 LONG_SUM(ntohl(((u_32_t *)(in))[3])))
449 #define CALC_SUMD(s1, s2, sd) { \
450 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
451 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
453 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
454 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
455 /* Because ~1 == -2, We really need ~1 == -1 */ \
456 if ((s1) > (s2)) (s2)--; \
457 (sd) = (s2) - (s1); \
458 (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
460 #define NAT_SYSSPACE 0x80000000
461 #define NAT_LOCKHELD 0x40000000
463 extern void fr_natsync
__P((void *, ipf_stack_t
*));
464 extern void fr_nataddrsync
__P((int, void *, void *, ipf_stack_t
*));
465 extern void fr_natifpsync
__P((int, int, void *, char *, ipf_stack_t
*));
467 #if defined(__OpenBSD__)
468 extern void nat_ifdetach
__P((void *, ipf_stack_t
*));
470 extern int fr_nat_ioctl
__P((caddr_t
, ioctlcmd_t
, int, int, void *, ipf_stack_t
*));
471 extern int fr_natinit
__P((ipf_stack_t
*));
472 extern nat_t
*nat_new
__P((fr_info_t
*, ipnat_t
*, nat_t
**, u_int
, int));
473 extern nat_t
*nat_outlookup
__P((fr_info_t
*, u_int
, u_int
, struct in_addr
,
475 extern void fix_datacksum
__P((u_short
*, u_32_t
));
476 extern nat_t
*nat_inlookup
__P((fr_info_t
*, u_int
, u_int
, struct in_addr
,
478 extern nat_t
*nat_tnlookup
__P((fr_info_t
*, int));
479 extern nat_t
*nat_maplookup
__P((void *, u_int
, struct in_addr
,
481 extern nat_t
*nat_lookupredir
__P((natlookup_t
*, ipf_stack_t
*));
482 extern nat_t
*nat_icmperrorlookup
__P((fr_info_t
*, int));
483 extern nat_t
*nat_icmperror
__P((fr_info_t
*, u_int
*, int));
484 extern int nat_delete
__P((struct nat
*, int, ipf_stack_t
*));
485 extern int nat_insert
__P((nat_t
*, int, ipf_stack_t
*));
487 extern int fr_checknatout
__P((fr_info_t
*, u_32_t
*));
488 extern int fr_natout
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
489 extern int fr_checknatin
__P((fr_info_t
*, u_32_t
*));
490 extern int fr_natin
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
491 extern void fr_natunload
__P((ipf_stack_t
*));
492 extern void nat_uncreate
__P((fr_info_t
*));
493 extern void fr_natexpire
__P((ipf_stack_t
*));
494 extern void nat_log
__P((struct nat
*, u_int
, ipf_stack_t
*));
495 extern void fix_incksum
__P((u_short
*, u_32_t
));
496 extern void fix_outcksum
__P((u_short
*, u_32_t
));
497 extern void fr_ipnatderef
__P((ipnat_t
**, ipf_stack_t
*));
498 extern void fr_natderef
__P((nat_t
**, ipf_stack_t
*));
499 extern u_short
*nat_proto
__P((fr_info_t
*, nat_t
*, u_int
));
500 extern void nat_update
__P((fr_info_t
*, nat_t
*, ipnat_t
*));
501 extern void fr_setnatqueue
__P((nat_t
*, int, ipf_stack_t
*));
502 extern void fr_hostmapdel
__P((hostmap_t
**));
504 extern void fr_natifindexsync
__P((void *, void *, ipf_stack_t
*));
507 extern nat_t
*fr_natclone
__P((fr_info_t
*, nat_t
*));
508 extern void nat_delrdr
__P((struct ipnat
*));
509 extern void nat_delnat
__P((struct ipnat
*));
510 extern int nat_wildok
__P((nat_t
*, int, int, int, int));
511 extern void nat_calc_chksum_diffs
__P((nat_t
*));
514 extern void nat6_addnat
__P((ipnat_t
*, ipf_stack_t
*));
515 extern void nat6_addrdr
__P((ipnat_t
*, ipf_stack_t
*));
516 extern nat_t
*nat6_new
__P((fr_info_t
*, ipnat_t
*, nat_t
**, u_int
, int));
517 extern nat_t
*nat6_outlookup
__P((fr_info_t
*, u_int
, u_int
,
518 struct in6_addr
*, struct in6_addr
*));
519 extern nat_t
*nat6_inlookup
__P((fr_info_t
*, u_int
, u_int
,
520 struct in6_addr
*, struct in6_addr
*));
521 extern nat_t
*nat6_lookupredir
__P((natlookup_t
*, ipf_stack_t
*));
522 extern nat_t
*nat6_icmperrorlookup
__P((fr_info_t
*, int));
523 extern nat_t
*nat6_icmperror
__P((fr_info_t
*, u_int
*, int));
524 extern int nat6_insert
__P((nat_t
*, int, ipf_stack_t
*));
525 extern int fr_checknat6out
__P((fr_info_t
*, u_32_t
*));
526 extern int fr_nat6out
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
527 extern int fr_checknat6in
__P((fr_info_t
*, u_32_t
*));
528 extern int fr_nat6in
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
531 #endif /* __IP_NAT_H__ */