]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_nat.h
This commit was generated by cvs2svn to compensate for changes in r53809,
[FreeBSD/FreeBSD.git] / sys / netinet / ip_nat.h
1 /*
2  * Copyright (C) 1995-1998 by Darren Reed.
3  *
4  * Redistribution and use in source and binary forms are permitted
5  * provided that this notice is preserved and due credit is given
6  * to the original author and the contributors.
7  *
8  * @(#)ip_nat.h 1.5 2/4/96
9  * $Id: ip_nat.h,v 2.1.2.1 1999/08/14 04:47:54 darrenr Exp $
10  * $FreeBSD$
11  */
12
13 #ifndef __IP_NAT_H__
14 #define __IP_NAT_H__
15
16 #ifndef SOLARIS
17 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
18 #endif
19
20 #if defined(__STDC__) || defined(__GNUC__)
21 #define SIOCADNAT       _IOW('r', 80, struct ipnat)
22 #define SIOCRMNAT       _IOW('r', 81, struct ipnat)
23 #define SIOCGNATS       _IOR('r', 82, struct natstat)
24 #define SIOCGNATL       _IOWR('r', 83, struct natlookup)
25 #define SIOCGFRST       _IOR('r', 84, struct ipfrstat)
26 #define SIOCGIPST       _IOR('r', 85, struct ips_stat)
27 #define SIOCFLNAT       _IOWR('r', 86, int)
28 #define SIOCCNATL       _IOWR('r', 87, int)
29 #else
30 #define SIOCADNAT       _IOW(r, 80, struct ipnat)
31 #define SIOCRMNAT       _IOW(r, 81, struct ipnat)
32 #define SIOCGNATS       _IOR(r, 82, struct natstat)
33 #define SIOCGNATL       _IOWR(r, 83, struct natlookup)
34 #define SIOCGFRST       _IOR(r, 84, struct ipfrstat)
35 #define SIOCGIPST       _IOR(r, 85, struct ips_stat)
36 #define SIOCFLNAT       _IOWR(r, 86, int)
37 #define SIOCCNATL       _IOWR(r, 87, int)
38 #endif
39
40 #undef  LARGE_NAT       /* define this if you're setting up a system to NAT
41                          * LARGE numbers of networks/hosts - i.e. in the
42                          * hundreds or thousands.  In such a case, you should
43                          * also change the RDR_SIZE and NAT_SIZE below to more
44                          * appropriate sizes.  The figures below were used for
45                          * a setup with 1000-2000 networks to NAT.
46                          */
47 #define NAT_SIZE        127
48 #define RDR_SIZE        127
49 #define NAT_TABLE_SZ    127
50 #ifdef  LARGE_NAT
51 #undef  NAT_SIZE
52 #undef  RDR_SIZE
53 #undef  NAT_TABLE_SZ
54 #define NAT_SIZE        2047
55 #define RDR_SIZE        2047
56 #define NAT_TABLE_SZ    16383
57 #endif
58 #ifndef APR_LABELLEN
59 #define APR_LABELLEN    16
60 #endif
61
62 #define DEF_NAT_AGE     1200     /* 10 minutes (600 seconds) */
63
64 typedef struct  nat     {
65         u_long  nat_age;
66         int     nat_flags;
67         u_32_t  nat_sumd;
68         u_32_t  nat_ipsumd;
69         void    *nat_data;
70         void    *nat_aps;               /* proxy session */
71         frentry_t       *nat_fr;        /* filter rule ptr if appropriate */
72         struct  in_addr nat_inip;
73         struct  in_addr nat_outip;
74         struct  in_addr nat_oip;        /* other ip */
75         U_QUAD_T        nat_pkts;
76         U_QUAD_T        nat_bytes;
77         u_short nat_oport;              /* other port */
78         u_short nat_inport;
79         u_short nat_outport;
80         u_short nat_use;
81         u_char  nat_tcpstate[2];
82         u_char  nat_p;                  /* protocol for NAT */
83         struct  ipnat   *nat_ptr;       /* pointer back to the rule */
84         struct  nat     *nat_next;
85         struct  nat     *nat_hnext[2];
86         struct  nat     **nat_hstart[2];
87         void    *nat_ifp;
88         int     nat_dir;
89 } nat_t;
90
91 typedef struct  ipnat   {
92         struct  ipnat   *in_next;
93         struct  ipnat   *in_rnext;
94         struct  ipnat   *in_mnext;
95         void    *in_ifp;
96         void    *in_apr;
97         u_long  in_space;
98         u_int   in_use;
99         u_int   in_hits;
100         struct  in_addr in_nextip;
101         u_short in_pnext;
102         u_short in_ppip;        /* ports per IP */
103         u_short in_ippip;       /* IP #'s per IP# */
104         u_short in_flags;       /* From here to in_dport must be reflected */
105         u_short in_port[2];     /* correctly in IPN_CMPSIZ */
106         struct  in_addr in_in[2];
107         struct  in_addr in_out[2];
108         struct  in_addr in_src[2];
109         int     in_redir; /* 0 if it's a mapping, 1 if it's a hard redir */
110         char    in_ifname[IFNAMSIZ];
111         char    in_plabel[APR_LABELLEN];        /* proxy label */
112         char    in_p;   /* protocol */
113         u_short in_dport;
114 } ipnat_t;
115
116 #define in_pmin         in_port[0]      /* Also holds static redir port */
117 #define in_pmax         in_port[1]
118 #define in_nip          in_nextip.s_addr
119 #define in_inip         in_in[0].s_addr
120 #define in_inmsk        in_in[1].s_addr
121 #define in_outip        in_out[0].s_addr
122 #define in_outmsk       in_out[1].s_addr
123 #define in_srcip        in_src[0].s_addr
124 #define in_srcmsk       in_src[1].s_addr
125
126 #define NAT_OUTBOUND    0
127 #define NAT_INBOUND     1
128
129 #define NAT_MAP         0x01
130 #define NAT_REDIRECT    0x02
131 #define NAT_BIMAP       (NAT_MAP|NAT_REDIRECT)
132 #define NAT_MAPBLK      0x04
133
134 #define MAPBLK_MINPORT  1024    /* don't use reserved ports for src port */
135 #define USABLE_PORTS    (65536 - MAPBLK_MINPORT)
136
137 #define IPN_CMPSIZ      (sizeof(ipnat_t) - offsetof(ipnat_t, in_flags))
138
139 typedef struct  natlookup {
140         struct  in_addr nl_inip;
141         struct  in_addr nl_outip;
142         struct  in_addr nl_realip;
143         int     nl_flags;
144         u_short nl_inport;
145         u_short nl_outport;
146         u_short nl_realport;
147 } natlookup_t;
148
149 typedef struct  natstat {
150         u_long  ns_mapped[2];
151         u_long  ns_rules;
152         u_long  ns_added;
153         u_long  ns_expire;
154         u_long  ns_inuse;
155         u_long  ns_logged;
156         u_long  ns_logfail;
157         nat_t   **ns_table[2];
158         ipnat_t *ns_list;
159         void    *ns_apslist;
160         u_int   ns_nattab_sz;
161         u_int   ns_rultab_sz;
162         u_int   ns_rdrtab_sz;
163         nat_t   *ns_instances;
164 } natstat_t;
165
166 #define IPN_ANY         0x00
167 #define IPN_TCP         0x01
168 #define IPN_UDP         0x02
169 #define IPN_TCPUDP      (IPN_TCP|IPN_UDP)
170 #define IPN_DELETE      0x04
171 #define IPN_ICMPERR     0x08
172 #define IPN_RF          (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
173 #define IPN_AUTOPORTMAP 0x10
174 #define IPN_RANGE       0x20
175 #define IPN_USERFLAGS   (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_RANGE)
176
177
178 typedef struct  natlog {
179         struct  in_addr nl_origip;
180         struct  in_addr nl_outip;
181         struct  in_addr nl_inip;
182         u_short nl_origport;
183         u_short nl_outport;
184         u_short nl_inport;
185         u_short nl_type;
186         int     nl_rule;
187         U_QUAD_T        nl_pkts;
188         U_QUAD_T        nl_bytes;
189 } natlog_t;
190
191
192 #define NL_NEWMAP       NAT_MAP
193 #define NL_NEWRDR       NAT_REDIRECT
194 #define NL_EXPIRE       0xffff
195
196 #define NAT_HASH_FN(k,m)        (((k) + ((k) >> 12)) % (m))
197
198 #define LONG_SUM(in)    (((in) & 0xffff) + ((in) >> 16))
199
200 #define CALC_SUMD(s1, s2, sd) { \
201                             (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
202                             (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
203                             /* Do it twice */ \
204                             (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
205                             (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
206                             /* Because ~1 == -2, We really need ~1 == -1 */ \
207                             if ((s1) > (s2)) (s2)--; \
208                             (sd) = (s2) - (s1); \
209                             (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
210
211
212 extern  u_int   ipf_nattable_sz;
213 extern  u_int   ipf_natrules_sz;
214 extern  u_int   ipf_rdrrules_sz;
215 extern  void    ip_natsync __P((void *));
216 extern  u_long  fr_defnatage;
217 extern  u_long  fr_defnaticmpage;
218 extern  nat_t   **nat_table[2];
219 extern  nat_t   *nat_instances;
220 extern  ipnat_t **nat_rules;
221 extern  ipnat_t **rdr_rules;
222 extern  natstat_t       nat_stats;
223 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
224 extern  int     nat_ioctl __P((caddr_t, u_long, int));
225 #else
226 extern  int     nat_ioctl __P((caddr_t, int, int));
227 #endif
228 extern  int     nat_init __P((void));
229 extern  nat_t   *nat_new __P((ipnat_t *, ip_t *, fr_info_t *, u_int, int));
230 extern  nat_t   *nat_outlookup __P((void *, u_int, u_int, struct in_addr,
231                                  struct in_addr, u_32_t));
232 extern  nat_t   *nat_inlookup __P((void *, u_int, u_int, struct in_addr,
233                                 struct in_addr, u_32_t));
234 extern  nat_t   *nat_maplookup __P((void *, u_int, struct in_addr,
235                                 struct in_addr));
236 extern  nat_t   *nat_lookupredir __P((natlookup_t *));
237 extern  nat_t   *nat_icmpinlookup __P((ip_t *, fr_info_t *));
238 extern  nat_t   *nat_icmpin __P((ip_t *, fr_info_t *, u_int *));
239
240 extern  int     ip_natout __P((ip_t *, fr_info_t *));
241 extern  int     ip_natin __P((ip_t *, fr_info_t *));
242 extern  void    ip_natunload __P((void)), ip_natexpire __P((void));
243 extern  void    nat_log __P((struct nat *, u_int));
244 extern  void    fix_incksum __P((u_short *, u_32_t));
245 extern  void    fix_outcksum __P((u_short *, u_32_t));
246
247 #endif /* __IP_NAT_H__ */