17c478bd9Sstevel@tonic-gate /*
2ab25eeb5Syz  * Copyright (C) 1995-2001, 2003 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  * @(#)ip_nat.h	1.5 2/4/96
7ab25eeb5Syz  * $Id: ip_nat.h,v 2.90.2.11 2005/06/18 02:41:32 darrenr Exp $
83805c50fSan  *
9*d6c23f6fSyx  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
105b6dcef9Sdr  * Use is subject to license terms.
117c478bd9Sstevel@tonic-gate  */
125b6dcef9Sdr #pragma ident	"%Z%%M%	%I%	%E% SMI"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #ifndef	__IP_NAT_H__
157c478bd9Sstevel@tonic-gate #define	__IP_NAT_H__
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #ifndef SOLARIS
187c478bd9Sstevel@tonic-gate #define	SOLARIS	(defined(sun) && (defined(__svr4__) || defined(__SVR4)))
197c478bd9Sstevel@tonic-gate #endif
207c478bd9Sstevel@tonic-gate 
21ab25eeb5Syz #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
227c478bd9Sstevel@tonic-gate #define	SIOCADNAT	_IOW('r', 60, struct ipfobj)
237c478bd9Sstevel@tonic-gate #define	SIOCRMNAT	_IOW('r', 61, struct ipfobj)
247c478bd9Sstevel@tonic-gate #define	SIOCGNATS	_IOWR('r', 62, struct ipfobj)
257c478bd9Sstevel@tonic-gate #define	SIOCGNATL	_IOWR('r', 63, struct ipfobj)
267c478bd9Sstevel@tonic-gate #else
277c478bd9Sstevel@tonic-gate #define	SIOCADNAT	_IOW(r, 60, struct ipfobj)
287c478bd9Sstevel@tonic-gate #define	SIOCRMNAT	_IOW(r, 61, struct ipfobj)
297c478bd9Sstevel@tonic-gate #define	SIOCGNATS	_IOWR(r, 62, struct ipfobj)
307c478bd9Sstevel@tonic-gate #define	SIOCGNATL	_IOWR(r, 63, struct ipfobj)
317c478bd9Sstevel@tonic-gate #endif
327c478bd9Sstevel@tonic-gate 
33*d6c23f6fSyx #undef	LARGE_NAT	/* define this if you're setting up a system to NAT
347c478bd9Sstevel@tonic-gate 			 * LARGE numbers of networks/hosts - i.e. in the
357c478bd9Sstevel@tonic-gate 			 * hundreds or thousands.  In such a case, you should
367c478bd9Sstevel@tonic-gate 			 * also change the RDR_SIZE and NAT_SIZE below to more
377c478bd9Sstevel@tonic-gate 			 * appropriate sizes.  The figures below were used for
387c478bd9Sstevel@tonic-gate 			 * a setup with 1000-2000 networks to NAT.
397c478bd9Sstevel@tonic-gate 			 */
407c478bd9Sstevel@tonic-gate #ifndef NAT_SIZE
417c478bd9Sstevel@tonic-gate # ifdef LARGE_NAT
427c478bd9Sstevel@tonic-gate #  define	NAT_SIZE	2047
437c478bd9Sstevel@tonic-gate # else
447c478bd9Sstevel@tonic-gate #  define	NAT_SIZE	127
457c478bd9Sstevel@tonic-gate # endif
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate #ifndef RDR_SIZE
487c478bd9Sstevel@tonic-gate # ifdef LARGE_NAT
497c478bd9Sstevel@tonic-gate #  define	RDR_SIZE	2047
507c478bd9Sstevel@tonic-gate # else
517c478bd9Sstevel@tonic-gate #  define	RDR_SIZE	127
527c478bd9Sstevel@tonic-gate # endif
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate #ifndef HOSTMAP_SIZE
557c478bd9Sstevel@tonic-gate # ifdef LARGE_NAT
56ab25eeb5Syz #  define	HOSTMAP_SIZE	8191
57ab25eeb5Syz # else
587c478bd9Sstevel@tonic-gate #  define	HOSTMAP_SIZE	2047
59ab25eeb5Syz # endif
60ab25eeb5Syz #endif
61ab25eeb5Syz #ifndef NAT_TABLE_MAX
62ab25eeb5Syz /*
63ab25eeb5Syz  * This is newly introduced and for the sake of "least surprise", the numbers
64ab25eeb5Syz  * present aren't what we'd normally use for creating a proper hash table.
65ab25eeb5Syz  */
66ab25eeb5Syz # ifdef	LARGE_NAT
67ab25eeb5Syz #  define	NAT_TABLE_MAX	180000
687c478bd9Sstevel@tonic-gate # else
69ab25eeb5Syz #  define	NAT_TABLE_MAX	30000
707c478bd9Sstevel@tonic-gate # endif
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate #ifndef NAT_TABLE_SZ
737c478bd9Sstevel@tonic-gate # ifdef LARGE_NAT
747c478bd9Sstevel@tonic-gate #  define	NAT_TABLE_SZ	16383
75ab25eeb5Syz # else
76ab25eeb5Syz #  define	NAT_TABLE_SZ	2047
777c478bd9Sstevel@tonic-gate # endif
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate #ifndef	APR_LABELLEN
807c478bd9Sstevel@tonic-gate #define	APR_LABELLEN	16
817c478bd9Sstevel@tonic-gate #endif
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	DEF_NAT_AGE	1200     /* 10 minutes (600 seconds) */
847c478bd9Sstevel@tonic-gate 
853805c50fSan /*
863805c50fSan  * Default hi and lo watermarks used with forced flush of nat table.
873805c50fSan  */
883805c50fSan 
893805c50fSan #define	NAT_FLUSH_HI	95
903805c50fSan #define	NAT_FLUSH_LO	75
913805c50fSan 
923805c50fSan /* How full is the nat table? */
933805c50fSan 
943805c50fSan #define	NAT_TAB_WATER_LEVEL(x)	((x)->ifs_nat_stats.ns_inuse * 100 \
953805c50fSan 				/ (x)->ifs_ipf_nattable_max)
963805c50fSan 
977c478bd9Sstevel@tonic-gate struct ipstate;
987c478bd9Sstevel@tonic-gate struct ap_session;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate typedef	struct	nat	{
1017c478bd9Sstevel@tonic-gate 	ipfmutex_t	nat_lock;
1027c478bd9Sstevel@tonic-gate 	struct	nat	*nat_next;
1037c478bd9Sstevel@tonic-gate 	struct	nat	**nat_pnext;
1047c478bd9Sstevel@tonic-gate 	struct	nat	*nat_hnext[2];
1057c478bd9Sstevel@tonic-gate 	struct	nat	**nat_phnext[2];
1067c478bd9Sstevel@tonic-gate 	struct	hostmap	*nat_hm;
1077c478bd9Sstevel@tonic-gate 	void		*nat_data;
1087c478bd9Sstevel@tonic-gate 	struct	nat	**nat_me;
1097c478bd9Sstevel@tonic-gate 	struct	ipstate	*nat_state;
1107c478bd9Sstevel@tonic-gate 	struct	ap_session	*nat_aps;		/* proxy session */
1117c478bd9Sstevel@tonic-gate 	frentry_t	*nat_fr;	/* filter rule ptr if appropriate */
1127c478bd9Sstevel@tonic-gate 	struct	ipnat	*nat_ptr;	/* pointer back to the rule */
1137c478bd9Sstevel@tonic-gate 	void		*nat_ifps[2];
1147c478bd9Sstevel@tonic-gate 	void		*nat_sync;
1157c478bd9Sstevel@tonic-gate 	ipftqent_t	nat_tqe;
1167c478bd9Sstevel@tonic-gate 	u_32_t		nat_flags;
117*d6c23f6fSyx 	u_32_t		nat_sumd[2];	/* ip checksum delta for data segment */
1187c478bd9Sstevel@tonic-gate 	u_32_t		nat_ipsumd;	/* ip checksum delta for ip header */
1197c478bd9Sstevel@tonic-gate 	u_32_t		nat_mssclamp;	/* if != zero clamp MSS to this */
1207c478bd9Sstevel@tonic-gate 	i6addr_t	nat_inip6;
1217c478bd9Sstevel@tonic-gate 	i6addr_t	nat_outip6;
1227c478bd9Sstevel@tonic-gate 	i6addr_t	nat_oip6;		/* other ip */
1237c478bd9Sstevel@tonic-gate 	U_QUAD_T	nat_pkts[2];
1247c478bd9Sstevel@tonic-gate 	U_QUAD_T	nat_bytes[2];
1257c478bd9Sstevel@tonic-gate 	union	{
1267c478bd9Sstevel@tonic-gate 		udpinfo_t	nat_unu;
1277c478bd9Sstevel@tonic-gate 		tcpinfo_t	nat_unt;
1287c478bd9Sstevel@tonic-gate 		icmpinfo_t	nat_uni;
1297c478bd9Sstevel@tonic-gate 		greinfo_t	nat_ugre;
1307c478bd9Sstevel@tonic-gate 	} nat_un;
1317c478bd9Sstevel@tonic-gate 	u_short		nat_oport;		/* other port */
1327c478bd9Sstevel@tonic-gate 	u_short		nat_use;
1337c478bd9Sstevel@tonic-gate 	u_char		nat_p;			/* protocol for NAT */
1347c478bd9Sstevel@tonic-gate 	int		nat_dir;
1357c478bd9Sstevel@tonic-gate 	int		nat_ref;		/* reference count */
1367c478bd9Sstevel@tonic-gate 	int		nat_hv[2];
1377c478bd9Sstevel@tonic-gate 	char		nat_ifnames[2][LIFNAMSIZ];
138ab25eeb5Syz 	int		nat_rev;		/* 0 = forward, 1 = reverse */
139*d6c23f6fSyx 	int		nat_v;
140f4b3ec61Sdh 	int		nat_redir;		/* copy of in_redir */
1417c478bd9Sstevel@tonic-gate } nat_t;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #define	nat_inip	nat_inip6.in4
1447c478bd9Sstevel@tonic-gate #define	nat_outip	nat_outip6.in4
1457c478bd9Sstevel@tonic-gate #define	nat_oip		nat_oip6.in4
1467c478bd9Sstevel@tonic-gate #define	nat_age		nat_tqe.tqe_die
1477c478bd9Sstevel@tonic-gate #define	nat_inport	nat_un.nat_unt.ts_sport
1487c478bd9Sstevel@tonic-gate #define	nat_outport	nat_un.nat_unt.ts_dport
1497c478bd9Sstevel@tonic-gate #define	nat_type	nat_un.nat_uni.ici_type
1507c478bd9Sstevel@tonic-gate #define	nat_seq		nat_un.nat_uni.ici_seq
1517c478bd9Sstevel@tonic-gate #define	nat_id		nat_un.nat_uni.ici_id
1527c478bd9Sstevel@tonic-gate #define	nat_tcpstate	nat_tqe.tqe_state
1533805c50fSan #define	nat_touched	nat_tqe.tqe_touched
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Values for nat_dir
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate #define	NAT_INBOUND	0
1597c478bd9Sstevel@tonic-gate #define	NAT_OUTBOUND	1
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Definitions for nat_flags
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate #define	NAT_TCP		0x0001	/* IPN_TCP */
1657c478bd9Sstevel@tonic-gate #define	NAT_UDP		0x0002	/* IPN_UDP */
1667c478bd9Sstevel@tonic-gate #define	NAT_ICMPERR	0x0004	/* IPN_ICMPERR */
1677c478bd9Sstevel@tonic-gate #define	NAT_ICMPQUERY	0x0008	/* IPN_ICMPQUERY */
1687c478bd9Sstevel@tonic-gate #define	NAT_SEARCH	0x0010
1697c478bd9Sstevel@tonic-gate #define	NAT_SLAVE	0x0020	/* Slave connection for a proxy */
1707c478bd9Sstevel@tonic-gate #define	NAT_NOTRULEPORT	0x0040
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #define	NAT_TCPUDP	(NAT_TCP|NAT_UDP)
1737c478bd9Sstevel@tonic-gate #define	NAT_TCPUDPICMP	(NAT_TCP|NAT_UDP|NAT_ICMPERR)
174ab25eeb5Syz #define	NAT_TCPUDPICMPQ	(NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
1757c478bd9Sstevel@tonic-gate #define	NAT_FROMRULE	(NAT_TCP|NAT_UDP)
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /* 0x0100 reserved for FI_W_SPORT */
1787c478bd9Sstevel@tonic-gate /* 0x0200 reserved for FI_W_DPORT */
1797c478bd9Sstevel@tonic-gate /* 0x0400 reserved for FI_W_SADDR */
1807c478bd9Sstevel@tonic-gate /* 0x0800 reserved for FI_W_DADDR */
1817c478bd9Sstevel@tonic-gate /* 0x1000 reserved for FI_W_NEWFR */
1827c478bd9Sstevel@tonic-gate /* 0x2000 reserved for SI_CLONE */
1837c478bd9Sstevel@tonic-gate /* 0x4000 reserved for SI_CLONED */
1847c478bd9Sstevel@tonic-gate /* 0x8000 reserved for SI_IGNOREPKT */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #define	NAT_DEBUG	0x800000
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate typedef	struct	ipnat	{
1897c478bd9Sstevel@tonic-gate 	struct	ipnat	*in_next;		/* NAT rule list next */
1907c478bd9Sstevel@tonic-gate 	struct	ipnat	*in_rnext;		/* rdr rule hash next */
1917c478bd9Sstevel@tonic-gate 	struct	ipnat	**in_prnext;		/* prior rdr next ptr */
1927c478bd9Sstevel@tonic-gate 	struct	ipnat	*in_mnext;		/* map rule hash next */
1937c478bd9Sstevel@tonic-gate 	struct	ipnat	**in_pmnext;		/* prior map next ptr */
1947c478bd9Sstevel@tonic-gate 	struct	ipftq	*in_tqehead[2];
1957c478bd9Sstevel@tonic-gate 	void		*in_ifps[2];
1967c478bd9Sstevel@tonic-gate 	void		*in_apr;
1977c478bd9Sstevel@tonic-gate 	char		*in_comment;
1987c478bd9Sstevel@tonic-gate 	i6addr_t	in_next6;
1997c478bd9Sstevel@tonic-gate 	u_long		in_space;
2007c478bd9Sstevel@tonic-gate 	u_long		in_hits;
2017c478bd9Sstevel@tonic-gate 	u_int		in_use;
2027c478bd9Sstevel@tonic-gate 	u_int		in_hv;
2037c478bd9Sstevel@tonic-gate 	int		in_flineno;		/* conf. file line number */
2047c478bd9Sstevel@tonic-gate 	u_short		in_pnext;
205ab25eeb5Syz 	u_char		in_v;
206ab25eeb5Syz 	u_char		in_xxx;
2077c478bd9Sstevel@tonic-gate 	/* From here to the end is covered by IPN_CMPSIZ */
2087c478bd9Sstevel@tonic-gate 	u_32_t		in_flags;
2097c478bd9Sstevel@tonic-gate 	u_32_t		in_mssclamp;		/* if != 0 clamp MSS to this */
2107c478bd9Sstevel@tonic-gate 	u_int		in_age[2];
2117c478bd9Sstevel@tonic-gate 	int		in_redir;		/* see below for values */
2127c478bd9Sstevel@tonic-gate 	int		in_p;			/* protocol. */
2137c478bd9Sstevel@tonic-gate 	i6addr_t	in_in[2];
2147c478bd9Sstevel@tonic-gate 	i6addr_t	in_out[2];
2157c478bd9Sstevel@tonic-gate 	i6addr_t	in_src[2];
2167c478bd9Sstevel@tonic-gate 	frtuc_t		in_tuc;
2177c478bd9Sstevel@tonic-gate 	u_short		in_port[2];
2187c478bd9Sstevel@tonic-gate 	u_short		in_ppip;		/* ports per IP. */
2197c478bd9Sstevel@tonic-gate 	u_short		in_ippip;		/* IP #'s per IP# */
2207c478bd9Sstevel@tonic-gate 	char		in_ifnames[2][LIFNAMSIZ];
2217c478bd9Sstevel@tonic-gate 	char		in_plabel[APR_LABELLEN];	/* proxy label. */
2227c478bd9Sstevel@tonic-gate 	ipftag_t	in_tag;
2237c478bd9Sstevel@tonic-gate } ipnat_t;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #define	in_pmin		in_port[0]	/* Also holds static redir port */
2267c478bd9Sstevel@tonic-gate #define	in_pmax		in_port[1]
2277c478bd9Sstevel@tonic-gate #define	in_nextip	in_next6.in4
2287c478bd9Sstevel@tonic-gate #define	in_nip		in_next6.in4.s_addr
2297c478bd9Sstevel@tonic-gate #define	in_inip		in_in[0].in4.s_addr
2307c478bd9Sstevel@tonic-gate #define	in_inmsk	in_in[1].in4.s_addr
2317c478bd9Sstevel@tonic-gate #define	in_outip	in_out[0].in4.s_addr
2327c478bd9Sstevel@tonic-gate #define	in_outmsk	in_out[1].in4.s_addr
2337c478bd9Sstevel@tonic-gate #define	in_srcip	in_src[0].in4.s_addr
2347c478bd9Sstevel@tonic-gate #define	in_srcmsk	in_src[1].in4.s_addr
2357c478bd9Sstevel@tonic-gate #define	in_scmp		in_tuc.ftu_scmp
2367c478bd9Sstevel@tonic-gate #define	in_dcmp		in_tuc.ftu_dcmp
2377c478bd9Sstevel@tonic-gate #define	in_stop		in_tuc.ftu_stop
2387c478bd9Sstevel@tonic-gate #define	in_dtop		in_tuc.ftu_dtop
2397c478bd9Sstevel@tonic-gate #define	in_sport	in_tuc.ftu_sport
2407c478bd9Sstevel@tonic-gate #define	in_dport	in_tuc.ftu_dport
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Bit definitions for in_flags
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate #define	IPN_ANY		0x00000
2467c478bd9Sstevel@tonic-gate #define	IPN_TCP		0x00001
2477c478bd9Sstevel@tonic-gate #define	IPN_UDP		0x00002
2487c478bd9Sstevel@tonic-gate #define	IPN_TCPUDP	(IPN_TCP|IPN_UDP)
2497c478bd9Sstevel@tonic-gate #define	IPN_ICMPERR	0x00004
2507c478bd9Sstevel@tonic-gate #define	IPN_TCPUDPICMP	(IPN_TCP|IPN_UDP|IPN_ICMPERR)
2517c478bd9Sstevel@tonic-gate #define	IPN_ICMPQUERY	0x00008
252ab25eeb5Syz #define	IPN_TCPUDPICMPQ	(IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
2537c478bd9Sstevel@tonic-gate #define	IPN_RF		(IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
2547c478bd9Sstevel@tonic-gate #define	IPN_AUTOPORTMAP	0x00010
2557c478bd9Sstevel@tonic-gate #define	IPN_IPRANGE	0x00020
2567c478bd9Sstevel@tonic-gate #define	IPN_FILTER	0x00040
2577c478bd9Sstevel@tonic-gate #define	IPN_SPLIT	0x00080
2587c478bd9Sstevel@tonic-gate #define	IPN_ROUNDR	0x00100
2597c478bd9Sstevel@tonic-gate #define	IPN_NOTSRC	0x04000
2607c478bd9Sstevel@tonic-gate #define	IPN_NOTDST	0x08000
2617c478bd9Sstevel@tonic-gate #define	IPN_DYNSRCIP	0x10000	/* dynamic src IP# */
2627c478bd9Sstevel@tonic-gate #define	IPN_DYNDSTIP	0x20000	/* dynamic dst IP# */
2637c478bd9Sstevel@tonic-gate #define	IPN_DELETE	0x40000
2647c478bd9Sstevel@tonic-gate #define	IPN_STICKY	0x80000
2657c478bd9Sstevel@tonic-gate #define	IPN_FRAG	0x100000
266ab25eeb5Syz #define	IPN_FIXEDDPORT	0x200000
2675b6dcef9Sdr #define	IPN_FINDFORWARD	0x400000
268ab25eeb5Syz #define	IPN_IN		0x800000
2697c478bd9Sstevel@tonic-gate #define	IPN_USERFLAGS	(IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\
2707c478bd9Sstevel@tonic-gate 			 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\
271ab25eeb5Syz 			 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY)
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * Values for in_redir
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate #define	NAT_MAP		0x01
2777c478bd9Sstevel@tonic-gate #define	NAT_REDIRECT	0x02
2787c478bd9Sstevel@tonic-gate #define	NAT_BIMAP	(NAT_MAP|NAT_REDIRECT)
2797c478bd9Sstevel@tonic-gate #define	NAT_MAPBLK	0x04
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate #define	MAPBLK_MINPORT	1024	/* don't use reserved ports for src port */
2827c478bd9Sstevel@tonic-gate #define	USABLE_PORTS	(65536 - MAPBLK_MINPORT)
2837c478bd9Sstevel@tonic-gate 
284*d6c23f6fSyx #define	IPN_CMPSIZ	(sizeof (ipnat_t) - offsetof(ipnat_t, in_flags))
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate typedef	struct	natlookup {
287*d6c23f6fSyx 	i6addr_t	nl_inipaddr;
288*d6c23f6fSyx 	i6addr_t	nl_outipaddr;
289*d6c23f6fSyx 	i6addr_t	nl_realipaddr;
290*d6c23f6fSyx 	int		nl_v;
291*d6c23f6fSyx 	int		nl_flags;
292*d6c23f6fSyx 	u_short		nl_inport;
293*d6c23f6fSyx 	u_short		nl_outport;
294*d6c23f6fSyx 	u_short		nl_realport;
2957c478bd9Sstevel@tonic-gate } natlookup_t;
2967c478bd9Sstevel@tonic-gate 
297*d6c23f6fSyx #define	nl_inip		nl_inipaddr.in4
298*d6c23f6fSyx #define	nl_outip	nl_outipaddr.in4
299*d6c23f6fSyx #define	nl_realip	nl_realipaddr.in4
300*d6c23f6fSyx #define	nl_inip6	nl_inipaddr.in6
301*d6c23f6fSyx #define	nl_outip6	nl_outipaddr.in6
302*d6c23f6fSyx #define	nl_realip6	nl_realipaddr.in6
303*d6c23f6fSyx 
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate typedef struct  nat_save    {
3067c478bd9Sstevel@tonic-gate 	void	*ipn_next;
3077c478bd9Sstevel@tonic-gate 	struct	nat	ipn_nat;
3087c478bd9Sstevel@tonic-gate 	struct	ipnat	ipn_ipnat;
3097c478bd9Sstevel@tonic-gate 	struct	frentry ipn_fr;
3107c478bd9Sstevel@tonic-gate 	int	ipn_dsize;
3117c478bd9Sstevel@tonic-gate 	char	ipn_data[4];
3127c478bd9Sstevel@tonic-gate } nat_save_t;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate #define	ipn_rule	ipn_nat.nat_fr
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate typedef	struct	natget	{
3177c478bd9Sstevel@tonic-gate 	void	*ng_ptr;
3187c478bd9Sstevel@tonic-gate 	int	ng_sz;
3197c478bd9Sstevel@tonic-gate } natget_t;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
322ab25eeb5Syz #undef	tr_flags
3237c478bd9Sstevel@tonic-gate typedef	struct	nattrpnt	{
3247c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_dstip;	/* real destination IP# */
3257c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_srcip;	/* real source IP# */
3267c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_locip;	/* local source IP# */
3277c478bd9Sstevel@tonic-gate 	u_int	tr_flags;
3287c478bd9Sstevel@tonic-gate 	int	tr_expire;
3297c478bd9Sstevel@tonic-gate 	u_short	tr_dstport;	/* real destination port# */
3307c478bd9Sstevel@tonic-gate 	u_short	tr_srcport;	/* real source port# */
3317c478bd9Sstevel@tonic-gate 	u_short	tr_locport;	/* local source port# */
3327c478bd9Sstevel@tonic-gate 	struct	nattrpnt	*tr_hnext;
3337c478bd9Sstevel@tonic-gate 	struct	nattrpnt	**tr_phnext;
3347c478bd9Sstevel@tonic-gate 	struct	nattrpnt	*tr_next;
3357c478bd9Sstevel@tonic-gate 	struct	nattrpnt	**tr_pnext;	/* previous next */
3367c478bd9Sstevel@tonic-gate } nattrpnt_t;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate #define	TN_CMPSIZ	offsetof(nattrpnt_t, tr_hnext)
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * This structure gets used to help NAT sessions keep the same NAT rule (and
3437c478bd9Sstevel@tonic-gate  * thus translation for IP address) when:
3447c478bd9Sstevel@tonic-gate  * (a) round-robin redirects are in use
3457c478bd9Sstevel@tonic-gate  * (b) different IP add
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate typedef	struct	hostmap	{
3487c478bd9Sstevel@tonic-gate 	struct	hostmap	*hm_next;
3497c478bd9Sstevel@tonic-gate 	struct	hostmap	**hm_pnext;
350f4b3ec61Sdh 	struct	hostmap	*hm_hnext;
351f4b3ec61Sdh 	struct	hostmap	**hm_phnext;
3527c478bd9Sstevel@tonic-gate 	struct	ipnat	*hm_ipnat;
353*d6c23f6fSyx 	i6addr_t	hm_src;
354*d6c23f6fSyx 	i6addr_t	hm_dst;
355*d6c23f6fSyx 	i6addr_t	hm_map;
3567c478bd9Sstevel@tonic-gate 	u_32_t		hm_port;
3577c478bd9Sstevel@tonic-gate 	int		hm_ref;
358*d6c23f6fSyx 	int		hm_v;
3597c478bd9Sstevel@tonic-gate } hostmap_t;
3607c478bd9Sstevel@tonic-gate 
361*d6c23f6fSyx #define	hm_srcip	hm_src.in4
362*d6c23f6fSyx #define	hm_dstip	hm_dst.in4
363*d6c23f6fSyx #define	hm_mapip	hm_map.in4
364*d6c23f6fSyx #define	hm_srcip6	hm_src.in6
365*d6c23f6fSyx #define	hm_dstip6	hm_dst.in6
366*d6c23f6fSyx #define	hm_mapip6	hm_map.in6
367*d6c23f6fSyx 
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * Structure used to pass information in to nat_newmap and nat_newrdr.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate typedef struct	natinfo	{
3737c478bd9Sstevel@tonic-gate 	ipnat_t		*nai_np;
3747c478bd9Sstevel@tonic-gate 	u_32_t		nai_nflags;
3757c478bd9Sstevel@tonic-gate 	u_32_t		nai_flags;
3767c478bd9Sstevel@tonic-gate 	struct	in_addr	nai_ip;
3777c478bd9Sstevel@tonic-gate 	u_short		nai_port;
3787c478bd9Sstevel@tonic-gate 	u_short		nai_nport;
3797c478bd9Sstevel@tonic-gate 	u_short		nai_sport;
3807c478bd9Sstevel@tonic-gate 	u_short		nai_dport;
3817c478bd9Sstevel@tonic-gate } natinfo_t;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate typedef	struct	natstat	{
3857c478bd9Sstevel@tonic-gate 	u_long	ns_mapped[2];
3867c478bd9Sstevel@tonic-gate 	u_long	ns_rules;
3877c478bd9Sstevel@tonic-gate 	u_long	ns_added;
3887c478bd9Sstevel@tonic-gate 	u_long	ns_expire;
3897c478bd9Sstevel@tonic-gate 	u_long	ns_inuse;
3907c478bd9Sstevel@tonic-gate 	u_long	ns_logged;
3917c478bd9Sstevel@tonic-gate 	u_long	ns_logfail;
3927c478bd9Sstevel@tonic-gate 	u_long	ns_memfail;
3937c478bd9Sstevel@tonic-gate 	u_long	ns_badnat;
3947c478bd9Sstevel@tonic-gate 	u_long	ns_addtrpnt;
3957c478bd9Sstevel@tonic-gate 	nat_t	**ns_table[2];
3967c478bd9Sstevel@tonic-gate 	hostmap_t **ns_maptable;
3977c478bd9Sstevel@tonic-gate 	ipnat_t	*ns_list;
3987c478bd9Sstevel@tonic-gate 	void	*ns_apslist;
3997c478bd9Sstevel@tonic-gate 	u_int	ns_wilds;
4007c478bd9Sstevel@tonic-gate 	u_int	ns_nattab_sz;
401ab25eeb5Syz 	u_int	ns_nattab_max;
4027c478bd9Sstevel@tonic-gate 	u_int	ns_rultab_sz;
4037c478bd9Sstevel@tonic-gate 	u_int	ns_rdrtab_sz;
4047c478bd9Sstevel@tonic-gate 	u_int	ns_trpntab_sz;
4057c478bd9Sstevel@tonic-gate 	u_int	ns_hostmap_sz;
4067c478bd9Sstevel@tonic-gate 	nat_t	*ns_instances;
4077c478bd9Sstevel@tonic-gate 	nattrpnt_t *ns_trpntlist;
408f4b3ec61Sdh 	hostmap_t *ns_maplist;
4097c478bd9Sstevel@tonic-gate 	u_long	*ns_bucketlen[2];
4107c478bd9Sstevel@tonic-gate } natstat_t;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate typedef	struct	natlog {
413*d6c23f6fSyx 	i6addr_t	nlg_origip;
414*d6c23f6fSyx 	i6addr_t	nlg_outip;
415*d6c23f6fSyx 	i6addr_t	nlg_inip;
416*d6c23f6fSyx 	u_short		nlg_origport;
417*d6c23f6fSyx 	u_short		nlg_outport;
418*d6c23f6fSyx 	u_short		nlg_inport;
419*d6c23f6fSyx 	u_short		nlg_type;
420*d6c23f6fSyx 	int		nlg_rule;
421*d6c23f6fSyx 	U_QUAD_T	nlg_pkts[2];
422*d6c23f6fSyx 	U_QUAD_T	nlg_bytes[2];
423*d6c23f6fSyx 	u_char		nlg_p;
424*d6c23f6fSyx 	int		nlg_v;
4257c478bd9Sstevel@tonic-gate } natlog_t;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate #define	NL_NEWMAP	NAT_MAP
4297c478bd9Sstevel@tonic-gate #define	NL_NEWRDR	NAT_REDIRECT
4307c478bd9Sstevel@tonic-gate #define	NL_NEWBIMAP	NAT_BIMAP
4317c478bd9Sstevel@tonic-gate #define	NL_NEWBLOCK	NAT_MAPBLK
4327c478bd9Sstevel@tonic-gate #define	NL_CLONE	0xfffd
4337c478bd9Sstevel@tonic-gate #define	NL_FLUSH	0xfffe
4347c478bd9Sstevel@tonic-gate #define	NL_EXPIRE	0xffff
4357c478bd9Sstevel@tonic-gate 
436*d6c23f6fSyx #define	NAT_HASH_FN(k, l, m)	(((k) + ((k) >> 12) + l) % (m))
437*d6c23f6fSyx #define	NAT_HASH_FN6(k, l, m)	((((u_32_t *)(k))[3] \
438*d6c23f6fSyx 				+ (((u_32_t *)(k))[3] >> 12) \
439*d6c23f6fSyx 				+ (((u_32_t *)(k))[2]) \
440*d6c23f6fSyx 				+ (((u_32_t *)(k))[2] >> 12) \
441*d6c23f6fSyx 				+ (((u_32_t *)(k))[1]) \
442*d6c23f6fSyx 				+ (((u_32_t *)(k))[1] >> 12) \
443*d6c23f6fSyx 				+ (((u_32_t *)(k))[0]) \
444*d6c23f6fSyx 				+ (((u_32_t *)(k))[0] >> 12) \
445*d6c23f6fSyx 				+ l) % (m))
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate #define	LONG_SUM(in)	(((in) & 0xffff) + ((in) >> 16))
448*d6c23f6fSyx #define	LONG_SUM6(in)	(LONG_SUM(ntohl(((u_32_t *)(in))[0])) + \
449*d6c23f6fSyx 			 LONG_SUM(ntohl(((u_32_t *)(in))[1])) + \
450*d6c23f6fSyx 			 LONG_SUM(ntohl(((u_32_t *)(in))[2])) + \
451*d6c23f6fSyx 			 LONG_SUM(ntohl(((u_32_t *)(in))[3])))
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate #define	CALC_SUMD(s1, s2, sd) { \
4547c478bd9Sstevel@tonic-gate 			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
4557c478bd9Sstevel@tonic-gate 			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
4567c478bd9Sstevel@tonic-gate 			    /* Do it twice */ \
4577c478bd9Sstevel@tonic-gate 			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
4587c478bd9Sstevel@tonic-gate 			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
4597c478bd9Sstevel@tonic-gate 			    /* Because ~1 == -2, We really need ~1 == -1 */ \
4607c478bd9Sstevel@tonic-gate 			    if ((s1) > (s2)) (s2)--; \
4617c478bd9Sstevel@tonic-gate 			    (sd) = (s2) - (s1); \
4627c478bd9Sstevel@tonic-gate 			    (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate #define	NAT_SYSSPACE		0x80000000
4657c478bd9Sstevel@tonic-gate #define	NAT_LOCKHELD		0x40000000
4667c478bd9Sstevel@tonic-gate 
467f4b3ec61Sdh extern	void	fr_natsync __P((void *, ipf_stack_t *));
468*d6c23f6fSyx extern	void	fr_nataddrsync __P((int, void *, void *, ipf_stack_t *));
469*d6c23f6fSyx extern	void	fr_natifpsync __P((int, int, void *, char *, ipf_stack_t *));
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate #if defined(__OpenBSD__)
472f4b3ec61Sdh extern	void	nat_ifdetach __P((void *, ipf_stack_t *));
4737c478bd9Sstevel@tonic-gate #endif
474f4b3ec61Sdh extern	int	fr_nat_ioctl __P((caddr_t, ioctlcmd_t, int, int, void *, ipf_stack_t *));
475f4b3ec61Sdh extern	int	fr_natinit __P((ipf_stack_t *));
4767c478bd9Sstevel@tonic-gate extern	nat_t	*nat_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int));
4777c478bd9Sstevel@tonic-gate extern	nat_t	*nat_outlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
4787c478bd9Sstevel@tonic-gate 				 struct in_addr));
4797c478bd9Sstevel@tonic-gate extern	void	fix_datacksum __P((u_short *, u_32_t));
4807c478bd9Sstevel@tonic-gate extern	nat_t	*nat_inlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
4817c478bd9Sstevel@tonic-gate 				struct in_addr));
4827c478bd9Sstevel@tonic-gate extern	nat_t	*nat_tnlookup __P((fr_info_t *, int));
4837c478bd9Sstevel@tonic-gate extern	nat_t	*nat_maplookup __P((void *, u_int, struct in_addr,
4847c478bd9Sstevel@tonic-gate 				struct in_addr));
485f4b3ec61Sdh extern	nat_t	*nat_lookupredir __P((natlookup_t *, ipf_stack_t *));
4867c478bd9Sstevel@tonic-gate extern	nat_t	*nat_icmperrorlookup __P((fr_info_t *, int));
4877c478bd9Sstevel@tonic-gate extern	nat_t	*nat_icmperror __P((fr_info_t *, u_int *, int));
488f4b3ec61Sdh extern	int	nat_insert __P((nat_t *, int, ipf_stack_t *));
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate extern	int	fr_checknatout __P((fr_info_t *, u_32_t *));
4917c478bd9Sstevel@tonic-gate extern	int	fr_natout __P((fr_info_t *, nat_t *, int, u_32_t));
4927c478bd9Sstevel@tonic-gate extern	int	fr_checknatin __P((fr_info_t *, u_32_t *));
4937c478bd9Sstevel@tonic-gate extern	int	fr_natin __P((fr_info_t *, nat_t *, int, u_32_t));
494f4b3ec61Sdh extern	void	fr_natunload __P((ipf_stack_t *));
495f4b3ec61Sdh extern	void	fr_natexpire __P((ipf_stack_t *));
496f4b3ec61Sdh extern	void	nat_log __P((struct nat *, u_int, ipf_stack_t *));
497381a2a9aSdr extern	void	fix_incksum __P((u_short *, u_32_t));
498381a2a9aSdr extern	void	fix_outcksum __P((u_short *, u_32_t));
499f4b3ec61Sdh extern  void    fr_ipnatderef __P((ipnat_t **, ipf_stack_t *));
500f4b3ec61Sdh extern	void	fr_natderef __P((nat_t **, ipf_stack_t *));
5017c478bd9Sstevel@tonic-gate extern	u_short	*nat_proto __P((fr_info_t *, nat_t *, u_int));
5027c478bd9Sstevel@tonic-gate extern	void	nat_update __P((fr_info_t *, nat_t *, ipnat_t *));
503f4b3ec61Sdh extern	void	fr_setnatqueue __P((nat_t *, int, ipf_stack_t *));
50490b0a856Sjojemann extern  void    fr_hostmapdel __P((hostmap_t **));
5057c478bd9Sstevel@tonic-gate 
506*d6c23f6fSyx extern	nat_t	*fr_natclone __P((fr_info_t *, nat_t *));
507*d6c23f6fSyx extern	void	nat_delrdr __P((struct ipnat *));
508*d6c23f6fSyx extern	void	nat_delnat __P((struct ipnat *));
509*d6c23f6fSyx extern	int	nat_wildok __P((nat_t *, int, int, int, int));
510*d6c23f6fSyx extern	void	nat_calc_chksum_diffs __P((nat_t *));
511*d6c23f6fSyx 
512*d6c23f6fSyx #ifdef	USE_INET6
513*d6c23f6fSyx extern	void	nat6_addnat __P((ipnat_t *, ipf_stack_t *));
514*d6c23f6fSyx extern	void	nat6_addrdr __P((ipnat_t *, ipf_stack_t *));
515*d6c23f6fSyx extern	nat_t	*nat6_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int));
516*d6c23f6fSyx extern	nat_t	*nat6_outlookup __P((fr_info_t *, u_int, u_int,
517*d6c23f6fSyx 				struct in6_addr *, struct in6_addr *));
518*d6c23f6fSyx extern	nat_t	*nat6_inlookup __P((fr_info_t *, u_int, u_int,
519*d6c23f6fSyx 				struct in6_addr *, struct in6_addr *));
520*d6c23f6fSyx extern	nat_t	*nat6_lookupredir __P((natlookup_t *, ipf_stack_t *));
521*d6c23f6fSyx extern	nat_t	*nat6_icmperrorlookup __P((fr_info_t *, int));
522*d6c23f6fSyx extern	nat_t	*nat6_icmperror __P((fr_info_t *, u_int *, int));
523*d6c23f6fSyx extern	int	nat6_insert __P((nat_t *, int, ipf_stack_t *));
524*d6c23f6fSyx extern	int	fr_checknat6out __P((fr_info_t *, u_32_t *));
525*d6c23f6fSyx extern	int	fr_nat6out __P((fr_info_t *, nat_t *, int, u_32_t));
526*d6c23f6fSyx extern	int	fr_checknat6in __P((fr_info_t *, u_32_t *));
527*d6c23f6fSyx extern	int	fr_nat6in __P((fr_info_t *, nat_t *, int, u_32_t));
528*d6c23f6fSyx #endif
529*d6c23f6fSyx 
5307c478bd9Sstevel@tonic-gate #endif /* __IP_NAT_H__ */
531