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