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