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