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