Merge commit '9bba04fed2291aa884d1da64bc62150d5562cc5b'
[unleashed.git] / include / netinet / ip_nat.h
blobd118cdf354732948a46f9b442a752e9473503da9
1 /*
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.
13 #ifndef __IP_NAT_H__
14 #define __IP_NAT_H__
16 #ifdef SOLARIS
17 #undef SOLARIS
18 #endif
19 #if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
20 #define SOLARIS (1)
21 #else
22 #define SOLARIS (0)
23 #endif
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)
30 #else
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)
35 #endif
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.
44 #ifndef NAT_SIZE
45 # ifdef LARGE_NAT
46 # define NAT_SIZE 2047
47 # else
48 # define NAT_SIZE 127
49 # endif
50 #endif
51 #ifndef RDR_SIZE
52 # ifdef LARGE_NAT
53 # define RDR_SIZE 2047
54 # else
55 # define RDR_SIZE 127
56 # endif
57 #endif
58 #ifndef HOSTMAP_SIZE
59 # ifdef LARGE_NAT
60 # define HOSTMAP_SIZE 8191
61 # else
62 # define HOSTMAP_SIZE 2047
63 # endif
64 #endif
65 #ifndef NAT_TABLE_MAX
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.
70 # ifdef LARGE_NAT
71 # define NAT_TABLE_MAX 180000
72 # else
73 # define NAT_TABLE_MAX 30000
74 # endif
75 #endif
76 #ifndef NAT_TABLE_SZ
77 # ifdef LARGE_NAT
78 # define NAT_TABLE_SZ 16383
79 # else
80 # define NAT_TABLE_SZ 2047
81 # endif
82 #endif
83 #ifndef APR_LABELLEN
84 #define APR_LABELLEN 16
85 #endif
87 #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */
89 struct ipstate;
90 struct ap_session;
92 typedef struct nat {
93 ipfmutex_t nat_lock;
94 struct nat *nat_next;
95 struct nat **nat_pnext;
96 struct nat *nat_hnext[2];
97 struct nat **nat_phnext[2];
98 struct hostmap *nat_hm;
99 void *nat_data;
100 struct nat **nat_me;
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 */
105 void *nat_ifps[2];
106 void *nat_sync;
107 ipftqent_t nat_tqe;
108 u_32_t nat_flags;
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 */
112 i6addr_t nat_inip6;
113 i6addr_t nat_outip6;
114 i6addr_t nat_oip6; /* other ip */
115 U_QUAD_T nat_pkts[2];
116 U_QUAD_T nat_bytes[2];
117 union {
118 udpinfo_t nat_unu;
119 tcpinfo_t nat_unt;
120 icmpinfo_t nat_uni;
121 greinfo_t nat_ugre;
122 } nat_un;
123 u_short nat_oport; /* other port */
124 u_short nat_use;
125 u_char nat_p; /* protocol for NAT */
126 int nat_dir;
127 int nat_ref; /* reference count */
128 int nat_hv[2];
129 char nat_ifnames[2][LIFNAMSIZ];
130 int nat_rev; /* 0 = forward, 1 = reverse */
131 int nat_v;
132 int nat_redir; /* copy of in_redir */
133 } nat_t;
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
147 * Values for nat_dir
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];
186 void *in_ifps[2];
187 void *in_apr;
188 char *in_comment;
189 i6addr_t in_next6;
190 u_long in_space;
191 u_long in_hits;
192 u_int in_use;
193 u_int in_hv;
194 int in_flineno; /* conf. file line number */
195 u_short in_pnext;
196 u_char in_v;
197 u_char in_xxx;
198 /* From here to the end is covered by IPN_CMPSIZ */
199 u_32_t in_flags;
200 u_32_t in_mssclamp; /* if != 0 clamp MSS to this */
201 u_int in_age[2];
202 int in_redir; /* see below for values */
203 int in_p; /* protocol. */
204 i6addr_t in_in[2];
205 i6addr_t in_out[2];
206 i6addr_t in_src[2];
207 frtuc_t in_tuc;
208 u_short in_port[2];
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. */
213 ipftag_t in_tag;
214 } ipnat_t;
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|\
264 IPN_SEQUENTIAL)
267 * Values for in_redir
269 #define NAT_MAP 0x01
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;
283 int nl_v;
284 int nl_flags;
285 u_short nl_inport;
286 u_short nl_outport;
287 u_short nl_realport;
288 } natlookup_t;
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 {
299 void *ipn_next;
300 struct nat ipn_nat;
301 struct ipnat ipn_ipnat;
302 struct frentry ipn_fr;
303 int ipn_dsize;
304 char ipn_data[4];
305 } nat_save_t;
307 #define ipn_rule ipn_nat.nat_fr
309 typedef struct natget {
310 void *ng_ptr;
311 int ng_sz;
312 } natget_t;
315 #undef tr_flags
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# */
320 u_int tr_flags;
321 int tr_expire;
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 */
329 } nattrpnt_t;
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;
346 i6addr_t hm_src;
347 i6addr_t hm_dst;
348 i6addr_t hm_map;
349 u_32_t hm_port;
350 int hm_ref;
351 int hm_v;
352 } hostmap_t;
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 {
366 ipnat_t *nai_np;
367 u_32_t nai_nflags;
368 u_32_t nai_flags;
369 struct in_addr nai_ip;
370 u_short nai_port;
371 u_short nai_nport;
372 u_short nai_sport;
373 u_short nai_dport;
374 } natinfo_t;
377 typedef struct natstat {
378 u_long ns_mapped[2];
379 u_long ns_rules;
380 u_long ns_added;
381 u_long ns_expire;
382 u_long ns_inuse;
383 u_long ns_logged;
384 u_long ns_logfail;
385 u_long ns_memfail;
386 u_long ns_badnat;
387 u_long ns_addtrpnt;
388 nat_t **ns_table[2];
389 hostmap_t **ns_maptable;
390 ipnat_t *ns_list;
391 void *ns_apslist;
392 u_int ns_wilds;
393 u_int ns_nattab_sz;
394 u_int ns_nattab_max;
395 u_int ns_rultab_sz;
396 u_int ns_rdrtab_sz;
397 u_int ns_trpntab_sz;
398 u_int ns_hostmap_sz;
399 nat_t *ns_instances;
400 nattrpnt_t *ns_trpntlist;
401 hostmap_t *ns_maplist;
402 u_long *ns_bucketlen[2];
403 u_int ns_orphans;
404 u_long ns_uncreate[2][2];
405 } natstat_t;
407 typedef struct natlog {
408 i6addr_t nlg_origip;
409 i6addr_t nlg_outip;
410 i6addr_t nlg_inip;
411 u_short nlg_origport;
412 u_short nlg_outport;
413 u_short nlg_inport;
414 u_short nlg_type;
415 int nlg_rule;
416 U_QUAD_T nlg_pkts[2];
417 U_QUAD_T nlg_bytes[2];
418 u_char nlg_p;
419 int nlg_v;
420 } natlog_t;
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) \
441 + l) % (m))
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); \
452 /* Do it twice */ \
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 *));
469 #endif
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,
474 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,
477 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,
480 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 **));
503 #if SOLARIS2 >= 10
504 extern void fr_natifindexsync __P((void *, void *, ipf_stack_t *));
505 #endif
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 *));
513 #ifdef USE_INET6
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));
529 #endif
531 #endif /* __IP_NAT_H__ */