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