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 $
8*3805c50fSan  *
9f4b3ec61Sdh  * Copyright 2007 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 
85*3805c50fSan /*
86*3805c50fSan  * Default hi and lo watermarks used with forced flush of nat table.
87*3805c50fSan  */
88*3805c50fSan 
89*3805c50fSan #define	NAT_FLUSH_HI	95
90*3805c50fSan #define	NAT_FLUSH_LO	75
91*3805c50fSan 
92*3805c50fSan /* How full is the nat table? */
93*3805c50fSan 
94*3805c50fSan #define	NAT_TAB_WATER_LEVEL(x)	((x)->ifs_nat_stats.ns_inuse * 100 \
95*3805c50fSan 				/ (x)->ifs_ipf_nattable_max)
96*3805c50fSan 
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;
1177c478bd9Sstevel@tonic-gate 	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 */
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
152*3805c50fSan #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
2687c478bd9Sstevel@tonic-gate #define	IPN_USERFLAGS	(IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\
2697c478bd9Sstevel@tonic-gate 			 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\
270ab25eeb5Syz 			 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY)
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * Values for in_redir
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate #define	NAT_MAP		0x01
2767c478bd9Sstevel@tonic-gate #define	NAT_REDIRECT	0x02
2777c478bd9Sstevel@tonic-gate #define	NAT_BIMAP	(NAT_MAP|NAT_REDIRECT)
2787c478bd9Sstevel@tonic-gate #define	NAT_MAPBLK	0x04
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate #define	MAPBLK_MINPORT	1024	/* don't use reserved ports for src port */
2817c478bd9Sstevel@tonic-gate #define	USABLE_PORTS	(65536 - MAPBLK_MINPORT)
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate #define	IPN_CMPSIZ	(sizeof(ipnat_t) - offsetof(ipnat_t, in_flags))
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate typedef	struct	natlookup {
2867c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_inip;
2877c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_outip;
2887c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_realip;
2897c478bd9Sstevel@tonic-gate 	int	nl_flags;
2907c478bd9Sstevel@tonic-gate 	u_short	nl_inport;
2917c478bd9Sstevel@tonic-gate 	u_short	nl_outport;
2927c478bd9Sstevel@tonic-gate 	u_short	nl_realport;
2937c478bd9Sstevel@tonic-gate } natlookup_t;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate typedef struct  nat_save    {
2977c478bd9Sstevel@tonic-gate 	void	*ipn_next;
2987c478bd9Sstevel@tonic-gate 	struct	nat	ipn_nat;
2997c478bd9Sstevel@tonic-gate 	struct	ipnat	ipn_ipnat;
3007c478bd9Sstevel@tonic-gate 	struct	frentry ipn_fr;
3017c478bd9Sstevel@tonic-gate 	int	ipn_dsize;
3027c478bd9Sstevel@tonic-gate 	char	ipn_data[4];
3037c478bd9Sstevel@tonic-gate } nat_save_t;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate #define	ipn_rule	ipn_nat.nat_fr
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate typedef	struct	natget	{
3087c478bd9Sstevel@tonic-gate 	void	*ng_ptr;
3097c478bd9Sstevel@tonic-gate 	int	ng_sz;
3107c478bd9Sstevel@tonic-gate } natget_t;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
313ab25eeb5Syz #undef	tr_flags
3147c478bd9Sstevel@tonic-gate typedef	struct	nattrpnt	{
3157c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_dstip;	/* real destination IP# */
3167c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_srcip;	/* real source IP# */
3177c478bd9Sstevel@tonic-gate 	struct	in_addr	tr_locip;	/* local source IP# */
3187c478bd9Sstevel@tonic-gate 	u_int	tr_flags;
3197c478bd9Sstevel@tonic-gate 	int	tr_expire;
3207c478bd9Sstevel@tonic-gate 	u_short	tr_dstport;	/* real destination port# */
3217c478bd9Sstevel@tonic-gate 	u_short	tr_srcport;	/* real source port# */
3227c478bd9Sstevel@tonic-gate 	u_short	tr_locport;	/* local source port# */
3237c478bd9Sstevel@tonic-gate 	struct	nattrpnt	*tr_hnext;
3247c478bd9Sstevel@tonic-gate 	struct	nattrpnt	**tr_phnext;
3257c478bd9Sstevel@tonic-gate 	struct	nattrpnt	*tr_next;
3267c478bd9Sstevel@tonic-gate 	struct	nattrpnt	**tr_pnext;	/* previous next */
3277c478bd9Sstevel@tonic-gate } nattrpnt_t;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate #define	TN_CMPSIZ	offsetof(nattrpnt_t, tr_hnext)
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * This structure gets used to help NAT sessions keep the same NAT rule (and
3347c478bd9Sstevel@tonic-gate  * thus translation for IP address) when:
3357c478bd9Sstevel@tonic-gate  * (a) round-robin redirects are in use
3367c478bd9Sstevel@tonic-gate  * (b) different IP add
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate typedef	struct	hostmap	{
3397c478bd9Sstevel@tonic-gate 	struct	hostmap	*hm_next;
3407c478bd9Sstevel@tonic-gate 	struct	hostmap	**hm_pnext;
341f4b3ec61Sdh 	struct	hostmap	*hm_hnext;
342f4b3ec61Sdh 	struct	hostmap	**hm_phnext;
3437c478bd9Sstevel@tonic-gate 	struct	ipnat	*hm_ipnat;
3447c478bd9Sstevel@tonic-gate 	struct	in_addr	hm_srcip;
3457c478bd9Sstevel@tonic-gate 	struct	in_addr	hm_dstip;
3467c478bd9Sstevel@tonic-gate 	struct	in_addr	hm_mapip;
3477c478bd9Sstevel@tonic-gate 	u_32_t		hm_port;
3487c478bd9Sstevel@tonic-gate 	int		hm_ref;
3497c478bd9Sstevel@tonic-gate } hostmap_t;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * Structure used to pass information in to nat_newmap and nat_newrdr.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate typedef struct	natinfo	{
3567c478bd9Sstevel@tonic-gate 	ipnat_t		*nai_np;
3577c478bd9Sstevel@tonic-gate 	u_32_t		nai_sum1;
3587c478bd9Sstevel@tonic-gate 	u_32_t		nai_sum2;
3597c478bd9Sstevel@tonic-gate 	u_32_t		nai_nflags;
3607c478bd9Sstevel@tonic-gate 	u_32_t		nai_flags;
3617c478bd9Sstevel@tonic-gate 	struct	in_addr	nai_ip;
3627c478bd9Sstevel@tonic-gate 	u_short		nai_port;
3637c478bd9Sstevel@tonic-gate 	u_short		nai_nport;
3647c478bd9Sstevel@tonic-gate 	u_short		nai_sport;
3657c478bd9Sstevel@tonic-gate 	u_short		nai_dport;
3667c478bd9Sstevel@tonic-gate } natinfo_t;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate typedef	struct	natstat	{
3707c478bd9Sstevel@tonic-gate 	u_long	ns_mapped[2];
3717c478bd9Sstevel@tonic-gate 	u_long	ns_rules;
3727c478bd9Sstevel@tonic-gate 	u_long	ns_added;
3737c478bd9Sstevel@tonic-gate 	u_long	ns_expire;
3747c478bd9Sstevel@tonic-gate 	u_long	ns_inuse;
3757c478bd9Sstevel@tonic-gate 	u_long	ns_logged;
3767c478bd9Sstevel@tonic-gate 	u_long	ns_logfail;
3777c478bd9Sstevel@tonic-gate 	u_long	ns_memfail;
3787c478bd9Sstevel@tonic-gate 	u_long	ns_badnat;
3797c478bd9Sstevel@tonic-gate 	u_long	ns_addtrpnt;
3807c478bd9Sstevel@tonic-gate 	nat_t	**ns_table[2];
3817c478bd9Sstevel@tonic-gate 	hostmap_t **ns_maptable;
3827c478bd9Sstevel@tonic-gate 	ipnat_t	*ns_list;
3837c478bd9Sstevel@tonic-gate 	void	*ns_apslist;
3847c478bd9Sstevel@tonic-gate 	u_int	ns_wilds;
3857c478bd9Sstevel@tonic-gate 	u_int	ns_nattab_sz;
386ab25eeb5Syz 	u_int	ns_nattab_max;
3877c478bd9Sstevel@tonic-gate 	u_int	ns_rultab_sz;
3887c478bd9Sstevel@tonic-gate 	u_int	ns_rdrtab_sz;
3897c478bd9Sstevel@tonic-gate 	u_int	ns_trpntab_sz;
3907c478bd9Sstevel@tonic-gate 	u_int	ns_hostmap_sz;
3917c478bd9Sstevel@tonic-gate 	nat_t	*ns_instances;
3927c478bd9Sstevel@tonic-gate 	nattrpnt_t *ns_trpntlist;
393f4b3ec61Sdh 	hostmap_t *ns_maplist;
3947c478bd9Sstevel@tonic-gate 	u_long	*ns_bucketlen[2];
3957c478bd9Sstevel@tonic-gate } natstat_t;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate typedef	struct	natlog {
3987c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_origip;
3997c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_outip;
4007c478bd9Sstevel@tonic-gate 	struct	in_addr	nl_inip;
4017c478bd9Sstevel@tonic-gate 	u_short	nl_origport;
4027c478bd9Sstevel@tonic-gate 	u_short	nl_outport;
4037c478bd9Sstevel@tonic-gate 	u_short	nl_inport;
4047c478bd9Sstevel@tonic-gate 	u_short	nl_type;
4057c478bd9Sstevel@tonic-gate 	int	nl_rule;
4067c478bd9Sstevel@tonic-gate 	U_QUAD_T	nl_pkts[2];
4077c478bd9Sstevel@tonic-gate 	U_QUAD_T	nl_bytes[2];
4087c478bd9Sstevel@tonic-gate 	u_char	nl_p;
4097c478bd9Sstevel@tonic-gate } natlog_t;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate #define	NL_NEWMAP	NAT_MAP
4137c478bd9Sstevel@tonic-gate #define	NL_NEWRDR	NAT_REDIRECT
4147c478bd9Sstevel@tonic-gate #define	NL_NEWBIMAP	NAT_BIMAP
4157c478bd9Sstevel@tonic-gate #define	NL_NEWBLOCK	NAT_MAPBLK
4167c478bd9Sstevel@tonic-gate #define	NL_CLONE	0xfffd
4177c478bd9Sstevel@tonic-gate #define	NL_FLUSH	0xfffe
4187c478bd9Sstevel@tonic-gate #define	NL_EXPIRE	0xffff
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate #define	NAT_HASH_FN(k,l,m)	(((k) + ((k) >> 12) + l) % (m))
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate #define	LONG_SUM(in)	(((in) & 0xffff) + ((in) >> 16))
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate #define	CALC_SUMD(s1, s2, sd) { \
4257c478bd9Sstevel@tonic-gate 			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
4267c478bd9Sstevel@tonic-gate 			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
4277c478bd9Sstevel@tonic-gate 			    /* Do it twice */ \
4287c478bd9Sstevel@tonic-gate 			    (s1) = ((s1) & 0xffff) + ((s1) >> 16); \
4297c478bd9Sstevel@tonic-gate 			    (s2) = ((s2) & 0xffff) + ((s2) >> 16); \
4307c478bd9Sstevel@tonic-gate 			    /* Because ~1 == -2, We really need ~1 == -1 */ \
4317c478bd9Sstevel@tonic-gate 			    if ((s1) > (s2)) (s2)--; \
4327c478bd9Sstevel@tonic-gate 			    (sd) = (s2) - (s1); \
4337c478bd9Sstevel@tonic-gate 			    (sd) = ((sd) & 0xffff) + ((sd) >> 16); }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate #define	NAT_SYSSPACE		0x80000000
4367c478bd9Sstevel@tonic-gate #define	NAT_LOCKHELD		0x40000000
4377c478bd9Sstevel@tonic-gate 
438f4b3ec61Sdh extern	void	fr_natsync __P((void *, ipf_stack_t *));
439f4b3ec61Sdh extern	void	fr_nataddrsync __P((void *, struct in_addr *, ipf_stack_t *));
440f4b3ec61Sdh extern	void	fr_natifpsync __P((int, void *, char *, ipf_stack_t *));
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate #if defined(__OpenBSD__)
443f4b3ec61Sdh extern	void	nat_ifdetach __P((void *, ipf_stack_t *));
4447c478bd9Sstevel@tonic-gate #endif
445f4b3ec61Sdh extern	int	fr_nat_ioctl __P((caddr_t, ioctlcmd_t, int, int, void *, ipf_stack_t *));
446f4b3ec61Sdh extern	int	fr_natinit __P((ipf_stack_t *));
4477c478bd9Sstevel@tonic-gate extern	nat_t	*nat_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int));
4487c478bd9Sstevel@tonic-gate extern	nat_t	*nat_outlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
4497c478bd9Sstevel@tonic-gate 				 struct in_addr));
4507c478bd9Sstevel@tonic-gate extern	void	fix_datacksum __P((u_short *, u_32_t));
4517c478bd9Sstevel@tonic-gate extern	nat_t	*nat_inlookup __P((fr_info_t *, u_int, u_int, struct in_addr,
4527c478bd9Sstevel@tonic-gate 				struct in_addr));
4537c478bd9Sstevel@tonic-gate extern	nat_t	*nat_tnlookup __P((fr_info_t *, int));
4547c478bd9Sstevel@tonic-gate extern	nat_t	*nat_maplookup __P((void *, u_int, struct in_addr,
4557c478bd9Sstevel@tonic-gate 				struct in_addr));
456f4b3ec61Sdh extern	nat_t	*nat_lookupredir __P((natlookup_t *, ipf_stack_t *));
4577c478bd9Sstevel@tonic-gate extern	nat_t	*nat_icmperrorlookup __P((fr_info_t *, int));
4587c478bd9Sstevel@tonic-gate extern	nat_t	*nat_icmperror __P((fr_info_t *, u_int *, int));
459f4b3ec61Sdh extern	int	nat_insert __P((nat_t *, int, ipf_stack_t *));
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate extern	int	fr_checknatout __P((fr_info_t *, u_32_t *));
4627c478bd9Sstevel@tonic-gate extern	int	fr_natout __P((fr_info_t *, nat_t *, int, u_32_t));
4637c478bd9Sstevel@tonic-gate extern	int	fr_checknatin __P((fr_info_t *, u_32_t *));
4647c478bd9Sstevel@tonic-gate extern	int	fr_natin __P((fr_info_t *, nat_t *, int, u_32_t));
465f4b3ec61Sdh extern	void	fr_natunload __P((ipf_stack_t *));
466f4b3ec61Sdh extern	void	fr_natexpire __P((ipf_stack_t *));
467f4b3ec61Sdh extern	void	nat_log __P((struct nat *, u_int, ipf_stack_t *));
468381a2a9aSdr extern	void	fix_incksum __P((u_short *, u_32_t));
469381a2a9aSdr extern	void	fix_outcksum __P((u_short *, u_32_t));
470f4b3ec61Sdh extern  void    fr_ipnatderef __P((ipnat_t **, ipf_stack_t *));
471f4b3ec61Sdh extern	void	fr_natderef __P((nat_t **, ipf_stack_t *));
4727c478bd9Sstevel@tonic-gate extern	u_short	*nat_proto __P((fr_info_t *, nat_t *, u_int));
4737c478bd9Sstevel@tonic-gate extern	void	nat_update __P((fr_info_t *, nat_t *, ipnat_t *));
474f4b3ec61Sdh extern	void	fr_setnatqueue __P((nat_t *, int, ipf_stack_t *));
475f4b3ec61Sdh extern  void    fr_hostmapderef __P((hostmap_t **));
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate #endif /* __IP_NAT_H__ */
478