xref: /onnv-gate/usr/src/uts/common/inet/ip/ipclassifier.c (revision 4691:ca8edd01a7e2)
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
2330Sstevel@tonic-gate  *		created.
2340Sstevel@tonic-gate  *		IPCL_TCPCONN	indicates a TCP connection
2350Sstevel@tonic-gate  *		IPCL_IPCONN	indicates all non-TCP connections.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * void ipcl_conn_destroy(connp)
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  * 	Destroys the connection state, removes it from the global
2400Sstevel@tonic-gate  * 	connection hash table and frees its memory.
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate #include <sys/types.h>
2440Sstevel@tonic-gate #include <sys/stream.h>
2450Sstevel@tonic-gate #include <sys/stropts.h>
2460Sstevel@tonic-gate #include <sys/sysmacros.h>
2470Sstevel@tonic-gate #include <sys/strsubr.h>
2480Sstevel@tonic-gate #include <sys/strsun.h>
2490Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
2500Sstevel@tonic-gate #include <sys/ddi.h>
2510Sstevel@tonic-gate #include <sys/cmn_err.h>
2520Sstevel@tonic-gate #include <sys/debug.h>
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #include <sys/systm.h>
2550Sstevel@tonic-gate #include <sys/param.h>
2560Sstevel@tonic-gate #include <sys/kmem.h>
2570Sstevel@tonic-gate #include <sys/isa_defs.h>
2580Sstevel@tonic-gate #include <inet/common.h>
2590Sstevel@tonic-gate #include <netinet/ip6.h>
2600Sstevel@tonic-gate #include <netinet/icmp6.h>
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate #include <inet/ip.h>
2630Sstevel@tonic-gate #include <inet/ip6.h>
2640Sstevel@tonic-gate #include <inet/tcp.h>
2650Sstevel@tonic-gate #include <inet/ip_ndp.h>
266741Smasputra #include <inet/udp_impl.h>
2670Sstevel@tonic-gate #include <inet/sctp_ip.h>
2683448Sdh155122 #include <inet/sctp/sctp_impl.h>
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate #include <sys/cpuvar.h>
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate #include <inet/ipclassifier.h>
2730Sstevel@tonic-gate #include <inet/ipsec_impl.h>
2740Sstevel@tonic-gate 
2751676Sjpk #include <sys/tsol/tnet.h>
2761676Sjpk 
2770Sstevel@tonic-gate #ifdef DEBUG
2780Sstevel@tonic-gate #define	IPCL_DEBUG
2790Sstevel@tonic-gate #else
2800Sstevel@tonic-gate #undef	IPCL_DEBUG
2810Sstevel@tonic-gate #endif
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate #ifdef	IPCL_DEBUG
2840Sstevel@tonic-gate int	ipcl_debug_level = 0;
2850Sstevel@tonic-gate #define	IPCL_DEBUG_LVL(level, args)	\
2860Sstevel@tonic-gate 	if (ipcl_debug_level  & level) { printf args; }
2870Sstevel@tonic-gate #else
2880Sstevel@tonic-gate #define	IPCL_DEBUG_LVL(level, args) {; }
2890Sstevel@tonic-gate #endif
2903448Sdh155122 /* Old value for compatibility. Setable in /etc/system */
2910Sstevel@tonic-gate uint_t tcp_conn_hash_size = 0;
2920Sstevel@tonic-gate 
2933448Sdh155122 /* New value. Zero means choose automatically.  Setable in /etc/system */
2940Sstevel@tonic-gate uint_t ipcl_conn_hash_size = 0;
2950Sstevel@tonic-gate uint_t ipcl_conn_hash_memfactor = 8192;
2960Sstevel@tonic-gate uint_t ipcl_conn_hash_maxsize = 82500;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /* bind/udp fanout table size */
2990Sstevel@tonic-gate uint_t ipcl_bind_fanout_size = 512;
3001503Sericheng uint_t ipcl_udp_fanout_size = 16384;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /* Raw socket fanout size.  Must be a power of 2. */
3030Sstevel@tonic-gate uint_t ipcl_raw_fanout_size = 256;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate  * Power of 2^N Primes useful for hashing for N of 0-28,
3070Sstevel@tonic-gate  * these primes are the nearest prime <= 2^N - 2^(N-2).
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate #define	P2Ps() {0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,	\
3110Sstevel@tonic-gate 		6143, 12281, 24571, 49139, 98299, 196597, 393209,	\
3120Sstevel@tonic-gate 		786431, 1572853, 3145721, 6291449, 12582893, 25165813,	\
3130Sstevel@tonic-gate 		50331599, 100663291, 201326557, 0}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * wrapper structure to ensure that conn+tcpb are aligned
3170Sstevel@tonic-gate  * on cache lines.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate typedef struct itc_s {
3200Sstevel@tonic-gate 	union {
3210Sstevel@tonic-gate 		conn_t	itcu_conn;
3220Sstevel@tonic-gate 		char	itcu_filler[CACHE_ALIGN(conn_s)];
3230Sstevel@tonic-gate 	}	itc_u;
3240Sstevel@tonic-gate 	tcp_t	itc_tcp;
3250Sstevel@tonic-gate } itc_t;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate #define	itc_conn	itc_u.itcu_conn
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate struct kmem_cache  *ipcl_tcpconn_cache;
3300Sstevel@tonic-gate struct kmem_cache  *ipcl_conn_cache;
3310Sstevel@tonic-gate extern struct kmem_cache  *sctp_conn_cache;
3320Sstevel@tonic-gate extern struct kmem_cache  *tcp_sack_info_cache;
3330Sstevel@tonic-gate extern struct kmem_cache  *tcp_iphc_cache;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate extern void	tcp_timermp_free(tcp_t *);
3360Sstevel@tonic-gate extern mblk_t	*tcp_timermp_alloc(int);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static int	ipcl_tcpconn_constructor(void *, void *, int);
3390Sstevel@tonic-gate static void	ipcl_tcpconn_destructor(void *, void *);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate #ifdef	IPCL_DEBUG
3420Sstevel@tonic-gate #define	INET_NTOA_BUFSIZE	18
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static char *
3450Sstevel@tonic-gate inet_ntoa_r(uint32_t in, char *b)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	unsigned char	*p;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	p = (unsigned char *)&in;
3500Sstevel@tonic-gate 	(void) sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
3510Sstevel@tonic-gate 	return (b);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate #endif
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate /*
3563448Sdh155122  * Global (for all stack instances) init routine
3570Sstevel@tonic-gate  */
3580Sstevel@tonic-gate void
3593448Sdh155122 ipcl_g_init(void)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	ipcl_conn_cache = kmem_cache_create("ipcl_conn_cache",
3620Sstevel@tonic-gate 	    sizeof (conn_t), CACHE_ALIGN_SIZE,
363741Smasputra 	    NULL, NULL, NULL, NULL, NULL, 0);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	ipcl_tcpconn_cache = kmem_cache_create("ipcl_tcpconn_cache",
3660Sstevel@tonic-gate 	    sizeof (itc_t), CACHE_ALIGN_SIZE,
3670Sstevel@tonic-gate 	    ipcl_tcpconn_constructor, ipcl_tcpconn_destructor,
3680Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
3693448Sdh155122 }
3703448Sdh155122 
3713448Sdh155122 /*
3723448Sdh155122  * ipclassifier intialization routine, sets up hash tables.
3733448Sdh155122  */
3743448Sdh155122 void
3753448Sdh155122 ipcl_init(ip_stack_t *ipst)
3763448Sdh155122 {
3773448Sdh155122 	int i;
3783448Sdh155122 	int sizes[] = P2Ps();
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/*
3813448Sdh155122 	 * Calculate size of conn fanout table from /etc/system settings
3820Sstevel@tonic-gate 	 */
3830Sstevel@tonic-gate 	if (ipcl_conn_hash_size != 0) {
3843448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = ipcl_conn_hash_size;
3850Sstevel@tonic-gate 	} else if (tcp_conn_hash_size != 0) {
3863448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = tcp_conn_hash_size;
3870Sstevel@tonic-gate 	} else {
3880Sstevel@tonic-gate 		extern pgcnt_t freemem;
3890Sstevel@tonic-gate 
3903448Sdh155122 		ipst->ips_ipcl_conn_fanout_size =
3910Sstevel@tonic-gate 		    (freemem * PAGESIZE) / ipcl_conn_hash_memfactor;
3920Sstevel@tonic-gate 
3933448Sdh155122 		if (ipst->ips_ipcl_conn_fanout_size > ipcl_conn_hash_maxsize) {
3943448Sdh155122 			ipst->ips_ipcl_conn_fanout_size =
3953448Sdh155122 			    ipcl_conn_hash_maxsize;
3963448Sdh155122 		}
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	for (i = 9; i < sizeof (sizes) / sizeof (*sizes) - 1; i++) {
4003448Sdh155122 		if (sizes[i] >= ipst->ips_ipcl_conn_fanout_size) {
4010Sstevel@tonic-gate 			break;
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 	}
4043448Sdh155122 	if ((ipst->ips_ipcl_conn_fanout_size = sizes[i]) == 0) {
4050Sstevel@tonic-gate 		/* Out of range, use the 2^16 value */
4063448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = sizes[16];
4070Sstevel@tonic-gate 	}
4083448Sdh155122 
4093448Sdh155122 	/* Take values from /etc/system */
4103448Sdh155122 	ipst->ips_ipcl_bind_fanout_size = ipcl_bind_fanout_size;
4113448Sdh155122 	ipst->ips_ipcl_udp_fanout_size = ipcl_udp_fanout_size;
4123448Sdh155122 	ipst->ips_ipcl_raw_fanout_size = ipcl_raw_fanout_size;
4130Sstevel@tonic-gate 
4143448Sdh155122 	ASSERT(ipst->ips_ipcl_conn_fanout == NULL);
4153448Sdh155122 
4163448Sdh155122 	ipst->ips_ipcl_conn_fanout = kmem_zalloc(
4173448Sdh155122 	    ipst->ips_ipcl_conn_fanout_size * sizeof (connf_t), KM_SLEEP);
4183448Sdh155122 
4193448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
4203448Sdh155122 		mutex_init(&ipst->ips_ipcl_conn_fanout[i].connf_lock, NULL,
4210Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4243448Sdh155122 	ipst->ips_ipcl_bind_fanout = kmem_zalloc(
4253448Sdh155122 	    ipst->ips_ipcl_bind_fanout_size * sizeof (connf_t), KM_SLEEP);
4260Sstevel@tonic-gate 
4273448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
4283448Sdh155122 		mutex_init(&ipst->ips_ipcl_bind_fanout[i].connf_lock, NULL,
4290Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4323448Sdh155122 	ipst->ips_ipcl_proto_fanout = kmem_zalloc(IPPROTO_MAX *
4333448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4343448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4353448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout[i].connf_lock, NULL,
4360Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4370Sstevel@tonic-gate 	}
4383448Sdh155122 
4393448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = kmem_zalloc(IPPROTO_MAX *
4403448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4413448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4423448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock, NULL,
4430Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4463448Sdh155122 	ipst->ips_rts_clients = kmem_zalloc(sizeof (connf_t), KM_SLEEP);
4473448Sdh155122 	mutex_init(&ipst->ips_rts_clients->connf_lock,
4483448Sdh155122 	    NULL, MUTEX_DEFAULT, NULL);
4490Sstevel@tonic-gate 
4503448Sdh155122 	ipst->ips_ipcl_udp_fanout = kmem_zalloc(
4513448Sdh155122 	    ipst->ips_ipcl_udp_fanout_size * sizeof (connf_t), KM_SLEEP);
4523448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
4533448Sdh155122 		mutex_init(&ipst->ips_ipcl_udp_fanout[i].connf_lock, NULL,
4540Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4573448Sdh155122 	ipst->ips_ipcl_raw_fanout = kmem_zalloc(
4583448Sdh155122 	    ipst->ips_ipcl_raw_fanout_size * sizeof (connf_t), KM_SLEEP);
4593448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
4603448Sdh155122 		mutex_init(&ipst->ips_ipcl_raw_fanout[i].connf_lock, NULL,
4610Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4643448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = kmem_zalloc(
4653448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE, KM_SLEEP);
4660Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
4673448Sdh155122 		mutex_init(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock,
4683448Sdh155122 		    NULL, MUTEX_DEFAULT, NULL);
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate void
4733448Sdh155122 ipcl_g_destroy(void)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	kmem_cache_destroy(ipcl_conn_cache);
4760Sstevel@tonic-gate 	kmem_cache_destroy(ipcl_tcpconn_cache);
4773448Sdh155122 }
4783448Sdh155122 
4793448Sdh155122 /*
4803448Sdh155122  * All user-level and kernel use of the stack must be gone
4813448Sdh155122  * by now.
4823448Sdh155122  */
4833448Sdh155122 void
4843448Sdh155122 ipcl_destroy(ip_stack_t *ipst)
4853448Sdh155122 {
4863448Sdh155122 	int i;
4873448Sdh155122 
4883448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
4893448Sdh155122 		ASSERT(ipst->ips_ipcl_conn_fanout[i].connf_head == NULL);
4903448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_conn_fanout[i].connf_lock);
4913448Sdh155122 	}
4923448Sdh155122 	kmem_free(ipst->ips_ipcl_conn_fanout, ipst->ips_ipcl_conn_fanout_size *
4933448Sdh155122 	    sizeof (connf_t));
4943448Sdh155122 	ipst->ips_ipcl_conn_fanout = NULL;
4953448Sdh155122 
4963448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
4973448Sdh155122 		ASSERT(ipst->ips_ipcl_bind_fanout[i].connf_head == NULL);
4983448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_bind_fanout[i].connf_lock);
4993448Sdh155122 	}
5003448Sdh155122 	kmem_free(ipst->ips_ipcl_bind_fanout, ipst->ips_ipcl_bind_fanout_size *
5013448Sdh155122 	    sizeof (connf_t));
5023448Sdh155122 	ipst->ips_ipcl_bind_fanout = NULL;
5033448Sdh155122 
5043448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5053448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout[i].connf_head == NULL);
5063448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout[i].connf_lock);
5073448Sdh155122 	}
5083448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout, IPPROTO_MAX * sizeof (connf_t));
5093448Sdh155122 	ipst->ips_ipcl_proto_fanout = NULL;
5100Sstevel@tonic-gate 
5113448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5123448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout_v6[i].connf_head == NULL);
5133448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock);
5143448Sdh155122 	}
5153448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout_v6,
5163448Sdh155122 	    IPPROTO_MAX * sizeof (connf_t));
5173448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = NULL;
5183448Sdh155122 
5193448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
5203448Sdh155122 		ASSERT(ipst->ips_ipcl_udp_fanout[i].connf_head == NULL);
5213448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_udp_fanout[i].connf_lock);
5223448Sdh155122 	}
5233448Sdh155122 	kmem_free(ipst->ips_ipcl_udp_fanout, ipst->ips_ipcl_udp_fanout_size *
5243448Sdh155122 	    sizeof (connf_t));
5253448Sdh155122 	ipst->ips_ipcl_udp_fanout = NULL;
5260Sstevel@tonic-gate 
5273448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
5283448Sdh155122 		ASSERT(ipst->ips_ipcl_raw_fanout[i].connf_head == NULL);
5293448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_raw_fanout[i].connf_lock);
5303448Sdh155122 	}
5313448Sdh155122 	kmem_free(ipst->ips_ipcl_raw_fanout, ipst->ips_ipcl_raw_fanout_size *
5323448Sdh155122 	    sizeof (connf_t));
5333448Sdh155122 	ipst->ips_ipcl_raw_fanout = NULL;
5340Sstevel@tonic-gate 
5353448Sdh155122 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
5363448Sdh155122 		ASSERT(ipst->ips_ipcl_globalhash_fanout[i].connf_head == NULL);
5373448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
5383448Sdh155122 	}
5393448Sdh155122 	kmem_free(ipst->ips_ipcl_globalhash_fanout,
5403448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE);
5413448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = NULL;
5420Sstevel@tonic-gate 
5433448Sdh155122 	ASSERT(ipst->ips_rts_clients->connf_head == NULL);
5443448Sdh155122 	mutex_destroy(&ipst->ips_rts_clients->connf_lock);
5453448Sdh155122 	kmem_free(ipst->ips_rts_clients, sizeof (connf_t));
5463448Sdh155122 	ipst->ips_rts_clients = NULL;
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * conn creation routine. initialize the conn, sets the reference
5510Sstevel@tonic-gate  * and inserts it in the global hash table.
5520Sstevel@tonic-gate  */
5530Sstevel@tonic-gate conn_t *
5543448Sdh155122 ipcl_conn_create(uint32_t type, int sleep, netstack_t *ns)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	itc_t	*itc;
5570Sstevel@tonic-gate 	conn_t	*connp;
5583448Sdh155122 	sctp_stack_t *sctps;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	switch (type) {
5610Sstevel@tonic-gate 	case IPCL_TCPCONN:
5620Sstevel@tonic-gate 		if ((itc = kmem_cache_alloc(ipcl_tcpconn_cache,
5630Sstevel@tonic-gate 		    sleep)) == NULL)
5640Sstevel@tonic-gate 			return (NULL);
5650Sstevel@tonic-gate 		connp = &itc->itc_conn;
5660Sstevel@tonic-gate 		connp->conn_ref = 1;
5673448Sdh155122 		netstack_hold(ns);
5683448Sdh155122 		connp->conn_netstack = ns;
5690Sstevel@tonic-gate 		IPCL_DEBUG_LVL(1,
5700Sstevel@tonic-gate 		    ("ipcl_conn_create: connp = %p tcp (%p)",
5710Sstevel@tonic-gate 		    (void *)connp, (void *)connp->conn_tcp));
5720Sstevel@tonic-gate 		ipcl_globalhash_insert(connp);
5730Sstevel@tonic-gate 		break;
5740Sstevel@tonic-gate 	case IPCL_SCTPCONN:
5750Sstevel@tonic-gate 		if ((connp = kmem_cache_alloc(sctp_conn_cache, sleep)) == NULL)
5760Sstevel@tonic-gate 			return (NULL);
577*4691Skcpoon 		sctp_conn_init(connp);
5783448Sdh155122 		sctps = ns->netstack_sctp;
5793448Sdh155122 		SCTP_G_Q_REFHOLD(sctps);
5803448Sdh155122 		netstack_hold(ns);
5813448Sdh155122 		connp->conn_netstack = ns;
5820Sstevel@tonic-gate 		break;
5830Sstevel@tonic-gate 	case IPCL_IPCCONN:
5840Sstevel@tonic-gate 		connp = kmem_cache_alloc(ipcl_conn_cache, sleep);
5850Sstevel@tonic-gate 		if (connp == NULL)
586741Smasputra 			return (NULL);
5870Sstevel@tonic-gate 		bzero(connp, sizeof (conn_t));
588741Smasputra 		mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
5890Sstevel@tonic-gate 		cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
590741Smasputra 		connp->conn_flags = IPCL_IPCCONN;
5910Sstevel@tonic-gate 		connp->conn_ref = 1;
5923448Sdh155122 		netstack_hold(ns);
5933448Sdh155122 		connp->conn_netstack = ns;
5940Sstevel@tonic-gate 		IPCL_DEBUG_LVL(1,
5950Sstevel@tonic-gate 		    ("ipcl_conn_create: connp = %p\n", (void *)connp));
5960Sstevel@tonic-gate 		ipcl_globalhash_insert(connp);
5970Sstevel@tonic-gate 		break;
598741Smasputra 	default:
599741Smasputra 		connp = NULL;
600741Smasputra 		ASSERT(0);
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	return (connp);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate void
6070Sstevel@tonic-gate ipcl_conn_destroy(conn_t *connp)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	mblk_t	*mp;
6103448Sdh155122 	netstack_t	*ns = connp->conn_netstack;
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&connp->conn_lock));
6130Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6140Sstevel@tonic-gate 	ASSERT(connp->conn_ire_cache == NULL);
6150Sstevel@tonic-gate 
6161676Sjpk 	if (connp->conn_peercred != NULL &&
6171676Sjpk 	    connp->conn_peercred != connp->conn_cred)
6181676Sjpk 		crfree(connp->conn_peercred);
6191676Sjpk 	connp->conn_peercred = NULL;
6201676Sjpk 
6211676Sjpk 	if (connp->conn_cred != NULL) {
6221676Sjpk 		crfree(connp->conn_cred);
6231676Sjpk 		connp->conn_cred = NULL;
6241676Sjpk 	}
6251676Sjpk 
6260Sstevel@tonic-gate 	ipcl_globalhash_remove(connp);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	cv_destroy(&connp->conn_cv);
6290Sstevel@tonic-gate 	if (connp->conn_flags & IPCL_TCPCONN) {
630741Smasputra 		tcp_t	*tcp = connp->conn_tcp;
6313448Sdh155122 		tcp_stack_t *tcps;
6323448Sdh155122 
6333448Sdh155122 		ASSERT(tcp != NULL);
6343448Sdh155122 		tcps = tcp->tcp_tcps;
6353448Sdh155122 		if (tcps != NULL) {
6363448Sdh155122 			if (connp->conn_latch != NULL) {
6373448Sdh155122 				IPLATCH_REFRELE(connp->conn_latch, ns);
6383448Sdh155122 				connp->conn_latch = NULL;
6393448Sdh155122 			}
6403448Sdh155122 			if (connp->conn_policy != NULL) {
6413448Sdh155122 				IPPH_REFRELE(connp->conn_policy, ns);
6423448Sdh155122 				connp->conn_policy = NULL;
6433448Sdh155122 			}
6443448Sdh155122 			tcp->tcp_tcps = NULL;
6453448Sdh155122 			TCPS_REFRELE(tcps);
6463448Sdh155122 		}
647741Smasputra 
6480Sstevel@tonic-gate 		mutex_destroy(&connp->conn_lock);
6490Sstevel@tonic-gate 		tcp_free(tcp);
6500Sstevel@tonic-gate 		mp = tcp->tcp_timercache;
6511676Sjpk 		tcp->tcp_cred = NULL;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		if (tcp->tcp_sack_info != NULL) {
6540Sstevel@tonic-gate 			bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
6550Sstevel@tonic-gate 			kmem_cache_free(tcp_sack_info_cache,
6560Sstevel@tonic-gate 			    tcp->tcp_sack_info);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		if (tcp->tcp_iphc != NULL) {
6590Sstevel@tonic-gate 			if (tcp->tcp_hdr_grown) {
6600Sstevel@tonic-gate 				kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len);
6610Sstevel@tonic-gate 			} else {
6620Sstevel@tonic-gate 				bzero(tcp->tcp_iphc, tcp->tcp_iphc_len);
6630Sstevel@tonic-gate 				kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc);
6640Sstevel@tonic-gate 			}
6650Sstevel@tonic-gate 			tcp->tcp_iphc_len = 0;
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 		ASSERT(tcp->tcp_iphc_len == 0);
6680Sstevel@tonic-gate 
6693448Sdh155122 		ASSERT(connp->conn_latch == NULL);
6703448Sdh155122 		ASSERT(connp->conn_policy == NULL);
6713448Sdh155122 
6720Sstevel@tonic-gate 		bzero(connp, sizeof (itc_t));
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		tcp->tcp_timercache = mp;
6750Sstevel@tonic-gate 		connp->conn_tcp = tcp;
6760Sstevel@tonic-gate 		connp->conn_flags = IPCL_TCPCONN;
6770Sstevel@tonic-gate 		connp->conn_ulp = IPPROTO_TCP;
6780Sstevel@tonic-gate 		tcp->tcp_connp = connp;
6793448Sdh155122 		if (ns != NULL) {
6803448Sdh155122 			ASSERT(tcp->tcp_tcps == NULL);
6813448Sdh155122 			connp->conn_netstack = NULL;
6823448Sdh155122 			netstack_rele(ns);
6833448Sdh155122 		}
6840Sstevel@tonic-gate 		kmem_cache_free(ipcl_tcpconn_cache, connp);
6850Sstevel@tonic-gate 	} else if (connp->conn_flags & IPCL_SCTPCONN) {
6863448Sdh155122 		ASSERT(ns != NULL);
6870Sstevel@tonic-gate 		sctp_free(connp);
6880Sstevel@tonic-gate 	} else {
689741Smasputra 		ASSERT(connp->conn_udp == NULL);
6900Sstevel@tonic-gate 		mutex_destroy(&connp->conn_lock);
6913448Sdh155122 		if (ns != NULL) {
6923448Sdh155122 			connp->conn_netstack = NULL;
6933448Sdh155122 			netstack_rele(ns);
6943448Sdh155122 		}
6950Sstevel@tonic-gate 		kmem_cache_free(ipcl_conn_cache, connp);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate /*
7000Sstevel@tonic-gate  * Running in cluster mode - deregister listener information
7010Sstevel@tonic-gate  */
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate static void
7040Sstevel@tonic-gate ipcl_conn_unlisten(conn_t *connp)
7050Sstevel@tonic-gate {
7060Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) != 0);
7070Sstevel@tonic-gate 	ASSERT(connp->conn_lport != 0);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	if (cl_inet_unlisten != NULL) {
7100Sstevel@tonic-gate 		sa_family_t	addr_family;
7110Sstevel@tonic-gate 		uint8_t		*laddrp;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 		if (connp->conn_pkt_isv6) {
7140Sstevel@tonic-gate 			addr_family = AF_INET6;
7150Sstevel@tonic-gate 			laddrp = (uint8_t *)&connp->conn_bound_source_v6;
7160Sstevel@tonic-gate 		} else {
7170Sstevel@tonic-gate 			addr_family = AF_INET;
7180Sstevel@tonic-gate 			laddrp = (uint8_t *)&connp->conn_bound_source;
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 		(*cl_inet_unlisten)(IPPROTO_TCP, addr_family, laddrp,
7210Sstevel@tonic-gate 		    connp->conn_lport);
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 	connp->conn_flags &= ~IPCL_CL_LISTENER;
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate /*
7270Sstevel@tonic-gate  * We set the IPCL_REMOVED flag (instead of clearing the flag indicating
7280Sstevel@tonic-gate  * which table the conn belonged to). So for debugging we can see which hash
7290Sstevel@tonic-gate  * table this connection was in.
7300Sstevel@tonic-gate  */
7310Sstevel@tonic-gate #define	IPCL_HASH_REMOVE(connp)	{					\
7320Sstevel@tonic-gate 	connf_t	*connfp = (connp)->conn_fanout;				\
7330Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&((connp)->conn_lock)));			\
7340Sstevel@tonic-gate 	if (connfp != NULL) {						\
7350Sstevel@tonic-gate 		IPCL_DEBUG_LVL(4, ("IPCL_HASH_REMOVE: connp %p",	\
7360Sstevel@tonic-gate 		    (void *)(connp)));					\
7370Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);			\
7380Sstevel@tonic-gate 		if ((connp)->conn_next != NULL)				\
7390Sstevel@tonic-gate 			(connp)->conn_next->conn_prev =			\
7400Sstevel@tonic-gate 			    (connp)->conn_prev;				\
7410Sstevel@tonic-gate 		if ((connp)->conn_prev != NULL)				\
7420Sstevel@tonic-gate 			(connp)->conn_prev->conn_next =			\
7430Sstevel@tonic-gate 			    (connp)->conn_next;				\
7440Sstevel@tonic-gate 		else							\
7450Sstevel@tonic-gate 			connfp->connf_head = (connp)->conn_next;	\
7460Sstevel@tonic-gate 		(connp)->conn_fanout = NULL;				\
7470Sstevel@tonic-gate 		(connp)->conn_next = NULL;				\
7480Sstevel@tonic-gate 		(connp)->conn_prev = NULL;				\
7490Sstevel@tonic-gate 		(connp)->conn_flags |= IPCL_REMOVED;			\
7500Sstevel@tonic-gate 		if (((connp)->conn_flags & IPCL_CL_LISTENER) != 0)	\
7510Sstevel@tonic-gate 			ipcl_conn_unlisten((connp));			\
7520Sstevel@tonic-gate 		CONN_DEC_REF((connp));					\
7530Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);			\
7540Sstevel@tonic-gate 	}								\
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate void
7580Sstevel@tonic-gate ipcl_hash_remove(conn_t *connp)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	IPCL_HASH_REMOVE(connp);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate /*
7640Sstevel@tonic-gate  * The whole purpose of this function is allow removal of
7650Sstevel@tonic-gate  * a conn_t from the connected hash for timewait reclaim.
7660Sstevel@tonic-gate  * This is essentially a TW reclaim fastpath where timewait
7670Sstevel@tonic-gate  * collector checks under fanout lock (so no one else can
7680Sstevel@tonic-gate  * get access to the conn_t) that refcnt is 2 i.e. one for
7690Sstevel@tonic-gate  * TCP and one for the classifier hash list. If ref count
7700Sstevel@tonic-gate  * is indeed 2, we can just remove the conn under lock and
7710Sstevel@tonic-gate  * avoid cleaning up the conn under squeue. This gives us
7720Sstevel@tonic-gate  * improved performance.
7730Sstevel@tonic-gate  */
7740Sstevel@tonic-gate void
7750Sstevel@tonic-gate ipcl_hash_remove_locked(conn_t *connp, connf_t	*connfp)
7760Sstevel@tonic-gate {
7770Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connfp->connf_lock));
7780Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
7790Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) == 0);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if ((connp)->conn_next != NULL) {
782*4691Skcpoon 		(connp)->conn_next->conn_prev = (connp)->conn_prev;
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	if ((connp)->conn_prev != NULL) {
785*4691Skcpoon 		(connp)->conn_prev->conn_next = (connp)->conn_next;
7860Sstevel@tonic-gate 	} else {
7870Sstevel@tonic-gate 		connfp->connf_head = (connp)->conn_next;
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	(connp)->conn_fanout = NULL;
7900Sstevel@tonic-gate 	(connp)->conn_next = NULL;
7910Sstevel@tonic-gate 	(connp)->conn_prev = NULL;
7920Sstevel@tonic-gate 	(connp)->conn_flags |= IPCL_REMOVED;
7930Sstevel@tonic-gate 	ASSERT((connp)->conn_ref == 2);
7940Sstevel@tonic-gate 	(connp)->conn_ref--;
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp) {		\
7980Sstevel@tonic-gate 	ASSERT((connp)->conn_fanout == NULL);				\
7990Sstevel@tonic-gate 	ASSERT((connp)->conn_next == NULL);				\
8000Sstevel@tonic-gate 	ASSERT((connp)->conn_prev == NULL);				\
8010Sstevel@tonic-gate 	if ((connfp)->connf_head != NULL) {				\
8020Sstevel@tonic-gate 		(connfp)->connf_head->conn_prev = (connp);		\
8030Sstevel@tonic-gate 		(connp)->conn_next = (connfp)->connf_head;		\
8040Sstevel@tonic-gate 	}								\
8050Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8060Sstevel@tonic-gate 	(connfp)->connf_head = (connp);					\
8070Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8080Sstevel@tonic-gate 	    IPCL_CONNECTED;						\
8090Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED(connfp, connp) {			\
8130Sstevel@tonic-gate 	IPCL_DEBUG_LVL(8, ("IPCL_HASH_INSERT_CONNECTED: connfp %p "	\
8140Sstevel@tonic-gate 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
8150Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8160Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8170Sstevel@tonic-gate 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);		\
8180Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate #define	IPCL_HASH_INSERT_BOUND(connfp, connp) {				\
8220Sstevel@tonic-gate 	conn_t *pconnp = NULL, *nconnp;					\
8230Sstevel@tonic-gate 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_BOUND: connfp %p "	\
8240Sstevel@tonic-gate 	    "connp %p", (void *)connfp, (void *)(connp)));		\
8250Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8260Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8270Sstevel@tonic-gate 	nconnp = (connfp)->connf_head;					\
828153Sethindra 	while (nconnp != NULL &&					\
829153Sethindra 	    !_IPCL_V4_MATCH_ANY(nconnp->conn_srcv6)) {			\
830153Sethindra 		pconnp = nconnp;					\
831153Sethindra 		nconnp = nconnp->conn_next;				\
8320Sstevel@tonic-gate 	}								\
8330Sstevel@tonic-gate 	if (pconnp != NULL) {						\
8340Sstevel@tonic-gate 		pconnp->conn_next = (connp);				\
8350Sstevel@tonic-gate 		(connp)->conn_prev = pconnp;				\
8360Sstevel@tonic-gate 	} else {							\
8370Sstevel@tonic-gate 		(connfp)->connf_head = (connp);				\
8380Sstevel@tonic-gate 	}								\
8390Sstevel@tonic-gate 	if (nconnp != NULL) {						\
8400Sstevel@tonic-gate 		(connp)->conn_next = nconnp;				\
8410Sstevel@tonic-gate 		nconnp->conn_prev = (connp);				\
8420Sstevel@tonic-gate 	}								\
8430Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8440Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8450Sstevel@tonic-gate 	    IPCL_BOUND;							\
8460Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
8470Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate #define	IPCL_HASH_INSERT_WILDCARD(connfp, connp) {			\
8510Sstevel@tonic-gate 	conn_t **list, *prev, *next;					\
8520Sstevel@tonic-gate 	boolean_t isv4mapped =						\
8530Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&(connp)->conn_srcv6);			\
8540Sstevel@tonic-gate 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_WILDCARD: connfp %p "	\
8550Sstevel@tonic-gate 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
8560Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8570Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8580Sstevel@tonic-gate 	list = &(connfp)->connf_head;					\
8590Sstevel@tonic-gate 	prev = NULL;							\
8600Sstevel@tonic-gate 	while ((next = *list) != NULL) {				\
8610Sstevel@tonic-gate 		if (isv4mapped &&					\
8620Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&next->conn_srcv6) &&	\
8630Sstevel@tonic-gate 		    connp->conn_zoneid == next->conn_zoneid) {		\
8640Sstevel@tonic-gate 			(connp)->conn_next = next;			\
8650Sstevel@tonic-gate 			if (prev != NULL)				\
8660Sstevel@tonic-gate 				prev = next->conn_prev;			\
8670Sstevel@tonic-gate 			next->conn_prev = (connp);			\
8680Sstevel@tonic-gate 			break;						\
8690Sstevel@tonic-gate 		}							\
8700Sstevel@tonic-gate 		list = &next->conn_next;				\
8710Sstevel@tonic-gate 		prev = next;						\
8720Sstevel@tonic-gate 	}								\
8730Sstevel@tonic-gate 	(connp)->conn_prev = prev;					\
8740Sstevel@tonic-gate 	*list = (connp);						\
8750Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8760Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8770Sstevel@tonic-gate 	    IPCL_BOUND;							\
8780Sstevel@tonic-gate 	CONN_INC_REF((connp));						\
8790Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate void
8830Sstevel@tonic-gate ipcl_hash_insert_wildcard(connf_t *connfp, conn_t *connp)
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate void
8890Sstevel@tonic-gate ipcl_proto_insert(conn_t *connp, uint8_t protocol)
8900Sstevel@tonic-gate {
8910Sstevel@tonic-gate 	connf_t	*connfp;
8923448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	ASSERT(connp != NULL);
8951676Sjpk 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
8961676Sjpk 	    protocol == IPPROTO_ESP);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	connp->conn_ulp = protocol;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	/* Insert it in the protocol hash */
9013448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[protocol];
9020Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate void
9060Sstevel@tonic-gate ipcl_proto_insert_v6(conn_t *connp, uint8_t protocol)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	connf_t	*connfp;
9093448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	ASSERT(connp != NULL);
9121676Sjpk 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
9131676Sjpk 	    protocol == IPPROTO_ESP);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	connp->conn_ulp = protocol;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	/* Insert it in the Bind Hash */
9183448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
9190Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate  * This function is used only for inserting SCTP raw socket now.
9240Sstevel@tonic-gate  * This may change later.
9250Sstevel@tonic-gate  *
9260Sstevel@tonic-gate  * Note that only one raw socket can be bound to a port.  The param
9270Sstevel@tonic-gate  * lport is in network byte order.
9280Sstevel@tonic-gate  */
9290Sstevel@tonic-gate static int
9300Sstevel@tonic-gate ipcl_sctp_hash_insert(conn_t *connp, in_port_t lport)
9310Sstevel@tonic-gate {
9320Sstevel@tonic-gate 	connf_t	*connfp;
9330Sstevel@tonic-gate 	conn_t	*oconnp;
9343448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
9350Sstevel@tonic-gate 
9363448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/* Check for existing raw socket already bound to the port. */
9390Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
9400Sstevel@tonic-gate 	for (oconnp = connfp->connf_head; oconnp != NULL;
941409Skcpoon 	    oconnp = oconnp->conn_next) {
9420Sstevel@tonic-gate 		if (oconnp->conn_lport == lport &&
9430Sstevel@tonic-gate 		    oconnp->conn_zoneid == connp->conn_zoneid &&
9440Sstevel@tonic-gate 		    oconnp->conn_af_isv6 == connp->conn_af_isv6 &&
9450Sstevel@tonic-gate 		    ((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
9460Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&oconnp->conn_srcv6) ||
9470Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6) ||
9480Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&oconnp->conn_srcv6)) ||
9490Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&oconnp->conn_srcv6,
9500Sstevel@tonic-gate 		    &connp->conn_srcv6))) {
9510Sstevel@tonic-gate 			break;
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
9550Sstevel@tonic-gate 	if (oconnp != NULL)
9560Sstevel@tonic-gate 		return (EADDRNOTAVAIL);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6) ||
9590Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_remv6)) {
9600Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
9610Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6)) {
9620Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9630Sstevel@tonic-gate 		} else {
9640Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 	} else {
9670Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED(connfp, connp);
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 	return (0);
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate /*
9731676Sjpk  * Check for a MAC exemption conflict on a labeled system.  Note that for
9741676Sjpk  * protocols that use port numbers (UDP, TCP, SCTP), we do this check up in the
9751676Sjpk  * transport layer.  This check is for binding all other protocols.
9761676Sjpk  *
9771676Sjpk  * Returns true if there's a conflict.
9781676Sjpk  */
9791676Sjpk static boolean_t
9803448Sdh155122 check_exempt_conflict_v4(conn_t *connp, ip_stack_t *ipst)
9811676Sjpk {
9821676Sjpk 	connf_t	*connfp;
9831676Sjpk 	conn_t *tconn;
9841676Sjpk 
9853448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[connp->conn_ulp];
9861676Sjpk 	mutex_enter(&connfp->connf_lock);
9871676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
9881676Sjpk 	    tconn = tconn->conn_next) {
9891676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
9901676Sjpk 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
9911676Sjpk 			continue;
9921676Sjpk 		/* If neither is exempt, then there's no conflict */
9931676Sjpk 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
9941676Sjpk 			continue;
9951676Sjpk 		/* If both are bound to different specific addrs, ok */
9961676Sjpk 		if (connp->conn_src != INADDR_ANY &&
9971676Sjpk 		    tconn->conn_src != INADDR_ANY &&
9981676Sjpk 		    connp->conn_src != tconn->conn_src)
9991676Sjpk 			continue;
10001676Sjpk 		/* These two conflict; fail */
10011676Sjpk 		break;
10021676Sjpk 	}
10031676Sjpk 	mutex_exit(&connfp->connf_lock);
10041676Sjpk 	return (tconn != NULL);
10051676Sjpk }
10061676Sjpk 
10071676Sjpk static boolean_t
10083448Sdh155122 check_exempt_conflict_v6(conn_t *connp, ip_stack_t *ipst)
10091676Sjpk {
10101676Sjpk 	connf_t	*connfp;
10111676Sjpk 	conn_t *tconn;
10121676Sjpk 
10133448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[connp->conn_ulp];
10141676Sjpk 	mutex_enter(&connfp->connf_lock);
10151676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
10161676Sjpk 	    tconn = tconn->conn_next) {
10171676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
10181676Sjpk 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
10191676Sjpk 			continue;
10201676Sjpk 		/* If neither is exempt, then there's no conflict */
10211676Sjpk 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
10221676Sjpk 			continue;
10231676Sjpk 		/* If both are bound to different addrs, ok */
10241676Sjpk 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) &&
10251676Sjpk 		    !IN6_IS_ADDR_UNSPECIFIED(&tconn->conn_srcv6) &&
10261676Sjpk 		    !IN6_ARE_ADDR_EQUAL(&connp->conn_srcv6, &tconn->conn_srcv6))
10271676Sjpk 			continue;
10281676Sjpk 		/* These two conflict; fail */
10291676Sjpk 		break;
10301676Sjpk 	}
10311676Sjpk 	mutex_exit(&connfp->connf_lock);
10321676Sjpk 	return (tconn != NULL);
10331676Sjpk }
10341676Sjpk 
10351676Sjpk /*
10360Sstevel@tonic-gate  * (v4, v6) bind hash insertion routines
10370Sstevel@tonic-gate  */
10380Sstevel@tonic-gate int
10390Sstevel@tonic-gate ipcl_bind_insert(conn_t *connp, uint8_t protocol, ipaddr_t src, uint16_t lport)
10400Sstevel@tonic-gate {
10410Sstevel@tonic-gate 	connf_t	*connfp;
10420Sstevel@tonic-gate #ifdef	IPCL_DEBUG
10430Sstevel@tonic-gate 	char	buf[INET_NTOA_BUFSIZE];
10440Sstevel@tonic-gate #endif
10450Sstevel@tonic-gate 	int	ret = 0;
10463448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	ASSERT(connp);
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	IPCL_DEBUG_LVL(64, ("ipcl_bind_insert: connp %p, src = %s, "
10510Sstevel@tonic-gate 	    "port = %d\n", (void *)connp, inet_ntoa_r(src, buf), lport));
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	connp->conn_ulp = protocol;
10540Sstevel@tonic-gate 	IN6_IPADDR_TO_V4MAPPED(src, &connp->conn_srcv6);
10550Sstevel@tonic-gate 	connp->conn_lport = lport;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	switch (protocol) {
10581676Sjpk 	default:
10593448Sdh155122 		if (is_system_labeled() &&
10603448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
10611676Sjpk 			return (EADDRINUSE);
10621676Sjpk 		/* FALLTHROUGH */
10630Sstevel@tonic-gate 	case IPPROTO_UDP:
10640Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
10650Sstevel@tonic-gate 			IPCL_DEBUG_LVL(64,
10660Sstevel@tonic-gate 			    ("ipcl_bind_insert: connp %p - udp\n",
10670Sstevel@tonic-gate 			    (void *)connp));
10683448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
10693448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
10700Sstevel@tonic-gate 		} else {
10710Sstevel@tonic-gate 			IPCL_DEBUG_LVL(64,
10720Sstevel@tonic-gate 			    ("ipcl_bind_insert: connp %p - protocol\n",
10730Sstevel@tonic-gate 			    (void *)connp));
10743448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout[protocol];
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 		if (connp->conn_rem != INADDR_ANY) {
10780Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
10790Sstevel@tonic-gate 		} else if (connp->conn_src != INADDR_ANY) {
10800Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
10810Sstevel@tonic-gate 		} else {
10820Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 		break;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	case IPPROTO_TCP:
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
10891676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
10903448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
10913448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
10920Sstevel@tonic-gate 		if (connp->conn_src != INADDR_ANY) {
10930Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
10940Sstevel@tonic-gate 		} else {
10950Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10960Sstevel@tonic-gate 		}
10970Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
10980Sstevel@tonic-gate 			ASSERT(!connp->conn_pkt_isv6);
10990Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
11000Sstevel@tonic-gate 			(*cl_inet_listen)(IPPROTO_TCP, AF_INET,
11010Sstevel@tonic-gate 			    (uint8_t *)&connp->conn_bound_source, lport);
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 		break;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	case IPPROTO_SCTP:
11060Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
11070Sstevel@tonic-gate 		break;
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	return (ret);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate int
11140Sstevel@tonic-gate ipcl_bind_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
11150Sstevel@tonic-gate     uint16_t lport)
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate 	connf_t	*connfp;
11180Sstevel@tonic-gate 	int	ret = 0;
11193448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	ASSERT(connp);
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	connp->conn_ulp = protocol;
11240Sstevel@tonic-gate 	connp->conn_srcv6 = *src;
11250Sstevel@tonic-gate 	connp->conn_lport = lport;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	switch (protocol) {
11281676Sjpk 	default:
11293448Sdh155122 		if (is_system_labeled() &&
11303448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
11311676Sjpk 			return (EADDRINUSE);
11321676Sjpk 		/* FALLTHROUGH */
11330Sstevel@tonic-gate 	case IPPROTO_UDP:
11340Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
11350Sstevel@tonic-gate 			IPCL_DEBUG_LVL(128,
11360Sstevel@tonic-gate 			    ("ipcl_bind_insert_v6: connp %p - udp\n",
11370Sstevel@tonic-gate 			    (void *)connp));
11383448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
11393448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
11400Sstevel@tonic-gate 		} else {
11410Sstevel@tonic-gate 			IPCL_DEBUG_LVL(128,
11420Sstevel@tonic-gate 			    ("ipcl_bind_insert_v6: connp %p - protocol\n",
11430Sstevel@tonic-gate 			    (void *)connp));
11443448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
11480Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
11490Sstevel@tonic-gate 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
11500Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
11510Sstevel@tonic-gate 		} else {
11520Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
11530Sstevel@tonic-gate 		}
11540Sstevel@tonic-gate 		break;
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	case IPPROTO_TCP:
11570Sstevel@tonic-gate 		/* XXX - Need a separate table for IN6_IS_ADDR_UNSPECIFIED? */
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
11601676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
11613448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
11623448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
11630Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
11640Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
11650Sstevel@tonic-gate 		} else {
11660Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
11670Sstevel@tonic-gate 		}
11680Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
11690Sstevel@tonic-gate 			sa_family_t	addr_family;
11700Sstevel@tonic-gate 			uint8_t		*laddrp;
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 			if (connp->conn_pkt_isv6) {
11730Sstevel@tonic-gate 				addr_family = AF_INET6;
11740Sstevel@tonic-gate 				laddrp =
11750Sstevel@tonic-gate 				    (uint8_t *)&connp->conn_bound_source_v6;
11760Sstevel@tonic-gate 			} else {
11770Sstevel@tonic-gate 				addr_family = AF_INET;
11780Sstevel@tonic-gate 				laddrp = (uint8_t *)&connp->conn_bound_source;
11790Sstevel@tonic-gate 			}
11800Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
11810Sstevel@tonic-gate 			(*cl_inet_listen)(IPPROTO_TCP, addr_family, laddrp,
11820Sstevel@tonic-gate 			    lport);
11830Sstevel@tonic-gate 		}
11840Sstevel@tonic-gate 		break;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	case IPPROTO_SCTP:
11870Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
11880Sstevel@tonic-gate 		break;
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	return (ret);
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate /*
11950Sstevel@tonic-gate  * ipcl_conn_hash insertion routines.
11960Sstevel@tonic-gate  */
11970Sstevel@tonic-gate int
11980Sstevel@tonic-gate ipcl_conn_insert(conn_t *connp, uint8_t protocol, ipaddr_t src,
11990Sstevel@tonic-gate     ipaddr_t rem, uint32_t ports)
12000Sstevel@tonic-gate {
12010Sstevel@tonic-gate 	connf_t		*connfp;
12020Sstevel@tonic-gate 	uint16_t	*up;
12030Sstevel@tonic-gate 	conn_t		*tconnp;
12040Sstevel@tonic-gate #ifdef	IPCL_DEBUG
12050Sstevel@tonic-gate 	char	sbuf[INET_NTOA_BUFSIZE], rbuf[INET_NTOA_BUFSIZE];
12060Sstevel@tonic-gate #endif
12070Sstevel@tonic-gate 	in_port_t	lport;
12080Sstevel@tonic-gate 	int		ret = 0;
12093448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	IPCL_DEBUG_LVL(256, ("ipcl_conn_insert: connp %p, src = %s, "
12120Sstevel@tonic-gate 	    "dst = %s, ports = %x, protocol = %x", (void *)connp,
12130Sstevel@tonic-gate 	    inet_ntoa_r(src, sbuf), inet_ntoa_r(rem, rbuf),
12140Sstevel@tonic-gate 	    ports, protocol));
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	switch (protocol) {
12170Sstevel@tonic-gate 	case IPPROTO_TCP:
12180Sstevel@tonic-gate 		if (!(connp->conn_flags & IPCL_EAGER)) {
12190Sstevel@tonic-gate 			/*
12200Sstevel@tonic-gate 			 * for a eager connection, i.e connections which
12210Sstevel@tonic-gate 			 * have just been created, the initialization is
12220Sstevel@tonic-gate 			 * already done in ip at conn_creation time, so
12230Sstevel@tonic-gate 			 * we can skip the checks here.
12240Sstevel@tonic-gate 			 */
12250Sstevel@tonic-gate 			IPCL_CONN_INIT(connp, protocol, src, rem, ports);
12260Sstevel@tonic-gate 		}
12273448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
12283448Sdh155122 		    IPCL_CONN_HASH(connp->conn_rem,
12293448Sdh155122 		    connp->conn_ports, ipst)];
12300Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
12310Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
12320Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
12330Sstevel@tonic-gate 			if (IPCL_CONN_MATCH(tconnp, connp->conn_ulp,
12340Sstevel@tonic-gate 			    connp->conn_rem, connp->conn_src,
12350Sstevel@tonic-gate 			    connp->conn_ports)) {
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 				/* Already have a conn. bail out */
12380Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
12390Sstevel@tonic-gate 				return (EADDRINUSE);
12400Sstevel@tonic-gate 			}
12410Sstevel@tonic-gate 		}
12420Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
12430Sstevel@tonic-gate 			/*
12440Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
12450Sstevel@tonic-gate 			 * rebind. Let it happen.
12460Sstevel@tonic-gate 			 */
12470Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
12480Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
12490Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
12500Sstevel@tonic-gate 		}
12513104Sjprakash 
12523104Sjprakash 		ASSERT(connp->conn_recv != NULL);
12533104Sjprakash 
12540Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
12550Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
12560Sstevel@tonic-gate 		break;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	case IPPROTO_SCTP:
1259409Skcpoon 		/*
1260409Skcpoon 		 * The raw socket may have already been bound, remove it
1261409Skcpoon 		 * from the hash first.
1262409Skcpoon 		 */
1263409Skcpoon 		IPCL_HASH_REMOVE(connp);
1264409Skcpoon 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
12650Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
12660Sstevel@tonic-gate 		break;
12670Sstevel@tonic-gate 
12681676Sjpk 	default:
12691676Sjpk 		/*
12701676Sjpk 		 * Check for conflicts among MAC exempt bindings.  For
12711676Sjpk 		 * transports with port numbers, this is done by the upper
12721676Sjpk 		 * level per-transport binding logic.  For all others, it's
12731676Sjpk 		 * done here.
12741676Sjpk 		 */
12753448Sdh155122 		if (is_system_labeled() &&
12763448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
12771676Sjpk 			return (EADDRINUSE);
12781676Sjpk 		/* FALLTHROUGH */
12791676Sjpk 
12800Sstevel@tonic-gate 	case IPPROTO_UDP:
12810Sstevel@tonic-gate 		up = (uint16_t *)&ports;
12820Sstevel@tonic-gate 		IPCL_CONN_INIT(connp, protocol, src, rem, ports);
12830Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
12843448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
12853448Sdh155122 			    IPCL_UDP_HASH(up[1], ipst)];
12860Sstevel@tonic-gate 		} else {
12873448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout[protocol];
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 		if (connp->conn_rem != INADDR_ANY) {
12910Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
12920Sstevel@tonic-gate 		} else if (connp->conn_src != INADDR_ANY) {
12930Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12940Sstevel@tonic-gate 		} else {
12950Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12960Sstevel@tonic-gate 		}
12970Sstevel@tonic-gate 		break;
12980Sstevel@tonic-gate 	}
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	return (ret);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate int
13040Sstevel@tonic-gate ipcl_conn_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
13050Sstevel@tonic-gate     const in6_addr_t *rem, uint32_t ports, uint_t ifindex)
13060Sstevel@tonic-gate {
13070Sstevel@tonic-gate 	connf_t		*connfp;
13080Sstevel@tonic-gate 	uint16_t	*up;
13090Sstevel@tonic-gate 	conn_t		*tconnp;
13100Sstevel@tonic-gate 	in_port_t	lport;
13110Sstevel@tonic-gate 	int		ret = 0;
13123448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	switch (protocol) {
13150Sstevel@tonic-gate 	case IPPROTO_TCP:
13160Sstevel@tonic-gate 		/* Just need to insert a conn struct */
13170Sstevel@tonic-gate 		if (!(connp->conn_flags & IPCL_EAGER)) {
13180Sstevel@tonic-gate 			IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
13190Sstevel@tonic-gate 		}
13203448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
13213448Sdh155122 		    IPCL_CONN_HASH_V6(connp->conn_remv6, connp->conn_ports,
13223448Sdh155122 		    ipst)];
13230Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
13240Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
13250Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
13260Sstevel@tonic-gate 			if (IPCL_CONN_MATCH_V6(tconnp, connp->conn_ulp,
13270Sstevel@tonic-gate 			    connp->conn_remv6, connp->conn_srcv6,
13280Sstevel@tonic-gate 			    connp->conn_ports) &&
13290Sstevel@tonic-gate 			    (tconnp->conn_tcp->tcp_bound_if == 0 ||
13300Sstevel@tonic-gate 			    tconnp->conn_tcp->tcp_bound_if == ifindex)) {
13310Sstevel@tonic-gate 				/* Already have a conn. bail out */
13320Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
13330Sstevel@tonic-gate 				return (EADDRINUSE);
13340Sstevel@tonic-gate 			}
13350Sstevel@tonic-gate 		}
13360Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
13370Sstevel@tonic-gate 			/*
13380Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
13390Sstevel@tonic-gate 			 * rebind. Let it happen.
13400Sstevel@tonic-gate 			 */
13410Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
13420Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
13430Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
13440Sstevel@tonic-gate 		}
13450Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
13460Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
13470Sstevel@tonic-gate 		break;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	case IPPROTO_SCTP:
1350409Skcpoon 		IPCL_HASH_REMOVE(connp);
1351409Skcpoon 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
13520Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
13530Sstevel@tonic-gate 		break;
13540Sstevel@tonic-gate 
13551676Sjpk 	default:
13563448Sdh155122 		if (is_system_labeled() &&
13573448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
13581676Sjpk 			return (EADDRINUSE);
13591676Sjpk 		/* FALLTHROUGH */
13600Sstevel@tonic-gate 	case IPPROTO_UDP:
13610Sstevel@tonic-gate 		up = (uint16_t *)&ports;
13620Sstevel@tonic-gate 		IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
13630Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
13643448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
13653448Sdh155122 			    IPCL_UDP_HASH(up[1], ipst)];
13660Sstevel@tonic-gate 		} else {
13673448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
13680Sstevel@tonic-gate 		}
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
13710Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
13720Sstevel@tonic-gate 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
13730Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
13740Sstevel@tonic-gate 		} else {
13750Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 		break;
13780Sstevel@tonic-gate 	}
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	return (ret);
13810Sstevel@tonic-gate }
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate /*
13840Sstevel@tonic-gate  * v4 packet classifying function. looks up the fanout table to
13850Sstevel@tonic-gate  * find the conn, the packet belongs to. returns the conn with
13860Sstevel@tonic-gate  * the reference held, null otherwise.
13871676Sjpk  *
13881676Sjpk  * If zoneid is ALL_ZONES, then the search rules described in the "Connection
13891676Sjpk  * Lookup" comment block are applied.  Labels are also checked as described
13901676Sjpk  * above.  If the packet is from the inside (looped back), and is from the same
13911676Sjpk  * zone, then label checks are omitted.
13920Sstevel@tonic-gate  */
13930Sstevel@tonic-gate conn_t *
13943448Sdh155122 ipcl_classify_v4(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid,
13953448Sdh155122     ip_stack_t *ipst)
13960Sstevel@tonic-gate {
13970Sstevel@tonic-gate 	ipha_t	*ipha;
13980Sstevel@tonic-gate 	connf_t	*connfp, *bind_connfp;
13990Sstevel@tonic-gate 	uint16_t lport;
14000Sstevel@tonic-gate 	uint16_t fport;
14010Sstevel@tonic-gate 	uint32_t ports;
14020Sstevel@tonic-gate 	conn_t	*connp;
14030Sstevel@tonic-gate 	uint16_t  *up;
14041676Sjpk 	boolean_t shared_addr;
14051676Sjpk 	boolean_t unlabeled;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
14080Sstevel@tonic-gate 	up = (uint16_t *)((uchar_t *)ipha + hdr_len + TCP_PORTS_OFFSET);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	switch (protocol) {
14110Sstevel@tonic-gate 	case IPPROTO_TCP:
14120Sstevel@tonic-gate 		ports = *(uint32_t *)up;
14130Sstevel@tonic-gate 		connfp =
14143448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_src,
14153448Sdh155122 		    ports, ipst)];
14160Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
14170Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
14180Sstevel@tonic-gate 		    connp = connp->conn_next) {
14190Sstevel@tonic-gate 			if (IPCL_CONN_MATCH(connp, protocol,
14200Sstevel@tonic-gate 			    ipha->ipha_src, ipha->ipha_dst, ports))
14210Sstevel@tonic-gate 				break;
14220Sstevel@tonic-gate 		}
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 		if (connp != NULL) {
14251676Sjpk 			/*
14261676Sjpk 			 * We have a fully-bound TCP connection.
14271676Sjpk 			 *
14281676Sjpk 			 * For labeled systems, there's no need to check the
14291676Sjpk 			 * label here.  It's known to be good as we checked
14301676Sjpk 			 * before allowing the connection to become bound.
14311676Sjpk 			 */
14320Sstevel@tonic-gate 			CONN_INC_REF(connp);
14330Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
14340Sstevel@tonic-gate 			return (connp);
14350Sstevel@tonic-gate 		}
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 		lport = up[1];
14401676Sjpk 		unlabeled = B_FALSE;
14411676Sjpk 		/* Cred cannot be null on IPv4 */
14421676Sjpk 		if (is_system_labeled())
14431676Sjpk 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
14441676Sjpk 			    TSLF_UNLABELED) != 0;
14451676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
14461676Sjpk 		if (shared_addr) {
14473448Sdh155122 			/*
14483448Sdh155122 			 * No need to handle exclusive-stack zones since
14493448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
14503448Sdh155122 			 */
14511676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
14521676Sjpk 			/*
14531676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
14541676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
14551676Sjpk 			 * search for the zone based on the packet label.
14561676Sjpk 			 *
14571676Sjpk 			 * If there is such a zone, we prefer to find a
14581676Sjpk 			 * connection in it.  Otherwise, we look for a
14591676Sjpk 			 * MAC-exempt connection in any zone whose label
14601676Sjpk 			 * dominates the default label on the packet.
14611676Sjpk 			 */
14621676Sjpk 			if (zoneid == ALL_ZONES)
14631676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
14641676Sjpk 			else
14651676Sjpk 				unlabeled = B_FALSE;
14661676Sjpk 		}
14671676Sjpk 
14683448Sdh155122 		bind_connfp =
14693448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
14700Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
14710Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
14720Sstevel@tonic-gate 		    connp = connp->conn_next) {
14731676Sjpk 			if (IPCL_BIND_MATCH(connp, protocol, ipha->ipha_dst,
14742263Ssommerfe 			    lport) && (IPCL_ZONE_MATCH(connp, zoneid) ||
14751676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
14760Sstevel@tonic-gate 				break;
14770Sstevel@tonic-gate 		}
14780Sstevel@tonic-gate 
14791676Sjpk 		/*
14801676Sjpk 		 * If the matching connection is SLP on a private address, then
14811676Sjpk 		 * the label on the packet must match the local zone's label.
14821676Sjpk 		 * Otherwise, it must be in the label range defined by tnrh.
14831676Sjpk 		 * This is ensured by tsol_receive_label.
14841676Sjpk 		 */
14851676Sjpk 		if (connp != NULL && is_system_labeled() &&
14861676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
14871676Sjpk 		    shared_addr, connp)) {
14881676Sjpk 				DTRACE_PROBE3(
14891676Sjpk 				    tx__ip__log__info__classify__tcp,
14901676Sjpk 				    char *,
14911676Sjpk 				    "connp(1) could not receive mp(2)",
14921676Sjpk 				    conn_t *, connp, mblk_t *, mp);
14931676Sjpk 			connp = NULL;
14941676Sjpk 		}
14951676Sjpk 
14960Sstevel@tonic-gate 		if (connp != NULL) {
14971676Sjpk 			/* Have a listener at least */
14980Sstevel@tonic-gate 			CONN_INC_REF(connp);
14990Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
15000Sstevel@tonic-gate 			return (connp);
15010Sstevel@tonic-gate 		}
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
15060Sstevel@tonic-gate 		    ("ipcl_classify: couldn't classify mp = %p\n",
15070Sstevel@tonic-gate 		    (void *)mp));
15080Sstevel@tonic-gate 		break;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	case IPPROTO_UDP:
15110Sstevel@tonic-gate 		lport = up[1];
15121676Sjpk 		unlabeled = B_FALSE;
15131676Sjpk 		/* Cred cannot be null on IPv4 */
15141676Sjpk 		if (is_system_labeled())
15151676Sjpk 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
15161676Sjpk 			    TSLF_UNLABELED) != 0;
15171676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
15181676Sjpk 		if (shared_addr) {
15193448Sdh155122 			/*
15203448Sdh155122 			 * No need to handle exclusive-stack zones since
15213448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
15223448Sdh155122 			 */
15231676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
15241676Sjpk 			/*
15251676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
15261676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
15271676Sjpk 			 * search for the zone based on the packet label.
15281676Sjpk 			 *
15291676Sjpk 			 * If there is such a zone, we prefer to find a
15301676Sjpk 			 * connection in it.  Otherwise, we look for a
15311676Sjpk 			 * MAC-exempt connection in any zone whose label
15321676Sjpk 			 * dominates the default label on the packet.
15331676Sjpk 			 */
15341676Sjpk 			if (zoneid == ALL_ZONES)
15351676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
15361676Sjpk 			else
15371676Sjpk 				unlabeled = B_FALSE;
15381676Sjpk 		}
15390Sstevel@tonic-gate 		fport = up[0];
15400Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify %x %x", lport, fport));
15413448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
15420Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
15430Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
15440Sstevel@tonic-gate 		    connp = connp->conn_next) {
15450Sstevel@tonic-gate 			if (IPCL_UDP_MATCH(connp, lport, ipha->ipha_dst,
15460Sstevel@tonic-gate 			    fport, ipha->ipha_src) &&
15472263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
15481676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
15490Sstevel@tonic-gate 				break;
15500Sstevel@tonic-gate 		}
15510Sstevel@tonic-gate 
15521676Sjpk 		if (connp != NULL && is_system_labeled() &&
15531676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
15541676Sjpk 		    shared_addr, connp)) {
15551676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp,
15561676Sjpk 			    char *, "connp(1) could not receive mp(2)",
15571676Sjpk 			    conn_t *, connp, mblk_t *, mp);
15581676Sjpk 			connp = NULL;
15591676Sjpk 		}
15601676Sjpk 
15610Sstevel@tonic-gate 		if (connp != NULL) {
15620Sstevel@tonic-gate 			CONN_INC_REF(connp);
15630Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
15640Sstevel@tonic-gate 			return (connp);
15650Sstevel@tonic-gate 		}
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 		/*
15680Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
15690Sstevel@tonic-gate 		 */
15700Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
15710Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
15720Sstevel@tonic-gate 		    ("ipcl_classify: cant find udp conn_t for ports : %x %x",
15730Sstevel@tonic-gate 		    lport, fport));
15740Sstevel@tonic-gate 		break;
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	return (NULL);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate conn_t *
15813448Sdh155122 ipcl_classify_v6(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid,
15823448Sdh155122     ip_stack_t *ipst)
15830Sstevel@tonic-gate {
15840Sstevel@tonic-gate 	ip6_t		*ip6h;
15850Sstevel@tonic-gate 	connf_t		*connfp, *bind_connfp;
15860Sstevel@tonic-gate 	uint16_t	lport;
15870Sstevel@tonic-gate 	uint16_t	fport;
15880Sstevel@tonic-gate 	tcph_t		*tcph;
15890Sstevel@tonic-gate 	uint32_t	ports;
15900Sstevel@tonic-gate 	conn_t		*connp;
15910Sstevel@tonic-gate 	uint16_t	*up;
15921676Sjpk 	boolean_t	shared_addr;
15931676Sjpk 	boolean_t	unlabeled;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	switch (protocol) {
15980Sstevel@tonic-gate 	case IPPROTO_TCP:
15990Sstevel@tonic-gate 		tcph = (tcph_t *)&mp->b_rptr[hdr_len];
16000Sstevel@tonic-gate 		up = (uint16_t *)tcph->th_lport;
16010Sstevel@tonic-gate 		ports = *(uint32_t *)up;
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 		connfp =
16043448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_src,
16053448Sdh155122 		    ports, ipst)];
16060Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16070Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16080Sstevel@tonic-gate 		    connp = connp->conn_next) {
16090Sstevel@tonic-gate 			if (IPCL_CONN_MATCH_V6(connp, protocol,
16100Sstevel@tonic-gate 			    ip6h->ip6_src, ip6h->ip6_dst, ports))
16110Sstevel@tonic-gate 				break;
16120Sstevel@tonic-gate 		}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 		if (connp != NULL) {
16151676Sjpk 			/*
16161676Sjpk 			 * We have a fully-bound TCP connection.
16171676Sjpk 			 *
16181676Sjpk 			 * For labeled systems, there's no need to check the
16191676Sjpk 			 * label here.  It's known to be good as we checked
16201676Sjpk 			 * before allowing the connection to become bound.
16211676Sjpk 			 */
16220Sstevel@tonic-gate 			CONN_INC_REF(connp);
16230Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
16240Sstevel@tonic-gate 			return (connp);
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 		lport = up[1];
16301676Sjpk 		unlabeled = B_FALSE;
16311676Sjpk 		/* Cred can be null on IPv6 */
16321676Sjpk 		if (is_system_labeled()) {
16331676Sjpk 			cred_t *cr = DB_CRED(mp);
16341676Sjpk 
16351676Sjpk 			unlabeled = (cr != NULL &&
16361676Sjpk 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
16371676Sjpk 		}
16381676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
16391676Sjpk 		if (shared_addr) {
16403448Sdh155122 			/*
16413448Sdh155122 			 * No need to handle exclusive-stack zones since
16423448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
16433448Sdh155122 			 */
16441676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
16451676Sjpk 			/*
16461676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
16471676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
16481676Sjpk 			 * search for the zone based on the packet label.
16491676Sjpk 			 *
16501676Sjpk 			 * If there is such a zone, we prefer to find a
16511676Sjpk 			 * connection in it.  Otherwise, we look for a
16521676Sjpk 			 * MAC-exempt connection in any zone whose label
16531676Sjpk 			 * dominates the default label on the packet.
16541676Sjpk 			 */
16551676Sjpk 			if (zoneid == ALL_ZONES)
16561676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
16571676Sjpk 			else
16581676Sjpk 				unlabeled = B_FALSE;
16591676Sjpk 		}
16601676Sjpk 
16613448Sdh155122 		bind_connfp =
16623448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
16630Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
16640Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
16650Sstevel@tonic-gate 		    connp = connp->conn_next) {
16660Sstevel@tonic-gate 			if (IPCL_BIND_MATCH_V6(connp, protocol,
16670Sstevel@tonic-gate 			    ip6h->ip6_dst, lport) &&
16682263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
16691676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
16700Sstevel@tonic-gate 				break;
16710Sstevel@tonic-gate 		}
16720Sstevel@tonic-gate 
16731676Sjpk 		if (connp != NULL && is_system_labeled() &&
16741676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
16751676Sjpk 		    shared_addr, connp)) {
16761676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp6,
16771676Sjpk 			    char *, "connp(1) could not receive mp(2)",
16781676Sjpk 			    conn_t *, connp, mblk_t *, mp);
16791676Sjpk 			connp = NULL;
16801676Sjpk 		}
16811676Sjpk 
16820Sstevel@tonic-gate 		if (connp != NULL) {
16830Sstevel@tonic-gate 			/* Have a listner at least */
16840Sstevel@tonic-gate 			CONN_INC_REF(connp);
16850Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
16860Sstevel@tonic-gate 			IPCL_DEBUG_LVL(512,
16870Sstevel@tonic-gate 			    ("ipcl_classify_v6: found listner "
16880Sstevel@tonic-gate 			    "connp = %p\n", (void *)connp));
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 			return (connp);
16910Sstevel@tonic-gate 		}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
16960Sstevel@tonic-gate 		    ("ipcl_classify_v6: couldn't classify mp = %p\n",
16970Sstevel@tonic-gate 		    (void *)mp));
16980Sstevel@tonic-gate 		break;
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	case IPPROTO_UDP:
17010Sstevel@tonic-gate 		up = (uint16_t *)&mp->b_rptr[hdr_len];
17020Sstevel@tonic-gate 		lport = up[1];
17031676Sjpk 		unlabeled = B_FALSE;
17041676Sjpk 		/* Cred can be null on IPv6 */
17051676Sjpk 		if (is_system_labeled()) {
17061676Sjpk 			cred_t *cr = DB_CRED(mp);
17071676Sjpk 
17081676Sjpk 			unlabeled = (cr != NULL &&
17091676Sjpk 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
17101676Sjpk 		}
17111676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
17121676Sjpk 		if (shared_addr) {
17133448Sdh155122 			/*
17143448Sdh155122 			 * No need to handle exclusive-stack zones since
17153448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
17163448Sdh155122 			 */
17171676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
17181676Sjpk 			/*
17191676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
17201676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
17211676Sjpk 			 * search for the zone based on the packet label.
17221676Sjpk 			 *
17231676Sjpk 			 * If there is such a zone, we prefer to find a
17241676Sjpk 			 * connection in it.  Otherwise, we look for a
17251676Sjpk 			 * MAC-exempt connection in any zone whose label
17261676Sjpk 			 * dominates the default label on the packet.
17271676Sjpk 			 */
17281676Sjpk 			if (zoneid == ALL_ZONES)
17291676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
17301676Sjpk 			else
17311676Sjpk 				unlabeled = B_FALSE;
17321676Sjpk 		}
17331676Sjpk 
17340Sstevel@tonic-gate 		fport = up[0];
17350Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify_v6 %x %x", lport,
17360Sstevel@tonic-gate 		    fport));
17373448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
17380Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
17390Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
17400Sstevel@tonic-gate 		    connp = connp->conn_next) {
17410Sstevel@tonic-gate 			if (IPCL_UDP_MATCH_V6(connp, lport, ip6h->ip6_dst,
17420Sstevel@tonic-gate 			    fport, ip6h->ip6_src) &&
17432263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
17441676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
17450Sstevel@tonic-gate 				break;
17460Sstevel@tonic-gate 		}
17470Sstevel@tonic-gate 
17481676Sjpk 		if (connp != NULL && is_system_labeled() &&
17491676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
17501676Sjpk 		    shared_addr, connp)) {
17511676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp6,
17521676Sjpk 			    char *, "connp(1) could not receive mp(2)",
17531676Sjpk 			    conn_t *, connp, mblk_t *, mp);
17541676Sjpk 			connp = NULL;
17551676Sjpk 		}
17561676Sjpk 
17570Sstevel@tonic-gate 		if (connp != NULL) {
17580Sstevel@tonic-gate 			CONN_INC_REF(connp);
17590Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
17600Sstevel@tonic-gate 			return (connp);
17610Sstevel@tonic-gate 		}
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 		/*
17640Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
17650Sstevel@tonic-gate 		 */
17660Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
17670Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
17680Sstevel@tonic-gate 		    ("ipcl_classify_v6: cant find udp conn_t for ports : %x %x",
17690Sstevel@tonic-gate 		    lport, fport));
17700Sstevel@tonic-gate 		break;
17710Sstevel@tonic-gate 	}
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	return (NULL);
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate /*
17770Sstevel@tonic-gate  * wrapper around ipcl_classify_(v4,v6) routines.
17780Sstevel@tonic-gate  */
17790Sstevel@tonic-gate conn_t *
17803448Sdh155122 ipcl_classify(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst)
17810Sstevel@tonic-gate {
17820Sstevel@tonic-gate 	uint16_t	hdr_len;
17830Sstevel@tonic-gate 	ipha_t		*ipha;
17840Sstevel@tonic-gate 	uint8_t		*nexthdrp;
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (ipha_t))
17870Sstevel@tonic-gate 		return (NULL);
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	switch (IPH_HDR_VERSION(mp->b_rptr)) {
17900Sstevel@tonic-gate 	case IPV4_VERSION:
17910Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
17920Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
17930Sstevel@tonic-gate 		return (ipcl_classify_v4(mp, ipha->ipha_protocol, hdr_len,
17943448Sdh155122 		    zoneid, ipst));
17950Sstevel@tonic-gate 	case IPV6_VERSION:
17960Sstevel@tonic-gate 		if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr,
17970Sstevel@tonic-gate 		    &hdr_len, &nexthdrp))
17980Sstevel@tonic-gate 			return (NULL);
17990Sstevel@tonic-gate 
18003448Sdh155122 		return (ipcl_classify_v6(mp, *nexthdrp, hdr_len, zoneid, ipst));
18010Sstevel@tonic-gate 	}
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	return (NULL);
18040Sstevel@tonic-gate }
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate conn_t *
18071676Sjpk ipcl_classify_raw(mblk_t *mp, uint8_t protocol, zoneid_t zoneid,
18083448Sdh155122     uint32_t ports, ipha_t *hdr, ip_stack_t *ipst)
18090Sstevel@tonic-gate {
18101676Sjpk 	connf_t		*connfp;
18110Sstevel@tonic-gate 	conn_t		*connp;
18120Sstevel@tonic-gate 	in_port_t	lport;
18130Sstevel@tonic-gate 	int		af;
18141676Sjpk 	boolean_t	shared_addr;
18151676Sjpk 	boolean_t	unlabeled;
18161676Sjpk 	const void	*dst;
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	lport = ((uint16_t *)&ports)[1];
18191676Sjpk 
18201676Sjpk 	unlabeled = B_FALSE;
18211676Sjpk 	/* Cred can be null on IPv6 */
18221676Sjpk 	if (is_system_labeled()) {
18231676Sjpk 		cred_t *cr = DB_CRED(mp);
18241676Sjpk 
18251676Sjpk 		unlabeled = (cr != NULL &&
18261676Sjpk 		    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
18271676Sjpk 	}
18281676Sjpk 	shared_addr = (zoneid == ALL_ZONES);
18291676Sjpk 	if (shared_addr) {
18303448Sdh155122 		/*
18313448Sdh155122 		 * No need to handle exclusive-stack zones since ALL_ZONES
18323448Sdh155122 		 * only applies to the shared stack.
18333448Sdh155122 		 */
18341676Sjpk 		zoneid = tsol_mlp_findzone(protocol, lport);
18351676Sjpk 		/*
18361676Sjpk 		 * If no shared MLP is found, tsol_mlp_findzone returns
18371676Sjpk 		 * ALL_ZONES.  In that case, we assume it's SLP, and search for
18381676Sjpk 		 * the zone based on the packet label.
18391676Sjpk 		 *
18401676Sjpk 		 * If there is such a zone, we prefer to find a connection in
18411676Sjpk 		 * it.  Otherwise, we look for a MAC-exempt connection in any
18421676Sjpk 		 * zone whose label dominates the default label on the packet.
18431676Sjpk 		 */
18441676Sjpk 		if (zoneid == ALL_ZONES)
18451676Sjpk 			zoneid = tsol_packet_to_zoneid(mp);
18461676Sjpk 		else
18471676Sjpk 			unlabeled = B_FALSE;
18481676Sjpk 	}
18491676Sjpk 
18500Sstevel@tonic-gate 	af = IPH_HDR_VERSION(hdr);
18511676Sjpk 	dst = af == IPV4_VERSION ? (const void *)&hdr->ipha_dst :
18521676Sjpk 	    (const void *)&((ip6_t *)hdr)->ip6_dst;
18533448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
18560Sstevel@tonic-gate 	for (connp = connfp->connf_head; connp != NULL;
18570Sstevel@tonic-gate 	    connp = connp->conn_next) {
18580Sstevel@tonic-gate 		/* We don't allow v4 fallback for v6 raw socket. */
18591676Sjpk 		if (af == (connp->conn_af_isv6 ? IPV4_VERSION :
18601676Sjpk 		    IPV6_VERSION))
18610Sstevel@tonic-gate 			continue;
18620Sstevel@tonic-gate 		if (connp->conn_fully_bound) {
18630Sstevel@tonic-gate 			if (af == IPV4_VERSION) {
18641676Sjpk 				if (!IPCL_CONN_MATCH(connp, protocol,
18651676Sjpk 				    hdr->ipha_src, hdr->ipha_dst, ports))
18661676Sjpk 					continue;
18670Sstevel@tonic-gate 			} else {
18681676Sjpk 				if (!IPCL_CONN_MATCH_V6(connp, protocol,
18690Sstevel@tonic-gate 				    ((ip6_t *)hdr)->ip6_src,
18701676Sjpk 				    ((ip6_t *)hdr)->ip6_dst, ports))
18711676Sjpk 					continue;
18720Sstevel@tonic-gate 			}
18730Sstevel@tonic-gate 		} else {
18740Sstevel@tonic-gate 			if (af == IPV4_VERSION) {
18751676Sjpk 				if (!IPCL_BIND_MATCH(connp, protocol,
18761676Sjpk 				    hdr->ipha_dst, lport))
18771676Sjpk 					continue;
18780Sstevel@tonic-gate 			} else {
18791676Sjpk 				if (!IPCL_BIND_MATCH_V6(connp, protocol,
18801676Sjpk 				    ((ip6_t *)hdr)->ip6_dst, lport))
18811676Sjpk 					continue;
18820Sstevel@tonic-gate 			}
18830Sstevel@tonic-gate 		}
18841676Sjpk 
18852263Ssommerfe 		if (IPCL_ZONE_MATCH(connp, zoneid) ||
18861676Sjpk 		    (unlabeled && connp->conn_mac_exempt))
18871676Sjpk 			break;
18881676Sjpk 	}
18891676Sjpk 	/*
18901676Sjpk 	 * If the connection is fully-bound and connection-oriented (TCP or
18911676Sjpk 	 * SCTP), then we've already validated the remote system's label.
18921676Sjpk 	 * There's no need to do it again for every packet.
18931676Sjpk 	 */
18941676Sjpk 	if (connp != NULL && is_system_labeled() && (!connp->conn_fully_bound ||
18951676Sjpk 	    !(connp->conn_flags & (IPCL_TCP|IPCL_SCTPCONN))) &&
18961676Sjpk 	    !tsol_receive_local(mp, dst, af, shared_addr, connp)) {
18971676Sjpk 		DTRACE_PROBE3(tx__ip__log__info__classify__rawip,
18981676Sjpk 		    char *, "connp(1) could not receive mp(2)",
18991676Sjpk 		    conn_t *, connp, mblk_t *, mp);
19001676Sjpk 		connp = NULL;
19010Sstevel@tonic-gate 	}
1902409Skcpoon 
1903409Skcpoon 	if (connp != NULL)
1904409Skcpoon 		goto found;
1905409Skcpoon 	mutex_exit(&connfp->connf_lock);
1906409Skcpoon 
1907409Skcpoon 	/* Try to look for a wildcard match. */
19083448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(0, ipst)];
1909409Skcpoon 	mutex_enter(&connfp->connf_lock);
1910409Skcpoon 	for (connp = connfp->connf_head; connp != NULL;
1911409Skcpoon 	    connp = connp->conn_next) {
1912409Skcpoon 		/* We don't allow v4 fallback for v6 raw socket. */
1913409Skcpoon 		if ((af == (connp->conn_af_isv6 ? IPV4_VERSION :
19142263Ssommerfe 		    IPV6_VERSION)) || !IPCL_ZONE_MATCH(connp, zoneid)) {
1915409Skcpoon 			continue;
1916409Skcpoon 		}
1917409Skcpoon 		if (af == IPV4_VERSION) {
1918409Skcpoon 			if (IPCL_RAW_MATCH(connp, protocol, hdr->ipha_dst))
1919409Skcpoon 				break;
1920409Skcpoon 		} else {
1921409Skcpoon 			if (IPCL_RAW_MATCH_V6(connp, protocol,
1922409Skcpoon 			    ((ip6_t *)hdr)->ip6_dst)) {
1923409Skcpoon 				break;
1924409Skcpoon 			}
1925409Skcpoon 		}
19260Sstevel@tonic-gate 	}
1927409Skcpoon 
1928409Skcpoon 	if (connp != NULL)
1929409Skcpoon 		goto found;
1930409Skcpoon 
19310Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
19320Sstevel@tonic-gate 	return (NULL);
1933409Skcpoon 
1934409Skcpoon found:
1935409Skcpoon 	ASSERT(connp != NULL);
1936409Skcpoon 	CONN_INC_REF(connp);
1937409Skcpoon 	mutex_exit(&connfp->connf_lock);
1938409Skcpoon 	return (connp);
19390Sstevel@tonic-gate }
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate /* ARGSUSED */
19420Sstevel@tonic-gate static int
19430Sstevel@tonic-gate ipcl_tcpconn_constructor(void *buf, void *cdrarg, int kmflags)
19440Sstevel@tonic-gate {
19450Sstevel@tonic-gate 	itc_t	*itc = (itc_t *)buf;
19460Sstevel@tonic-gate 	conn_t 	*connp = &itc->itc_conn;
19470Sstevel@tonic-gate 	tcp_t	*tcp = &itc->itc_tcp;
19480Sstevel@tonic-gate 	bzero(itc, sizeof (itc_t));
19490Sstevel@tonic-gate 	tcp->tcp_timercache = tcp_timermp_alloc(KM_NOSLEEP);
19500Sstevel@tonic-gate 	connp->conn_tcp = tcp;
19510Sstevel@tonic-gate 	connp->conn_flags = IPCL_TCPCONN;
19520Sstevel@tonic-gate 	connp->conn_ulp = IPPROTO_TCP;
19530Sstevel@tonic-gate 	tcp->tcp_connp = connp;
19540Sstevel@tonic-gate 	return (0);
19550Sstevel@tonic-gate }
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate /* ARGSUSED */
19580Sstevel@tonic-gate static void
19590Sstevel@tonic-gate ipcl_tcpconn_destructor(void *buf, void *cdrarg)
19600Sstevel@tonic-gate {
19610Sstevel@tonic-gate 	tcp_timermp_free(((conn_t *)buf)->conn_tcp);
19620Sstevel@tonic-gate }
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate /*
19650Sstevel@tonic-gate  * All conns are inserted in a global multi-list for the benefit of
19660Sstevel@tonic-gate  * walkers. The walk is guaranteed to walk all open conns at the time
19670Sstevel@tonic-gate  * of the start of the walk exactly once. This property is needed to
19680Sstevel@tonic-gate  * achieve some cleanups during unplumb of interfaces. This is achieved
19690Sstevel@tonic-gate  * as follows.
19700Sstevel@tonic-gate  *
19710Sstevel@tonic-gate  * ipcl_conn_create and ipcl_conn_destroy are the only functions that
19720Sstevel@tonic-gate  * call the insert and delete functions below at creation and deletion
19730Sstevel@tonic-gate  * time respectively. The conn never moves or changes its position in this
19740Sstevel@tonic-gate  * multi-list during its lifetime. CONN_CONDEMNED ensures that the refcnt
19750Sstevel@tonic-gate  * won't increase due to walkers, once the conn deletion has started. Note
19760Sstevel@tonic-gate  * that we can't remove the conn from the global list and then wait for
19770Sstevel@tonic-gate  * the refcnt to drop to zero, since walkers would then see a truncated
19780Sstevel@tonic-gate  * list. CONN_INCIPIENT ensures that walkers don't start looking at
19790Sstevel@tonic-gate  * conns until ip_open is ready to make them globally visible.
19800Sstevel@tonic-gate  * The global round robin multi-list locks are held only to get the
19810Sstevel@tonic-gate  * next member/insertion/deletion and contention should be negligible
19820Sstevel@tonic-gate  * if the multi-list is much greater than the number of cpus.
19830Sstevel@tonic-gate  */
19840Sstevel@tonic-gate void
19850Sstevel@tonic-gate ipcl_globalhash_insert(conn_t *connp)
19860Sstevel@tonic-gate {
19870Sstevel@tonic-gate 	int	index;
19883448Sdh155122 	struct connf_s	*connfp;
19893448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 	/*
19920Sstevel@tonic-gate 	 * No need for atomic here. Approximate even distribution
19930Sstevel@tonic-gate 	 * in the global lists is sufficient.
19940Sstevel@tonic-gate 	 */
19953448Sdh155122 	ipst->ips_conn_g_index++;
19963448Sdh155122 	index = ipst->ips_conn_g_index & (CONN_G_HASH_SIZE - 1);
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
19990Sstevel@tonic-gate 	/*
20000Sstevel@tonic-gate 	 * Mark as INCIPIENT, so that walkers will ignore this
20010Sstevel@tonic-gate 	 * for now, till ip_open is ready to make it visible globally.
20020Sstevel@tonic-gate 	 */
20030Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_INCIPIENT;
20040Sstevel@tonic-gate 
20053448Sdh155122 	connfp = &ipst->ips_ipcl_globalhash_fanout[index];
20060Sstevel@tonic-gate 	/* Insert at the head of the list */
20073448Sdh155122 	mutex_enter(&connfp->connf_lock);
20083448Sdh155122 	connp->conn_g_next = connfp->connf_head;
20090Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
20100Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp;
20113448Sdh155122 	connfp->connf_head = connp;
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	/* The fanout bucket this conn points to */
20143448Sdh155122 	connp->conn_g_fanout = connfp;
20150Sstevel@tonic-gate 
20163448Sdh155122 	mutex_exit(&connfp->connf_lock);
20170Sstevel@tonic-gate }
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate void
20200Sstevel@tonic-gate ipcl_globalhash_remove(conn_t *connp)
20210Sstevel@tonic-gate {
20223448Sdh155122 	struct connf_s	*connfp;
20233448Sdh155122 
20240Sstevel@tonic-gate 	/*
20250Sstevel@tonic-gate 	 * We were never inserted in the global multi list.
20260Sstevel@tonic-gate 	 * IPCL_NONE variety is never inserted in the global multilist
20270Sstevel@tonic-gate 	 * since it is presumed to not need any cleanup and is transient.
20280Sstevel@tonic-gate 	 */
20290Sstevel@tonic-gate 	if (connp->conn_g_fanout == NULL)
20300Sstevel@tonic-gate 		return;
20310Sstevel@tonic-gate 
20323448Sdh155122 	connfp = connp->conn_g_fanout;
20333448Sdh155122 	mutex_enter(&connfp->connf_lock);
20340Sstevel@tonic-gate 	if (connp->conn_g_prev != NULL)
20350Sstevel@tonic-gate 		connp->conn_g_prev->conn_g_next = connp->conn_g_next;
20360Sstevel@tonic-gate 	else
20373448Sdh155122 		connfp->connf_head = connp->conn_g_next;
20380Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
20390Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp->conn_g_prev;
20403448Sdh155122 	mutex_exit(&connfp->connf_lock);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	/* Better to stumble on a null pointer than to corrupt memory */
20430Sstevel@tonic-gate 	connp->conn_g_next = NULL;
20440Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
20450Sstevel@tonic-gate }
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate /*
20480Sstevel@tonic-gate  * Walk the list of all conn_t's in the system, calling the function provided
20490Sstevel@tonic-gate  * with the specified argument for each.
20500Sstevel@tonic-gate  * Applies to both IPv4 and IPv6.
20510Sstevel@tonic-gate  *
20520Sstevel@tonic-gate  * IPCs may hold pointers to ipif/ill. To guard against stale pointers
20530Sstevel@tonic-gate  * ipcl_walk() is called to cleanup the conn_t's, typically when an interface is
20540Sstevel@tonic-gate  * unplumbed or removed. New conn_t's that are created while we are walking
20550Sstevel@tonic-gate  * may be missed by this walk, because they are not necessarily inserted
20560Sstevel@tonic-gate  * at the tail of the list. They are new conn_t's and thus don't have any
20570Sstevel@tonic-gate  * stale pointers. The CONN_CLOSING flag ensures that no new reference
20580Sstevel@tonic-gate  * is created to the struct that is going away.
20590Sstevel@tonic-gate  */
20600Sstevel@tonic-gate void
20613448Sdh155122 ipcl_walk(pfv_t func, void *arg, ip_stack_t *ipst)
20620Sstevel@tonic-gate {
20630Sstevel@tonic-gate 	int	i;
20640Sstevel@tonic-gate 	conn_t	*connp;
20650Sstevel@tonic-gate 	conn_t	*prev_connp;
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
20683448Sdh155122 		mutex_enter(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
20690Sstevel@tonic-gate 		prev_connp = NULL;
20703448Sdh155122 		connp = ipst->ips_ipcl_globalhash_fanout[i].connf_head;
20710Sstevel@tonic-gate 		while (connp != NULL) {
20720Sstevel@tonic-gate 			mutex_enter(&connp->conn_lock);
20730Sstevel@tonic-gate 			if (connp->conn_state_flags &
20740Sstevel@tonic-gate 			    (CONN_CONDEMNED | CONN_INCIPIENT)) {
20750Sstevel@tonic-gate 				mutex_exit(&connp->conn_lock);
20760Sstevel@tonic-gate 				connp = connp->conn_g_next;
20770Sstevel@tonic-gate 				continue;
20780Sstevel@tonic-gate 			}
20790Sstevel@tonic-gate 			CONN_INC_REF_LOCKED(connp);
20800Sstevel@tonic-gate 			mutex_exit(&connp->conn_lock);
20813448Sdh155122 			mutex_exit(
20823448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
20830Sstevel@tonic-gate 			(*func)(connp, arg);
20840Sstevel@tonic-gate 			if (prev_connp != NULL)
20850Sstevel@tonic-gate 				CONN_DEC_REF(prev_connp);
20863448Sdh155122 			mutex_enter(
20873448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
20880Sstevel@tonic-gate 			prev_connp = connp;
20890Sstevel@tonic-gate 			connp = connp->conn_g_next;
20900Sstevel@tonic-gate 		}
20913448Sdh155122 		mutex_exit(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
20920Sstevel@tonic-gate 		if (prev_connp != NULL)
20930Sstevel@tonic-gate 			CONN_DEC_REF(prev_connp);
20940Sstevel@tonic-gate 	}
20950Sstevel@tonic-gate }
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate /*
20980Sstevel@tonic-gate  * Search for a peer TCP/IPv4 loopback conn by doing a reverse lookup on
20990Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
21000Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
21012323Sethindra  * (peer tcp in ESTABLISHED state).
21020Sstevel@tonic-gate  */
21030Sstevel@tonic-gate conn_t *
21043448Sdh155122 ipcl_conn_tcp_lookup_reversed_ipv4(conn_t *connp, ipha_t *ipha, tcph_t *tcph,
21053448Sdh155122     ip_stack_t *ipst)
21060Sstevel@tonic-gate {
21070Sstevel@tonic-gate 	uint32_t ports;
21080Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
21090Sstevel@tonic-gate 	connf_t	*connfp;
21100Sstevel@tonic-gate 	conn_t	*tconnp;
21110Sstevel@tonic-gate 	boolean_t zone_chk;
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	/*
21140Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
21150Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
21160Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
21170Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.
21180Sstevel@tonic-gate 	 */
21190Sstevel@tonic-gate 	zone_chk = (ipha->ipha_src == htonl(INADDR_LOOPBACK) ||
21200Sstevel@tonic-gate 	    ipha->ipha_dst == htonl(INADDR_LOOPBACK));
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
21230Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
21240Sstevel@tonic-gate 
21253448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
21263448Sdh155122 	    ports, ipst)];
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
21290Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
21300Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
21330Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
21342323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
21350Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 			ASSERT(tconnp != connp);
21380Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
21390Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
21400Sstevel@tonic-gate 			return (tconnp);
21410Sstevel@tonic-gate 		}
21420Sstevel@tonic-gate 	}
21430Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
21440Sstevel@tonic-gate 	return (NULL);
21450Sstevel@tonic-gate }
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate /*
21480Sstevel@tonic-gate  * Search for a peer TCP/IPv6 loopback conn by doing a reverse lookup on
21490Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
21500Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
21512323Sethindra  * (peer tcp in ESTABLISHED state).
21520Sstevel@tonic-gate  */
21530Sstevel@tonic-gate conn_t *
21543448Sdh155122 ipcl_conn_tcp_lookup_reversed_ipv6(conn_t *connp, ip6_t *ip6h, tcph_t *tcph,
21553448Sdh155122     ip_stack_t *ipst)
21560Sstevel@tonic-gate {
21570Sstevel@tonic-gate 	uint32_t ports;
21580Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
21590Sstevel@tonic-gate 	connf_t	*connfp;
21600Sstevel@tonic-gate 	conn_t	*tconnp;
21610Sstevel@tonic-gate 	boolean_t zone_chk;
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	/*
21640Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
21650Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
21660Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
21670Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.  We
21680Sstevel@tonic-gate 	 * don't do Zone check for link local address(es) because the
21690Sstevel@tonic-gate 	 * current Zone implementation treats each link local address as
21700Sstevel@tonic-gate 	 * being unique per system node, i.e. they belong to global Zone.
21710Sstevel@tonic-gate 	 */
21720Sstevel@tonic-gate 	zone_chk = (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src) ||
21730Sstevel@tonic-gate 	    IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst));
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
21760Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
21770Sstevel@tonic-gate 
21783448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
21793448Sdh155122 	    ports, ipst)];
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
21820Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
21830Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 		/* We skip tcp_bound_if check here as this is loopback tcp */
21860Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
21870Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
21882323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
21890Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 			ASSERT(tconnp != connp);
21920Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
21930Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
21940Sstevel@tonic-gate 			return (tconnp);
21950Sstevel@tonic-gate 		}
21960Sstevel@tonic-gate 	}
21970Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
21980Sstevel@tonic-gate 	return (NULL);
21990Sstevel@tonic-gate }
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate /*
22020Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
22030Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
22040Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
22050Sstevel@tonic-gate  */
22060Sstevel@tonic-gate conn_t *
22073448Sdh155122 ipcl_tcp_lookup_reversed_ipv4(ipha_t *ipha, tcph_t *tcph, int min_state,
22083448Sdh155122     ip_stack_t *ipst)
22090Sstevel@tonic-gate {
22100Sstevel@tonic-gate 	uint32_t ports;
22110Sstevel@tonic-gate 	uint16_t *pports;
22120Sstevel@tonic-gate 	connf_t	*connfp;
22130Sstevel@tonic-gate 	conn_t	*tconnp;
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
22160Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
22170Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
22180Sstevel@tonic-gate 
22193448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
2220*4691Skcpoon 	    ports, ipst)];
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
22230Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
22240Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
22270Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
22280Sstevel@tonic-gate 		    tconnp->conn_tcp->tcp_state >= min_state) {
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
22310Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
22320Sstevel@tonic-gate 			return (tconnp);
22330Sstevel@tonic-gate 		}
22340Sstevel@tonic-gate 	}
22350Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
22360Sstevel@tonic-gate 	return (NULL);
22370Sstevel@tonic-gate }
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate /*
22400Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
22410Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
22420Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
22430Sstevel@tonic-gate  * Match on ifindex in addition to addresses.
22440Sstevel@tonic-gate  */
22450Sstevel@tonic-gate conn_t *
22460Sstevel@tonic-gate ipcl_tcp_lookup_reversed_ipv6(ip6_t *ip6h, tcpha_t *tcpha, int min_state,
22473448Sdh155122     uint_t ifindex, ip_stack_t *ipst)
22480Sstevel@tonic-gate {
22490Sstevel@tonic-gate 	tcp_t	*tcp;
22500Sstevel@tonic-gate 	uint32_t ports;
22510Sstevel@tonic-gate 	uint16_t *pports;
22520Sstevel@tonic-gate 	connf_t	*connfp;
22530Sstevel@tonic-gate 	conn_t	*tconnp;
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
22560Sstevel@tonic-gate 	pports[0] = tcpha->tha_fport;
22570Sstevel@tonic-gate 	pports[1] = tcpha->tha_lport;
22580Sstevel@tonic-gate 
22593448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
2260*4691Skcpoon 	    ports, ipst)];
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
22630Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
22640Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 		tcp = tconnp->conn_tcp;
22670Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
22680Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
22690Sstevel@tonic-gate 		    tcp->tcp_state >= min_state &&
22700Sstevel@tonic-gate 		    (tcp->tcp_bound_if == 0 ||
22710Sstevel@tonic-gate 		    tcp->tcp_bound_if == ifindex)) {
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
22740Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
22750Sstevel@tonic-gate 			return (tconnp);
22760Sstevel@tonic-gate 		}
22770Sstevel@tonic-gate 	}
22780Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
22790Sstevel@tonic-gate 	return (NULL);
22800Sstevel@tonic-gate }
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate /*
22831676Sjpk  * Finds a TCP/IPv4 listening connection; called by tcp_disconnect to locate
22841676Sjpk  * a listener when changing state.
22850Sstevel@tonic-gate  */
22860Sstevel@tonic-gate conn_t *
22873448Sdh155122 ipcl_lookup_listener_v4(uint16_t lport, ipaddr_t laddr, zoneid_t zoneid,
22883448Sdh155122     ip_stack_t *ipst)
22890Sstevel@tonic-gate {
22900Sstevel@tonic-gate 	connf_t		*bind_connfp;
22910Sstevel@tonic-gate 	conn_t		*connp;
22920Sstevel@tonic-gate 	tcp_t		*tcp;
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	/*
22950Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
22960Sstevel@tonic-gate 	 * all zeros.
22970Sstevel@tonic-gate 	 */
22980Sstevel@tonic-gate 	if (laddr == 0)
22990Sstevel@tonic-gate 		return (NULL);
23000Sstevel@tonic-gate 
23011676Sjpk 	ASSERT(zoneid != ALL_ZONES);
23021676Sjpk 
23033448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
23040Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
23050Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
23060Sstevel@tonic-gate 	    connp = connp->conn_next) {
23070Sstevel@tonic-gate 		tcp = connp->conn_tcp;
23080Sstevel@tonic-gate 		if (IPCL_BIND_MATCH(connp, IPPROTO_TCP, laddr, lport) &&
23092263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
23100Sstevel@tonic-gate 		    (tcp->tcp_listener == NULL)) {
23110Sstevel@tonic-gate 			CONN_INC_REF(connp);
23120Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
23130Sstevel@tonic-gate 			return (connp);
23140Sstevel@tonic-gate 		}
23150Sstevel@tonic-gate 	}
23160Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
23170Sstevel@tonic-gate 	return (NULL);
23180Sstevel@tonic-gate }
23190Sstevel@tonic-gate 
23201676Sjpk /*
23211676Sjpk  * Finds a TCP/IPv6 listening connection; called by tcp_disconnect to locate
23221676Sjpk  * a listener when changing state.
23231676Sjpk  */
23240Sstevel@tonic-gate conn_t *
23250Sstevel@tonic-gate ipcl_lookup_listener_v6(uint16_t lport, in6_addr_t *laddr, uint_t ifindex,
23263448Sdh155122     zoneid_t zoneid, ip_stack_t *ipst)
23270Sstevel@tonic-gate {
23280Sstevel@tonic-gate 	connf_t		*bind_connfp;
23290Sstevel@tonic-gate 	conn_t		*connp = NULL;
23300Sstevel@tonic-gate 	tcp_t		*tcp;
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 	/*
23330Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
23340Sstevel@tonic-gate 	 * all zeros.
23350Sstevel@tonic-gate 	 */
23360Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(laddr))
23370Sstevel@tonic-gate 		return (NULL);
23380Sstevel@tonic-gate 
23391676Sjpk 	ASSERT(zoneid != ALL_ZONES);
23400Sstevel@tonic-gate 
23413448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
23420Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
23430Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
23440Sstevel@tonic-gate 	    connp = connp->conn_next) {
23450Sstevel@tonic-gate 		tcp = connp->conn_tcp;
23460Sstevel@tonic-gate 		if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP, *laddr, lport) &&
23472263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
23480Sstevel@tonic-gate 		    (tcp->tcp_bound_if == 0 ||
23490Sstevel@tonic-gate 		    tcp->tcp_bound_if == ifindex) &&
23500Sstevel@tonic-gate 		    tcp->tcp_listener == NULL) {
23510Sstevel@tonic-gate 			CONN_INC_REF(connp);
23520Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
23530Sstevel@tonic-gate 			return (connp);
23540Sstevel@tonic-gate 		}
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
23570Sstevel@tonic-gate 	return (NULL);
23580Sstevel@tonic-gate }
23590Sstevel@tonic-gate 
2360741Smasputra /*
2361741Smasputra  * ipcl_get_next_conn
2362741Smasputra  *	get the next entry in the conn global list
2363741Smasputra  *	and put a reference on the next_conn.
2364741Smasputra  *	decrement the reference on the current conn.
2365741Smasputra  *
2366741Smasputra  * This is an iterator based walker function that also provides for
2367741Smasputra  * some selection by the caller. It walks through the conn_hash bucket
2368741Smasputra  * searching for the next valid connp in the list, and selects connections
2369741Smasputra  * that are neither closed nor condemned. It also REFHOLDS the conn
2370741Smasputra  * thus ensuring that the conn exists when the caller uses the conn.
2371741Smasputra  */
2372741Smasputra conn_t *
2373741Smasputra ipcl_get_next_conn(connf_t *connfp, conn_t *connp, uint32_t conn_flags)
2374741Smasputra {
2375741Smasputra 	conn_t	*next_connp;
2376741Smasputra 
2377741Smasputra 	if (connfp == NULL)
2378741Smasputra 		return (NULL);
2379741Smasputra 
2380741Smasputra 	mutex_enter(&connfp->connf_lock);
2381741Smasputra 
2382741Smasputra 	next_connp = (connp == NULL) ?
2383741Smasputra 	    connfp->connf_head : connp->conn_g_next;
2384741Smasputra 
2385741Smasputra 	while (next_connp != NULL) {
2386741Smasputra 		mutex_enter(&next_connp->conn_lock);
2387741Smasputra 		if (!(next_connp->conn_flags & conn_flags) ||
2388741Smasputra 		    (next_connp->conn_state_flags &
2389741Smasputra 		    (CONN_CONDEMNED | CONN_INCIPIENT))) {
2390741Smasputra 			/*
2391741Smasputra 			 * This conn has been condemned or
2392741Smasputra 			 * is closing, or the flags don't match
2393741Smasputra 			 */
2394741Smasputra 			mutex_exit(&next_connp->conn_lock);
2395741Smasputra 			next_connp = next_connp->conn_g_next;
2396741Smasputra 			continue;
2397741Smasputra 		}
2398741Smasputra 		CONN_INC_REF_LOCKED(next_connp);
2399741Smasputra 		mutex_exit(&next_connp->conn_lock);
2400741Smasputra 		break;
2401741Smasputra 	}
2402741Smasputra 
2403741Smasputra 	mutex_exit(&connfp->connf_lock);
2404741Smasputra 
2405741Smasputra 	if (connp != NULL)
2406741Smasputra 		CONN_DEC_REF(connp);
2407741Smasputra 
2408741Smasputra 	return (next_connp);
2409741Smasputra }
2410741Smasputra 
24110Sstevel@tonic-gate #ifdef CONN_DEBUG
24120Sstevel@tonic-gate /*
24130Sstevel@tonic-gate  * Trace of the last NBUF refhold/refrele
24140Sstevel@tonic-gate  */
24150Sstevel@tonic-gate int
24160Sstevel@tonic-gate conn_trace_ref(conn_t *connp)
24170Sstevel@tonic-gate {
24180Sstevel@tonic-gate 	int	last;
24190Sstevel@tonic-gate 	conn_trace_t	*ctb;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
24220Sstevel@tonic-gate 	last = connp->conn_trace_last;
24230Sstevel@tonic-gate 	last++;
24240Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
24250Sstevel@tonic-gate 		last = 0;
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
24280Sstevel@tonic-gate 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, IP_STACK_DEPTH);
24290Sstevel@tonic-gate 	connp->conn_trace_last = last;
24300Sstevel@tonic-gate 	return (1);
24310Sstevel@tonic-gate }
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate int
24340Sstevel@tonic-gate conn_untrace_ref(conn_t *connp)
24350Sstevel@tonic-gate {
24360Sstevel@tonic-gate 	int	last;
24370Sstevel@tonic-gate 	conn_trace_t	*ctb;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
24400Sstevel@tonic-gate 	last = connp->conn_trace_last;
24410Sstevel@tonic-gate 	last++;
24420Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
24430Sstevel@tonic-gate 		last = 0;
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
24460Sstevel@tonic-gate 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, IP_STACK_DEPTH);
24470Sstevel@tonic-gate 	connp->conn_trace_last = last;
24480Sstevel@tonic-gate 	return (1);
24490Sstevel@tonic-gate }
24500Sstevel@tonic-gate #endif
2451