ip_nat.h revision 1.4 1 1.4 plunky /* $NetBSD: ip_nat.h,v 1.4 2012/09/15 16:56:45 plunky Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.3 darrenr * Copyright (C) 2012 by Darren Reed.
5 1.1 christos *
6 1.1 christos * See the IPFILTER.LICENCE file for details on licencing.
7 1.1 christos *
8 1.1 christos * @(#)ip_nat.h 1.5 2/4/96
9 1.3 darrenr * Id: ip_nat.h,v 1.1.1.2 2012/07/22 13:45:29 darrenr Exp
10 1.1 christos */
11 1.1 christos
12 1.1 christos #ifndef __IP_NAT_H__
13 1.1 christos #define __IP_NAT_H__
14 1.1 christos
15 1.4 plunky #ifndef SOLARIS
16 1.4 plunky # if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
17 1.4 plunky # define SOLARIS 1
18 1.4 plunky # else
19 1.4 plunky # define SOLARIS 0
20 1.4 plunky # endif
21 1.1 christos #endif
22 1.1 christos
23 1.1 christos #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
24 1.1 christos #define SIOCADNAT _IOW('r', 60, struct ipfobj)
25 1.1 christos #define SIOCRMNAT _IOW('r', 61, struct ipfobj)
26 1.1 christos #define SIOCGNATS _IOWR('r', 62, struct ipfobj)
27 1.1 christos #define SIOCGNATL _IOWR('r', 63, struct ipfobj)
28 1.3 darrenr #define SIOCPURGENAT _IOWR('r', 100, struct ipfobj)
29 1.1 christos #else
30 1.1 christos #define SIOCADNAT _IOW(r, 60, struct ipfobj)
31 1.1 christos #define SIOCRMNAT _IOW(r, 61, struct ipfobj)
32 1.1 christos #define SIOCGNATS _IOWR(r, 62, struct ipfobj)
33 1.1 christos #define SIOCGNATL _IOWR(r, 63, struct ipfobj)
34 1.3 darrenr #define SIOCPURGENAT _IOWR(r, 100, struct ipfobj)
35 1.1 christos #endif
36 1.1 christos
37 1.1 christos #undef LARGE_NAT /* define this if you're setting up a system to NAT
38 1.1 christos * LARGE numbers of networks/hosts - i.e. in the
39 1.1 christos * hundreds or thousands. In such a case, you should
40 1.1 christos * also change the RDR_SIZE and NAT_SIZE below to more
41 1.1 christos * appropriate sizes. The figures below were used for
42 1.1 christos * a setup with 1000-2000 networks to NAT.
43 1.1 christos */
44 1.1 christos #ifndef NAT_SIZE
45 1.1 christos # ifdef LARGE_NAT
46 1.1 christos # define NAT_SIZE 2047
47 1.1 christos # else
48 1.1 christos # define NAT_SIZE 127
49 1.1 christos # endif
50 1.1 christos #endif
51 1.1 christos #ifndef RDR_SIZE
52 1.1 christos # ifdef LARGE_NAT
53 1.1 christos # define RDR_SIZE 2047
54 1.1 christos # else
55 1.1 christos # define RDR_SIZE 127
56 1.1 christos # endif
57 1.1 christos #endif
58 1.1 christos #ifndef HOSTMAP_SIZE
59 1.1 christos # ifdef LARGE_NAT
60 1.1 christos # define HOSTMAP_SIZE 8191
61 1.1 christos # else
62 1.1 christos # define HOSTMAP_SIZE 2047
63 1.1 christos # endif
64 1.1 christos #endif
65 1.1 christos #ifndef NAT_TABLE_MAX
66 1.1 christos /*
67 1.1 christos * This is newly introduced and for the sake of "least surprise", the numbers
68 1.1 christos * present aren't what we'd normally use for creating a proper hash table.
69 1.1 christos */
70 1.1 christos # ifdef LARGE_NAT
71 1.1 christos # define NAT_TABLE_MAX 180000
72 1.1 christos # else
73 1.1 christos # define NAT_TABLE_MAX 30000
74 1.1 christos # endif
75 1.1 christos #endif
76 1.1 christos #ifndef NAT_TABLE_SZ
77 1.1 christos # ifdef LARGE_NAT
78 1.1 christos # define NAT_TABLE_SZ 16383
79 1.1 christos # else
80 1.1 christos # define NAT_TABLE_SZ 2047
81 1.1 christos # endif
82 1.1 christos #endif
83 1.1 christos #ifndef APR_LABELLEN
84 1.1 christos #define APR_LABELLEN 16
85 1.1 christos #endif
86 1.3 darrenr #define NAT_HW_CKSUM 0x80000000
87 1.3 darrenr #define NAT_HW_CKSUM_PART 0x40000000
88 1.1 christos
89 1.1 christos #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */
90 1.1 christos
91 1.1 christos struct ipstate;
92 1.1 christos struct ap_session;
93 1.1 christos
94 1.1 christos /*
95 1.1 christos * This structure is used in the active NAT table and represents an
96 1.1 christos * active NAT session.
97 1.1 christos */
98 1.1 christos typedef struct nat {
99 1.1 christos ipfmutex_t nat_lock;
100 1.1 christos struct nat *nat_next;
101 1.1 christos struct nat **nat_pnext;
102 1.1 christos struct nat *nat_hnext[2];
103 1.1 christos struct nat **nat_phnext[2];
104 1.1 christos struct hostmap *nat_hm;
105 1.1 christos void *nat_data;
106 1.1 christos struct nat **nat_me;
107 1.1 christos struct ipstate *nat_state;
108 1.1 christos struct ap_session *nat_aps; /* proxy session */
109 1.1 christos frentry_t *nat_fr; /* filter rule ptr if appropriate */
110 1.1 christos struct ipnat *nat_ptr; /* pointer back to the rule */
111 1.1 christos void *nat_ifps[2];
112 1.1 christos void *nat_sync;
113 1.1 christos ipftqent_t nat_tqe;
114 1.1 christos int nat_mtu[2];
115 1.1 christos u_32_t nat_flags;
116 1.1 christos u_32_t nat_sumd[2]; /* ip checksum delta for data segment*/
117 1.1 christos u_32_t nat_ipsumd; /* ip checksum delta for ip header */
118 1.1 christos u_32_t nat_mssclamp; /* if != zero clamp MSS to this */
119 1.1 christos i6addr_t nat_odst6;
120 1.1 christos i6addr_t nat_osrc6;
121 1.1 christos i6addr_t nat_ndst6;
122 1.1 christos i6addr_t nat_nsrc6;
123 1.1 christos U_QUAD_T nat_pkts[2];
124 1.1 christos U_QUAD_T nat_bytes[2];
125 1.1 christos union {
126 1.1 christos udpinfo_t nat_unu;
127 1.1 christos tcpinfo_t nat_unt;
128 1.1 christos icmpinfo_t nat_uni;
129 1.1 christos greinfo_t nat_ugre;
130 1.1 christos } nat_unold, nat_unnew;
131 1.1 christos int nat_use;
132 1.1 christos int nat_pr[2]; /* protocol for NAT */
133 1.1 christos int nat_dir;
134 1.1 christos int nat_ref; /* reference count */
135 1.1 christos u_int nat_hv[2];
136 1.1 christos char nat_ifnames[2][LIFNAMSIZ];
137 1.1 christos int nat_rev; /* 0 = forward, 1 = reverse */
138 1.1 christos int nat_dlocal;
139 1.1 christos int nat_v[2]; /* 0 = old, 1 = new */
140 1.1 christos u_int nat_redir; /* copy of in_redir */
141 1.1 christos } nat_t;
142 1.1 christos
143 1.1 christos #define nat_osrcip nat_osrc6.in4
144 1.1 christos #define nat_odstip nat_odst6.in4
145 1.1 christos #define nat_nsrcip nat_nsrc6.in4
146 1.1 christos #define nat_ndstip nat_ndst6.in4
147 1.1 christos #define nat_osrcaddr nat_osrc6.in4.s_addr
148 1.1 christos #define nat_odstaddr nat_odst6.in4.s_addr
149 1.1 christos #define nat_nsrcaddr nat_nsrc6.in4.s_addr
150 1.1 christos #define nat_ndstaddr nat_ndst6.in4.s_addr
151 1.1 christos #define nat_age nat_tqe.tqe_die
152 1.1 christos #define nat_osport nat_unold.nat_unt.ts_sport
153 1.1 christos #define nat_odport nat_unold.nat_unt.ts_dport
154 1.1 christos #define nat_nsport nat_unnew.nat_unt.ts_sport
155 1.1 christos #define nat_ndport nat_unnew.nat_unt.ts_dport
156 1.1 christos #define nat_oicmpid nat_unold.nat_uni.ici_id
157 1.1 christos #define nat_nicmpid nat_unnew.nat_uni.ici_id
158 1.1 christos #define nat_type nat_unold.nat_uni.ici_type
159 1.1 christos #define nat_oseq nat_unold.nat_uni.ici_seq
160 1.1 christos #define nat_nseq nat_unnew.nat_uni.ici_seq
161 1.1 christos #define nat_tcpstate nat_tqe.tqe_state
162 1.1 christos #define nat_die nat_tqe.tqe_die
163 1.1 christos #define nat_touched nat_tqe.tqe_touched
164 1.1 christos
165 1.1 christos /*
166 1.1 christos * Values for nat_dir
167 1.1 christos */
168 1.1 christos #define NAT_INBOUND 0
169 1.1 christos #define NAT_OUTBOUND 1
170 1.1 christos #define NAT_ENCAPIN 2
171 1.1 christos #define NAT_ENCAPOUT 3
172 1.1 christos #define NAT_DIVERTIN 4
173 1.1 christos #define NAT_DIVERTOUT 5
174 1.1 christos
175 1.1 christos /*
176 1.1 christos * Definitions for nat_flags
177 1.1 christos */
178 1.1 christos #define NAT_TCP 0x0001 /* IPN_TCP */
179 1.1 christos #define NAT_UDP 0x0002 /* IPN_UDP */
180 1.1 christos #define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */
181 1.1 christos #define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */
182 1.1 christos #define NAT_SEARCH 0x0010
183 1.1 christos #define NAT_SLAVE 0x0020 /* Slave connection for a proxy */
184 1.1 christos #define NAT_NOTRULEPORT 0x0040 /* Don't use the port # in the NAT rule */
185 1.1 christos
186 1.1 christos #define NAT_TCPUDP (NAT_TCP|NAT_UDP)
187 1.1 christos #define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR)
188 1.1 christos #define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
189 1.1 christos #define NAT_FROMRULE (NAT_TCP|NAT_UDP)
190 1.1 christos
191 1.1 christos /* 0x0100 reserved for FI_W_SPORT */
192 1.1 christos /* 0x0200 reserved for FI_W_DPORT */
193 1.1 christos /* 0x0400 reserved for FI_W_SADDR */
194 1.1 christos /* 0x0800 reserved for FI_W_DADDR */
195 1.1 christos /* 0x1000 reserved for FI_W_NEWFR */
196 1.1 christos /* 0x2000 reserved for SI_CLONE */
197 1.1 christos /* 0x4000 reserved for SI_CLONED */
198 1.1 christos /* 0x8000 reserved for SI_IGNOREPKT */
199 1.1 christos
200 1.1 christos #define NAT_DEBUG 0x800000
201 1.1 christos
202 1.1 christos typedef struct nat_addr_s {
203 1.1 christos i6addr_t na_addr[2];
204 1.1 christos i6addr_t na_nextaddr;
205 1.1 christos int na_atype;
206 1.1 christos int na_function;
207 1.1 christos } nat_addr_t;
208 1.1 christos
209 1.1 christos #define na_nextip na_nextaddr.in4.s_addr
210 1.1 christos #define na_nextip6 na_nextaddr.in6
211 1.1 christos #define na_num na_addr[0].iplookupnum
212 1.1 christos #define na_type na_addr[0].iplookuptype
213 1.1 christos #define na_subtype na_addr[0].iplookupsubtype
214 1.1 christos #define na_ptr na_addr[1].iplookupptr
215 1.1 christos #define na_func na_addr[1].iplookupfunc
216 1.1 christos
217 1.1 christos
218 1.1 christos /*
219 1.1 christos * This structure represents an actual NAT rule, loaded by ipnat.
220 1.1 christos */
221 1.1 christos typedef struct ipnat {
222 1.1 christos ipfmutex_t in_lock;
223 1.1 christos struct ipnat *in_next; /* NAT rule list next */
224 1.1 christos struct ipnat **in_pnext; /* prior rdr next ptr */
225 1.1 christos struct ipnat *in_rnext; /* rdr rule hash next */
226 1.1 christos struct ipnat **in_prnext; /* prior rdr next ptr */
227 1.1 christos struct ipnat *in_mnext; /* map rule hash next */
228 1.1 christos struct ipnat **in_pmnext; /* prior map next ptr */
229 1.1 christos struct ipftq *in_tqehead[2];
230 1.1 christos void *in_ifps[2];
231 1.1 christos void *in_apr;
232 1.1 christos char *in_comment;
233 1.1 christos mb_t *in_divmp;
234 1.1 christos void *in_pconf;
235 1.3 darrenr U_QUAD_T in_pkts[2];
236 1.3 darrenr U_QUAD_T in_bytes[2];
237 1.1 christos u_long in_space;
238 1.1 christos u_long in_hits;
239 1.1 christos int in_size;
240 1.3 darrenr int in_use;
241 1.1 christos u_int in_hv[2];
242 1.1 christos int in_flineno; /* conf. file line number */
243 1.1 christos int in_stepnext;
244 1.1 christos int in_dlocal;
245 1.1 christos u_short in_dpnext;
246 1.1 christos u_short in_spnext;
247 1.1 christos /* From here to the end is covered by IPN_CMPSIZ */
248 1.1 christos u_char in_v[2]; /* 0 = old, 1 = new */
249 1.1 christos u_32_t in_flags;
250 1.1 christos u_32_t in_mssclamp; /* if != 0 clamp MSS to this */
251 1.1 christos u_int in_age[2];
252 1.1 christos int in_redir; /* see below for values */
253 1.1 christos int in_pr[2]; /* protocol. */
254 1.1 christos nat_addr_t in_ndst;
255 1.1 christos nat_addr_t in_nsrc;
256 1.1 christos nat_addr_t in_osrc;
257 1.1 christos nat_addr_t in_odst;
258 1.1 christos frtuc_t in_tuc;
259 1.1 christos u_short in_ppip; /* ports per IP. */
260 1.1 christos u_short in_ippip; /* IP #'s per IP# */
261 1.1 christos u_short in_ndports[2];
262 1.1 christos u_short in_nsports[2];
263 1.1 christos int in_ifnames[2];
264 1.1 christos int in_plabel; /* proxy label. */
265 1.1 christos int in_pconfig; /* proxy label. */
266 1.1 christos ipftag_t in_tag;
267 1.1 christos int in_namelen;
268 1.1 christos char in_names[1];
269 1.1 christos } ipnat_t;
270 1.1 christos
271 1.1 christos /*
272 1.1 christos * MAP-IN MAP-OUT RDR-IN RDR-OUT
273 1.1 christos * osrc X == src == src X
274 1.1 christos * odst X == dst == dst X
275 1.1 christos * nsrc == dst X X == dst
276 1.1 christos * ndst == src X X == src
277 1.1 christos */
278 1.1 christos #define in_dpmin in_ndports[0] /* Also holds static redir port */
279 1.1 christos #define in_dpmax in_ndports[1]
280 1.1 christos #define in_spmin in_nsports[0] /* Also holds static redir port */
281 1.1 christos #define in_spmax in_nsports[1]
282 1.1 christos #define in_ndport in_ndports[0]
283 1.1 christos #define in_nsport in_nsports[0]
284 1.1 christos #define in_dipnext in_ndst.na_nextaddr.in4
285 1.1 christos #define in_dipnext6 in_ndst.na_nextaddr
286 1.1 christos #define in_dnip in_ndst.na_nextaddr.in4.s_addr
287 1.1 christos #define in_dnip6 in_ndst.na_nextaddr
288 1.1 christos #define in_sipnext in_nsrc.na_nextaddr.in4
289 1.1 christos #define in_snip in_nsrc.na_nextaddr.in4.s_addr
290 1.1 christos #define in_snip6 in_nsrc.na_nextaddr
291 1.1 christos #define in_odstip in_odst.na_addr[0].in4
292 1.1 christos #define in_odstip6 in_odst.na_addr[0]
293 1.1 christos #define in_odstaddr in_odst.na_addr[0].in4.s_addr
294 1.1 christos #define in_odstmsk in_odst.na_addr[1].in4.s_addr
295 1.1 christos #define in_odstmsk6 in_odst.na_addr[1]
296 1.1 christos #define in_odstatype in_odst.na_atype
297 1.1 christos #define in_osrcip in_osrc.na_addr[0].in4
298 1.1 christos #define in_osrcip6 in_osrc.na_addr[0]
299 1.1 christos #define in_osrcaddr in_osrc.na_addr[0].in4.s_addr
300 1.1 christos #define in_osrcmsk in_osrc.na_addr[1].in4.s_addr
301 1.1 christos #define in_osrcmsk6 in_osrc.na_addr[1]
302 1.1 christos #define in_osrcatype in_osrc.na_atype
303 1.1 christos #define in_ndstip in_ndst.na_addr[0].in4
304 1.1 christos #define in_ndstip6 in_ndst.na_addr[0]
305 1.1 christos #define in_ndstaddr in_ndst.na_addr[0].in4.s_addr
306 1.1 christos #define in_ndstmsk in_ndst.na_addr[1].in4.s_addr
307 1.1 christos #define in_ndstmsk6 in_ndst.na_addr[1]
308 1.1 christos #define in_ndstatype in_ndst.na_atype
309 1.1 christos #define in_ndstafunc in_ndst.na_function
310 1.1 christos #define in_nsrcip in_nsrc.na_addr[0].in4
311 1.1 christos #define in_nsrcip6 in_nsrc.na_addr[0]
312 1.1 christos #define in_nsrcaddr in_nsrc.na_addr[0].in4.s_addr
313 1.1 christos #define in_nsrcmsk in_nsrc.na_addr[1].in4.s_addr
314 1.1 christos #define in_nsrcmsk6 in_nsrc.na_addr[1]
315 1.1 christos #define in_nsrcatype in_nsrc.na_atype
316 1.1 christos #define in_nsrcafunc in_nsrc.na_function
317 1.1 christos #define in_scmp in_tuc.ftu_scmp
318 1.1 christos #define in_dcmp in_tuc.ftu_dcmp
319 1.1 christos #define in_stop in_tuc.ftu_stop
320 1.1 christos #define in_dtop in_tuc.ftu_dtop
321 1.1 christos #define in_osport in_tuc.ftu_sport
322 1.1 christos #define in_odport in_tuc.ftu_dport
323 1.1 christos #define in_ndstnum in_ndst.na_addr[0].iplookupnum
324 1.1 christos #define in_ndsttype in_ndst.na_addr[0].iplookuptype
325 1.1 christos #define in_ndstptr in_ndst.na_addr[1].iplookupptr
326 1.1 christos #define in_ndstfunc in_ndst.na_addr[1].iplookupfunc
327 1.1 christos #define in_nsrcnum in_nsrc.na_addr[0].iplookupnum
328 1.1 christos #define in_nsrctype in_nsrc.na_addr[0].iplookuptype
329 1.1 christos #define in_nsrcptr in_nsrc.na_addr[1].iplookupptr
330 1.1 christos #define in_nsrcfunc in_nsrc.na_addr[1].iplookupfunc
331 1.1 christos #define in_odstnum in_odst.na_addr[0].iplookupnum
332 1.1 christos #define in_odsttype in_odst.na_addr[0].iplookuptype
333 1.1 christos #define in_odstptr in_odst.na_addr[1].iplookupptr
334 1.1 christos #define in_odstfunc in_odst.na_addr[1].iplookupfunc
335 1.1 christos #define in_osrcnum in_osrc.na_addr[0].iplookupnum
336 1.1 christos #define in_osrctype in_osrc.na_addr[0].iplookuptype
337 1.1 christos #define in_osrcptr in_osrc.na_addr[1].iplookupptr
338 1.1 christos #define in_osrcfunc in_osrc.na_addr[1].iplookupfunc
339 1.1 christos #define in_icmpidmin in_nsports[0]
340 1.1 christos #define in_icmpidmax in_nsports[1]
341 1.1 christos
342 1.1 christos /*
343 1.1 christos * Bit definitions for in_flags
344 1.1 christos */
345 1.1 christos #define IPN_ANY 0x00000
346 1.1 christos #define IPN_TCP 0x00001
347 1.1 christos #define IPN_UDP 0x00002
348 1.1 christos #define IPN_TCPUDP (IPN_TCP|IPN_UDP)
349 1.1 christos #define IPN_ICMPERR 0x00004
350 1.1 christos #define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR)
351 1.1 christos #define IPN_ICMPQUERY 0x00008
352 1.1 christos #define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
353 1.1 christos #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
354 1.1 christos #define IPN_AUTOPORTMAP 0x00010
355 1.1 christos #define IPN_FILTER 0x00020
356 1.1 christos #define IPN_SPLIT 0x00040
357 1.1 christos #define IPN_ROUNDR 0x00080
358 1.1 christos #define IPN_SIPRANGE 0x00100
359 1.1 christos #define IPN_DIPRANGE 0x00200
360 1.1 christos #define IPN_NOTSRC 0x00400
361 1.1 christos #define IPN_NOTDST 0x00800
362 1.1 christos #define IPN_NO 0x01000
363 1.1 christos #define IPN_DYNSRCIP 0x02000 /* dynamic src IP# */
364 1.1 christos #define IPN_DYNDSTIP 0x04000 /* dynamic dst IP# */
365 1.1 christos #define IPN_DELETE 0x08000
366 1.1 christos #define IPN_STICKY 0x10000
367 1.1 christos #define IPN_FRAG 0x20000
368 1.1 christos #define IPN_FIXEDSPORT 0x40000
369 1.1 christos #define IPN_FIXEDDPORT 0x80000
370 1.1 christos #define IPN_FINDFORWARD 0x100000
371 1.1 christos #define IPN_IN 0x200000
372 1.1 christos #define IPN_SEQUENTIAL 0x400000
373 1.3 darrenr #define IPN_PURGE 0x800000
374 1.3 darrenr #define IPN_PROXYRULE 0x1000000
375 1.1 christos #define IPN_USERFLAGS (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_SIPRANGE|IPN_SPLIT|\
376 1.1 christos IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|IPN_NO|\
377 1.1 christos IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY|\
378 1.3 darrenr IPN_DIPRANGE|IPN_SEQUENTIAL|IPN_PURGE)
379 1.1 christos
380 1.1 christos /*
381 1.1 christos * Values for in_redir
382 1.1 christos */
383 1.1 christos #define NAT_MAP 0x01
384 1.1 christos #define NAT_REDIRECT 0x02
385 1.1 christos #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT)
386 1.1 christos #define NAT_MAPBLK 0x04
387 1.1 christos #define NAT_REWRITE 0x08
388 1.1 christos #define NAT_ENCAP 0x10
389 1.1 christos #define NAT_DIVERTUDP 0x20
390 1.1 christos
391 1.1 christos #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */
392 1.1 christos #define USABLE_PORTS (65536 - MAPBLK_MINPORT)
393 1.1 christos
394 1.1 christos #define IPN_CMPSIZ (sizeof(ipnat_t) - offsetof(ipnat_t, in_v))
395 1.1 christos
396 1.1 christos typedef struct natlookup {
397 1.1 christos i6addr_t nl_inipaddr;
398 1.1 christos i6addr_t nl_outipaddr;
399 1.1 christos i6addr_t nl_realipaddr;
400 1.1 christos int nl_v;
401 1.1 christos int nl_flags;
402 1.1 christos u_short nl_inport;
403 1.1 christos u_short nl_outport;
404 1.1 christos u_short nl_realport;
405 1.1 christos } natlookup_t;
406 1.1 christos
407 1.1 christos #define nl_inip nl_inipaddr.in4
408 1.1 christos #define nl_outip nl_outipaddr.in4
409 1.1 christos #define nl_realip nl_realipaddr.in4
410 1.1 christos #define nl_inip6 nl_inipaddr.in6
411 1.1 christos #define nl_outip6 nl_outipaddr.in6
412 1.1 christos #define nl_realip6 nl_realipaddr.in6
413 1.1 christos
414 1.1 christos
415 1.1 christos typedef struct nat_save {
416 1.1 christos void *ipn_next;
417 1.1 christos struct nat ipn_nat;
418 1.1 christos struct ipnat ipn_ipnat;
419 1.1 christos struct frentry ipn_fr;
420 1.1 christos int ipn_dsize;
421 1.1 christos char ipn_data[4];
422 1.1 christos } nat_save_t;
423 1.1 christos
424 1.1 christos #define ipn_rule ipn_nat.nat_fr
425 1.1 christos
426 1.1 christos typedef struct natget {
427 1.1 christos void *ng_ptr;
428 1.1 christos int ng_sz;
429 1.1 christos } natget_t;
430 1.1 christos
431 1.1 christos
432 1.1 christos /*
433 1.1 christos * This structure gets used to help NAT sessions keep the same NAT rule (and
434 1.1 christos * thus translation for IP address) when:
435 1.1 christos * (a) round-robin redirects are in use
436 1.1 christos * (b) different IP add
437 1.1 christos */
438 1.1 christos typedef struct hostmap {
439 1.1 christos struct hostmap *hm_hnext;
440 1.1 christos struct hostmap **hm_phnext;
441 1.1 christos struct hostmap *hm_next;
442 1.1 christos struct hostmap **hm_pnext;
443 1.1 christos struct ipnat *hm_ipnat;
444 1.1 christos i6addr_t hm_osrcip6;
445 1.1 christos i6addr_t hm_odstip6;
446 1.1 christos i6addr_t hm_nsrcip6;
447 1.1 christos i6addr_t hm_ndstip6;
448 1.1 christos u_32_t hm_port;
449 1.1 christos int hm_ref;
450 1.1 christos int hm_hv;
451 1.1 christos int hm_v;
452 1.1 christos } hostmap_t;
453 1.1 christos
454 1.1 christos #define hm_osrcip hm_osrcip6.in4
455 1.1 christos #define hm_odstip hm_odstip6.in4
456 1.1 christos #define hm_nsrcip hm_nsrcip6.in4
457 1.1 christos #define hm_ndstip hm_ndstip6.in4
458 1.1 christos #define hm_osrc6 hm_osrcip6.in6
459 1.1 christos #define hm_odst6 hm_odstip6.in6
460 1.1 christos #define hm_nsrc6 hm_nsrcip6.in6
461 1.1 christos #define hm_ndst6 hm_ndstip6.in6
462 1.1 christos
463 1.1 christos
464 1.1 christos /*
465 1.1 christos * Structure used to pass information in to nat_newmap and nat_newrdr.
466 1.1 christos */
467 1.1 christos typedef struct natinfo {
468 1.1 christos ipnat_t *nai_np;
469 1.1 christos u_32_t nai_sum1;
470 1.1 christos u_32_t nai_sum2;
471 1.1 christos struct in_addr nai_ip; /* In host byte order */
472 1.1 christos u_short nai_port;
473 1.1 christos u_short nai_nport;
474 1.1 christos u_short nai_sport;
475 1.1 christos u_short nai_dport;
476 1.1 christos } natinfo_t;
477 1.1 christos
478 1.1 christos
479 1.1 christos typedef struct nat_stat_side {
480 1.1 christos u_int *ns_bucketlen;
481 1.1 christos nat_t **ns_table;
482 1.1 christos u_long ns_added;
483 1.1 christos u_long ns_appr_fail;
484 1.1 christos u_long ns_badnat;
485 1.1 christos u_long ns_badnatnew;
486 1.1 christos u_long ns_badnextaddr;
487 1.1 christos u_long ns_bucket_max;
488 1.1 christos u_long ns_clone_nomem;
489 1.1 christos u_long ns_decap_bad;
490 1.1 christos u_long ns_decap_fail;
491 1.1 christos u_long ns_decap_pullup;
492 1.1 christos u_long ns_divert_dup;
493 1.1 christos u_long ns_divert_exist;
494 1.1 christos u_long ns_drop;
495 1.1 christos u_long ns_encap_dup;
496 1.1 christos u_long ns_encap_pullup;
497 1.1 christos u_long ns_exhausted;
498 1.1 christos u_long ns_icmp_address;
499 1.1 christos u_long ns_icmp_basic;
500 1.1 christos u_long ns_icmp_mbuf;
501 1.1 christos u_long ns_icmp_notfound;
502 1.1 christos u_long ns_icmp_rebuild;
503 1.1 christos u_long ns_icmp_short;
504 1.1 christos u_long ns_icmp_size;
505 1.1 christos u_long ns_ifpaddrfail;
506 1.1 christos u_long ns_ignored;
507 1.1 christos u_long ns_insert_fail;
508 1.1 christos u_long ns_inuse;
509 1.1 christos u_long ns_log;
510 1.1 christos u_long ns_lookup_miss;
511 1.1 christos u_long ns_lookup_nowild;
512 1.1 christos u_long ns_new_ifpaddr;
513 1.1 christos u_long ns_memfail;
514 1.1 christos u_long ns_table_max;
515 1.1 christos u_long ns_translated;
516 1.1 christos u_long ns_unfinalised;
517 1.1 christos u_long ns_wrap;
518 1.1 christos u_long ns_xlate_null;
519 1.1 christos u_long ns_xlate_exists;
520 1.1 christos u_long ns_ipf_proxy_fail;
521 1.1 christos u_long ns_uncreate[2];
522 1.1 christos } nat_stat_side_t;
523 1.1 christos
524 1.1 christos
525 1.1 christos typedef struct natstat {
526 1.1 christos nat_t *ns_instances;
527 1.1 christos ipnat_t *ns_list;
528 1.1 christos hostmap_t *ns_maplist;
529 1.1 christos hostmap_t **ns_maptable;
530 1.1 christos u_int ns_active;
531 1.1 christos u_long ns_addtrpnt;
532 1.1 christos u_long ns_divert_build;
533 1.1 christos u_long ns_expire;
534 1.1 christos u_long ns_flush_all;
535 1.1 christos u_long ns_flush_closing;
536 1.1 christos u_long ns_flush_queue;
537 1.1 christos u_long ns_flush_state;
538 1.1 christos u_long ns_flush_timeout;
539 1.1 christos u_long ns_hm_new;
540 1.1 christos u_long ns_hm_newfail;
541 1.1 christos u_long ns_hm_addref;
542 1.1 christos u_long ns_hm_nullnp;
543 1.1 christos u_long ns_log_ok;
544 1.1 christos u_long ns_log_fail;
545 1.1 christos u_int ns_hostmap_sz;
546 1.1 christos u_int ns_nattab_sz;
547 1.1 christos u_int ns_nattab_max;
548 1.1 christos u_int ns_orphans;
549 1.1 christos u_int ns_rules;
550 1.1 christos u_int ns_rules_map;
551 1.1 christos u_int ns_rules_rdr;
552 1.1 christos u_int ns_rultab_sz;
553 1.1 christos u_int ns_rdrtab_sz;
554 1.1 christos u_32_t ns_ticks;
555 1.1 christos u_int ns_trpntab_sz;
556 1.1 christos u_int ns_wilds;
557 1.1 christos u_long ns_proto[256];
558 1.1 christos nat_stat_side_t ns_side[2];
559 1.1 christos #ifdef USE_INET6
560 1.1 christos nat_stat_side_t ns_side6[2];
561 1.1 christos #endif
562 1.1 christos } natstat_t;
563 1.1 christos
564 1.1 christos typedef struct natlog {
565 1.1 christos i6addr_t nl_osrcip;
566 1.1 christos i6addr_t nl_odstip;
567 1.1 christos i6addr_t nl_nsrcip;
568 1.1 christos i6addr_t nl_ndstip;
569 1.1 christos u_short nl_osrcport;
570 1.1 christos u_short nl_odstport;
571 1.1 christos u_short nl_nsrcport;
572 1.1 christos u_short nl_ndstport;
573 1.1 christos int nl_action;
574 1.1 christos int nl_type;
575 1.1 christos int nl_rule;
576 1.1 christos U_QUAD_T nl_pkts[2];
577 1.1 christos U_QUAD_T nl_bytes[2];
578 1.1 christos u_char nl_p[2];
579 1.1 christos u_char nl_v[2];
580 1.1 christos u_char nl_ifnames[2][LIFNAMSIZ];
581 1.1 christos } natlog_t;
582 1.1 christos
583 1.1 christos
584 1.1 christos #define NL_NEW 0
585 1.1 christos #define NL_CLONE 1
586 1.3 darrenr #define NL_PURGE 0xfffc
587 1.1 christos #define NL_DESTROY 0xfffd
588 1.1 christos #define NL_FLUSH 0xfffe
589 1.1 christos #define NL_EXPIRE 0xffff
590 1.1 christos
591 1.1 christos #define NAT_HASH_FN(_k,_l,_m) (((_k) + ((_k) >> 12) + _l) % (_m))
592 1.1 christos #define NAT_HASH_FN6(_k,_l,_m) ((((u_32_t *)(_k))[3] \
593 1.1 christos + (((u_32_t *)(_k))[3] >> 12) \
594 1.1 christos + (((u_32_t *)(_k))[2]) \
595 1.1 christos + (((u_32_t *)(_k))[2] >> 12) \
596 1.1 christos + (((u_32_t *)(_k))[1]) \
597 1.1 christos + (((u_32_t *)(_k))[1] >> 12) \
598 1.1 christos + (((u_32_t *)(_k))[0]) \
599 1.1 christos + (((u_32_t *)(_k))[0] >> 12) \
600 1.1 christos + _l) % (_m))
601 1.1 christos
602 1.1 christos #define LONG_SUM(_i) (((_i) & 0xffff) + ((_i) >> 16))
603 1.1 christos #define LONG_SUM6(_i) (LONG_SUM(ntohl(((u_32_t *)(_i))[0])) + \
604 1.1 christos LONG_SUM(ntohl(((u_32_t *)(_i))[1])) + \
605 1.1 christos LONG_SUM(ntohl(((u_32_t *)(_i))[2])) + \
606 1.1 christos LONG_SUM(ntohl(((u_32_t *)(_i))[3])))
607 1.1 christos
608 1.1 christos #define CALC_SUMD(s1, s2, sd) { \
609 1.1 christos (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
610 1.1 christos (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
611 1.1 christos /* Do it twice */ \
612 1.1 christos (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
613 1.1 christos (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
614 1.1 christos /* Because ~1 == -2, We really need ~1 == -1 */ \
615 1.1 christos if ((s1) > (s2)) (s2)--; \
616 1.1 christos (sd) = (s2) - (s1); \
617 1.1 christos (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
618 1.1 christos
619 1.1 christos #define NAT_SYSSPACE 0x80000000
620 1.1 christos #define NAT_LOCKHELD 0x40000000
621 1.1 christos
622 1.1 christos /*
623 1.1 christos * This is present in ip_nat.h because it needs to be shared between
624 1.1 christos * ip_nat.c and ip_nat6.c
625 1.1 christos */
626 1.1 christos typedef struct ipf_nat_softc_s {
627 1.1 christos ipfmutex_t ipf_nat_new;
628 1.1 christos ipfmutex_t ipf_nat_io;
629 1.1 christos int ipf_nat_doflush;
630 1.1 christos int ipf_nat_logging;
631 1.1 christos int ipf_nat_lock;
632 1.1 christos int ipf_nat_inited;
633 1.1 christos int ipf_nat_table_wm_high;
634 1.1 christos int ipf_nat_table_wm_low;
635 1.1 christos u_int ipf_nat_table_max;
636 1.1 christos u_int ipf_nat_table_sz;
637 1.1 christos u_int ipf_nat_maprules_sz;
638 1.1 christos u_int ipf_nat_rdrrules_sz;
639 1.1 christos u_int ipf_nat_hostmap_sz;
640 1.1 christos u_int ipf_nat_maxbucket;
641 1.1 christos u_int ipf_nat_last_force_flush;
642 1.1 christos u_int ipf_nat_defage;
643 1.1 christos u_int ipf_nat_defipage;
644 1.1 christos u_int ipf_nat_deficmpage;
645 1.3 darrenr ipf_v4_masktab_t ipf_nat_map_mask;
646 1.3 darrenr ipf_v6_masktab_t ipf_nat6_map_mask;
647 1.3 darrenr ipf_v4_masktab_t ipf_nat_rdr_mask;
648 1.3 darrenr ipf_v6_masktab_t ipf_nat6_rdr_mask;
649 1.1 christos nat_t **ipf_nat_table[2];
650 1.1 christos nat_t *ipf_nat_instances;
651 1.1 christos ipnat_t *ipf_nat_list;
652 1.3 darrenr ipnat_t **ipf_nat_list_tail;
653 1.1 christos ipnat_t **ipf_nat_map_rules;
654 1.1 christos ipnat_t **ipf_nat_rdr_rules;
655 1.1 christos ipftq_t *ipf_nat_utqe;
656 1.1 christos hostmap_t **ipf_hm_maptable ;
657 1.1 christos hostmap_t *ipf_hm_maplist ;
658 1.1 christos ipftuneable_t *ipf_nat_tune;
659 1.1 christos ipftq_t ipf_nat_udptq;
660 1.1 christos ipftq_t ipf_nat_udpacktq;
661 1.1 christos ipftq_t ipf_nat_icmptq;
662 1.1 christos ipftq_t ipf_nat_icmpacktq;
663 1.1 christos ipftq_t ipf_nat_iptq;
664 1.1 christos ipftq_t ipf_nat_pending;
665 1.1 christos ipftq_t ipf_nat_tcptq[IPF_TCP_NSTATES];
666 1.1 christos natstat_t ipf_nat_stats;
667 1.1 christos } ipf_nat_softc_t ;
668 1.1 christos
669 1.3 darrenr #define ipf_nat_map_max ipf_nat_map_mask.imt4_max
670 1.3 darrenr #define ipf_nat_rdr_max ipf_nat_rdr_mask.imt4_max
671 1.3 darrenr #define ipf_nat6_map_max ipf_nat6_map_mask.imt6_max
672 1.3 darrenr #define ipf_nat6_rdr_max ipf_nat6_rdr_mask.imt6_max
673 1.3 darrenr #define ipf_nat_map_active_masks ipf_nat_map_mask.imt4_active
674 1.3 darrenr #define ipf_nat_rdr_active_masks ipf_nat_rdr_mask.imt4_active
675 1.3 darrenr #define ipf_nat6_map_active_masks ipf_nat6_map_mask.imt6_active
676 1.3 darrenr #define ipf_nat6_rdr_active_masks ipf_nat6_rdr_mask.imt6_active
677 1.3 darrenr
678 1.1 christos extern frentry_t ipfnatblock;
679 1.1 christos
680 1.2 christos extern void ipf_fix_datacksum(u_short *, u_32_t);
681 1.3 darrenr extern void ipf_fix_incksum(int, u_short *, u_32_t, u_32_t);
682 1.3 darrenr extern void ipf_fix_outcksum(int, u_short *, u_32_t, u_32_t);
683 1.2 christos
684 1.2 christos extern int ipf_nat_checkin(fr_info_t *, u_32_t *);
685 1.2 christos extern int ipf_nat_checkout(fr_info_t *, u_32_t *);
686 1.2 christos extern void ipf_nat_delete(ipf_main_softc_t *, struct nat *, int);
687 1.2 christos extern void ipf_nat_deref(ipf_main_softc_t *, nat_t **);
688 1.2 christos extern void ipf_nat_expire(ipf_main_softc_t *);
689 1.3 darrenr extern int ipf_nat_hashtab_add(ipf_main_softc_t *,
690 1.3 darrenr ipf_nat_softc_t *, nat_t *);
691 1.3 darrenr extern void ipf_nat_hostmapdel(ipf_main_softc_t *, hostmap_t **);
692 1.2 christos extern int ipf_nat_hostmap_rehash(ipf_main_softc_t *,
693 1.2 christos ipftuneable_t *, ipftuneval_t *);
694 1.2 christos extern nat_t *ipf_nat_icmperrorlookup(fr_info_t *, int);
695 1.2 christos extern nat_t *ipf_nat_icmperror(fr_info_t *, u_int *, int);
696 1.1 christos #if defined(__OpenBSD__)
697 1.2 christos extern void ipf_nat_ifdetach(void *);
698 1.1 christos #endif
699 1.2 christos extern int ipf_nat_init(void);
700 1.2 christos extern nat_t *ipf_nat_inlookup(fr_info_t *, u_int, u_int,
701 1.2 christos struct in_addr, struct in_addr);
702 1.2 christos extern int ipf_nat_in(fr_info_t *, nat_t *, int, u_32_t);
703 1.2 christos extern int ipf_nat_insert(ipf_main_softc_t *, ipf_nat_softc_t *,
704 1.2 christos nat_t *);
705 1.2 christos extern int ipf_nat_ioctl(ipf_main_softc_t *, void *, ioctlcmd_t,
706 1.2 christos int, int, void *);
707 1.2 christos extern void ipf_nat_log(ipf_main_softc_t *, ipf_nat_softc_t *,
708 1.2 christos struct nat *, u_int);
709 1.2 christos extern nat_t *ipf_nat_lookupredir(natlookup_t *);
710 1.2 christos extern nat_t *ipf_nat_maplookup(void *, u_int, struct in_addr,
711 1.2 christos struct in_addr);
712 1.2 christos extern nat_t *ipf_nat_add(fr_info_t *, ipnat_t *, nat_t **,
713 1.2 christos u_int, int);
714 1.2 christos extern int ipf_nat_out(fr_info_t *, nat_t *, int, u_32_t);
715 1.2 christos extern nat_t *ipf_nat_outlookup(fr_info_t *, u_int, u_int,
716 1.2 christos struct in_addr, struct in_addr);
717 1.2 christos extern u_short *ipf_nat_proto(fr_info_t *, nat_t *, u_int);
718 1.3 darrenr extern void ipf_nat_rule_deref(ipf_main_softc_t *, ipnat_t **);
719 1.2 christos extern void ipf_nat_setqueue(ipf_main_softc_t *, ipf_nat_softc_t *,
720 1.2 christos nat_t *);
721 1.2 christos extern void ipf_nat_setpending(ipf_main_softc_t *, nat_t *);
722 1.2 christos extern nat_t *ipf_nat_tnlookup(fr_info_t *, int);
723 1.2 christos extern void ipf_nat_update(fr_info_t *, nat_t *);
724 1.2 christos extern frentry_t *ipf_nat_ipfin(fr_info_t *, u_32_t *);
725 1.2 christos extern frentry_t *ipf_nat_ipfout(fr_info_t *, u_32_t *);
726 1.2 christos extern int ipf_nat_in(fr_info_t *, nat_t *, int, u_32_t);
727 1.2 christos extern int ipf_nat_out(fr_info_t *, nat_t *, int, u_32_t);
728 1.2 christos extern int ipf_nat_rehash(ipf_main_softc_t *, ipftuneable_t *,
729 1.2 christos ipftuneval_t *);
730 1.2 christos extern int ipf_nat_rehash_rules(ipf_main_softc_t *, ipftuneable_t *,
731 1.2 christos ipftuneval_t *);
732 1.2 christos extern int ipf_nat_settimeout(struct ipf_main_softc_s *,
733 1.2 christos ipftuneable_t *, ipftuneval_t *);
734 1.2 christos extern void ipf_nat_sync(ipf_main_softc_t *, void *);
735 1.2 christos
736 1.2 christos extern nat_t *ipf_nat_clone(fr_info_t *, nat_t *);
737 1.2 christos extern void ipf_nat_delmap(ipf_nat_softc_t *, ipnat_t *);
738 1.2 christos extern void ipf_nat_delrdr(ipf_nat_softc_t *, ipnat_t *);
739 1.2 christos extern int ipf_nat_wildok(nat_t *, int, int, int, int);
740 1.2 christos extern void ipf_nat_setlock(void *, int);
741 1.2 christos extern void ipf_nat_load(void);
742 1.2 christos extern void *ipf_nat_soft_create(ipf_main_softc_t *);
743 1.2 christos extern int ipf_nat_soft_init(ipf_main_softc_t *, void *);
744 1.2 christos extern void ipf_nat_soft_destroy(ipf_main_softc_t *, void *);
745 1.2 christos extern int ipf_nat_soft_fini(ipf_main_softc_t *, void *);
746 1.2 christos extern int ipf_nat_main_load(void);
747 1.2 christos extern int ipf_nat_main_unload(void);
748 1.2 christos extern ipftq_t *ipf_nat_add_tq(ipf_main_softc_t *, int);
749 1.2 christos extern void ipf_nat_uncreate(fr_info_t *);
750 1.1 christos
751 1.1 christos #ifdef USE_INET6
752 1.2 christos extern nat_t *ipf_nat6_add(fr_info_t *, ipnat_t *, nat_t **,
753 1.2 christos u_int, int);
754 1.2 christos extern void ipf_nat6_addrdr(ipf_nat_softc_t *, ipnat_t *);
755 1.2 christos extern void ipf_nat6_addmap(ipf_nat_softc_t *, ipnat_t *);
756 1.2 christos extern int ipf_nat6_checkout(fr_info_t *, u_32_t *);
757 1.2 christos extern int ipf_nat6_checkin(fr_info_t *, u_32_t *);
758 1.3 darrenr extern void ipf_nat6_delmap(ipf_nat_softc_t *, ipnat_t *);
759 1.3 darrenr extern void ipf_nat6_delrdr(ipf_nat_softc_t *, ipnat_t *);
760 1.2 christos extern int ipf_nat6_finalise(fr_info_t *, nat_t *);
761 1.2 christos extern nat_t *ipf_nat6_icmperror(fr_info_t *, u_int *, int);
762 1.2 christos extern nat_t *ipf_nat6_icmperrorlookup(fr_info_t *, int);
763 1.2 christos extern nat_t *ipf_nat6_inlookup(fr_info_t *, u_int, u_int,
764 1.2 christos struct in6_addr *, struct in6_addr *);
765 1.2 christos extern u_32_t ipf_nat6_ip6subtract(i6addr_t *, i6addr_t *);
766 1.2 christos extern frentry_t *ipf_nat6_ipfin(fr_info_t *, u_32_t *);
767 1.2 christos extern frentry_t *ipf_nat6_ipfout(fr_info_t *, u_32_t *);
768 1.2 christos extern nat_t *ipf_nat6_lookupredir(natlookup_t *);
769 1.2 christos extern int ipf_nat6_newmap(fr_info_t *, nat_t *, natinfo_t *);
770 1.2 christos extern int ipf_nat6_newrdr(fr_info_t *, nat_t *, natinfo_t *);
771 1.2 christos extern nat_t *ipf_nat6_outlookup(fr_info_t *, u_int, u_int,
772 1.2 christos struct in6_addr *, struct in6_addr *);
773 1.2 christos extern int ipf_nat6_newrewrite(fr_info_t *, nat_t *, natinfo_t *);
774 1.2 christos extern int ipf_nat6_newdivert(fr_info_t *, nat_t *, natinfo_t *);
775 1.2 christos extern int ipf_nat6_ruleaddrinit(ipf_main_softc_t *, ipf_nat_softc_t *, ipnat_t *);
776 1.1 christos
777 1.1 christos #endif
778 1.1 christos
779 1.1 christos
780 1.1 christos #endif /* __IP_NAT_H__ */
781