xref: /onnv-gate/usr/src/uts/common/inet/ip/ipclassifier.c (revision 5277:4f992c690322)
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 /*
223422Snh145002  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
281676Sjpk const char ipclassifier_version[] = "@(#)ipclassifier.c	%I%	%E% SMI";
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * IP PACKET CLASSIFIER
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * The IP packet classifier provides mapping between IP packets and persistent
340Sstevel@tonic-gate  * connection state for connection-oriented protocols. It also provides
350Sstevel@tonic-gate  * interface for managing connection states.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * The connection state is kept in conn_t data structure and contains, among
380Sstevel@tonic-gate  * other things:
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  *	o local/remote address and ports
410Sstevel@tonic-gate  *	o Transport protocol
420Sstevel@tonic-gate  *	o squeue for the connection (for TCP only)
430Sstevel@tonic-gate  *	o reference counter
440Sstevel@tonic-gate  *	o Connection state
450Sstevel@tonic-gate  *	o hash table linkage
460Sstevel@tonic-gate  *	o interface/ire information
470Sstevel@tonic-gate  *	o credentials
480Sstevel@tonic-gate  *	o ipsec policy
490Sstevel@tonic-gate  *	o send and receive functions.
500Sstevel@tonic-gate  *	o mutex lock.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * Connections use a reference counting scheme. They are freed when the
530Sstevel@tonic-gate  * reference counter drops to zero. A reference is incremented when connection
540Sstevel@tonic-gate  * is placed in a list or table, when incoming packet for the connection arrives
550Sstevel@tonic-gate  * and when connection is processed via squeue (squeue processing may be
560Sstevel@tonic-gate  * asynchronous and the reference protects the connection from being destroyed
570Sstevel@tonic-gate  * before its processing is finished).
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * send and receive functions are currently used for TCP only. The send function
600Sstevel@tonic-gate  * determines the IP entry point for the packet once it leaves TCP to be sent to
610Sstevel@tonic-gate  * the destination address. The receive function is used by IP when the packet
620Sstevel@tonic-gate  * should be passed for TCP processing. When a new connection is created these
630Sstevel@tonic-gate  * are set to ip_output() and tcp_input() respectively. During the lifetime of
640Sstevel@tonic-gate  * the connection the send and receive functions may change depending on the
650Sstevel@tonic-gate  * changes in the connection state. For example, Once the connection is bound to
660Sstevel@tonic-gate  * an addresse, the receive function for this connection is set to
670Sstevel@tonic-gate  * tcp_conn_request().  This allows incoming SYNs to go directly into the
680Sstevel@tonic-gate  * listener SYN processing function without going to tcp_input() first.
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  * Classifier uses several hash tables:
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  * 	ipcl_conn_fanout:	contains all TCP connections in CONNECTED state
730Sstevel@tonic-gate  *	ipcl_bind_fanout:	contains all connections in BOUND state
740Sstevel@tonic-gate  *	ipcl_proto_fanout:	IPv4 protocol fanout
750Sstevel@tonic-gate  *	ipcl_proto_fanout_v6:	IPv6 protocol fanout
760Sstevel@tonic-gate  *	ipcl_udp_fanout:	contains all UDP connections
770Sstevel@tonic-gate  *	ipcl_globalhash_fanout:	contains all connections
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  * The ipcl_globalhash_fanout is used for any walkers (like snmp and Clustering)
800Sstevel@tonic-gate  * which need to view all existing connections.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * All tables are protected by per-bucket locks. When both per-bucket lock and
830Sstevel@tonic-gate  * connection lock need to be held, the per-bucket lock should be acquired
840Sstevel@tonic-gate  * first, followed by the connection lock.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * All functions doing search in one of these tables increment a reference
870Sstevel@tonic-gate  * counter on the connection found (if any). This reference should be dropped
880Sstevel@tonic-gate  * when the caller has finished processing the connection.
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  * INTERFACES:
920Sstevel@tonic-gate  * ===========
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * Connection Lookup:
950Sstevel@tonic-gate  * ------------------
960Sstevel@tonic-gate  *
973448Sdh155122  * conn_t *ipcl_classify_v4(mp, protocol, hdr_len, zoneid, ip_stack)
983448Sdh155122  * conn_t *ipcl_classify_v6(mp, protocol, hdr_len, zoneid, ip_stack)
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  * Finds connection for an incoming IPv4 or IPv6 packet. Returns NULL if
1010Sstevel@tonic-gate  * it can't find any associated connection. If the connection is found, its
1020Sstevel@tonic-gate  * reference counter is incremented.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  *	mp:	mblock, containing packet header. The full header should fit
1050Sstevel@tonic-gate  *		into a single mblock. It should also contain at least full IP
1060Sstevel@tonic-gate  *		and TCP or UDP header.
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  *	protocol: Either IPPROTO_TCP or IPPROTO_UDP.
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  *	hdr_len: The size of IP header. It is used to find TCP or UDP header in
1110Sstevel@tonic-gate  *		 the packet.
1120Sstevel@tonic-gate  *
1131676Sjpk  * 	zoneid: The zone in which the returned connection must be; the zoneid
1141676Sjpk  *		corresponding to the ire_zoneid on the IRE located for the
1151676Sjpk  *		packet's destination address.
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  *	For TCP connections, the lookup order is as follows:
1180Sstevel@tonic-gate  *		5-tuple {src, dst, protocol, local port, remote port}
1190Sstevel@tonic-gate  *			lookup in ipcl_conn_fanout table.
1200Sstevel@tonic-gate  *		3-tuple {dst, remote port, protocol} lookup in
1210Sstevel@tonic-gate  *			ipcl_bind_fanout table.
1220Sstevel@tonic-gate  *
1230Sstevel@tonic-gate  *	For UDP connections, a 5-tuple {src, dst, protocol, local port,
1240Sstevel@tonic-gate  *	remote port} lookup is done on ipcl_udp_fanout. Note that,
1250Sstevel@tonic-gate  *	these interfaces do not handle cases where a packets belongs
1260Sstevel@tonic-gate  *	to multiple UDP clients, which is handled in IP itself.
1270Sstevel@tonic-gate  *
1281676Sjpk  * If the destination IRE is ALL_ZONES (indicated by zoneid), then we must
1291676Sjpk  * determine which actual zone gets the segment.  This is used only in a
1301676Sjpk  * labeled environment.  The matching rules are:
1311676Sjpk  *
1321676Sjpk  *	- If it's not a multilevel port, then the label on the packet selects
1331676Sjpk  *	  the zone.  Unlabeled packets are delivered to the global zone.
1341676Sjpk  *
1351676Sjpk  *	- If it's a multilevel port, then only the zone registered to receive
1361676Sjpk  *	  packets on that port matches.
1371676Sjpk  *
1381676Sjpk  * Also, in a labeled environment, packet labels need to be checked.  For fully
1391676Sjpk  * bound TCP connections, we can assume that the packet label was checked
1401676Sjpk  * during connection establishment, and doesn't need to be checked on each
1411676Sjpk  * packet.  For others, though, we need to check for strict equality or, for
1421676Sjpk  * multilevel ports, membership in the range or set.  This part currently does
1431676Sjpk  * a tnrh lookup on each packet, but could be optimized to use cached results
1441676Sjpk  * if that were necessary.  (SCTP doesn't come through here, but if it did,
1451676Sjpk  * we would apply the same rules as TCP.)
1461676Sjpk  *
1471676Sjpk  * An implication of the above is that fully-bound TCP sockets must always use
1481676Sjpk  * distinct 4-tuples; they can't be discriminated by label alone.
1491676Sjpk  *
1501676Sjpk  * Note that we cannot trust labels on packets sent to fully-bound UDP sockets,
1511676Sjpk  * as there's no connection set-up handshake and no shared state.
1521676Sjpk  *
1531676Sjpk  * Labels on looped-back packets within a single zone do not need to be
1541676Sjpk  * checked, as all processes in the same zone have the same label.
1551676Sjpk  *
1561676Sjpk  * Finally, for unlabeled packets received by a labeled system, special rules
1571676Sjpk  * apply.  We consider only the MLP if there is one.  Otherwise, we prefer a
1581676Sjpk  * socket in the zone whose label matches the default label of the sender, if
1591676Sjpk  * any.  In any event, the receiving socket must have SO_MAC_EXEMPT set and the
1601676Sjpk  * receiver's label must dominate the sender's default label.
1611676Sjpk  *
1623448Sdh155122  * conn_t *ipcl_tcp_lookup_reversed_ipv4(ipha_t *, tcph_t *, int, ip_stack);
1633448Sdh155122  * conn_t *ipcl_tcp_lookup_reversed_ipv6(ip6_t *, tcpha_t *, int, uint_t,
1643448Sdh155122  *					 ip_stack);
1650Sstevel@tonic-gate  *
1660Sstevel@tonic-gate  *	Lookup routine to find a exact match for {src, dst, local port,
1670Sstevel@tonic-gate  *	remote port) for TCP connections in ipcl_conn_fanout. The address and
1680Sstevel@tonic-gate  *	ports are read from the IP and TCP header respectively.
1690Sstevel@tonic-gate  *
1703448Sdh155122  * conn_t	*ipcl_lookup_listener_v4(lport, laddr, protocol,
1713448Sdh155122  *					 zoneid, ip_stack);
1723448Sdh155122  * conn_t	*ipcl_lookup_listener_v6(lport, laddr, protocol, ifindex,
1733448Sdh155122  *					 zoneid, ip_stack);
1740Sstevel@tonic-gate  *
1750Sstevel@tonic-gate  * 	Lookup routine to find a listener with the tuple {lport, laddr,
1760Sstevel@tonic-gate  * 	protocol} in the ipcl_bind_fanout table. For IPv6, an additional
1770Sstevel@tonic-gate  * 	parameter interface index is also compared.
1780Sstevel@tonic-gate  *
1793448Sdh155122  * void ipcl_walk(func, arg, ip_stack)
1800Sstevel@tonic-gate  *
1810Sstevel@tonic-gate  * 	Apply 'func' to every connection available. The 'func' is called as
1820Sstevel@tonic-gate  *	(*func)(connp, arg). The walk is non-atomic so connections may be
1830Sstevel@tonic-gate  *	created and destroyed during the walk. The CONN_CONDEMNED and
1840Sstevel@tonic-gate  *	CONN_INCIPIENT flags ensure that connections which are newly created
1850Sstevel@tonic-gate  *	or being destroyed are not selected by the walker.
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  * Table Updates
1880Sstevel@tonic-gate  * -------------
1890Sstevel@tonic-gate  *
1900Sstevel@tonic-gate  * int ipcl_conn_insert(connp, protocol, src, dst, ports)
1910Sstevel@tonic-gate  * int ipcl_conn_insert_v6(connp, protocol, src, dst, ports, ifindex)
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  *	Insert 'connp' in the ipcl_conn_fanout.
1940Sstevel@tonic-gate  *	Arguements :
1950Sstevel@tonic-gate  *		connp		conn_t to be inserted
1960Sstevel@tonic-gate  *		protocol	connection protocol
1970Sstevel@tonic-gate  *		src		source address
1980Sstevel@tonic-gate  *		dst		destination address
1990Sstevel@tonic-gate  *		ports		local and remote port
2000Sstevel@tonic-gate  *		ifindex		interface index for IPv6 connections
2010Sstevel@tonic-gate  *
2020Sstevel@tonic-gate  *	Return value :
2030Sstevel@tonic-gate  *		0		if connp was inserted
2040Sstevel@tonic-gate  *		EADDRINUSE	if the connection with the same tuple
2050Sstevel@tonic-gate  *				already exists.
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  * int ipcl_bind_insert(connp, protocol, src, lport);
2080Sstevel@tonic-gate  * int ipcl_bind_insert_v6(connp, protocol, src, lport);
2090Sstevel@tonic-gate  *
2100Sstevel@tonic-gate  * 	Insert 'connp' in ipcl_bind_fanout.
2110Sstevel@tonic-gate  * 	Arguements :
2120Sstevel@tonic-gate  * 		connp		conn_t to be inserted
2130Sstevel@tonic-gate  * 		protocol	connection protocol
2140Sstevel@tonic-gate  * 		src		source address connection wants
2150Sstevel@tonic-gate  * 				to bind to
2160Sstevel@tonic-gate  * 		lport		local port connection wants to
2170Sstevel@tonic-gate  * 				bind to
2180Sstevel@tonic-gate  *
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * void ipcl_hash_remove(connp);
2210Sstevel@tonic-gate  *
2220Sstevel@tonic-gate  * 	Removes the 'connp' from the connection fanout table.
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  * Connection Creation/Destruction
2250Sstevel@tonic-gate  * -------------------------------
2260Sstevel@tonic-gate  *
2273448Sdh155122  * conn_t *ipcl_conn_create(type, sleep, netstack_t *)
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  * 	Creates a new conn based on the type flag, inserts it into
2300Sstevel@tonic-gate  * 	globalhash table.
2310Sstevel@tonic-gate  *
2320Sstevel@tonic-gate  *	type:	This flag determines the type of conn_t which needs to be
2335240Snordmark  *		created i.e., which kmem_cache it comes from.
2340Sstevel@tonic-gate  *		IPCL_TCPCONN	indicates a TCP connection
2355240Snordmark  *		IPCL_SCTPCONN	indicates a SCTP connection
2365240Snordmark  *		IPCL_UDPCONN	indicates a UDP conn_t.
2375240Snordmark  *		IPCL_RAWIPCONN	indicates a RAWIP/ICMP conn_t.
2385240Snordmark  *		IPCL_RTSCONN	indicates a RTS conn_t.
2395240Snordmark  *		IPCL_IPCCONN	indicates all other connections.
2400Sstevel@tonic-gate  *
2410Sstevel@tonic-gate  * void ipcl_conn_destroy(connp)
2420Sstevel@tonic-gate  *
2430Sstevel@tonic-gate  * 	Destroys the connection state, removes it from the global
2440Sstevel@tonic-gate  * 	connection hash table and frees its memory.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate #include <sys/types.h>
2480Sstevel@tonic-gate #include <sys/stream.h>
2490Sstevel@tonic-gate #include <sys/stropts.h>
2500Sstevel@tonic-gate #include <sys/sysmacros.h>
2510Sstevel@tonic-gate #include <sys/strsubr.h>
2520Sstevel@tonic-gate #include <sys/strsun.h>
2530Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
2540Sstevel@tonic-gate #include <sys/ddi.h>
2550Sstevel@tonic-gate #include <sys/cmn_err.h>
2560Sstevel@tonic-gate #include <sys/debug.h>
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate #include <sys/systm.h>
2590Sstevel@tonic-gate #include <sys/param.h>
2600Sstevel@tonic-gate #include <sys/kmem.h>
2610Sstevel@tonic-gate #include <sys/isa_defs.h>
2620Sstevel@tonic-gate #include <inet/common.h>
2630Sstevel@tonic-gate #include <netinet/ip6.h>
2640Sstevel@tonic-gate #include <netinet/icmp6.h>
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate #include <inet/ip.h>
2670Sstevel@tonic-gate #include <inet/ip6.h>
2680Sstevel@tonic-gate #include <inet/tcp.h>
2690Sstevel@tonic-gate #include <inet/ip_ndp.h>
270741Smasputra #include <inet/udp_impl.h>
2710Sstevel@tonic-gate #include <inet/sctp_ip.h>
2723448Sdh155122 #include <inet/sctp/sctp_impl.h>
2735240Snordmark #include <inet/rawip_impl.h>
2745240Snordmark #include <inet/rts_impl.h>
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate #include <sys/cpuvar.h>
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate #include <inet/ipclassifier.h>
2790Sstevel@tonic-gate #include <inet/ipsec_impl.h>
2800Sstevel@tonic-gate 
2811676Sjpk #include <sys/tsol/tnet.h>
2821676Sjpk 
2830Sstevel@tonic-gate #ifdef DEBUG
2840Sstevel@tonic-gate #define	IPCL_DEBUG
2850Sstevel@tonic-gate #else
2860Sstevel@tonic-gate #undef	IPCL_DEBUG
2870Sstevel@tonic-gate #endif
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate #ifdef	IPCL_DEBUG
2900Sstevel@tonic-gate int	ipcl_debug_level = 0;
2910Sstevel@tonic-gate #define	IPCL_DEBUG_LVL(level, args)	\
2920Sstevel@tonic-gate 	if (ipcl_debug_level  & level) { printf args; }
2930Sstevel@tonic-gate #else
2940Sstevel@tonic-gate #define	IPCL_DEBUG_LVL(level, args) {; }
2950Sstevel@tonic-gate #endif
2963448Sdh155122 /* Old value for compatibility. Setable in /etc/system */
2970Sstevel@tonic-gate uint_t tcp_conn_hash_size = 0;
2980Sstevel@tonic-gate 
2993448Sdh155122 /* New value. Zero means choose automatically.  Setable in /etc/system */
3000Sstevel@tonic-gate uint_t ipcl_conn_hash_size = 0;
3010Sstevel@tonic-gate uint_t ipcl_conn_hash_memfactor = 8192;
3020Sstevel@tonic-gate uint_t ipcl_conn_hash_maxsize = 82500;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /* bind/udp fanout table size */
3050Sstevel@tonic-gate uint_t ipcl_bind_fanout_size = 512;
3061503Sericheng uint_t ipcl_udp_fanout_size = 16384;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate /* Raw socket fanout size.  Must be a power of 2. */
3090Sstevel@tonic-gate uint_t ipcl_raw_fanout_size = 256;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate  * Power of 2^N Primes useful for hashing for N of 0-28,
3130Sstevel@tonic-gate  * these primes are the nearest prime <= 2^N - 2^(N-2).
3140Sstevel@tonic-gate  */
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate #define	P2Ps() {0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,	\
3170Sstevel@tonic-gate 		6143, 12281, 24571, 49139, 98299, 196597, 393209,	\
3180Sstevel@tonic-gate 		786431, 1572853, 3145721, 6291449, 12582893, 25165813,	\
3190Sstevel@tonic-gate 		50331599, 100663291, 201326557, 0}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3225240Snordmark  * wrapper structure to ensure that conn and what follows it (tcp_t, etc)
3235240Snordmark  * are aligned on cache lines.
3240Sstevel@tonic-gate  */
3255240Snordmark typedef union itc_s {
3265240Snordmark 	conn_t	itc_conn;
3275240Snordmark 	char	itcu_filler[CACHE_ALIGN(conn_s)];
3280Sstevel@tonic-gate } itc_t;
3290Sstevel@tonic-gate 
3305240Snordmark struct kmem_cache  *tcp_conn_cache;
3315240Snordmark struct kmem_cache  *ip_conn_cache;
3320Sstevel@tonic-gate extern struct kmem_cache  *sctp_conn_cache;
3330Sstevel@tonic-gate extern struct kmem_cache  *tcp_sack_info_cache;
3340Sstevel@tonic-gate extern struct kmem_cache  *tcp_iphc_cache;
3355240Snordmark struct kmem_cache  *udp_conn_cache;
3365240Snordmark struct kmem_cache  *rawip_conn_cache;
3375240Snordmark struct kmem_cache  *rts_conn_cache;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate extern void	tcp_timermp_free(tcp_t *);
3400Sstevel@tonic-gate extern mblk_t	*tcp_timermp_alloc(int);
3410Sstevel@tonic-gate 
3425240Snordmark static int	ip_conn_constructor(void *, void *, int);
3435240Snordmark static void	ip_conn_destructor(void *, void *);
3445240Snordmark 
3455240Snordmark static int	tcp_conn_constructor(void *, void *, int);
3465240Snordmark static void	tcp_conn_destructor(void *, void *);
3475240Snordmark 
3485240Snordmark static int	udp_conn_constructor(void *, void *, int);
3495240Snordmark static void	udp_conn_destructor(void *, void *);
3505240Snordmark 
3515240Snordmark static int	rawip_conn_constructor(void *, void *, int);
3525240Snordmark static void	rawip_conn_destructor(void *, void *);
3535240Snordmark 
3545240Snordmark static int	rts_conn_constructor(void *, void *, int);
3555240Snordmark static void	rts_conn_destructor(void *, void *);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate #ifdef	IPCL_DEBUG
3580Sstevel@tonic-gate #define	INET_NTOA_BUFSIZE	18
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate static char *
3610Sstevel@tonic-gate inet_ntoa_r(uint32_t in, char *b)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	unsigned char	*p;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	p = (unsigned char *)&in;
3660Sstevel@tonic-gate 	(void) sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
3670Sstevel@tonic-gate 	return (b);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate #endif
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate /*
3723448Sdh155122  * Global (for all stack instances) init routine
3730Sstevel@tonic-gate  */
3740Sstevel@tonic-gate void
3753448Sdh155122 ipcl_g_init(void)
3760Sstevel@tonic-gate {
3775240Snordmark 	ip_conn_cache = kmem_cache_create("ip_conn_cache",
3780Sstevel@tonic-gate 	    sizeof (conn_t), CACHE_ALIGN_SIZE,
3795240Snordmark 	    ip_conn_constructor, ip_conn_destructor,
3805240Snordmark 	    NULL, NULL, NULL, 0);
3815240Snordmark 
3825240Snordmark 	tcp_conn_cache = kmem_cache_create("tcp_conn_cache",
3835240Snordmark 	    sizeof (itc_t) + sizeof (tcp_t), CACHE_ALIGN_SIZE,
3845240Snordmark 	    tcp_conn_constructor, tcp_conn_destructor,
3855240Snordmark 	    NULL, NULL, NULL, 0);
3860Sstevel@tonic-gate 
3875240Snordmark 	udp_conn_cache = kmem_cache_create("udp_conn_cache",
3885240Snordmark 	    sizeof (itc_t) + sizeof (udp_t), CACHE_ALIGN_SIZE,
3895240Snordmark 	    udp_conn_constructor, udp_conn_destructor,
3905240Snordmark 	    NULL, NULL, NULL, 0);
3915240Snordmark 
3925240Snordmark 	rawip_conn_cache = kmem_cache_create("rawip_conn_cache",
3935240Snordmark 	    sizeof (itc_t) + sizeof (icmp_t), CACHE_ALIGN_SIZE,
3945240Snordmark 	    rawip_conn_constructor, rawip_conn_destructor,
3955240Snordmark 	    NULL, NULL, NULL, 0);
3965240Snordmark 
3975240Snordmark 	rts_conn_cache = kmem_cache_create("rts_conn_cache",
3985240Snordmark 	    sizeof (itc_t) + sizeof (rts_t), CACHE_ALIGN_SIZE,
3995240Snordmark 	    rts_conn_constructor, rts_conn_destructor,
4000Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
4013448Sdh155122 }
4023448Sdh155122 
4033448Sdh155122 /*
4043448Sdh155122  * ipclassifier intialization routine, sets up hash tables.
4053448Sdh155122  */
4063448Sdh155122 void
4073448Sdh155122 ipcl_init(ip_stack_t *ipst)
4083448Sdh155122 {
4093448Sdh155122 	int i;
4103448Sdh155122 	int sizes[] = P2Ps();
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/*
4133448Sdh155122 	 * Calculate size of conn fanout table from /etc/system settings
4140Sstevel@tonic-gate 	 */
4150Sstevel@tonic-gate 	if (ipcl_conn_hash_size != 0) {
4163448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = ipcl_conn_hash_size;
4170Sstevel@tonic-gate 	} else if (tcp_conn_hash_size != 0) {
4183448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = tcp_conn_hash_size;
4190Sstevel@tonic-gate 	} else {
4200Sstevel@tonic-gate 		extern pgcnt_t freemem;
4210Sstevel@tonic-gate 
4223448Sdh155122 		ipst->ips_ipcl_conn_fanout_size =
4230Sstevel@tonic-gate 		    (freemem * PAGESIZE) / ipcl_conn_hash_memfactor;
4240Sstevel@tonic-gate 
4253448Sdh155122 		if (ipst->ips_ipcl_conn_fanout_size > ipcl_conn_hash_maxsize) {
4263448Sdh155122 			ipst->ips_ipcl_conn_fanout_size =
4273448Sdh155122 			    ipcl_conn_hash_maxsize;
4283448Sdh155122 		}
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	for (i = 9; i < sizeof (sizes) / sizeof (*sizes) - 1; i++) {
4323448Sdh155122 		if (sizes[i] >= ipst->ips_ipcl_conn_fanout_size) {
4330Sstevel@tonic-gate 			break;
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 	}
4363448Sdh155122 	if ((ipst->ips_ipcl_conn_fanout_size = sizes[i]) == 0) {
4370Sstevel@tonic-gate 		/* Out of range, use the 2^16 value */
4383448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = sizes[16];
4390Sstevel@tonic-gate 	}
4403448Sdh155122 
4413448Sdh155122 	/* Take values from /etc/system */
4423448Sdh155122 	ipst->ips_ipcl_bind_fanout_size = ipcl_bind_fanout_size;
4433448Sdh155122 	ipst->ips_ipcl_udp_fanout_size = ipcl_udp_fanout_size;
4443448Sdh155122 	ipst->ips_ipcl_raw_fanout_size = ipcl_raw_fanout_size;
4450Sstevel@tonic-gate 
4463448Sdh155122 	ASSERT(ipst->ips_ipcl_conn_fanout == NULL);
4473448Sdh155122 
4483448Sdh155122 	ipst->ips_ipcl_conn_fanout = kmem_zalloc(
4493448Sdh155122 	    ipst->ips_ipcl_conn_fanout_size * sizeof (connf_t), KM_SLEEP);
4503448Sdh155122 
4513448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
4523448Sdh155122 		mutex_init(&ipst->ips_ipcl_conn_fanout[i].connf_lock, NULL,
4530Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4563448Sdh155122 	ipst->ips_ipcl_bind_fanout = kmem_zalloc(
4573448Sdh155122 	    ipst->ips_ipcl_bind_fanout_size * sizeof (connf_t), KM_SLEEP);
4580Sstevel@tonic-gate 
4593448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
4603448Sdh155122 		mutex_init(&ipst->ips_ipcl_bind_fanout[i].connf_lock, NULL,
4610Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4643448Sdh155122 	ipst->ips_ipcl_proto_fanout = kmem_zalloc(IPPROTO_MAX *
4653448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4663448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4673448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout[i].connf_lock, NULL,
4680Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4690Sstevel@tonic-gate 	}
4703448Sdh155122 
4713448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = kmem_zalloc(IPPROTO_MAX *
4723448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4733448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4743448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock, NULL,
4750Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4783448Sdh155122 	ipst->ips_rts_clients = kmem_zalloc(sizeof (connf_t), KM_SLEEP);
4793448Sdh155122 	mutex_init(&ipst->ips_rts_clients->connf_lock,
4803448Sdh155122 	    NULL, MUTEX_DEFAULT, NULL);
4810Sstevel@tonic-gate 
4823448Sdh155122 	ipst->ips_ipcl_udp_fanout = kmem_zalloc(
4833448Sdh155122 	    ipst->ips_ipcl_udp_fanout_size * sizeof (connf_t), KM_SLEEP);
4843448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
4853448Sdh155122 		mutex_init(&ipst->ips_ipcl_udp_fanout[i].connf_lock, NULL,
4860Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4893448Sdh155122 	ipst->ips_ipcl_raw_fanout = kmem_zalloc(
4903448Sdh155122 	    ipst->ips_ipcl_raw_fanout_size * sizeof (connf_t), KM_SLEEP);
4913448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
4923448Sdh155122 		mutex_init(&ipst->ips_ipcl_raw_fanout[i].connf_lock, NULL,
4930Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4963448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = kmem_zalloc(
4973448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE, KM_SLEEP);
4980Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
4993448Sdh155122 		mutex_init(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock,
5003448Sdh155122 		    NULL, MUTEX_DEFAULT, NULL);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate void
5053448Sdh155122 ipcl_g_destroy(void)
5060Sstevel@tonic-gate {
5075240Snordmark 	kmem_cache_destroy(ip_conn_cache);
5085240Snordmark 	kmem_cache_destroy(tcp_conn_cache);
5095240Snordmark 	kmem_cache_destroy(udp_conn_cache);
5105240Snordmark 	kmem_cache_destroy(rawip_conn_cache);
5115240Snordmark 	kmem_cache_destroy(rts_conn_cache);
5123448Sdh155122 }
5133448Sdh155122 
5143448Sdh155122 /*
5153448Sdh155122  * All user-level and kernel use of the stack must be gone
5163448Sdh155122  * by now.
5173448Sdh155122  */
5183448Sdh155122 void
5193448Sdh155122 ipcl_destroy(ip_stack_t *ipst)
5203448Sdh155122 {
5213448Sdh155122 	int i;
5223448Sdh155122 
5233448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
5243448Sdh155122 		ASSERT(ipst->ips_ipcl_conn_fanout[i].connf_head == NULL);
5253448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_conn_fanout[i].connf_lock);
5263448Sdh155122 	}
5273448Sdh155122 	kmem_free(ipst->ips_ipcl_conn_fanout, ipst->ips_ipcl_conn_fanout_size *
5283448Sdh155122 	    sizeof (connf_t));
5293448Sdh155122 	ipst->ips_ipcl_conn_fanout = NULL;
5303448Sdh155122 
5313448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
5323448Sdh155122 		ASSERT(ipst->ips_ipcl_bind_fanout[i].connf_head == NULL);
5333448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_bind_fanout[i].connf_lock);
5343448Sdh155122 	}
5353448Sdh155122 	kmem_free(ipst->ips_ipcl_bind_fanout, ipst->ips_ipcl_bind_fanout_size *
5363448Sdh155122 	    sizeof (connf_t));
5373448Sdh155122 	ipst->ips_ipcl_bind_fanout = NULL;
5383448Sdh155122 
5393448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5403448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout[i].connf_head == NULL);
5413448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout[i].connf_lock);
5423448Sdh155122 	}
5433448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout, IPPROTO_MAX * sizeof (connf_t));
5443448Sdh155122 	ipst->ips_ipcl_proto_fanout = NULL;
5450Sstevel@tonic-gate 
5463448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5473448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout_v6[i].connf_head == NULL);
5483448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock);
5493448Sdh155122 	}
5503448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout_v6,
5513448Sdh155122 	    IPPROTO_MAX * sizeof (connf_t));
5523448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = NULL;
5533448Sdh155122 
5543448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
5553448Sdh155122 		ASSERT(ipst->ips_ipcl_udp_fanout[i].connf_head == NULL);
5563448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_udp_fanout[i].connf_lock);
5573448Sdh155122 	}
5583448Sdh155122 	kmem_free(ipst->ips_ipcl_udp_fanout, ipst->ips_ipcl_udp_fanout_size *
5593448Sdh155122 	    sizeof (connf_t));
5603448Sdh155122 	ipst->ips_ipcl_udp_fanout = NULL;
5610Sstevel@tonic-gate 
5623448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
5633448Sdh155122 		ASSERT(ipst->ips_ipcl_raw_fanout[i].connf_head == NULL);
5643448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_raw_fanout[i].connf_lock);
5653448Sdh155122 	}
5663448Sdh155122 	kmem_free(ipst->ips_ipcl_raw_fanout, ipst->ips_ipcl_raw_fanout_size *
5673448Sdh155122 	    sizeof (connf_t));
5683448Sdh155122 	ipst->ips_ipcl_raw_fanout = NULL;
5690Sstevel@tonic-gate 
5703448Sdh155122 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
5713448Sdh155122 		ASSERT(ipst->ips_ipcl_globalhash_fanout[i].connf_head == NULL);
5723448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
5733448Sdh155122 	}
5743448Sdh155122 	kmem_free(ipst->ips_ipcl_globalhash_fanout,
5753448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE);
5763448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = NULL;
5770Sstevel@tonic-gate 
5783448Sdh155122 	ASSERT(ipst->ips_rts_clients->connf_head == NULL);
5793448Sdh155122 	mutex_destroy(&ipst->ips_rts_clients->connf_lock);
5803448Sdh155122 	kmem_free(ipst->ips_rts_clients, sizeof (connf_t));
5813448Sdh155122 	ipst->ips_rts_clients = NULL;
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate  * conn creation routine. initialize the conn, sets the reference
5860Sstevel@tonic-gate  * and inserts it in the global hash table.
5870Sstevel@tonic-gate  */
5880Sstevel@tonic-gate conn_t *
5893448Sdh155122 ipcl_conn_create(uint32_t type, int sleep, netstack_t *ns)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	conn_t	*connp;
5923448Sdh155122 	sctp_stack_t *sctps;
5935240Snordmark 	struct kmem_cache *conn_cache;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	switch (type) {
5960Sstevel@tonic-gate 	case IPCL_SCTPCONN:
5970Sstevel@tonic-gate 		if ((connp = kmem_cache_alloc(sctp_conn_cache, sleep)) == NULL)
5980Sstevel@tonic-gate 			return (NULL);
5994691Skcpoon 		sctp_conn_init(connp);
6003448Sdh155122 		sctps = ns->netstack_sctp;
6013448Sdh155122 		SCTP_G_Q_REFHOLD(sctps);
6023448Sdh155122 		netstack_hold(ns);
6033448Sdh155122 		connp->conn_netstack = ns;
6045240Snordmark 		return (connp);
6055240Snordmark 
6065240Snordmark 	case IPCL_TCPCONN:
6075240Snordmark 		conn_cache = tcp_conn_cache;
6080Sstevel@tonic-gate 		break;
6095240Snordmark 
6105240Snordmark 	case IPCL_UDPCONN:
6115240Snordmark 		conn_cache = udp_conn_cache;
6125240Snordmark 		break;
6135240Snordmark 
6145240Snordmark 	case IPCL_RAWIPCONN:
6155240Snordmark 		conn_cache = rawip_conn_cache;
6165240Snordmark 		break;
6175240Snordmark 
6185240Snordmark 	case IPCL_RTSCONN:
6195240Snordmark 		conn_cache = rts_conn_cache;
6205240Snordmark 		break;
6215240Snordmark 
6220Sstevel@tonic-gate 	case IPCL_IPCCONN:
6235240Snordmark 		conn_cache = ip_conn_cache;
6240Sstevel@tonic-gate 		break;
6255240Snordmark 
626741Smasputra 	default:
627741Smasputra 		connp = NULL;
628741Smasputra 		ASSERT(0);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
6315240Snordmark 	if ((connp = kmem_cache_alloc(conn_cache, sleep)) == NULL)
6325240Snordmark 		return (NULL);
6335240Snordmark 
6345240Snordmark 	connp->conn_ref = 1;
6355240Snordmark 	netstack_hold(ns);
6365240Snordmark 	connp->conn_netstack = ns;
6375240Snordmark 	ipcl_globalhash_insert(connp);
6380Sstevel@tonic-gate 	return (connp);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate void
6420Sstevel@tonic-gate ipcl_conn_destroy(conn_t *connp)
6430Sstevel@tonic-gate {
6440Sstevel@tonic-gate 	mblk_t	*mp;
6453448Sdh155122 	netstack_t	*ns = connp->conn_netstack;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&connp->conn_lock));
6480Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6490Sstevel@tonic-gate 	ASSERT(connp->conn_ire_cache == NULL);
6500Sstevel@tonic-gate 
6511676Sjpk 	if (connp->conn_peercred != NULL &&
6521676Sjpk 	    connp->conn_peercred != connp->conn_cred)
6531676Sjpk 		crfree(connp->conn_peercred);
6541676Sjpk 	connp->conn_peercred = NULL;
6551676Sjpk 
6561676Sjpk 	if (connp->conn_cred != NULL) {
6571676Sjpk 		crfree(connp->conn_cred);
6581676Sjpk 		connp->conn_cred = NULL;
6591676Sjpk 	}
6601676Sjpk 
6610Sstevel@tonic-gate 	ipcl_globalhash_remove(connp);
6620Sstevel@tonic-gate 
6635240Snordmark 	/* FIXME: add separate tcp_conn_free()? */
6640Sstevel@tonic-gate 	if (connp->conn_flags & IPCL_TCPCONN) {
665741Smasputra 		tcp_t	*tcp = connp->conn_tcp;
6663448Sdh155122 		tcp_stack_t *tcps;
6673448Sdh155122 
6683448Sdh155122 		ASSERT(tcp != NULL);
6693448Sdh155122 		tcps = tcp->tcp_tcps;
6703448Sdh155122 		if (tcps != NULL) {
6713448Sdh155122 			if (connp->conn_latch != NULL) {
6723448Sdh155122 				IPLATCH_REFRELE(connp->conn_latch, ns);
6733448Sdh155122 				connp->conn_latch = NULL;
6743448Sdh155122 			}
6753448Sdh155122 			if (connp->conn_policy != NULL) {
6763448Sdh155122 				IPPH_REFRELE(connp->conn_policy, ns);
6773448Sdh155122 				connp->conn_policy = NULL;
6783448Sdh155122 			}
6793448Sdh155122 			tcp->tcp_tcps = NULL;
6803448Sdh155122 			TCPS_REFRELE(tcps);
6813448Sdh155122 		}
682741Smasputra 
6830Sstevel@tonic-gate 		tcp_free(tcp);
6840Sstevel@tonic-gate 		mp = tcp->tcp_timercache;
6851676Sjpk 		tcp->tcp_cred = NULL;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 		if (tcp->tcp_sack_info != NULL) {
6880Sstevel@tonic-gate 			bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
6890Sstevel@tonic-gate 			kmem_cache_free(tcp_sack_info_cache,
6900Sstevel@tonic-gate 			    tcp->tcp_sack_info);
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		if (tcp->tcp_iphc != NULL) {
6930Sstevel@tonic-gate 			if (tcp->tcp_hdr_grown) {
6940Sstevel@tonic-gate 				kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len);
6950Sstevel@tonic-gate 			} else {
6960Sstevel@tonic-gate 				bzero(tcp->tcp_iphc, tcp->tcp_iphc_len);
6970Sstevel@tonic-gate 				kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc);
6980Sstevel@tonic-gate 			}
6990Sstevel@tonic-gate 			tcp->tcp_iphc_len = 0;
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 		ASSERT(tcp->tcp_iphc_len == 0);
7020Sstevel@tonic-gate 
7033448Sdh155122 		ASSERT(connp->conn_latch == NULL);
7043448Sdh155122 		ASSERT(connp->conn_policy == NULL);
7053448Sdh155122 
7063448Sdh155122 		if (ns != NULL) {
7073448Sdh155122 			ASSERT(tcp->tcp_tcps == NULL);
7083448Sdh155122 			connp->conn_netstack = NULL;
7093448Sdh155122 			netstack_rele(ns);
7103448Sdh155122 		}
7115240Snordmark 
7125240Snordmark 		ipcl_conn_cleanup(connp);
7135240Snordmark 		connp->conn_flags = IPCL_TCPCONN;
7145240Snordmark 		bzero(tcp, sizeof (tcp_t));
7155240Snordmark 
7165240Snordmark 		tcp->tcp_timercache = mp;
7175240Snordmark 		tcp->tcp_connp = connp;
7185240Snordmark 		kmem_cache_free(tcp_conn_cache, connp);
7195240Snordmark 		return;
7205240Snordmark 	}
7215240Snordmark 	if (connp->conn_latch != NULL) {
7225240Snordmark 		IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack);
7235240Snordmark 		connp->conn_latch = NULL;
7245240Snordmark 	}
7255240Snordmark 	if (connp->conn_policy != NULL) {
7265240Snordmark 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
7275240Snordmark 		connp->conn_policy = NULL;
7285240Snordmark 	}
7295240Snordmark 	if (connp->conn_ipsec_opt_mp != NULL) {
7305240Snordmark 		freemsg(connp->conn_ipsec_opt_mp);
7315240Snordmark 		connp->conn_ipsec_opt_mp = NULL;
7325240Snordmark 	}
7335240Snordmark 
7345240Snordmark 	if (connp->conn_flags & IPCL_SCTPCONN) {
7353448Sdh155122 		ASSERT(ns != NULL);
7360Sstevel@tonic-gate 		sctp_free(connp);
7375240Snordmark 		return;
7385240Snordmark 	}
7395240Snordmark 
7405240Snordmark 	if (ns != NULL) {
7415240Snordmark 		connp->conn_netstack = NULL;
7425240Snordmark 		netstack_rele(ns);
7435240Snordmark 	}
7445240Snordmark 	ipcl_conn_cleanup(connp);
7455240Snordmark 
7465240Snordmark 	/* leave conn_priv aka conn_udp, conn_icmp, etc in place. */
7475240Snordmark 	if (connp->conn_flags & IPCL_UDPCONN) {
7485240Snordmark 		connp->conn_flags = IPCL_UDPCONN;
7495240Snordmark 		kmem_cache_free(udp_conn_cache, connp);
7505240Snordmark 	} else if (connp->conn_flags & IPCL_RAWIPCONN) {
7515240Snordmark 		connp->conn_flags = IPCL_RAWIPCONN;
7525240Snordmark 		connp->conn_ulp = IPPROTO_ICMP;
7535240Snordmark 		kmem_cache_free(rawip_conn_cache, connp);
7545240Snordmark 	} else if (connp->conn_flags & IPCL_RTSCONN) {
7555240Snordmark 		connp->conn_flags = IPCL_RTSCONN;
7565240Snordmark 		kmem_cache_free(rts_conn_cache, connp);
7570Sstevel@tonic-gate 	} else {
7585240Snordmark 		connp->conn_flags = IPCL_IPCCONN;
7595240Snordmark 		ASSERT(connp->conn_flags & IPCL_IPCCONN);
7605240Snordmark 		ASSERT(connp->conn_priv == NULL);
7615240Snordmark 		kmem_cache_free(ip_conn_cache, connp);
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * Running in cluster mode - deregister listener information
7670Sstevel@tonic-gate  */
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate static void
7700Sstevel@tonic-gate ipcl_conn_unlisten(conn_t *connp)
7710Sstevel@tonic-gate {
7720Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) != 0);
7730Sstevel@tonic-gate 	ASSERT(connp->conn_lport != 0);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if (cl_inet_unlisten != NULL) {
7760Sstevel@tonic-gate 		sa_family_t	addr_family;
7770Sstevel@tonic-gate 		uint8_t		*laddrp;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		if (connp->conn_pkt_isv6) {
7800Sstevel@tonic-gate 			addr_family = AF_INET6;
7810Sstevel@tonic-gate 			laddrp = (uint8_t *)&connp->conn_bound_source_v6;
7820Sstevel@tonic-gate 		} else {
7830Sstevel@tonic-gate 			addr_family = AF_INET;
7840Sstevel@tonic-gate 			laddrp = (uint8_t *)&connp->conn_bound_source;
7850Sstevel@tonic-gate 		}
7860Sstevel@tonic-gate 		(*cl_inet_unlisten)(IPPROTO_TCP, addr_family, laddrp,
7870Sstevel@tonic-gate 		    connp->conn_lport);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	connp->conn_flags &= ~IPCL_CL_LISTENER;
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate /*
7930Sstevel@tonic-gate  * We set the IPCL_REMOVED flag (instead of clearing the flag indicating
7940Sstevel@tonic-gate  * which table the conn belonged to). So for debugging we can see which hash
7950Sstevel@tonic-gate  * table this connection was in.
7960Sstevel@tonic-gate  */
7970Sstevel@tonic-gate #define	IPCL_HASH_REMOVE(connp)	{					\
7980Sstevel@tonic-gate 	connf_t	*connfp = (connp)->conn_fanout;				\
7990Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&((connp)->conn_lock)));			\
8000Sstevel@tonic-gate 	if (connfp != NULL) {						\
8010Sstevel@tonic-gate 		IPCL_DEBUG_LVL(4, ("IPCL_HASH_REMOVE: connp %p",	\
8020Sstevel@tonic-gate 		    (void *)(connp)));					\
8030Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);			\
8040Sstevel@tonic-gate 		if ((connp)->conn_next != NULL)				\
8050Sstevel@tonic-gate 			(connp)->conn_next->conn_prev =			\
8060Sstevel@tonic-gate 			    (connp)->conn_prev;				\
8070Sstevel@tonic-gate 		if ((connp)->conn_prev != NULL)				\
8080Sstevel@tonic-gate 			(connp)->conn_prev->conn_next =			\
8090Sstevel@tonic-gate 			    (connp)->conn_next;				\
8100Sstevel@tonic-gate 		else							\
8110Sstevel@tonic-gate 			connfp->connf_head = (connp)->conn_next;	\
8120Sstevel@tonic-gate 		(connp)->conn_fanout = NULL;				\
8130Sstevel@tonic-gate 		(connp)->conn_next = NULL;				\
8140Sstevel@tonic-gate 		(connp)->conn_prev = NULL;				\
8150Sstevel@tonic-gate 		(connp)->conn_flags |= IPCL_REMOVED;			\
8160Sstevel@tonic-gate 		if (((connp)->conn_flags & IPCL_CL_LISTENER) != 0)	\
8170Sstevel@tonic-gate 			ipcl_conn_unlisten((connp));			\
8180Sstevel@tonic-gate 		CONN_DEC_REF((connp));					\
8190Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);			\
8200Sstevel@tonic-gate 	}								\
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate void
8240Sstevel@tonic-gate ipcl_hash_remove(conn_t *connp)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	IPCL_HASH_REMOVE(connp);
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate /*
8300Sstevel@tonic-gate  * The whole purpose of this function is allow removal of
8310Sstevel@tonic-gate  * a conn_t from the connected hash for timewait reclaim.
8320Sstevel@tonic-gate  * This is essentially a TW reclaim fastpath where timewait
8330Sstevel@tonic-gate  * collector checks under fanout lock (so no one else can
8340Sstevel@tonic-gate  * get access to the conn_t) that refcnt is 2 i.e. one for
8350Sstevel@tonic-gate  * TCP and one for the classifier hash list. If ref count
8360Sstevel@tonic-gate  * is indeed 2, we can just remove the conn under lock and
8370Sstevel@tonic-gate  * avoid cleaning up the conn under squeue. This gives us
8380Sstevel@tonic-gate  * improved performance.
8390Sstevel@tonic-gate  */
8400Sstevel@tonic-gate void
8410Sstevel@tonic-gate ipcl_hash_remove_locked(conn_t *connp, connf_t	*connfp)
8420Sstevel@tonic-gate {
8430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connfp->connf_lock));
8440Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
8450Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) == 0);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	if ((connp)->conn_next != NULL) {
8484691Skcpoon 		(connp)->conn_next->conn_prev = (connp)->conn_prev;
8490Sstevel@tonic-gate 	}
8500Sstevel@tonic-gate 	if ((connp)->conn_prev != NULL) {
8514691Skcpoon 		(connp)->conn_prev->conn_next = (connp)->conn_next;
8520Sstevel@tonic-gate 	} else {
8530Sstevel@tonic-gate 		connfp->connf_head = (connp)->conn_next;
8540Sstevel@tonic-gate 	}
8550Sstevel@tonic-gate 	(connp)->conn_fanout = NULL;
8560Sstevel@tonic-gate 	(connp)->conn_next = NULL;
8570Sstevel@tonic-gate 	(connp)->conn_prev = NULL;
8580Sstevel@tonic-gate 	(connp)->conn_flags |= IPCL_REMOVED;
8590Sstevel@tonic-gate 	ASSERT((connp)->conn_ref == 2);
8600Sstevel@tonic-gate 	(connp)->conn_ref--;
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp) {		\
8640Sstevel@tonic-gate 	ASSERT((connp)->conn_fanout == NULL);				\
8650Sstevel@tonic-gate 	ASSERT((connp)->conn_next == NULL);				\
8660Sstevel@tonic-gate 	ASSERT((connp)->conn_prev == NULL);				\
8670Sstevel@tonic-gate 	if ((connfp)->connf_head != NULL) {				\
8680Sstevel@tonic-gate 		(connfp)->connf_head->conn_prev = (connp);		\
8690Sstevel@tonic-gate 		(connp)->conn_next = (connfp)->connf_head;		\
8700Sstevel@tonic-gate 	}								\
8710Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8720Sstevel@tonic-gate 	(connfp)->connf_head = (connp);					\
8730Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8740Sstevel@tonic-gate 	    IPCL_CONNECTED;						\
8750Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED(connfp, connp) {			\
8790Sstevel@tonic-gate 	IPCL_DEBUG_LVL(8, ("IPCL_HASH_INSERT_CONNECTED: connfp %p "	\
8800Sstevel@tonic-gate 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
8810Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8820Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8830Sstevel@tonic-gate 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);		\
8840Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate #define	IPCL_HASH_INSERT_BOUND(connfp, connp) {				\
8880Sstevel@tonic-gate 	conn_t *pconnp = NULL, *nconnp;					\
8890Sstevel@tonic-gate 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_BOUND: connfp %p "	\
8900Sstevel@tonic-gate 	    "connp %p", (void *)connfp, (void *)(connp)));		\
8910Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8920Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8930Sstevel@tonic-gate 	nconnp = (connfp)->connf_head;					\
894153Sethindra 	while (nconnp != NULL &&					\
895153Sethindra 	    !_IPCL_V4_MATCH_ANY(nconnp->conn_srcv6)) {			\
896153Sethindra 		pconnp = nconnp;					\
897153Sethindra 		nconnp = nconnp->conn_next;				\
8980Sstevel@tonic-gate 	}								\
8990Sstevel@tonic-gate 	if (pconnp != NULL) {						\
9000Sstevel@tonic-gate 		pconnp->conn_next = (connp);				\
9010Sstevel@tonic-gate 		(connp)->conn_prev = pconnp;				\
9020Sstevel@tonic-gate 	} else {							\
9030Sstevel@tonic-gate 		(connfp)->connf_head = (connp);				\
9040Sstevel@tonic-gate 	}								\
9050Sstevel@tonic-gate 	if (nconnp != NULL) {						\
9060Sstevel@tonic-gate 		(connp)->conn_next = nconnp;				\
9070Sstevel@tonic-gate 		nconnp->conn_prev = (connp);				\
9080Sstevel@tonic-gate 	}								\
9090Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9100Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9110Sstevel@tonic-gate 	    IPCL_BOUND;							\
9120Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
9130Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate #define	IPCL_HASH_INSERT_WILDCARD(connfp, connp) {			\
9170Sstevel@tonic-gate 	conn_t **list, *prev, *next;					\
9180Sstevel@tonic-gate 	boolean_t isv4mapped =						\
9190Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&(connp)->conn_srcv6);			\
9200Sstevel@tonic-gate 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_WILDCARD: connfp %p "	\
9210Sstevel@tonic-gate 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
9220Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
9230Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
9240Sstevel@tonic-gate 	list = &(connfp)->connf_head;					\
9250Sstevel@tonic-gate 	prev = NULL;							\
9260Sstevel@tonic-gate 	while ((next = *list) != NULL) {				\
9270Sstevel@tonic-gate 		if (isv4mapped &&					\
9280Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&next->conn_srcv6) &&	\
9290Sstevel@tonic-gate 		    connp->conn_zoneid == next->conn_zoneid) {		\
9300Sstevel@tonic-gate 			(connp)->conn_next = next;			\
9310Sstevel@tonic-gate 			if (prev != NULL)				\
9320Sstevel@tonic-gate 				prev = next->conn_prev;			\
9330Sstevel@tonic-gate 			next->conn_prev = (connp);			\
9340Sstevel@tonic-gate 			break;						\
9350Sstevel@tonic-gate 		}							\
9360Sstevel@tonic-gate 		list = &next->conn_next;				\
9370Sstevel@tonic-gate 		prev = next;						\
9380Sstevel@tonic-gate 	}								\
9390Sstevel@tonic-gate 	(connp)->conn_prev = prev;					\
9400Sstevel@tonic-gate 	*list = (connp);						\
9410Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9420Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9430Sstevel@tonic-gate 	    IPCL_BOUND;							\
9440Sstevel@tonic-gate 	CONN_INC_REF((connp));						\
9450Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate void
9490Sstevel@tonic-gate ipcl_hash_insert_wildcard(connf_t *connfp, conn_t *connp)
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate void
9550Sstevel@tonic-gate ipcl_proto_insert(conn_t *connp, uint8_t protocol)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	connf_t	*connfp;
9583448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	ASSERT(connp != NULL);
9611676Sjpk 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
9621676Sjpk 	    protocol == IPPROTO_ESP);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	connp->conn_ulp = protocol;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/* Insert it in the protocol hash */
9673448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[protocol];
9680Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate void
9720Sstevel@tonic-gate ipcl_proto_insert_v6(conn_t *connp, uint8_t protocol)
9730Sstevel@tonic-gate {
9740Sstevel@tonic-gate 	connf_t	*connfp;
9753448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	ASSERT(connp != NULL);
9781676Sjpk 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
9791676Sjpk 	    protocol == IPPROTO_ESP);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	connp->conn_ulp = protocol;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/* Insert it in the Bind Hash */
9843448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
9850Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate /*
9890Sstevel@tonic-gate  * This function is used only for inserting SCTP raw socket now.
9900Sstevel@tonic-gate  * This may change later.
9910Sstevel@tonic-gate  *
9920Sstevel@tonic-gate  * Note that only one raw socket can be bound to a port.  The param
9930Sstevel@tonic-gate  * lport is in network byte order.
9940Sstevel@tonic-gate  */
9950Sstevel@tonic-gate static int
9960Sstevel@tonic-gate ipcl_sctp_hash_insert(conn_t *connp, in_port_t lport)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	connf_t	*connfp;
9990Sstevel@tonic-gate 	conn_t	*oconnp;
10003448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
10010Sstevel@tonic-gate 
10023448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	/* Check for existing raw socket already bound to the port. */
10050Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
10060Sstevel@tonic-gate 	for (oconnp = connfp->connf_head; oconnp != NULL;
1007409Skcpoon 	    oconnp = oconnp->conn_next) {
10080Sstevel@tonic-gate 		if (oconnp->conn_lport == lport &&
10090Sstevel@tonic-gate 		    oconnp->conn_zoneid == connp->conn_zoneid &&
10100Sstevel@tonic-gate 		    oconnp->conn_af_isv6 == connp->conn_af_isv6 &&
10110Sstevel@tonic-gate 		    ((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
10120Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&oconnp->conn_srcv6) ||
10130Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6) ||
10140Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&oconnp->conn_srcv6)) ||
10150Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&oconnp->conn_srcv6,
10160Sstevel@tonic-gate 		    &connp->conn_srcv6))) {
10170Sstevel@tonic-gate 			break;
10180Sstevel@tonic-gate 		}
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
10210Sstevel@tonic-gate 	if (oconnp != NULL)
10220Sstevel@tonic-gate 		return (EADDRNOTAVAIL);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6) ||
10250Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_remv6)) {
10260Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
10270Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6)) {
10280Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10290Sstevel@tonic-gate 		} else {
10300Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
10310Sstevel@tonic-gate 		}
10320Sstevel@tonic-gate 	} else {
10330Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED(connfp, connp);
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 	return (0);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate /*
10391676Sjpk  * Check for a MAC exemption conflict on a labeled system.  Note that for
10401676Sjpk  * protocols that use port numbers (UDP, TCP, SCTP), we do this check up in the
10411676Sjpk  * transport layer.  This check is for binding all other protocols.
10421676Sjpk  *
10431676Sjpk  * Returns true if there's a conflict.
10441676Sjpk  */
10451676Sjpk static boolean_t
10463448Sdh155122 check_exempt_conflict_v4(conn_t *connp, ip_stack_t *ipst)
10471676Sjpk {
10481676Sjpk 	connf_t	*connfp;
10491676Sjpk 	conn_t *tconn;
10501676Sjpk 
10513448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[connp->conn_ulp];
10521676Sjpk 	mutex_enter(&connfp->connf_lock);
10531676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
10541676Sjpk 	    tconn = tconn->conn_next) {
10551676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
10561676Sjpk 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
10571676Sjpk 			continue;
10581676Sjpk 		/* If neither is exempt, then there's no conflict */
10591676Sjpk 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
10601676Sjpk 			continue;
10611676Sjpk 		/* If both are bound to different specific addrs, ok */
10621676Sjpk 		if (connp->conn_src != INADDR_ANY &&
10631676Sjpk 		    tconn->conn_src != INADDR_ANY &&
10641676Sjpk 		    connp->conn_src != tconn->conn_src)
10651676Sjpk 			continue;
10661676Sjpk 		/* These two conflict; fail */
10671676Sjpk 		break;
10681676Sjpk 	}
10691676Sjpk 	mutex_exit(&connfp->connf_lock);
10701676Sjpk 	return (tconn != NULL);
10711676Sjpk }
10721676Sjpk 
10731676Sjpk static boolean_t
10743448Sdh155122 check_exempt_conflict_v6(conn_t *connp, ip_stack_t *ipst)
10751676Sjpk {
10761676Sjpk 	connf_t	*connfp;
10771676Sjpk 	conn_t *tconn;
10781676Sjpk 
10793448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[connp->conn_ulp];
10801676Sjpk 	mutex_enter(&connfp->connf_lock);
10811676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
10821676Sjpk 	    tconn = tconn->conn_next) {
10831676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
10841676Sjpk 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
10851676Sjpk 			continue;
10861676Sjpk 		/* If neither is exempt, then there's no conflict */
10871676Sjpk 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
10881676Sjpk 			continue;
10891676Sjpk 		/* If both are bound to different addrs, ok */
10901676Sjpk 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) &&
10911676Sjpk 		    !IN6_IS_ADDR_UNSPECIFIED(&tconn->conn_srcv6) &&
10921676Sjpk 		    !IN6_ARE_ADDR_EQUAL(&connp->conn_srcv6, &tconn->conn_srcv6))
10931676Sjpk 			continue;
10941676Sjpk 		/* These two conflict; fail */
10951676Sjpk 		break;
10961676Sjpk 	}
10971676Sjpk 	mutex_exit(&connfp->connf_lock);
10981676Sjpk 	return (tconn != NULL);
10991676Sjpk }
11001676Sjpk 
11011676Sjpk /*
11020Sstevel@tonic-gate  * (v4, v6) bind hash insertion routines
11030Sstevel@tonic-gate  */
11040Sstevel@tonic-gate int
11050Sstevel@tonic-gate ipcl_bind_insert(conn_t *connp, uint8_t protocol, ipaddr_t src, uint16_t lport)
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate 	connf_t	*connfp;
11080Sstevel@tonic-gate #ifdef	IPCL_DEBUG
11090Sstevel@tonic-gate 	char	buf[INET_NTOA_BUFSIZE];
11100Sstevel@tonic-gate #endif
11110Sstevel@tonic-gate 	int	ret = 0;
11123448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	ASSERT(connp);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	IPCL_DEBUG_LVL(64, ("ipcl_bind_insert: connp %p, src = %s, "
11170Sstevel@tonic-gate 	    "port = %d\n", (void *)connp, inet_ntoa_r(src, buf), lport));
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	connp->conn_ulp = protocol;
11200Sstevel@tonic-gate 	IN6_IPADDR_TO_V4MAPPED(src, &connp->conn_srcv6);
11210Sstevel@tonic-gate 	connp->conn_lport = lport;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	switch (protocol) {
11241676Sjpk 	default:
11253448Sdh155122 		if (is_system_labeled() &&
11263448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
11271676Sjpk 			return (EADDRINUSE);
11281676Sjpk 		/* FALLTHROUGH */
11290Sstevel@tonic-gate 	case IPPROTO_UDP:
11300Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
11310Sstevel@tonic-gate 			IPCL_DEBUG_LVL(64,
11320Sstevel@tonic-gate 			    ("ipcl_bind_insert: connp %p - udp\n",
11330Sstevel@tonic-gate 			    (void *)connp));
11343448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
11353448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
11360Sstevel@tonic-gate 		} else {
11370Sstevel@tonic-gate 			IPCL_DEBUG_LVL(64,
11380Sstevel@tonic-gate 			    ("ipcl_bind_insert: connp %p - protocol\n",
11390Sstevel@tonic-gate 			    (void *)connp));
11403448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout[protocol];
11410Sstevel@tonic-gate 		}
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 		if (connp->conn_rem != INADDR_ANY) {
11440Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
11450Sstevel@tonic-gate 		} else if (connp->conn_src != INADDR_ANY) {
11460Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
11470Sstevel@tonic-gate 		} else {
11480Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
11490Sstevel@tonic-gate 		}
11500Sstevel@tonic-gate 		break;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	case IPPROTO_TCP:
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
11551676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
11563448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
11573448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
11580Sstevel@tonic-gate 		if (connp->conn_src != INADDR_ANY) {
11590Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
11600Sstevel@tonic-gate 		} else {
11610Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
11620Sstevel@tonic-gate 		}
11630Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
11640Sstevel@tonic-gate 			ASSERT(!connp->conn_pkt_isv6);
11650Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
11660Sstevel@tonic-gate 			(*cl_inet_listen)(IPPROTO_TCP, AF_INET,
11670Sstevel@tonic-gate 			    (uint8_t *)&connp->conn_bound_source, lport);
11680Sstevel@tonic-gate 		}
11690Sstevel@tonic-gate 		break;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	case IPPROTO_SCTP:
11720Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
11730Sstevel@tonic-gate 		break;
11740Sstevel@tonic-gate 	}
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	return (ret);
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate int
11800Sstevel@tonic-gate ipcl_bind_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
11810Sstevel@tonic-gate     uint16_t lport)
11820Sstevel@tonic-gate {
11830Sstevel@tonic-gate 	connf_t	*connfp;
11840Sstevel@tonic-gate 	int	ret = 0;
11853448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	ASSERT(connp);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	connp->conn_ulp = protocol;
11900Sstevel@tonic-gate 	connp->conn_srcv6 = *src;
11910Sstevel@tonic-gate 	connp->conn_lport = lport;
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	switch (protocol) {
11941676Sjpk 	default:
11953448Sdh155122 		if (is_system_labeled() &&
11963448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
11971676Sjpk 			return (EADDRINUSE);
11981676Sjpk 		/* FALLTHROUGH */
11990Sstevel@tonic-gate 	case IPPROTO_UDP:
12000Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
12010Sstevel@tonic-gate 			IPCL_DEBUG_LVL(128,
12020Sstevel@tonic-gate 			    ("ipcl_bind_insert_v6: connp %p - udp\n",
12030Sstevel@tonic-gate 			    (void *)connp));
12043448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
12053448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
12060Sstevel@tonic-gate 		} else {
12070Sstevel@tonic-gate 			IPCL_DEBUG_LVL(128,
12080Sstevel@tonic-gate 			    ("ipcl_bind_insert_v6: connp %p - protocol\n",
12090Sstevel@tonic-gate 			    (void *)connp));
12103448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
12140Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
12150Sstevel@tonic-gate 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
12160Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12170Sstevel@tonic-gate 		} else {
12180Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 		break;
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	case IPPROTO_TCP:
12230Sstevel@tonic-gate 		/* XXX - Need a separate table for IN6_IS_ADDR_UNSPECIFIED? */
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
12261676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
12273448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
12283448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
12290Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
12300Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12310Sstevel@tonic-gate 		} else {
12320Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12330Sstevel@tonic-gate 		}
12340Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
12350Sstevel@tonic-gate 			sa_family_t	addr_family;
12360Sstevel@tonic-gate 			uint8_t		*laddrp;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 			if (connp->conn_pkt_isv6) {
12390Sstevel@tonic-gate 				addr_family = AF_INET6;
12400Sstevel@tonic-gate 				laddrp =
12410Sstevel@tonic-gate 				    (uint8_t *)&connp->conn_bound_source_v6;
12420Sstevel@tonic-gate 			} else {
12430Sstevel@tonic-gate 				addr_family = AF_INET;
12440Sstevel@tonic-gate 				laddrp = (uint8_t *)&connp->conn_bound_source;
12450Sstevel@tonic-gate 			}
12460Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
12470Sstevel@tonic-gate 			(*cl_inet_listen)(IPPROTO_TCP, addr_family, laddrp,
12480Sstevel@tonic-gate 			    lport);
12490Sstevel@tonic-gate 		}
12500Sstevel@tonic-gate 		break;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	case IPPROTO_SCTP:
12530Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
12540Sstevel@tonic-gate 		break;
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	return (ret);
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate /*
12610Sstevel@tonic-gate  * ipcl_conn_hash insertion routines.
12620Sstevel@tonic-gate  */
12630Sstevel@tonic-gate int
12640Sstevel@tonic-gate ipcl_conn_insert(conn_t *connp, uint8_t protocol, ipaddr_t src,
12650Sstevel@tonic-gate     ipaddr_t rem, uint32_t ports)
12660Sstevel@tonic-gate {
12670Sstevel@tonic-gate 	connf_t		*connfp;
12680Sstevel@tonic-gate 	uint16_t	*up;
12690Sstevel@tonic-gate 	conn_t		*tconnp;
12700Sstevel@tonic-gate #ifdef	IPCL_DEBUG
12710Sstevel@tonic-gate 	char	sbuf[INET_NTOA_BUFSIZE], rbuf[INET_NTOA_BUFSIZE];
12720Sstevel@tonic-gate #endif
12730Sstevel@tonic-gate 	in_port_t	lport;
12740Sstevel@tonic-gate 	int		ret = 0;
12753448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	IPCL_DEBUG_LVL(256, ("ipcl_conn_insert: connp %p, src = %s, "
12780Sstevel@tonic-gate 	    "dst = %s, ports = %x, protocol = %x", (void *)connp,
12790Sstevel@tonic-gate 	    inet_ntoa_r(src, sbuf), inet_ntoa_r(rem, rbuf),
12800Sstevel@tonic-gate 	    ports, protocol));
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	switch (protocol) {
12830Sstevel@tonic-gate 	case IPPROTO_TCP:
12840Sstevel@tonic-gate 		if (!(connp->conn_flags & IPCL_EAGER)) {
12850Sstevel@tonic-gate 			/*
12860Sstevel@tonic-gate 			 * for a eager connection, i.e connections which
12870Sstevel@tonic-gate 			 * have just been created, the initialization is
12880Sstevel@tonic-gate 			 * already done in ip at conn_creation time, so
12890Sstevel@tonic-gate 			 * we can skip the checks here.
12900Sstevel@tonic-gate 			 */
12910Sstevel@tonic-gate 			IPCL_CONN_INIT(connp, protocol, src, rem, ports);
12920Sstevel@tonic-gate 		}
12933448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
12943448Sdh155122 		    IPCL_CONN_HASH(connp->conn_rem,
12953448Sdh155122 		    connp->conn_ports, ipst)];
12960Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
12970Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
12980Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
12990Sstevel@tonic-gate 			if (IPCL_CONN_MATCH(tconnp, connp->conn_ulp,
13000Sstevel@tonic-gate 			    connp->conn_rem, connp->conn_src,
13010Sstevel@tonic-gate 			    connp->conn_ports)) {
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 				/* Already have a conn. bail out */
13040Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
13050Sstevel@tonic-gate 				return (EADDRINUSE);
13060Sstevel@tonic-gate 			}
13070Sstevel@tonic-gate 		}
13080Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
13090Sstevel@tonic-gate 			/*
13100Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
13110Sstevel@tonic-gate 			 * rebind. Let it happen.
13120Sstevel@tonic-gate 			 */
13130Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
13140Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
13150Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
13160Sstevel@tonic-gate 		}
13173104Sjprakash 
13183104Sjprakash 		ASSERT(connp->conn_recv != NULL);
13193104Sjprakash 
13200Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
13210Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
13220Sstevel@tonic-gate 		break;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	case IPPROTO_SCTP:
1325409Skcpoon 		/*
1326409Skcpoon 		 * The raw socket may have already been bound, remove it
1327409Skcpoon 		 * from the hash first.
1328409Skcpoon 		 */
1329409Skcpoon 		IPCL_HASH_REMOVE(connp);
1330409Skcpoon 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
13310Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
13320Sstevel@tonic-gate 		break;
13330Sstevel@tonic-gate 
13341676Sjpk 	default:
13351676Sjpk 		/*
13361676Sjpk 		 * Check for conflicts among MAC exempt bindings.  For
13371676Sjpk 		 * transports with port numbers, this is done by the upper
13381676Sjpk 		 * level per-transport binding logic.  For all others, it's
13391676Sjpk 		 * done here.
13401676Sjpk 		 */
13413448Sdh155122 		if (is_system_labeled() &&
13423448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
13431676Sjpk 			return (EADDRINUSE);
13441676Sjpk 		/* FALLTHROUGH */
13451676Sjpk 
13460Sstevel@tonic-gate 	case IPPROTO_UDP:
13470Sstevel@tonic-gate 		up = (uint16_t *)&ports;
13480Sstevel@tonic-gate 		IPCL_CONN_INIT(connp, protocol, src, rem, ports);
13490Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
13503448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
13513448Sdh155122 			    IPCL_UDP_HASH(up[1], ipst)];
13520Sstevel@tonic-gate 		} else {
13533448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout[protocol];
13540Sstevel@tonic-gate 		}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 		if (connp->conn_rem != INADDR_ANY) {
13570Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
13580Sstevel@tonic-gate 		} else if (connp->conn_src != INADDR_ANY) {
13590Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
13600Sstevel@tonic-gate 		} else {
13610Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
13620Sstevel@tonic-gate 		}
13630Sstevel@tonic-gate 		break;
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	return (ret);
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate int
13700Sstevel@tonic-gate ipcl_conn_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
13710Sstevel@tonic-gate     const in6_addr_t *rem, uint32_t ports, uint_t ifindex)
13720Sstevel@tonic-gate {
13730Sstevel@tonic-gate 	connf_t		*connfp;
13740Sstevel@tonic-gate 	uint16_t	*up;
13750Sstevel@tonic-gate 	conn_t		*tconnp;
13760Sstevel@tonic-gate 	in_port_t	lport;
13770Sstevel@tonic-gate 	int		ret = 0;
13783448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	switch (protocol) {
13810Sstevel@tonic-gate 	case IPPROTO_TCP:
13820Sstevel@tonic-gate 		/* Just need to insert a conn struct */
13830Sstevel@tonic-gate 		if (!(connp->conn_flags & IPCL_EAGER)) {
13840Sstevel@tonic-gate 			IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
13850Sstevel@tonic-gate 		}
13863448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
13873448Sdh155122 		    IPCL_CONN_HASH_V6(connp->conn_remv6, connp->conn_ports,
13883448Sdh155122 		    ipst)];
13890Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
13900Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
13910Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
13920Sstevel@tonic-gate 			if (IPCL_CONN_MATCH_V6(tconnp, connp->conn_ulp,
13930Sstevel@tonic-gate 			    connp->conn_remv6, connp->conn_srcv6,
13940Sstevel@tonic-gate 			    connp->conn_ports) &&
13950Sstevel@tonic-gate 			    (tconnp->conn_tcp->tcp_bound_if == 0 ||
13960Sstevel@tonic-gate 			    tconnp->conn_tcp->tcp_bound_if == ifindex)) {
13970Sstevel@tonic-gate 				/* Already have a conn. bail out */
13980Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
13990Sstevel@tonic-gate 				return (EADDRINUSE);
14000Sstevel@tonic-gate 			}
14010Sstevel@tonic-gate 		}
14020Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
14030Sstevel@tonic-gate 			/*
14040Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
14050Sstevel@tonic-gate 			 * rebind. Let it happen.
14060Sstevel@tonic-gate 			 */
14070Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
14080Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
14090Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
14100Sstevel@tonic-gate 		}
14110Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
14120Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
14130Sstevel@tonic-gate 		break;
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	case IPPROTO_SCTP:
1416409Skcpoon 		IPCL_HASH_REMOVE(connp);
1417409Skcpoon 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
14180Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
14190Sstevel@tonic-gate 		break;
14200Sstevel@tonic-gate 
14211676Sjpk 	default:
14223448Sdh155122 		if (is_system_labeled() &&
14233448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
14241676Sjpk 			return (EADDRINUSE);
14251676Sjpk 		/* FALLTHROUGH */
14260Sstevel@tonic-gate 	case IPPROTO_UDP:
14270Sstevel@tonic-gate 		up = (uint16_t *)&ports;
14280Sstevel@tonic-gate 		IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
14290Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
14303448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
14313448Sdh155122 			    IPCL_UDP_HASH(up[1], ipst)];
14320Sstevel@tonic-gate 		} else {
14333448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
14340Sstevel@tonic-gate 		}
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
14370Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
14380Sstevel@tonic-gate 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
14390Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
14400Sstevel@tonic-gate 		} else {
14410Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
14420Sstevel@tonic-gate 		}
14430Sstevel@tonic-gate 		break;
14440Sstevel@tonic-gate 	}
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	return (ret);
14470Sstevel@tonic-gate }
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate /*
14500Sstevel@tonic-gate  * v4 packet classifying function. looks up the fanout table to
14510Sstevel@tonic-gate  * find the conn, the packet belongs to. returns the conn with
14520Sstevel@tonic-gate  * the reference held, null otherwise.
14531676Sjpk  *
14541676Sjpk  * If zoneid is ALL_ZONES, then the search rules described in the "Connection
14551676Sjpk  * Lookup" comment block are applied.  Labels are also checked as described
14561676Sjpk  * above.  If the packet is from the inside (looped back), and is from the same
14571676Sjpk  * zone, then label checks are omitted.
14580Sstevel@tonic-gate  */
14590Sstevel@tonic-gate conn_t *
14603448Sdh155122 ipcl_classify_v4(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid,
14613448Sdh155122     ip_stack_t *ipst)
14620Sstevel@tonic-gate {
14630Sstevel@tonic-gate 	ipha_t	*ipha;
14640Sstevel@tonic-gate 	connf_t	*connfp, *bind_connfp;
14650Sstevel@tonic-gate 	uint16_t lport;
14660Sstevel@tonic-gate 	uint16_t fport;
14670Sstevel@tonic-gate 	uint32_t ports;
14680Sstevel@tonic-gate 	conn_t	*connp;
14690Sstevel@tonic-gate 	uint16_t  *up;
14701676Sjpk 	boolean_t shared_addr;
14711676Sjpk 	boolean_t unlabeled;
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
14740Sstevel@tonic-gate 	up = (uint16_t *)((uchar_t *)ipha + hdr_len + TCP_PORTS_OFFSET);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	switch (protocol) {
14770Sstevel@tonic-gate 	case IPPROTO_TCP:
14780Sstevel@tonic-gate 		ports = *(uint32_t *)up;
14790Sstevel@tonic-gate 		connfp =
14803448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_src,
14813448Sdh155122 		    ports, ipst)];
14820Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
14830Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
14840Sstevel@tonic-gate 		    connp = connp->conn_next) {
14850Sstevel@tonic-gate 			if (IPCL_CONN_MATCH(connp, protocol,
14860Sstevel@tonic-gate 			    ipha->ipha_src, ipha->ipha_dst, ports))
14870Sstevel@tonic-gate 				break;
14880Sstevel@tonic-gate 		}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 		if (connp != NULL) {
14911676Sjpk 			/*
14921676Sjpk 			 * We have a fully-bound TCP connection.
14931676Sjpk 			 *
14941676Sjpk 			 * For labeled systems, there's no need to check the
14951676Sjpk 			 * label here.  It's known to be good as we checked
14961676Sjpk 			 * before allowing the connection to become bound.
14971676Sjpk 			 */
14980Sstevel@tonic-gate 			CONN_INC_REF(connp);
14990Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
15000Sstevel@tonic-gate 			return (connp);
15010Sstevel@tonic-gate 		}
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 		lport = up[1];
15061676Sjpk 		unlabeled = B_FALSE;
15071676Sjpk 		/* Cred cannot be null on IPv4 */
15081676Sjpk 		if (is_system_labeled())
15091676Sjpk 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
15101676Sjpk 			    TSLF_UNLABELED) != 0;
15111676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
15121676Sjpk 		if (shared_addr) {
15133448Sdh155122 			/*
15143448Sdh155122 			 * No need to handle exclusive-stack zones since
15153448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
15163448Sdh155122 			 */
15171676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
15181676Sjpk 			/*
15191676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
15201676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
15211676Sjpk 			 * search for the zone based on the packet label.
15221676Sjpk 			 *
15231676Sjpk 			 * If there is such a zone, we prefer to find a
15241676Sjpk 			 * connection in it.  Otherwise, we look for a
15251676Sjpk 			 * MAC-exempt connection in any zone whose label
15261676Sjpk 			 * dominates the default label on the packet.
15271676Sjpk 			 */
15281676Sjpk 			if (zoneid == ALL_ZONES)
15291676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
15301676Sjpk 			else
15311676Sjpk 				unlabeled = B_FALSE;
15321676Sjpk 		}
15331676Sjpk 
15343448Sdh155122 		bind_connfp =
15353448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
15360Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
15370Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
15380Sstevel@tonic-gate 		    connp = connp->conn_next) {
15391676Sjpk 			if (IPCL_BIND_MATCH(connp, protocol, ipha->ipha_dst,
15402263Ssommerfe 			    lport) && (IPCL_ZONE_MATCH(connp, zoneid) ||
15411676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
15420Sstevel@tonic-gate 				break;
15430Sstevel@tonic-gate 		}
15440Sstevel@tonic-gate 
15451676Sjpk 		/*
15461676Sjpk 		 * If the matching connection is SLP on a private address, then
15471676Sjpk 		 * the label on the packet must match the local zone's label.
15481676Sjpk 		 * Otherwise, it must be in the label range defined by tnrh.
15491676Sjpk 		 * This is ensured by tsol_receive_label.
15501676Sjpk 		 */
15511676Sjpk 		if (connp != NULL && is_system_labeled() &&
15521676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
15531676Sjpk 		    shared_addr, connp)) {
15541676Sjpk 				DTRACE_PROBE3(
15551676Sjpk 				    tx__ip__log__info__classify__tcp,
15561676Sjpk 				    char *,
15571676Sjpk 				    "connp(1) could not receive mp(2)",
15581676Sjpk 				    conn_t *, connp, mblk_t *, mp);
15591676Sjpk 			connp = NULL;
15601676Sjpk 		}
15611676Sjpk 
15620Sstevel@tonic-gate 		if (connp != NULL) {
15631676Sjpk 			/* Have a listener at least */
15640Sstevel@tonic-gate 			CONN_INC_REF(connp);
15650Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
15660Sstevel@tonic-gate 			return (connp);
15670Sstevel@tonic-gate 		}
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
15720Sstevel@tonic-gate 		    ("ipcl_classify: couldn't classify mp = %p\n",
15730Sstevel@tonic-gate 		    (void *)mp));
15740Sstevel@tonic-gate 		break;
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	case IPPROTO_UDP:
15770Sstevel@tonic-gate 		lport = up[1];
15781676Sjpk 		unlabeled = B_FALSE;
15791676Sjpk 		/* Cred cannot be null on IPv4 */
15801676Sjpk 		if (is_system_labeled())
15811676Sjpk 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
15821676Sjpk 			    TSLF_UNLABELED) != 0;
15831676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
15841676Sjpk 		if (shared_addr) {
15853448Sdh155122 			/*
15863448Sdh155122 			 * No need to handle exclusive-stack zones since
15873448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
15883448Sdh155122 			 */
15891676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
15901676Sjpk 			/*
15911676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
15921676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
15931676Sjpk 			 * search for the zone based on the packet label.
15941676Sjpk 			 *
15951676Sjpk 			 * If there is such a zone, we prefer to find a
15961676Sjpk 			 * connection in it.  Otherwise, we look for a
15971676Sjpk 			 * MAC-exempt connection in any zone whose label
15981676Sjpk 			 * dominates the default label on the packet.
15991676Sjpk 			 */
16001676Sjpk 			if (zoneid == ALL_ZONES)
16011676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
16021676Sjpk 			else
16031676Sjpk 				unlabeled = B_FALSE;
16041676Sjpk 		}
16050Sstevel@tonic-gate 		fport = up[0];
16060Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify %x %x", lport, fport));
16073448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
16080Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16090Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16100Sstevel@tonic-gate 		    connp = connp->conn_next) {
16110Sstevel@tonic-gate 			if (IPCL_UDP_MATCH(connp, lport, ipha->ipha_dst,
16120Sstevel@tonic-gate 			    fport, ipha->ipha_src) &&
16132263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
16141676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
16150Sstevel@tonic-gate 				break;
16160Sstevel@tonic-gate 		}
16170Sstevel@tonic-gate 
16181676Sjpk 		if (connp != NULL && is_system_labeled() &&
16191676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
16201676Sjpk 		    shared_addr, connp)) {
16211676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp,
16221676Sjpk 			    char *, "connp(1) could not receive mp(2)",
16231676Sjpk 			    conn_t *, connp, mblk_t *, mp);
16241676Sjpk 			connp = NULL;
16251676Sjpk 		}
16261676Sjpk 
16270Sstevel@tonic-gate 		if (connp != NULL) {
16280Sstevel@tonic-gate 			CONN_INC_REF(connp);
16290Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
16300Sstevel@tonic-gate 			return (connp);
16310Sstevel@tonic-gate 		}
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 		/*
16340Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
16350Sstevel@tonic-gate 		 */
16360Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
16370Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
16380Sstevel@tonic-gate 		    ("ipcl_classify: cant find udp conn_t for ports : %x %x",
16390Sstevel@tonic-gate 		    lport, fport));
16400Sstevel@tonic-gate 		break;
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	return (NULL);
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate conn_t *
16473448Sdh155122 ipcl_classify_v6(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid,
16483448Sdh155122     ip_stack_t *ipst)
16490Sstevel@tonic-gate {
16500Sstevel@tonic-gate 	ip6_t		*ip6h;
16510Sstevel@tonic-gate 	connf_t		*connfp, *bind_connfp;
16520Sstevel@tonic-gate 	uint16_t	lport;
16530Sstevel@tonic-gate 	uint16_t	fport;
16540Sstevel@tonic-gate 	tcph_t		*tcph;
16550Sstevel@tonic-gate 	uint32_t	ports;
16560Sstevel@tonic-gate 	conn_t		*connp;
16570Sstevel@tonic-gate 	uint16_t	*up;
16581676Sjpk 	boolean_t	shared_addr;
16591676Sjpk 	boolean_t	unlabeled;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	switch (protocol) {
16640Sstevel@tonic-gate 	case IPPROTO_TCP:
16650Sstevel@tonic-gate 		tcph = (tcph_t *)&mp->b_rptr[hdr_len];
16660Sstevel@tonic-gate 		up = (uint16_t *)tcph->th_lport;
16670Sstevel@tonic-gate 		ports = *(uint32_t *)up;
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 		connfp =
16703448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_src,
16713448Sdh155122 		    ports, ipst)];
16720Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16730Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16740Sstevel@tonic-gate 		    connp = connp->conn_next) {
16750Sstevel@tonic-gate 			if (IPCL_CONN_MATCH_V6(connp, protocol,
16760Sstevel@tonic-gate 			    ip6h->ip6_src, ip6h->ip6_dst, ports))
16770Sstevel@tonic-gate 				break;
16780Sstevel@tonic-gate 		}
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 		if (connp != NULL) {
16811676Sjpk 			/*
16821676Sjpk 			 * We have a fully-bound TCP connection.
16831676Sjpk 			 *
16841676Sjpk 			 * For labeled systems, there's no need to check the
16851676Sjpk 			 * label here.  It's known to be good as we checked
16861676Sjpk 			 * before allowing the connection to become bound.
16871676Sjpk 			 */
16880Sstevel@tonic-gate 			CONN_INC_REF(connp);
16890Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
16900Sstevel@tonic-gate 			return (connp);
16910Sstevel@tonic-gate 		}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 		lport = up[1];
16961676Sjpk 		unlabeled = B_FALSE;
16971676Sjpk 		/* Cred can be null on IPv6 */
16981676Sjpk 		if (is_system_labeled()) {
16991676Sjpk 			cred_t *cr = DB_CRED(mp);
17001676Sjpk 
17011676Sjpk 			unlabeled = (cr != NULL &&
17021676Sjpk 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
17031676Sjpk 		}
17041676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
17051676Sjpk 		if (shared_addr) {
17063448Sdh155122 			/*
17073448Sdh155122 			 * No need to handle exclusive-stack zones since
17083448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
17093448Sdh155122 			 */
17101676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
17111676Sjpk 			/*
17121676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
17131676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
17141676Sjpk 			 * search for the zone based on the packet label.
17151676Sjpk 			 *
17161676Sjpk 			 * If there is such a zone, we prefer to find a
17171676Sjpk 			 * connection in it.  Otherwise, we look for a
17181676Sjpk 			 * MAC-exempt connection in any zone whose label
17191676Sjpk 			 * dominates the default label on the packet.
17201676Sjpk 			 */
17211676Sjpk 			if (zoneid == ALL_ZONES)
17221676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
17231676Sjpk 			else
17241676Sjpk 				unlabeled = B_FALSE;
17251676Sjpk 		}
17261676Sjpk 
17273448Sdh155122 		bind_connfp =
17283448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
17290Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
17300Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
17310Sstevel@tonic-gate 		    connp = connp->conn_next) {
17320Sstevel@tonic-gate 			if (IPCL_BIND_MATCH_V6(connp, protocol,
17330Sstevel@tonic-gate 			    ip6h->ip6_dst, lport) &&
17342263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
17351676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
17360Sstevel@tonic-gate 				break;
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 
17391676Sjpk 		if (connp != NULL && is_system_labeled() &&
17401676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
17411676Sjpk 		    shared_addr, connp)) {
17421676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp6,
17431676Sjpk 			    char *, "connp(1) could not receive mp(2)",
17441676Sjpk 			    conn_t *, connp, mblk_t *, mp);
17451676Sjpk 			connp = NULL;
17461676Sjpk 		}
17471676Sjpk 
17480Sstevel@tonic-gate 		if (connp != NULL) {
17490Sstevel@tonic-gate 			/* Have a listner at least */
17500Sstevel@tonic-gate 			CONN_INC_REF(connp);
17510Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
17520Sstevel@tonic-gate 			IPCL_DEBUG_LVL(512,
17530Sstevel@tonic-gate 			    ("ipcl_classify_v6: found listner "
17540Sstevel@tonic-gate 			    "connp = %p\n", (void *)connp));
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 			return (connp);
17570Sstevel@tonic-gate 		}
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
17620Sstevel@tonic-gate 		    ("ipcl_classify_v6: couldn't classify mp = %p\n",
17630Sstevel@tonic-gate 		    (void *)mp));
17640Sstevel@tonic-gate 		break;
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	case IPPROTO_UDP:
17670Sstevel@tonic-gate 		up = (uint16_t *)&mp->b_rptr[hdr_len];
17680Sstevel@tonic-gate 		lport = up[1];
17691676Sjpk 		unlabeled = B_FALSE;
17701676Sjpk 		/* Cred can be null on IPv6 */
17711676Sjpk 		if (is_system_labeled()) {
17721676Sjpk 			cred_t *cr = DB_CRED(mp);
17731676Sjpk 
17741676Sjpk 			unlabeled = (cr != NULL &&
17751676Sjpk 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
17761676Sjpk 		}
17771676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
17781676Sjpk 		if (shared_addr) {
17793448Sdh155122 			/*
17803448Sdh155122 			 * No need to handle exclusive-stack zones since
17813448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
17823448Sdh155122 			 */
17831676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
17841676Sjpk 			/*
17851676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
17861676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
17871676Sjpk 			 * search for the zone based on the packet label.
17881676Sjpk 			 *
17891676Sjpk 			 * If there is such a zone, we prefer to find a
17901676Sjpk 			 * connection in it.  Otherwise, we look for a
17911676Sjpk 			 * MAC-exempt connection in any zone whose label
17921676Sjpk 			 * dominates the default label on the packet.
17931676Sjpk 			 */
17941676Sjpk 			if (zoneid == ALL_ZONES)
17951676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
17961676Sjpk 			else
17971676Sjpk 				unlabeled = B_FALSE;
17981676Sjpk 		}
17991676Sjpk 
18000Sstevel@tonic-gate 		fport = up[0];
18010Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify_v6 %x %x", lport,
18020Sstevel@tonic-gate 		    fport));
18033448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
18040Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
18050Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
18060Sstevel@tonic-gate 		    connp = connp->conn_next) {
18070Sstevel@tonic-gate 			if (IPCL_UDP_MATCH_V6(connp, lport, ip6h->ip6_dst,
18080Sstevel@tonic-gate 			    fport, ip6h->ip6_src) &&
18092263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
18101676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
18110Sstevel@tonic-gate 				break;
18120Sstevel@tonic-gate 		}
18130Sstevel@tonic-gate 
18141676Sjpk 		if (connp != NULL && is_system_labeled() &&
18151676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
18161676Sjpk 		    shared_addr, connp)) {
18171676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp6,
18181676Sjpk 			    char *, "connp(1) could not receive mp(2)",
18191676Sjpk 			    conn_t *, connp, mblk_t *, mp);
18201676Sjpk 			connp = NULL;
18211676Sjpk 		}
18221676Sjpk 
18230Sstevel@tonic-gate 		if (connp != NULL) {
18240Sstevel@tonic-gate 			CONN_INC_REF(connp);
18250Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
18260Sstevel@tonic-gate 			return (connp);
18270Sstevel@tonic-gate 		}
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 		/*
18300Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
18310Sstevel@tonic-gate 		 */
18320Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
18330Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
18340Sstevel@tonic-gate 		    ("ipcl_classify_v6: cant find udp conn_t for ports : %x %x",
18350Sstevel@tonic-gate 		    lport, fport));
18360Sstevel@tonic-gate 		break;
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	return (NULL);
18400Sstevel@tonic-gate }
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate /*
18430Sstevel@tonic-gate  * wrapper around ipcl_classify_(v4,v6) routines.
18440Sstevel@tonic-gate  */
18450Sstevel@tonic-gate conn_t *
18463448Sdh155122 ipcl_classify(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst)
18470Sstevel@tonic-gate {
18480Sstevel@tonic-gate 	uint16_t	hdr_len;
18490Sstevel@tonic-gate 	ipha_t		*ipha;
18500Sstevel@tonic-gate 	uint8_t		*nexthdrp;
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (ipha_t))
18530Sstevel@tonic-gate 		return (NULL);
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	switch (IPH_HDR_VERSION(mp->b_rptr)) {
18560Sstevel@tonic-gate 	case IPV4_VERSION:
18570Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
18580Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
18590Sstevel@tonic-gate 		return (ipcl_classify_v4(mp, ipha->ipha_protocol, hdr_len,
18603448Sdh155122 		    zoneid, ipst));
18610Sstevel@tonic-gate 	case IPV6_VERSION:
18620Sstevel@tonic-gate 		if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr,
18630Sstevel@tonic-gate 		    &hdr_len, &nexthdrp))
18640Sstevel@tonic-gate 			return (NULL);
18650Sstevel@tonic-gate 
18663448Sdh155122 		return (ipcl_classify_v6(mp, *nexthdrp, hdr_len, zoneid, ipst));
18670Sstevel@tonic-gate 	}
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	return (NULL);
18700Sstevel@tonic-gate }
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate conn_t *
18731676Sjpk ipcl_classify_raw(mblk_t *mp, uint8_t protocol, zoneid_t zoneid,
18743448Sdh155122     uint32_t ports, ipha_t *hdr, ip_stack_t *ipst)
18750Sstevel@tonic-gate {
18761676Sjpk 	connf_t		*connfp;
18770Sstevel@tonic-gate 	conn_t		*connp;
18780Sstevel@tonic-gate 	in_port_t	lport;
18790Sstevel@tonic-gate 	int		af;
18801676Sjpk 	boolean_t	shared_addr;
18811676Sjpk 	boolean_t	unlabeled;
18821676Sjpk 	const void	*dst;
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 	lport = ((uint16_t *)&ports)[1];
18851676Sjpk 
18861676Sjpk 	unlabeled = B_FALSE;
18871676Sjpk 	/* Cred can be null on IPv6 */
18881676Sjpk 	if (is_system_labeled()) {
18891676Sjpk 		cred_t *cr = DB_CRED(mp);
18901676Sjpk 
18911676Sjpk 		unlabeled = (cr != NULL &&
18921676Sjpk 		    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
18931676Sjpk 	}
18941676Sjpk 	shared_addr = (zoneid == ALL_ZONES);
18951676Sjpk 	if (shared_addr) {
18963448Sdh155122 		/*
18973448Sdh155122 		 * No need to handle exclusive-stack zones since ALL_ZONES
18983448Sdh155122 		 * only applies to the shared stack.
18993448Sdh155122 		 */
19001676Sjpk 		zoneid = tsol_mlp_findzone(protocol, lport);
19011676Sjpk 		/*
19021676Sjpk 		 * If no shared MLP is found, tsol_mlp_findzone returns
19031676Sjpk 		 * ALL_ZONES.  In that case, we assume it's SLP, and search for
19041676Sjpk 		 * the zone based on the packet label.
19051676Sjpk 		 *
19061676Sjpk 		 * If there is such a zone, we prefer to find a connection in
19071676Sjpk 		 * it.  Otherwise, we look for a MAC-exempt connection in any
19081676Sjpk 		 * zone whose label dominates the default label on the packet.
19091676Sjpk 		 */
19101676Sjpk 		if (zoneid == ALL_ZONES)
19111676Sjpk 			zoneid = tsol_packet_to_zoneid(mp);
19121676Sjpk 		else
19131676Sjpk 			unlabeled = B_FALSE;
19141676Sjpk 	}
19151676Sjpk 
19160Sstevel@tonic-gate 	af = IPH_HDR_VERSION(hdr);
19171676Sjpk 	dst = af == IPV4_VERSION ? (const void *)&hdr->ipha_dst :
19181676Sjpk 	    (const void *)&((ip6_t *)hdr)->ip6_dst;
19193448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
19220Sstevel@tonic-gate 	for (connp = connfp->connf_head; connp != NULL;
19230Sstevel@tonic-gate 	    connp = connp->conn_next) {
19240Sstevel@tonic-gate 		/* We don't allow v4 fallback for v6 raw socket. */
19251676Sjpk 		if (af == (connp->conn_af_isv6 ? IPV4_VERSION :
19261676Sjpk 		    IPV6_VERSION))
19270Sstevel@tonic-gate 			continue;
19280Sstevel@tonic-gate 		if (connp->conn_fully_bound) {
19290Sstevel@tonic-gate 			if (af == IPV4_VERSION) {
19301676Sjpk 				if (!IPCL_CONN_MATCH(connp, protocol,
19311676Sjpk 				    hdr->ipha_src, hdr->ipha_dst, ports))
19321676Sjpk 					continue;
19330Sstevel@tonic-gate 			} else {
19341676Sjpk 				if (!IPCL_CONN_MATCH_V6(connp, protocol,
19350Sstevel@tonic-gate 				    ((ip6_t *)hdr)->ip6_src,
19361676Sjpk 				    ((ip6_t *)hdr)->ip6_dst, ports))
19371676Sjpk 					continue;
19380Sstevel@tonic-gate 			}
19390Sstevel@tonic-gate 		} else {
19400Sstevel@tonic-gate 			if (af == IPV4_VERSION) {
19411676Sjpk 				if (!IPCL_BIND_MATCH(connp, protocol,
19421676Sjpk 				    hdr->ipha_dst, lport))
19431676Sjpk 					continue;
19440Sstevel@tonic-gate 			} else {
19451676Sjpk 				if (!IPCL_BIND_MATCH_V6(connp, protocol,
19461676Sjpk 				    ((ip6_t *)hdr)->ip6_dst, lport))
19471676Sjpk 					continue;
19480Sstevel@tonic-gate 			}
19490Sstevel@tonic-gate 		}
19501676Sjpk 
19512263Ssommerfe 		if (IPCL_ZONE_MATCH(connp, zoneid) ||
19521676Sjpk 		    (unlabeled && connp->conn_mac_exempt))
19531676Sjpk 			break;
19541676Sjpk 	}
19551676Sjpk 	/*
19561676Sjpk 	 * If the connection is fully-bound and connection-oriented (TCP or
19571676Sjpk 	 * SCTP), then we've already validated the remote system's label.
19581676Sjpk 	 * There's no need to do it again for every packet.
19591676Sjpk 	 */
19601676Sjpk 	if (connp != NULL && is_system_labeled() && (!connp->conn_fully_bound ||
19611676Sjpk 	    !(connp->conn_flags & (IPCL_TCP|IPCL_SCTPCONN))) &&
19621676Sjpk 	    !tsol_receive_local(mp, dst, af, shared_addr, connp)) {
19631676Sjpk 		DTRACE_PROBE3(tx__ip__log__info__classify__rawip,
19641676Sjpk 		    char *, "connp(1) could not receive mp(2)",
19651676Sjpk 		    conn_t *, connp, mblk_t *, mp);
19661676Sjpk 		connp = NULL;
19670Sstevel@tonic-gate 	}
1968409Skcpoon 
1969409Skcpoon 	if (connp != NULL)
1970409Skcpoon 		goto found;
1971409Skcpoon 	mutex_exit(&connfp->connf_lock);
1972409Skcpoon 
1973409Skcpoon 	/* Try to look for a wildcard match. */
19743448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(0, ipst)];
1975409Skcpoon 	mutex_enter(&connfp->connf_lock);
1976409Skcpoon 	for (connp = connfp->connf_head; connp != NULL;
1977409Skcpoon 	    connp = connp->conn_next) {
1978409Skcpoon 		/* We don't allow v4 fallback for v6 raw socket. */
1979409Skcpoon 		if ((af == (connp->conn_af_isv6 ? IPV4_VERSION :
19802263Ssommerfe 		    IPV6_VERSION)) || !IPCL_ZONE_MATCH(connp, zoneid)) {
1981409Skcpoon 			continue;
1982409Skcpoon 		}
1983409Skcpoon 		if (af == IPV4_VERSION) {
1984409Skcpoon 			if (IPCL_RAW_MATCH(connp, protocol, hdr->ipha_dst))
1985409Skcpoon 				break;
1986409Skcpoon 		} else {
1987409Skcpoon 			if (IPCL_RAW_MATCH_V6(connp, protocol,
1988409Skcpoon 			    ((ip6_t *)hdr)->ip6_dst)) {
1989409Skcpoon 				break;
1990409Skcpoon 			}
1991409Skcpoon 		}
19920Sstevel@tonic-gate 	}
1993409Skcpoon 
1994409Skcpoon 	if (connp != NULL)
1995409Skcpoon 		goto found;
1996409Skcpoon 
19970Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
19980Sstevel@tonic-gate 	return (NULL);
1999409Skcpoon 
2000409Skcpoon found:
2001409Skcpoon 	ASSERT(connp != NULL);
2002409Skcpoon 	CONN_INC_REF(connp);
2003409Skcpoon 	mutex_exit(&connfp->connf_lock);
2004409Skcpoon 	return (connp);
20050Sstevel@tonic-gate }
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate /* ARGSUSED */
20080Sstevel@tonic-gate static int
20095240Snordmark tcp_conn_constructor(void *buf, void *cdrarg, int kmflags)
20100Sstevel@tonic-gate {
20110Sstevel@tonic-gate 	itc_t	*itc = (itc_t *)buf;
20120Sstevel@tonic-gate 	conn_t 	*connp = &itc->itc_conn;
20135240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
20145240Snordmark 
20155240Snordmark 	bzero(connp, sizeof (conn_t));
20165240Snordmark 	bzero(tcp, sizeof (tcp_t));
20175240Snordmark 
20185240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20195240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20200Sstevel@tonic-gate 	tcp->tcp_timercache = tcp_timermp_alloc(KM_NOSLEEP);
20210Sstevel@tonic-gate 	connp->conn_tcp = tcp;
20220Sstevel@tonic-gate 	connp->conn_flags = IPCL_TCPCONN;
20230Sstevel@tonic-gate 	connp->conn_ulp = IPPROTO_TCP;
20240Sstevel@tonic-gate 	tcp->tcp_connp = connp;
20250Sstevel@tonic-gate 	return (0);
20260Sstevel@tonic-gate }
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate /* ARGSUSED */
20290Sstevel@tonic-gate static void
20305240Snordmark tcp_conn_destructor(void *buf, void *cdrarg)
20315240Snordmark {
20325240Snordmark 	itc_t	*itc = (itc_t *)buf;
20335240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20345240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
20355240Snordmark 
20365240Snordmark 	ASSERT(connp->conn_flags & IPCL_TCPCONN);
20375240Snordmark 	ASSERT(tcp->tcp_connp == connp);
20385240Snordmark 	ASSERT(connp->conn_tcp == tcp);
20395240Snordmark 	tcp_timermp_free(tcp);
20405240Snordmark 	mutex_destroy(&connp->conn_lock);
20415240Snordmark 	cv_destroy(&connp->conn_cv);
20425240Snordmark }
20435240Snordmark 
20445240Snordmark /* ARGSUSED */
20455240Snordmark static int
20465240Snordmark ip_conn_constructor(void *buf, void *cdrarg, int kmflags)
20475240Snordmark {
20485240Snordmark 	itc_t	*itc = (itc_t *)buf;
20495240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20505240Snordmark 
20515240Snordmark 	bzero(connp, sizeof (conn_t));
20525240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20535240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20545240Snordmark 	connp->conn_flags = IPCL_IPCCONN;
20555240Snordmark 
20565240Snordmark 	return (0);
20575240Snordmark }
20585240Snordmark 
20595240Snordmark /* ARGSUSED */
20605240Snordmark static void
20615240Snordmark ip_conn_destructor(void *buf, void *cdrarg)
20625240Snordmark {
20635240Snordmark 	itc_t	*itc = (itc_t *)buf;
20645240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20655240Snordmark 
20665240Snordmark 	ASSERT(connp->conn_flags & IPCL_IPCCONN);
20675240Snordmark 	ASSERT(connp->conn_priv == NULL);
20685240Snordmark 	mutex_destroy(&connp->conn_lock);
20695240Snordmark 	cv_destroy(&connp->conn_cv);
20705240Snordmark }
20715240Snordmark 
20725240Snordmark /* ARGSUSED */
20735240Snordmark static int
20745240Snordmark udp_conn_constructor(void *buf, void *cdrarg, int kmflags)
20755240Snordmark {
20765240Snordmark 	itc_t	*itc = (itc_t *)buf;
20775240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20785240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
20795240Snordmark 
20805240Snordmark 	bzero(connp, sizeof (conn_t));
20815240Snordmark 	bzero(udp, sizeof (udp_t));
20825240Snordmark 
20835240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20845240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20855240Snordmark 	connp->conn_udp = udp;
20865240Snordmark 	connp->conn_flags = IPCL_UDPCONN;
20875240Snordmark 	connp->conn_ulp = IPPROTO_UDP;
20885240Snordmark 	udp->udp_connp = connp;
20895240Snordmark 	return (0);
20905240Snordmark }
20915240Snordmark 
20925240Snordmark /* ARGSUSED */
20935240Snordmark static void
20945240Snordmark udp_conn_destructor(void *buf, void *cdrarg)
20955240Snordmark {
20965240Snordmark 	itc_t	*itc = (itc_t *)buf;
20975240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20985240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
20995240Snordmark 
21005240Snordmark 	ASSERT(connp->conn_flags & IPCL_UDPCONN);
21015240Snordmark 	ASSERT(udp->udp_connp == connp);
21025240Snordmark 	ASSERT(connp->conn_udp == udp);
21035240Snordmark 	mutex_destroy(&connp->conn_lock);
21045240Snordmark 	cv_destroy(&connp->conn_cv);
21055240Snordmark }
21065240Snordmark 
21075240Snordmark /* ARGSUSED */
21085240Snordmark static int
21095240Snordmark rawip_conn_constructor(void *buf, void *cdrarg, int kmflags)
21100Sstevel@tonic-gate {
21115240Snordmark 	itc_t	*itc = (itc_t *)buf;
21125240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21135240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
21145240Snordmark 
21155240Snordmark 	bzero(connp, sizeof (conn_t));
21165240Snordmark 	bzero(icmp, sizeof (icmp_t));
21175240Snordmark 
21185240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21195240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21205240Snordmark 	connp->conn_icmp = icmp;
21215240Snordmark 	connp->conn_flags = IPCL_RAWIPCONN;
21225240Snordmark 	connp->conn_ulp = IPPROTO_ICMP;
21235240Snordmark 	icmp->icmp_connp = connp;
21245240Snordmark 	return (0);
21255240Snordmark }
21265240Snordmark 
21275240Snordmark /* ARGSUSED */
21285240Snordmark static void
21295240Snordmark rawip_conn_destructor(void *buf, void *cdrarg)
21305240Snordmark {
21315240Snordmark 	itc_t	*itc = (itc_t *)buf;
21325240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21335240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
21345240Snordmark 
21355240Snordmark 	ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
21365240Snordmark 	ASSERT(icmp->icmp_connp == connp);
21375240Snordmark 	ASSERT(connp->conn_icmp == icmp);
21385240Snordmark 	mutex_destroy(&connp->conn_lock);
21395240Snordmark 	cv_destroy(&connp->conn_cv);
21405240Snordmark }
21415240Snordmark 
21425240Snordmark /* ARGSUSED */
21435240Snordmark static int
21445240Snordmark rts_conn_constructor(void *buf, void *cdrarg, int kmflags)
21455240Snordmark {
21465240Snordmark 	itc_t	*itc = (itc_t *)buf;
21475240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21485240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21495240Snordmark 
21505240Snordmark 	bzero(connp, sizeof (conn_t));
21515240Snordmark 	bzero(rts, sizeof (rts_t));
21525240Snordmark 
21535240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21545240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21555240Snordmark 	connp->conn_rts = rts;
21565240Snordmark 	connp->conn_flags = IPCL_RTSCONN;
21575240Snordmark 	rts->rts_connp = connp;
21585240Snordmark 	return (0);
21595240Snordmark }
21605240Snordmark 
21615240Snordmark /* ARGSUSED */
21625240Snordmark static void
21635240Snordmark rts_conn_destructor(void *buf, void *cdrarg)
21645240Snordmark {
21655240Snordmark 	itc_t	*itc = (itc_t *)buf;
21665240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21675240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21685240Snordmark 
21695240Snordmark 	ASSERT(connp->conn_flags & IPCL_RTSCONN);
21705240Snordmark 	ASSERT(rts->rts_connp == connp);
21715240Snordmark 	ASSERT(connp->conn_rts == rts);
21725240Snordmark 	mutex_destroy(&connp->conn_lock);
21735240Snordmark 	cv_destroy(&connp->conn_cv);
21745240Snordmark }
21755240Snordmark 
21765240Snordmark /*
21775240Snordmark  * Called as part of ipcl_conn_destroy to assert and clear any pointers
21785240Snordmark  * in the conn_t.
2179*5277Snordmark  *
2180*5277Snordmark  * Below we list all the pointers in the conn_t as a documentation aid.
2181*5277Snordmark  * The ones that we can not ASSERT to be NULL are #ifdef'ed out.
2182*5277Snordmark  * If you add any pointers to the conn_t please add an ASSERT here
2183*5277Snordmark  * and #ifdef it out if it can't be actually asserted to be NULL.
2184*5277Snordmark  * In any case, we bzero most of the conn_t at the end of the function.
21855240Snordmark  */
21865240Snordmark void
21875240Snordmark ipcl_conn_cleanup(conn_t *connp)
21885240Snordmark {
21895240Snordmark 	ASSERT(connp->conn_ire_cache == NULL);
21905240Snordmark 	ASSERT(connp->conn_latch == NULL);
21915240Snordmark #ifdef notdef
2192*5277Snordmark 	/* These are not cleared */
21935240Snordmark 	ASSERT(connp->conn_rq == NULL);
21945240Snordmark 	ASSERT(connp->conn_wq == NULL);
21955240Snordmark #endif
21965240Snordmark 	ASSERT(connp->conn_cred == NULL);
21975240Snordmark 	ASSERT(connp->conn_g_fanout == NULL);
21985240Snordmark 	ASSERT(connp->conn_g_next == NULL);
21995240Snordmark 	ASSERT(connp->conn_g_prev == NULL);
22005240Snordmark 	ASSERT(connp->conn_policy == NULL);
22015240Snordmark 	ASSERT(connp->conn_fanout == NULL);
22025240Snordmark 	ASSERT(connp->conn_next == NULL);
22035240Snordmark 	ASSERT(connp->conn_prev == NULL);
22045240Snordmark #ifdef notdef
22055240Snordmark 	/*
22065240Snordmark 	 * The ill and ipif pointers are not cleared before the conn_t
22075240Snordmark 	 * goes away since they do not hold a reference on the ill/ipif.
22085240Snordmark 	 * We should replace these pointers with ifindex/ipaddr_t to
22095240Snordmark 	 * make the code less complex.
22105240Snordmark 	 */
22115240Snordmark 	ASSERT(connp->conn_xmit_if_ill == NULL);
22125240Snordmark 	ASSERT(connp->conn_nofailover_ill == NULL);
22135240Snordmark 	ASSERT(connp->conn_outgoing_ill == NULL);
22145240Snordmark 	ASSERT(connp->conn_incoming_ill == NULL);
22155240Snordmark 	ASSERT(connp->conn_outgoing_pill == NULL);
22165240Snordmark 	ASSERT(connp->conn_multicast_ipif == NULL);
22175240Snordmark 	ASSERT(connp->conn_multicast_ill == NULL);
22185240Snordmark #endif
22195240Snordmark 	ASSERT(connp->conn_oper_pending_ill == NULL);
22205240Snordmark 	ASSERT(connp->conn_ilg == NULL);
22215240Snordmark 	ASSERT(connp->conn_drain_next == NULL);
22225240Snordmark 	ASSERT(connp->conn_drain_prev == NULL);
2223*5277Snordmark #ifdef notdef
2224*5277Snordmark 	/* conn_idl is not cleared when removed from idl list */
22255240Snordmark 	ASSERT(connp->conn_idl == NULL);
2226*5277Snordmark #endif
22275240Snordmark 	ASSERT(connp->conn_ipsec_opt_mp == NULL);
22285240Snordmark 	ASSERT(connp->conn_peercred == NULL);
22295240Snordmark 	ASSERT(connp->conn_netstack == NULL);
22305240Snordmark 
22315240Snordmark 	/* Clear out the conn_t fields that are not preserved */
22325240Snordmark 	bzero(&connp->conn_start_clr,
22335240Snordmark 	    sizeof (conn_t) -
22345240Snordmark 	    ((uchar_t *)&connp->conn_start_clr - (uchar_t *)connp));
22355240Snordmark 
22360Sstevel@tonic-gate }
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate /*
22390Sstevel@tonic-gate  * All conns are inserted in a global multi-list for the benefit of
22400Sstevel@tonic-gate  * walkers. The walk is guaranteed to walk all open conns at the time
22410Sstevel@tonic-gate  * of the start of the walk exactly once. This property is needed to
22420Sstevel@tonic-gate  * achieve some cleanups during unplumb of interfaces. This is achieved
22430Sstevel@tonic-gate  * as follows.
22440Sstevel@tonic-gate  *
22450Sstevel@tonic-gate  * ipcl_conn_create and ipcl_conn_destroy are the only functions that
22460Sstevel@tonic-gate  * call the insert and delete functions below at creation and deletion
22470Sstevel@tonic-gate  * time respectively. The conn never moves or changes its position in this
22480Sstevel@tonic-gate  * multi-list during its lifetime. CONN_CONDEMNED ensures that the refcnt
22490Sstevel@tonic-gate  * won't increase due to walkers, once the conn deletion has started. Note
22500Sstevel@tonic-gate  * that we can't remove the conn from the global list and then wait for
22510Sstevel@tonic-gate  * the refcnt to drop to zero, since walkers would then see a truncated
22520Sstevel@tonic-gate  * list. CONN_INCIPIENT ensures that walkers don't start looking at
22530Sstevel@tonic-gate  * conns until ip_open is ready to make them globally visible.
22540Sstevel@tonic-gate  * The global round robin multi-list locks are held only to get the
22550Sstevel@tonic-gate  * next member/insertion/deletion and contention should be negligible
22560Sstevel@tonic-gate  * if the multi-list is much greater than the number of cpus.
22570Sstevel@tonic-gate  */
22580Sstevel@tonic-gate void
22590Sstevel@tonic-gate ipcl_globalhash_insert(conn_t *connp)
22600Sstevel@tonic-gate {
22610Sstevel@tonic-gate 	int	index;
22623448Sdh155122 	struct connf_s	*connfp;
22633448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	/*
22660Sstevel@tonic-gate 	 * No need for atomic here. Approximate even distribution
22670Sstevel@tonic-gate 	 * in the global lists is sufficient.
22680Sstevel@tonic-gate 	 */
22693448Sdh155122 	ipst->ips_conn_g_index++;
22703448Sdh155122 	index = ipst->ips_conn_g_index & (CONN_G_HASH_SIZE - 1);
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
22730Sstevel@tonic-gate 	/*
22740Sstevel@tonic-gate 	 * Mark as INCIPIENT, so that walkers will ignore this
22750Sstevel@tonic-gate 	 * for now, till ip_open is ready to make it visible globally.
22760Sstevel@tonic-gate 	 */
22770Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_INCIPIENT;
22780Sstevel@tonic-gate 
22793448Sdh155122 	connfp = &ipst->ips_ipcl_globalhash_fanout[index];
22800Sstevel@tonic-gate 	/* Insert at the head of the list */
22813448Sdh155122 	mutex_enter(&connfp->connf_lock);
22823448Sdh155122 	connp->conn_g_next = connfp->connf_head;
22830Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
22840Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp;
22853448Sdh155122 	connfp->connf_head = connp;
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	/* The fanout bucket this conn points to */
22883448Sdh155122 	connp->conn_g_fanout = connfp;
22890Sstevel@tonic-gate 
22903448Sdh155122 	mutex_exit(&connfp->connf_lock);
22910Sstevel@tonic-gate }
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate void
22940Sstevel@tonic-gate ipcl_globalhash_remove(conn_t *connp)
22950Sstevel@tonic-gate {
22963448Sdh155122 	struct connf_s	*connfp;
22973448Sdh155122 
22980Sstevel@tonic-gate 	/*
22990Sstevel@tonic-gate 	 * We were never inserted in the global multi list.
23000Sstevel@tonic-gate 	 * IPCL_NONE variety is never inserted in the global multilist
23010Sstevel@tonic-gate 	 * since it is presumed to not need any cleanup and is transient.
23020Sstevel@tonic-gate 	 */
23030Sstevel@tonic-gate 	if (connp->conn_g_fanout == NULL)
23040Sstevel@tonic-gate 		return;
23050Sstevel@tonic-gate 
23063448Sdh155122 	connfp = connp->conn_g_fanout;
23073448Sdh155122 	mutex_enter(&connfp->connf_lock);
23080Sstevel@tonic-gate 	if (connp->conn_g_prev != NULL)
23090Sstevel@tonic-gate 		connp->conn_g_prev->conn_g_next = connp->conn_g_next;
23100Sstevel@tonic-gate 	else
23113448Sdh155122 		connfp->connf_head = connp->conn_g_next;
23120Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
23130Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp->conn_g_prev;
23143448Sdh155122 	mutex_exit(&connfp->connf_lock);
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	/* Better to stumble on a null pointer than to corrupt memory */
23170Sstevel@tonic-gate 	connp->conn_g_next = NULL;
23180Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
23195240Snordmark 	connp->conn_g_fanout = NULL;
23200Sstevel@tonic-gate }
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate /*
23230Sstevel@tonic-gate  * Walk the list of all conn_t's in the system, calling the function provided
23240Sstevel@tonic-gate  * with the specified argument for each.
23250Sstevel@tonic-gate  * Applies to both IPv4 and IPv6.
23260Sstevel@tonic-gate  *
23270Sstevel@tonic-gate  * IPCs may hold pointers to ipif/ill. To guard against stale pointers
23280Sstevel@tonic-gate  * ipcl_walk() is called to cleanup the conn_t's, typically when an interface is
23290Sstevel@tonic-gate  * unplumbed or removed. New conn_t's that are created while we are walking
23300Sstevel@tonic-gate  * may be missed by this walk, because they are not necessarily inserted
23310Sstevel@tonic-gate  * at the tail of the list. They are new conn_t's and thus don't have any
23320Sstevel@tonic-gate  * stale pointers. The CONN_CLOSING flag ensures that no new reference
23330Sstevel@tonic-gate  * is created to the struct that is going away.
23340Sstevel@tonic-gate  */
23350Sstevel@tonic-gate void
23363448Sdh155122 ipcl_walk(pfv_t func, void *arg, ip_stack_t *ipst)
23370Sstevel@tonic-gate {
23380Sstevel@tonic-gate 	int	i;
23390Sstevel@tonic-gate 	conn_t	*connp;
23400Sstevel@tonic-gate 	conn_t	*prev_connp;
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
23433448Sdh155122 		mutex_enter(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23440Sstevel@tonic-gate 		prev_connp = NULL;
23453448Sdh155122 		connp = ipst->ips_ipcl_globalhash_fanout[i].connf_head;
23460Sstevel@tonic-gate 		while (connp != NULL) {
23470Sstevel@tonic-gate 			mutex_enter(&connp->conn_lock);
23480Sstevel@tonic-gate 			if (connp->conn_state_flags &
23490Sstevel@tonic-gate 			    (CONN_CONDEMNED | CONN_INCIPIENT)) {
23500Sstevel@tonic-gate 				mutex_exit(&connp->conn_lock);
23510Sstevel@tonic-gate 				connp = connp->conn_g_next;
23520Sstevel@tonic-gate 				continue;
23530Sstevel@tonic-gate 			}
23540Sstevel@tonic-gate 			CONN_INC_REF_LOCKED(connp);
23550Sstevel@tonic-gate 			mutex_exit(&connp->conn_lock);
23563448Sdh155122 			mutex_exit(
23573448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23580Sstevel@tonic-gate 			(*func)(connp, arg);
23590Sstevel@tonic-gate 			if (prev_connp != NULL)
23600Sstevel@tonic-gate 				CONN_DEC_REF(prev_connp);
23613448Sdh155122 			mutex_enter(
23623448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23630Sstevel@tonic-gate 			prev_connp = connp;
23640Sstevel@tonic-gate 			connp = connp->conn_g_next;
23650Sstevel@tonic-gate 		}
23663448Sdh155122 		mutex_exit(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23670Sstevel@tonic-gate 		if (prev_connp != NULL)
23680Sstevel@tonic-gate 			CONN_DEC_REF(prev_connp);
23690Sstevel@tonic-gate 	}
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate /*
23730Sstevel@tonic-gate  * Search for a peer TCP/IPv4 loopback conn by doing a reverse lookup on
23740Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
23750Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
23762323Sethindra  * (peer tcp in ESTABLISHED state).
23770Sstevel@tonic-gate  */
23780Sstevel@tonic-gate conn_t *
23793448Sdh155122 ipcl_conn_tcp_lookup_reversed_ipv4(conn_t *connp, ipha_t *ipha, tcph_t *tcph,
23803448Sdh155122     ip_stack_t *ipst)
23810Sstevel@tonic-gate {
23820Sstevel@tonic-gate 	uint32_t ports;
23830Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
23840Sstevel@tonic-gate 	connf_t	*connfp;
23850Sstevel@tonic-gate 	conn_t	*tconnp;
23860Sstevel@tonic-gate 	boolean_t zone_chk;
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	/*
23890Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
23900Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
23910Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
23920Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.
23930Sstevel@tonic-gate 	 */
23940Sstevel@tonic-gate 	zone_chk = (ipha->ipha_src == htonl(INADDR_LOOPBACK) ||
23950Sstevel@tonic-gate 	    ipha->ipha_dst == htonl(INADDR_LOOPBACK));
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
23980Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
23990Sstevel@tonic-gate 
24003448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
24013448Sdh155122 	    ports, ipst)];
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24040Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24050Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
24080Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
24092323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24100Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24130Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24140Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24150Sstevel@tonic-gate 			return (tconnp);
24160Sstevel@tonic-gate 		}
24170Sstevel@tonic-gate 	}
24180Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
24190Sstevel@tonic-gate 	return (NULL);
24200Sstevel@tonic-gate }
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate /*
24230Sstevel@tonic-gate  * Search for a peer TCP/IPv6 loopback conn by doing a reverse lookup on
24240Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
24250Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
24262323Sethindra  * (peer tcp in ESTABLISHED state).
24270Sstevel@tonic-gate  */
24280Sstevel@tonic-gate conn_t *
24293448Sdh155122 ipcl_conn_tcp_lookup_reversed_ipv6(conn_t *connp, ip6_t *ip6h, tcph_t *tcph,
24303448Sdh155122     ip_stack_t *ipst)
24310Sstevel@tonic-gate {
24320Sstevel@tonic-gate 	uint32_t ports;
24330Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
24340Sstevel@tonic-gate 	connf_t	*connfp;
24350Sstevel@tonic-gate 	conn_t	*tconnp;
24360Sstevel@tonic-gate 	boolean_t zone_chk;
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	/*
24390Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
24400Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
24410Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
24420Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.  We
24430Sstevel@tonic-gate 	 * don't do Zone check for link local address(es) because the
24440Sstevel@tonic-gate 	 * current Zone implementation treats each link local address as
24450Sstevel@tonic-gate 	 * being unique per system node, i.e. they belong to global Zone.
24460Sstevel@tonic-gate 	 */
24470Sstevel@tonic-gate 	zone_chk = (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src) ||
24480Sstevel@tonic-gate 	    IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst));
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
24510Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
24520Sstevel@tonic-gate 
24533448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
24543448Sdh155122 	    ports, ipst)];
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24570Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24580Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 		/* We skip tcp_bound_if check here as this is loopback tcp */
24610Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
24620Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
24632323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24640Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24650Sstevel@tonic-gate 
24660Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24670Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24680Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24690Sstevel@tonic-gate 			return (tconnp);
24700Sstevel@tonic-gate 		}
24710Sstevel@tonic-gate 	}
24720Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
24730Sstevel@tonic-gate 	return (NULL);
24740Sstevel@tonic-gate }
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate /*
24770Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
24780Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
24790Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
24800Sstevel@tonic-gate  */
24810Sstevel@tonic-gate conn_t *
24823448Sdh155122 ipcl_tcp_lookup_reversed_ipv4(ipha_t *ipha, tcph_t *tcph, int min_state,
24833448Sdh155122     ip_stack_t *ipst)
24840Sstevel@tonic-gate {
24850Sstevel@tonic-gate 	uint32_t ports;
24860Sstevel@tonic-gate 	uint16_t *pports;
24870Sstevel@tonic-gate 	connf_t	*connfp;
24880Sstevel@tonic-gate 	conn_t	*tconnp;
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
24910Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
24920Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
24930Sstevel@tonic-gate 
24943448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
24954691Skcpoon 	    ports, ipst)];
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24980Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24990Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
25020Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
25030Sstevel@tonic-gate 		    tconnp->conn_tcp->tcp_state >= min_state) {
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25060Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25070Sstevel@tonic-gate 			return (tconnp);
25080Sstevel@tonic-gate 		}
25090Sstevel@tonic-gate 	}
25100Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25110Sstevel@tonic-gate 	return (NULL);
25120Sstevel@tonic-gate }
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate /*
25150Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
25160Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
25170Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
25180Sstevel@tonic-gate  * Match on ifindex in addition to addresses.
25190Sstevel@tonic-gate  */
25200Sstevel@tonic-gate conn_t *
25210Sstevel@tonic-gate ipcl_tcp_lookup_reversed_ipv6(ip6_t *ip6h, tcpha_t *tcpha, int min_state,
25223448Sdh155122     uint_t ifindex, ip_stack_t *ipst)
25230Sstevel@tonic-gate {
25240Sstevel@tonic-gate 	tcp_t	*tcp;
25250Sstevel@tonic-gate 	uint32_t ports;
25260Sstevel@tonic-gate 	uint16_t *pports;
25270Sstevel@tonic-gate 	connf_t	*connfp;
25280Sstevel@tonic-gate 	conn_t	*tconnp;
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
25310Sstevel@tonic-gate 	pports[0] = tcpha->tha_fport;
25320Sstevel@tonic-gate 	pports[1] = tcpha->tha_lport;
25330Sstevel@tonic-gate 
25343448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
25354691Skcpoon 	    ports, ipst)];
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
25380Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
25390Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 		tcp = tconnp->conn_tcp;
25420Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
25430Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
25440Sstevel@tonic-gate 		    tcp->tcp_state >= min_state &&
25450Sstevel@tonic-gate 		    (tcp->tcp_bound_if == 0 ||
25460Sstevel@tonic-gate 		    tcp->tcp_bound_if == ifindex)) {
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25490Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25500Sstevel@tonic-gate 			return (tconnp);
25510Sstevel@tonic-gate 		}
25520Sstevel@tonic-gate 	}
25530Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25540Sstevel@tonic-gate 	return (NULL);
25550Sstevel@tonic-gate }
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate /*
25581676Sjpk  * Finds a TCP/IPv4 listening connection; called by tcp_disconnect to locate
25591676Sjpk  * a listener when changing state.
25600Sstevel@tonic-gate  */
25610Sstevel@tonic-gate conn_t *
25623448Sdh155122 ipcl_lookup_listener_v4(uint16_t lport, ipaddr_t laddr, zoneid_t zoneid,
25633448Sdh155122     ip_stack_t *ipst)
25640Sstevel@tonic-gate {
25650Sstevel@tonic-gate 	connf_t		*bind_connfp;
25660Sstevel@tonic-gate 	conn_t		*connp;
25670Sstevel@tonic-gate 	tcp_t		*tcp;
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	/*
25700Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
25710Sstevel@tonic-gate 	 * all zeros.
25720Sstevel@tonic-gate 	 */
25730Sstevel@tonic-gate 	if (laddr == 0)
25740Sstevel@tonic-gate 		return (NULL);
25750Sstevel@tonic-gate 
25761676Sjpk 	ASSERT(zoneid != ALL_ZONES);
25771676Sjpk 
25783448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
25790Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
25800Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
25810Sstevel@tonic-gate 	    connp = connp->conn_next) {
25820Sstevel@tonic-gate 		tcp = connp->conn_tcp;
25830Sstevel@tonic-gate 		if (IPCL_BIND_MATCH(connp, IPPROTO_TCP, laddr, lport) &&
25842263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
25850Sstevel@tonic-gate 		    (tcp->tcp_listener == NULL)) {
25860Sstevel@tonic-gate 			CONN_INC_REF(connp);
25870Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
25880Sstevel@tonic-gate 			return (connp);
25890Sstevel@tonic-gate 		}
25900Sstevel@tonic-gate 	}
25910Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
25920Sstevel@tonic-gate 	return (NULL);
25930Sstevel@tonic-gate }
25940Sstevel@tonic-gate 
25951676Sjpk /*
25961676Sjpk  * Finds a TCP/IPv6 listening connection; called by tcp_disconnect to locate
25971676Sjpk  * a listener when changing state.
25981676Sjpk  */
25990Sstevel@tonic-gate conn_t *
26000Sstevel@tonic-gate ipcl_lookup_listener_v6(uint16_t lport, in6_addr_t *laddr, uint_t ifindex,
26013448Sdh155122     zoneid_t zoneid, ip_stack_t *ipst)
26020Sstevel@tonic-gate {
26030Sstevel@tonic-gate 	connf_t		*bind_connfp;
26040Sstevel@tonic-gate 	conn_t		*connp = NULL;
26050Sstevel@tonic-gate 	tcp_t		*tcp;
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate 	/*
26080Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
26090Sstevel@tonic-gate 	 * all zeros.
26100Sstevel@tonic-gate 	 */
26110Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(laddr))
26120Sstevel@tonic-gate 		return (NULL);
26130Sstevel@tonic-gate 
26141676Sjpk 	ASSERT(zoneid != ALL_ZONES);
26150Sstevel@tonic-gate 
26163448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
26170Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
26180Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
26190Sstevel@tonic-gate 	    connp = connp->conn_next) {
26200Sstevel@tonic-gate 		tcp = connp->conn_tcp;
26210Sstevel@tonic-gate 		if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP, *laddr, lport) &&
26222263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
26230Sstevel@tonic-gate 		    (tcp->tcp_bound_if == 0 ||
26240Sstevel@tonic-gate 		    tcp->tcp_bound_if == ifindex) &&
26250Sstevel@tonic-gate 		    tcp->tcp_listener == NULL) {
26260Sstevel@tonic-gate 			CONN_INC_REF(connp);
26270Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
26280Sstevel@tonic-gate 			return (connp);
26290Sstevel@tonic-gate 		}
26300Sstevel@tonic-gate 	}
26310Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
26320Sstevel@tonic-gate 	return (NULL);
26330Sstevel@tonic-gate }
26340Sstevel@tonic-gate 
2635741Smasputra /*
2636741Smasputra  * ipcl_get_next_conn
2637741Smasputra  *	get the next entry in the conn global list
2638741Smasputra  *	and put a reference on the next_conn.
2639741Smasputra  *	decrement the reference on the current conn.
2640741Smasputra  *
2641741Smasputra  * This is an iterator based walker function that also provides for
2642741Smasputra  * some selection by the caller. It walks through the conn_hash bucket
2643741Smasputra  * searching for the next valid connp in the list, and selects connections
2644741Smasputra  * that are neither closed nor condemned. It also REFHOLDS the conn
2645741Smasputra  * thus ensuring that the conn exists when the caller uses the conn.
2646741Smasputra  */
2647741Smasputra conn_t *
2648741Smasputra ipcl_get_next_conn(connf_t *connfp, conn_t *connp, uint32_t conn_flags)
2649741Smasputra {
2650741Smasputra 	conn_t	*next_connp;
2651741Smasputra 
2652741Smasputra 	if (connfp == NULL)
2653741Smasputra 		return (NULL);
2654741Smasputra 
2655741Smasputra 	mutex_enter(&connfp->connf_lock);
2656741Smasputra 
2657741Smasputra 	next_connp = (connp == NULL) ?
2658741Smasputra 	    connfp->connf_head : connp->conn_g_next;
2659741Smasputra 
2660741Smasputra 	while (next_connp != NULL) {
2661741Smasputra 		mutex_enter(&next_connp->conn_lock);
2662741Smasputra 		if (!(next_connp->conn_flags & conn_flags) ||
2663741Smasputra 		    (next_connp->conn_state_flags &
2664741Smasputra 		    (CONN_CONDEMNED | CONN_INCIPIENT))) {
2665741Smasputra 			/*
2666741Smasputra 			 * This conn has been condemned or
2667741Smasputra 			 * is closing, or the flags don't match
2668741Smasputra 			 */
2669741Smasputra 			mutex_exit(&next_connp->conn_lock);
2670741Smasputra 			next_connp = next_connp->conn_g_next;
2671741Smasputra 			continue;
2672741Smasputra 		}
2673741Smasputra 		CONN_INC_REF_LOCKED(next_connp);
2674741Smasputra 		mutex_exit(&next_connp->conn_lock);
2675741Smasputra 		break;
2676741Smasputra 	}
2677741Smasputra 
2678741Smasputra 	mutex_exit(&connfp->connf_lock);
2679741Smasputra 
2680741Smasputra 	if (connp != NULL)
2681741Smasputra 		CONN_DEC_REF(connp);
2682741Smasputra 
2683741Smasputra 	return (next_connp);
2684741Smasputra }
2685741Smasputra 
26860Sstevel@tonic-gate #ifdef CONN_DEBUG
26870Sstevel@tonic-gate /*
26880Sstevel@tonic-gate  * Trace of the last NBUF refhold/refrele
26890Sstevel@tonic-gate  */
26900Sstevel@tonic-gate int
26910Sstevel@tonic-gate conn_trace_ref(conn_t *connp)
26920Sstevel@tonic-gate {
26930Sstevel@tonic-gate 	int	last;
26940Sstevel@tonic-gate 	conn_trace_t	*ctb;
26950Sstevel@tonic-gate 
26960Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
26970Sstevel@tonic-gate 	last = connp->conn_trace_last;
26980Sstevel@tonic-gate 	last++;
26990Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
27000Sstevel@tonic-gate 		last = 0;
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
27035023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27040Sstevel@tonic-gate 	connp->conn_trace_last = last;
27050Sstevel@tonic-gate 	return (1);
27060Sstevel@tonic-gate }
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate int
27090Sstevel@tonic-gate conn_untrace_ref(conn_t *connp)
27100Sstevel@tonic-gate {
27110Sstevel@tonic-gate 	int	last;
27120Sstevel@tonic-gate 	conn_trace_t	*ctb;
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
27150Sstevel@tonic-gate 	last = connp->conn_trace_last;
27160Sstevel@tonic-gate 	last++;
27170Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
27180Sstevel@tonic-gate 		last = 0;
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
27215023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27220Sstevel@tonic-gate 	connp->conn_trace_last = last;
27230Sstevel@tonic-gate 	return (1);
27240Sstevel@tonic-gate }
27250Sstevel@tonic-gate #endif
2726