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