4 * Copyright (C) 1995-2001, 2003 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * @(#)ip_nat.h 1.5 2/4/96
9 * Id: ip_nat.h,v 2.90.2.23 2008/11/06 21:18:36 darrenr Exp
16 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
19 #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
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)
25 #define SIOCADNAT _IOW(r, 60, struct ipfobj)
26 #define SIOCRMNAT _IOW(r, 61, struct ipfobj)
27 #define SIOCGNATS _IOWR(r, 62, struct ipfobj)
28 #define SIOCGNATL _IOWR(r, 63, struct ipfobj)
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.
40 # define NAT_SIZE 2047
47 # define RDR_SIZE 2047
54 # define HOSTMAP_SIZE 8191
56 # define HOSTMAP_SIZE 2047
61 * This is newly introduced and for the sake of "least surprise", the numbers
62 * present aren't what we'd normally use for creating a proper hash table.
65 # define NAT_TABLE_MAX 180000
67 # define NAT_TABLE_MAX 30000
72 # define NAT_TABLE_SZ 16383
74 # define NAT_TABLE_SZ 2047
78 #define APR_LABELLEN 16
80 #define NAT_HW_CKSUM 0x80000000
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 */
126 int nat_redir
; /* copy of in_redir */
127 u_32_t nat_seqnext
[2];
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
140 #define nat_die nat_tqe.tqe_die
141 #define nat_touched nat_tqe.tqe_touched
146 #define NAT_INBOUND 0
147 #define NAT_OUTBOUND 1
150 * Definitions for nat_flags
152 #define NAT_TCP 0x0001 /* IPN_TCP */
153 #define NAT_UDP 0x0002 /* IPN_UDP */
154 #define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */
155 #define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */
156 #define NAT_SEARCH 0x0010
157 #define NAT_SLAVE 0x0020 /* Slave connection for a proxy */
158 #define NAT_NOTRULEPORT 0x0040 /* Don't use the port # in the NAT rule */
160 #define NAT_TCPUDP (NAT_TCP|NAT_UDP)
161 #define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR)
162 #define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
163 #define NAT_FROMRULE (NAT_TCP|NAT_UDP)
165 /* 0x0100 reserved for FI_W_SPORT */
166 /* 0x0200 reserved for FI_W_DPORT */
167 /* 0x0400 reserved for FI_W_SADDR */
168 /* 0x0800 reserved for FI_W_DADDR */
169 /* 0x1000 reserved for FI_W_NEWFR */
170 /* 0x2000 reserved for SI_CLONE */
171 /* 0x4000 reserved for SI_CLONED */
172 /* 0x8000 reserved for SI_IGNOREPKT */
174 #define NAT_DEBUG 0x800000
176 typedef struct ipnat
{
178 struct ipnat
*in_next
; /* NAT rule list next */
179 struct ipnat
*in_rnext
; /* rdr rule hash next */
180 struct ipnat
**in_prnext
; /* prior rdr next ptr */
181 struct ipnat
*in_mnext
; /* map rule hash next */
182 struct ipnat
**in_pmnext
; /* prior map next ptr */
183 struct ipftq
*in_tqehead
[2];
192 int in_flineno
; /* conf. file line number */
196 /* From here to the end is covered by IPN_CMPSIZ */
198 u_32_t in_mssclamp
; /* if != 0 clamp MSS to this */
200 int in_redir
; /* see below for values */
201 int in_p
; /* protocol. */
207 u_short in_ppip
; /* ports per IP. */
208 u_short in_ippip
; /* IP #'s per IP# */
209 char in_ifnames
[2][LIFNAMSIZ
];
210 char in_plabel
[APR_LABELLEN
]; /* proxy label. */
214 #define in_pmin in_port[0] /* Also holds static redir port */
215 #define in_pmax in_port[1]
216 #define in_nextip in_next6.in4
217 #define in_nip in_next6.in4.s_addr
218 #define in_inip in_in[0].in4.s_addr
219 #define in_inmsk in_in[1].in4.s_addr
220 #define in_outip in_out[0].in4.s_addr
221 #define in_outmsk in_out[1].in4.s_addr
222 #define in_srcip in_src[0].in4.s_addr
223 #define in_srcmsk in_src[1].in4.s_addr
224 #define in_scmp in_tuc.ftu_scmp
225 #define in_dcmp in_tuc.ftu_dcmp
226 #define in_stop in_tuc.ftu_stop
227 #define in_dtop in_tuc.ftu_dtop
228 #define in_sport in_tuc.ftu_sport
229 #define in_dport in_tuc.ftu_dport
232 * Bit definitions for in_flags
234 #define IPN_ANY 0x00000
235 #define IPN_TCP 0x00001
236 #define IPN_UDP 0x00002
237 #define IPN_TCPUDP (IPN_TCP|IPN_UDP)
238 #define IPN_ICMPERR 0x00004
239 #define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR)
240 #define IPN_ICMPQUERY 0x00008
241 #define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
242 #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
243 #define IPN_AUTOPORTMAP 0x00010
244 #define IPN_IPRANGE 0x00020
245 #define IPN_FILTER 0x00040
246 #define IPN_SPLIT 0x00080
247 #define IPN_ROUNDR 0x00100
248 #define IPN_NOTSRC 0x04000
249 #define IPN_NOTDST 0x08000
250 #define IPN_DYNSRCIP 0x10000 /* dynamic src IP# */
251 #define IPN_DYNDSTIP 0x20000 /* dynamic dst IP# */
252 #define IPN_DELETE 0x40000
253 #define IPN_STICKY 0x80000
254 #define IPN_FRAG 0x100000
255 #define IPN_FIXEDDPORT 0x200000
256 #define IPN_FINDFORWARD 0x400000
257 #define IPN_IN 0x800000
258 #define IPN_SEQUENTIAL 0x1000000
259 #define IPN_USERFLAGS (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\
260 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\
261 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY|\
265 * Values for in_redir
268 #define NAT_REDIRECT 0x02
269 #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT)
270 #define NAT_MAPBLK 0x04
272 #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */
273 #define USABLE_PORTS (65536 - MAPBLK_MINPORT)
275 #define IPN_CMPSIZ (sizeof(ipnat_t) - offsetof(ipnat_t, in_flags))
277 typedef struct natlookup
{
278 struct in_addr nl_inip
;
279 struct in_addr nl_outip
;
280 struct in_addr nl_realip
;
288 typedef struct nat_save
{
291 struct ipnat ipn_ipnat
;
292 struct frentry ipn_fr
;
297 #define ipn_rule ipn_nat.nat_fr
299 typedef struct natget
{
306 * This structure gets used to help NAT sessions keep the same NAT rule (and
307 * thus translation for IP address) when:
308 * (a) round-robin redirects are in use
309 * (b) different IP add
311 typedef struct hostmap
{
312 struct hostmap
*hm_hnext
;
313 struct hostmap
**hm_phnext
;
314 struct hostmap
*hm_next
;
315 struct hostmap
**hm_pnext
;
316 struct ipnat
*hm_ipnat
;
317 struct in_addr hm_srcip
;
318 struct in_addr hm_dstip
;
319 struct in_addr hm_mapip
;
326 * Structure used to pass information in to nat_newmap and nat_newrdr.
328 typedef struct natinfo
{
334 struct in_addr nai_ip
;
342 typedef struct natstat
{
354 hostmap_t
**ns_maptable
;
365 hostmap_t
*ns_maplist
;
366 u_long
*ns_bucketlen
[2];
369 u_long ns_uncreate
[2][2];
372 typedef struct natlog
{
373 struct in_addr nl_origip
;
374 struct in_addr nl_outip
;
375 struct in_addr nl_inip
;
382 U_QUAD_T nl_bytes
[2];
387 #define NL_NEWMAP NAT_MAP
388 #define NL_NEWRDR NAT_REDIRECT
389 #define NL_NEWBIMAP NAT_BIMAP
390 #define NL_NEWBLOCK NAT_MAPBLK
391 #define NL_DESTROY 0xfffc
392 #define NL_CLONE 0xfffd
393 #define NL_FLUSH 0xfffe
394 #define NL_EXPIRE 0xffff
396 #define NAT_HASH_FN(k,l,m) (((k) + ((k) >> 12) + l) % (m))
398 #define LONG_SUM(in) (((in) & 0xffff) + ((in) >> 16))
400 #define CALC_SUMD(s1, s2, sd) { \
401 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
402 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
404 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
405 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
406 /* Because ~1 == -2, We really need ~1 == -1 */ \
407 if ((s1) > (s2)) (s2)--; \
408 (sd) = (s2) - (s1); \
409 (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
411 #define NAT_SYSSPACE 0x80000000
412 #define NAT_LOCKHELD 0x40000000
415 extern u_int ipf_nattable_sz
;
416 extern u_int ipf_nattable_max
;
417 extern u_int ipf_natrules_sz
;
418 extern u_int ipf_rdrrules_sz
;
419 extern u_int ipf_hostmap_sz
;
420 extern u_int fr_nat_maxbucket
;
421 extern u_int fr_nat_maxbucket_reset
;
422 extern int fr_nat_lock
;
423 extern int fr_nat_doflush
;
424 extern void fr_natsync
__P((void *));
425 extern u_long fr_defnatage
;
426 extern u_long fr_defnaticmpage
;
427 extern u_long fr_defnatipage
;
428 /* nat_table[0] -> hashed list sorted by inside (ip, port) */
429 /* nat_table[1] -> hashed list sorted by outside (ip, port) */
430 extern nat_t
**nat_table
[2];
431 extern nat_t
*nat_instances
;
432 extern ipnat_t
*nat_list
;
433 extern ipnat_t
**nat_rules
;
434 extern ipnat_t
**rdr_rules
;
435 extern ipftq_t
*nat_utqe
;
436 extern natstat_t nat_stats
;
438 #if defined(__OpenBSD__)
439 extern void nat_ifdetach
__P((void *));
441 extern int fr_nat_ioctl
__P((void *, ioctlcmd_t
, int, int, void *));
442 extern int fr_natinit
__P((void));
443 extern nat_t
*nat_new
__P((fr_info_t
*, ipnat_t
*, nat_t
**, u_int
, int));
444 extern nat_t
*nat_outlookup
__P((fr_info_t
*, u_int
, u_int
, struct in_addr
,
446 extern void fix_datacksum
__P((u_short
*, u_32_t
));
447 extern nat_t
*nat_inlookup
__P((fr_info_t
*, u_int
, u_int
, struct in_addr
,
449 extern nat_t
*nat_tnlookup
__P((fr_info_t
*, int));
450 extern nat_t
*nat_maplookup
__P((void *, u_int
, struct in_addr
,
452 extern nat_t
*nat_lookupredir
__P((natlookup_t
*));
453 extern nat_t
*nat_icmperrorlookup
__P((fr_info_t
*, int));
454 extern nat_t
*nat_icmperror
__P((fr_info_t
*, u_int
*, int));
455 extern void nat_delete
__P((struct nat
*, int));
456 extern int nat_insert
__P((nat_t
*, int));
457 extern void nat_uncreate
__P((fr_info_t
*));
459 extern int fr_checknatout
__P((fr_info_t
*, u_32_t
*));
460 extern int fr_natout
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
461 extern int fr_checknatin
__P((fr_info_t
*, u_32_t
*));
462 extern int fr_natin
__P((fr_info_t
*, nat_t
*, int, u_32_t
));
463 extern void fr_natunload
__P((void));
464 extern void fr_natexpire
__P((void));
465 extern void nat_log
__P((struct nat
*, u_int
));
466 extern void fix_incksum
__P((fr_info_t
*, u_short
*, u_32_t
));
467 extern void fix_outcksum
__P((fr_info_t
*, u_short
*, u_32_t
));
468 extern void fr_ipnatderef
__P((ipnat_t
**));
469 extern void fr_natderef
__P((nat_t
**));
470 extern u_short
*nat_proto
__P((fr_info_t
*, nat_t
*, u_int
));
471 extern void nat_update
__P((fr_info_t
*, nat_t
*));
472 extern void fr_setnatqueue
__P((nat_t
*, int));
473 extern void fr_hostmapdel
__P((hostmap_t
**));
475 #endif /* __IP_NAT_H__ */