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