Merge from vendor branch BZIP:
[dragonfly.git] / sys / contrib / ipfilter / netinet / ip_nat.h
1 /*
2  * Copyright (C) 1995-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * @(#)ip_nat.h 1.5 2/4/96
7  * $Id: ip_nat.h,v 2.17.2.27 2002/08/28 12:45:51 darrenr Exp $
8  * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_nat.h,v 1.15.2.6 2004/07/04 09:24:39 darrenr Exp $
9  * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_nat.h,v 1.5 2004/07/28 00:22:37 hmp Exp $
10  */
11
12 #ifndef __IP_NAT_H__
13 #define __IP_NAT_H__
14
15 #ifndef SOLARIS
16 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
17 #endif
18
19 #if defined(__STDC__) || defined(__GNUC__)
20 #define SIOCADNAT       _IOW('r', 60, struct ipnat *)
21 #define SIOCRMNAT       _IOW('r', 61, struct ipnat *)
22 #define SIOCGNATS       _IOWR('r', 62, struct natstat *)
23 #define SIOCGNATL       _IOWR('r', 63, struct natlookup *)
24 #else
25 #define SIOCADNAT       _IOW(r, 60, struct ipnat *)
26 #define SIOCRMNAT       _IOW(r, 61, struct ipnat *)
27 #define SIOCGNATS       _IOWR(r, 62, struct natstat *)
28 #define SIOCGNATL       _IOWR(r, 63, struct natlookup *)
29 #endif
30
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.
37                          */
38 #ifndef NAT_SIZE
39 # ifdef LARGE_NAT
40 #  define       NAT_SIZE        2047
41 # else
42 #  define       NAT_SIZE        127
43 # endif
44 #endif
45 #ifndef RDR_SIZE
46 # ifdef LARGE_NAT
47 #  define       RDR_SIZE        2047
48 # else
49 #  define       RDR_SIZE        127
50 # endif
51 #endif
52 #ifndef HOSTMAP_SIZE
53 # ifdef LARGE_NAT
54 #  define       HOSTMAP_SIZE    8191
55 # else
56 #  define       HOSTMAP_SIZE    2047
57 # endif
58 #endif
59 #ifndef NAT_TABLE_MAX
60 # ifdef LARGE_NAT
61 #  define       NAT_TABLE_MAX   180000
62 # else
63 #  define       NAT_TABLE_MAX   30000
64 # endif
65 #endif
66 #ifndef NAT_TABLE_SZ
67 # ifdef LARGE_NAT
68 #  define       NAT_TABLE_SZ    16383
69 # else
70 #  define       NAT_TABLE_SZ    2047
71 # endif
72 #endif
73 #ifndef APR_LABELLEN
74 #define APR_LABELLEN    16
75 #endif
76 #define NAT_HW_CKSUM    0x80000000
77
78 #define DEF_NAT_AGE     1200     /* 10 minutes (600 seconds) */
79
80 struct ap_session;
81
82 typedef struct  nat     {
83         u_long  nat_age;
84         int     nat_flags;
85         u_32_t  nat_sumd[2];
86         u_32_t  nat_ipsumd;
87         void    *nat_data;
88         struct  ap_session      *nat_aps;               /* proxy session */
89         struct  frentry *nat_fr;        /* filter rule ptr if appropriate */
90         struct  in_addr nat_inip;
91         struct  in_addr nat_outip;
92         struct  in_addr nat_oip;        /* other ip */
93         U_QUAD_T        nat_pkts;
94         U_QUAD_T        nat_bytes;
95         u_int   nat_drop[2];
96         u_short nat_oport;              /* other port */
97         u_short nat_inport;
98         u_short nat_outport;
99         u_short nat_use;
100         u_char  nat_tcpstate[2];
101         u_char  nat_p;                  /* protocol for NAT */
102         u_32_t  nat_mssclamp;   /* if != zero clamp MSS to this */
103         struct  ipnat   *nat_ptr;       /* pointer back to the rule */
104         struct  hostmap *nat_hm;
105         struct  nat     *nat_next;
106         struct  nat     *nat_hnext[2];
107         struct  nat     **nat_phnext[2];
108         struct  nat     **nat_me;
109         void    *nat_ifp;
110         int     nat_dir;
111         char    nat_ifname[IFNAMSIZ];
112 #if SOLARIS || defined(__sgi)
113         kmutex_t        nat_lock;
114 #endif
115 } nat_t;
116
117 typedef struct  ipnat   {
118         struct  ipnat   *in_next;
119         struct  ipnat   *in_rnext;
120         struct  ipnat   **in_prnext;
121         struct  ipnat   *in_mnext;
122         struct  ipnat   **in_pmnext;
123         void    *in_ifp;
124         void    *in_apr;
125         u_long  in_space;
126         u_int   in_use;
127         u_int   in_hits;
128         struct  in_addr in_nextip;
129         u_short in_pnext;
130         u_short in_ippip;       /* IP #'s per IP# */
131         u_32_t  in_flags;       /* From here to in_dport must be reflected */
132         u_32_t  in_mssclamp;    /* if != zero clamp MSS to this */
133         u_short in_spare;
134         u_short in_ppip;        /* ports per IP */
135         u_short in_port[2];     /* correctly in IPN_CMPSIZ */
136         struct  in_addr in_in[2];
137         struct  in_addr in_out[2];
138         struct  in_addr in_src[2];
139         struct  frtuc   in_tuc;
140         u_int   in_age[2];      /* Aging for NAT entries. Not for TCP */
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 */
145 } ipnat_t;
146
147 #define in_pmin         in_port[0]      /* Also holds static redir port */
148 #define in_pmax         in_port[1]
149 #define in_nip          in_nextip.s_addr
150 #define in_inip         in_in[0].s_addr
151 #define in_inmsk        in_in[1].s_addr
152 #define in_outip        in_out[0].s_addr
153 #define in_outmsk       in_out[1].s_addr
154 #define in_srcip        in_src[0].s_addr
155 #define in_srcmsk       in_src[1].s_addr
156 #define in_scmp         in_tuc.ftu_scmp
157 #define in_dcmp         in_tuc.ftu_dcmp
158 #define in_stop         in_tuc.ftu_stop
159 #define in_dtop         in_tuc.ftu_dtop
160 #define in_sport        in_tuc.ftu_sport
161 #define in_dport        in_tuc.ftu_dport
162
163 #define NAT_OUTBOUND    0
164 #define NAT_INBOUND     1
165
166 #define NAT_MAP         0x01
167 #define NAT_REDIRECT    0x02
168 #define NAT_BIMAP       (NAT_MAP|NAT_REDIRECT)
169 #define NAT_MAPBLK      0x04
170 /* 0x100 reserved for FI_W_SPORT */
171 /* 0x200 reserved for FI_W_DPORT */
172 /* 0x400 reserved for FI_W_SADDR */
173 /* 0x800 reserved for FI_W_DADDR */
174 /* 0x1000 reserved for FI_W_NEWFR */
175
176 #define MAPBLK_MINPORT  1024    /* don't use reserved ports for src port */
177 #define USABLE_PORTS    (65536 - MAPBLK_MINPORT)
178
179 #define IPN_CMPSIZ      (sizeof(ipnat_t) - offsetof(ipnat_t, in_flags))
180
181 typedef struct  natlookup {
182         struct  in_addr nl_inip;
183         struct  in_addr nl_outip;
184         struct  in_addr nl_realip;
185         int     nl_flags;
186         u_short nl_inport;
187         u_short nl_outport;
188         u_short nl_realport;
189 } natlookup_t;
190
191
192 typedef struct  nat_save    {
193         void    *ipn_next;
194         struct  nat     ipn_nat;
195         struct  ipnat   ipn_ipnat;
196         struct  frentry ipn_fr;
197         int     ipn_dsize;
198         char    ipn_data[4];
199 } nat_save_t;
200
201 #define ipn_rule        ipn_nat.nat_fr
202
203 typedef struct  natget  {
204         void    *ng_ptr;
205         int     ng_sz;
206 } natget_t;
207
208
209 typedef struct  hostmap {
210         struct  hostmap *hm_next;
211         struct  hostmap **hm_pnext;
212         struct  ipnat   *hm_ipnat;
213         struct  in_addr hm_realip;
214         struct  in_addr hm_mapip;
215         int     hm_ref;
216 } hostmap_t;
217
218
219 typedef struct  natstat {
220         u_long  ns_mapped[2];
221         u_long  ns_rules;
222         u_long  ns_added;
223         u_long  ns_expire;
224         u_long  ns_inuse;
225         u_long  ns_logged;
226         u_long  ns_logfail;
227         u_long  ns_memfail;
228         u_long  ns_badnat;
229         nat_t   **ns_table[2];
230         hostmap_t **ns_maptable;
231         ipnat_t *ns_list;
232         void    *ns_apslist;
233         u_int   ns_nattab_sz;
234         u_int   ns_rultab_sz;
235         u_int   ns_rdrtab_sz;
236         u_int   ns_hostmap_sz;
237         nat_t   *ns_instances;
238         u_int   ns_wilds;
239 } natstat_t;
240
241 #define IPN_ANY         0x000
242 #define IPN_TCP         0x001
243 #define IPN_UDP         0x002
244 #define IPN_TCPUDP      (IPN_TCP|IPN_UDP)
245 #define IPN_DELETE      0x004
246 #define IPN_ICMPERR     0x008
247 #define IPN_RF          (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
248 #define IPN_AUTOPORTMAP 0x010
249 #define IPN_IPRANGE     0x020
250 #define IPN_USERFLAGS   (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\
251                          IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|IPN_FRAG)
252 #define IPN_FILTER      0x040
253 #define IPN_SPLIT       0x080
254 #define IPN_ROUNDR      0x100
255 #define IPN_NOTSRC      0x080000
256 #define IPN_NOTDST      0x100000
257 #define IPN_FRAG        0x200000
258
259
260 typedef struct  natlog {
261         struct  in_addr nl_origip;
262         struct  in_addr nl_outip;
263         struct  in_addr nl_inip;
264         u_short nl_origport;
265         u_short nl_outport;
266         u_short nl_inport;
267         u_short nl_type;
268         int     nl_rule;
269         U_QUAD_T        nl_pkts;
270         U_QUAD_T        nl_bytes;
271         u_char  nl_p;
272 } natlog_t;
273
274
275 #define NL_NEWMAP       NAT_MAP
276 #define NL_NEWRDR       NAT_REDIRECT
277 #define NL_NEWBIMAP     NAT_BIMAP
278 #define NL_NEWBLOCK     NAT_MAPBLK
279 #define NL_FLUSH        0xfffe
280 #define NL_EXPIRE       0xffff
281
282 #define NAT_HASH_FN(k,l,m)      (((k) + ((k) >> 12) + l) % (m))
283
284 #define LONG_SUM(in)    (((in) & 0xffff) + ((in) >> 16))
285
286 #define CALC_SUMD(s1, s2, sd) { \
287                             (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
288                             (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
289                             /* Do it twice */ \
290                             (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
291                             (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
292                             /* Because ~1 == -2, We really need ~1 == -1 */ \
293                             if ((s1) > (s2)) (s2)--; \
294                             (sd) = (s2) - (s1); \
295                             (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
296
297 #define NAT_SYSSPACE            0x80000000
298 #define NAT_LOCKHELD            0x40000000
299
300 extern  u_int   ipf_nattable_sz;
301 extern  u_int   ipf_natrules_sz;
302 extern  u_int   ipf_rdrrules_sz;
303 extern  int     fr_nat_lock;
304 extern  void    ip_natsync (void *);
305 extern  u_long  fr_defnatage;
306 extern  u_long  fr_defnaticmpage;
307 extern  nat_t   **nat_table[2];
308 extern  nat_t   *nat_instances;
309 extern  ipnat_t **nat_rules;
310 extern  ipnat_t **rdr_rules;
311 extern  ipnat_t *nat_list;
312 extern  natstat_t       nat_stats;
313 #if defined(__OpenBSD__)
314 extern  void    nat_ifdetach (void *);
315 #endif
316 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
317 extern  int     nat_ioctl (caddr_t, u_long, int);
318 #else
319 extern  int     nat_ioctl (caddr_t, int, int);
320 #endif
321 extern  int     nat_init (void);
322 extern  nat_t   *nat_new (fr_info_t *, ip_t *, ipnat_t *, nat_t **,
323                               u_int, int);
324 extern  nat_t   *nat_outlookup (fr_info_t *, u_int, u_int, struct in_addr,
325                                  struct in_addr, int);
326 extern  nat_t   *nat_inlookup (fr_info_t *, u_int, u_int, struct in_addr,
327                                 struct in_addr, int);
328 extern  nat_t   *nat_lookupredir (natlookup_t *);
329 extern  nat_t   *nat_icmplookup (ip_t *, fr_info_t *, int);
330 extern  nat_t   *nat_icmp (ip_t *, fr_info_t *, u_int *, int);
331 extern  int     nat_clearlist (void);
332 extern  void    nat_insert (nat_t *);
333
334 extern  int     ip_natout (ip_t *, fr_info_t *);
335 extern  int     ip_natin (ip_t *, fr_info_t *);
336 extern  void    ip_natunload (void), ip_natexpire (void);
337 extern  void    nat_log (struct nat *, u_int);
338 extern  void    fix_incksum (fr_info_t *, u_short *, u_32_t);
339 extern  void    fix_outcksum (fr_info_t *, u_short *, u_32_t);
340 extern  void    fix_datacksum (u_short *, u_32_t);
341
342 #endif /* __IP_NAT_H__ */