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 $
8ab25eeb5Syz *
9ab25eeb5Syz  * Copyright 2006 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 
337c478bd9Sstevel@tonic-gate #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 
857c478bd9Sstevel@tonic-gate struct ipstate;
867c478bd9Sstevel@tonic-gate struct ap_session;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate typedef	struct	nat	{
897c478bd9Sstevel@tonic-gate 	ipfmutex_t	nat_lock;
907c478bd9Sstevel@tonic-gate 	struct	nat	*nat_next;
917c478bd9Sstevel@tonic-gate 	struct	nat	**nat_pnext;
927c478bd9Sstevel@tonic-gate 	struct	nat	*nat_hnext[2];
937c478bd9Sstevel@tonic-gate 	struct	nat	**nat_phnext[2];
947c478bd9Sstevel@tonic-gate 	struct	hostmap	*nat_hm;
957c478bd9Sstevel@tonic-gate 	void		*nat_data;
967c478bd9Sstevel@tonic-gate 	struct	nat	**nat_me;
977c478bd9Sstevel@tonic-gate 	struct	ipstate	*nat_state;
987c478bd9Sstevel@tonic-gate 	struct	ap_session	*nat_aps;		/* proxy session */
997c478bd9Sstevel@tonic-gate 	frentry_t	*nat_fr;	/* filter rule ptr if appropriate */
1007c478bd9Sstevel@tonic-gate 	struct	ipnat	*nat_ptr;	/* pointer back to the rule */
1017c478bd9Sstevel@tonic-gate 	void		*nat_ifps[2];
1027c478bd9Sstevel@tonic-gate 	void		*nat_sync;
1037c478bd9Sstevel@tonic-gate 	ipftqent_t	nat_tqe;
1047c478bd9Sstevel@tonic-gate 	u_32_t		nat_flags;
1057c478bd9Sstevel@tonic-gate 	u_32_t		nat_sumd[2];	/* ip checksum delta for data segment*/
1067c478bd9Sstevel@tonic-gate 	u_32_t		nat_ipsumd;	/* ip checksum delta for ip header */
1077c478bd9Sstevel@tonic-gate 	u_32_t		nat_mssclamp;	/* if != zero clamp MSS to this */
1087c478bd9Sstevel@tonic-gate 	i6addr_t	nat_inip6;
1097c478bd9Sstevel@tonic-gate 	i6addr_t	nat_outip6;
1107c478bd9Sstevel@tonic-gate 	i6addr_t	nat_oip6;		/* other ip */
1117c478bd9Sstevel@tonic-gate 	U_QUAD_T	nat_pkts[2];
1127c478bd9Sstevel@tonic-gate 	U_QUAD_T	nat_bytes[2];
1137c478bd9Sstevel@tonic-gate 	union	{
1147c478bd9Sstevel@tonic-gate 		udpinfo_t	nat_unu;
1157c478bd9Sstevel@tonic-gate 		tcpinfo_t	nat_unt;
1167c478bd9Sstevel@tonic-gate 		icmpinfo_t	nat_uni;
1177c478bd9Sstevel@tonic-gate 		greinfo_t	nat_ugre;
1187c478bd9Sstevel@tonic-gate 	} nat_un;
1197c478bd9Sstevel@tonic-gate 	u_short		nat_oport;		/* other port */
1207c478bd9Sstevel@tonic-gate 	u_short		nat_use;
1217c478bd9Sstevel@tonic-gate 	u_char		nat_p;			/* protocol for NAT */
1227c478bd9Sstevel@tonic-gate 	int		nat_dir;
1237c478bd9Sstevel@tonic-gate 	int		nat_ref;		/* reference count */
1247c478bd9Sstevel@tonic-gate 	int		nat_hv[2];
1257c478bd9Sstevel@tonic-gate 	char		nat_ifnames[2][LIFNAMSIZ];
126ab25eeb5Syz 	int		nat_rev;		/* 0 = forward, 1 = reverse */
1277c478bd9Sstevel@tonic-gate } nat_t;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	nat_inip	nat_inip6.in4
1307c478bd9Sstevel@tonic-gate #define	nat_outip	nat_outip6.in4
1317c478bd9Sstevel@tonic-gate #define	nat_oip		nat_oip6.in4
1327c478bd9Sstevel@tonic-gate #define	nat_age		nat_tqe.tqe_die
1337c478bd9Sstevel@tonic-gate #define	nat_inport	nat_un.nat_unt.ts_sport
1347c478bd9Sstevel@tonic-gate #define	nat_outport	nat_un.nat_unt.ts_dport
1357c478bd9Sstevel@tonic-gate #define	nat_type	nat_un.nat_uni.ici_type
1367c478bd9Sstevel@tonic-gate #define	nat_seq		nat_un.nat_uni.ici_seq
1377c478bd9Sstevel@tonic-gate #define	nat_id		nat_un.nat_uni.ici_id
1387c478bd9Sstevel@tonic-gate #define	nat_tcpstate	nat_tqe.tqe_state
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * Values for nat_dir
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate #define	NAT_INBOUND	0
1447c478bd9Sstevel@tonic-gate #define	NAT_OUTBOUND	1
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Definitions for nat_flags
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate #define	NAT_TCP		0x0001	/* IPN_TCP */
1507c478bd9Sstevel@tonic-gate #define	NAT_UDP		0x0002	/* IPN_UDP */
1517c478bd9Sstevel@tonic-gate #define	NAT_ICMPERR	0x0004	/* IPN_ICMPERR */
1527c478bd9Sstevel@tonic-gate #define	NAT_ICMPQUERY	0x0008	/* IPN_ICMPQUERY */
1537c478bd9Sstevel@tonic-gate #define	NAT_SEARCH	0x0010
1547c478bd9Sstevel@tonic-gate #define	NAT_SLAVE	0x0020	/* Slave connection for a proxy */
1557c478bd9Sstevel@tonic-gate #define	NAT_NOTRULEPORT	0x0040
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #define	NAT_TCPUDP	(NAT_TCP|NAT_UDP)
1587c478bd9Sstevel@tonic-gate #define	NAT_TCPUDPICMP	(NAT_TCP|NAT_UDP|NAT_ICMPERR)
159ab25eeb5Syz #define	NAT_TCPUDPICMPQ	(NAT_TCP|NAT_UDP|NAT_ICMPQUERY)
1607c478bd9Sstevel@tonic-gate #define	NAT_FROMRULE	(NAT_TCP|NAT_UDP)
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /* 0x0100 reserved for FI_W_SPORT */
1637c478bd9Sstevel@tonic-gate /* 0x0200 reserved for FI_W_DPORT */
1647c478bd9Sstevel@tonic-gate /* 0x0400 reserved for FI_W_SADDR */
1657c478bd9Sstevel@tonic-gate /* 0x0800 reserved for FI_W_DADDR */
1667c478bd9Sstevel@tonic-gate /* 0x1000 reserved for FI_W_NEWFR */
1677c478bd9Sstevel@tonic-gate /* 0x2000 reserved for SI_CLONE */
1687c478bd9Sstevel@tonic-gate /* 0x4000 reserved for SI_CLONED */
1697c478bd9Sstevel@tonic-gate /* 0x8000 reserved for SI_IGNOREPKT */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #define	NAT_DEBUG	0x800000
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate typedef	struct	ipnat	{
1747c478bd9Sstevel@tonic-gate 	struct	ipnat	*in_next;		/* NAT rule list next */
1757c478bd9Sstevel@tonic-gate 	struct	ipnat	*in_rnext;		/* rdr rule hash next */
1767c478bd9Sstevel@tonic-gate 	struct	ipnat	**in_prnext;		/* prior rdr next ptr */
1777c478bd9Sstevel@tonic-gate 	struct	ipnat	*in_mnext;		/* map rule hash next */
1787c478bd9Sstevel@tonic-gate 	struct	ipnat	**in_pmnext;		/* prior map next ptr */
1797c478bd9Sstevel@tonic-gate 	struct	ipftq	*in_tqehead[2];
1807c478bd9Sstevel@tonic-gate 	void		*in_ifps[2];
1817c478bd9Sstevel@tonic-gate 	void		*in_apr;
1827c478bd9Sstevel@tonic-gate 	char		*in_comment;
1837c478bd9Sstevel@tonic-gate 	i6addr_t	in_next6;
1847c478bd9Sstevel@tonic-gate 	u_long		in_space;
1857c478bd9Sstevel@tonic-gate 	u_long		in_hits;
1867c478bd9Sstevel@tonic-gate 	u_int		in_use;
1877c478bd9Sstevel@tonic-gate 	u_int		in_hv;
1887c478bd9Sstevel@tonic-gate 	int		in_flineno;		/* conf. file line number */
1897c478bd9Sstevel@tonic-gate 	u_short		in_pnext;
190ab25eeb5Syz 	u_char		in_v;
191ab25eeb5Syz 	u_char		in_xxx;
1927c478bd9Sstevel@tonic-gate 	/* From here to the end is covered by IPN_CMPSIZ */
1937c478bd9Sstevel@tonic-gate 	u_32_t		in_flags;
1947c478bd9Sstevel@tonic-gate 	u_32_t		in_mssclamp;		/* if != 0 clamp MSS to this */
1957c478bd9Sstevel@tonic-gate 	u_int		in_age[2];
1967c478bd9Sstevel@tonic-gate 	int		in_redir;		/* see below for values */
1977c478bd9Sstevel@tonic-gate 	int		in_p;			/* protocol. */
1987c478bd9Sstevel@tonic-gate 	i6addr_t	in_in[2];
1997c478bd9Sstevel@tonic-gate 	i6addr_t	in_out[2];
2007c478bd9Sstevel@tonic-gate 	i6addr_t	in_src[2];
2017c478bd9Sstevel@tonic-gate 	frtuc_t		in_tuc;
2027c478bd9Sstevel@tonic-gate 	u_short		in_port[2];
2037c478bd9Sstevel@tonic-gate 	u_short		in_ppip;		/* ports per IP. */
2047c478bd9Sstevel@tonic-gate 	u_short		in_ippip;		/* IP #'s per IP# */
2057c478bd9Sstevel@tonic-gate 	char		in_ifnames[2][LIFNAMSIZ];
2067c478bd9Sstevel@tonic-gate 	char		in_plabel[APR_LABELLEN];	/* proxy label. */
2077c478bd9Sstevel@tonic-gate 	ipftag_t	in_tag;
2087c478bd9Sstevel@tonic-gate } ipnat_t;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate #define	in_pmin		in_port[0]	/* Also holds static redir port */
2117c478bd9Sstevel@tonic-gate #define	in_pmax		in_port[1]
2127c478bd9Sstevel@tonic-gate #define	in_nextip	in_next6.in4
2137c478bd9Sstevel@tonic-gate #define	in_nip		in_next6.in4.s_addr
2147c478bd9Sstevel@tonic-gate #define	in_inip		in_in[0].in4.s_addr
2157c478bd9Sstevel@tonic-gate #define	in_inmsk	in_in[1].in4.s_addr
2167c478bd9Sstevel@tonic-gate #define	in_outip	in_out[0].in4.s_addr
2177c478bd9Sstevel@tonic-gate #define	in_outmsk	in_out[1].in4.s_addr
2187c478bd9Sstevel@tonic-gate #define	in_srcip	in_src[0].in4.s_addr
2197c478bd9Sstevel@tonic-gate #define	in_srcmsk	in_src[1].in4.s_addr
2207c478bd9Sstevel@tonic-gate #define	in_scmp		in_tuc.ftu_scmp
2217c478bd9Sstevel@tonic-gate #define	in_dcmp		in_tuc.ftu_dcmp
2227c478bd9Sstevel@tonic-gate #define	in_stop		in_tuc.ftu_stop
2237c478bd9Sstevel@tonic-gate #define	in_dtop		in_tuc.ftu_dtop
2247c478bd9Sstevel@tonic-gate #define	in_sport	in_tuc.ftu_sport
2257c478bd9Sstevel@tonic-gate #define	in_dport	in_tuc.ftu_dport
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * Bit definitions for in_flags
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate #define	IPN_ANY		0x00000
2317c478bd9Sstevel@tonic-gate #define	IPN_TCP		0x00001
2327c478bd9Sstevel@tonic-gate #define	IPN_UDP		0x00002
2337c478bd9Sstevel@tonic-gate #define	IPN_TCPUDP	(IPN_TCP|IPN_UDP)
2347c478bd9Sstevel@tonic-gate #define	IPN_ICMPERR	0x00004
2357c478bd9Sstevel@tonic-gate #define	IPN_TCPUDPICMP	(IPN_TCP|IPN_UDP|IPN_ICMPERR)
2367c478bd9Sstevel@tonic-gate #define	IPN_ICMPQUERY	0x00008
237ab25eeb5Syz #define	IPN_TCPUDPICMPQ	(IPN_TCP|IPN_UDP|IPN_ICMPQUERY)
2387c478bd9Sstevel@tonic-gate #define	IPN_RF		(IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR)
2397c478bd9Sstevel@tonic-gate #define	IPN_AUTOPORTMAP	0x00010
2407c478bd9Sstevel@tonic-gate #define	IPN_IPRANGE	0x00020
2417c478bd9Sstevel@tonic-gate #define	IPN_FILTER	0x00040
2427c478bd9Sstevel@tonic-gate #define	IPN_SPLIT	0x00080
2437c478bd9Sstevel@tonic-gate #define	IPN_ROUNDR	0x00100
2447c478bd9Sstevel@tonic-gate #define	IPN_NOTSRC	0x04000
2457c478bd9Sstevel@tonic-gate #define	IPN_NOTDST	0x08000
2467c478bd9Sstevel@tonic-gate #define	IPN_DYNSRCIP	0x10000	/* dynamic src IP# */
2477c478bd9Sstevel@tonic-gate #define	IPN_DYNDSTIP	0x20000	/* dynamic dst IP# */
2487c478bd9Sstevel@tonic-gate #define	IPN_DELETE	0x40000
2497c478bd9Sstevel@tonic-gate #define	IPN_STICKY	0x80000
2507c478bd9Sstevel@tonic-gate #define	IPN_FRAG	0x100000
251ab25eeb5Syz #define	IPN_FIXEDDPORT	0x200000
2525b6dcef9Sdr #define	IPN_FINDFORWARD	0x400000
253ab25eeb5Syz #define	IPN_IN		0x800000
2547c478bd9Sstevel@tonic-gate #define	IPN_USERFLAGS	(IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\
2557c478bd9Sstevel@tonic-gate 			 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\
256ab25eeb5Syz 			 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY)
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Values for in_redir
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate #define	NAT_MAP		0x01
2627c478bd9Sstevel@tonic-gate #define	NAT_REDIRECT	0x02
2637c478bd9Sstevel@tonic-gate #define	NAT_BIMAP	(NAT_MAP|NAT_REDIRECT)
2647c478bd9Sstevel@tonic-gate #define	NAT_MAPBLK	0x04
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate #define	MAPBLK_MINPORT	1024	/* don't use reserved ports for src port */
2677c478bd9Sstevel@tonic-gate #define	USABLE_PORTS	(65536 - MAPBLK_MINPORT)
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate #define	IPN_CMPSIZ	(sizeof(ipnat_t) - offsetof(ipnat_t, in_flags))
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate typedef	struct	natlookup {
2727c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_inip;
2737c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_outip;
2747c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_realip;
2757c478bd9Sstevel@tonic-gate 	int	nl_flags;
2767c478bd9Sstevel@tonic-gate 	u_short	nl_inport;
2777c478bd9Sstevel@tonic-gate 	u_short	nl_outport;
2787c478bd9Sstevel@tonic-gate 	u_short	nl_realport;
2797c478bd9Sstevel@tonic-gate } natlookup_t;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate typedef struct  nat_save    {
2837c478bd9Sstevel@tonic-gate 	void	*ipn_next;
2847c478bd9Sstevel@tonic-gate 	struct	nat	ipn_nat;
2857c478bd9Sstevel@tonic-gate 	struct	ipnat	ipn_ipnat;
2867c478bd9Sstevel@tonic-gate 	struct	frentry ipn_fr;
2877c478bd9Sstevel@tonic-gate 	int	ipn_dsize;
2887c478bd9Sstevel@tonic-gate 	char	ipn_data[4];
2897c478bd9Sstevel@tonic-gate } nat_save_t;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate #define	ipn_rule	ipn_nat.nat_fr
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate typedef	struct	natget	{
2947c478bd9Sstevel@tonic-gate 	void	*ng_ptr;
2957c478bd9Sstevel@tonic-gate 	int	ng_sz;
2967c478bd9Sstevel@tonic-gate } natget_t;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 
299ab25eeb5Syz #undef	tr_flags
3007c478bd9Sstevel@tonic-gate typedef	struct	nattrpnt	{
3017c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_dstip;	/* real destination IP# */
3027c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_srcip;	/* real source IP# */
3037c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_locip;	/* local source IP# */
3047c478bd9Sstevel@tonic-gate 	u_int	tr_flags;
3057c478bd9Sstevel@tonic-gate 	int	tr_expire;
3067c478bd9Sstevel@tonic-gate 	u_short	tr_dstport;	/* real destination port# */
3077c478bd9Sstevel@tonic-gate 	u_short	tr_srcport;	/* real source port# */
3087c478bd9Sstevel@tonic-gate 	u_short	tr_locport;	/* local source port# */
3097c478bd9Sstevel@tonic-gate 	struct	nattrpnt	*tr_hnext;
3107c478bd9Sstevel@tonic-gate 	struct	nattrpnt	**tr_phnext;
3117c478bd9Sstevel@tonic-gate 	struct	nattrpnt	*tr_next;
3127c478bd9Sstevel@tonic-gate 	struct	nattrpnt	**tr_pnext;	/* previous next */
3137c478bd9Sstevel@tonic-gate } nattrpnt_t;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate #define	TN_CMPSIZ	offsetof(nattrpnt_t, tr_hnext)
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * This structure gets used to help NAT sessions keep the same NAT rule (and
3207c478bd9Sstevel@tonic-gate  * thus translation for IP address) when:
3217c478bd9Sstevel@tonic-gate  * (a) round-robin redirects are in use
3227c478bd9Sstevel@tonic-gate  * (b) different IP add
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate typedef	struct	hostmap	{
3257c478bd9Sstevel@tonic-gate 	struct	hostmap	*hm_next;
3267c478bd9Sstevel@tonic-gate 	struct	hostmap	**hm_pnext;
3277c478bd9Sstevel@tonic-gate 	struct	ipnat	*hm_ipnat;
3287c478bd9Sstevel@tonic-gate 	struct	in_addr	hm_srcip;
3297c478bd9Sstevel@tonic-gate 	struct	in_addr	hm_dstip;
3307c478bd9Sstevel@tonic-gate 	struct	in_addr	hm_mapip;
3317c478bd9Sstevel@tonic-gate 	u_32_t		hm_port;
3327c478bd9Sstevel@tonic-gate 	int		hm_ref;
3337c478bd9Sstevel@tonic-gate } hostmap_t;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate  * Structure used to pass information in to nat_newmap and nat_newrdr.
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate typedef struct	natinfo	{
3407c478bd9Sstevel@tonic-gate 	ipnat_t		*nai_np;
3417c478bd9Sstevel@tonic-gate 	u_32_t		nai_sum1;
3427c478bd9Sstevel@tonic-gate 	u_32_t		nai_sum2;
3437c478bd9Sstevel@tonic-gate 	u_32_t		nai_nflags;
3447c478bd9Sstevel@tonic-gate 	u_32_t		nai_flags;
3457c478bd9Sstevel@tonic-gate 	struct	in_addr	nai_ip;
3467c478bd9Sstevel@tonic-gate 	u_short		nai_port;
3477c478bd9Sstevel@tonic-gate 	u_short		nai_nport;
3487c478bd9Sstevel@tonic-gate 	u_short		nai_sport;
3497c478bd9Sstevel@tonic-gate 	u_short		nai_dport;
3507c478bd9Sstevel@tonic-gate } natinfo_t;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate typedef	struct	natstat	{
3547c478bd9Sstevel@tonic-gate 	u_long	ns_mapped[2];
3557c478bd9Sstevel@tonic-gate 	u_long	ns_rules;
3567c478bd9Sstevel@tonic-gate 	u_long	ns_added;
3577c478bd9Sstevel@tonic-gate 	u_long	ns_expire;
3587c478bd9Sstevel@tonic-gate 	u_long	ns_inuse;
3597c478bd9Sstevel@tonic-gate 	u_long	ns_logged;
3607c478bd9Sstevel@tonic-gate 	u_long	ns_logfail;
3617c478bd9Sstevel@tonic-gate 	u_long	ns_memfail;
3627c478bd9Sstevel@tonic-gate 	u_long	ns_badnat;
3637c478bd9Sstevel@tonic-gate 	u_long	ns_addtrpnt;
3647c478bd9Sstevel@tonic-gate 	nat_t	**ns_table[2];
3657c478bd9Sstevel@tonic-gate 	hostmap_t **ns_maptable;
3667c478bd9Sstevel@tonic-gate 	ipnat_t	*ns_list;
3677c478bd9Sstevel@tonic-gate 	void	*ns_apslist;
3687c478bd9Sstevel@tonic-gate 	u_int	ns_wilds;
3697c478bd9Sstevel@tonic-gate 	u_int	ns_nattab_sz;
370ab25eeb5Syz 	u_int	ns_nattab_max;
3717c478bd9Sstevel@tonic-gate 	u_int	ns_rultab_sz;
3727c478bd9Sstevel@tonic-gate 	u_int	ns_rdrtab_sz;
3737c478bd9Sstevel@tonic-gate 	u_int	ns_trpntab_sz;
3747c478bd9Sstevel@tonic-gate 	u_int	ns_hostmap_sz;
3757c478bd9Sstevel@tonic-gate 	nat_t	*ns_instances;
3767c478bd9Sstevel@tonic-gate 	nattrpnt_t *ns_trpntlist;
3777c478bd9Sstevel@tonic-gate 	u_long	*ns_bucketlen[2];
3787c478bd9Sstevel@tonic-gate } natstat_t;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate typedef	struct	natlog {
3817c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_origip;
3827c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_outip;
3837c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_inip;
3847c478bd9Sstevel@tonic-gate 	u_short	nl_origport;
3857c478bd9Sstevel@tonic-gate 	u_short	nl_outport;
3867c478bd9Sstevel@tonic-gate 	u_short	nl_inport;
3877c478bd9Sstevel@tonic-gate 	u_short	nl_type;
3887c478bd9Sstevel@tonic-gate 	int	nl_rule;
3897c478bd9Sstevel@tonic-gate 	U_QUAD_T	nl_pkts[2];
3907c478bd9Sstevel@tonic-gate 	U_QUAD_T	nl_bytes[2];
3917c478bd9Sstevel@tonic-gate 	u_char	nl_p;
3927c478bd9Sstevel@tonic-gate } natlog_t;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate #define	NL_NEWMAP	NAT_MAP
3967c478bd9Sstevel@tonic-gate #define	NL_NEWRDR	NAT_REDIRECT
3977c478bd9Sstevel@tonic-gate #define	NL_NEWBIMAP	NAT_BIMAP
3987c478bd9Sstevel@tonic-gate #define	NL_NEWBLOCK	NAT_MAPBLK
3997c478bd9Sstevel@tonic-gate #define	NL_CLONE	0xfffd
4007c478bd9Sstevel@tonic-gate #define	NL_FLUSH	0xfffe
4017c478bd9Sstevel@tonic-gate #define	NL_EXPIRE	0xffff
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate #define	NAT_HASH_FN(k,l,m)	(((k) + ((k) >> 12) + l) % (m))
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate #define	LONG_SUM(in)	(((in) & 0xffff) + ((in) >> 16))
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate #define	CALC_SUMD(s1, s2, sd) { \
4087c478bd9Sstevel@tonic-gate 			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
4097c478bd9Sstevel@tonic-gate 			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
4107c478bd9Sstevel@tonic-gate 			    /* Do it twice */ \
4117c478bd9Sstevel@tonic-gate 			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
4127c478bd9Sstevel@tonic-gate 			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
4137c478bd9Sstevel@tonic-gate 			    /* Because ~1 == -2, We really need ~1 == -1 */ \
4147c478bd9Sstevel@tonic-gate 			    if ((s1) > (s2)) (s2)--; \
4157c478bd9Sstevel@tonic-gate 			    (sd) = (s2) - (s1); \
4167c478bd9Sstevel@tonic-gate 			    (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate #define	NAT_SYSSPACE		0x80000000
4197c478bd9Sstevel@tonic-gate #define	NAT_LOCKHELD		0x40000000
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate extern	u_int	ipf_nattable_sz;
423ab25eeb5Syz extern	u_int	ipf_nattable_max;
4247c478bd9Sstevel@tonic-gate extern	u_int	ipf_natrules_sz;
4257c478bd9Sstevel@tonic-gate extern	u_int	ipf_rdrrules_sz;
4267c478bd9Sstevel@tonic-gate extern	u_int	ipf_hostmap_sz;
4277c478bd9Sstevel@tonic-gate extern	u_int	fr_nat_maxbucket;
4287c478bd9Sstevel@tonic-gate extern	u_int	fr_nat_maxbucket_reset;
4297c478bd9Sstevel@tonic-gate extern	int	fr_nat_lock;
430*381a2a9aSdr extern	void	fr_nataddrsync __P((void *, struct in_addr *));
431*381a2a9aSdr extern	void	fr_natifpsync __P((int, void *, char *));
4327c478bd9Sstevel@tonic-gate extern	u_long	fr_defnatage;
4337c478bd9Sstevel@tonic-gate extern	u_long	fr_defnaticmpage;
434ab25eeb5Syz extern	u_long	fr_defnatipage;
4357c478bd9Sstevel@tonic-gate 	/* nat_table[0] -> hashed list sorted by inside (ip, port) */
4367c478bd9Sstevel@tonic-gate 	/* nat_table[1] -> hashed list sorted by outside (ip, port) */
4377c478bd9Sstevel@tonic-gate extern	nat_t	**nat_table[2];
4387c478bd9Sstevel@tonic-gate extern	nat_t	*nat_instances;
4397c478bd9Sstevel@tonic-gate extern	ipnat_t	*nat_list;
4407c478bd9Sstevel@tonic-gate extern	ipnat_t	**nat_rules;
4417c478bd9Sstevel@tonic-gate extern	ipnat_t	**rdr_rules;
4427c478bd9Sstevel@tonic-gate extern	ipftq_t	*nat_utqe;
4437c478bd9Sstevel@tonic-gate extern	natstat_t	nat_stats;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate #if defined(__OpenBSD__)
4467c478bd9Sstevel@tonic-gate extern	void	nat_ifdetach __P((void *));
4477c478bd9Sstevel@tonic-gate #endif
448ab25eeb5Syz extern	int	fr_nat_ioctl __P((caddr_t, ioctlcmd_t, int));
4497c478bd9Sstevel@tonic-gate extern	int	fr_natinit __P((void));
4507c478bd9Sstevel@tonic-gate extern	nat_t	*nat_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int));
4517c478bd9Sstevel@tonic-gate extern	nat_t	*nat_outlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
4527c478bd9Sstevel@tonic-gate 				 struct in_addr));
4537c478bd9Sstevel@tonic-gate extern	void	fix_datacksum __P((u_short *, u_32_t));
4547c478bd9Sstevel@tonic-gate extern	nat_t	*nat_inlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
4557c478bd9Sstevel@tonic-gate 				struct in_addr));
4567c478bd9Sstevel@tonic-gate extern	nat_t	*nat_tnlookup __P((fr_info_t *, int));
4577c478bd9Sstevel@tonic-gate extern	nat_t	*nat_maplookup __P((void *, u_int, struct in_addr,
4587c478bd9Sstevel@tonic-gate 				struct in_addr));
4597c478bd9Sstevel@tonic-gate extern	nat_t	*nat_lookupredir __P((natlookup_t *));
4607c478bd9Sstevel@tonic-gate extern	nat_t	*nat_icmperrorlookup __P((fr_info_t *, int));
4617c478bd9Sstevel@tonic-gate extern	nat_t	*nat_icmperror __P((fr_info_t *, u_int *, int));
462ab25eeb5Syz extern	int	nat_insert __P((nat_t *, int));
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate extern	int	fr_checknatout __P((fr_info_t *, u_32_t *));
4657c478bd9Sstevel@tonic-gate extern	int	fr_natout __P((fr_info_t *, nat_t *, int, u_32_t));
4667c478bd9Sstevel@tonic-gate extern	int	fr_checknatin __P((fr_info_t *, u_32_t *));
4677c478bd9Sstevel@tonic-gate extern	int	fr_natin __P((fr_info_t *, nat_t *, int, u_32_t));
4687c478bd9Sstevel@tonic-gate extern	void	fr_natunload __P((void));
4697c478bd9Sstevel@tonic-gate extern	void	fr_natexpire __P((void));
4707c478bd9Sstevel@tonic-gate extern	void	nat_log __P((struct nat *, u_int));
471*381a2a9aSdr extern	void	fix_incksum __P((u_short *, u_32_t));
472*381a2a9aSdr extern	void	fix_outcksum __P((u_short *, u_32_t));
4737c478bd9Sstevel@tonic-gate extern	void	fr_natderef __P((nat_t **));
4747c478bd9Sstevel@tonic-gate extern	u_short	*nat_proto __P((fr_info_t *, nat_t *, u_int));
4757c478bd9Sstevel@tonic-gate extern	void	nat_update __P((fr_info_t *, nat_t *, ipnat_t *));
4767c478bd9Sstevel@tonic-gate extern	void	fr_setnatqueue __P((nat_t *, int));
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate #endif /* __IP_NAT_H__ */
479