xref: /onnv-gate/usr/src/uts/common/inet/ip/ipclassifier.c (revision 8392:9ac6f680b09c)
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 /*
227502Saruna@cs.umn.edu  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * IP PACKET CLASSIFIER
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * The IP packet classifier provides mapping between IP packets and persistent
300Sstevel@tonic-gate  * connection state for connection-oriented protocols. It also provides
310Sstevel@tonic-gate  * interface for managing connection states.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * The connection state is kept in conn_t data structure and contains, among
340Sstevel@tonic-gate  * other things:
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *	o local/remote address and ports
370Sstevel@tonic-gate  *	o Transport protocol
380Sstevel@tonic-gate  *	o squeue for the connection (for TCP only)
390Sstevel@tonic-gate  *	o reference counter
400Sstevel@tonic-gate  *	o Connection state
410Sstevel@tonic-gate  *	o hash table linkage
420Sstevel@tonic-gate  *	o interface/ire information
430Sstevel@tonic-gate  *	o credentials
440Sstevel@tonic-gate  *	o ipsec policy
450Sstevel@tonic-gate  *	o send and receive functions.
460Sstevel@tonic-gate  *	o mutex lock.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * Connections use a reference counting scheme. They are freed when the
490Sstevel@tonic-gate  * reference counter drops to zero. A reference is incremented when connection
500Sstevel@tonic-gate  * is placed in a list or table, when incoming packet for the connection arrives
510Sstevel@tonic-gate  * and when connection is processed via squeue (squeue processing may be
520Sstevel@tonic-gate  * asynchronous and the reference protects the connection from being destroyed
530Sstevel@tonic-gate  * before its processing is finished).
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * send and receive functions are currently used for TCP only. The send function
560Sstevel@tonic-gate  * determines the IP entry point for the packet once it leaves TCP to be sent to
570Sstevel@tonic-gate  * the destination address. The receive function is used by IP when the packet
580Sstevel@tonic-gate  * should be passed for TCP processing. When a new connection is created these
590Sstevel@tonic-gate  * are set to ip_output() and tcp_input() respectively. During the lifetime of
600Sstevel@tonic-gate  * the connection the send and receive functions may change depending on the
610Sstevel@tonic-gate  * changes in the connection state. For example, Once the connection is bound to
620Sstevel@tonic-gate  * an addresse, the receive function for this connection is set to
630Sstevel@tonic-gate  * tcp_conn_request().  This allows incoming SYNs to go directly into the
640Sstevel@tonic-gate  * listener SYN processing function without going to tcp_input() first.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * Classifier uses several hash tables:
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 	ipcl_conn_fanout:	contains all TCP connections in CONNECTED state
690Sstevel@tonic-gate  *	ipcl_bind_fanout:	contains all connections in BOUND state
700Sstevel@tonic-gate  *	ipcl_proto_fanout:	IPv4 protocol fanout
710Sstevel@tonic-gate  *	ipcl_proto_fanout_v6:	IPv6 protocol fanout
720Sstevel@tonic-gate  *	ipcl_udp_fanout:	contains all UDP connections
730Sstevel@tonic-gate  *	ipcl_globalhash_fanout:	contains all connections
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * The ipcl_globalhash_fanout is used for any walkers (like snmp and Clustering)
760Sstevel@tonic-gate  * which need to view all existing connections.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * All tables are protected by per-bucket locks. When both per-bucket lock and
790Sstevel@tonic-gate  * connection lock need to be held, the per-bucket lock should be acquired
800Sstevel@tonic-gate  * first, followed by the connection lock.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * All functions doing search in one of these tables increment a reference
830Sstevel@tonic-gate  * counter on the connection found (if any). This reference should be dropped
840Sstevel@tonic-gate  * when the caller has finished processing the connection.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * INTERFACES:
880Sstevel@tonic-gate  * ===========
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  * Connection Lookup:
910Sstevel@tonic-gate  * ------------------
920Sstevel@tonic-gate  *
933448Sdh155122  * conn_t *ipcl_classify_v4(mp, protocol, hdr_len, zoneid, ip_stack)
943448Sdh155122  * conn_t *ipcl_classify_v6(mp, protocol, hdr_len, zoneid, ip_stack)
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * Finds connection for an incoming IPv4 or IPv6 packet. Returns NULL if
970Sstevel@tonic-gate  * it can't find any associated connection. If the connection is found, its
980Sstevel@tonic-gate  * reference counter is incremented.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *	mp:	mblock, containing packet header. The full header should fit
1010Sstevel@tonic-gate  *		into a single mblock. It should also contain at least full IP
1020Sstevel@tonic-gate  *		and TCP or UDP header.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  *	protocol: Either IPPROTO_TCP or IPPROTO_UDP.
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  *	hdr_len: The size of IP header. It is used to find TCP or UDP header in
1070Sstevel@tonic-gate  *		 the packet.
1080Sstevel@tonic-gate  *
1091676Sjpk  * 	zoneid: The zone in which the returned connection must be; the zoneid
1101676Sjpk  *		corresponding to the ire_zoneid on the IRE located for the
1111676Sjpk  *		packet's destination address.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  *	For TCP connections, the lookup order is as follows:
1140Sstevel@tonic-gate  *		5-tuple {src, dst, protocol, local port, remote port}
1150Sstevel@tonic-gate  *			lookup in ipcl_conn_fanout table.
1160Sstevel@tonic-gate  *		3-tuple {dst, remote port, protocol} lookup in
1170Sstevel@tonic-gate  *			ipcl_bind_fanout table.
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  *	For UDP connections, a 5-tuple {src, dst, protocol, local port,
1200Sstevel@tonic-gate  *	remote port} lookup is done on ipcl_udp_fanout. Note that,
1210Sstevel@tonic-gate  *	these interfaces do not handle cases where a packets belongs
1220Sstevel@tonic-gate  *	to multiple UDP clients, which is handled in IP itself.
1230Sstevel@tonic-gate  *
1241676Sjpk  * If the destination IRE is ALL_ZONES (indicated by zoneid), then we must
1251676Sjpk  * determine which actual zone gets the segment.  This is used only in a
1261676Sjpk  * labeled environment.  The matching rules are:
1271676Sjpk  *
1281676Sjpk  *	- If it's not a multilevel port, then the label on the packet selects
1291676Sjpk  *	  the zone.  Unlabeled packets are delivered to the global zone.
1301676Sjpk  *
1311676Sjpk  *	- If it's a multilevel port, then only the zone registered to receive
1321676Sjpk  *	  packets on that port matches.
1331676Sjpk  *
1341676Sjpk  * Also, in a labeled environment, packet labels need to be checked.  For fully
1351676Sjpk  * bound TCP connections, we can assume that the packet label was checked
1361676Sjpk  * during connection establishment, and doesn't need to be checked on each
1371676Sjpk  * packet.  For others, though, we need to check for strict equality or, for
1381676Sjpk  * multilevel ports, membership in the range or set.  This part currently does
1391676Sjpk  * a tnrh lookup on each packet, but could be optimized to use cached results
1401676Sjpk  * if that were necessary.  (SCTP doesn't come through here, but if it did,
1411676Sjpk  * we would apply the same rules as TCP.)
1421676Sjpk  *
1431676Sjpk  * An implication of the above is that fully-bound TCP sockets must always use
1441676Sjpk  * distinct 4-tuples; they can't be discriminated by label alone.
1451676Sjpk  *
1461676Sjpk  * Note that we cannot trust labels on packets sent to fully-bound UDP sockets,
1471676Sjpk  * as there's no connection set-up handshake and no shared state.
1481676Sjpk  *
1491676Sjpk  * Labels on looped-back packets within a single zone do not need to be
1501676Sjpk  * checked, as all processes in the same zone have the same label.
1511676Sjpk  *
1521676Sjpk  * Finally, for unlabeled packets received by a labeled system, special rules
1531676Sjpk  * apply.  We consider only the MLP if there is one.  Otherwise, we prefer a
1541676Sjpk  * socket in the zone whose label matches the default label of the sender, if
1551676Sjpk  * any.  In any event, the receiving socket must have SO_MAC_EXEMPT set and the
1561676Sjpk  * receiver's label must dominate the sender's default label.
1571676Sjpk  *
1583448Sdh155122  * conn_t *ipcl_tcp_lookup_reversed_ipv4(ipha_t *, tcph_t *, int, ip_stack);
1593448Sdh155122  * conn_t *ipcl_tcp_lookup_reversed_ipv6(ip6_t *, tcpha_t *, int, uint_t,
1603448Sdh155122  *					 ip_stack);
1610Sstevel@tonic-gate  *
1620Sstevel@tonic-gate  *	Lookup routine to find a exact match for {src, dst, local port,
1630Sstevel@tonic-gate  *	remote port) for TCP connections in ipcl_conn_fanout. The address and
1640Sstevel@tonic-gate  *	ports are read from the IP and TCP header respectively.
1650Sstevel@tonic-gate  *
1663448Sdh155122  * conn_t	*ipcl_lookup_listener_v4(lport, laddr, protocol,
1673448Sdh155122  *					 zoneid, ip_stack);
1683448Sdh155122  * conn_t	*ipcl_lookup_listener_v6(lport, laddr, protocol, ifindex,
1693448Sdh155122  *					 zoneid, ip_stack);
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  * 	Lookup routine to find a listener with the tuple {lport, laddr,
1720Sstevel@tonic-gate  * 	protocol} in the ipcl_bind_fanout table. For IPv6, an additional
1730Sstevel@tonic-gate  * 	parameter interface index is also compared.
1740Sstevel@tonic-gate  *
1753448Sdh155122  * void ipcl_walk(func, arg, ip_stack)
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  * 	Apply 'func' to every connection available. The 'func' is called as
1780Sstevel@tonic-gate  *	(*func)(connp, arg). The walk is non-atomic so connections may be
1790Sstevel@tonic-gate  *	created and destroyed during the walk. The CONN_CONDEMNED and
1800Sstevel@tonic-gate  *	CONN_INCIPIENT flags ensure that connections which are newly created
1810Sstevel@tonic-gate  *	or being destroyed are not selected by the walker.
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * Table Updates
1840Sstevel@tonic-gate  * -------------
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  * int ipcl_conn_insert(connp, protocol, src, dst, ports)
1870Sstevel@tonic-gate  * int ipcl_conn_insert_v6(connp, protocol, src, dst, ports, ifindex)
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  *	Insert 'connp' in the ipcl_conn_fanout.
1900Sstevel@tonic-gate  *	Arguements :
1910Sstevel@tonic-gate  *		connp		conn_t to be inserted
1920Sstevel@tonic-gate  *		protocol	connection protocol
1930Sstevel@tonic-gate  *		src		source address
1940Sstevel@tonic-gate  *		dst		destination address
1950Sstevel@tonic-gate  *		ports		local and remote port
1960Sstevel@tonic-gate  *		ifindex		interface index for IPv6 connections
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  *	Return value :
1990Sstevel@tonic-gate  *		0		if connp was inserted
2000Sstevel@tonic-gate  *		EADDRINUSE	if the connection with the same tuple
2010Sstevel@tonic-gate  *				already exists.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  * int ipcl_bind_insert(connp, protocol, src, lport);
2040Sstevel@tonic-gate  * int ipcl_bind_insert_v6(connp, protocol, src, lport);
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  * 	Insert 'connp' in ipcl_bind_fanout.
2070Sstevel@tonic-gate  * 	Arguements :
2080Sstevel@tonic-gate  * 		connp		conn_t to be inserted
2090Sstevel@tonic-gate  * 		protocol	connection protocol
2100Sstevel@tonic-gate  * 		src		source address connection wants
2110Sstevel@tonic-gate  * 				to bind to
2120Sstevel@tonic-gate  * 		lport		local port connection wants to
2130Sstevel@tonic-gate  * 				bind to
2140Sstevel@tonic-gate  *
2150Sstevel@tonic-gate  *
2160Sstevel@tonic-gate  * void ipcl_hash_remove(connp);
2170Sstevel@tonic-gate  *
2180Sstevel@tonic-gate  * 	Removes the 'connp' from the connection fanout table.
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * Connection Creation/Destruction
2210Sstevel@tonic-gate  * -------------------------------
2220Sstevel@tonic-gate  *
2233448Sdh155122  * conn_t *ipcl_conn_create(type, sleep, netstack_t *)
2240Sstevel@tonic-gate  *
2250Sstevel@tonic-gate  * 	Creates a new conn based on the type flag, inserts it into
2260Sstevel@tonic-gate  * 	globalhash table.
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  *	type:	This flag determines the type of conn_t which needs to be
2295240Snordmark  *		created i.e., which kmem_cache it comes from.
2300Sstevel@tonic-gate  *		IPCL_TCPCONN	indicates a TCP connection
2315240Snordmark  *		IPCL_SCTPCONN	indicates a SCTP connection
2325240Snordmark  *		IPCL_UDPCONN	indicates a UDP conn_t.
2335240Snordmark  *		IPCL_RAWIPCONN	indicates a RAWIP/ICMP conn_t.
2345240Snordmark  *		IPCL_RTSCONN	indicates a RTS conn_t.
2355240Snordmark  *		IPCL_IPCCONN	indicates all other connections.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * void ipcl_conn_destroy(connp)
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  * 	Destroys the connection state, removes it from the global
2400Sstevel@tonic-gate  * 	connection hash table and frees its memory.
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate #include <sys/types.h>
2440Sstevel@tonic-gate #include <sys/stream.h>
2450Sstevel@tonic-gate #include <sys/stropts.h>
2460Sstevel@tonic-gate #include <sys/sysmacros.h>
2470Sstevel@tonic-gate #include <sys/strsubr.h>
2480Sstevel@tonic-gate #include <sys/strsun.h>
2490Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
2500Sstevel@tonic-gate #include <sys/ddi.h>
2510Sstevel@tonic-gate #include <sys/cmn_err.h>
2520Sstevel@tonic-gate #include <sys/debug.h>
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #include <sys/systm.h>
2550Sstevel@tonic-gate #include <sys/param.h>
2560Sstevel@tonic-gate #include <sys/kmem.h>
2570Sstevel@tonic-gate #include <sys/isa_defs.h>
2580Sstevel@tonic-gate #include <inet/common.h>
2590Sstevel@tonic-gate #include <netinet/ip6.h>
2600Sstevel@tonic-gate #include <netinet/icmp6.h>
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate #include <inet/ip.h>
2630Sstevel@tonic-gate #include <inet/ip6.h>
2640Sstevel@tonic-gate #include <inet/ip_ndp.h>
2658348SEric.Yu@Sun.COM #include <inet/ip_impl.h>
266741Smasputra #include <inet/udp_impl.h>
2670Sstevel@tonic-gate #include <inet/sctp_ip.h>
2683448Sdh155122 #include <inet/sctp/sctp_impl.h>
2695240Snordmark #include <inet/rawip_impl.h>
2705240Snordmark #include <inet/rts_impl.h>
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate #include <sys/cpuvar.h>
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate #include <inet/ipclassifier.h>
2758348SEric.Yu@Sun.COM #include <inet/tcp.h>
2760Sstevel@tonic-gate #include <inet/ipsec_impl.h>
2770Sstevel@tonic-gate 
2781676Sjpk #include <sys/tsol/tnet.h>
2798348SEric.Yu@Sun.COM #include <sys/sockio.h>
2801676Sjpk 
2810Sstevel@tonic-gate #ifdef DEBUG
2820Sstevel@tonic-gate #define	IPCL_DEBUG
2830Sstevel@tonic-gate #else
2840Sstevel@tonic-gate #undef	IPCL_DEBUG
2850Sstevel@tonic-gate #endif
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate #ifdef	IPCL_DEBUG
2880Sstevel@tonic-gate int	ipcl_debug_level = 0;
2890Sstevel@tonic-gate #define	IPCL_DEBUG_LVL(level, args)	\
2900Sstevel@tonic-gate 	if (ipcl_debug_level  & level) { printf args; }
2910Sstevel@tonic-gate #else
2920Sstevel@tonic-gate #define	IPCL_DEBUG_LVL(level, args) {; }
2930Sstevel@tonic-gate #endif
2943448Sdh155122 /* Old value for compatibility. Setable in /etc/system */
2950Sstevel@tonic-gate uint_t tcp_conn_hash_size = 0;
2960Sstevel@tonic-gate 
2973448Sdh155122 /* New value. Zero means choose automatically.  Setable in /etc/system */
2980Sstevel@tonic-gate uint_t ipcl_conn_hash_size = 0;
2990Sstevel@tonic-gate uint_t ipcl_conn_hash_memfactor = 8192;
3000Sstevel@tonic-gate uint_t ipcl_conn_hash_maxsize = 82500;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /* bind/udp fanout table size */
3030Sstevel@tonic-gate uint_t ipcl_bind_fanout_size = 512;
3041503Sericheng uint_t ipcl_udp_fanout_size = 16384;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /* Raw socket fanout size.  Must be a power of 2. */
3070Sstevel@tonic-gate uint_t ipcl_raw_fanout_size = 256;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate  * Power of 2^N Primes useful for hashing for N of 0-28,
3110Sstevel@tonic-gate  * these primes are the nearest prime <= 2^N - 2^(N-2).
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate #define	P2Ps() {0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,	\
3150Sstevel@tonic-gate 		6143, 12281, 24571, 49139, 98299, 196597, 393209,	\
3160Sstevel@tonic-gate 		786431, 1572853, 3145721, 6291449, 12582893, 25165813,	\
3170Sstevel@tonic-gate 		50331599, 100663291, 201326557, 0}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3205240Snordmark  * wrapper structure to ensure that conn and what follows it (tcp_t, etc)
3215240Snordmark  * are aligned on cache lines.
3220Sstevel@tonic-gate  */
3235240Snordmark typedef union itc_s {
3245240Snordmark 	conn_t	itc_conn;
3255240Snordmark 	char	itcu_filler[CACHE_ALIGN(conn_s)];
3260Sstevel@tonic-gate } itc_t;
3270Sstevel@tonic-gate 
3285240Snordmark struct kmem_cache  *tcp_conn_cache;
3295240Snordmark struct kmem_cache  *ip_conn_cache;
3308348SEric.Yu@Sun.COM struct kmem_cache  *ip_helper_stream_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;
3345240Snordmark struct kmem_cache  *udp_conn_cache;
3355240Snordmark struct kmem_cache  *rawip_conn_cache;
3365240Snordmark struct kmem_cache  *rts_conn_cache;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate extern void	tcp_timermp_free(tcp_t *);
3390Sstevel@tonic-gate extern mblk_t	*tcp_timermp_alloc(int);
3400Sstevel@tonic-gate 
3415240Snordmark static int	ip_conn_constructor(void *, void *, int);
3425240Snordmark static void	ip_conn_destructor(void *, void *);
3435240Snordmark 
3445240Snordmark static int	tcp_conn_constructor(void *, void *, int);
3455240Snordmark static void	tcp_conn_destructor(void *, void *);
3465240Snordmark 
3475240Snordmark static int	udp_conn_constructor(void *, void *, int);
3485240Snordmark static void	udp_conn_destructor(void *, void *);
3495240Snordmark 
3505240Snordmark static int	rawip_conn_constructor(void *, void *, int);
3515240Snordmark static void	rawip_conn_destructor(void *, void *);
3525240Snordmark 
3535240Snordmark static int	rts_conn_constructor(void *, void *, int);
3545240Snordmark static void	rts_conn_destructor(void *, void *);
3550Sstevel@tonic-gate 
3568348SEric.Yu@Sun.COM static int	ip_helper_stream_constructor(void *, void *, int);
3578348SEric.Yu@Sun.COM static void	ip_helper_stream_destructor(void *, void *);
3588348SEric.Yu@Sun.COM 
3598348SEric.Yu@Sun.COM boolean_t	ip_use_helper_cache = B_TRUE;
3608348SEric.Yu@Sun.COM 
361*8392SHuafeng.Lv@Sun.COM /*
362*8392SHuafeng.Lv@Sun.COM  * Hook functions to enable cluster networking
363*8392SHuafeng.Lv@Sun.COM  * On non-clustered systems these vectors must always be NULL.
364*8392SHuafeng.Lv@Sun.COM  */
365*8392SHuafeng.Lv@Sun.COM extern void	(*cl_inet_listen)(netstackid_t, uint8_t, sa_family_t,
366*8392SHuafeng.Lv@Sun.COM 		    uint8_t *, in_port_t, void *);
367*8392SHuafeng.Lv@Sun.COM extern void	(*cl_inet_unlisten)(netstackid_t, uint8_t, sa_family_t,
368*8392SHuafeng.Lv@Sun.COM 		    uint8_t *, in_port_t, void *);
369*8392SHuafeng.Lv@Sun.COM 
3700Sstevel@tonic-gate #ifdef	IPCL_DEBUG
3710Sstevel@tonic-gate #define	INET_NTOA_BUFSIZE	18
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate static char *
3740Sstevel@tonic-gate inet_ntoa_r(uint32_t in, char *b)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate 	unsigned char	*p;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	p = (unsigned char *)&in;
3790Sstevel@tonic-gate 	(void) sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
3800Sstevel@tonic-gate 	return (b);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate #endif
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate /*
3853448Sdh155122  * Global (for all stack instances) init routine
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate void
3883448Sdh155122 ipcl_g_init(void)
3890Sstevel@tonic-gate {
3905240Snordmark 	ip_conn_cache = kmem_cache_create("ip_conn_cache",
3910Sstevel@tonic-gate 	    sizeof (conn_t), CACHE_ALIGN_SIZE,
3925240Snordmark 	    ip_conn_constructor, ip_conn_destructor,
3935240Snordmark 	    NULL, NULL, NULL, 0);
3945240Snordmark 
3955240Snordmark 	tcp_conn_cache = kmem_cache_create("tcp_conn_cache",
3965240Snordmark 	    sizeof (itc_t) + sizeof (tcp_t), CACHE_ALIGN_SIZE,
3975240Snordmark 	    tcp_conn_constructor, tcp_conn_destructor,
3985240Snordmark 	    NULL, NULL, NULL, 0);
3990Sstevel@tonic-gate 
4005240Snordmark 	udp_conn_cache = kmem_cache_create("udp_conn_cache",
4015240Snordmark 	    sizeof (itc_t) + sizeof (udp_t), CACHE_ALIGN_SIZE,
4025240Snordmark 	    udp_conn_constructor, udp_conn_destructor,
4035240Snordmark 	    NULL, NULL, NULL, 0);
4045240Snordmark 
4055240Snordmark 	rawip_conn_cache = kmem_cache_create("rawip_conn_cache",
4065240Snordmark 	    sizeof (itc_t) + sizeof (icmp_t), CACHE_ALIGN_SIZE,
4075240Snordmark 	    rawip_conn_constructor, rawip_conn_destructor,
4085240Snordmark 	    NULL, NULL, NULL, 0);
4095240Snordmark 
4105240Snordmark 	rts_conn_cache = kmem_cache_create("rts_conn_cache",
4115240Snordmark 	    sizeof (itc_t) + sizeof (rts_t), CACHE_ALIGN_SIZE,
4125240Snordmark 	    rts_conn_constructor, rts_conn_destructor,
4130Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
4148348SEric.Yu@Sun.COM 
4158348SEric.Yu@Sun.COM 	if (ip_use_helper_cache) {
4168348SEric.Yu@Sun.COM 		ip_helper_stream_cache = kmem_cache_create
4178348SEric.Yu@Sun.COM 		    ("ip_helper_stream_cache", sizeof (ip_helper_stream_info_t),
4188348SEric.Yu@Sun.COM 		    CACHE_ALIGN_SIZE, ip_helper_stream_constructor,
4198348SEric.Yu@Sun.COM 		    ip_helper_stream_destructor, NULL, NULL, NULL, 0);
4208348SEric.Yu@Sun.COM 	} else {
4218348SEric.Yu@Sun.COM 		ip_helper_stream_cache = NULL;
4228348SEric.Yu@Sun.COM 	}
4233448Sdh155122 }
4243448Sdh155122 
4253448Sdh155122 /*
4263448Sdh155122  * ipclassifier intialization routine, sets up hash tables.
4273448Sdh155122  */
4283448Sdh155122 void
4293448Sdh155122 ipcl_init(ip_stack_t *ipst)
4303448Sdh155122 {
4313448Sdh155122 	int i;
4323448Sdh155122 	int sizes[] = P2Ps();
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/*
4353448Sdh155122 	 * Calculate size of conn fanout table from /etc/system settings
4360Sstevel@tonic-gate 	 */
4370Sstevel@tonic-gate 	if (ipcl_conn_hash_size != 0) {
4383448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = ipcl_conn_hash_size;
4390Sstevel@tonic-gate 	} else if (tcp_conn_hash_size != 0) {
4403448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = tcp_conn_hash_size;
4410Sstevel@tonic-gate 	} else {
4420Sstevel@tonic-gate 		extern pgcnt_t freemem;
4430Sstevel@tonic-gate 
4443448Sdh155122 		ipst->ips_ipcl_conn_fanout_size =
4450Sstevel@tonic-gate 		    (freemem * PAGESIZE) / ipcl_conn_hash_memfactor;
4460Sstevel@tonic-gate 
4473448Sdh155122 		if (ipst->ips_ipcl_conn_fanout_size > ipcl_conn_hash_maxsize) {
4483448Sdh155122 			ipst->ips_ipcl_conn_fanout_size =
4493448Sdh155122 			    ipcl_conn_hash_maxsize;
4503448Sdh155122 		}
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	for (i = 9; i < sizeof (sizes) / sizeof (*sizes) - 1; i++) {
4543448Sdh155122 		if (sizes[i] >= ipst->ips_ipcl_conn_fanout_size) {
4550Sstevel@tonic-gate 			break;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 	}
4583448Sdh155122 	if ((ipst->ips_ipcl_conn_fanout_size = sizes[i]) == 0) {
4590Sstevel@tonic-gate 		/* Out of range, use the 2^16 value */
4603448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = sizes[16];
4610Sstevel@tonic-gate 	}
4623448Sdh155122 
4633448Sdh155122 	/* Take values from /etc/system */
4643448Sdh155122 	ipst->ips_ipcl_bind_fanout_size = ipcl_bind_fanout_size;
4653448Sdh155122 	ipst->ips_ipcl_udp_fanout_size = ipcl_udp_fanout_size;
4663448Sdh155122 	ipst->ips_ipcl_raw_fanout_size = ipcl_raw_fanout_size;
4670Sstevel@tonic-gate 
4683448Sdh155122 	ASSERT(ipst->ips_ipcl_conn_fanout == NULL);
4693448Sdh155122 
4703448Sdh155122 	ipst->ips_ipcl_conn_fanout = kmem_zalloc(
4713448Sdh155122 	    ipst->ips_ipcl_conn_fanout_size * sizeof (connf_t), KM_SLEEP);
4723448Sdh155122 
4733448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
4743448Sdh155122 		mutex_init(&ipst->ips_ipcl_conn_fanout[i].connf_lock, NULL,
4750Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4783448Sdh155122 	ipst->ips_ipcl_bind_fanout = kmem_zalloc(
4793448Sdh155122 	    ipst->ips_ipcl_bind_fanout_size * sizeof (connf_t), KM_SLEEP);
4800Sstevel@tonic-gate 
4813448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
4823448Sdh155122 		mutex_init(&ipst->ips_ipcl_bind_fanout[i].connf_lock, NULL,
4830Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4863448Sdh155122 	ipst->ips_ipcl_proto_fanout = kmem_zalloc(IPPROTO_MAX *
4873448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4883448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4893448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout[i].connf_lock, NULL,
4900Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4910Sstevel@tonic-gate 	}
4923448Sdh155122 
4933448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = kmem_zalloc(IPPROTO_MAX *
4943448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4953448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4963448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock, NULL,
4970Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5003448Sdh155122 	ipst->ips_rts_clients = kmem_zalloc(sizeof (connf_t), KM_SLEEP);
5013448Sdh155122 	mutex_init(&ipst->ips_rts_clients->connf_lock,
5023448Sdh155122 	    NULL, MUTEX_DEFAULT, NULL);
5030Sstevel@tonic-gate 
5043448Sdh155122 	ipst->ips_ipcl_udp_fanout = kmem_zalloc(
5053448Sdh155122 	    ipst->ips_ipcl_udp_fanout_size * sizeof (connf_t), KM_SLEEP);
5063448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
5073448Sdh155122 		mutex_init(&ipst->ips_ipcl_udp_fanout[i].connf_lock, NULL,
5080Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5113448Sdh155122 	ipst->ips_ipcl_raw_fanout = kmem_zalloc(
5123448Sdh155122 	    ipst->ips_ipcl_raw_fanout_size * sizeof (connf_t), KM_SLEEP);
5133448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
5143448Sdh155122 		mutex_init(&ipst->ips_ipcl_raw_fanout[i].connf_lock, NULL,
5150Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5183448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = kmem_zalloc(
5193448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE, KM_SLEEP);
5200Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
5213448Sdh155122 		mutex_init(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock,
5223448Sdh155122 		    NULL, MUTEX_DEFAULT, NULL);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate void
5273448Sdh155122 ipcl_g_destroy(void)
5280Sstevel@tonic-gate {
5295240Snordmark 	kmem_cache_destroy(ip_conn_cache);
5305240Snordmark 	kmem_cache_destroy(tcp_conn_cache);
5315240Snordmark 	kmem_cache_destroy(udp_conn_cache);
5325240Snordmark 	kmem_cache_destroy(rawip_conn_cache);
5335240Snordmark 	kmem_cache_destroy(rts_conn_cache);
5343448Sdh155122 }
5353448Sdh155122 
5363448Sdh155122 /*
5373448Sdh155122  * All user-level and kernel use of the stack must be gone
5383448Sdh155122  * by now.
5393448Sdh155122  */
5403448Sdh155122 void
5413448Sdh155122 ipcl_destroy(ip_stack_t *ipst)
5423448Sdh155122 {
5433448Sdh155122 	int i;
5443448Sdh155122 
5453448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
5463448Sdh155122 		ASSERT(ipst->ips_ipcl_conn_fanout[i].connf_head == NULL);
5473448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_conn_fanout[i].connf_lock);
5483448Sdh155122 	}
5493448Sdh155122 	kmem_free(ipst->ips_ipcl_conn_fanout, ipst->ips_ipcl_conn_fanout_size *
5503448Sdh155122 	    sizeof (connf_t));
5513448Sdh155122 	ipst->ips_ipcl_conn_fanout = NULL;
5523448Sdh155122 
5533448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
5543448Sdh155122 		ASSERT(ipst->ips_ipcl_bind_fanout[i].connf_head == NULL);
5553448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_bind_fanout[i].connf_lock);
5563448Sdh155122 	}
5573448Sdh155122 	kmem_free(ipst->ips_ipcl_bind_fanout, ipst->ips_ipcl_bind_fanout_size *
5583448Sdh155122 	    sizeof (connf_t));
5593448Sdh155122 	ipst->ips_ipcl_bind_fanout = NULL;
5603448Sdh155122 
5613448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5623448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout[i].connf_head == NULL);
5633448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout[i].connf_lock);
5643448Sdh155122 	}
5653448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout, IPPROTO_MAX * sizeof (connf_t));
5663448Sdh155122 	ipst->ips_ipcl_proto_fanout = NULL;
5670Sstevel@tonic-gate 
5683448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5693448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout_v6[i].connf_head == NULL);
5703448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock);
5713448Sdh155122 	}
5723448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout_v6,
5733448Sdh155122 	    IPPROTO_MAX * sizeof (connf_t));
5743448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = NULL;
5753448Sdh155122 
5763448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
5773448Sdh155122 		ASSERT(ipst->ips_ipcl_udp_fanout[i].connf_head == NULL);
5783448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_udp_fanout[i].connf_lock);
5793448Sdh155122 	}
5803448Sdh155122 	kmem_free(ipst->ips_ipcl_udp_fanout, ipst->ips_ipcl_udp_fanout_size *
5813448Sdh155122 	    sizeof (connf_t));
5823448Sdh155122 	ipst->ips_ipcl_udp_fanout = NULL;
5830Sstevel@tonic-gate 
5843448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
5853448Sdh155122 		ASSERT(ipst->ips_ipcl_raw_fanout[i].connf_head == NULL);
5863448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_raw_fanout[i].connf_lock);
5873448Sdh155122 	}
5883448Sdh155122 	kmem_free(ipst->ips_ipcl_raw_fanout, ipst->ips_ipcl_raw_fanout_size *
5893448Sdh155122 	    sizeof (connf_t));
5903448Sdh155122 	ipst->ips_ipcl_raw_fanout = NULL;
5910Sstevel@tonic-gate 
5923448Sdh155122 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
5933448Sdh155122 		ASSERT(ipst->ips_ipcl_globalhash_fanout[i].connf_head == NULL);
5943448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
5953448Sdh155122 	}
5963448Sdh155122 	kmem_free(ipst->ips_ipcl_globalhash_fanout,
5973448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE);
5983448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = NULL;
5990Sstevel@tonic-gate 
6003448Sdh155122 	ASSERT(ipst->ips_rts_clients->connf_head == NULL);
6013448Sdh155122 	mutex_destroy(&ipst->ips_rts_clients->connf_lock);
6023448Sdh155122 	kmem_free(ipst->ips_rts_clients, sizeof (connf_t));
6033448Sdh155122 	ipst->ips_rts_clients = NULL;
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate  * conn creation routine. initialize the conn, sets the reference
6080Sstevel@tonic-gate  * and inserts it in the global hash table.
6090Sstevel@tonic-gate  */
6100Sstevel@tonic-gate conn_t *
6113448Sdh155122 ipcl_conn_create(uint32_t type, int sleep, netstack_t *ns)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	conn_t	*connp;
6143448Sdh155122 	sctp_stack_t *sctps;
6155240Snordmark 	struct kmem_cache *conn_cache;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	switch (type) {
6180Sstevel@tonic-gate 	case IPCL_SCTPCONN:
6190Sstevel@tonic-gate 		if ((connp = kmem_cache_alloc(sctp_conn_cache, sleep)) == NULL)
6200Sstevel@tonic-gate 			return (NULL);
6214691Skcpoon 		sctp_conn_init(connp);
6223448Sdh155122 		sctps = ns->netstack_sctp;
6233448Sdh155122 		SCTP_G_Q_REFHOLD(sctps);
6243448Sdh155122 		netstack_hold(ns);
6253448Sdh155122 		connp->conn_netstack = ns;
6265240Snordmark 		return (connp);
6275240Snordmark 
6285240Snordmark 	case IPCL_TCPCONN:
6295240Snordmark 		conn_cache = tcp_conn_cache;
6300Sstevel@tonic-gate 		break;
6315240Snordmark 
6325240Snordmark 	case IPCL_UDPCONN:
6335240Snordmark 		conn_cache = udp_conn_cache;
6345240Snordmark 		break;
6355240Snordmark 
6365240Snordmark 	case IPCL_RAWIPCONN:
6375240Snordmark 		conn_cache = rawip_conn_cache;
6385240Snordmark 		break;
6395240Snordmark 
6405240Snordmark 	case IPCL_RTSCONN:
6415240Snordmark 		conn_cache = rts_conn_cache;
6425240Snordmark 		break;
6435240Snordmark 
6440Sstevel@tonic-gate 	case IPCL_IPCCONN:
6455240Snordmark 		conn_cache = ip_conn_cache;
6460Sstevel@tonic-gate 		break;
6475240Snordmark 
648741Smasputra 	default:
649741Smasputra 		connp = NULL;
650741Smasputra 		ASSERT(0);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6535240Snordmark 	if ((connp = kmem_cache_alloc(conn_cache, sleep)) == NULL)
6545240Snordmark 		return (NULL);
6555240Snordmark 
6565240Snordmark 	connp->conn_ref = 1;
6575240Snordmark 	netstack_hold(ns);
6585240Snordmark 	connp->conn_netstack = ns;
6595240Snordmark 	ipcl_globalhash_insert(connp);
6600Sstevel@tonic-gate 	return (connp);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate void
6640Sstevel@tonic-gate ipcl_conn_destroy(conn_t *connp)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	mblk_t	*mp;
6673448Sdh155122 	netstack_t	*ns = connp->conn_netstack;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&connp->conn_lock));
6700Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6710Sstevel@tonic-gate 	ASSERT(connp->conn_ire_cache == NULL);
6720Sstevel@tonic-gate 
6737502Saruna@cs.umn.edu 	DTRACE_PROBE1(conn__destroy, conn_t *, connp);
6747502Saruna@cs.umn.edu 
6751676Sjpk 	if (connp->conn_peercred != NULL &&
6761676Sjpk 	    connp->conn_peercred != connp->conn_cred)
6771676Sjpk 		crfree(connp->conn_peercred);
6781676Sjpk 	connp->conn_peercred = NULL;
6791676Sjpk 
6801676Sjpk 	if (connp->conn_cred != NULL) {
6811676Sjpk 		crfree(connp->conn_cred);
6821676Sjpk 		connp->conn_cred = NULL;
6831676Sjpk 	}
6841676Sjpk 
6850Sstevel@tonic-gate 	ipcl_globalhash_remove(connp);
6860Sstevel@tonic-gate 
6875240Snordmark 	/* FIXME: add separate tcp_conn_free()? */
6880Sstevel@tonic-gate 	if (connp->conn_flags & IPCL_TCPCONN) {
689741Smasputra 		tcp_t	*tcp = connp->conn_tcp;
6903448Sdh155122 		tcp_stack_t *tcps;
6913448Sdh155122 
6923448Sdh155122 		ASSERT(tcp != NULL);
6933448Sdh155122 		tcps = tcp->tcp_tcps;
6943448Sdh155122 		if (tcps != NULL) {
6953448Sdh155122 			if (connp->conn_latch != NULL) {
6963448Sdh155122 				IPLATCH_REFRELE(connp->conn_latch, ns);
6973448Sdh155122 				connp->conn_latch = NULL;
6983448Sdh155122 			}
6993448Sdh155122 			if (connp->conn_policy != NULL) {
7003448Sdh155122 				IPPH_REFRELE(connp->conn_policy, ns);
7013448Sdh155122 				connp->conn_policy = NULL;
7023448Sdh155122 			}
7033448Sdh155122 			tcp->tcp_tcps = NULL;
7043448Sdh155122 			TCPS_REFRELE(tcps);
7053448Sdh155122 		}
706741Smasputra 
7070Sstevel@tonic-gate 		tcp_free(tcp);
7080Sstevel@tonic-gate 		mp = tcp->tcp_timercache;
7091676Sjpk 		tcp->tcp_cred = NULL;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		if (tcp->tcp_sack_info != NULL) {
7120Sstevel@tonic-gate 			bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
7130Sstevel@tonic-gate 			kmem_cache_free(tcp_sack_info_cache,
7140Sstevel@tonic-gate 			    tcp->tcp_sack_info);
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 		if (tcp->tcp_iphc != NULL) {
7170Sstevel@tonic-gate 			if (tcp->tcp_hdr_grown) {
7180Sstevel@tonic-gate 				kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len);
7190Sstevel@tonic-gate 			} else {
7200Sstevel@tonic-gate 				bzero(tcp->tcp_iphc, tcp->tcp_iphc_len);
7210Sstevel@tonic-gate 				kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc);
7220Sstevel@tonic-gate 			}
7230Sstevel@tonic-gate 			tcp->tcp_iphc_len = 0;
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 		ASSERT(tcp->tcp_iphc_len == 0);
7260Sstevel@tonic-gate 
7278014SKacheong.Poon@Sun.COM 		/*
7288014SKacheong.Poon@Sun.COM 		 * tcp_rsrv_mp can be NULL if tcp_get_conn() fails to allocate
7298014SKacheong.Poon@Sun.COM 		 * the mblk.
7308014SKacheong.Poon@Sun.COM 		 */
7318014SKacheong.Poon@Sun.COM 		if (tcp->tcp_rsrv_mp != NULL) {
7328014SKacheong.Poon@Sun.COM 			freeb(tcp->tcp_rsrv_mp);
7338014SKacheong.Poon@Sun.COM 			tcp->tcp_rsrv_mp = NULL;
7348014SKacheong.Poon@Sun.COM 			mutex_destroy(&tcp->tcp_rsrv_mp_lock);
7358014SKacheong.Poon@Sun.COM 		}
7368014SKacheong.Poon@Sun.COM 
7373448Sdh155122 		ASSERT(connp->conn_latch == NULL);
7383448Sdh155122 		ASSERT(connp->conn_policy == NULL);
7393448Sdh155122 
7403448Sdh155122 		if (ns != NULL) {
7413448Sdh155122 			ASSERT(tcp->tcp_tcps == NULL);
7423448Sdh155122 			connp->conn_netstack = NULL;
7433448Sdh155122 			netstack_rele(ns);
7443448Sdh155122 		}
7455240Snordmark 
7465240Snordmark 		ipcl_conn_cleanup(connp);
7475240Snordmark 		connp->conn_flags = IPCL_TCPCONN;
7485240Snordmark 		bzero(tcp, sizeof (tcp_t));
7495240Snordmark 
7505240Snordmark 		tcp->tcp_timercache = mp;
7515240Snordmark 		tcp->tcp_connp = connp;
7525240Snordmark 		kmem_cache_free(tcp_conn_cache, connp);
7535240Snordmark 		return;
7545240Snordmark 	}
7555240Snordmark 	if (connp->conn_latch != NULL) {
7565240Snordmark 		IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack);
7575240Snordmark 		connp->conn_latch = NULL;
7585240Snordmark 	}
7595240Snordmark 	if (connp->conn_policy != NULL) {
7605240Snordmark 		IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
7615240Snordmark 		connp->conn_policy = NULL;
7625240Snordmark 	}
7635240Snordmark 	if (connp->conn_ipsec_opt_mp != NULL) {
7645240Snordmark 		freemsg(connp->conn_ipsec_opt_mp);
7655240Snordmark 		connp->conn_ipsec_opt_mp = NULL;
7665240Snordmark 	}
7675240Snordmark 
7685240Snordmark 	if (connp->conn_flags & IPCL_SCTPCONN) {
7693448Sdh155122 		ASSERT(ns != NULL);
7700Sstevel@tonic-gate 		sctp_free(connp);
7715240Snordmark 		return;
7725240Snordmark 	}
7735240Snordmark 
7745240Snordmark 	if (ns != NULL) {
7755240Snordmark 		connp->conn_netstack = NULL;
7765240Snordmark 		netstack_rele(ns);
7775240Snordmark 	}
7788348SEric.Yu@Sun.COM 
7795240Snordmark 	ipcl_conn_cleanup(connp);
7805240Snordmark 
7815240Snordmark 	/* leave conn_priv aka conn_udp, conn_icmp, etc in place. */
7825240Snordmark 	if (connp->conn_flags & IPCL_UDPCONN) {
7835240Snordmark 		connp->conn_flags = IPCL_UDPCONN;
7845240Snordmark 		kmem_cache_free(udp_conn_cache, connp);
7855240Snordmark 	} else if (connp->conn_flags & IPCL_RAWIPCONN) {
7868348SEric.Yu@Sun.COM 
7875240Snordmark 		connp->conn_flags = IPCL_RAWIPCONN;
7885240Snordmark 		connp->conn_ulp = IPPROTO_ICMP;
7895240Snordmark 		kmem_cache_free(rawip_conn_cache, connp);
7905240Snordmark 	} else if (connp->conn_flags & IPCL_RTSCONN) {
7915240Snordmark 		connp->conn_flags = IPCL_RTSCONN;
7925240Snordmark 		kmem_cache_free(rts_conn_cache, connp);
7930Sstevel@tonic-gate 	} else {
7945240Snordmark 		connp->conn_flags = IPCL_IPCCONN;
7955240Snordmark 		ASSERT(connp->conn_flags & IPCL_IPCCONN);
7965240Snordmark 		ASSERT(connp->conn_priv == NULL);
7975240Snordmark 		kmem_cache_free(ip_conn_cache, connp);
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate /*
8020Sstevel@tonic-gate  * Running in cluster mode - deregister listener information
8030Sstevel@tonic-gate  */
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate static void
8060Sstevel@tonic-gate ipcl_conn_unlisten(conn_t *connp)
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) != 0);
8090Sstevel@tonic-gate 	ASSERT(connp->conn_lport != 0);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	if (cl_inet_unlisten != NULL) {
8120Sstevel@tonic-gate 		sa_family_t	addr_family;
8130Sstevel@tonic-gate 		uint8_t		*laddrp;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 		if (connp->conn_pkt_isv6) {
8160Sstevel@tonic-gate 			addr_family = AF_INET6;
8170Sstevel@tonic-gate 			laddrp = (uint8_t *)&connp->conn_bound_source_v6;
8180Sstevel@tonic-gate 		} else {
8190Sstevel@tonic-gate 			addr_family = AF_INET;
8200Sstevel@tonic-gate 			laddrp = (uint8_t *)&connp->conn_bound_source;
8210Sstevel@tonic-gate 		}
822*8392SHuafeng.Lv@Sun.COM 		(*cl_inet_unlisten)(connp->conn_netstack->netstack_stackid,
823*8392SHuafeng.Lv@Sun.COM 		    IPPROTO_TCP, addr_family, laddrp, connp->conn_lport, NULL);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 	connp->conn_flags &= ~IPCL_CL_LISTENER;
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate /*
8290Sstevel@tonic-gate  * We set the IPCL_REMOVED flag (instead of clearing the flag indicating
8300Sstevel@tonic-gate  * which table the conn belonged to). So for debugging we can see which hash
8310Sstevel@tonic-gate  * table this connection was in.
8320Sstevel@tonic-gate  */
8330Sstevel@tonic-gate #define	IPCL_HASH_REMOVE(connp)	{					\
8340Sstevel@tonic-gate 	connf_t	*connfp = (connp)->conn_fanout;				\
8350Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&((connp)->conn_lock)));			\
8360Sstevel@tonic-gate 	if (connfp != NULL) {						\
8370Sstevel@tonic-gate 		IPCL_DEBUG_LVL(4, ("IPCL_HASH_REMOVE: connp %p",	\
8380Sstevel@tonic-gate 		    (void *)(connp)));					\
8390Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);			\
8400Sstevel@tonic-gate 		if ((connp)->conn_next != NULL)				\
8410Sstevel@tonic-gate 			(connp)->conn_next->conn_prev =			\
8420Sstevel@tonic-gate 			    (connp)->conn_prev;				\
8430Sstevel@tonic-gate 		if ((connp)->conn_prev != NULL)				\
8440Sstevel@tonic-gate 			(connp)->conn_prev->conn_next =			\
8450Sstevel@tonic-gate 			    (connp)->conn_next;				\
8460Sstevel@tonic-gate 		else							\
8470Sstevel@tonic-gate 			connfp->connf_head = (connp)->conn_next;	\
8480Sstevel@tonic-gate 		(connp)->conn_fanout = NULL;				\
8490Sstevel@tonic-gate 		(connp)->conn_next = NULL;				\
8500Sstevel@tonic-gate 		(connp)->conn_prev = NULL;				\
8510Sstevel@tonic-gate 		(connp)->conn_flags |= IPCL_REMOVED;			\
8520Sstevel@tonic-gate 		if (((connp)->conn_flags & IPCL_CL_LISTENER) != 0)	\
8530Sstevel@tonic-gate 			ipcl_conn_unlisten((connp));			\
8540Sstevel@tonic-gate 		CONN_DEC_REF((connp));					\
8550Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);			\
8560Sstevel@tonic-gate 	}								\
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate void
8600Sstevel@tonic-gate ipcl_hash_remove(conn_t *connp)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate 	IPCL_HASH_REMOVE(connp);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate  * The whole purpose of this function is allow removal of
8670Sstevel@tonic-gate  * a conn_t from the connected hash for timewait reclaim.
8680Sstevel@tonic-gate  * This is essentially a TW reclaim fastpath where timewait
8690Sstevel@tonic-gate  * collector checks under fanout lock (so no one else can
8700Sstevel@tonic-gate  * get access to the conn_t) that refcnt is 2 i.e. one for
8710Sstevel@tonic-gate  * TCP and one for the classifier hash list. If ref count
8720Sstevel@tonic-gate  * is indeed 2, we can just remove the conn under lock and
8730Sstevel@tonic-gate  * avoid cleaning up the conn under squeue. This gives us
8740Sstevel@tonic-gate  * improved performance.
8750Sstevel@tonic-gate  */
8760Sstevel@tonic-gate void
8770Sstevel@tonic-gate ipcl_hash_remove_locked(conn_t *connp, connf_t	*connfp)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connfp->connf_lock));
8800Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
8810Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) == 0);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if ((connp)->conn_next != NULL) {
8844691Skcpoon 		(connp)->conn_next->conn_prev = (connp)->conn_prev;
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 	if ((connp)->conn_prev != NULL) {
8874691Skcpoon 		(connp)->conn_prev->conn_next = (connp)->conn_next;
8880Sstevel@tonic-gate 	} else {
8890Sstevel@tonic-gate 		connfp->connf_head = (connp)->conn_next;
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 	(connp)->conn_fanout = NULL;
8920Sstevel@tonic-gate 	(connp)->conn_next = NULL;
8930Sstevel@tonic-gate 	(connp)->conn_prev = NULL;
8940Sstevel@tonic-gate 	(connp)->conn_flags |= IPCL_REMOVED;
8950Sstevel@tonic-gate 	ASSERT((connp)->conn_ref == 2);
8960Sstevel@tonic-gate 	(connp)->conn_ref--;
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp) {		\
9000Sstevel@tonic-gate 	ASSERT((connp)->conn_fanout == NULL);				\
9010Sstevel@tonic-gate 	ASSERT((connp)->conn_next == NULL);				\
9020Sstevel@tonic-gate 	ASSERT((connp)->conn_prev == NULL);				\
9030Sstevel@tonic-gate 	if ((connfp)->connf_head != NULL) {				\
9040Sstevel@tonic-gate 		(connfp)->connf_head->conn_prev = (connp);		\
9050Sstevel@tonic-gate 		(connp)->conn_next = (connfp)->connf_head;		\
9060Sstevel@tonic-gate 	}								\
9070Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9080Sstevel@tonic-gate 	(connfp)->connf_head = (connp);					\
9090Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9100Sstevel@tonic-gate 	    IPCL_CONNECTED;						\
9110Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED(connfp, connp) {			\
9150Sstevel@tonic-gate 	IPCL_DEBUG_LVL(8, ("IPCL_HASH_INSERT_CONNECTED: connfp %p "	\
9160Sstevel@tonic-gate 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
9170Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
9180Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
9190Sstevel@tonic-gate 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);		\
9200Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate #define	IPCL_HASH_INSERT_BOUND(connfp, connp) {				\
9240Sstevel@tonic-gate 	conn_t *pconnp = NULL, *nconnp;					\
9250Sstevel@tonic-gate 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_BOUND: connfp %p "	\
9260Sstevel@tonic-gate 	    "connp %p", (void *)connfp, (void *)(connp)));		\
9270Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
9280Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
9290Sstevel@tonic-gate 	nconnp = (connfp)->connf_head;					\
930153Sethindra 	while (nconnp != NULL &&					\
931153Sethindra 	    !_IPCL_V4_MATCH_ANY(nconnp->conn_srcv6)) {			\
932153Sethindra 		pconnp = nconnp;					\
933153Sethindra 		nconnp = nconnp->conn_next;				\
9340Sstevel@tonic-gate 	}								\
9350Sstevel@tonic-gate 	if (pconnp != NULL) {						\
9360Sstevel@tonic-gate 		pconnp->conn_next = (connp);				\
9370Sstevel@tonic-gate 		(connp)->conn_prev = pconnp;				\
9380Sstevel@tonic-gate 	} else {							\
9390Sstevel@tonic-gate 		(connfp)->connf_head = (connp);				\
9400Sstevel@tonic-gate 	}								\
9410Sstevel@tonic-gate 	if (nconnp != NULL) {						\
9420Sstevel@tonic-gate 		(connp)->conn_next = nconnp;				\
9430Sstevel@tonic-gate 		nconnp->conn_prev = (connp);				\
9440Sstevel@tonic-gate 	}								\
9450Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9460Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9470Sstevel@tonic-gate 	    IPCL_BOUND;							\
9480Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
9490Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate #define	IPCL_HASH_INSERT_WILDCARD(connfp, connp) {			\
9530Sstevel@tonic-gate 	conn_t **list, *prev, *next;					\
9540Sstevel@tonic-gate 	boolean_t isv4mapped =						\
9550Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&(connp)->conn_srcv6);			\
9560Sstevel@tonic-gate 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_WILDCARD: connfp %p "	\
9570Sstevel@tonic-gate 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
9580Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
9590Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
9600Sstevel@tonic-gate 	list = &(connfp)->connf_head;					\
9610Sstevel@tonic-gate 	prev = NULL;							\
9620Sstevel@tonic-gate 	while ((next = *list) != NULL) {				\
9630Sstevel@tonic-gate 		if (isv4mapped &&					\
9640Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&next->conn_srcv6) &&	\
9650Sstevel@tonic-gate 		    connp->conn_zoneid == next->conn_zoneid) {		\
9660Sstevel@tonic-gate 			(connp)->conn_next = next;			\
9670Sstevel@tonic-gate 			if (prev != NULL)				\
9680Sstevel@tonic-gate 				prev = next->conn_prev;			\
9690Sstevel@tonic-gate 			next->conn_prev = (connp);			\
9700Sstevel@tonic-gate 			break;						\
9710Sstevel@tonic-gate 		}							\
9720Sstevel@tonic-gate 		list = &next->conn_next;				\
9730Sstevel@tonic-gate 		prev = next;						\
9740Sstevel@tonic-gate 	}								\
9750Sstevel@tonic-gate 	(connp)->conn_prev = prev;					\
9760Sstevel@tonic-gate 	*list = (connp);						\
9770Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9780Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9790Sstevel@tonic-gate 	    IPCL_BOUND;							\
9800Sstevel@tonic-gate 	CONN_INC_REF((connp));						\
9810Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate void
9850Sstevel@tonic-gate ipcl_hash_insert_wildcard(connf_t *connfp, conn_t *connp)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate void
9910Sstevel@tonic-gate ipcl_proto_insert(conn_t *connp, uint8_t protocol)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 	connf_t	*connfp;
9943448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	ASSERT(connp != NULL);
9971676Sjpk 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
9981676Sjpk 	    protocol == IPPROTO_ESP);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	connp->conn_ulp = protocol;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	/* Insert it in the protocol hash */
10033448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[protocol];
10040Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate void
10080Sstevel@tonic-gate ipcl_proto_insert_v6(conn_t *connp, uint8_t protocol)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate 	connf_t	*connfp;
10113448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	ASSERT(connp != NULL);
10141676Sjpk 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
10151676Sjpk 	    protocol == IPPROTO_ESP);
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	connp->conn_ulp = protocol;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	/* Insert it in the Bind Hash */
10203448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
10210Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate /*
10250Sstevel@tonic-gate  * This function is used only for inserting SCTP raw socket now.
10260Sstevel@tonic-gate  * This may change later.
10270Sstevel@tonic-gate  *
10280Sstevel@tonic-gate  * Note that only one raw socket can be bound to a port.  The param
10290Sstevel@tonic-gate  * lport is in network byte order.
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate static int
10320Sstevel@tonic-gate ipcl_sctp_hash_insert(conn_t *connp, in_port_t lport)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	connf_t	*connfp;
10350Sstevel@tonic-gate 	conn_t	*oconnp;
10363448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
10370Sstevel@tonic-gate 
10383448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	/* Check for existing raw socket already bound to the port. */
10410Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
10420Sstevel@tonic-gate 	for (oconnp = connfp->connf_head; oconnp != NULL;
1043409Skcpoon 	    oconnp = oconnp->conn_next) {
10440Sstevel@tonic-gate 		if (oconnp->conn_lport == lport &&
10450Sstevel@tonic-gate 		    oconnp->conn_zoneid == connp->conn_zoneid &&
10460Sstevel@tonic-gate 		    oconnp->conn_af_isv6 == connp->conn_af_isv6 &&
10470Sstevel@tonic-gate 		    ((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
10480Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&oconnp->conn_srcv6) ||
10490Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6) ||
10500Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&oconnp->conn_srcv6)) ||
10510Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&oconnp->conn_srcv6,
10520Sstevel@tonic-gate 		    &connp->conn_srcv6))) {
10530Sstevel@tonic-gate 			break;
10540Sstevel@tonic-gate 		}
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
10570Sstevel@tonic-gate 	if (oconnp != NULL)
10580Sstevel@tonic-gate 		return (EADDRNOTAVAIL);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6) ||
10610Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_remv6)) {
10620Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
10630Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6)) {
10640Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10650Sstevel@tonic-gate 		} else {
10660Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
10670Sstevel@tonic-gate 		}
10680Sstevel@tonic-gate 	} else {
10690Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED(connfp, connp);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 	return (0);
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate /*
10751676Sjpk  * Check for a MAC exemption conflict on a labeled system.  Note that for
10761676Sjpk  * protocols that use port numbers (UDP, TCP, SCTP), we do this check up in the
10771676Sjpk  * transport layer.  This check is for binding all other protocols.
10781676Sjpk  *
10791676Sjpk  * Returns true if there's a conflict.
10801676Sjpk  */
10811676Sjpk static boolean_t
10823448Sdh155122 check_exempt_conflict_v4(conn_t *connp, ip_stack_t *ipst)
10831676Sjpk {
10841676Sjpk 	connf_t	*connfp;
10851676Sjpk 	conn_t *tconn;
10861676Sjpk 
10873448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[connp->conn_ulp];
10881676Sjpk 	mutex_enter(&connfp->connf_lock);
10891676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
10901676Sjpk 	    tconn = tconn->conn_next) {
10911676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
10921676Sjpk 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
10931676Sjpk 			continue;
10941676Sjpk 		/* If neither is exempt, then there's no conflict */
10951676Sjpk 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
10961676Sjpk 			continue;
10971676Sjpk 		/* If both are bound to different specific addrs, ok */
10981676Sjpk 		if (connp->conn_src != INADDR_ANY &&
10991676Sjpk 		    tconn->conn_src != INADDR_ANY &&
11001676Sjpk 		    connp->conn_src != tconn->conn_src)
11011676Sjpk 			continue;
11021676Sjpk 		/* These two conflict; fail */
11031676Sjpk 		break;
11041676Sjpk 	}
11051676Sjpk 	mutex_exit(&connfp->connf_lock);
11061676Sjpk 	return (tconn != NULL);
11071676Sjpk }
11081676Sjpk 
11091676Sjpk static boolean_t
11103448Sdh155122 check_exempt_conflict_v6(conn_t *connp, ip_stack_t *ipst)
11111676Sjpk {
11121676Sjpk 	connf_t	*connfp;
11131676Sjpk 	conn_t *tconn;
11141676Sjpk 
11153448Sdh155122 	connfp = &ipst->ips_ipcl_proto_fanout[connp->conn_ulp];
11161676Sjpk 	mutex_enter(&connfp->connf_lock);
11171676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
11181676Sjpk 	    tconn = tconn->conn_next) {
11191676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
11201676Sjpk 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
11211676Sjpk 			continue;
11221676Sjpk 		/* If neither is exempt, then there's no conflict */
11231676Sjpk 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
11241676Sjpk 			continue;
11251676Sjpk 		/* If both are bound to different addrs, ok */
11261676Sjpk 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) &&
11271676Sjpk 		    !IN6_IS_ADDR_UNSPECIFIED(&tconn->conn_srcv6) &&
11281676Sjpk 		    !IN6_ARE_ADDR_EQUAL(&connp->conn_srcv6, &tconn->conn_srcv6))
11291676Sjpk 			continue;
11301676Sjpk 		/* These two conflict; fail */
11311676Sjpk 		break;
11321676Sjpk 	}
11331676Sjpk 	mutex_exit(&connfp->connf_lock);
11341676Sjpk 	return (tconn != NULL);
11351676Sjpk }
11361676Sjpk 
11371676Sjpk /*
11380Sstevel@tonic-gate  * (v4, v6) bind hash insertion routines
11390Sstevel@tonic-gate  */
11400Sstevel@tonic-gate int
11410Sstevel@tonic-gate ipcl_bind_insert(conn_t *connp, uint8_t protocol, ipaddr_t src, uint16_t lport)
11420Sstevel@tonic-gate {
11430Sstevel@tonic-gate 	connf_t	*connfp;
11440Sstevel@tonic-gate #ifdef	IPCL_DEBUG
11450Sstevel@tonic-gate 	char	buf[INET_NTOA_BUFSIZE];
11460Sstevel@tonic-gate #endif
11470Sstevel@tonic-gate 	int	ret = 0;
11483448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	ASSERT(connp);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	IPCL_DEBUG_LVL(64, ("ipcl_bind_insert: connp %p, src = %s, "
11530Sstevel@tonic-gate 	    "port = %d\n", (void *)connp, inet_ntoa_r(src, buf), lport));
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	connp->conn_ulp = protocol;
11560Sstevel@tonic-gate 	IN6_IPADDR_TO_V4MAPPED(src, &connp->conn_srcv6);
11570Sstevel@tonic-gate 	connp->conn_lport = lport;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	switch (protocol) {
11601676Sjpk 	default:
11613448Sdh155122 		if (is_system_labeled() &&
11623448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
11631676Sjpk 			return (EADDRINUSE);
11641676Sjpk 		/* FALLTHROUGH */
11650Sstevel@tonic-gate 	case IPPROTO_UDP:
11660Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
11670Sstevel@tonic-gate 			IPCL_DEBUG_LVL(64,
11680Sstevel@tonic-gate 			    ("ipcl_bind_insert: connp %p - udp\n",
11690Sstevel@tonic-gate 			    (void *)connp));
11703448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
11713448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
11720Sstevel@tonic-gate 		} else {
11730Sstevel@tonic-gate 			IPCL_DEBUG_LVL(64,
11740Sstevel@tonic-gate 			    ("ipcl_bind_insert: connp %p - protocol\n",
11750Sstevel@tonic-gate 			    (void *)connp));
11763448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout[protocol];
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 		if (connp->conn_rem != INADDR_ANY) {
11800Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
11810Sstevel@tonic-gate 		} else if (connp->conn_src != INADDR_ANY) {
11820Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
11830Sstevel@tonic-gate 		} else {
11840Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
11850Sstevel@tonic-gate 		}
11860Sstevel@tonic-gate 		break;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	case IPPROTO_TCP:
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
11911676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
11923448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
11933448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
11940Sstevel@tonic-gate 		if (connp->conn_src != INADDR_ANY) {
11950Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
11960Sstevel@tonic-gate 		} else {
11970Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
11980Sstevel@tonic-gate 		}
11990Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
12000Sstevel@tonic-gate 			ASSERT(!connp->conn_pkt_isv6);
12010Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
1202*8392SHuafeng.Lv@Sun.COM 			(*cl_inet_listen)(
1203*8392SHuafeng.Lv@Sun.COM 			    connp->conn_netstack->netstack_stackid,
1204*8392SHuafeng.Lv@Sun.COM 			    IPPROTO_TCP, AF_INET,
1205*8392SHuafeng.Lv@Sun.COM 			    (uint8_t *)&connp->conn_bound_source, lport, NULL);
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 		break;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	case IPPROTO_SCTP:
12100Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
12110Sstevel@tonic-gate 		break;
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	return (ret);
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate int
12180Sstevel@tonic-gate ipcl_bind_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
12190Sstevel@tonic-gate     uint16_t lport)
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate 	connf_t	*connfp;
12220Sstevel@tonic-gate 	int	ret = 0;
12233448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	ASSERT(connp);
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	connp->conn_ulp = protocol;
12280Sstevel@tonic-gate 	connp->conn_srcv6 = *src;
12290Sstevel@tonic-gate 	connp->conn_lport = lport;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	switch (protocol) {
12321676Sjpk 	default:
12333448Sdh155122 		if (is_system_labeled() &&
12343448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
12351676Sjpk 			return (EADDRINUSE);
12361676Sjpk 		/* FALLTHROUGH */
12370Sstevel@tonic-gate 	case IPPROTO_UDP:
12380Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
12390Sstevel@tonic-gate 			IPCL_DEBUG_LVL(128,
12400Sstevel@tonic-gate 			    ("ipcl_bind_insert_v6: connp %p - udp\n",
12410Sstevel@tonic-gate 			    (void *)connp));
12423448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
12433448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
12440Sstevel@tonic-gate 		} else {
12450Sstevel@tonic-gate 			IPCL_DEBUG_LVL(128,
12460Sstevel@tonic-gate 			    ("ipcl_bind_insert_v6: connp %p - protocol\n",
12470Sstevel@tonic-gate 			    (void *)connp));
12483448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
12490Sstevel@tonic-gate 		}
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
12520Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
12530Sstevel@tonic-gate 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
12540Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12550Sstevel@tonic-gate 		} else {
12560Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12570Sstevel@tonic-gate 		}
12580Sstevel@tonic-gate 		break;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	case IPPROTO_TCP:
12610Sstevel@tonic-gate 		/* XXX - Need a separate table for IN6_IS_ADDR_UNSPECIFIED? */
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
12641676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
12653448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
12663448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
12670Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
12680Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12690Sstevel@tonic-gate 		} else {
12700Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12710Sstevel@tonic-gate 		}
12720Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
12730Sstevel@tonic-gate 			sa_family_t	addr_family;
12740Sstevel@tonic-gate 			uint8_t		*laddrp;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 			if (connp->conn_pkt_isv6) {
12770Sstevel@tonic-gate 				addr_family = AF_INET6;
12780Sstevel@tonic-gate 				laddrp =
12790Sstevel@tonic-gate 				    (uint8_t *)&connp->conn_bound_source_v6;
12800Sstevel@tonic-gate 			} else {
12810Sstevel@tonic-gate 				addr_family = AF_INET;
12820Sstevel@tonic-gate 				laddrp = (uint8_t *)&connp->conn_bound_source;
12830Sstevel@tonic-gate 			}
12840Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
1285*8392SHuafeng.Lv@Sun.COM 			(*cl_inet_listen)(
1286*8392SHuafeng.Lv@Sun.COM 			    connp->conn_netstack->netstack_stackid,
1287*8392SHuafeng.Lv@Sun.COM 			    IPPROTO_TCP, addr_family, laddrp, lport, NULL);
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 		break;
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	case IPPROTO_SCTP:
12920Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
12930Sstevel@tonic-gate 		break;
12940Sstevel@tonic-gate 	}
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	return (ret);
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate  * ipcl_conn_hash insertion routines.
13010Sstevel@tonic-gate  */
13020Sstevel@tonic-gate int
13030Sstevel@tonic-gate ipcl_conn_insert(conn_t *connp, uint8_t protocol, ipaddr_t src,
13040Sstevel@tonic-gate     ipaddr_t rem, uint32_t ports)
13050Sstevel@tonic-gate {
13060Sstevel@tonic-gate 	connf_t		*connfp;
13070Sstevel@tonic-gate 	uint16_t	*up;
13080Sstevel@tonic-gate 	conn_t		*tconnp;
13090Sstevel@tonic-gate #ifdef	IPCL_DEBUG
13100Sstevel@tonic-gate 	char	sbuf[INET_NTOA_BUFSIZE], rbuf[INET_NTOA_BUFSIZE];
13110Sstevel@tonic-gate #endif
13120Sstevel@tonic-gate 	in_port_t	lport;
13130Sstevel@tonic-gate 	int		ret = 0;
13143448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	IPCL_DEBUG_LVL(256, ("ipcl_conn_insert: connp %p, src = %s, "
13170Sstevel@tonic-gate 	    "dst = %s, ports = %x, protocol = %x", (void *)connp,
13180Sstevel@tonic-gate 	    inet_ntoa_r(src, sbuf), inet_ntoa_r(rem, rbuf),
13190Sstevel@tonic-gate 	    ports, protocol));
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	switch (protocol) {
13220Sstevel@tonic-gate 	case IPPROTO_TCP:
13230Sstevel@tonic-gate 		if (!(connp->conn_flags & IPCL_EAGER)) {
13240Sstevel@tonic-gate 			/*
13250Sstevel@tonic-gate 			 * for a eager connection, i.e connections which
13260Sstevel@tonic-gate 			 * have just been created, the initialization is
13270Sstevel@tonic-gate 			 * already done in ip at conn_creation time, so
13280Sstevel@tonic-gate 			 * we can skip the checks here.
13290Sstevel@tonic-gate 			 */
13300Sstevel@tonic-gate 			IPCL_CONN_INIT(connp, protocol, src, rem, ports);
13310Sstevel@tonic-gate 		}
13323448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
13333448Sdh155122 		    IPCL_CONN_HASH(connp->conn_rem,
13343448Sdh155122 		    connp->conn_ports, ipst)];
13350Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
13360Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
13370Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
13380Sstevel@tonic-gate 			if (IPCL_CONN_MATCH(tconnp, connp->conn_ulp,
13390Sstevel@tonic-gate 			    connp->conn_rem, connp->conn_src,
13400Sstevel@tonic-gate 			    connp->conn_ports)) {
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 				/* Already have a conn. bail out */
13430Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
13440Sstevel@tonic-gate 				return (EADDRINUSE);
13450Sstevel@tonic-gate 			}
13460Sstevel@tonic-gate 		}
13470Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
13480Sstevel@tonic-gate 			/*
13490Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
13500Sstevel@tonic-gate 			 * rebind. Let it happen.
13510Sstevel@tonic-gate 			 */
13520Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
13530Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
13540Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
13550Sstevel@tonic-gate 		}
13563104Sjprakash 
13573104Sjprakash 		ASSERT(connp->conn_recv != NULL);
13583104Sjprakash 
13590Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
13600Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
13610Sstevel@tonic-gate 		break;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	case IPPROTO_SCTP:
1364409Skcpoon 		/*
1365409Skcpoon 		 * The raw socket may have already been bound, remove it
1366409Skcpoon 		 * from the hash first.
1367409Skcpoon 		 */
1368409Skcpoon 		IPCL_HASH_REMOVE(connp);
1369409Skcpoon 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
13700Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
13710Sstevel@tonic-gate 		break;
13720Sstevel@tonic-gate 
13731676Sjpk 	default:
13741676Sjpk 		/*
13751676Sjpk 		 * Check for conflicts among MAC exempt bindings.  For
13761676Sjpk 		 * transports with port numbers, this is done by the upper
13771676Sjpk 		 * level per-transport binding logic.  For all others, it's
13781676Sjpk 		 * done here.
13791676Sjpk 		 */
13803448Sdh155122 		if (is_system_labeled() &&
13813448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
13821676Sjpk 			return (EADDRINUSE);
13831676Sjpk 		/* FALLTHROUGH */
13841676Sjpk 
13850Sstevel@tonic-gate 	case IPPROTO_UDP:
13860Sstevel@tonic-gate 		up = (uint16_t *)&ports;
13870Sstevel@tonic-gate 		IPCL_CONN_INIT(connp, protocol, src, rem, ports);
13880Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
13893448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
13903448Sdh155122 			    IPCL_UDP_HASH(up[1], ipst)];
13910Sstevel@tonic-gate 		} else {
13923448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout[protocol];
13930Sstevel@tonic-gate 		}
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		if (connp->conn_rem != INADDR_ANY) {
13960Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
13970Sstevel@tonic-gate 		} else if (connp->conn_src != INADDR_ANY) {
13980Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
13990Sstevel@tonic-gate 		} else {
14000Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
14010Sstevel@tonic-gate 		}
14020Sstevel@tonic-gate 		break;
14030Sstevel@tonic-gate 	}
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	return (ret);
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate int
14090Sstevel@tonic-gate ipcl_conn_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
14100Sstevel@tonic-gate     const in6_addr_t *rem, uint32_t ports, uint_t ifindex)
14110Sstevel@tonic-gate {
14120Sstevel@tonic-gate 	connf_t		*connfp;
14130Sstevel@tonic-gate 	uint16_t	*up;
14140Sstevel@tonic-gate 	conn_t		*tconnp;
14150Sstevel@tonic-gate 	in_port_t	lport;
14160Sstevel@tonic-gate 	int		ret = 0;
14173448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	switch (protocol) {
14200Sstevel@tonic-gate 	case IPPROTO_TCP:
14210Sstevel@tonic-gate 		/* Just need to insert a conn struct */
14220Sstevel@tonic-gate 		if (!(connp->conn_flags & IPCL_EAGER)) {
14230Sstevel@tonic-gate 			IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
14240Sstevel@tonic-gate 		}
14253448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
14263448Sdh155122 		    IPCL_CONN_HASH_V6(connp->conn_remv6, connp->conn_ports,
14273448Sdh155122 		    ipst)];
14280Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
14290Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
14300Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
14310Sstevel@tonic-gate 			if (IPCL_CONN_MATCH_V6(tconnp, connp->conn_ulp,
14320Sstevel@tonic-gate 			    connp->conn_remv6, connp->conn_srcv6,
14330Sstevel@tonic-gate 			    connp->conn_ports) &&
14340Sstevel@tonic-gate 			    (tconnp->conn_tcp->tcp_bound_if == 0 ||
14350Sstevel@tonic-gate 			    tconnp->conn_tcp->tcp_bound_if == ifindex)) {
14360Sstevel@tonic-gate 				/* Already have a conn. bail out */
14370Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
14380Sstevel@tonic-gate 				return (EADDRINUSE);
14390Sstevel@tonic-gate 			}
14400Sstevel@tonic-gate 		}
14410Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
14420Sstevel@tonic-gate 			/*
14430Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
14440Sstevel@tonic-gate 			 * rebind. Let it happen.
14450Sstevel@tonic-gate 			 */
14460Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
14470Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
14480Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
14490Sstevel@tonic-gate 		}
14500Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
14510Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
14520Sstevel@tonic-gate 		break;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	case IPPROTO_SCTP:
1455409Skcpoon 		IPCL_HASH_REMOVE(connp);
1456409Skcpoon 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
14570Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
14580Sstevel@tonic-gate 		break;
14590Sstevel@tonic-gate 
14601676Sjpk 	default:
14613448Sdh155122 		if (is_system_labeled() &&
14623448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
14631676Sjpk 			return (EADDRINUSE);
14641676Sjpk 		/* FALLTHROUGH */
14650Sstevel@tonic-gate 	case IPPROTO_UDP:
14660Sstevel@tonic-gate 		up = (uint16_t *)&ports;
14670Sstevel@tonic-gate 		IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
14680Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
14693448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
14703448Sdh155122 			    IPCL_UDP_HASH(up[1], ipst)];
14710Sstevel@tonic-gate 		} else {
14723448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
14730Sstevel@tonic-gate 		}
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
14760Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
14770Sstevel@tonic-gate 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
14780Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
14790Sstevel@tonic-gate 		} else {
14800Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
14810Sstevel@tonic-gate 		}
14820Sstevel@tonic-gate 		break;
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	return (ret);
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate /*
14890Sstevel@tonic-gate  * v4 packet classifying function. looks up the fanout table to
14900Sstevel@tonic-gate  * find the conn, the packet belongs to. returns the conn with
14910Sstevel@tonic-gate  * the reference held, null otherwise.
14921676Sjpk  *
14931676Sjpk  * If zoneid is ALL_ZONES, then the search rules described in the "Connection
14941676Sjpk  * Lookup" comment block are applied.  Labels are also checked as described
14951676Sjpk  * above.  If the packet is from the inside (looped back), and is from the same
14961676Sjpk  * zone, then label checks are omitted.
14970Sstevel@tonic-gate  */
14980Sstevel@tonic-gate conn_t *
14993448Sdh155122 ipcl_classify_v4(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid,
15003448Sdh155122     ip_stack_t *ipst)
15010Sstevel@tonic-gate {
15020Sstevel@tonic-gate 	ipha_t	*ipha;
15030Sstevel@tonic-gate 	connf_t	*connfp, *bind_connfp;
15040Sstevel@tonic-gate 	uint16_t lport;
15050Sstevel@tonic-gate 	uint16_t fport;
15060Sstevel@tonic-gate 	uint32_t ports;
15070Sstevel@tonic-gate 	conn_t	*connp;
15080Sstevel@tonic-gate 	uint16_t  *up;
15091676Sjpk 	boolean_t shared_addr;
15101676Sjpk 	boolean_t unlabeled;
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
15130Sstevel@tonic-gate 	up = (uint16_t *)((uchar_t *)ipha + hdr_len + TCP_PORTS_OFFSET);
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	switch (protocol) {
15160Sstevel@tonic-gate 	case IPPROTO_TCP:
15170Sstevel@tonic-gate 		ports = *(uint32_t *)up;
15180Sstevel@tonic-gate 		connfp =
15193448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_src,
15203448Sdh155122 		    ports, ipst)];
15210Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
15220Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
15230Sstevel@tonic-gate 		    connp = connp->conn_next) {
15240Sstevel@tonic-gate 			if (IPCL_CONN_MATCH(connp, protocol,
15250Sstevel@tonic-gate 			    ipha->ipha_src, ipha->ipha_dst, ports))
15260Sstevel@tonic-gate 				break;
15270Sstevel@tonic-gate 		}
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 		if (connp != NULL) {
15301676Sjpk 			/*
15311676Sjpk 			 * We have a fully-bound TCP connection.
15321676Sjpk 			 *
15331676Sjpk 			 * For labeled systems, there's no need to check the
15341676Sjpk 			 * label here.  It's known to be good as we checked
15351676Sjpk 			 * before allowing the connection to become bound.
15361676Sjpk 			 */
15370Sstevel@tonic-gate 			CONN_INC_REF(connp);
15380Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
15390Sstevel@tonic-gate 			return (connp);
15400Sstevel@tonic-gate 		}
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 		lport = up[1];
15451676Sjpk 		unlabeled = B_FALSE;
15461676Sjpk 		/* Cred cannot be null on IPv4 */
15471676Sjpk 		if (is_system_labeled())
15481676Sjpk 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
15491676Sjpk 			    TSLF_UNLABELED) != 0;
15501676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
15511676Sjpk 		if (shared_addr) {
15523448Sdh155122 			/*
15533448Sdh155122 			 * No need to handle exclusive-stack zones since
15543448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
15553448Sdh155122 			 */
15561676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
15571676Sjpk 			/*
15581676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
15591676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
15601676Sjpk 			 * search for the zone based on the packet label.
15611676Sjpk 			 *
15621676Sjpk 			 * If there is such a zone, we prefer to find a
15631676Sjpk 			 * connection in it.  Otherwise, we look for a
15641676Sjpk 			 * MAC-exempt connection in any zone whose label
15651676Sjpk 			 * dominates the default label on the packet.
15661676Sjpk 			 */
15671676Sjpk 			if (zoneid == ALL_ZONES)
15681676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
15691676Sjpk 			else
15701676Sjpk 				unlabeled = B_FALSE;
15711676Sjpk 		}
15721676Sjpk 
15733448Sdh155122 		bind_connfp =
15743448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
15750Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
15760Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
15770Sstevel@tonic-gate 		    connp = connp->conn_next) {
15781676Sjpk 			if (IPCL_BIND_MATCH(connp, protocol, ipha->ipha_dst,
15792263Ssommerfe 			    lport) && (IPCL_ZONE_MATCH(connp, zoneid) ||
15801676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
15810Sstevel@tonic-gate 				break;
15820Sstevel@tonic-gate 		}
15830Sstevel@tonic-gate 
15841676Sjpk 		/*
15851676Sjpk 		 * If the matching connection is SLP on a private address, then
15861676Sjpk 		 * the label on the packet must match the local zone's label.
15871676Sjpk 		 * Otherwise, it must be in the label range defined by tnrh.
15881676Sjpk 		 * This is ensured by tsol_receive_label.
15891676Sjpk 		 */
15901676Sjpk 		if (connp != NULL && is_system_labeled() &&
15911676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
15921676Sjpk 		    shared_addr, connp)) {
15931676Sjpk 				DTRACE_PROBE3(
15941676Sjpk 				    tx__ip__log__info__classify__tcp,
15951676Sjpk 				    char *,
15961676Sjpk 				    "connp(1) could not receive mp(2)",
15971676Sjpk 				    conn_t *, connp, mblk_t *, mp);
15981676Sjpk 			connp = NULL;
15991676Sjpk 		}
16001676Sjpk 
16010Sstevel@tonic-gate 		if (connp != NULL) {
16021676Sjpk 			/* Have a listener at least */
16030Sstevel@tonic-gate 			CONN_INC_REF(connp);
16040Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
16050Sstevel@tonic-gate 			return (connp);
16060Sstevel@tonic-gate 		}
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
16110Sstevel@tonic-gate 		    ("ipcl_classify: couldn't classify mp = %p\n",
16120Sstevel@tonic-gate 		    (void *)mp));
16130Sstevel@tonic-gate 		break;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	case IPPROTO_UDP:
16160Sstevel@tonic-gate 		lport = up[1];
16171676Sjpk 		unlabeled = B_FALSE;
16181676Sjpk 		/* Cred cannot be null on IPv4 */
16191676Sjpk 		if (is_system_labeled())
16201676Sjpk 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
16211676Sjpk 			    TSLF_UNLABELED) != 0;
16221676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
16231676Sjpk 		if (shared_addr) {
16243448Sdh155122 			/*
16253448Sdh155122 			 * No need to handle exclusive-stack zones since
16263448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
16273448Sdh155122 			 */
16281676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
16291676Sjpk 			/*
16301676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
16311676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
16321676Sjpk 			 * search for the zone based on the packet label.
16331676Sjpk 			 *
16341676Sjpk 			 * If there is such a zone, we prefer to find a
16351676Sjpk 			 * connection in it.  Otherwise, we look for a
16361676Sjpk 			 * MAC-exempt connection in any zone whose label
16371676Sjpk 			 * dominates the default label on the packet.
16381676Sjpk 			 */
16391676Sjpk 			if (zoneid == ALL_ZONES)
16401676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
16411676Sjpk 			else
16421676Sjpk 				unlabeled = B_FALSE;
16431676Sjpk 		}
16440Sstevel@tonic-gate 		fport = up[0];
16450Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify %x %x", lport, fport));
16463448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
16470Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16480Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16490Sstevel@tonic-gate 		    connp = connp->conn_next) {
16500Sstevel@tonic-gate 			if (IPCL_UDP_MATCH(connp, lport, ipha->ipha_dst,
16510Sstevel@tonic-gate 			    fport, ipha->ipha_src) &&
16522263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
16531676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
16540Sstevel@tonic-gate 				break;
16550Sstevel@tonic-gate 		}
16560Sstevel@tonic-gate 
16571676Sjpk 		if (connp != NULL && is_system_labeled() &&
16581676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
16591676Sjpk 		    shared_addr, connp)) {
16601676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp,
16611676Sjpk 			    char *, "connp(1) could not receive mp(2)",
16621676Sjpk 			    conn_t *, connp, mblk_t *, mp);
16631676Sjpk 			connp = NULL;
16641676Sjpk 		}
16651676Sjpk 
16660Sstevel@tonic-gate 		if (connp != NULL) {
16670Sstevel@tonic-gate 			CONN_INC_REF(connp);
16680Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
16690Sstevel@tonic-gate 			return (connp);
16700Sstevel@tonic-gate 		}
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 		/*
16730Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
16740Sstevel@tonic-gate 		 */
16750Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
16760Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
16770Sstevel@tonic-gate 		    ("ipcl_classify: cant find udp conn_t for ports : %x %x",
16780Sstevel@tonic-gate 		    lport, fport));
16790Sstevel@tonic-gate 		break;
16800Sstevel@tonic-gate 	}
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	return (NULL);
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate conn_t *
16863448Sdh155122 ipcl_classify_v6(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid,
16873448Sdh155122     ip_stack_t *ipst)
16880Sstevel@tonic-gate {
16890Sstevel@tonic-gate 	ip6_t		*ip6h;
16900Sstevel@tonic-gate 	connf_t		*connfp, *bind_connfp;
16910Sstevel@tonic-gate 	uint16_t	lport;
16920Sstevel@tonic-gate 	uint16_t	fport;
16930Sstevel@tonic-gate 	tcph_t		*tcph;
16940Sstevel@tonic-gate 	uint32_t	ports;
16950Sstevel@tonic-gate 	conn_t		*connp;
16960Sstevel@tonic-gate 	uint16_t	*up;
16971676Sjpk 	boolean_t	shared_addr;
16981676Sjpk 	boolean_t	unlabeled;
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	switch (protocol) {
17030Sstevel@tonic-gate 	case IPPROTO_TCP:
17040Sstevel@tonic-gate 		tcph = (tcph_t *)&mp->b_rptr[hdr_len];
17050Sstevel@tonic-gate 		up = (uint16_t *)tcph->th_lport;
17060Sstevel@tonic-gate 		ports = *(uint32_t *)up;
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 		connfp =
17093448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_src,
17103448Sdh155122 		    ports, ipst)];
17110Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
17120Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
17130Sstevel@tonic-gate 		    connp = connp->conn_next) {
17140Sstevel@tonic-gate 			if (IPCL_CONN_MATCH_V6(connp, protocol,
17150Sstevel@tonic-gate 			    ip6h->ip6_src, ip6h->ip6_dst, ports))
17160Sstevel@tonic-gate 				break;
17170Sstevel@tonic-gate 		}
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 		if (connp != NULL) {
17201676Sjpk 			/*
17211676Sjpk 			 * We have a fully-bound TCP connection.
17221676Sjpk 			 *
17231676Sjpk 			 * For labeled systems, there's no need to check the
17241676Sjpk 			 * label here.  It's known to be good as we checked
17251676Sjpk 			 * before allowing the connection to become bound.
17261676Sjpk 			 */
17270Sstevel@tonic-gate 			CONN_INC_REF(connp);
17280Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
17290Sstevel@tonic-gate 			return (connp);
17300Sstevel@tonic-gate 		}
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 		lport = up[1];
17351676Sjpk 		unlabeled = B_FALSE;
17361676Sjpk 		/* Cred can be null on IPv6 */
17371676Sjpk 		if (is_system_labeled()) {
17381676Sjpk 			cred_t *cr = DB_CRED(mp);
17391676Sjpk 
17401676Sjpk 			unlabeled = (cr != NULL &&
17411676Sjpk 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
17421676Sjpk 		}
17431676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
17441676Sjpk 		if (shared_addr) {
17453448Sdh155122 			/*
17463448Sdh155122 			 * No need to handle exclusive-stack zones since
17473448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
17483448Sdh155122 			 */
17491676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
17501676Sjpk 			/*
17511676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
17521676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
17531676Sjpk 			 * search for the zone based on the packet label.
17541676Sjpk 			 *
17551676Sjpk 			 * If there is such a zone, we prefer to find a
17561676Sjpk 			 * connection in it.  Otherwise, we look for a
17571676Sjpk 			 * MAC-exempt connection in any zone whose label
17581676Sjpk 			 * dominates the default label on the packet.
17591676Sjpk 			 */
17601676Sjpk 			if (zoneid == ALL_ZONES)
17611676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
17621676Sjpk 			else
17631676Sjpk 				unlabeled = B_FALSE;
17641676Sjpk 		}
17651676Sjpk 
17663448Sdh155122 		bind_connfp =
17673448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
17680Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
17690Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
17700Sstevel@tonic-gate 		    connp = connp->conn_next) {
17710Sstevel@tonic-gate 			if (IPCL_BIND_MATCH_V6(connp, protocol,
17720Sstevel@tonic-gate 			    ip6h->ip6_dst, lport) &&
17732263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
17741676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
17750Sstevel@tonic-gate 				break;
17760Sstevel@tonic-gate 		}
17770Sstevel@tonic-gate 
17781676Sjpk 		if (connp != NULL && is_system_labeled() &&
17791676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
17801676Sjpk 		    shared_addr, connp)) {
17811676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp6,
17821676Sjpk 			    char *, "connp(1) could not receive mp(2)",
17831676Sjpk 			    conn_t *, connp, mblk_t *, mp);
17841676Sjpk 			connp = NULL;
17851676Sjpk 		}
17861676Sjpk 
17870Sstevel@tonic-gate 		if (connp != NULL) {
17880Sstevel@tonic-gate 			/* Have a listner at least */
17890Sstevel@tonic-gate 			CONN_INC_REF(connp);
17900Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
17910Sstevel@tonic-gate 			IPCL_DEBUG_LVL(512,
17920Sstevel@tonic-gate 			    ("ipcl_classify_v6: found listner "
17930Sstevel@tonic-gate 			    "connp = %p\n", (void *)connp));
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 			return (connp);
17960Sstevel@tonic-gate 		}
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
18010Sstevel@tonic-gate 		    ("ipcl_classify_v6: couldn't classify mp = %p\n",
18020Sstevel@tonic-gate 		    (void *)mp));
18030Sstevel@tonic-gate 		break;
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	case IPPROTO_UDP:
18060Sstevel@tonic-gate 		up = (uint16_t *)&mp->b_rptr[hdr_len];
18070Sstevel@tonic-gate 		lport = up[1];
18081676Sjpk 		unlabeled = B_FALSE;
18091676Sjpk 		/* Cred can be null on IPv6 */
18101676Sjpk 		if (is_system_labeled()) {
18111676Sjpk 			cred_t *cr = DB_CRED(mp);
18121676Sjpk 
18131676Sjpk 			unlabeled = (cr != NULL &&
18141676Sjpk 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
18151676Sjpk 		}
18161676Sjpk 		shared_addr = (zoneid == ALL_ZONES);
18171676Sjpk 		if (shared_addr) {
18183448Sdh155122 			/*
18193448Sdh155122 			 * No need to handle exclusive-stack zones since
18203448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
18213448Sdh155122 			 */
18221676Sjpk 			zoneid = tsol_mlp_findzone(protocol, lport);
18231676Sjpk 			/*
18241676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
18251676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
18261676Sjpk 			 * search for the zone based on the packet label.
18271676Sjpk 			 *
18281676Sjpk 			 * If there is such a zone, we prefer to find a
18291676Sjpk 			 * connection in it.  Otherwise, we look for a
18301676Sjpk 			 * MAC-exempt connection in any zone whose label
18311676Sjpk 			 * dominates the default label on the packet.
18321676Sjpk 			 */
18331676Sjpk 			if (zoneid == ALL_ZONES)
18341676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
18351676Sjpk 			else
18361676Sjpk 				unlabeled = B_FALSE;
18371676Sjpk 		}
18381676Sjpk 
18390Sstevel@tonic-gate 		fport = up[0];
18400Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify_v6 %x %x", lport,
18410Sstevel@tonic-gate 		    fport));
18423448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
18430Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
18440Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
18450Sstevel@tonic-gate 		    connp = connp->conn_next) {
18460Sstevel@tonic-gate 			if (IPCL_UDP_MATCH_V6(connp, lport, ip6h->ip6_dst,
18470Sstevel@tonic-gate 			    fport, ip6h->ip6_src) &&
18482263Ssommerfe 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
18491676Sjpk 			    (unlabeled && connp->conn_mac_exempt)))
18500Sstevel@tonic-gate 				break;
18510Sstevel@tonic-gate 		}
18520Sstevel@tonic-gate 
18531676Sjpk 		if (connp != NULL && is_system_labeled() &&
18541676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
18551676Sjpk 		    shared_addr, connp)) {
18561676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp6,
18571676Sjpk 			    char *, "connp(1) could not receive mp(2)",
18581676Sjpk 			    conn_t *, connp, mblk_t *, mp);
18591676Sjpk 			connp = NULL;
18601676Sjpk 		}
18611676Sjpk 
18620Sstevel@tonic-gate 		if (connp != NULL) {
18630Sstevel@tonic-gate 			CONN_INC_REF(connp);
18640Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
18650Sstevel@tonic-gate 			return (connp);
18660Sstevel@tonic-gate 		}
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 		/*
18690Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
18700Sstevel@tonic-gate 		 */
18710Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
18720Sstevel@tonic-gate 		IPCL_DEBUG_LVL(512,
18730Sstevel@tonic-gate 		    ("ipcl_classify_v6: cant find udp conn_t for ports : %x %x",
18740Sstevel@tonic-gate 		    lport, fport));
18750Sstevel@tonic-gate 		break;
18760Sstevel@tonic-gate 	}
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	return (NULL);
18790Sstevel@tonic-gate }
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate /*
18820Sstevel@tonic-gate  * wrapper around ipcl_classify_(v4,v6) routines.
18830Sstevel@tonic-gate  */
18840Sstevel@tonic-gate conn_t *
18853448Sdh155122 ipcl_classify(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst)
18860Sstevel@tonic-gate {
18870Sstevel@tonic-gate 	uint16_t	hdr_len;
18880Sstevel@tonic-gate 	ipha_t		*ipha;
18890Sstevel@tonic-gate 	uint8_t		*nexthdrp;
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (ipha_t))
18920Sstevel@tonic-gate 		return (NULL);
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	switch (IPH_HDR_VERSION(mp->b_rptr)) {
18950Sstevel@tonic-gate 	case IPV4_VERSION:
18960Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
18970Sstevel@tonic-gate 		hdr_len = IPH_HDR_LENGTH(ipha);
18980Sstevel@tonic-gate 		return (ipcl_classify_v4(mp, ipha->ipha_protocol, hdr_len,
18993448Sdh155122 		    zoneid, ipst));
19000Sstevel@tonic-gate 	case IPV6_VERSION:
19010Sstevel@tonic-gate 		if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr,
19020Sstevel@tonic-gate 		    &hdr_len, &nexthdrp))
19030Sstevel@tonic-gate 			return (NULL);
19040Sstevel@tonic-gate 
19053448Sdh155122 		return (ipcl_classify_v6(mp, *nexthdrp, hdr_len, zoneid, ipst));
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	return (NULL);
19090Sstevel@tonic-gate }
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate conn_t *
19121676Sjpk ipcl_classify_raw(mblk_t *mp, uint8_t protocol, zoneid_t zoneid,
19133448Sdh155122     uint32_t ports, ipha_t *hdr, ip_stack_t *ipst)
19140Sstevel@tonic-gate {
19151676Sjpk 	connf_t		*connfp;
19160Sstevel@tonic-gate 	conn_t		*connp;
19170Sstevel@tonic-gate 	in_port_t	lport;
19180Sstevel@tonic-gate 	int		af;
19191676Sjpk 	boolean_t	shared_addr;
19201676Sjpk 	boolean_t	unlabeled;
19211676Sjpk 	const void	*dst;
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	lport = ((uint16_t *)&ports)[1];
19241676Sjpk 
19251676Sjpk 	unlabeled = B_FALSE;
19261676Sjpk 	/* Cred can be null on IPv6 */
19271676Sjpk 	if (is_system_labeled()) {
19281676Sjpk 		cred_t *cr = DB_CRED(mp);
19291676Sjpk 
19301676Sjpk 		unlabeled = (cr != NULL &&
19311676Sjpk 		    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
19321676Sjpk 	}
19331676Sjpk 	shared_addr = (zoneid == ALL_ZONES);
19341676Sjpk 	if (shared_addr) {
19353448Sdh155122 		/*
19363448Sdh155122 		 * No need to handle exclusive-stack zones since ALL_ZONES
19373448Sdh155122 		 * only applies to the shared stack.
19383448Sdh155122 		 */
19391676Sjpk 		zoneid = tsol_mlp_findzone(protocol, lport);
19401676Sjpk 		/*
19411676Sjpk 		 * If no shared MLP is found, tsol_mlp_findzone returns
19421676Sjpk 		 * ALL_ZONES.  In that case, we assume it's SLP, and search for
19431676Sjpk 		 * the zone based on the packet label.
19441676Sjpk 		 *
19451676Sjpk 		 * If there is such a zone, we prefer to find a connection in
19461676Sjpk 		 * it.  Otherwise, we look for a MAC-exempt connection in any
19471676Sjpk 		 * zone whose label dominates the default label on the packet.
19481676Sjpk 		 */
19491676Sjpk 		if (zoneid == ALL_ZONES)
19501676Sjpk 			zoneid = tsol_packet_to_zoneid(mp);
19511676Sjpk 		else
19521676Sjpk 			unlabeled = B_FALSE;
19531676Sjpk 	}
19541676Sjpk 
19550Sstevel@tonic-gate 	af = IPH_HDR_VERSION(hdr);
19561676Sjpk 	dst = af == IPV4_VERSION ? (const void *)&hdr->ipha_dst :
19571676Sjpk 	    (const void *)&((ip6_t *)hdr)->ip6_dst;
19583448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
19610Sstevel@tonic-gate 	for (connp = connfp->connf_head; connp != NULL;
19620Sstevel@tonic-gate 	    connp = connp->conn_next) {
19630Sstevel@tonic-gate 		/* We don't allow v4 fallback for v6 raw socket. */
19641676Sjpk 		if (af == (connp->conn_af_isv6 ? IPV4_VERSION :
19651676Sjpk 		    IPV6_VERSION))
19660Sstevel@tonic-gate 			continue;
19670Sstevel@tonic-gate 		if (connp->conn_fully_bound) {
19680Sstevel@tonic-gate 			if (af == IPV4_VERSION) {
19691676Sjpk 				if (!IPCL_CONN_MATCH(connp, protocol,
19701676Sjpk 				    hdr->ipha_src, hdr->ipha_dst, ports))
19711676Sjpk 					continue;
19720Sstevel@tonic-gate 			} else {
19731676Sjpk 				if (!IPCL_CONN_MATCH_V6(connp, protocol,
19740Sstevel@tonic-gate 				    ((ip6_t *)hdr)->ip6_src,
19751676Sjpk 				    ((ip6_t *)hdr)->ip6_dst, ports))
19761676Sjpk 					continue;
19770Sstevel@tonic-gate 			}
19780Sstevel@tonic-gate 		} else {
19790Sstevel@tonic-gate 			if (af == IPV4_VERSION) {
19801676Sjpk 				if (!IPCL_BIND_MATCH(connp, protocol,
19811676Sjpk 				    hdr->ipha_dst, lport))
19821676Sjpk 					continue;
19830Sstevel@tonic-gate 			} else {
19841676Sjpk 				if (!IPCL_BIND_MATCH_V6(connp, protocol,
19851676Sjpk 				    ((ip6_t *)hdr)->ip6_dst, lport))
19861676Sjpk 					continue;
19870Sstevel@tonic-gate 			}
19880Sstevel@tonic-gate 		}
19891676Sjpk 
19902263Ssommerfe 		if (IPCL_ZONE_MATCH(connp, zoneid) ||
19911676Sjpk 		    (unlabeled && connp->conn_mac_exempt))
19921676Sjpk 			break;
19931676Sjpk 	}
19941676Sjpk 	/*
19951676Sjpk 	 * If the connection is fully-bound and connection-oriented (TCP or
19961676Sjpk 	 * SCTP), then we've already validated the remote system's label.
19971676Sjpk 	 * There's no need to do it again for every packet.
19981676Sjpk 	 */
19991676Sjpk 	if (connp != NULL && is_system_labeled() && (!connp->conn_fully_bound ||
20001676Sjpk 	    !(connp->conn_flags & (IPCL_TCP|IPCL_SCTPCONN))) &&
20011676Sjpk 	    !tsol_receive_local(mp, dst, af, shared_addr, connp)) {
20021676Sjpk 		DTRACE_PROBE3(tx__ip__log__info__classify__rawip,
20031676Sjpk 		    char *, "connp(1) could not receive mp(2)",
20041676Sjpk 		    conn_t *, connp, mblk_t *, mp);
20051676Sjpk 		connp = NULL;
20060Sstevel@tonic-gate 	}
2007409Skcpoon 
2008409Skcpoon 	if (connp != NULL)
2009409Skcpoon 		goto found;
2010409Skcpoon 	mutex_exit(&connfp->connf_lock);
2011409Skcpoon 
2012409Skcpoon 	/* Try to look for a wildcard match. */
20133448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(0, ipst)];
2014409Skcpoon 	mutex_enter(&connfp->connf_lock);
2015409Skcpoon 	for (connp = connfp->connf_head; connp != NULL;
2016409Skcpoon 	    connp = connp->conn_next) {
2017409Skcpoon 		/* We don't allow v4 fallback for v6 raw socket. */
2018409Skcpoon 		if ((af == (connp->conn_af_isv6 ? IPV4_VERSION :
20192263Ssommerfe 		    IPV6_VERSION)) || !IPCL_ZONE_MATCH(connp, zoneid)) {
2020409Skcpoon 			continue;
2021409Skcpoon 		}
2022409Skcpoon 		if (af == IPV4_VERSION) {
2023409Skcpoon 			if (IPCL_RAW_MATCH(connp, protocol, hdr->ipha_dst))
2024409Skcpoon 				break;
2025409Skcpoon 		} else {
2026409Skcpoon 			if (IPCL_RAW_MATCH_V6(connp, protocol,
2027409Skcpoon 			    ((ip6_t *)hdr)->ip6_dst)) {
2028409Skcpoon 				break;
2029409Skcpoon 			}
2030409Skcpoon 		}
20310Sstevel@tonic-gate 	}
2032409Skcpoon 
2033409Skcpoon 	if (connp != NULL)
2034409Skcpoon 		goto found;
2035409Skcpoon 
20360Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
20370Sstevel@tonic-gate 	return (NULL);
2038409Skcpoon 
2039409Skcpoon found:
2040409Skcpoon 	ASSERT(connp != NULL);
2041409Skcpoon 	CONN_INC_REF(connp);
2042409Skcpoon 	mutex_exit(&connfp->connf_lock);
2043409Skcpoon 	return (connp);
20440Sstevel@tonic-gate }
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate /* ARGSUSED */
20470Sstevel@tonic-gate static int
20485240Snordmark tcp_conn_constructor(void *buf, void *cdrarg, int kmflags)
20490Sstevel@tonic-gate {
20500Sstevel@tonic-gate 	itc_t	*itc = (itc_t *)buf;
20510Sstevel@tonic-gate 	conn_t 	*connp = &itc->itc_conn;
20525240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
20535240Snordmark 
20545240Snordmark 	bzero(connp, sizeof (conn_t));
20555240Snordmark 	bzero(tcp, sizeof (tcp_t));
20565240Snordmark 
20575240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20585240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20598348SEric.Yu@Sun.COM 	cv_init(&connp->conn_sq_cv, NULL, CV_DEFAULT, NULL);
20600Sstevel@tonic-gate 	tcp->tcp_timercache = tcp_timermp_alloc(KM_NOSLEEP);
20610Sstevel@tonic-gate 	connp->conn_tcp = tcp;
20620Sstevel@tonic-gate 	connp->conn_flags = IPCL_TCPCONN;
20630Sstevel@tonic-gate 	connp->conn_ulp = IPPROTO_TCP;
20640Sstevel@tonic-gate 	tcp->tcp_connp = connp;
20650Sstevel@tonic-gate 	return (0);
20660Sstevel@tonic-gate }
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate /* ARGSUSED */
20690Sstevel@tonic-gate static void
20705240Snordmark tcp_conn_destructor(void *buf, void *cdrarg)
20715240Snordmark {
20725240Snordmark 	itc_t	*itc = (itc_t *)buf;
20735240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20745240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
20755240Snordmark 
20765240Snordmark 	ASSERT(connp->conn_flags & IPCL_TCPCONN);
20775240Snordmark 	ASSERT(tcp->tcp_connp == connp);
20785240Snordmark 	ASSERT(connp->conn_tcp == tcp);
20795240Snordmark 	tcp_timermp_free(tcp);
20805240Snordmark 	mutex_destroy(&connp->conn_lock);
20815240Snordmark 	cv_destroy(&connp->conn_cv);
20828348SEric.Yu@Sun.COM 	cv_destroy(&connp->conn_sq_cv);
20835240Snordmark }
20845240Snordmark 
20855240Snordmark /* ARGSUSED */
20865240Snordmark static int
20875240Snordmark ip_conn_constructor(void *buf, void *cdrarg, int kmflags)
20885240Snordmark {
20895240Snordmark 	itc_t	*itc = (itc_t *)buf;
20905240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20915240Snordmark 
20925240Snordmark 	bzero(connp, sizeof (conn_t));
20935240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20945240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20955240Snordmark 	connp->conn_flags = IPCL_IPCCONN;
20965240Snordmark 
20975240Snordmark 	return (0);
20985240Snordmark }
20995240Snordmark 
21005240Snordmark /* ARGSUSED */
21015240Snordmark static void
21025240Snordmark ip_conn_destructor(void *buf, void *cdrarg)
21035240Snordmark {
21045240Snordmark 	itc_t	*itc = (itc_t *)buf;
21055240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21065240Snordmark 
21075240Snordmark 	ASSERT(connp->conn_flags & IPCL_IPCCONN);
21085240Snordmark 	ASSERT(connp->conn_priv == NULL);
21095240Snordmark 	mutex_destroy(&connp->conn_lock);
21105240Snordmark 	cv_destroy(&connp->conn_cv);
21115240Snordmark }
21125240Snordmark 
21135240Snordmark /* ARGSUSED */
21145240Snordmark static int
21155240Snordmark udp_conn_constructor(void *buf, void *cdrarg, int kmflags)
21165240Snordmark {
21175240Snordmark 	itc_t	*itc = (itc_t *)buf;
21185240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21195240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
21205240Snordmark 
21215240Snordmark 	bzero(connp, sizeof (conn_t));
21225240Snordmark 	bzero(udp, sizeof (udp_t));
21235240Snordmark 
21245240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21255240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21265240Snordmark 	connp->conn_udp = udp;
21275240Snordmark 	connp->conn_flags = IPCL_UDPCONN;
21285240Snordmark 	connp->conn_ulp = IPPROTO_UDP;
21295240Snordmark 	udp->udp_connp = connp;
21305240Snordmark 	return (0);
21315240Snordmark }
21325240Snordmark 
21335240Snordmark /* ARGSUSED */
21345240Snordmark static void
21355240Snordmark udp_conn_destructor(void *buf, void *cdrarg)
21365240Snordmark {
21375240Snordmark 	itc_t	*itc = (itc_t *)buf;
21385240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21395240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
21405240Snordmark 
21415240Snordmark 	ASSERT(connp->conn_flags & IPCL_UDPCONN);
21425240Snordmark 	ASSERT(udp->udp_connp == connp);
21435240Snordmark 	ASSERT(connp->conn_udp == udp);
21445240Snordmark 	mutex_destroy(&connp->conn_lock);
21455240Snordmark 	cv_destroy(&connp->conn_cv);
21465240Snordmark }
21475240Snordmark 
21485240Snordmark /* ARGSUSED */
21495240Snordmark static int
21505240Snordmark rawip_conn_constructor(void *buf, void *cdrarg, int kmflags)
21510Sstevel@tonic-gate {
21525240Snordmark 	itc_t	*itc = (itc_t *)buf;
21535240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21545240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
21555240Snordmark 
21565240Snordmark 	bzero(connp, sizeof (conn_t));
21575240Snordmark 	bzero(icmp, sizeof (icmp_t));
21585240Snordmark 
21595240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21605240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21615240Snordmark 	connp->conn_icmp = icmp;
21625240Snordmark 	connp->conn_flags = IPCL_RAWIPCONN;
21635240Snordmark 	connp->conn_ulp = IPPROTO_ICMP;
21645240Snordmark 	icmp->icmp_connp = connp;
21655240Snordmark 	return (0);
21665240Snordmark }
21675240Snordmark 
21685240Snordmark /* ARGSUSED */
21695240Snordmark static void
21705240Snordmark rawip_conn_destructor(void *buf, void *cdrarg)
21715240Snordmark {
21725240Snordmark 	itc_t	*itc = (itc_t *)buf;
21735240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21745240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
21755240Snordmark 
21765240Snordmark 	ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
21775240Snordmark 	ASSERT(icmp->icmp_connp == connp);
21785240Snordmark 	ASSERT(connp->conn_icmp == icmp);
21795240Snordmark 	mutex_destroy(&connp->conn_lock);
21805240Snordmark 	cv_destroy(&connp->conn_cv);
21815240Snordmark }
21825240Snordmark 
21835240Snordmark /* ARGSUSED */
21845240Snordmark static int
21855240Snordmark rts_conn_constructor(void *buf, void *cdrarg, int kmflags)
21865240Snordmark {
21875240Snordmark 	itc_t	*itc = (itc_t *)buf;
21885240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21895240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21905240Snordmark 
21915240Snordmark 	bzero(connp, sizeof (conn_t));
21925240Snordmark 	bzero(rts, sizeof (rts_t));
21935240Snordmark 
21945240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21955240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21965240Snordmark 	connp->conn_rts = rts;
21975240Snordmark 	connp->conn_flags = IPCL_RTSCONN;
21985240Snordmark 	rts->rts_connp = connp;
21995240Snordmark 	return (0);
22005240Snordmark }
22015240Snordmark 
22025240Snordmark /* ARGSUSED */
22035240Snordmark static void
22045240Snordmark rts_conn_destructor(void *buf, void *cdrarg)
22055240Snordmark {
22065240Snordmark 	itc_t	*itc = (itc_t *)buf;
22075240Snordmark 	conn_t 	*connp = &itc->itc_conn;
22085240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
22095240Snordmark 
22105240Snordmark 	ASSERT(connp->conn_flags & IPCL_RTSCONN);
22115240Snordmark 	ASSERT(rts->rts_connp == connp);
22125240Snordmark 	ASSERT(connp->conn_rts == rts);
22135240Snordmark 	mutex_destroy(&connp->conn_lock);
22145240Snordmark 	cv_destroy(&connp->conn_cv);
22155240Snordmark }
22165240Snordmark 
22178348SEric.Yu@Sun.COM /* ARGSUSED */
22188348SEric.Yu@Sun.COM int
22198348SEric.Yu@Sun.COM ip_helper_stream_constructor(void *buf, void *cdrarg, int kmflags)
22208348SEric.Yu@Sun.COM {
22218348SEric.Yu@Sun.COM 	int error;
22228348SEric.Yu@Sun.COM 	netstack_t	*ns;
22238348SEric.Yu@Sun.COM 	int		ret;
22248348SEric.Yu@Sun.COM 	tcp_stack_t	*tcps;
22258348SEric.Yu@Sun.COM 	ip_helper_stream_info_t	*ip_helper_str;
22268348SEric.Yu@Sun.COM 	ip_stack_t	*ipst;
22278348SEric.Yu@Sun.COM 
22288348SEric.Yu@Sun.COM 	ns = netstack_find_by_cred(kcred);
22298348SEric.Yu@Sun.COM 	ASSERT(ns != NULL);
22308348SEric.Yu@Sun.COM 	tcps = ns->netstack_tcp;
22318348SEric.Yu@Sun.COM 	ipst = ns->netstack_ip;
22328348SEric.Yu@Sun.COM 	ASSERT(tcps != NULL);
22338348SEric.Yu@Sun.COM 	ip_helper_str = (ip_helper_stream_info_t *)buf;
22348348SEric.Yu@Sun.COM 
22358348SEric.Yu@Sun.COM 	error = ldi_open_by_name(DEV_IP, IP_HELPER_STR, kcred,
22368348SEric.Yu@Sun.COM 	    &ip_helper_str->ip_helper_stream_handle, ipst->ips_ldi_ident);
22378348SEric.Yu@Sun.COM 	if (error != 0) {
22388348SEric.Yu@Sun.COM 		goto done;
22398348SEric.Yu@Sun.COM 	}
22408348SEric.Yu@Sun.COM 	error = ldi_ioctl(ip_helper_str->ip_helper_stream_handle,
22418348SEric.Yu@Sun.COM 	    SIOCSQPTR, (intptr_t)buf, FKIOCTL, kcred, &ret);
22428348SEric.Yu@Sun.COM 	if (error != 0) {
22438348SEric.Yu@Sun.COM 		(void) ldi_close(ip_helper_str->ip_helper_stream_handle, 0,
22448348SEric.Yu@Sun.COM 		    kcred);
22458348SEric.Yu@Sun.COM 	}
22468348SEric.Yu@Sun.COM done:
22478348SEric.Yu@Sun.COM 	netstack_rele(ipst->ips_netstack);
22488348SEric.Yu@Sun.COM 	return (error);
22498348SEric.Yu@Sun.COM }
22508348SEric.Yu@Sun.COM 
22518348SEric.Yu@Sun.COM /* ARGSUSED */
22528348SEric.Yu@Sun.COM static void
22538348SEric.Yu@Sun.COM ip_helper_stream_destructor(void *buf, void *cdrarg)
22548348SEric.Yu@Sun.COM {
22558348SEric.Yu@Sun.COM 	ip_helper_stream_info_t *ip_helper_str = (ip_helper_stream_info_t *)buf;
22568348SEric.Yu@Sun.COM 
22578348SEric.Yu@Sun.COM 	ip_helper_str->ip_helper_stream_rq->q_ptr =
22588348SEric.Yu@Sun.COM 	    ip_helper_str->ip_helper_stream_wq->q_ptr =
22598348SEric.Yu@Sun.COM 	    ip_helper_str->ip_helper_stream_minfo;
22608348SEric.Yu@Sun.COM 	(void) ldi_close(ip_helper_str->ip_helper_stream_handle, 0, kcred);
22618348SEric.Yu@Sun.COM }
22628348SEric.Yu@Sun.COM 
22638348SEric.Yu@Sun.COM 
22645240Snordmark /*
22655240Snordmark  * Called as part of ipcl_conn_destroy to assert and clear any pointers
22665240Snordmark  * in the conn_t.
22675240Snordmark  */
22685240Snordmark void
22695240Snordmark ipcl_conn_cleanup(conn_t *connp)
22705240Snordmark {
22715240Snordmark 	ASSERT(connp->conn_ire_cache == NULL);
22725240Snordmark 	ASSERT(connp->conn_latch == NULL);
22735240Snordmark #ifdef notdef
22745240Snordmark 	ASSERT(connp->conn_rq == NULL);
22755240Snordmark 	ASSERT(connp->conn_wq == NULL);
22765240Snordmark #endif
22775240Snordmark 	ASSERT(connp->conn_cred == NULL);
22785240Snordmark 	ASSERT(connp->conn_g_fanout == NULL);
22795240Snordmark 	ASSERT(connp->conn_g_next == NULL);
22805240Snordmark 	ASSERT(connp->conn_g_prev == NULL);
22815240Snordmark 	ASSERT(connp->conn_policy == NULL);
22825240Snordmark 	ASSERT(connp->conn_fanout == NULL);
22835240Snordmark 	ASSERT(connp->conn_next == NULL);
22845240Snordmark 	ASSERT(connp->conn_prev == NULL);
22855240Snordmark #ifdef notdef
22865240Snordmark 	/*
22875240Snordmark 	 * The ill and ipif pointers are not cleared before the conn_t
22885240Snordmark 	 * goes away since they do not hold a reference on the ill/ipif.
22895240Snordmark 	 * We should replace these pointers with ifindex/ipaddr_t to
22905240Snordmark 	 * make the code less complex.
22915240Snordmark 	 */
22925240Snordmark 	ASSERT(connp->conn_xmit_if_ill == NULL);
22935240Snordmark 	ASSERT(connp->conn_nofailover_ill == NULL);
22945240Snordmark 	ASSERT(connp->conn_outgoing_ill == NULL);
22955240Snordmark 	ASSERT(connp->conn_incoming_ill == NULL);
22965240Snordmark 	ASSERT(connp->conn_outgoing_pill == NULL);
22975240Snordmark 	ASSERT(connp->conn_multicast_ipif == NULL);
22985240Snordmark 	ASSERT(connp->conn_multicast_ill == NULL);
22995240Snordmark #endif
23005240Snordmark 	ASSERT(connp->conn_oper_pending_ill == NULL);
23015240Snordmark 	ASSERT(connp->conn_ilg == NULL);
23025240Snordmark 	ASSERT(connp->conn_drain_next == NULL);
23035240Snordmark 	ASSERT(connp->conn_drain_prev == NULL);
23045277Snordmark #ifdef notdef
23055277Snordmark 	/* conn_idl is not cleared when removed from idl list */
23065240Snordmark 	ASSERT(connp->conn_idl == NULL);
23075277Snordmark #endif
23085240Snordmark 	ASSERT(connp->conn_ipsec_opt_mp == NULL);
23095240Snordmark 	ASSERT(connp->conn_peercred == NULL);
23105240Snordmark 	ASSERT(connp->conn_netstack == NULL);
23115240Snordmark 
23128348SEric.Yu@Sun.COM 	ASSERT(connp->conn_helper_info == NULL);
23135240Snordmark 	/* Clear out the conn_t fields that are not preserved */
23145240Snordmark 	bzero(&connp->conn_start_clr,
23155240Snordmark 	    sizeof (conn_t) -
23165240Snordmark 	    ((uchar_t *)&connp->conn_start_clr - (uchar_t *)connp));
23170Sstevel@tonic-gate }
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate /*
23200Sstevel@tonic-gate  * All conns are inserted in a global multi-list for the benefit of
23210Sstevel@tonic-gate  * walkers. The walk is guaranteed to walk all open conns at the time
23220Sstevel@tonic-gate  * of the start of the walk exactly once. This property is needed to
23230Sstevel@tonic-gate  * achieve some cleanups during unplumb of interfaces. This is achieved
23240Sstevel@tonic-gate  * as follows.
23250Sstevel@tonic-gate  *
23260Sstevel@tonic-gate  * ipcl_conn_create and ipcl_conn_destroy are the only functions that
23270Sstevel@tonic-gate  * call the insert and delete functions below at creation and deletion
23280Sstevel@tonic-gate  * time respectively. The conn never moves or changes its position in this
23290Sstevel@tonic-gate  * multi-list during its lifetime. CONN_CONDEMNED ensures that the refcnt
23300Sstevel@tonic-gate  * won't increase due to walkers, once the conn deletion has started. Note
23310Sstevel@tonic-gate  * that we can't remove the conn from the global list and then wait for
23320Sstevel@tonic-gate  * the refcnt to drop to zero, since walkers would then see a truncated
23330Sstevel@tonic-gate  * list. CONN_INCIPIENT ensures that walkers don't start looking at
23340Sstevel@tonic-gate  * conns until ip_open is ready to make them globally visible.
23350Sstevel@tonic-gate  * The global round robin multi-list locks are held only to get the
23360Sstevel@tonic-gate  * next member/insertion/deletion and contention should be negligible
23370Sstevel@tonic-gate  * if the multi-list is much greater than the number of cpus.
23380Sstevel@tonic-gate  */
23390Sstevel@tonic-gate void
23400Sstevel@tonic-gate ipcl_globalhash_insert(conn_t *connp)
23410Sstevel@tonic-gate {
23420Sstevel@tonic-gate 	int	index;
23433448Sdh155122 	struct connf_s	*connfp;
23443448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	/*
23470Sstevel@tonic-gate 	 * No need for atomic here. Approximate even distribution
23480Sstevel@tonic-gate 	 * in the global lists is sufficient.
23490Sstevel@tonic-gate 	 */
23503448Sdh155122 	ipst->ips_conn_g_index++;
23513448Sdh155122 	index = ipst->ips_conn_g_index & (CONN_G_HASH_SIZE - 1);
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
23540Sstevel@tonic-gate 	/*
23550Sstevel@tonic-gate 	 * Mark as INCIPIENT, so that walkers will ignore this
23560Sstevel@tonic-gate 	 * for now, till ip_open is ready to make it visible globally.
23570Sstevel@tonic-gate 	 */
23580Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_INCIPIENT;
23590Sstevel@tonic-gate 
23603448Sdh155122 	connfp = &ipst->ips_ipcl_globalhash_fanout[index];
23610Sstevel@tonic-gate 	/* Insert at the head of the list */
23623448Sdh155122 	mutex_enter(&connfp->connf_lock);
23633448Sdh155122 	connp->conn_g_next = connfp->connf_head;
23640Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
23650Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp;
23663448Sdh155122 	connfp->connf_head = connp;
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 	/* The fanout bucket this conn points to */
23693448Sdh155122 	connp->conn_g_fanout = connfp;
23700Sstevel@tonic-gate 
23713448Sdh155122 	mutex_exit(&connfp->connf_lock);
23720Sstevel@tonic-gate }
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate void
23750Sstevel@tonic-gate ipcl_globalhash_remove(conn_t *connp)
23760Sstevel@tonic-gate {
23773448Sdh155122 	struct connf_s	*connfp;
23783448Sdh155122 
23790Sstevel@tonic-gate 	/*
23800Sstevel@tonic-gate 	 * We were never inserted in the global multi list.
23810Sstevel@tonic-gate 	 * IPCL_NONE variety is never inserted in the global multilist
23820Sstevel@tonic-gate 	 * since it is presumed to not need any cleanup and is transient.
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	if (connp->conn_g_fanout == NULL)
23850Sstevel@tonic-gate 		return;
23860Sstevel@tonic-gate 
23873448Sdh155122 	connfp = connp->conn_g_fanout;
23883448Sdh155122 	mutex_enter(&connfp->connf_lock);
23890Sstevel@tonic-gate 	if (connp->conn_g_prev != NULL)
23900Sstevel@tonic-gate 		connp->conn_g_prev->conn_g_next = connp->conn_g_next;
23910Sstevel@tonic-gate 	else
23923448Sdh155122 		connfp->connf_head = connp->conn_g_next;
23930Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
23940Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp->conn_g_prev;
23953448Sdh155122 	mutex_exit(&connfp->connf_lock);
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 	/* Better to stumble on a null pointer than to corrupt memory */
23980Sstevel@tonic-gate 	connp->conn_g_next = NULL;
23990Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
24005240Snordmark 	connp->conn_g_fanout = NULL;
24010Sstevel@tonic-gate }
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate /*
24040Sstevel@tonic-gate  * Walk the list of all conn_t's in the system, calling the function provided
24050Sstevel@tonic-gate  * with the specified argument for each.
24060Sstevel@tonic-gate  * Applies to both IPv4 and IPv6.
24070Sstevel@tonic-gate  *
24080Sstevel@tonic-gate  * IPCs may hold pointers to ipif/ill. To guard against stale pointers
24090Sstevel@tonic-gate  * ipcl_walk() is called to cleanup the conn_t's, typically when an interface is
24100Sstevel@tonic-gate  * unplumbed or removed. New conn_t's that are created while we are walking
24110Sstevel@tonic-gate  * may be missed by this walk, because they are not necessarily inserted
24120Sstevel@tonic-gate  * at the tail of the list. They are new conn_t's and thus don't have any
24130Sstevel@tonic-gate  * stale pointers. The CONN_CLOSING flag ensures that no new reference
24140Sstevel@tonic-gate  * is created to the struct that is going away.
24150Sstevel@tonic-gate  */
24160Sstevel@tonic-gate void
24173448Sdh155122 ipcl_walk(pfv_t func, void *arg, ip_stack_t *ipst)
24180Sstevel@tonic-gate {
24190Sstevel@tonic-gate 	int	i;
24200Sstevel@tonic-gate 	conn_t	*connp;
24210Sstevel@tonic-gate 	conn_t	*prev_connp;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
24243448Sdh155122 		mutex_enter(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
24250Sstevel@tonic-gate 		prev_connp = NULL;
24263448Sdh155122 		connp = ipst->ips_ipcl_globalhash_fanout[i].connf_head;
24270Sstevel@tonic-gate 		while (connp != NULL) {
24280Sstevel@tonic-gate 			mutex_enter(&connp->conn_lock);
24290Sstevel@tonic-gate 			if (connp->conn_state_flags &
24300Sstevel@tonic-gate 			    (CONN_CONDEMNED | CONN_INCIPIENT)) {
24310Sstevel@tonic-gate 				mutex_exit(&connp->conn_lock);
24320Sstevel@tonic-gate 				connp = connp->conn_g_next;
24330Sstevel@tonic-gate 				continue;
24340Sstevel@tonic-gate 			}
24350Sstevel@tonic-gate 			CONN_INC_REF_LOCKED(connp);
24360Sstevel@tonic-gate 			mutex_exit(&connp->conn_lock);
24373448Sdh155122 			mutex_exit(
24383448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
24390Sstevel@tonic-gate 			(*func)(connp, arg);
24400Sstevel@tonic-gate 			if (prev_connp != NULL)
24410Sstevel@tonic-gate 				CONN_DEC_REF(prev_connp);
24423448Sdh155122 			mutex_enter(
24433448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
24440Sstevel@tonic-gate 			prev_connp = connp;
24450Sstevel@tonic-gate 			connp = connp->conn_g_next;
24460Sstevel@tonic-gate 		}
24473448Sdh155122 		mutex_exit(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
24480Sstevel@tonic-gate 		if (prev_connp != NULL)
24490Sstevel@tonic-gate 			CONN_DEC_REF(prev_connp);
24500Sstevel@tonic-gate 	}
24510Sstevel@tonic-gate }
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate /*
24540Sstevel@tonic-gate  * Search for a peer TCP/IPv4 loopback conn by doing a reverse lookup on
24550Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
24560Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
24572323Sethindra  * (peer tcp in ESTABLISHED state).
24580Sstevel@tonic-gate  */
24590Sstevel@tonic-gate conn_t *
24603448Sdh155122 ipcl_conn_tcp_lookup_reversed_ipv4(conn_t *connp, ipha_t *ipha, tcph_t *tcph,
24613448Sdh155122     ip_stack_t *ipst)
24620Sstevel@tonic-gate {
24630Sstevel@tonic-gate 	uint32_t ports;
24640Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
24650Sstevel@tonic-gate 	connf_t	*connfp;
24660Sstevel@tonic-gate 	conn_t	*tconnp;
24670Sstevel@tonic-gate 	boolean_t zone_chk;
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	/*
24700Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
24710Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
24720Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
24730Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.
24740Sstevel@tonic-gate 	 */
24750Sstevel@tonic-gate 	zone_chk = (ipha->ipha_src == htonl(INADDR_LOOPBACK) ||
24760Sstevel@tonic-gate 	    ipha->ipha_dst == htonl(INADDR_LOOPBACK));
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
24790Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
24800Sstevel@tonic-gate 
24813448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
24823448Sdh155122 	    ports, ipst)];
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24850Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24860Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
24890Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
24902323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24910Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24940Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24950Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24960Sstevel@tonic-gate 			return (tconnp);
24970Sstevel@tonic-gate 		}
24980Sstevel@tonic-gate 	}
24990Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25000Sstevel@tonic-gate 	return (NULL);
25010Sstevel@tonic-gate }
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate /*
25040Sstevel@tonic-gate  * Search for a peer TCP/IPv6 loopback conn by doing a reverse lookup on
25050Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
25060Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
25072323Sethindra  * (peer tcp in ESTABLISHED state).
25080Sstevel@tonic-gate  */
25090Sstevel@tonic-gate conn_t *
25103448Sdh155122 ipcl_conn_tcp_lookup_reversed_ipv6(conn_t *connp, ip6_t *ip6h, tcph_t *tcph,
25113448Sdh155122     ip_stack_t *ipst)
25120Sstevel@tonic-gate {
25130Sstevel@tonic-gate 	uint32_t ports;
25140Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
25150Sstevel@tonic-gate 	connf_t	*connfp;
25160Sstevel@tonic-gate 	conn_t	*tconnp;
25170Sstevel@tonic-gate 	boolean_t zone_chk;
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	/*
25200Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
25210Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
25220Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
25230Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.  We
25240Sstevel@tonic-gate 	 * don't do Zone check for link local address(es) because the
25250Sstevel@tonic-gate 	 * current Zone implementation treats each link local address as
25260Sstevel@tonic-gate 	 * being unique per system node, i.e. they belong to global Zone.
25270Sstevel@tonic-gate 	 */
25280Sstevel@tonic-gate 	zone_chk = (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src) ||
25290Sstevel@tonic-gate 	    IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst));
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
25320Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
25330Sstevel@tonic-gate 
25343448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
25353448Sdh155122 	    ports, ipst)];
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
25380Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
25390Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 		/* We skip tcp_bound_if check here as this is loopback tcp */
25420Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
25430Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
25442323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
25450Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 			ASSERT(tconnp != connp);
25480Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25490Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25500Sstevel@tonic-gate 			return (tconnp);
25510Sstevel@tonic-gate 		}
25520Sstevel@tonic-gate 	}
25530Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25540Sstevel@tonic-gate 	return (NULL);
25550Sstevel@tonic-gate }
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate /*
25580Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
25590Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
25600Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
25610Sstevel@tonic-gate  */
25620Sstevel@tonic-gate conn_t *
25633448Sdh155122 ipcl_tcp_lookup_reversed_ipv4(ipha_t *ipha, tcph_t *tcph, int min_state,
25643448Sdh155122     ip_stack_t *ipst)
25650Sstevel@tonic-gate {
25660Sstevel@tonic-gate 	uint32_t ports;
25670Sstevel@tonic-gate 	uint16_t *pports;
25680Sstevel@tonic-gate 	connf_t	*connfp;
25690Sstevel@tonic-gate 	conn_t	*tconnp;
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
25720Sstevel@tonic-gate 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
25730Sstevel@tonic-gate 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
25740Sstevel@tonic-gate 
25753448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
25764691Skcpoon 	    ports, ipst)];
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
25790Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
25800Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
25830Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
25840Sstevel@tonic-gate 		    tconnp->conn_tcp->tcp_state >= min_state) {
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25870Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25880Sstevel@tonic-gate 			return (tconnp);
25890Sstevel@tonic-gate 		}
25900Sstevel@tonic-gate 	}
25910Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25920Sstevel@tonic-gate 	return (NULL);
25930Sstevel@tonic-gate }
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate /*
25960Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
25970Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
25980Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
25990Sstevel@tonic-gate  * Match on ifindex in addition to addresses.
26000Sstevel@tonic-gate  */
26010Sstevel@tonic-gate conn_t *
26020Sstevel@tonic-gate ipcl_tcp_lookup_reversed_ipv6(ip6_t *ip6h, tcpha_t *tcpha, int min_state,
26033448Sdh155122     uint_t ifindex, ip_stack_t *ipst)
26040Sstevel@tonic-gate {
26050Sstevel@tonic-gate 	tcp_t	*tcp;
26060Sstevel@tonic-gate 	uint32_t ports;
26070Sstevel@tonic-gate 	uint16_t *pports;
26080Sstevel@tonic-gate 	connf_t	*connfp;
26090Sstevel@tonic-gate 	conn_t	*tconnp;
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
26120Sstevel@tonic-gate 	pports[0] = tcpha->tha_fport;
26130Sstevel@tonic-gate 	pports[1] = tcpha->tha_lport;
26140Sstevel@tonic-gate 
26153448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
26164691Skcpoon 	    ports, ipst)];
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
26190Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
26200Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate 		tcp = tconnp->conn_tcp;
26230Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
26240Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
26250Sstevel@tonic-gate 		    tcp->tcp_state >= min_state &&
26260Sstevel@tonic-gate 		    (tcp->tcp_bound_if == 0 ||
26270Sstevel@tonic-gate 		    tcp->tcp_bound_if == ifindex)) {
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
26300Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
26310Sstevel@tonic-gate 			return (tconnp);
26320Sstevel@tonic-gate 		}
26330Sstevel@tonic-gate 	}
26340Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
26350Sstevel@tonic-gate 	return (NULL);
26360Sstevel@tonic-gate }
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate /*
26391676Sjpk  * Finds a TCP/IPv4 listening connection; called by tcp_disconnect to locate
26401676Sjpk  * a listener when changing state.
26410Sstevel@tonic-gate  */
26420Sstevel@tonic-gate conn_t *
26433448Sdh155122 ipcl_lookup_listener_v4(uint16_t lport, ipaddr_t laddr, zoneid_t zoneid,
26443448Sdh155122     ip_stack_t *ipst)
26450Sstevel@tonic-gate {
26460Sstevel@tonic-gate 	connf_t		*bind_connfp;
26470Sstevel@tonic-gate 	conn_t		*connp;
26480Sstevel@tonic-gate 	tcp_t		*tcp;
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 	/*
26510Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
26520Sstevel@tonic-gate 	 * all zeros.
26530Sstevel@tonic-gate 	 */
26540Sstevel@tonic-gate 	if (laddr == 0)
26550Sstevel@tonic-gate 		return (NULL);
26560Sstevel@tonic-gate 
26571676Sjpk 	ASSERT(zoneid != ALL_ZONES);
26581676Sjpk 
26593448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
26600Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
26610Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
26620Sstevel@tonic-gate 	    connp = connp->conn_next) {
26630Sstevel@tonic-gate 		tcp = connp->conn_tcp;
26640Sstevel@tonic-gate 		if (IPCL_BIND_MATCH(connp, IPPROTO_TCP, laddr, lport) &&
26652263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
26660Sstevel@tonic-gate 		    (tcp->tcp_listener == NULL)) {
26670Sstevel@tonic-gate 			CONN_INC_REF(connp);
26680Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
26690Sstevel@tonic-gate 			return (connp);
26700Sstevel@tonic-gate 		}
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
26730Sstevel@tonic-gate 	return (NULL);
26740Sstevel@tonic-gate }
26750Sstevel@tonic-gate 
26761676Sjpk /*
26771676Sjpk  * Finds a TCP/IPv6 listening connection; called by tcp_disconnect to locate
26781676Sjpk  * a listener when changing state.
26791676Sjpk  */
26800Sstevel@tonic-gate conn_t *
26810Sstevel@tonic-gate ipcl_lookup_listener_v6(uint16_t lport, in6_addr_t *laddr, uint_t ifindex,
26823448Sdh155122     zoneid_t zoneid, ip_stack_t *ipst)
26830Sstevel@tonic-gate {
26840Sstevel@tonic-gate 	connf_t		*bind_connfp;
26850Sstevel@tonic-gate 	conn_t		*connp = NULL;
26860Sstevel@tonic-gate 	tcp_t		*tcp;
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	/*
26890Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
26900Sstevel@tonic-gate 	 * all zeros.
26910Sstevel@tonic-gate 	 */
26920Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(laddr))
26930Sstevel@tonic-gate 		return (NULL);
26940Sstevel@tonic-gate 
26951676Sjpk 	ASSERT(zoneid != ALL_ZONES);
26960Sstevel@tonic-gate 
26973448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
26980Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
26990Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
27000Sstevel@tonic-gate 	    connp = connp->conn_next) {
27010Sstevel@tonic-gate 		tcp = connp->conn_tcp;
27020Sstevel@tonic-gate 		if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP, *laddr, lport) &&
27032263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
27040Sstevel@tonic-gate 		    (tcp->tcp_bound_if == 0 ||
27050Sstevel@tonic-gate 		    tcp->tcp_bound_if == ifindex) &&
27060Sstevel@tonic-gate 		    tcp->tcp_listener == NULL) {
27070Sstevel@tonic-gate 			CONN_INC_REF(connp);
27080Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
27090Sstevel@tonic-gate 			return (connp);
27100Sstevel@tonic-gate 		}
27110Sstevel@tonic-gate 	}
27120Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
27130Sstevel@tonic-gate 	return (NULL);
27140Sstevel@tonic-gate }
27150Sstevel@tonic-gate 
2716741Smasputra /*
2717741Smasputra  * ipcl_get_next_conn
2718741Smasputra  *	get the next entry in the conn global list
2719741Smasputra  *	and put a reference on the next_conn.
2720741Smasputra  *	decrement the reference on the current conn.
2721741Smasputra  *
2722741Smasputra  * This is an iterator based walker function that also provides for
2723741Smasputra  * some selection by the caller. It walks through the conn_hash bucket
2724741Smasputra  * searching for the next valid connp in the list, and selects connections
2725741Smasputra  * that are neither closed nor condemned. It also REFHOLDS the conn
2726741Smasputra  * thus ensuring that the conn exists when the caller uses the conn.
2727741Smasputra  */
2728741Smasputra conn_t *
2729741Smasputra ipcl_get_next_conn(connf_t *connfp, conn_t *connp, uint32_t conn_flags)
2730741Smasputra {
2731741Smasputra 	conn_t	*next_connp;
2732741Smasputra 
2733741Smasputra 	if (connfp == NULL)
2734741Smasputra 		return (NULL);
2735741Smasputra 
2736741Smasputra 	mutex_enter(&connfp->connf_lock);
2737741Smasputra 
2738741Smasputra 	next_connp = (connp == NULL) ?
2739741Smasputra 	    connfp->connf_head : connp->conn_g_next;
2740741Smasputra 
2741741Smasputra 	while (next_connp != NULL) {
2742741Smasputra 		mutex_enter(&next_connp->conn_lock);
2743741Smasputra 		if (!(next_connp->conn_flags & conn_flags) ||
2744741Smasputra 		    (next_connp->conn_state_flags &
2745741Smasputra 		    (CONN_CONDEMNED | CONN_INCIPIENT))) {
2746741Smasputra 			/*
2747741Smasputra 			 * This conn has been condemned or
2748741Smasputra 			 * is closing, or the flags don't match
2749741Smasputra 			 */
2750741Smasputra 			mutex_exit(&next_connp->conn_lock);
2751741Smasputra 			next_connp = next_connp->conn_g_next;
2752741Smasputra 			continue;
2753741Smasputra 		}
2754741Smasputra 		CONN_INC_REF_LOCKED(next_connp);
2755741Smasputra 		mutex_exit(&next_connp->conn_lock);
2756741Smasputra 		break;
2757741Smasputra 	}
2758741Smasputra 
2759741Smasputra 	mutex_exit(&connfp->connf_lock);
2760741Smasputra 
2761741Smasputra 	if (connp != NULL)
2762741Smasputra 		CONN_DEC_REF(connp);
2763741Smasputra 
2764741Smasputra 	return (next_connp);
2765741Smasputra }
2766741Smasputra 
27670Sstevel@tonic-gate #ifdef CONN_DEBUG
27680Sstevel@tonic-gate /*
27690Sstevel@tonic-gate  * Trace of the last NBUF refhold/refrele
27700Sstevel@tonic-gate  */
27710Sstevel@tonic-gate int
27720Sstevel@tonic-gate conn_trace_ref(conn_t *connp)
27730Sstevel@tonic-gate {
27740Sstevel@tonic-gate 	int	last;
27750Sstevel@tonic-gate 	conn_trace_t	*ctb;
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
27780Sstevel@tonic-gate 	last = connp->conn_trace_last;
27790Sstevel@tonic-gate 	last++;
27800Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
27810Sstevel@tonic-gate 		last = 0;
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
27845023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27850Sstevel@tonic-gate 	connp->conn_trace_last = last;
27860Sstevel@tonic-gate 	return (1);
27870Sstevel@tonic-gate }
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate int
27900Sstevel@tonic-gate conn_untrace_ref(conn_t *connp)
27910Sstevel@tonic-gate {
27920Sstevel@tonic-gate 	int	last;
27930Sstevel@tonic-gate 	conn_trace_t	*ctb;
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
27960Sstevel@tonic-gate 	last = connp->conn_trace_last;
27970Sstevel@tonic-gate 	last++;
27980Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
27990Sstevel@tonic-gate 		last = 0;
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
28025023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
28030Sstevel@tonic-gate 	connp->conn_trace_last = last;
28040Sstevel@tonic-gate 	return (1);
28050Sstevel@tonic-gate }
28060Sstevel@tonic-gate #endif
2807