xref: /onnv-gate/usr/src/uts/common/inet/ip/ipclassifier.c (revision 7502:da077e5d991e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51503Sericheng  * Common Development and Distribution License (the "License").
61503Sericheng  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7502Saruna@cs.umn.edu  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * IP PACKET CLASSIFIER
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * The IP packet classifier provides mapping between IP packets and persistent
300Sstevel@tonic-gate  * connection state for connection-oriented protocols. It also provides
310Sstevel@tonic-gate  * interface for managing connection states.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * The connection state is kept in conn_t data structure and contains, among
340Sstevel@tonic-gate  * other things:
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *	o local/remote address and ports
370Sstevel@tonic-gate  *	o Transport protocol
380Sstevel@tonic-gate  *	o squeue for the connection (for TCP only)
390Sstevel@tonic-gate  *	o reference counter
400Sstevel@tonic-gate  *	o Connection state
410Sstevel@tonic-gate  *	o hash table linkage
420Sstevel@tonic-gate  *	o interface/ire information
430Sstevel@tonic-gate  *	o credentials
440Sstevel@tonic-gate  *	o ipsec policy
450Sstevel@tonic-gate  *	o send and receive functions.
460Sstevel@tonic-gate  *	o mutex lock.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * Connections use a reference counting scheme. They are freed when the
490Sstevel@tonic-gate  * reference counter drops to zero. A reference is incremented when connection
500Sstevel@tonic-gate  * is placed in a list or table, when incoming packet for the connection arrives
510Sstevel@tonic-gate  * and when connection is processed via squeue (squeue processing may be
520Sstevel@tonic-gate  * asynchronous and the reference protects the connection from being destroyed
530Sstevel@tonic-gate  * before its processing is finished).
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * send and receive functions are currently used for TCP only. The send function
560Sstevel@tonic-gate  * determines the IP entry point for the packet once it leaves TCP to be sent to
570Sstevel@tonic-gate  * the destination address. The receive function is used by IP when the packet
580Sstevel@tonic-gate  * should be passed for TCP processing. When a new connection is created these
590Sstevel@tonic-gate  * are set to ip_output() and tcp_input() respectively. During the lifetime of
600Sstevel@tonic-gate  * the connection the send and receive functions may change depending on the
610Sstevel@tonic-gate  * changes in the connection state. For example, Once the connection is bound to
620Sstevel@tonic-gate  * an addresse, the receive function for this connection is set to
630Sstevel@tonic-gate  * tcp_conn_request().  This allows incoming SYNs to go directly into the
640Sstevel@tonic-gate  * listener SYN processing function without going to tcp_input() first.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * Classifier uses several hash tables:
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 	ipcl_conn_fanout:	contains all TCP connections in CONNECTED state
690Sstevel@tonic-gate  *	ipcl_bind_fanout:	contains all connections in BOUND state
700Sstevel@tonic-gate  *	ipcl_proto_fanout:	IPv4 protocol fanout
710Sstevel@tonic-gate  *	ipcl_proto_fanout_v6:	IPv6 protocol fanout
720Sstevel@tonic-gate  *	ipcl_udp_fanout:	contains all UDP connections
730Sstevel@tonic-gate  *	ipcl_globalhash_fanout:	contains all connections
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * The ipcl_globalhash_fanout is used for any walkers (like snmp and Clustering)
760Sstevel@tonic-gate  * which need to view all existing connections.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * All tables are protected by per-bucket locks. When both per-bucket lock and
790Sstevel@tonic-gate  * connection lock need to be held, the per-bucket lock should be acquired
800Sstevel@tonic-gate  * first, followed by the connection lock.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * All functions doing search in one of these tables increment a reference
830Sstevel@tonic-gate  * counter on the connection found (if any). This reference should be dropped
840Sstevel@tonic-gate  * when the caller has finished processing the connection.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * INTERFACES:
880Sstevel@tonic-gate  * ===========
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  * Connection Lookup:
910Sstevel@tonic-gate  * ------------------
920Sstevel@tonic-gate  *
933448Sdh155122  * conn_t *ipcl_classify_v4(mp, protocol, hdr_len, zoneid, ip_stack)
943448Sdh155122  * conn_t *ipcl_classify_v6(mp, protocol, hdr_len, zoneid, ip_stack)
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * Finds connection for an incoming IPv4 or IPv6 packet. Returns NULL if
970Sstevel@tonic-gate  * it can't find any associated connection. If the connection is found, its
980Sstevel@tonic-gate  * reference counter is incremented.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *	mp:	mblock, containing packet header. The full header should fit
1010Sstevel@tonic-gate  *		into a single mblock. It should also contain at least full IP
1020Sstevel@tonic-gate  *		and TCP or UDP header.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  *	protocol: Either IPPROTO_TCP or IPPROTO_UDP.
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  *	hdr_len: The size of IP header. It is used to find TCP or UDP header in
1070Sstevel@tonic-gate  *		 the packet.
1080Sstevel@tonic-gate  *
1091676Sjpk  * 	zoneid: The zone in which the returned connection must be; the zoneid
1101676Sjpk  *		corresponding to the ire_zoneid on the IRE located for the
1111676Sjpk  *		packet's destination address.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  *	For TCP connections, the lookup order is as follows:
1140Sstevel@tonic-gate  *		5-tuple {src, dst, protocol, local port, remote port}
1150Sstevel@tonic-gate  *			lookup in ipcl_conn_fanout table.
1160Sstevel@tonic-gate  *		3-tuple {dst, remote port, protocol} lookup in
1170Sstevel@tonic-gate  *			ipcl_bind_fanout table.
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  *	For UDP connections, a 5-tuple {src, dst, protocol, local port,
1200Sstevel@tonic-gate  *	remote port} lookup is done on ipcl_udp_fanout. Note that,
1210Sstevel@tonic-gate  *	these interfaces do not handle cases where a packets belongs
1220Sstevel@tonic-gate  *	to multiple UDP clients, which is handled in IP itself.
1230Sstevel@tonic-gate  *
1241676Sjpk  * If the destination IRE is ALL_ZONES (indicated by zoneid), then we must
1251676Sjpk  * determine which actual zone gets the segment.  This is used only in a
1261676Sjpk  * labeled environment.  The matching rules are:
1271676Sjpk  *
1281676Sjpk  *	- If it's not a multilevel port, then the label on the packet selects
1291676Sjpk  *	  the zone.  Unlabeled packets are delivered to the global zone.
1301676Sjpk  *
1311676Sjpk  *	- If it's a multilevel port, then only the zone registered to receive
1321676Sjpk  *	  packets on that port matches.
1331676Sjpk  *
1341676Sjpk  * Also, in a labeled environment, packet labels need to be checked.  For fully
1351676Sjpk  * bound TCP connections, we can assume that the packet label was checked
1361676Sjpk  * during connection establishment, and doesn't need to be checked on each
1371676Sjpk  * packet.  For others, though, we need to check for strict equality or, for
1381676Sjpk  * multilevel ports, membership in the range or set.  This part currently does
1391676Sjpk  * a tnrh lookup on each packet, but could be optimized to use cached results
1401676Sjpk  * if that were necessary.  (SCTP doesn't come through here, but if it did,
1411676Sjpk  * we would apply the same rules as TCP.)
1421676Sjpk  *
1431676Sjpk  * An implication of the above is that fully-bound TCP sockets must always use
1441676Sjpk  * distinct 4-tuples; they can't be discriminated by label alone.
1451676Sjpk  *
1461676Sjpk  * Note that we cannot trust labels on packets sent to fully-bound UDP sockets,
1471676Sjpk  * as there's no connection set-up handshake and no shared state.
1481676Sjpk  *
1491676Sjpk  * Labels on looped-back packets within a single zone do not need to be
1501676Sjpk  * checked, as all processes in the same zone have the same label.
1511676Sjpk  *
1521676Sjpk  * Finally, for unlabeled packets received by a labeled system, special rules
1531676Sjpk  * apply.  We consider only the MLP if there is one.  Otherwise, we prefer a
1541676Sjpk  * socket in the zone whose label matches the default label of the sender, if
1551676Sjpk  * any.  In any event, the receiving socket must have SO_MAC_EXEMPT set and the
1561676Sjpk  * receiver's label must dominate the sender's default label.
1571676Sjpk  *
1583448Sdh155122  * conn_t *ipcl_tcp_lookup_reversed_ipv4(ipha_t *, tcph_t *, int, ip_stack);
1593448Sdh155122  * conn_t *ipcl_tcp_lookup_reversed_ipv6(ip6_t *, tcpha_t *, int, uint_t,
1603448Sdh155122  *					 ip_stack);
1610Sstevel@tonic-gate  *
1620Sstevel@tonic-gate  *	Lookup routine to find a exact match for {src, dst, local port,
1630Sstevel@tonic-gate  *	remote port) for TCP connections in ipcl_conn_fanout. The address and
1640Sstevel@tonic-gate  *	ports are read from the IP and TCP header respectively.
1650Sstevel@tonic-gate  *
1663448Sdh155122  * conn_t	*ipcl_lookup_listener_v4(lport, laddr, protocol,
1673448Sdh155122  *					 zoneid, ip_stack);
1683448Sdh155122  * conn_t	*ipcl_lookup_listener_v6(lport, laddr, protocol, ifindex,
1693448Sdh155122  *					 zoneid, ip_stack);
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  * 	Lookup routine to find a listener with the tuple {lport, laddr,
1720Sstevel@tonic-gate  * 	protocol} in the ipcl_bind_fanout table. For IPv6, an additional
1730Sstevel@tonic-gate  * 	parameter interface index is also compared.
1740Sstevel@tonic-gate  *
1753448Sdh155122  * void ipcl_walk(func, arg, ip_stack)
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  * 	Apply 'func' to every connection available. The 'func' is called as
1780Sstevel@tonic-gate  *	(*func)(connp, arg). The walk is non-atomic so connections may be
1790Sstevel@tonic-gate  *	created and destroyed during the walk. The CONN_CONDEMNED and
1800Sstevel@tonic-gate  *	CONN_INCIPIENT flags ensure that connections which are newly created
1810Sstevel@tonic-gate  *	or being destroyed are not selected by the walker.
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * Table Updates
1840Sstevel@tonic-gate  * -------------
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  * int ipcl_conn_insert(connp, protocol, src, dst, ports)
1870Sstevel@tonic-gate  * int ipcl_conn_insert_v6(connp, protocol, src, dst, ports, ifindex)
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  *	Insert 'connp' in the ipcl_conn_fanout.
1900Sstevel@tonic-gate  *	Arguements :
1910Sstevel@tonic-gate  *		connp		conn_t to be inserted
1920Sstevel@tonic-gate  *		protocol	connection protocol
1930Sstevel@tonic-gate  *		src		source address
1940Sstevel@tonic-gate  *		dst		destination address
1950Sstevel@tonic-gate  *		ports		local and remote port
1960Sstevel@tonic-gate  *		ifindex		interface index for IPv6 connections
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  *	Return value :
1990Sstevel@tonic-gate  *		0		if connp was inserted
2000Sstevel@tonic-gate  *		EADDRINUSE	if the connection with the same tuple
2010Sstevel@tonic-gate  *				already exists.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  * int ipcl_bind_insert(connp, protocol, src, lport);
2040Sstevel@tonic-gate  * int ipcl_bind_insert_v6(connp, protocol, src, lport);
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  * 	Insert 'connp' in ipcl_bind_fanout.
2070Sstevel@tonic-gate  * 	Arguements :
2080Sstevel@tonic-gate  * 		connp		conn_t to be inserted
2090Sstevel@tonic-gate  * 		protocol	connection protocol
2100Sstevel@tonic-gate  * 		src		source address connection wants
2110Sstevel@tonic-gate  * 				to bind to
2120Sstevel@tonic-gate  * 		lport		local port connection wants to
2130Sstevel@tonic-gate  * 				bind to
2140Sstevel@tonic-gate  *
2150Sstevel@tonic-gate  *
2160Sstevel@tonic-gate  * void ipcl_hash_remove(connp);
2170Sstevel@tonic-gate  *
2180Sstevel@tonic-gate  * 	Removes the 'connp' from the connection fanout table.
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * Connection Creation/Destruction
2210Sstevel@tonic-gate  * -------------------------------
2220Sstevel@tonic-gate  *
2233448Sdh155122  * conn_t *ipcl_conn_create(type, sleep, netstack_t *)
2240Sstevel@tonic-gate  *
2250Sstevel@tonic-gate  * 	Creates a new conn based on the type flag, inserts it into
2260Sstevel@tonic-gate  * 	globalhash table.
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  *	type:	This flag determines the type of conn_t which needs to be
2295240Snordmark  *		created i.e., which kmem_cache it comes from.
2300Sstevel@tonic-gate  *		IPCL_TCPCONN	indicates a TCP connection
2315240Snordmark  *		IPCL_SCTPCONN	indicates a SCTP connection
2325240Snordmark  *		IPCL_UDPCONN	indicates a UDP conn_t.
2335240Snordmark  *		IPCL_RAWIPCONN	indicates a RAWIP/ICMP conn_t.
2345240Snordmark  *		IPCL_RTSCONN	indicates a RTS conn_t.
2355240Snordmark  *		IPCL_IPCCONN	indicates all other connections.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * void ipcl_conn_destroy(connp)
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  * 	Destroys the connection state, removes it from the global
2400Sstevel@tonic-gate  * 	connection hash table and frees its memory.
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate #include <sys/types.h>
2440Sstevel@tonic-gate #include <sys/stream.h>
2450Sstevel@tonic-gate #include <sys/stropts.h>
2460Sstevel@tonic-gate #include <sys/sysmacros.h>
2470Sstevel@tonic-gate #include <sys/strsubr.h>
2480Sstevel@tonic-gate #include <sys/strsun.h>
2490Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
2500Sstevel@tonic-gate #include <sys/ddi.h>
2510Sstevel@tonic-gate #include <sys/cmn_err.h>
2520Sstevel@tonic-gate #include <sys/debug.h>
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #include <sys/systm.h>
2550Sstevel@tonic-gate #include <sys/param.h>
2560Sstevel@tonic-gate #include <sys/kmem.h>
2570Sstevel@tonic-gate #include <sys/isa_defs.h>
2580Sstevel@tonic-gate #include <inet/common.h>
2590Sstevel@tonic-gate #include <netinet/ip6.h>
2600Sstevel@tonic-gate #include <netinet/icmp6.h>
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate #include <inet/ip.h>
2630Sstevel@tonic-gate #include <inet/ip6.h>
2640Sstevel@tonic-gate #include <inet/tcp.h>
2650Sstevel@tonic-gate #include <inet/ip_ndp.h>
266741Smasputra #include <inet/udp_impl.h>
2670Sstevel@tonic-gate #include <inet/sctp_ip.h>
2683448Sdh155122 #include <inet/sctp/sctp_impl.h>
2695240Snordmark #include <inet/rawip_impl.h>
2705240Snordmark #include <inet/rts_impl.h>
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate #include <sys/cpuvar.h>
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate #include <inet/ipclassifier.h>
2750Sstevel@tonic-gate #include <inet/ipsec_impl.h>
2760Sstevel@tonic-gate 
2771676Sjpk #include <sys/tsol/tnet.h>
2781676Sjpk 
2790Sstevel@tonic-gate #ifdef DEBUG
2800Sstevel@tonic-gate #define	IPCL_DEBUG
2810Sstevel@tonic-gate #else
2820Sstevel@tonic-gate #undef	IPCL_DEBUG
2830Sstevel@tonic-gate #endif
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate #ifdef	IPCL_DEBUG
2860Sstevel@tonic-gate int	ipcl_debug_level = 0;
2870Sstevel@tonic-gate #define	IPCL_DEBUG_LVL(level, args)	\
2880Sstevel@tonic-gate 	if (ipcl_debug_level  & level) { printf args; }
2890Sstevel@tonic-gate #else
2900Sstevel@tonic-gate #define	IPCL_DEBUG_LVL(level, args) {; }
2910Sstevel@tonic-gate #endif
2923448Sdh155122 /* Old value for compatibility. Setable in /etc/system */
2930Sstevel@tonic-gate uint_t tcp_conn_hash_size = 0;
2940Sstevel@tonic-gate 
2953448Sdh155122 /* New value. Zero means choose automatically.  Setable in /etc/system */
2960Sstevel@tonic-gate uint_t ipcl_conn_hash_size = 0;
2970Sstevel@tonic-gate uint_t ipcl_conn_hash_memfactor = 8192;
2980Sstevel@tonic-gate uint_t ipcl_conn_hash_maxsize = 82500;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /* bind/udp fanout table size */
3010Sstevel@tonic-gate uint_t ipcl_bind_fanout_size = 512;
3021503Sericheng uint_t ipcl_udp_fanout_size = 16384;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /* Raw socket fanout size.  Must be a power of 2. */
3050Sstevel@tonic-gate uint_t ipcl_raw_fanout_size = 256;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * Power of 2^N Primes useful for hashing for N of 0-28,
3090Sstevel@tonic-gate  * these primes are the nearest prime <= 2^N - 2^(N-2).
3100Sstevel@tonic-gate  */
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate #define	P2Ps() {0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,	\
3130Sstevel@tonic-gate 		6143, 12281, 24571, 49139, 98299, 196597, 393209,	\
3140Sstevel@tonic-gate 		786431, 1572853, 3145721, 6291449, 12582893, 25165813,	\
3150Sstevel@tonic-gate 		50331599, 100663291, 201326557, 0}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate /*
3185240Snordmark  * wrapper structure to ensure that conn and what follows it (tcp_t, etc)
3195240Snordmark  * are aligned on cache lines.
3200Sstevel@tonic-gate  */
3215240Snordmark typedef union itc_s {
3225240Snordmark 	conn_t	itc_conn;
3235240Snordmark 	char	itcu_filler[CACHE_ALIGN(conn_s)];
3240Sstevel@tonic-gate } itc_t;
3250Sstevel@tonic-gate 
3265240Snordmark struct kmem_cache  *tcp_conn_cache;
3275240Snordmark struct kmem_cache  *ip_conn_cache;
3280Sstevel@tonic-gate extern struct kmem_cache  *sctp_conn_cache;
3290Sstevel@tonic-gate extern struct kmem_cache  *tcp_sack_info_cache;
3300Sstevel@tonic-gate extern struct kmem_cache  *tcp_iphc_cache;
3315240Snordmark struct kmem_cache  *udp_conn_cache;
3325240Snordmark struct kmem_cache  *rawip_conn_cache;
3335240Snordmark struct kmem_cache  *rts_conn_cache;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate extern void	tcp_timermp_free(tcp_t *);
3360Sstevel@tonic-gate extern mblk_t	*tcp_timermp_alloc(int);
3370Sstevel@tonic-gate 
3385240Snordmark static int	ip_conn_constructor(void *, void *, int);
3395240Snordmark static void	ip_conn_destructor(void *, void *);
3405240Snordmark 
3415240Snordmark static int	tcp_conn_constructor(void *, void *, int);
3425240Snordmark static void	tcp_conn_destructor(void *, void *);
3435240Snordmark 
3445240Snordmark static int	udp_conn_constructor(void *, void *, int);
3455240Snordmark static void	udp_conn_destructor(void *, void *);
3465240Snordmark 
3475240Snordmark static int	rawip_conn_constructor(void *, void *, int);
3485240Snordmark static void	rawip_conn_destructor(void *, void *);
3495240Snordmark 
3505240Snordmark static int	rts_conn_constructor(void *, void *, int);
3515240Snordmark static void	rts_conn_destructor(void *, void *);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate #ifdef	IPCL_DEBUG
3540Sstevel@tonic-gate #define	INET_NTOA_BUFSIZE	18
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate static char *
3570Sstevel@tonic-gate inet_ntoa_r(uint32_t in, char *b)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	unsigned char	*p;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	p = (unsigned char *)&in;
3620Sstevel@tonic-gate 	(void) sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
3630Sstevel@tonic-gate 	return (b);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate #endif
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate /*
3683448Sdh155122  * Global (for all stack instances) init routine
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate void
3713448Sdh155122 ipcl_g_init(void)
3720Sstevel@tonic-gate {
3735240Snordmark 	ip_conn_cache = kmem_cache_create("ip_conn_cache",
3740Sstevel@tonic-gate 	    sizeof (conn_t), CACHE_ALIGN_SIZE,
3755240Snordmark 	    ip_conn_constructor, ip_conn_destructor,
3765240Snordmark 	    NULL, NULL, NULL, 0);
3775240Snordmark 
3785240Snordmark 	tcp_conn_cache = kmem_cache_create("tcp_conn_cache",
3795240Snordmark 	    sizeof (itc_t) + sizeof (tcp_t), CACHE_ALIGN_SIZE,
3805240Snordmark 	    tcp_conn_constructor, tcp_conn_destructor,
3815240Snordmark 	    NULL, NULL, NULL, 0);
3820Sstevel@tonic-gate 
3835240Snordmark 	udp_conn_cache = kmem_cache_create("udp_conn_cache",
3845240Snordmark 	    sizeof (itc_t) + sizeof (udp_t), CACHE_ALIGN_SIZE,
3855240Snordmark 	    udp_conn_constructor, udp_conn_destructor,
3865240Snordmark 	    NULL, NULL, NULL, 0);
3875240Snordmark 
3885240Snordmark 	rawip_conn_cache = kmem_cache_create("rawip_conn_cache",
3895240Snordmark 	    sizeof (itc_t) + sizeof (icmp_t), CACHE_ALIGN_SIZE,
3905240Snordmark 	    rawip_conn_constructor, rawip_conn_destructor,
3915240Snordmark 	    NULL, NULL, NULL, 0);
3925240Snordmark 
3935240Snordmark 	rts_conn_cache = kmem_cache_create("rts_conn_cache",
3945240Snordmark 	    sizeof (itc_t) + sizeof (rts_t), CACHE_ALIGN_SIZE,
3955240Snordmark 	    rts_conn_constructor, rts_conn_destructor,
3960Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
3973448Sdh155122 }
3983448Sdh155122 
3993448Sdh155122 /*
4003448Sdh155122  * ipclassifier intialization routine, sets up hash tables.
4013448Sdh155122  */
4023448Sdh155122 void
4033448Sdh155122 ipcl_init(ip_stack_t *ipst)
4043448Sdh155122 {
4053448Sdh155122 	int i;
4063448Sdh155122 	int sizes[] = P2Ps();
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/*
4093448Sdh155122 	 * Calculate size of conn fanout table from /etc/system settings
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate 	if (ipcl_conn_hash_size != 0) {
4123448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = ipcl_conn_hash_size;
4130Sstevel@tonic-gate 	} else if (tcp_conn_hash_size != 0) {
4143448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = tcp_conn_hash_size;
4150Sstevel@tonic-gate 	} else {
4160Sstevel@tonic-gate 		extern pgcnt_t freemem;
4170Sstevel@tonic-gate 
4183448Sdh155122 		ipst->ips_ipcl_conn_fanout_size =
4190Sstevel@tonic-gate 		    (freemem * PAGESIZE) / ipcl_conn_hash_memfactor;
4200Sstevel@tonic-gate 
4213448Sdh155122 		if (ipst->ips_ipcl_conn_fanout_size > ipcl_conn_hash_maxsize) {
4223448Sdh155122 			ipst->ips_ipcl_conn_fanout_size =
4233448Sdh155122 			    ipcl_conn_hash_maxsize;
4243448Sdh155122 		}
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	for (i = 9; i < sizeof (sizes) / sizeof (*sizes) - 1; i++) {
4283448Sdh155122 		if (sizes[i] >= ipst->ips_ipcl_conn_fanout_size) {
4290Sstevel@tonic-gate 			break;
4300Sstevel@tonic-gate 		}
4310Sstevel@tonic-gate 	}
4323448Sdh155122 	if ((ipst->ips_ipcl_conn_fanout_size = sizes[i]) == 0) {
4330Sstevel@tonic-gate 		/* Out of range, use the 2^16 value */
4343448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = sizes[16];
4350Sstevel@tonic-gate 	}
4363448Sdh155122 
4373448Sdh155122 	/* Take values from /etc/system */
4383448Sdh155122 	ipst->ips_ipcl_bind_fanout_size = ipcl_bind_fanout_size;
4393448Sdh155122 	ipst->ips_ipcl_udp_fanout_size = ipcl_udp_fanout_size;
4403448Sdh155122 	ipst->ips_ipcl_raw_fanout_size = ipcl_raw_fanout_size;
4410Sstevel@tonic-gate 
4423448Sdh155122 	ASSERT(ipst->ips_ipcl_conn_fanout == NULL);
4433448Sdh155122 
4443448Sdh155122 	ipst->ips_ipcl_conn_fanout = kmem_zalloc(
4453448Sdh155122 	    ipst->ips_ipcl_conn_fanout_size * sizeof (connf_t), KM_SLEEP);
4463448Sdh155122 
4473448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
4483448Sdh155122 		mutex_init(&ipst->ips_ipcl_conn_fanout[i].connf_lock, NULL,
4490Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4523448Sdh155122 	ipst->ips_ipcl_bind_fanout = kmem_zalloc(
4533448Sdh155122 	    ipst->ips_ipcl_bind_fanout_size * sizeof (connf_t), KM_SLEEP);
4540Sstevel@tonic-gate 
4553448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
4563448Sdh155122 		mutex_init(&ipst->ips_ipcl_bind_fanout[i].connf_lock, NULL,
4570Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4603448Sdh155122 	ipst->ips_ipcl_proto_fanout = kmem_zalloc(IPPROTO_MAX *
4613448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4623448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4633448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout[i].connf_lock, NULL,
4640Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4650Sstevel@tonic-gate 	}
4663448Sdh155122 
4673448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = kmem_zalloc(IPPROTO_MAX *
4683448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4693448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4703448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock, NULL,
4710Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4743448Sdh155122 	ipst->ips_rts_clients = kmem_zalloc(sizeof (connf_t), KM_SLEEP);
4753448Sdh155122 	mutex_init(&ipst->ips_rts_clients->connf_lock,
4763448Sdh155122 	    NULL, MUTEX_DEFAULT, NULL);
4770Sstevel@tonic-gate 
4783448Sdh155122 	ipst->ips_ipcl_udp_fanout = kmem_zalloc(
4793448Sdh155122 	    ipst->ips_ipcl_udp_fanout_size * sizeof (connf_t), KM_SLEEP);
4803448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
4813448Sdh155122 		mutex_init(&ipst->ips_ipcl_udp_fanout[i].connf_lock, NULL,
4820Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4853448Sdh155122 	ipst->ips_ipcl_raw_fanout = kmem_zalloc(
4863448Sdh155122 	    ipst->ips_ipcl_raw_fanout_size * sizeof (connf_t), KM_SLEEP);
4873448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
4883448Sdh155122 		mutex_init(&ipst->ips_ipcl_raw_fanout[i].connf_lock, NULL,
4890Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4923448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = kmem_zalloc(
4933448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE, KM_SLEEP);
4940Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
4953448Sdh155122 		mutex_init(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock,
4963448Sdh155122 		    NULL, MUTEX_DEFAULT, NULL);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate void
5013448Sdh155122 ipcl_g_destroy(void)
5020Sstevel@tonic-gate {
5035240Snordmark 	kmem_cache_destroy(ip_conn_cache);
5045240Snordmark 	kmem_cache_destroy(tcp_conn_cache);
5055240Snordmark 	kmem_cache_destroy(udp_conn_cache);
5065240Snordmark 	kmem_cache_destroy(rawip_conn_cache);
5075240Snordmark 	kmem_cache_destroy(rts_conn_cache);
5083448Sdh155122 }
5093448Sdh155122 
5103448Sdh155122 /*
5113448Sdh155122  * All user-level and kernel use of the stack must be gone
5123448Sdh155122  * by now.
5133448Sdh155122  */
5143448Sdh155122 void
5153448Sdh155122 ipcl_destroy(ip_stack_t *ipst)
5163448Sdh155122 {
5173448Sdh155122 	int i;
5183448Sdh155122 
5193448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
5203448Sdh155122 		ASSERT(ipst->ips_ipcl_conn_fanout[i].connf_head == NULL);
5213448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_conn_fanout[i].connf_lock);
5223448Sdh155122 	}
5233448Sdh155122 	kmem_free(ipst->ips_ipcl_conn_fanout, ipst->ips_ipcl_conn_fanout_size *
5243448Sdh155122 	    sizeof (connf_t));
5253448Sdh155122 	ipst->ips_ipcl_conn_fanout = NULL;
5263448Sdh155122 
5273448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
5283448Sdh155122 		ASSERT(ipst->ips_ipcl_bind_fanout[i].connf_head == NULL);
5293448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_bind_fanout[i].connf_lock);
5303448Sdh155122 	}
5313448Sdh155122 	kmem_free(ipst->ips_ipcl_bind_fanout, ipst->ips_ipcl_bind_fanout_size *
5323448Sdh155122 	    sizeof (connf_t));
5333448Sdh155122 	ipst->ips_ipcl_bind_fanout = NULL;
5343448Sdh155122 
5353448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5363448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout[i].connf_head == NULL);
5373448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout[i].connf_lock);
5383448Sdh155122 	}
5393448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout, IPPROTO_MAX * sizeof (connf_t));
5403448Sdh155122 	ipst->ips_ipcl_proto_fanout = NULL;
5410Sstevel@tonic-gate 
5423448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5433448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout_v6[i].connf_head == NULL);
5443448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock);
5453448Sdh155122 	}
5463448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout_v6,
5473448Sdh155122 	    IPPROTO_MAX * sizeof (connf_t));
5483448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = NULL;
5493448Sdh155122 
5503448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
5513448Sdh155122 		ASSERT(ipst->ips_ipcl_udp_fanout[i].connf_head == NULL);
5523448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_udp_fanout[i].connf_lock);
5533448Sdh155122 	}
5543448Sdh155122 	kmem_free(ipst->ips_ipcl_udp_fanout, ipst->ips_ipcl_udp_fanout_size *
5553448Sdh155122 	    sizeof (connf_t));
5563448Sdh155122 	ipst->ips_ipcl_udp_fanout = NULL;
5570Sstevel@tonic-gate 
5583448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
5593448Sdh155122 		ASSERT(ipst->ips_ipcl_raw_fanout[i].connf_head == NULL);
5603448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_raw_fanout[i].connf_lock);
5613448Sdh155122 	}
5623448Sdh155122 	kmem_free(ipst->ips_ipcl_raw_fanout, ipst->ips_ipcl_raw_fanout_size *
5633448Sdh155122 	    sizeof (connf_t));
5643448Sdh155122 	ipst->ips_ipcl_raw_fanout = NULL;
5650Sstevel@tonic-gate 
5663448Sdh155122 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
5673448Sdh155122 		ASSERT(ipst->ips_ipcl_globalhash_fanout[i].connf_head == NULL);
5683448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
5693448Sdh155122 	}
5703448Sdh155122 	kmem_free(ipst->ips_ipcl_globalhash_fanout,
5713448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE);
5723448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = NULL;
5730Sstevel@tonic-gate 
5743448Sdh155122 	ASSERT(ipst->ips_rts_clients->connf_head == NULL);
5753448Sdh155122 	mutex_destroy(&ipst->ips_rts_clients->connf_lock);
5763448Sdh155122 	kmem_free(ipst->ips_rts_clients, sizeof (connf_t));
5773448Sdh155122 	ipst->ips_rts_clients = NULL;
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * conn creation routine. initialize the conn, sets the reference
5820Sstevel@tonic-gate  * and inserts it in the global hash table.
5830Sstevel@tonic-gate  */
5840Sstevel@tonic-gate conn_t *
5853448Sdh155122 ipcl_conn_create(uint32_t type, int sleep, netstack_t *ns)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate 	conn_t	*connp;
5883448Sdh155122 	sctp_stack_t *sctps;
5895240Snordmark 	struct kmem_cache *conn_cache;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	switch (type) {
5920Sstevel@tonic-gate 	case IPCL_SCTPCONN:
5930Sstevel@tonic-gate 		if ((connp = kmem_cache_alloc(sctp_conn_cache, sleep)) == NULL)
5940Sstevel@tonic-gate 			return (NULL);
5954691Skcpoon 		sctp_conn_init(connp);
5963448Sdh155122 		sctps = ns->netstack_sctp;
5973448Sdh155122 		SCTP_G_Q_REFHOLD(sctps);
5983448Sdh155122 		netstack_hold(ns);
5993448Sdh155122 		connp->conn_netstack = ns;
6005240Snordmark 		return (connp);
6015240Snordmark 
6025240Snordmark 	case IPCL_TCPCONN:
6035240Snordmark 		conn_cache = tcp_conn_cache;
6040Sstevel@tonic-gate 		break;
6055240Snordmark 
6065240Snordmark 	case IPCL_UDPCONN:
6075240Snordmark 		conn_cache = udp_conn_cache;
6085240Snordmark 		break;
6095240Snordmark 
6105240Snordmark 	case IPCL_RAWIPCONN:
6115240Snordmark 		conn_cache = rawip_conn_cache;
6125240Snordmark 		break;
6135240Snordmark 
6145240Snordmark 	case IPCL_RTSCONN:
6155240Snordmark 		conn_cache = rts_conn_cache;
6165240Snordmark 		break;
6175240Snordmark 
6180Sstevel@tonic-gate 	case IPCL_IPCCONN:
6195240Snordmark 		conn_cache = ip_conn_cache;
6200Sstevel@tonic-gate 		break;
6215240Snordmark 
622741Smasputra 	default:
623741Smasputra 		connp = NULL;
624741Smasputra 		ASSERT(0);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6275240Snordmark 	if ((connp = kmem_cache_alloc(conn_cache, sleep)) == NULL)
6285240Snordmark 		return (NULL);
6295240Snordmark 
6305240Snordmark 	connp->conn_ref = 1;
6315240Snordmark 	netstack_hold(ns);
6325240Snordmark 	connp->conn_netstack = ns;
6335240Snordmark 	ipcl_globalhash_insert(connp);
6340Sstevel@tonic-gate 	return (connp);
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate void
6380Sstevel@tonic-gate ipcl_conn_destroy(conn_t *connp)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	mblk_t	*mp;
6413448Sdh155122 	netstack_t	*ns = connp->conn_netstack;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&connp->conn_lock));
6440Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6450Sstevel@tonic-gate 	ASSERT(connp->conn_ire_cache == NULL);
6460Sstevel@tonic-gate 
647*7502Saruna@cs.umn.edu 	DTRACE_PROBE1(conn__destroy, conn_t *, connp);
648*7502Saruna@cs.umn.edu 
6491676Sjpk 	if (connp->conn_peercred != NULL &&
6501676Sjpk 	    connp->conn_peercred != connp->conn_cred)
6511676Sjpk 		crfree(connp->conn_peercred);
6521676Sjpk 	connp->conn_peercred = NULL;
6531676Sjpk 
6541676Sjpk 	if (connp->conn_cred != NULL) {
6551676Sjpk 		crfree(connp->conn_cred);
6561676Sjpk 		connp->conn_cred = NULL;
6571676Sjpk 	}
6581676Sjpk 
6590Sstevel@tonic-gate 	ipcl_globalhash_remove(connp);
6600Sstevel@tonic-gate 
6615240Snordmark 	/* FIXME: add separate tcp_conn_free()? */
6620Sstevel@tonic-gate 	if (connp->conn_flags & IPCL_TCPCONN) {
663741Smasputra 		tcp_t	*tcp = connp->conn_tcp;
6643448Sdh155122 		tcp_stack_t *tcps;
6653448Sdh155122 
6663448Sdh155122 		ASSERT(tcp != NULL);
6673448Sdh155122 		tcps = tcp->tcp_tcps;
6683448Sdh155122 		if (tcps != NULL) {
6693448Sdh155122 			if (connp->conn_latch != NULL) {
6703448Sdh155122 				IPLATCH_REFRELE(connp->conn_latch, ns);
6713448Sdh155122 				connp->conn_latch = NULL;
6723448Sdh155122 			}
6733448Sdh155122 			if (connp->conn_policy != NULL) {
6743448Sdh155122 				IPPH_REFRELE(connp->conn_policy, ns);
6753448Sdh155122 				connp->conn_policy = NULL;
6763448Sdh155122 			}
6773448Sdh155122 			tcp->tcp_tcps = NULL;
6783448Sdh155122 			TCPS_REFRELE(tcps);
6793448Sdh155122 		}
680741Smasputra 
6810Sstevel@tonic-gate 		tcp_free(tcp);
6820Sstevel@tonic-gate 		mp = tcp->tcp_timercache;
6831676Sjpk 		tcp->tcp_cred = NULL;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		if (tcp->tcp_sack_info != NULL) {
6860Sstevel@tonic-gate 			bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
6870Sstevel@tonic-gate 			kmem_cache_free(tcp_sack_info_cache,
6880Sstevel@tonic-gate 			    tcp->tcp_sack_info);
6890Sstevel@tonic-gate 		}
6900Sstevel@tonic-gate 		if (tcp->tcp_iphc != NULL) {
6910Sstevel@tonic-gate 			if (tcp->tcp_hdr_grown) {
6920Sstevel@tonic-gate 				kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len);
6930Sstevel@tonic-gate 			} else {
6940Sstevel@tonic-gate 				bzero(tcp->tcp_iphc, tcp->tcp_iphc_len);
6950Sstevel@tonic-gate 				kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc);
6960Sstevel@tonic-gate 			}
6970Sstevel@tonic-gate 			tcp->tcp_iphc_len = 0;
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 		ASSERT(tcp->tcp_iphc_len == 0);
7000Sstevel@tonic-gate 
7013448Sdh155122 		ASSERT(connp->conn_latch == NULL);
7023448Sdh155122 		ASSERT(connp->conn_policy == NULL);
7033448Sdh155122 
7043448Sdh155122 		if (ns != NULL) {
7053448Sdh155122 			ASSERT(tcp->tcp_tcps == NULL);
7063448Sdh155122 			connp->conn_netstack = NULL;
7073448Sdh155122 			netstack_rele(ns);
7083448Sdh155122 		}
7095240Snordmark 
7105240Snordmark 		ipcl_conn_cleanup(connp);
7115240Snordmark 		connp->conn_flags = IPCL_TCPCONN;
7125240Snordmark 		bzero(tcp, sizeof (tcp_t));
7135240Snordmark 
7145240Snordmark 		tcp->tcp_timercache = mp;
7155240Snordmark 		tcp->tcp_connp = connp;
7165240Snordmark 		kmem_cache_free(tcp_conn_cache, connp);
7175240Snordmark 		return;
7185240Snordmark 	}
7195240Snordmark 	if (connp->conn_latch != NULL) {
7205240Snordmark 		IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack);
7215240Snordmark 		connp->conn_latch = NULL;
7225240Snordmark 	}
7235240Snordmark 	if (connp->conn_policy != NULL) {
7245240Snordmark 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
7255240Snordmark 		connp->conn_policy = NULL;
7265240Snordmark 	}
7275240Snordmark 	if (connp->conn_ipsec_opt_mp != NULL) {
7285240Snordmark 		freemsg(connp->conn_ipsec_opt_mp);
7295240Snordmark 		connp->conn_ipsec_opt_mp = NULL;
7305240Snordmark 	}
7315240Snordmark 
7325240Snordmark 	if (connp->conn_flags & IPCL_SCTPCONN) {
7333448Sdh155122 		ASSERT(ns != NULL);
7340Sstevel@tonic-gate 		sctp_free(connp);
7355240Snordmark 		return;
7365240Snordmark 	}
7375240Snordmark 
7385240Snordmark 	if (ns != NULL) {
7395240Snordmark 		connp->conn_netstack = NULL;
7405240Snordmark 		netstack_rele(ns);
7415240Snordmark 	}
7425240Snordmark 	ipcl_conn_cleanup(connp);
7435240Snordmark 
7445240Snordmark 	/* leave conn_priv aka conn_udp, conn_icmp, etc in place. */
7455240Snordmark 	if (connp->conn_flags & IPCL_UDPCONN) {
7465240Snordmark 		connp->conn_flags = IPCL_UDPCONN;
7475240Snordmark 		kmem_cache_free(udp_conn_cache, connp);
7485240Snordmark 	} else if (connp->conn_flags & IPCL_RAWIPCONN) {
7495240Snordmark 		connp->conn_flags = IPCL_RAWIPCONN;
7505240Snordmark 		connp->conn_ulp = IPPROTO_ICMP;
7515240Snordmark 		kmem_cache_free(rawip_conn_cache, connp);
7525240Snordmark 	} else if (connp->conn_flags & IPCL_RTSCONN) {
7535240Snordmark 		connp->conn_flags = IPCL_RTSCONN;
7545240Snordmark 		kmem_cache_free(rts_conn_cache, connp);
7550Sstevel@tonic-gate 	} else {
7565240Snordmark 		connp->conn_flags = IPCL_IPCCONN;
7575240Snordmark 		ASSERT(connp->conn_flags & IPCL_IPCCONN);
7585240Snordmark 		ASSERT(connp->conn_priv == NULL);
7595240Snordmark 		kmem_cache_free(ip_conn_cache, connp);
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate /*
7640Sstevel@tonic-gate  * Running in cluster mode - deregister listener information
7650Sstevel@tonic-gate  */
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate static void
7680Sstevel@tonic-gate ipcl_conn_unlisten(conn_t *connp)
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) != 0);
7710Sstevel@tonic-gate 	ASSERT(connp->conn_lport != 0);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (cl_inet_unlisten != NULL) {
7740Sstevel@tonic-gate 		sa_family_t	addr_family;
7750Sstevel@tonic-gate 		uint8_t		*laddrp;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 		if (connp->conn_pkt_isv6) {
7780Sstevel@tonic-gate 			addr_family = AF_INET6;
7790Sstevel@tonic-gate 			laddrp = (uint8_t *)&connp->conn_bound_source_v6;
7800Sstevel@tonic-gate 		} else {
7810Sstevel@tonic-gate 			addr_family = AF_INET;
7820Sstevel@tonic-gate 			laddrp = (uint8_t *)&connp->conn_bound_source;
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 		(*cl_inet_unlisten)(IPPROTO_TCP, addr_family, laddrp,
7850Sstevel@tonic-gate 		    connp->conn_lport);
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 	connp->conn_flags &= ~IPCL_CL_LISTENER;
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate  * We set the IPCL_REMOVED flag (instead of clearing the flag indicating
7920Sstevel@tonic-gate  * which table the conn belonged to). So for debugging we can see which hash
7930Sstevel@tonic-gate  * table this connection was in.
7940Sstevel@tonic-gate  */
7950Sstevel@tonic-gate #define	IPCL_HASH_REMOVE(connp)	{					\
7960Sstevel@tonic-gate 	connf_t	*connfp = (connp)->conn_fanout;				\
7970Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&((connp)->conn_lock)));			\
7980Sstevel@tonic-gate 	if (connfp != NULL) {						\
7990Sstevel@tonic-gate 		IPCL_DEBUG_LVL(4, ("IPCL_HASH_REMOVE: connp %p",	\
8000Sstevel@tonic-gate 		    (void *)(connp)));					\
8010Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);			\
8020Sstevel@tonic-gate 		if ((connp)->conn_next != NULL)				\
8030Sstevel@tonic-gate 			(connp)->conn_next->conn_prev =			\
8040Sstevel@tonic-gate 			    (connp)->conn_prev;				\
8050Sstevel@tonic-gate 		if ((connp)->conn_prev != NULL)				\
8060Sstevel@tonic-gate 			(connp)->conn_prev->conn_next =			\
8070Sstevel@tonic-gate 			    (connp)->conn_next;				\
8080Sstevel@tonic-gate 		else							\
8090Sstevel@tonic-gate 			connfp->connf_head = (connp)->conn_next;	\
8100Sstevel@tonic-gate 		(connp)->conn_fanout = NULL;				\
8110Sstevel@tonic-gate 		(connp)->conn_next = NULL;				\
8120Sstevel@tonic-gate 		(connp)->conn_prev = NULL;				\
8130Sstevel@tonic-gate 		(connp)->conn_flags |= IPCL_REMOVED;			\
8140Sstevel@tonic-gate 		if (((connp)->conn_flags & IPCL_CL_LISTENER) != 0)	\
8150Sstevel@tonic-gate 			ipcl_conn_unlisten((connp));			\
8160Sstevel@tonic-gate 		CONN_DEC_REF((connp));					\
8170Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);			\
8180Sstevel@tonic-gate 	}								\
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate void
8220Sstevel@tonic-gate ipcl_hash_remove(conn_t *connp)
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate 	IPCL_HASH_REMOVE(connp);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate  * The whole purpose of this function is allow removal of
8290Sstevel@tonic-gate  * a conn_t from the connected hash for timewait reclaim.
8300Sstevel@tonic-gate  * This is essentially a TW reclaim fastpath where timewait
8310Sstevel@tonic-gate  * collector checks under fanout lock (so no one else can
8320Sstevel@tonic-gate  * get access to the conn_t) that refcnt is 2 i.e. one for
8330Sstevel@tonic-gate  * TCP and one for the classifier hash list. If ref count
8340Sstevel@tonic-gate  * is indeed 2, we can just remove the conn under lock and
8350Sstevel@tonic-gate  * avoid cleaning up the conn under squeue. This gives us
8360Sstevel@tonic-gate  * improved performance.
8370Sstevel@tonic-gate  */
8380Sstevel@tonic-gate void
8390Sstevel@tonic-gate ipcl_hash_remove_locked(conn_t *connp, connf_t	*connfp)
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connfp->connf_lock));
8420Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
8430Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) == 0);
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	if ((connp)->conn_next != NULL) {
8464691Skcpoon 		(connp)->conn_next->conn_prev = (connp)->conn_prev;
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 	if ((connp)->conn_prev != NULL) {
8494691Skcpoon 		(connp)->conn_prev->conn_next = (connp)->conn_next;
8500Sstevel@tonic-gate 	} else {
8510Sstevel@tonic-gate 		connfp->connf_head = (connp)->conn_next;
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 	(connp)->conn_fanout = NULL;
8540Sstevel@tonic-gate 	(connp)->conn_next = NULL;
8550Sstevel@tonic-gate 	(connp)->conn_prev = NULL;
8560Sstevel@tonic-gate 	(connp)->conn_flags |= IPCL_REMOVED;
8570Sstevel@tonic-gate 	ASSERT((connp)->conn_ref == 2);
8580Sstevel@tonic-gate 	(connp)->conn_ref--;
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp) {		\
8620Sstevel@tonic-gate 	ASSERT((connp)->conn_fanout == NULL);				\
8630Sstevel@tonic-gate 	ASSERT((connp)->conn_next == NULL);				\
8640Sstevel@tonic-gate 	ASSERT((connp)->conn_prev == NULL);				\
8650Sstevel@tonic-gate 	if ((connfp)->connf_head != NULL) {				\
8660Sstevel@tonic-gate 		(connfp)->connf_head->conn_prev = (connp);		\
8670Sstevel@tonic-gate 		(connp)->conn_next = (connfp)->connf_head;		\
8680Sstevel@tonic-gate 	}								\
8690Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8700Sstevel@tonic-gate 	(connfp)->connf_head = (connp);					\
8710Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8720Sstevel@tonic-gate 	    IPCL_CONNECTED;						\
8730Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED(connfp, connp) {			\
8770Sstevel@tonic-gate 	IPCL_DEBUG_LVL(8, ("IPCL_HASH_INSERT_CONNECTED: connfp %p "	\
8780Sstevel@tonic-gate 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
8790Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8800Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8810Sstevel@tonic-gate 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);		\
8820Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate #define	IPCL_HASH_INSERT_BOUND(connfp, connp) {				\
8860Sstevel@tonic-gate 	conn_t *pconnp = NULL, *nconnp;					\
8870Sstevel@tonic-gate 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_BOUND: connfp %p "	\
8880Sstevel@tonic-gate 	    "connp %p", (void *)connfp, (void *)(connp)));		\
8890Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8900Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8910Sstevel@tonic-gate 	nconnp = (connfp)->connf_head;					\
892153Sethindra 	while (nconnp != NULL &&					\
893153Sethindra 	    !_IPCL_V4_MATCH_ANY(nconnp->conn_srcv6)) {			\
894153Sethindra 		pconnp = nconnp;					\
895153Sethindra 		nconnp = nconnp->conn_next;				\
8960Sstevel@tonic-gate 	}								\
8970Sstevel@tonic-gate 	if (pconnp != NULL) {						\
8980Sstevel@tonic-gate 		pconnp->conn_next = (connp);				\
8990Sstevel@tonic-gate 		(connp)->conn_prev = pconnp;				\
9000Sstevel@tonic-gate 	} else {							\
9010Sstevel@tonic-gate 		(connfp)->connf_head = (connp);				\
9020Sstevel@tonic-gate 	}								\
9030Sstevel@tonic-gate 	if (nconnp != NULL) {						\
9040Sstevel@tonic-gate 		(connp)->conn_next = nconnp;				\
9050Sstevel@tonic-gate 		nconnp->conn_prev = (connp);				\
9060Sstevel@tonic-gate 	}								\
9070Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9080Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9090Sstevel@tonic-gate 	    IPCL_BOUND;							\
9100Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
9110Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate #define	IPCL_HASH_INSERT_WILDCARD(connfp, connp) {			\
9150Sstevel@tonic-gate 	conn_t **list, *prev, *next;					\
9160Sstevel@tonic-gate 	boolean_t isv4mapped =						\
9170Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&(connp)->conn_srcv6);			\
9180Sstevel@tonic-gate 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_WILDCARD: connfp %p "	\
9190Sstevel@tonic-gate 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
9200Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
9210Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
9220Sstevel@tonic-gate 	list = &(connfp)->connf_head;					\
9230Sstevel@tonic-gate 	prev = NULL;							\
9240Sstevel@tonic-gate 	while ((next = *list) != NULL) {				\
9250Sstevel@tonic-gate 		if (isv4mapped &&					\
9260Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&next->conn_srcv6) &&	\
9270Sstevel@tonic-gate 		    connp->conn_zoneid == next->conn_zoneid) {		\
9280Sstevel@tonic-gate 			(connp)->conn_next = next;			\
9290Sstevel@tonic-gate 			if (prev != NULL)				\
9300Sstevel@tonic-gate 				prev = next->conn_prev;			\
9310Sstevel@tonic-gate 			next->conn_prev = (connp);			\
9320Sstevel@tonic-gate 			break;						\
9330Sstevel@tonic-gate 		}							\
9340Sstevel@tonic-gate 		list = &next->conn_next;				\
9350Sstevel@tonic-gate 		prev = next;						\
9360Sstevel@tonic-gate 	}								\
9370Sstevel@tonic-gate 	(connp)->conn_prev = prev;					\
9380Sstevel@tonic-gate 	*list = (connp);						\
9390Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9400Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9410Sstevel@tonic-gate 	    IPCL_BOUND;							\
9420Sstevel@tonic-gate 	CONN_INC_REF((connp));						\
9430Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate void
9470Sstevel@tonic-gate ipcl_hash_insert_wildcard(connf_t *connfp, conn_t *connp)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate void
9530Sstevel@tonic-gate ipcl_proto_insert(conn_t *connp, uint8_t protocol)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	connf_t	*connfp;
9563448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	ASSERT(connp != NULL);
9591676Sjpk 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
9601676Sjpk 	    protocol == IPPROTO_ESP);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	connp->conn_ulp = protocol;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/* Insert it in the protocol hash */
9653448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[protocol];
9660Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate void
9700Sstevel@tonic-gate ipcl_proto_insert_v6(conn_t *connp, uint8_t protocol)
9710Sstevel@tonic-gate {
9720Sstevel@tonic-gate 	connf_t	*connfp;
9733448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	ASSERT(connp != NULL);
9761676Sjpk 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
9771676Sjpk 	    protocol == IPPROTO_ESP);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	connp->conn_ulp = protocol;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	/* Insert it in the Bind Hash */
9823448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
9830Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate /*
9870Sstevel@tonic-gate  * This function is used only for inserting SCTP raw socket now.
9880Sstevel@tonic-gate  * This may change later.
9890Sstevel@tonic-gate  *
9900Sstevel@tonic-gate  * Note that only one raw socket can be bound to a port.  The param
9910Sstevel@tonic-gate  * lport is in network byte order.
9920Sstevel@tonic-gate  */
9930Sstevel@tonic-gate static int
9940Sstevel@tonic-gate ipcl_sctp_hash_insert(conn_t *connp, in_port_t lport)
9950Sstevel@tonic-gate {
9960Sstevel@tonic-gate 	connf_t	*connfp;
9970Sstevel@tonic-gate 	conn_t	*oconnp;
9983448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
9990Sstevel@tonic-gate 
10003448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	/* Check for existing raw socket already bound to the port. */
10030Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
10040Sstevel@tonic-gate 	for (oconnp = connfp->connf_head; oconnp != NULL;
1005409Skcpoon 	    oconnp = oconnp->conn_next) {
10060Sstevel@tonic-gate 		if (oconnp->conn_lport == lport &&
10070Sstevel@tonic-gate 		    oconnp->conn_zoneid == connp->conn_zoneid &&
10080Sstevel@tonic-gate 		    oconnp->conn_af_isv6 == connp->conn_af_isv6 &&
10090Sstevel@tonic-gate 		    ((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
10100Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&oconnp->conn_srcv6) ||
10110Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6) ||
10120Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&oconnp->conn_srcv6)) ||
10130Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&oconnp->conn_srcv6,
10140Sstevel@tonic-gate 		    &connp->conn_srcv6))) {
10150Sstevel@tonic-gate 			break;
10160Sstevel@tonic-gate 		}
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
10190Sstevel@tonic-gate 	if (oconnp != NULL)
10200Sstevel@tonic-gate 		return (EADDRNOTAVAIL);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6) ||
10230Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_remv6)) {
10240Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
10250Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6)) {
10260Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10270Sstevel@tonic-gate 		} else {
10280Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
10290Sstevel@tonic-gate 		}
10300Sstevel@tonic-gate 	} else {
10310Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED(connfp, connp);
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 	return (0);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate /*
10371676Sjpk  * Check for a MAC exemption conflict on a labeled system.  Note that for
10381676Sjpk  * protocols that use port numbers (UDP, TCP, SCTP), we do this check up in the
10391676Sjpk  * transport layer.  This check is for binding all other protocols.
10401676Sjpk  *
10411676Sjpk  * Returns true if there's a conflict.
10421676Sjpk  */
10431676Sjpk static boolean_t
10443448Sdh155122 check_exempt_conflict_v4(conn_t *connp, ip_stack_t *ipst)
10451676Sjpk {
10461676Sjpk 	connf_t	*connfp;
10471676Sjpk 	conn_t *tconn;
10481676Sjpk 
10493448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[connp->conn_ulp];
10501676Sjpk 	mutex_enter(&connfp->connf_lock);
10511676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
10521676Sjpk 	    tconn = tconn->conn_next) {
10531676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
10541676Sjpk 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
10551676Sjpk 			continue;
10561676Sjpk 		/* If neither is exempt, then there's no conflict */
10571676Sjpk 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
10581676Sjpk 			continue;
10591676Sjpk 		/* If both are bound to different specific addrs, ok */
10601676Sjpk 		if (connp->conn_src != INADDR_ANY &&
10611676Sjpk 		    tconn->conn_src != INADDR_ANY &&
10621676Sjpk 		    connp->conn_src != tconn->conn_src)
10631676Sjpk 			continue;
10641676Sjpk 		/* These two conflict; fail */
10651676Sjpk 		break;
10661676Sjpk 	}
10671676Sjpk 	mutex_exit(&connfp->connf_lock);
10681676Sjpk 	return (tconn != NULL);
10691676Sjpk }
10701676Sjpk 
10711676Sjpk static boolean_t
10723448Sdh155122 check_exempt_conflict_v6(conn_t *connp, ip_stack_t *ipst)
10731676Sjpk {
10741676Sjpk 	connf_t	*connfp;
10751676Sjpk 	conn_t *tconn;
10761676Sjpk 
10773448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[connp->conn_ulp];
10781676Sjpk 	mutex_enter(&connfp->connf_lock);
10791676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
10801676Sjpk 	    tconn = tconn->conn_next) {
10811676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
10821676Sjpk 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
10831676Sjpk 			continue;
10841676Sjpk 		/* If neither is exempt, then there's no conflict */
10851676Sjpk 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
10861676Sjpk 			continue;
10871676Sjpk 		/* If both are bound to different addrs, ok */
10881676Sjpk 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) &&
10891676Sjpk 		    !IN6_IS_ADDR_UNSPECIFIED(&tconn->conn_srcv6) &&
10901676Sjpk 		    !IN6_ARE_ADDR_EQUAL(&connp->conn_srcv6, &tconn->conn_srcv6))
10911676Sjpk 			continue;
10921676Sjpk 		/* These two conflict; fail */
10931676Sjpk 		break;
10941676Sjpk 	}
10951676Sjpk 	mutex_exit(&connfp->connf_lock);
10961676Sjpk 	return (tconn != NULL);
10971676Sjpk }
10981676Sjpk 
10991676Sjpk /*
11000Sstevel@tonic-gate  * (v4, v6) bind hash insertion routines
11010Sstevel@tonic-gate  */
11020Sstevel@tonic-gate int
11030Sstevel@tonic-gate ipcl_bind_insert(conn_t *connp, uint8_t protocol, ipaddr_t src, uint16_t lport)
11040Sstevel@tonic-gate {
11050Sstevel@tonic-gate 	connf_t	*connfp;
11060Sstevel@tonic-gate #ifdef	IPCL_DEBUG
11070Sstevel@tonic-gate 	char	buf[INET_NTOA_BUFSIZE];
11080Sstevel@tonic-gate #endif
11090Sstevel@tonic-gate 	int	ret = 0;
11103448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	ASSERT(connp);
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	IPCL_DEBUG_LVL(64, ("ipcl_bind_insert: connp %p, src = %s, "
11150Sstevel@tonic-gate 	    "port = %d\n", (void *)connp, inet_ntoa_r(src, buf), lport));
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	connp->conn_ulp = protocol;
11180Sstevel@tonic-gate 	IN6_IPADDR_TO_V4MAPPED(src, &connp->conn_srcv6);
11190Sstevel@tonic-gate 	connp->conn_lport = lport;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	switch (protocol) {
11221676Sjpk 	default:
11233448Sdh155122 		if (is_system_labeled() &&
11243448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
11251676Sjpk 			return (EADDRINUSE);
11261676Sjpk 		/* FALLTHROUGH */
11270Sstevel@tonic-gate 	case IPPROTO_UDP:
11280Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
11290Sstevel@tonic-gate 			IPCL_DEBUG_LVL(64,
11300Sstevel@tonic-gate 			    ("ipcl_bind_insert: connp %p - udp\n",
11310Sstevel@tonic-gate 			    (void *)connp));
11323448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
11333448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
11340Sstevel@tonic-gate 		} else {
11350Sstevel@tonic-gate 			IPCL_DEBUG_LVL(64,
11360Sstevel@tonic-gate 			    ("ipcl_bind_insert: connp %p - protocol\n",
11370Sstevel@tonic-gate 			    (void *)connp));
11383448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout[protocol];
11390Sstevel@tonic-gate 		}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 		if (connp->conn_rem != INADDR_ANY) {
11420Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
11430Sstevel@tonic-gate 		} else if (connp->conn_src != INADDR_ANY) {
11440Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
11450Sstevel@tonic-gate 		} else {
11460Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
11470Sstevel@tonic-gate 		}
11480Sstevel@tonic-gate 		break;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	case IPPROTO_TCP:
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
11531676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
11543448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
11553448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
11560Sstevel@tonic-gate 		if (connp->conn_src != INADDR_ANY) {
11570Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
11580Sstevel@tonic-gate 		} else {
11590Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
11600Sstevel@tonic-gate 		}
11610Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
11620Sstevel@tonic-gate 			ASSERT(!connp->conn_pkt_isv6);
11630Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
11640Sstevel@tonic-gate 			(*cl_inet_listen)(IPPROTO_TCP, AF_INET,
11650Sstevel@tonic-gate 			    (uint8_t *)&connp->conn_bound_source, lport);
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 		break;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	case IPPROTO_SCTP:
11700Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
11710Sstevel@tonic-gate 		break;
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	return (ret);
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate int
11780Sstevel@tonic-gate ipcl_bind_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
11790Sstevel@tonic-gate     uint16_t lport)
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate 	connf_t	*connfp;
11820Sstevel@tonic-gate 	int	ret = 0;
11833448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	ASSERT(connp);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	connp->conn_ulp = protocol;
11880Sstevel@tonic-gate 	connp->conn_srcv6 = *src;
11890Sstevel@tonic-gate 	connp->conn_lport = lport;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	switch (protocol) {
11921676Sjpk 	default:
11933448Sdh155122 		if (is_system_labeled() &&
11943448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
11951676Sjpk 			return (EADDRINUSE);
11961676Sjpk 		/* FALLTHROUGH */
11970Sstevel@tonic-gate 	case IPPROTO_UDP:
11980Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
11990Sstevel@tonic-gate 			IPCL_DEBUG_LVL(128,
12000Sstevel@tonic-gate 			    ("ipcl_bind_insert_v6: connp %p - udp\n",
12010Sstevel@tonic-gate 			    (void *)connp));
12023448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
12033448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
12040Sstevel@tonic-gate 		} else {
12050Sstevel@tonic-gate 			IPCL_DEBUG_LVL(128,
12060Sstevel@tonic-gate 			    ("ipcl_bind_insert_v6: connp %p - protocol\n",
12070Sstevel@tonic-gate 			    (void *)connp));
12083448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
12090Sstevel@tonic-gate 		}
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
12120Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
12130Sstevel@tonic-gate 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
12140Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12150Sstevel@tonic-gate 		} else {
12160Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 		break;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	case IPPROTO_TCP:
12210Sstevel@tonic-gate 		/* XXX - Need a separate table for IN6_IS_ADDR_UNSPECIFIED? */
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
12241676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
12253448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
12263448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
12270Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
12280Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12290Sstevel@tonic-gate 		} else {
12300Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12310Sstevel@tonic-gate 		}
12320Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
12330Sstevel@tonic-gate 			sa_family_t	addr_family;
12340Sstevel@tonic-gate 			uint8_t		*laddrp;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 			if (connp->conn_pkt_isv6) {
12370Sstevel@tonic-gate 				addr_family = AF_INET6;
12380Sstevel@tonic-gate 				laddrp =
12390Sstevel@tonic-gate 				    (uint8_t *)&connp->conn_bound_source_v6;
12400Sstevel@tonic-gate 			} else {
12410Sstevel@tonic-gate 				addr_family = AF_INET;
12420Sstevel@tonic-gate 				laddrp = (uint8_t *)&connp->conn_bound_source;
12430Sstevel@tonic-gate 			}
12440Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
12450Sstevel@tonic-gate 			(*cl_inet_listen)(IPPROTO_TCP, addr_family, laddrp,
12460Sstevel@tonic-gate 			    lport);
12470Sstevel@tonic-gate 		}
12480Sstevel@tonic-gate 		break;
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	case IPPROTO_SCTP:
12510Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
12520Sstevel@tonic-gate 		break;
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	return (ret);
12560Sstevel@tonic-gate }
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate /*
12590Sstevel@tonic-gate  * ipcl_conn_hash insertion routines.
12600Sstevel@tonic-gate  */
12610Sstevel@tonic-gate int
12620Sstevel@tonic-gate ipcl_conn_insert(conn_t *connp, uint8_t protocol, ipaddr_t src,
12630Sstevel@tonic-gate     ipaddr_t rem, uint32_t ports)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	connf_t		*connfp;
12660Sstevel@tonic-gate 	uint16_t	*up;
12670Sstevel@tonic-gate 	conn_t		*tconnp;
12680Sstevel@tonic-gate #ifdef	IPCL_DEBUG
12690Sstevel@tonic-gate 	char	sbuf[INET_NTOA_BUFSIZE], rbuf[INET_NTOA_BUFSIZE];
12700Sstevel@tonic-gate #endif
12710Sstevel@tonic-gate 	in_port_t	lport;
12720Sstevel@tonic-gate 	int		ret = 0;
12733448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	IPCL_DEBUG_LVL(256, ("ipcl_conn_insert: connp %p, src = %s, "
12760Sstevel@tonic-gate 	    "dst = %s, ports = %x, protocol = %x", (void *)connp,
12770Sstevel@tonic-gate 	    inet_ntoa_r(src, sbuf), inet_ntoa_r(rem, rbuf),
12780Sstevel@tonic-gate 	    ports, protocol));
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	switch (protocol) {
12810Sstevel@tonic-gate 	case IPPROTO_TCP:
12820Sstevel@tonic-gate 		if (!(connp->conn_flags & IPCL_EAGER)) {
12830Sstevel@tonic-gate 			/*
12840Sstevel@tonic-gate 			 * for a eager connection, i.e connections which
12850Sstevel@tonic-gate 			 * have just been created, the initialization is
12860Sstevel@tonic-gate 			 * already done in ip at conn_creation time, so
12870Sstevel@tonic-gate 			 * we can skip the checks here.
12880Sstevel@tonic-gate 			 */
12890Sstevel@tonic-gate 			IPCL_CONN_INIT(connp, protocol, src, rem, ports);
12900Sstevel@tonic-gate 		}
12913448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
12923448Sdh155122 		    IPCL_CONN_HASH(connp->conn_rem,
12933448Sdh155122 		    connp->conn_ports, ipst)];
12940Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
12950Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
12960Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
12970Sstevel@tonic-gate 			if (IPCL_CONN_MATCH(tconnp, connp->conn_ulp,
12980Sstevel@tonic-gate 			    connp->conn_rem, connp->conn_src,
12990Sstevel@tonic-gate 			    connp->conn_ports)) {
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 				/* Already have a conn. bail out */
13020Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
13030Sstevel@tonic-gate 				return (EADDRINUSE);
13040Sstevel@tonic-gate 			}
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
13070Sstevel@tonic-gate 			/*
13080Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
13090Sstevel@tonic-gate 			 * rebind. Let it happen.
13100Sstevel@tonic-gate 			 */
13110Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
13120Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
13130Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
13140Sstevel@tonic-gate 		}
13153104Sjprakash 
13163104Sjprakash 		ASSERT(connp->conn_recv != NULL);
13173104Sjprakash 
13180Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
13190Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
13200Sstevel@tonic-gate 		break;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	case IPPROTO_SCTP:
1323409Skcpoon 		/*
1324409Skcpoon 		 * The raw socket may have already been bound, remove it
1325409Skcpoon 		 * from the hash first.
1326409Skcpoon 		 */
1327409Skcpoon 		IPCL_HASH_REMOVE(connp);
1328409Skcpoon 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
13290Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
13300Sstevel@tonic-gate 		break;
13310Sstevel@tonic-gate 
13321676Sjpk 	default:
13331676Sjpk 		/*
13341676Sjpk 		 * Check for conflicts among MAC exempt bindings.  For
13351676Sjpk 		 * transports with port numbers, this is done by the upper
13361676Sjpk 		 * level per-transport binding logic.  For all others, it's
13371676Sjpk 		 * done here.
13381676Sjpk 		 */
13393448Sdh155122 		if (is_system_labeled() &&
13403448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
13411676Sjpk 			return (EADDRINUSE);
13421676Sjpk 		/* FALLTHROUGH */
13431676Sjpk 
13440Sstevel@tonic-gate 	case IPPROTO_UDP:
13450Sstevel@tonic-gate 		up = (uint16_t *)&ports;
13460Sstevel@tonic-gate 		IPCL_CONN_INIT(connp, protocol, src, rem, ports);
13470Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
13483448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
13493448Sdh155122 			    IPCL_UDP_HASH(up[1], ipst)];
13500Sstevel@tonic-gate 		} else {
13513448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout[protocol];
13520Sstevel@tonic-gate 		}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 		if (connp->conn_rem != INADDR_ANY) {
13550Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
13560Sstevel@tonic-gate 		} else if (connp->conn_src != INADDR_ANY) {
13570Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
13580Sstevel@tonic-gate 		} else {
13590Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
13600Sstevel@tonic-gate 		}
13610Sstevel@tonic-gate 		break;
13620Sstevel@tonic-gate 	}
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	return (ret);
13650Sstevel@tonic-gate }
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate int
13680Sstevel@tonic-gate ipcl_conn_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
13690Sstevel@tonic-gate     const in6_addr_t *rem, uint32_t ports, uint_t ifindex)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	connf_t		*connfp;
13720Sstevel@tonic-gate 	uint16_t	*up;
13730Sstevel@tonic-gate 	conn_t		*tconnp;
13740Sstevel@tonic-gate 	in_port_t	lport;
13750Sstevel@tonic-gate 	int		ret = 0;
13763448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	switch (protocol) {
13790Sstevel@tonic-gate 	case IPPROTO_TCP:
13800Sstevel@tonic-gate 		/* Just need to insert a conn struct */
13810Sstevel@tonic-gate 		if (!(connp->conn_flags & IPCL_EAGER)) {
13820Sstevel@tonic-gate 			IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
13830Sstevel@tonic-gate 		}
13843448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
13853448Sdh155122 		    IPCL_CONN_HASH_V6(connp->conn_remv6, connp->conn_ports,
13863448Sdh155122 		    ipst)];
13870Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
13880Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
13890Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
13900Sstevel@tonic-gate 			if (IPCL_CONN_MATCH_V6(tconnp, connp->conn_ulp,
13910Sstevel@tonic-gate 			    connp->conn_remv6, connp->conn_srcv6,
13920Sstevel@tonic-gate 			    connp->conn_ports) &&
13930Sstevel@tonic-gate 			    (tconnp->conn_tcp->tcp_bound_if == 0 ||
13940Sstevel@tonic-gate 			    tconnp->conn_tcp->tcp_bound_if == ifindex)) {
13950Sstevel@tonic-gate 				/* Already have a conn. bail out */
13960Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
13970Sstevel@tonic-gate 				return (EADDRINUSE);
13980Sstevel@tonic-gate 			}
13990Sstevel@tonic-gate 		}
14000Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
14010Sstevel@tonic-gate 			/*
14020Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
14030Sstevel@tonic-gate 			 * rebind. Let it happen.
14040Sstevel@tonic-gate 			 */
14050Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
14060Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
14070Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
14080Sstevel@tonic-gate 		}
14090Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
14100Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
14110Sstevel@tonic-gate 		break;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	case IPPROTO_SCTP:
1414409Skcpoon 		IPCL_HASH_REMOVE(connp);
1415409Skcpoon 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
14160Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
14170Sstevel@tonic-gate 		break;
14180Sstevel@tonic-gate 
14191676Sjpk 	default:
14203448Sdh155122 		if (is_system_labeled() &&
14213448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
14221676Sjpk 			return (EADDRINUSE);
14231676Sjpk 		/* FALLTHROUGH */
14240Sstevel@tonic-gate 	case IPPROTO_UDP:
14250Sstevel@tonic-gate 		up = (uint16_t *)&ports;
14260Sstevel@tonic-gate 		IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
14270Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
14283448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
14293448Sdh155122 			    IPCL_UDP_HASH(up[1], ipst)];
14300Sstevel@tonic-gate 		} else {
14313448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
14320Sstevel@tonic-gate 		}
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
14350Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
14360Sstevel@tonic-gate 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
14370Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
14380Sstevel@tonic-gate 		} else {
14390Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
14400Sstevel@tonic-gate 		}
14410Sstevel@tonic-gate 		break;
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	return (ret);
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate /*
14480Sstevel@tonic-gate  * v4 packet classifying function. looks up the fanout table to
14490Sstevel@tonic-gate  * find the conn, the packet belongs to. returns the conn with
14500Sstevel@tonic-gate  * the reference held, null otherwise.
14511676Sjpk  *
14521676Sjpk  * If zoneid is ALL_ZONES, then the search rules described in the "Connection
14531676Sjpk  * Lookup" comment block are applied.  Labels are also checked as described
14541676Sjpk  * above.  If the packet is from the inside (looped back), and is from the same
14551676Sjpk  * zone, then label checks are omitted.
14560Sstevel@tonic-gate  */
14570Sstevel@tonic-gate conn_t *
14583448Sdh155122 ipcl_classify_v4(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid,
14593448Sdh155122     ip_stack_t *ipst)
14600Sstevel@tonic-gate {
14610Sstevel@tonic-gate 	ipha_t	*ipha;
14620Sstevel@tonic-gate 	connf_t	*connfp, *bind_connfp;
14630Sstevel@tonic-gate 	uint16_t lport;
14640Sstevel@tonic-gate 	uint16_t fport;
14650Sstevel@tonic-gate 	uint32_t ports;
14660Sstevel@tonic-gate 	conn_t	*connp;
14670Sstevel@tonic-gate 	uint16_t  *up;
14681676Sjpk 	boolean_t shared_addr;
14691676Sjpk 	boolean_t unlabeled;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
14720Sstevel@tonic-gate 	up = (uint16_t *)((uchar_t *)ipha + hdr_len + TCP_PORTS_OFFSET);
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	switch (protocol) {
14750Sstevel@tonic-gate 	case IPPROTO_TCP:
14760Sstevel@tonic-gate 		ports = *(uint32_t *)up;
14770Sstevel@tonic-gate 		connfp =
14783448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_src,
14793448Sdh155122 		    ports, ipst)];
14800Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
14810Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
14820Sstevel@tonic-gate 		    connp = connp->conn_next) {
14830Sstevel@tonic-gate 			if (IPCL_CONN_MATCH(connp, protocol,
14840Sstevel@tonic-gate 			    ipha->ipha_src, ipha->ipha_dst, ports))
14850Sstevel@tonic-gate 				break;
14860Sstevel@tonic-gate 		}
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 		if (connp != NULL) {
14891676Sjpk 			/*
14901676Sjpk 			 * We have a fully-bound TCP connection.
14911676Sjpk 			 *
14921676Sjpk 			 * For labeled systems, there's no need to check the
14931676Sjpk 			 * label here.  It's known to be good as we checked
14941676Sjpk 			 * before allowing the connection to become bound.
14951676Sjpk 			 */
14960Sstevel@tonic-gate 			CONN_INC_REF(connp);
14970Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
14980Sstevel@tonic-gate 			return (connp);
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 		lport = up[1];
15041676Sjpk 		unlabeled = B_FALSE;
15051676Sjpk 		/* Cred cannot be null on IPv4 */
15061676Sjpk 		if (is_system_labeled())
15071676Sjpk 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
15081676Sjpk 			    TSLF_UNLABELED) != 0;
15091676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
15101676Sjpk 		if (shared_addr) {
15113448Sdh155122 			/*
15123448Sdh155122 			 * No need to handle exclusive-stack zones since
15133448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
15143448Sdh155122 			 */
15151676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
15161676Sjpk 			/*
15171676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
15181676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
15191676Sjpk 			 * search for the zone based on the packet label.
15201676Sjpk 			 *
15211676Sjpk 			 * If there is such a zone, we prefer to find a
15221676Sjpk 			 * connection in it.  Otherwise, we look for a
15231676Sjpk 			 * MAC-exempt connection in any zone whose label
15241676Sjpk 			 * dominates the default label on the packet.
15251676Sjpk 			 */
15261676Sjpk 			if (zoneid == ALL_ZONES)
15271676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
15281676Sjpk 			else
15291676Sjpk 				unlabeled = B_FALSE;
15301676Sjpk 		}
15311676Sjpk 
15323448Sdh155122 		bind_connfp =
15333448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
15340Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
15350Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
15360Sstevel@tonic-gate 		    connp = connp->conn_next) {
15371676Sjpk 			if (IPCL_BIND_MATCH(connp, protocol, ipha->ipha_dst,
15382263Ssommerfe 			    lport) && (IPCL_ZONE_MATCH(connp, zoneid) ||
15391676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
15400Sstevel@tonic-gate 				break;
15410Sstevel@tonic-gate 		}
15420Sstevel@tonic-gate 
15431676Sjpk 		/*
15441676Sjpk 		 * If the matching connection is SLP on a private address, then
15451676Sjpk 		 * the label on the packet must match the local zone's label.
15461676Sjpk 		 * Otherwise, it must be in the label range defined by tnrh.
15471676Sjpk 		 * This is ensured by tsol_receive_label.
15481676Sjpk 		 */
15491676Sjpk 		if (connp != NULL && is_system_labeled() &&
15501676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
15511676Sjpk 		    shared_addr, connp)) {
15521676Sjpk 				DTRACE_PROBE3(
15531676Sjpk 				    tx__ip__log__info__classify__tcp,
15541676Sjpk 				    char *,
15551676Sjpk 				    "connp(1) could not receive mp(2)",
15561676Sjpk 				    conn_t *, connp, mblk_t *, mp);
15571676Sjpk 			connp = NULL;
15581676Sjpk 		}
15591676Sjpk 
15600Sstevel@tonic-gate 		if (connp != NULL) {
15611676Sjpk 			/* Have a listener at least */
15620Sstevel@tonic-gate 			CONN_INC_REF(connp);
15630Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
15640Sstevel@tonic-gate 			return (connp);
15650Sstevel@tonic-gate 		}
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
15700Sstevel@tonic-gate 		    ("ipcl_classify: couldn't classify mp = %p\n",
15710Sstevel@tonic-gate 		    (void *)mp));
15720Sstevel@tonic-gate 		break;
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	case IPPROTO_UDP:
15750Sstevel@tonic-gate 		lport = up[1];
15761676Sjpk 		unlabeled = B_FALSE;
15771676Sjpk 		/* Cred cannot be null on IPv4 */
15781676Sjpk 		if (is_system_labeled())
15791676Sjpk 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
15801676Sjpk 			    TSLF_UNLABELED) != 0;
15811676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
15821676Sjpk 		if (shared_addr) {
15833448Sdh155122 			/*
15843448Sdh155122 			 * No need to handle exclusive-stack zones since
15853448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
15863448Sdh155122 			 */
15871676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
15881676Sjpk 			/*
15891676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
15901676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
15911676Sjpk 			 * search for the zone based on the packet label.
15921676Sjpk 			 *
15931676Sjpk 			 * If there is such a zone, we prefer to find a
15941676Sjpk 			 * connection in it.  Otherwise, we look for a
15951676Sjpk 			 * MAC-exempt connection in any zone whose label
15961676Sjpk 			 * dominates the default label on the packet.
15971676Sjpk 			 */
15981676Sjpk 			if (zoneid == ALL_ZONES)
15991676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
16001676Sjpk 			else
16011676Sjpk 				unlabeled = B_FALSE;
16021676Sjpk 		}
16030Sstevel@tonic-gate 		fport = up[0];
16040Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify %x %x", lport, fport));
16053448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
16060Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16070Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16080Sstevel@tonic-gate 		    connp = connp->conn_next) {
16090Sstevel@tonic-gate 			if (IPCL_UDP_MATCH(connp, lport, ipha->ipha_dst,
16100Sstevel@tonic-gate 			    fport, ipha->ipha_src) &&
16112263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
16121676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
16130Sstevel@tonic-gate 				break;
16140Sstevel@tonic-gate 		}
16150Sstevel@tonic-gate 
16161676Sjpk 		if (connp != NULL && is_system_labeled() &&
16171676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
16181676Sjpk 		    shared_addr, connp)) {
16191676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp,
16201676Sjpk 			    char *, "connp(1) could not receive mp(2)",
16211676Sjpk 			    conn_t *, connp, mblk_t *, mp);
16221676Sjpk 			connp = NULL;
16231676Sjpk 		}
16241676Sjpk 
16250Sstevel@tonic-gate 		if (connp != NULL) {
16260Sstevel@tonic-gate 			CONN_INC_REF(connp);
16270Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
16280Sstevel@tonic-gate 			return (connp);
16290Sstevel@tonic-gate 		}
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 		/*
16320Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
16330Sstevel@tonic-gate 		 */
16340Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
16350Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
16360Sstevel@tonic-gate 		    ("ipcl_classify: cant find udp conn_t for ports : %x %x",
16370Sstevel@tonic-gate 		    lport, fport));
16380Sstevel@tonic-gate 		break;
16390Sstevel@tonic-gate 	}
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	return (NULL);
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate conn_t *
16453448Sdh155122 ipcl_classify_v6(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid,
16463448Sdh155122     ip_stack_t *ipst)
16470Sstevel@tonic-gate {
16480Sstevel@tonic-gate 	ip6_t		*ip6h;
16490Sstevel@tonic-gate 	connf_t		*connfp, *bind_connfp;
16500Sstevel@tonic-gate 	uint16_t	lport;
16510Sstevel@tonic-gate 	uint16_t	fport;
16520Sstevel@tonic-gate 	tcph_t		*tcph;
16530Sstevel@tonic-gate 	uint32_t	ports;
16540Sstevel@tonic-gate 	conn_t		*connp;
16550Sstevel@tonic-gate 	uint16_t	*up;
16561676Sjpk 	boolean_t	shared_addr;
16571676Sjpk 	boolean_t	unlabeled;
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	switch (protocol) {
16620Sstevel@tonic-gate 	case IPPROTO_TCP:
16630Sstevel@tonic-gate 		tcph = (tcph_t *)&mp->b_rptr[hdr_len];
16640Sstevel@tonic-gate 		up = (uint16_t *)tcph->th_lport;
16650Sstevel@tonic-gate 		ports = *(uint32_t *)up;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		connfp =
16683448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_src,
16693448Sdh155122 		    ports, ipst)];
16700Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16710Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16720Sstevel@tonic-gate 		    connp = connp->conn_next) {
16730Sstevel@tonic-gate 			if (IPCL_CONN_MATCH_V6(connp, protocol,
16740Sstevel@tonic-gate 			    ip6h->ip6_src, ip6h->ip6_dst, ports))
16750Sstevel@tonic-gate 				break;
16760Sstevel@tonic-gate 		}
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 		if (connp != NULL) {
16791676Sjpk 			/*
16801676Sjpk 			 * We have a fully-bound TCP connection.
16811676Sjpk 			 *
16821676Sjpk 			 * For labeled systems, there's no need to check the
16831676Sjpk 			 * label here.  It's known to be good as we checked
16841676Sjpk 			 * before allowing the connection to become bound.
16851676Sjpk 			 */
16860Sstevel@tonic-gate 			CONN_INC_REF(connp);
16870Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
16880Sstevel@tonic-gate 			return (connp);
16890Sstevel@tonic-gate 		}
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 		lport = up[1];
16941676Sjpk 		unlabeled = B_FALSE;
16951676Sjpk 		/* Cred can be null on IPv6 */
16961676Sjpk 		if (is_system_labeled()) {
16971676Sjpk 			cred_t *cr = DB_CRED(mp);
16981676Sjpk 
16991676Sjpk 			unlabeled = (cr != NULL &&
17001676Sjpk 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
17011676Sjpk 		}
17021676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
17031676Sjpk 		if (shared_addr) {
17043448Sdh155122 			/*
17053448Sdh155122 			 * No need to handle exclusive-stack zones since
17063448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
17073448Sdh155122 			 */
17081676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
17091676Sjpk 			/*
17101676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
17111676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
17121676Sjpk 			 * search for the zone based on the packet label.
17131676Sjpk 			 *
17141676Sjpk 			 * If there is such a zone, we prefer to find a
17151676Sjpk 			 * connection in it.  Otherwise, we look for a
17161676Sjpk 			 * MAC-exempt connection in any zone whose label
17171676Sjpk 			 * dominates the default label on the packet.
17181676Sjpk 			 */
17191676Sjpk 			if (zoneid == ALL_ZONES)
17201676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
17211676Sjpk 			else
17221676Sjpk 				unlabeled = B_FALSE;
17231676Sjpk 		}
17241676Sjpk 
17253448Sdh155122 		bind_connfp =
17263448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
17270Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
17280Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
17290Sstevel@tonic-gate 		    connp = connp->conn_next) {
17300Sstevel@tonic-gate 			if (IPCL_BIND_MATCH_V6(connp, protocol,
17310Sstevel@tonic-gate 			    ip6h->ip6_dst, lport) &&
17322263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
17331676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
17340Sstevel@tonic-gate 				break;
17350Sstevel@tonic-gate 		}
17360Sstevel@tonic-gate 
17371676Sjpk 		if (connp != NULL && is_system_labeled() &&
17381676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
17391676Sjpk 		    shared_addr, connp)) {
17401676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp6,
17411676Sjpk 			    char *, "connp(1) could not receive mp(2)",
17421676Sjpk 			    conn_t *, connp, mblk_t *, mp);
17431676Sjpk 			connp = NULL;
17441676Sjpk 		}
17451676Sjpk 
17460Sstevel@tonic-gate 		if (connp != NULL) {
17470Sstevel@tonic-gate 			/* Have a listner at least */
17480Sstevel@tonic-gate 			CONN_INC_REF(connp);
17490Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
17500Sstevel@tonic-gate 			IPCL_DEBUG_LVL(512,
17510Sstevel@tonic-gate 			    ("ipcl_classify_v6: found listner "
17520Sstevel@tonic-gate 			    "connp = %p\n", (void *)connp));
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 			return (connp);
17550Sstevel@tonic-gate 		}
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
17600Sstevel@tonic-gate 		    ("ipcl_classify_v6: couldn't classify mp = %p\n",
17610Sstevel@tonic-gate 		    (void *)mp));
17620Sstevel@tonic-gate 		break;
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 	case IPPROTO_UDP:
17650Sstevel@tonic-gate 		up = (uint16_t *)&mp->b_rptr[hdr_len];
17660Sstevel@tonic-gate 		lport = up[1];
17671676Sjpk 		unlabeled = B_FALSE;
17681676Sjpk 		/* Cred can be null on IPv6 */
17691676Sjpk 		if (is_system_labeled()) {
17701676Sjpk 			cred_t *cr = DB_CRED(mp);
17711676Sjpk 
17721676Sjpk 			unlabeled = (cr != NULL &&
17731676Sjpk 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
17741676Sjpk 		}
17751676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
17761676Sjpk 		if (shared_addr) {
17773448Sdh155122 			/*
17783448Sdh155122 			 * No need to handle exclusive-stack zones since
17793448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
17803448Sdh155122 			 */
17811676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
17821676Sjpk 			/*
17831676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
17841676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
17851676Sjpk 			 * search for the zone based on the packet label.
17861676Sjpk 			 *
17871676Sjpk 			 * If there is such a zone, we prefer to find a
17881676Sjpk 			 * connection in it.  Otherwise, we look for a
17891676Sjpk 			 * MAC-exempt connection in any zone whose label
17901676Sjpk 			 * dominates the default label on the packet.
17911676Sjpk 			 */
17921676Sjpk 			if (zoneid == ALL_ZONES)
17931676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
17941676Sjpk 			else
17951676Sjpk 				unlabeled = B_FALSE;
17961676Sjpk 		}
17971676Sjpk 
17980Sstevel@tonic-gate 		fport = up[0];
17990Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify_v6 %x %x", lport,
18000Sstevel@tonic-gate 		    fport));
18013448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
18020Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
18030Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
18040Sstevel@tonic-gate 		    connp = connp->conn_next) {
18050Sstevel@tonic-gate 			if (IPCL_UDP_MATCH_V6(connp, lport, ip6h->ip6_dst,
18060Sstevel@tonic-gate 			    fport, ip6h->ip6_src) &&
18072263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
18081676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
18090Sstevel@tonic-gate 				break;
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 
18121676Sjpk 		if (connp != NULL && is_system_labeled() &&
18131676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
18141676Sjpk 		    shared_addr, connp)) {
18151676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp6,
18161676Sjpk 			    char *, "connp(1) could not receive mp(2)",
18171676Sjpk 			    conn_t *, connp, mblk_t *, mp);
18181676Sjpk 			connp = NULL;
18191676Sjpk 		}
18201676Sjpk 
18210Sstevel@tonic-gate 		if (connp != NULL) {
18220Sstevel@tonic-gate 			CONN_INC_REF(connp);
18230Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
18240Sstevel@tonic-gate 			return (connp);
18250Sstevel@tonic-gate 		}
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 		/*
18280Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
18290Sstevel@tonic-gate 		 */
18300Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
18310Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
18320Sstevel@tonic-gate 		    ("ipcl_classify_v6: cant find udp conn_t for ports : %x %x",
18330Sstevel@tonic-gate 		    lport, fport));
18340Sstevel@tonic-gate 		break;
18350Sstevel@tonic-gate 	}
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	return (NULL);
18380Sstevel@tonic-gate }
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate /*
18410Sstevel@tonic-gate  * wrapper around ipcl_classify_(v4,v6) routines.
18420Sstevel@tonic-gate  */
18430Sstevel@tonic-gate conn_t *
18443448Sdh155122 ipcl_classify(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst)
18450Sstevel@tonic-gate {
18460Sstevel@tonic-gate 	uint16_t	hdr_len;
18470Sstevel@tonic-gate 	ipha_t		*ipha;
18480Sstevel@tonic-gate 	uint8_t		*nexthdrp;
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (ipha_t))
18510Sstevel@tonic-gate 		return (NULL);
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	switch (IPH_HDR_VERSION(mp->b_rptr)) {
18540Sstevel@tonic-gate 	case IPV4_VERSION:
18550Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
18560Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
18570Sstevel@tonic-gate 		return (ipcl_classify_v4(mp, ipha->ipha_protocol, hdr_len,
18583448Sdh155122 		    zoneid, ipst));
18590Sstevel@tonic-gate 	case IPV6_VERSION:
18600Sstevel@tonic-gate 		if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr,
18610Sstevel@tonic-gate 		    &hdr_len, &nexthdrp))
18620Sstevel@tonic-gate 			return (NULL);
18630Sstevel@tonic-gate 
18643448Sdh155122 		return (ipcl_classify_v6(mp, *nexthdrp, hdr_len, zoneid, ipst));
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	return (NULL);
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate conn_t *
18711676Sjpk ipcl_classify_raw(mblk_t *mp, uint8_t protocol, zoneid_t zoneid,
18723448Sdh155122     uint32_t ports, ipha_t *hdr, ip_stack_t *ipst)
18730Sstevel@tonic-gate {
18741676Sjpk 	connf_t		*connfp;
18750Sstevel@tonic-gate 	conn_t		*connp;
18760Sstevel@tonic-gate 	in_port_t	lport;
18770Sstevel@tonic-gate 	int		af;
18781676Sjpk 	boolean_t	shared_addr;
18791676Sjpk 	boolean_t	unlabeled;
18801676Sjpk 	const void	*dst;
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	lport = ((uint16_t *)&ports)[1];
18831676Sjpk 
18841676Sjpk 	unlabeled = B_FALSE;
18851676Sjpk 	/* Cred can be null on IPv6 */
18861676Sjpk 	if (is_system_labeled()) {
18871676Sjpk 		cred_t *cr = DB_CRED(mp);
18881676Sjpk 
18891676Sjpk 		unlabeled = (cr != NULL &&
18901676Sjpk 		    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
18911676Sjpk 	}
18921676Sjpk 	shared_addr = (zoneid == ALL_ZONES);
18931676Sjpk 	if (shared_addr) {
18943448Sdh155122 		/*
18953448Sdh155122 		 * No need to handle exclusive-stack zones since ALL_ZONES
18963448Sdh155122 		 * only applies to the shared stack.
18973448Sdh155122 		 */
18981676Sjpk 		zoneid = tsol_mlp_findzone(protocol, lport);
18991676Sjpk 		/*
19001676Sjpk 		 * If no shared MLP is found, tsol_mlp_findzone returns
19011676Sjpk 		 * ALL_ZONES.  In that case, we assume it's SLP, and search for
19021676Sjpk 		 * the zone based on the packet label.
19031676Sjpk 		 *
19041676Sjpk 		 * If there is such a zone, we prefer to find a connection in
19051676Sjpk 		 * it.  Otherwise, we look for a MAC-exempt connection in any
19061676Sjpk 		 * zone whose label dominates the default label on the packet.
19071676Sjpk 		 */
19081676Sjpk 		if (zoneid == ALL_ZONES)
19091676Sjpk 			zoneid = tsol_packet_to_zoneid(mp);
19101676Sjpk 		else
19111676Sjpk 			unlabeled = B_FALSE;
19121676Sjpk 	}
19131676Sjpk 
19140Sstevel@tonic-gate 	af = IPH_HDR_VERSION(hdr);
19151676Sjpk 	dst = af == IPV4_VERSION ? (const void *)&hdr->ipha_dst :
19161676Sjpk 	    (const void *)&((ip6_t *)hdr)->ip6_dst;
19173448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
19200Sstevel@tonic-gate 	for (connp = connfp->connf_head; connp != NULL;
19210Sstevel@tonic-gate 	    connp = connp->conn_next) {
19220Sstevel@tonic-gate 		/* We don't allow v4 fallback for v6 raw socket. */
19231676Sjpk 		if (af == (connp->conn_af_isv6 ? IPV4_VERSION :
19241676Sjpk 		    IPV6_VERSION))
19250Sstevel@tonic-gate 			continue;
19260Sstevel@tonic-gate 		if (connp->conn_fully_bound) {
19270Sstevel@tonic-gate 			if (af == IPV4_VERSION) {
19281676Sjpk 				if (!IPCL_CONN_MATCH(connp, protocol,
19291676Sjpk 				    hdr->ipha_src, hdr->ipha_dst, ports))
19301676Sjpk 					continue;
19310Sstevel@tonic-gate 			} else {
19321676Sjpk 				if (!IPCL_CONN_MATCH_V6(connp, protocol,
19330Sstevel@tonic-gate 				    ((ip6_t *)hdr)->ip6_src,
19341676Sjpk 				    ((ip6_t *)hdr)->ip6_dst, ports))
19351676Sjpk 					continue;
19360Sstevel@tonic-gate 			}
19370Sstevel@tonic-gate 		} else {
19380Sstevel@tonic-gate 			if (af == IPV4_VERSION) {
19391676Sjpk 				if (!IPCL_BIND_MATCH(connp, protocol,
19401676Sjpk 				    hdr->ipha_dst, lport))
19411676Sjpk 					continue;
19420Sstevel@tonic-gate 			} else {
19431676Sjpk 				if (!IPCL_BIND_MATCH_V6(connp, protocol,
19441676Sjpk 				    ((ip6_t *)hdr)->ip6_dst, lport))
19451676Sjpk 					continue;
19460Sstevel@tonic-gate 			}
19470Sstevel@tonic-gate 		}
19481676Sjpk 
19492263Ssommerfe 		if (IPCL_ZONE_MATCH(connp, zoneid) ||
19501676Sjpk 		    (unlabeled && connp->conn_mac_exempt))
19511676Sjpk 			break;
19521676Sjpk 	}
19531676Sjpk 	/*
19541676Sjpk 	 * If the connection is fully-bound and connection-oriented (TCP or
19551676Sjpk 	 * SCTP), then we've already validated the remote system's label.
19561676Sjpk 	 * There's no need to do it again for every packet.
19571676Sjpk 	 */
19581676Sjpk 	if (connp != NULL && is_system_labeled() && (!connp->conn_fully_bound ||
19591676Sjpk 	    !(connp->conn_flags & (IPCL_TCP|IPCL_SCTPCONN))) &&
19601676Sjpk 	    !tsol_receive_local(mp, dst, af, shared_addr, connp)) {
19611676Sjpk 		DTRACE_PROBE3(tx__ip__log__info__classify__rawip,
19621676Sjpk 		    char *, "connp(1) could not receive mp(2)",
19631676Sjpk 		    conn_t *, connp, mblk_t *, mp);
19641676Sjpk 		connp = NULL;
19650Sstevel@tonic-gate 	}
1966409Skcpoon 
1967409Skcpoon 	if (connp != NULL)
1968409Skcpoon 		goto found;
1969409Skcpoon 	mutex_exit(&connfp->connf_lock);
1970409Skcpoon 
1971409Skcpoon 	/* Try to look for a wildcard match. */
19723448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(0, ipst)];
1973409Skcpoon 	mutex_enter(&connfp->connf_lock);
1974409Skcpoon 	for (connp = connfp->connf_head; connp != NULL;
1975409Skcpoon 	    connp = connp->conn_next) {
1976409Skcpoon 		/* We don't allow v4 fallback for v6 raw socket. */
1977409Skcpoon 		if ((af == (connp->conn_af_isv6 ? IPV4_VERSION :
19782263Ssommerfe 		    IPV6_VERSION)) || !IPCL_ZONE_MATCH(connp, zoneid)) {
1979409Skcpoon 			continue;
1980409Skcpoon 		}
1981409Skcpoon 		if (af == IPV4_VERSION) {
1982409Skcpoon 			if (IPCL_RAW_MATCH(connp, protocol, hdr->ipha_dst))
1983409Skcpoon 				break;
1984409Skcpoon 		} else {
1985409Skcpoon 			if (IPCL_RAW_MATCH_V6(connp, protocol,
1986409Skcpoon 			    ((ip6_t *)hdr)->ip6_dst)) {
1987409Skcpoon 				break;
1988409Skcpoon 			}
1989409Skcpoon 		}
19900Sstevel@tonic-gate 	}
1991409Skcpoon 
1992409Skcpoon 	if (connp != NULL)
1993409Skcpoon 		goto found;
1994409Skcpoon 
19950Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
19960Sstevel@tonic-gate 	return (NULL);
1997409Skcpoon 
1998409Skcpoon found:
1999409Skcpoon 	ASSERT(connp != NULL);
2000409Skcpoon 	CONN_INC_REF(connp);
2001409Skcpoon 	mutex_exit(&connfp->connf_lock);
2002409Skcpoon 	return (connp);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate /* ARGSUSED */
20060Sstevel@tonic-gate static int
20075240Snordmark tcp_conn_constructor(void *buf, void *cdrarg, int kmflags)
20080Sstevel@tonic-gate {
20090Sstevel@tonic-gate 	itc_t	*itc = (itc_t *)buf;
20100Sstevel@tonic-gate 	conn_t 	*connp = &itc->itc_conn;
20115240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
20125240Snordmark 
20135240Snordmark 	bzero(connp, sizeof (conn_t));
20145240Snordmark 	bzero(tcp, sizeof (tcp_t));
20155240Snordmark 
20165240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20175240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20180Sstevel@tonic-gate 	tcp->tcp_timercache = tcp_timermp_alloc(KM_NOSLEEP);
20190Sstevel@tonic-gate 	connp->conn_tcp = tcp;
20200Sstevel@tonic-gate 	connp->conn_flags = IPCL_TCPCONN;
20210Sstevel@tonic-gate 	connp->conn_ulp = IPPROTO_TCP;
20220Sstevel@tonic-gate 	tcp->tcp_connp = connp;
20230Sstevel@tonic-gate 	return (0);
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate /* ARGSUSED */
20270Sstevel@tonic-gate static void
20285240Snordmark tcp_conn_destructor(void *buf, void *cdrarg)
20295240Snordmark {
20305240Snordmark 	itc_t	*itc = (itc_t *)buf;
20315240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20325240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
20335240Snordmark 
20345240Snordmark 	ASSERT(connp->conn_flags & IPCL_TCPCONN);
20355240Snordmark 	ASSERT(tcp->tcp_connp == connp);
20365240Snordmark 	ASSERT(connp->conn_tcp == tcp);
20375240Snordmark 	tcp_timermp_free(tcp);
20385240Snordmark 	mutex_destroy(&connp->conn_lock);
20395240Snordmark 	cv_destroy(&connp->conn_cv);
20405240Snordmark }
20415240Snordmark 
20425240Snordmark /* ARGSUSED */
20435240Snordmark static int
20445240Snordmark ip_conn_constructor(void *buf, void *cdrarg, int kmflags)
20455240Snordmark {
20465240Snordmark 	itc_t	*itc = (itc_t *)buf;
20475240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20485240Snordmark 
20495240Snordmark 	bzero(connp, sizeof (conn_t));
20505240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20515240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20525240Snordmark 	connp->conn_flags = IPCL_IPCCONN;
20535240Snordmark 
20545240Snordmark 	return (0);
20555240Snordmark }
20565240Snordmark 
20575240Snordmark /* ARGSUSED */
20585240Snordmark static void
20595240Snordmark ip_conn_destructor(void *buf, void *cdrarg)
20605240Snordmark {
20615240Snordmark 	itc_t	*itc = (itc_t *)buf;
20625240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20635240Snordmark 
20645240Snordmark 	ASSERT(connp->conn_flags & IPCL_IPCCONN);
20655240Snordmark 	ASSERT(connp->conn_priv == NULL);
20665240Snordmark 	mutex_destroy(&connp->conn_lock);
20675240Snordmark 	cv_destroy(&connp->conn_cv);
20685240Snordmark }
20695240Snordmark 
20705240Snordmark /* ARGSUSED */
20715240Snordmark static int
20725240Snordmark udp_conn_constructor(void *buf, void *cdrarg, int kmflags)
20735240Snordmark {
20745240Snordmark 	itc_t	*itc = (itc_t *)buf;
20755240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20765240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
20775240Snordmark 
20785240Snordmark 	bzero(connp, sizeof (conn_t));
20795240Snordmark 	bzero(udp, sizeof (udp_t));
20805240Snordmark 
20815240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20825240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20835240Snordmark 	connp->conn_udp = udp;
20845240Snordmark 	connp->conn_flags = IPCL_UDPCONN;
20855240Snordmark 	connp->conn_ulp = IPPROTO_UDP;
20865240Snordmark 	udp->udp_connp = connp;
20875240Snordmark 	return (0);
20885240Snordmark }
20895240Snordmark 
20905240Snordmark /* ARGSUSED */
20915240Snordmark static void
20925240Snordmark udp_conn_destructor(void *buf, void *cdrarg)
20935240Snordmark {
20945240Snordmark 	itc_t	*itc = (itc_t *)buf;
20955240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20965240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
20975240Snordmark 
20985240Snordmark 	ASSERT(connp->conn_flags & IPCL_UDPCONN);
20995240Snordmark 	ASSERT(udp->udp_connp == connp);
21005240Snordmark 	ASSERT(connp->conn_udp == udp);
21015240Snordmark 	mutex_destroy(&connp->conn_lock);
21025240Snordmark 	cv_destroy(&connp->conn_cv);
21035240Snordmark }
21045240Snordmark 
21055240Snordmark /* ARGSUSED */
21065240Snordmark static int
21075240Snordmark rawip_conn_constructor(void *buf, void *cdrarg, int kmflags)
21080Sstevel@tonic-gate {
21095240Snordmark 	itc_t	*itc = (itc_t *)buf;
21105240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21115240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
21125240Snordmark 
21135240Snordmark 	bzero(connp, sizeof (conn_t));
21145240Snordmark 	bzero(icmp, sizeof (icmp_t));
21155240Snordmark 
21165240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21175240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21185240Snordmark 	connp->conn_icmp = icmp;
21195240Snordmark 	connp->conn_flags = IPCL_RAWIPCONN;
21205240Snordmark 	connp->conn_ulp = IPPROTO_ICMP;
21215240Snordmark 	icmp->icmp_connp = connp;
21225240Snordmark 	return (0);
21235240Snordmark }
21245240Snordmark 
21255240Snordmark /* ARGSUSED */
21265240Snordmark static void
21275240Snordmark rawip_conn_destructor(void *buf, void *cdrarg)
21285240Snordmark {
21295240Snordmark 	itc_t	*itc = (itc_t *)buf;
21305240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21315240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
21325240Snordmark 
21335240Snordmark 	ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
21345240Snordmark 	ASSERT(icmp->icmp_connp == connp);
21355240Snordmark 	ASSERT(connp->conn_icmp == icmp);
21365240Snordmark 	mutex_destroy(&connp->conn_lock);
21375240Snordmark 	cv_destroy(&connp->conn_cv);
21385240Snordmark }
21395240Snordmark 
21405240Snordmark /* ARGSUSED */
21415240Snordmark static int
21425240Snordmark rts_conn_constructor(void *buf, void *cdrarg, int kmflags)
21435240Snordmark {
21445240Snordmark 	itc_t	*itc = (itc_t *)buf;
21455240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21465240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21475240Snordmark 
21485240Snordmark 	bzero(connp, sizeof (conn_t));
21495240Snordmark 	bzero(rts, sizeof (rts_t));
21505240Snordmark 
21515240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21525240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21535240Snordmark 	connp->conn_rts = rts;
21545240Snordmark 	connp->conn_flags = IPCL_RTSCONN;
21555240Snordmark 	rts->rts_connp = connp;
21565240Snordmark 	return (0);
21575240Snordmark }
21585240Snordmark 
21595240Snordmark /* ARGSUSED */
21605240Snordmark static void
21615240Snordmark rts_conn_destructor(void *buf, void *cdrarg)
21625240Snordmark {
21635240Snordmark 	itc_t	*itc = (itc_t *)buf;
21645240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21655240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21665240Snordmark 
21675240Snordmark 	ASSERT(connp->conn_flags & IPCL_RTSCONN);
21685240Snordmark 	ASSERT(rts->rts_connp == connp);
21695240Snordmark 	ASSERT(connp->conn_rts == rts);
21705240Snordmark 	mutex_destroy(&connp->conn_lock);
21715240Snordmark 	cv_destroy(&connp->conn_cv);
21725240Snordmark }
21735240Snordmark 
21745240Snordmark /*
21755240Snordmark  * Called as part of ipcl_conn_destroy to assert and clear any pointers
21765240Snordmark  * in the conn_t.
21775277Snordmark  *
21785277Snordmark  * Below we list all the pointers in the conn_t as a documentation aid.
21795277Snordmark  * The ones that we can not ASSERT to be NULL are #ifdef'ed out.
21805277Snordmark  * If you add any pointers to the conn_t please add an ASSERT here
21815277Snordmark  * and #ifdef it out if it can't be actually asserted to be NULL.
21825277Snordmark  * In any case, we bzero most of the conn_t at the end of the function.
21835240Snordmark  */
21845240Snordmark void
21855240Snordmark ipcl_conn_cleanup(conn_t *connp)
21865240Snordmark {
21875240Snordmark 	ASSERT(connp->conn_ire_cache == NULL);
21885240Snordmark 	ASSERT(connp->conn_latch == NULL);
21895240Snordmark #ifdef notdef
21905277Snordmark 	/* These are not cleared */
21915240Snordmark 	ASSERT(connp->conn_rq == NULL);
21925240Snordmark 	ASSERT(connp->conn_wq == NULL);
21935240Snordmark #endif
21945240Snordmark 	ASSERT(connp->conn_cred == NULL);
21955240Snordmark 	ASSERT(connp->conn_g_fanout == NULL);
21965240Snordmark 	ASSERT(connp->conn_g_next == NULL);
21975240Snordmark 	ASSERT(connp->conn_g_prev == NULL);
21985240Snordmark 	ASSERT(connp->conn_policy == NULL);
21995240Snordmark 	ASSERT(connp->conn_fanout == NULL);
22005240Snordmark 	ASSERT(connp->conn_next == NULL);
22015240Snordmark 	ASSERT(connp->conn_prev == NULL);
22025240Snordmark #ifdef notdef
22035240Snordmark 	/*
22045240Snordmark 	 * The ill and ipif pointers are not cleared before the conn_t
22055240Snordmark 	 * goes away since they do not hold a reference on the ill/ipif.
22065240Snordmark 	 * We should replace these pointers with ifindex/ipaddr_t to
22075240Snordmark 	 * make the code less complex.
22085240Snordmark 	 */
22095240Snordmark 	ASSERT(connp->conn_xmit_if_ill == NULL);
22105240Snordmark 	ASSERT(connp->conn_nofailover_ill == NULL);
22115240Snordmark 	ASSERT(connp->conn_outgoing_ill == NULL);
22125240Snordmark 	ASSERT(connp->conn_incoming_ill == NULL);
22135240Snordmark 	ASSERT(connp->conn_outgoing_pill == NULL);
22145240Snordmark 	ASSERT(connp->conn_multicast_ipif == NULL);
22155240Snordmark 	ASSERT(connp->conn_multicast_ill == NULL);
22165240Snordmark #endif
22175240Snordmark 	ASSERT(connp->conn_oper_pending_ill == NULL);
22185240Snordmark 	ASSERT(connp->conn_ilg == NULL);
22195240Snordmark 	ASSERT(connp->conn_drain_next == NULL);
22205240Snordmark 	ASSERT(connp->conn_drain_prev == NULL);
22215277Snordmark #ifdef notdef
22225277Snordmark 	/* conn_idl is not cleared when removed from idl list */
22235240Snordmark 	ASSERT(connp->conn_idl == NULL);
22245277Snordmark #endif
22255240Snordmark 	ASSERT(connp->conn_ipsec_opt_mp == NULL);
22265240Snordmark 	ASSERT(connp->conn_peercred == NULL);
22275240Snordmark 	ASSERT(connp->conn_netstack == NULL);
22285240Snordmark 
22295240Snordmark 	/* Clear out the conn_t fields that are not preserved */
22305240Snordmark 	bzero(&connp->conn_start_clr,
22315240Snordmark 	    sizeof (conn_t) -
22325240Snordmark 	    ((uchar_t *)&connp->conn_start_clr - (uchar_t *)connp));
22335240Snordmark 
22340Sstevel@tonic-gate }
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate /*
22370Sstevel@tonic-gate  * All conns are inserted in a global multi-list for the benefit of
22380Sstevel@tonic-gate  * walkers. The walk is guaranteed to walk all open conns at the time
22390Sstevel@tonic-gate  * of the start of the walk exactly once. This property is needed to
22400Sstevel@tonic-gate  * achieve some cleanups during unplumb of interfaces. This is achieved
22410Sstevel@tonic-gate  * as follows.
22420Sstevel@tonic-gate  *
22430Sstevel@tonic-gate  * ipcl_conn_create and ipcl_conn_destroy are the only functions that
22440Sstevel@tonic-gate  * call the insert and delete functions below at creation and deletion
22450Sstevel@tonic-gate  * time respectively. The conn never moves or changes its position in this
22460Sstevel@tonic-gate  * multi-list during its lifetime. CONN_CONDEMNED ensures that the refcnt
22470Sstevel@tonic-gate  * won't increase due to walkers, once the conn deletion has started. Note
22480Sstevel@tonic-gate  * that we can't remove the conn from the global list and then wait for
22490Sstevel@tonic-gate  * the refcnt to drop to zero, since walkers would then see a truncated
22500Sstevel@tonic-gate  * list. CONN_INCIPIENT ensures that walkers don't start looking at
22510Sstevel@tonic-gate  * conns until ip_open is ready to make them globally visible.
22520Sstevel@tonic-gate  * The global round robin multi-list locks are held only to get the
22530Sstevel@tonic-gate  * next member/insertion/deletion and contention should be negligible
22540Sstevel@tonic-gate  * if the multi-list is much greater than the number of cpus.
22550Sstevel@tonic-gate  */
22560Sstevel@tonic-gate void
22570Sstevel@tonic-gate ipcl_globalhash_insert(conn_t *connp)
22580Sstevel@tonic-gate {
22590Sstevel@tonic-gate 	int	index;
22603448Sdh155122 	struct connf_s	*connfp;
22613448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	/*
22640Sstevel@tonic-gate 	 * No need for atomic here. Approximate even distribution
22650Sstevel@tonic-gate 	 * in the global lists is sufficient.
22660Sstevel@tonic-gate 	 */
22673448Sdh155122 	ipst->ips_conn_g_index++;
22683448Sdh155122 	index = ipst->ips_conn_g_index & (CONN_G_HASH_SIZE - 1);
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
22710Sstevel@tonic-gate 	/*
22720Sstevel@tonic-gate 	 * Mark as INCIPIENT, so that walkers will ignore this
22730Sstevel@tonic-gate 	 * for now, till ip_open is ready to make it visible globally.
22740Sstevel@tonic-gate 	 */
22750Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_INCIPIENT;
22760Sstevel@tonic-gate 
22773448Sdh155122 	connfp = &ipst->ips_ipcl_globalhash_fanout[index];
22780Sstevel@tonic-gate 	/* Insert at the head of the list */
22793448Sdh155122 	mutex_enter(&connfp->connf_lock);
22803448Sdh155122 	connp->conn_g_next = connfp->connf_head;
22810Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
22820Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp;
22833448Sdh155122 	connfp->connf_head = connp;
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	/* The fanout bucket this conn points to */
22863448Sdh155122 	connp->conn_g_fanout = connfp;
22870Sstevel@tonic-gate 
22883448Sdh155122 	mutex_exit(&connfp->connf_lock);
22890Sstevel@tonic-gate }
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate void
22920Sstevel@tonic-gate ipcl_globalhash_remove(conn_t *connp)
22930Sstevel@tonic-gate {
22943448Sdh155122 	struct connf_s	*connfp;
22953448Sdh155122 
22960Sstevel@tonic-gate 	/*
22970Sstevel@tonic-gate 	 * We were never inserted in the global multi list.
22980Sstevel@tonic-gate 	 * IPCL_NONE variety is never inserted in the global multilist
22990Sstevel@tonic-gate 	 * since it is presumed to not need any cleanup and is transient.
23000Sstevel@tonic-gate 	 */
23010Sstevel@tonic-gate 	if (connp->conn_g_fanout == NULL)
23020Sstevel@tonic-gate 		return;
23030Sstevel@tonic-gate 
23043448Sdh155122 	connfp = connp->conn_g_fanout;
23053448Sdh155122 	mutex_enter(&connfp->connf_lock);
23060Sstevel@tonic-gate 	if (connp->conn_g_prev != NULL)
23070Sstevel@tonic-gate 		connp->conn_g_prev->conn_g_next = connp->conn_g_next;
23080Sstevel@tonic-gate 	else
23093448Sdh155122 		connfp->connf_head = connp->conn_g_next;
23100Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
23110Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp->conn_g_prev;
23123448Sdh155122 	mutex_exit(&connfp->connf_lock);
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	/* Better to stumble on a null pointer than to corrupt memory */
23150Sstevel@tonic-gate 	connp->conn_g_next = NULL;
23160Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
23175240Snordmark 	connp->conn_g_fanout = NULL;
23180Sstevel@tonic-gate }
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate /*
23210Sstevel@tonic-gate  * Walk the list of all conn_t's in the system, calling the function provided
23220Sstevel@tonic-gate  * with the specified argument for each.
23230Sstevel@tonic-gate  * Applies to both IPv4 and IPv6.
23240Sstevel@tonic-gate  *
23250Sstevel@tonic-gate  * IPCs may hold pointers to ipif/ill. To guard against stale pointers
23260Sstevel@tonic-gate  * ipcl_walk() is called to cleanup the conn_t's, typically when an interface is
23270Sstevel@tonic-gate  * unplumbed or removed. New conn_t's that are created while we are walking
23280Sstevel@tonic-gate  * may be missed by this walk, because they are not necessarily inserted
23290Sstevel@tonic-gate  * at the tail of the list. They are new conn_t's and thus don't have any
23300Sstevel@tonic-gate  * stale pointers. The CONN_CLOSING flag ensures that no new reference
23310Sstevel@tonic-gate  * is created to the struct that is going away.
23320Sstevel@tonic-gate  */
23330Sstevel@tonic-gate void
23343448Sdh155122 ipcl_walk(pfv_t func, void *arg, ip_stack_t *ipst)
23350Sstevel@tonic-gate {
23360Sstevel@tonic-gate 	int	i;
23370Sstevel@tonic-gate 	conn_t	*connp;
23380Sstevel@tonic-gate 	conn_t	*prev_connp;
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
23413448Sdh155122 		mutex_enter(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23420Sstevel@tonic-gate 		prev_connp = NULL;
23433448Sdh155122 		connp = ipst->ips_ipcl_globalhash_fanout[i].connf_head;
23440Sstevel@tonic-gate 		while (connp != NULL) {
23450Sstevel@tonic-gate 			mutex_enter(&connp->conn_lock);
23460Sstevel@tonic-gate 			if (connp->conn_state_flags &
23470Sstevel@tonic-gate 			    (CONN_CONDEMNED | CONN_INCIPIENT)) {
23480Sstevel@tonic-gate 				mutex_exit(&connp->conn_lock);
23490Sstevel@tonic-gate 				connp = connp->conn_g_next;
23500Sstevel@tonic-gate 				continue;
23510Sstevel@tonic-gate 			}
23520Sstevel@tonic-gate 			CONN_INC_REF_LOCKED(connp);
23530Sstevel@tonic-gate 			mutex_exit(&connp->conn_lock);
23543448Sdh155122 			mutex_exit(
23553448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23560Sstevel@tonic-gate 			(*func)(connp, arg);
23570Sstevel@tonic-gate 			if (prev_connp != NULL)
23580Sstevel@tonic-gate 				CONN_DEC_REF(prev_connp);
23593448Sdh155122 			mutex_enter(
23603448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23610Sstevel@tonic-gate 			prev_connp = connp;
23620Sstevel@tonic-gate 			connp = connp->conn_g_next;
23630Sstevel@tonic-gate 		}
23643448Sdh155122 		mutex_exit(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23650Sstevel@tonic-gate 		if (prev_connp != NULL)
23660Sstevel@tonic-gate 			CONN_DEC_REF(prev_connp);
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate /*
23710Sstevel@tonic-gate  * Search for a peer TCP/IPv4 loopback conn by doing a reverse lookup on
23720Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
23730Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
23742323Sethindra  * (peer tcp in ESTABLISHED state).
23750Sstevel@tonic-gate  */
23760Sstevel@tonic-gate conn_t *
23773448Sdh155122 ipcl_conn_tcp_lookup_reversed_ipv4(conn_t *connp, ipha_t *ipha, tcph_t *tcph,
23783448Sdh155122     ip_stack_t *ipst)
23790Sstevel@tonic-gate {
23800Sstevel@tonic-gate 	uint32_t ports;
23810Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
23820Sstevel@tonic-gate 	connf_t	*connfp;
23830Sstevel@tonic-gate 	conn_t	*tconnp;
23840Sstevel@tonic-gate 	boolean_t zone_chk;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	/*
23870Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
23880Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
23890Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
23900Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.
23910Sstevel@tonic-gate 	 */
23920Sstevel@tonic-gate 	zone_chk = (ipha->ipha_src == htonl(INADDR_LOOPBACK) ||
23930Sstevel@tonic-gate 	    ipha->ipha_dst == htonl(INADDR_LOOPBACK));
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
23960Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
23970Sstevel@tonic-gate 
23983448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
23993448Sdh155122 	    ports, ipst)];
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24020Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24030Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
24060Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
24072323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24080Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24110Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24120Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24130Sstevel@tonic-gate 			return (tconnp);
24140Sstevel@tonic-gate 		}
24150Sstevel@tonic-gate 	}
24160Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
24170Sstevel@tonic-gate 	return (NULL);
24180Sstevel@tonic-gate }
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate /*
24210Sstevel@tonic-gate  * Search for a peer TCP/IPv6 loopback conn by doing a reverse lookup on
24220Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
24230Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
24242323Sethindra  * (peer tcp in ESTABLISHED state).
24250Sstevel@tonic-gate  */
24260Sstevel@tonic-gate conn_t *
24273448Sdh155122 ipcl_conn_tcp_lookup_reversed_ipv6(conn_t *connp, ip6_t *ip6h, tcph_t *tcph,
24283448Sdh155122     ip_stack_t *ipst)
24290Sstevel@tonic-gate {
24300Sstevel@tonic-gate 	uint32_t ports;
24310Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
24320Sstevel@tonic-gate 	connf_t	*connfp;
24330Sstevel@tonic-gate 	conn_t	*tconnp;
24340Sstevel@tonic-gate 	boolean_t zone_chk;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	/*
24370Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
24380Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
24390Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
24400Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.  We
24410Sstevel@tonic-gate 	 * don't do Zone check for link local address(es) because the
24420Sstevel@tonic-gate 	 * current Zone implementation treats each link local address as
24430Sstevel@tonic-gate 	 * being unique per system node, i.e. they belong to global Zone.
24440Sstevel@tonic-gate 	 */
24450Sstevel@tonic-gate 	zone_chk = (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src) ||
24460Sstevel@tonic-gate 	    IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst));
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
24490Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
24500Sstevel@tonic-gate 
24513448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
24523448Sdh155122 	    ports, ipst)];
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24550Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24560Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 		/* We skip tcp_bound_if check here as this is loopback tcp */
24590Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
24600Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
24612323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24620Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24650Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24660Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24670Sstevel@tonic-gate 			return (tconnp);
24680Sstevel@tonic-gate 		}
24690Sstevel@tonic-gate 	}
24700Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
24710Sstevel@tonic-gate 	return (NULL);
24720Sstevel@tonic-gate }
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate /*
24750Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
24760Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
24770Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
24780Sstevel@tonic-gate  */
24790Sstevel@tonic-gate conn_t *
24803448Sdh155122 ipcl_tcp_lookup_reversed_ipv4(ipha_t *ipha, tcph_t *tcph, int min_state,
24813448Sdh155122     ip_stack_t *ipst)
24820Sstevel@tonic-gate {
24830Sstevel@tonic-gate 	uint32_t ports;
24840Sstevel@tonic-gate 	uint16_t *pports;
24850Sstevel@tonic-gate 	connf_t	*connfp;
24860Sstevel@tonic-gate 	conn_t	*tconnp;
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
24890Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
24900Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
24910Sstevel@tonic-gate 
24923448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
24934691Skcpoon 	    ports, ipst)];
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24960Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24970Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
25000Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
25010Sstevel@tonic-gate 		    tconnp->conn_tcp->tcp_state >= min_state) {
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25040Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25050Sstevel@tonic-gate 			return (tconnp);
25060Sstevel@tonic-gate 		}
25070Sstevel@tonic-gate 	}
25080Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25090Sstevel@tonic-gate 	return (NULL);
25100Sstevel@tonic-gate }
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate /*
25130Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
25140Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
25150Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
25160Sstevel@tonic-gate  * Match on ifindex in addition to addresses.
25170Sstevel@tonic-gate  */
25180Sstevel@tonic-gate conn_t *
25190Sstevel@tonic-gate ipcl_tcp_lookup_reversed_ipv6(ip6_t *ip6h, tcpha_t *tcpha, int min_state,
25203448Sdh155122     uint_t ifindex, ip_stack_t *ipst)
25210Sstevel@tonic-gate {
25220Sstevel@tonic-gate 	tcp_t	*tcp;
25230Sstevel@tonic-gate 	uint32_t ports;
25240Sstevel@tonic-gate 	uint16_t *pports;
25250Sstevel@tonic-gate 	connf_t	*connfp;
25260Sstevel@tonic-gate 	conn_t	*tconnp;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
25290Sstevel@tonic-gate 	pports[0] = tcpha->tha_fport;
25300Sstevel@tonic-gate 	pports[1] = tcpha->tha_lport;
25310Sstevel@tonic-gate 
25323448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
25334691Skcpoon 	    ports, ipst)];
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
25360Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
25370Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 		tcp = tconnp->conn_tcp;
25400Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
25410Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
25420Sstevel@tonic-gate 		    tcp->tcp_state >= min_state &&
25430Sstevel@tonic-gate 		    (tcp->tcp_bound_if == 0 ||
25440Sstevel@tonic-gate 		    tcp->tcp_bound_if == ifindex)) {
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25470Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25480Sstevel@tonic-gate 			return (tconnp);
25490Sstevel@tonic-gate 		}
25500Sstevel@tonic-gate 	}
25510Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25520Sstevel@tonic-gate 	return (NULL);
25530Sstevel@tonic-gate }
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate /*
25561676Sjpk  * Finds a TCP/IPv4 listening connection; called by tcp_disconnect to locate
25571676Sjpk  * a listener when changing state.
25580Sstevel@tonic-gate  */
25590Sstevel@tonic-gate conn_t *
25603448Sdh155122 ipcl_lookup_listener_v4(uint16_t lport, ipaddr_t laddr, zoneid_t zoneid,
25613448Sdh155122     ip_stack_t *ipst)
25620Sstevel@tonic-gate {
25630Sstevel@tonic-gate 	connf_t		*bind_connfp;
25640Sstevel@tonic-gate 	conn_t		*connp;
25650Sstevel@tonic-gate 	tcp_t		*tcp;
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 	/*
25680Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
25690Sstevel@tonic-gate 	 * all zeros.
25700Sstevel@tonic-gate 	 */
25710Sstevel@tonic-gate 	if (laddr == 0)
25720Sstevel@tonic-gate 		return (NULL);
25730Sstevel@tonic-gate 
25741676Sjpk 	ASSERT(zoneid != ALL_ZONES);
25751676Sjpk 
25763448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
25770Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
25780Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
25790Sstevel@tonic-gate 	    connp = connp->conn_next) {
25800Sstevel@tonic-gate 		tcp = connp->conn_tcp;
25810Sstevel@tonic-gate 		if (IPCL_BIND_MATCH(connp, IPPROTO_TCP, laddr, lport) &&
25822263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
25830Sstevel@tonic-gate 		    (tcp->tcp_listener == NULL)) {
25840Sstevel@tonic-gate 			CONN_INC_REF(connp);
25850Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
25860Sstevel@tonic-gate 			return (connp);
25870Sstevel@tonic-gate 		}
25880Sstevel@tonic-gate 	}
25890Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
25900Sstevel@tonic-gate 	return (NULL);
25910Sstevel@tonic-gate }
25920Sstevel@tonic-gate 
25931676Sjpk /*
25941676Sjpk  * Finds a TCP/IPv6 listening connection; called by tcp_disconnect to locate
25951676Sjpk  * a listener when changing state.
25961676Sjpk  */
25970Sstevel@tonic-gate conn_t *
25980Sstevel@tonic-gate ipcl_lookup_listener_v6(uint16_t lport, in6_addr_t *laddr, uint_t ifindex,
25993448Sdh155122     zoneid_t zoneid, ip_stack_t *ipst)
26000Sstevel@tonic-gate {
26010Sstevel@tonic-gate 	connf_t		*bind_connfp;
26020Sstevel@tonic-gate 	conn_t		*connp = NULL;
26030Sstevel@tonic-gate 	tcp_t		*tcp;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	/*
26060Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
26070Sstevel@tonic-gate 	 * all zeros.
26080Sstevel@tonic-gate 	 */
26090Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(laddr))
26100Sstevel@tonic-gate 		return (NULL);
26110Sstevel@tonic-gate 
26121676Sjpk 	ASSERT(zoneid != ALL_ZONES);
26130Sstevel@tonic-gate 
26143448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
26150Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
26160Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
26170Sstevel@tonic-gate 	    connp = connp->conn_next) {
26180Sstevel@tonic-gate 		tcp = connp->conn_tcp;
26190Sstevel@tonic-gate 		if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP, *laddr, lport) &&
26202263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
26210Sstevel@tonic-gate 		    (tcp->tcp_bound_if == 0 ||
26220Sstevel@tonic-gate 		    tcp->tcp_bound_if == ifindex) &&
26230Sstevel@tonic-gate 		    tcp->tcp_listener == NULL) {
26240Sstevel@tonic-gate 			CONN_INC_REF(connp);
26250Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
26260Sstevel@tonic-gate 			return (connp);
26270Sstevel@tonic-gate 		}
26280Sstevel@tonic-gate 	}
26290Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
26300Sstevel@tonic-gate 	return (NULL);
26310Sstevel@tonic-gate }
26320Sstevel@tonic-gate 
2633741Smasputra /*
2634741Smasputra  * ipcl_get_next_conn
2635741Smasputra  *	get the next entry in the conn global list
2636741Smasputra  *	and put a reference on the next_conn.
2637741Smasputra  *	decrement the reference on the current conn.
2638741Smasputra  *
2639741Smasputra  * This is an iterator based walker function that also provides for
2640741Smasputra  * some selection by the caller. It walks through the conn_hash bucket
2641741Smasputra  * searching for the next valid connp in the list, and selects connections
2642741Smasputra  * that are neither closed nor condemned. It also REFHOLDS the conn
2643741Smasputra  * thus ensuring that the conn exists when the caller uses the conn.
2644741Smasputra  */
2645741Smasputra conn_t *
2646741Smasputra ipcl_get_next_conn(connf_t *connfp, conn_t *connp, uint32_t conn_flags)
2647741Smasputra {
2648741Smasputra 	conn_t	*next_connp;
2649741Smasputra 
2650741Smasputra 	if (connfp == NULL)
2651741Smasputra 		return (NULL);
2652741Smasputra 
2653741Smasputra 	mutex_enter(&connfp->connf_lock);
2654741Smasputra 
2655741Smasputra 	next_connp = (connp == NULL) ?
2656741Smasputra 	    connfp->connf_head : connp->conn_g_next;
2657741Smasputra 
2658741Smasputra 	while (next_connp != NULL) {
2659741Smasputra 		mutex_enter(&next_connp->conn_lock);
2660741Smasputra 		if (!(next_connp->conn_flags & conn_flags) ||
2661741Smasputra 		    (next_connp->conn_state_flags &
2662741Smasputra 		    (CONN_CONDEMNED | CONN_INCIPIENT))) {
2663741Smasputra 			/*
2664741Smasputra 			 * This conn has been condemned or
2665741Smasputra 			 * is closing, or the flags don't match
2666741Smasputra 			 */
2667741Smasputra 			mutex_exit(&next_connp->conn_lock);
2668741Smasputra 			next_connp = next_connp->conn_g_next;
2669741Smasputra 			continue;
2670741Smasputra 		}
2671741Smasputra 		CONN_INC_REF_LOCKED(next_connp);
2672741Smasputra 		mutex_exit(&next_connp->conn_lock);
2673741Smasputra 		break;
2674741Smasputra 	}
2675741Smasputra 
2676741Smasputra 	mutex_exit(&connfp->connf_lock);
2677741Smasputra 
2678741Smasputra 	if (connp != NULL)
2679741Smasputra 		CONN_DEC_REF(connp);
2680741Smasputra 
2681741Smasputra 	return (next_connp);
2682741Smasputra }
2683741Smasputra 
26840Sstevel@tonic-gate #ifdef CONN_DEBUG
26850Sstevel@tonic-gate /*
26860Sstevel@tonic-gate  * Trace of the last NBUF refhold/refrele
26870Sstevel@tonic-gate  */
26880Sstevel@tonic-gate int
26890Sstevel@tonic-gate conn_trace_ref(conn_t *connp)
26900Sstevel@tonic-gate {
26910Sstevel@tonic-gate 	int	last;
26920Sstevel@tonic-gate 	conn_trace_t	*ctb;
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
26950Sstevel@tonic-gate 	last = connp->conn_trace_last;
26960Sstevel@tonic-gate 	last++;
26970Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
26980Sstevel@tonic-gate 		last = 0;
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
27015023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27020Sstevel@tonic-gate 	connp->conn_trace_last = last;
27030Sstevel@tonic-gate 	return (1);
27040Sstevel@tonic-gate }
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate int
27070Sstevel@tonic-gate conn_untrace_ref(conn_t *connp)
27080Sstevel@tonic-gate {
27090Sstevel@tonic-gate 	int	last;
27100Sstevel@tonic-gate 	conn_trace_t	*ctb;
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
27130Sstevel@tonic-gate 	last = connp->conn_trace_last;
27140Sstevel@tonic-gate 	last++;
27150Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
27160Sstevel@tonic-gate 		last = 0;
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
27195023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27200Sstevel@tonic-gate 	connp->conn_trace_last = last;
27210Sstevel@tonic-gate 	return (1);
27220Sstevel@tonic-gate }
27230Sstevel@tonic-gate #endif
2724