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