1 //==========================================================================
3 // include/netinet/ip_nat.h
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
10 // -------------------------------------------
12 // Portions of this software may have been derived from OpenBSD or other sources,
13 // and are covered by the appropriate copyright disclaimers included herein.
15 // -------------------------------------------
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
22 // Contributors: gthomas
28 //####DESCRIPTIONEND####
30 //==========================================================================
33 /* $OpenBSD: ip_nat.h,v 1.13 1999/12/15 05:20:22 kjell Exp $ */
35 * Copyright (C) 1995-1998 by Darren Reed.
37 * Redistribution and use in source and binary forms are permitted
38 * provided that this notice is preserved and due credit is given
39 * to the original author and the contributors.
41 * @(#)ip_nat.h 1.5 2/4/96
44 #ifndef _NETINET_IP_NAT_H__
45 #define _NETINET_IP_NAT_H__
48 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
51 #if defined(__STDC__) || defined(__GNUC__)
52 #define SIOCADNAT _IOW('r', 80, struct ipnat)
53 #define SIOCRMNAT _IOW('r', 81, struct ipnat)
54 #define SIOCGNATS _IOR('r', 82, struct natstat)
55 #define SIOCGNATL _IOWR('r', 83, struct natlookup)
56 #define SIOCGFRST _IOR('r', 84, struct ipfrstat)
57 #define SIOCGIPST _IOR('r', 85, struct ips_stat)
58 #define SIOCFLNAT _IOWR('r', 86, int)
59 #define SIOCCNATL _IOWR('r', 87, int)
61 #define SIOCADNAT _IOW(r, 80, struct ipnat)
62 #define SIOCRMNAT _IOW(r, 81, struct ipnat)
63 #define SIOCGNATS _IOR(r, 82, struct natstat)
64 #define SIOCGNATL _IOWR(r, 83, struct natlookup)
65 #define SIOCGFRST _IOR(r, 84, struct ipfrstat)
66 #define SIOCGIPST _IOR(r, 85, struct ips_stat)
67 #define SIOCFLNAT _IOWR(r, 86, int)
68 #define SIOCCNATL _IOWR(r, 87, int)
71 #undef LARGE_NAT /* define this if you're setting up a system to NAT
72 * LARGE numbers of networks/hosts - i.e. in the
73 * hundreds or thousands. In such a case, you should
74 * also change the RDR_SIZE and NAT_SIZE below to more
75 * appropriate sizes. The figures below were used for
76 * a setup with 1000-2000 networks to NAT.
80 #define NAT_TABLE_SZ 127
87 #define NAT_TABLE_SZ 16383
90 #define APR_LABELLEN 16
92 #define NAT_HW_CKSUM 0x80000000
94 #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */
102 void *nat_aps; /* proxy session */
103 frentry_t *nat_fr; /* filter rule ptr if appropriate */
104 struct in_addr nat_inip;
105 struct in_addr nat_outip;
106 struct in_addr nat_oip; /* other ip */
109 u_short nat_oport; /* other port */
113 u_char nat_tcpstate[2];
114 u_char nat_p; /* protocol for NAT */
115 struct ipnat *nat_ptr; /* pointer back to the rule */
116 struct nat *nat_next;
117 struct nat *nat_hnext[2];
118 struct nat **nat_hstart[2];
123 typedef struct ipnat {
124 struct ipnat *in_next;
125 struct ipnat *in_rnext;
126 struct ipnat *in_mnext;
132 struct in_addr in_nextip;
134 u_short in_ppip; /* ports per IP */
135 u_short in_ippip; /* IP #'s per IP# */
136 u_short in_flags; /* From here to in_dport must be reflected */
137 u_short in_port[2]; /* correctly in IPN_CMPSIZ */
138 struct in_addr in_in[2];
139 struct in_addr in_out[2];
140 struct in_addr in_src[2];
141 int in_redir; /* 0 if it's a mapping, 1 if it's a hard redir */
142 char in_ifname[IFNAMSIZ];
143 char in_plabel[APR_LABELLEN]; /* proxy label */
144 char in_p; /* protocol */
148 #define in_pmin in_port[0] /* Also holds static redir port */
149 #define in_pmax in_port[1]
150 #define in_nip in_nextip.s_addr
151 #define in_inip in_in[0].s_addr
152 #define in_inmsk in_in[1].s_addr
153 #define in_outip in_out[0].s_addr
154 #define in_outmsk in_out[1].s_addr
155 #define in_srcip in_src[0].s_addr
156 #define in_srcmsk in_src[1].s_addr
158 #define NAT_OUTBOUND 0
159 #define NAT_INBOUND 1
162 #define NAT_REDIRECT 0x02
163 #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT)
164 #define NAT_MAPBLK 0x04
166 #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */
167 #define USABLE_PORTS (65536 - MAPBLK_MINPORT)
169 #define IPN_CMPSIZ (sizeof(ipnat_t) - offsetof(ipnat_t, in_flags))
171 typedef struct natlookup {
172 struct in_addr nl_inip;
173 struct in_addr nl_outip;
174 struct in_addr nl_realip;
181 typedef struct natstat {
201 #define IPN_TCPUDP (IPN_TCP|IPN_UDP)
202 #define IPN_DELETE 0x04
203 #define IPN_ICMPERR 0x08
204 #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
205 #define IPN_AUTOPORTMAP 0x10
206 #define IPN_RANGE 0x20
207 #define IPN_USERFLAGS (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_RANGE)
210 typedef struct natlog {
211 struct in_addr nl_origip;
212 struct in_addr nl_outip;
213 struct in_addr nl_inip;
224 #define NL_NEWMAP NAT_MAP
225 #define NL_NEWRDR NAT_REDIRECT
226 #define NL_EXPIRE 0xffff
228 #define NAT_HASH_FN(k,m) (((k) + ((k) >> 12)) % (m))
230 #define LONG_SUM(in) (((in) & 0xffff) + ((in) >> 16))
232 #define CALC_SUMD(s1, s2, sd) { \
233 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
234 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
236 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
237 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
238 /* Because ~1 == -2, We really need ~1 == -1 */ \
239 if ((s1) > (s2)) (s2)--; \
240 (sd) = (s2) - (s1); \
241 (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
244 extern u_int ipf_nattable_sz;
245 extern u_int ipf_natrules_sz;
246 extern u_int ipf_rdrrules_sz;
247 extern void ip_natsync __P((void *));
248 extern u_long fr_defnatage;
249 extern u_long fr_defnaticmpage;
250 extern nat_t **nat_table[2];
251 extern nat_t *nat_instances;
252 extern ipnat_t **nat_rules;
253 extern ipnat_t **rdr_rules;
254 extern natstat_t nat_stats;
255 #if defined(__NetBSD__) || defined(__OpenBSD__)
256 extern int nat_ioctl __P((caddr_t, u_long, int));
258 extern int nat_ioctl __P((caddr_t, int, int));
260 extern void nat_ifdetach __P((struct ifnet *));
261 extern int nat_init __P((void));
262 extern nat_t *nat_new __P((ipnat_t *, ip_t *, fr_info_t *, u_int, int));
263 extern nat_t *nat_outlookup __P((void *, u_int, u_int, struct in_addr,
264 struct in_addr, u_32_t));
265 extern nat_t *nat_inlookup __P((void *, u_int, u_int, struct in_addr,
266 struct in_addr, u_32_t));
267 extern nat_t *nat_maplookup __P((void *, u_int, struct in_addr,
269 extern nat_t *nat_lookupredir __P((natlookup_t *));
270 extern nat_t *nat_icmpinlookup __P((ip_t *, fr_info_t *));
271 extern nat_t *nat_icmpin __P((ip_t *, fr_info_t *, u_int *));
273 extern int ip_natout __P((ip_t *, fr_info_t *));
274 extern int ip_natin __P((ip_t *, fr_info_t *));
275 extern void ip_natunload __P((void)), ip_natexpire __P((void));
276 extern void nat_log __P((struct nat *, u_int));
277 extern void fix_incksum __P((u_short *, u_32_t, int));
278 extern void fix_outcksum __P((u_short *, u_32_t, int));
280 #endif /* _NETINET_IP_NAT_H__ */