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
10 * Id: ip_nat.h,v 2.90.2.20 2007/09/25 08:27:32 darrenr Exp $
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
81 #define NAT_HW_CKSUM 0x80000000
83 #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */
91 struct nat **nat_pnext;
92 struct nat *nat_hnext[2];
93 struct nat **nat_phnext[2];
94 struct hostmap *nat_hm;
97 struct ipstate *nat_state;
98 struct ap_session *nat_aps; /* proxy session */
99 frentry_t *nat_fr; /* filter rule ptr if appropriate */
100 struct ipnat *nat_ptr; /* pointer back to the rule */
105 u_32_t nat_sumd[2]; /* ip checksum delta for data segment*/
106 u_32_t nat_ipsumd; /* ip checksum delta for ip header */
107 u_32_t nat_mssclamp; /* if != zero clamp MSS to this */
110 i6addr_t nat_oip6; /* other ip */
111 U_QUAD_T nat_pkts[2];
112 U_QUAD_T nat_bytes[2];
119 u_short nat_oport; /* other port */
121 u_char nat_p; /* protocol for NAT */
123 int nat_ref; /* reference count */
125 char nat_ifnames[2][LIFNAMSIZ];
126 int nat_rev; /* 0 = forward, 1 = reverse */
127 int nat_redir; /* copy of in_redir */
128 u_32_t nat_seqnext[2];
131 #define nat_inip nat_inip6.in4
132 #define nat_outip nat_outip6.in4
133 #define nat_oip nat_oip6.in4
134 #define nat_age nat_tqe.tqe_die
135 #define nat_inport nat_un.nat_unt.ts_sport
136 #define nat_outport nat_un.nat_unt.ts_dport
137 #define nat_type nat_un.nat_uni.ici_type
138 #define nat_seq nat_un.nat_uni.ici_seq
139 #define nat_id nat_un.nat_uni.ici_id
140 #define nat_tcpstate nat_tqe.tqe_state
141 #define nat_die nat_tqe.tqe_die
142 #define nat_touched nat_tqe.tqe_touched
147 #define NAT_INBOUND 0
148 #define NAT_OUTBOUND 1
151 * Definitions for nat_flags
153 #define NAT_TCP 0x0001 /* IPN_TCP */
154 #define NAT_UDP 0x0002 /* IPN_UDP */
155 #define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */
156 #define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */
157 #define NAT_SEARCH 0x0010
158 #define NAT_SLAVE 0x0020 /* Slave connection for a proxy */
159 #define NAT_NOTRULEPORT 0x0040 /* Don't use the port # in the NAT rule */
161 #define NAT_TCPUDP (NAT_TCP|NAT_UDP)
162 #define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR)
163 #define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
164 #define NAT_FROMRULE (NAT_TCP|NAT_UDP)
166 /* 0x0100 reserved for FI_W_SPORT */
167 /* 0x0200 reserved for FI_W_DPORT */
168 /* 0x0400 reserved for FI_W_SADDR */
169 /* 0x0800 reserved for FI_W_DADDR */
170 /* 0x1000 reserved for FI_W_NEWFR */
171 /* 0x2000 reserved for SI_CLONE */
172 /* 0x4000 reserved for SI_CLONED */
173 /* 0x8000 reserved for SI_IGNOREPKT */
175 #define NAT_DEBUG 0x800000
177 typedef struct ipnat {
179 struct ipnat *in_next; /* NAT rule list next */
180 struct ipnat *in_rnext; /* rdr rule hash next */
181 struct ipnat **in_prnext; /* prior rdr next ptr */
182 struct ipnat *in_mnext; /* map rule hash next */
183 struct ipnat **in_pmnext; /* prior map next ptr */
184 struct ipftq *in_tqehead[2];
193 int in_flineno; /* conf. file line number */
197 /* From here to the end is covered by IPN_CMPSIZ */
199 u_32_t in_mssclamp; /* if != 0 clamp MSS to this */
201 int in_redir; /* see below for values */
202 int in_p; /* protocol. */
208 u_short in_ppip; /* ports per IP. */
209 u_short in_ippip; /* IP #'s per IP# */
210 char in_ifnames[2][LIFNAMSIZ];
211 char in_plabel[APR_LABELLEN]; /* proxy label. */
215 #define in_pmin in_port[0] /* Also holds static redir port */
216 #define in_pmax in_port[1]
217 #define in_nextip in_next6.in4
218 #define in_nip in_next6.in4.s_addr
219 #define in_inip in_in[0].in4.s_addr
220 #define in_inmsk in_in[1].in4.s_addr
221 #define in_outip in_out[0].in4.s_addr
222 #define in_outmsk in_out[1].in4.s_addr
223 #define in_srcip in_src[0].in4.s_addr
224 #define in_srcmsk in_src[1].in4.s_addr
225 #define in_scmp in_tuc.ftu_scmp
226 #define in_dcmp in_tuc.ftu_dcmp
227 #define in_stop in_tuc.ftu_stop
228 #define in_dtop in_tuc.ftu_dtop
229 #define in_sport in_tuc.ftu_sport
230 #define in_dport in_tuc.ftu_dport
233 * Bit definitions for in_flags
235 #define IPN_ANY 0x00000
236 #define IPN_TCP 0x00001
237 #define IPN_UDP 0x00002
238 #define IPN_TCPUDP (IPN_TCP|IPN_UDP)
239 #define IPN_ICMPERR 0x00004
240 #define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR)
241 #define IPN_ICMPQUERY 0x00008
242 #define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
243 #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
244 #define IPN_AUTOPORTMAP 0x00010
245 #define IPN_IPRANGE 0x00020
246 #define IPN_FILTER 0x00040
247 #define IPN_SPLIT 0x00080
248 #define IPN_ROUNDR 0x00100
249 #define IPN_NOTSRC 0x04000
250 #define IPN_NOTDST 0x08000
251 #define IPN_DYNSRCIP 0x10000 /* dynamic src IP# */
252 #define IPN_DYNDSTIP 0x20000 /* dynamic dst IP# */
253 #define IPN_DELETE 0x40000
254 #define IPN_STICKY 0x80000
255 #define IPN_FRAG 0x100000
256 #define IPN_FIXEDDPORT 0x200000
257 #define IPN_FINDFORWARD 0x400000
258 #define IPN_IN 0x800000
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)
264 * Values for in_redir
267 #define NAT_REDIRECT 0x02
268 #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT)
269 #define NAT_MAPBLK 0x04
271 #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */
272 #define USABLE_PORTS (65536 - MAPBLK_MINPORT)
274 #define IPN_CMPSIZ (sizeof(ipnat_t) - offsetof(ipnat_t, in_flags))
276 typedef struct natlookup {
277 struct in_addr nl_inip;
278 struct in_addr nl_outip;
279 struct in_addr nl_realip;
287 typedef struct nat_save {
290 struct ipnat ipn_ipnat;
291 struct frentry ipn_fr;
296 #define ipn_rule ipn_nat.nat_fr
298 typedef struct natget {
305 * This structure gets used to help NAT sessions keep the same NAT rule (and
306 * thus translation for IP address) when:
307 * (a) round-robin redirects are in use
308 * (b) different IP add
310 typedef struct hostmap {
311 struct hostmap *hm_hnext;
312 struct hostmap **hm_phnext;
313 struct hostmap *hm_next;
314 struct hostmap **hm_pnext;
315 struct ipnat *hm_ipnat;
316 struct in_addr hm_srcip;
317 struct in_addr hm_dstip;
318 struct in_addr hm_mapip;
325 * Structure used to pass information in to nat_newmap and nat_newrdr.
327 typedef struct natinfo {
333 struct in_addr nai_ip;
341 typedef struct natstat {
353 hostmap_t **ns_maptable;
364 hostmap_t *ns_maplist;
365 u_long *ns_bucketlen[2];
370 typedef struct natlog {
371 struct in_addr nl_origip;
372 struct in_addr nl_outip;
373 struct in_addr nl_inip;
380 U_QUAD_T nl_bytes[2];
385 #define NL_NEWMAP NAT_MAP
386 #define NL_NEWRDR NAT_REDIRECT
387 #define NL_NEWBIMAP NAT_BIMAP
388 #define NL_NEWBLOCK NAT_MAPBLK
389 #define NL_DESTROY 0xfffc
390 #define NL_CLONE 0xfffd
391 #define NL_FLUSH 0xfffe
392 #define NL_EXPIRE 0xffff
394 #define NAT_HASH_FN(k,l,m) (((k) + ((k) >> 12) + l) % (m))
396 #define LONG_SUM(in) (((in) & 0xffff) + ((in) >> 16))
398 #define CALC_SUMD(s1, s2, sd) { \
399 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
400 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
402 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
403 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
404 /* Because ~1 == -2, We really need ~1 == -1 */ \
405 if ((s1) > (s2)) (s2)--; \
406 (sd) = (s2) - (s1); \
407 (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
409 #define NAT_SYSSPACE 0x80000000
410 #define NAT_LOCKHELD 0x40000000
413 extern u_int ipf_nattable_sz;
414 extern u_int ipf_nattable_max;
415 extern u_int ipf_natrules_sz;
416 extern u_int ipf_rdrrules_sz;
417 extern u_int ipf_hostmap_sz;
418 extern u_int fr_nat_maxbucket;
419 extern u_int fr_nat_maxbucket_reset;
420 extern int fr_nat_lock;
421 extern int fr_nat_doflush;
422 extern void fr_natsync __P((void *));
423 extern u_long fr_defnatage;
424 extern u_long fr_defnaticmpage;
425 extern u_long fr_defnatipage;
426 /* nat_table[0] -> hashed list sorted by inside (ip, port) */
427 /* nat_table[1] -> hashed list sorted by outside (ip, port) */
428 extern nat_t **nat_table[2];
429 extern nat_t *nat_instances;
430 extern ipnat_t *nat_list;
431 extern ipnat_t **nat_rules;
432 extern ipnat_t **rdr_rules;
433 extern ipftq_t *nat_utqe;
434 extern natstat_t nat_stats;
436 #if defined(__OpenBSD__)
437 extern void nat_ifdetach __P((void *));
439 extern int fr_nat_ioctl __P((caddr_t, ioctlcmd_t, int, int, void *));
440 extern int fr_natinit __P((void));
441 extern nat_t *nat_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int));
442 extern nat_t *nat_outlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
444 extern void fix_datacksum __P((u_short *, u_32_t));
445 extern nat_t *nat_inlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
447 extern nat_t *nat_tnlookup __P((fr_info_t *, int));
448 extern nat_t *nat_maplookup __P((void *, u_int, struct in_addr,
450 extern nat_t *nat_lookupredir __P((natlookup_t *));
451 extern nat_t *nat_icmperrorlookup __P((fr_info_t *, int));
452 extern nat_t *nat_icmperror __P((fr_info_t *, u_int *, int));
453 extern void nat_delete __P((struct nat *, int));
454 extern int nat_insert __P((nat_t *, int));
456 extern int fr_checknatout __P((fr_info_t *, u_32_t *));
457 extern int fr_natout __P((fr_info_t *, nat_t *, int, u_32_t));
458 extern int fr_checknatin __P((fr_info_t *, u_32_t *));
459 extern int fr_natin __P((fr_info_t *, nat_t *, int, u_32_t));
460 extern void fr_natunload __P((void));
461 extern void fr_natexpire __P((void));
462 extern void nat_log __P((struct nat *, u_int));
463 extern void fix_incksum __P((fr_info_t *, u_short *, u_32_t));
464 extern void fix_outcksum __P((fr_info_t *, u_short *, u_32_t));
465 extern void fr_ipnatderef __P((ipnat_t **));
466 extern void fr_natderef __P((nat_t **));
467 extern u_short *nat_proto __P((fr_info_t *, nat_t *, u_int));
468 extern void nat_update __P((fr_info_t *, nat_t *, ipnat_t *));
469 extern void fr_setnatqueue __P((nat_t *, int));
470 extern void fr_hostmapdel __P((hostmap_t **));
472 #endif /* __IP_NAT_H__ */