xref: /onnv-gate/usr/src/uts/common/inet/ip/ipclassifier.c (revision 12507:501806a754d2)
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 /*
2212056SKacheong.Poon@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * IP PACKET CLASSIFIER
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * The IP packet classifier provides mapping between IP packets and persistent
290Sstevel@tonic-gate  * connection state for connection-oriented protocols. It also provides
300Sstevel@tonic-gate  * interface for managing connection states.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * The connection state is kept in conn_t data structure and contains, among
330Sstevel@tonic-gate  * other things:
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  *	o local/remote address and ports
360Sstevel@tonic-gate  *	o Transport protocol
370Sstevel@tonic-gate  *	o squeue for the connection (for TCP only)
380Sstevel@tonic-gate  *	o reference counter
390Sstevel@tonic-gate  *	o Connection state
400Sstevel@tonic-gate  *	o hash table linkage
410Sstevel@tonic-gate  *	o interface/ire information
420Sstevel@tonic-gate  *	o credentials
430Sstevel@tonic-gate  *	o ipsec policy
440Sstevel@tonic-gate  *	o send and receive functions.
450Sstevel@tonic-gate  *	o mutex lock.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * Connections use a reference counting scheme. They are freed when the
480Sstevel@tonic-gate  * reference counter drops to zero. A reference is incremented when connection
490Sstevel@tonic-gate  * is placed in a list or table, when incoming packet for the connection arrives
500Sstevel@tonic-gate  * and when connection is processed via squeue (squeue processing may be
510Sstevel@tonic-gate  * asynchronous and the reference protects the connection from being destroyed
520Sstevel@tonic-gate  * before its processing is finished).
530Sstevel@tonic-gate  *
5411042SErik.Nordmark@Sun.COM  * conn_recv is used to pass up packets to the ULP.
5511042SErik.Nordmark@Sun.COM  * For TCP conn_recv changes. It is tcp_input_listener_unbound initially for
5611042SErik.Nordmark@Sun.COM  * a listener, and changes to tcp_input_listener as the listener has picked a
5711042SErik.Nordmark@Sun.COM  * good squeue. For other cases it is set to tcp_input_data.
5811042SErik.Nordmark@Sun.COM  *
5911042SErik.Nordmark@Sun.COM  * conn_recvicmp is used to pass up ICMP errors to the ULP.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Classifier uses several hash tables:
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * 	ipcl_conn_fanout:	contains all TCP connections in CONNECTED state
640Sstevel@tonic-gate  *	ipcl_bind_fanout:	contains all connections in BOUND state
650Sstevel@tonic-gate  *	ipcl_proto_fanout:	IPv4 protocol fanout
660Sstevel@tonic-gate  *	ipcl_proto_fanout_v6:	IPv6 protocol fanout
670Sstevel@tonic-gate  *	ipcl_udp_fanout:	contains all UDP connections
6810616SSebastien.Roy@Sun.COM  *	ipcl_iptun_fanout:	contains all IP tunnel connections
690Sstevel@tonic-gate  *	ipcl_globalhash_fanout:	contains all connections
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * The ipcl_globalhash_fanout is used for any walkers (like snmp and Clustering)
720Sstevel@tonic-gate  * which need to view all existing connections.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * All tables are protected by per-bucket locks. When both per-bucket lock and
750Sstevel@tonic-gate  * connection lock need to be held, the per-bucket lock should be acquired
760Sstevel@tonic-gate  * first, followed by the connection lock.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * All functions doing search in one of these tables increment a reference
790Sstevel@tonic-gate  * counter on the connection found (if any). This reference should be dropped
800Sstevel@tonic-gate  * when the caller has finished processing the connection.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * INTERFACES:
840Sstevel@tonic-gate  * ===========
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * Connection Lookup:
870Sstevel@tonic-gate  * ------------------
880Sstevel@tonic-gate  *
8911042SErik.Nordmark@Sun.COM  * conn_t *ipcl_classify_v4(mp, protocol, hdr_len, ira, ip_stack)
9011042SErik.Nordmark@Sun.COM  * conn_t *ipcl_classify_v6(mp, protocol, hdr_len, ira, ip_stack)
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * Finds connection for an incoming IPv4 or IPv6 packet. Returns NULL if
930Sstevel@tonic-gate  * it can't find any associated connection. If the connection is found, its
940Sstevel@tonic-gate  * reference counter is incremented.
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  *	mp:	mblock, containing packet header. The full header should fit
970Sstevel@tonic-gate  *		into a single mblock. It should also contain at least full IP
980Sstevel@tonic-gate  *		and TCP or UDP header.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *	protocol: Either IPPROTO_TCP or IPPROTO_UDP.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  *	hdr_len: The size of IP header. It is used to find TCP or UDP header in
1030Sstevel@tonic-gate  *		 the packet.
1040Sstevel@tonic-gate  *
10511042SErik.Nordmark@Sun.COM  * 	ira->ira_zoneid: The zone in which the returned connection must be; the
10611042SErik.Nordmark@Sun.COM  *		zoneid corresponding to the ire_zoneid on the IRE located for
10711042SErik.Nordmark@Sun.COM  *		the packet's destination address.
10811042SErik.Nordmark@Sun.COM  *
10911042SErik.Nordmark@Sun.COM  *	ira->ira_flags: Contains the IRAF_TX_MAC_EXEMPTABLE and
11011042SErik.Nordmark@Sun.COM  *		IRAF_TX_SHARED_ADDR flags
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  *	For TCP connections, the lookup order is as follows:
1130Sstevel@tonic-gate  *		5-tuple {src, dst, protocol, local port, remote port}
1140Sstevel@tonic-gate  *			lookup in ipcl_conn_fanout table.
1150Sstevel@tonic-gate  *		3-tuple {dst, remote port, protocol} lookup in
1160Sstevel@tonic-gate  *			ipcl_bind_fanout table.
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  *	For UDP connections, a 5-tuple {src, dst, protocol, local port,
1190Sstevel@tonic-gate  *	remote port} lookup is done on ipcl_udp_fanout. Note that,
1200Sstevel@tonic-gate  *	these interfaces do not handle cases where a packets belongs
1210Sstevel@tonic-gate  *	to multiple UDP clients, which is handled in IP itself.
1220Sstevel@tonic-gate  *
1231676Sjpk  * If the destination IRE is ALL_ZONES (indicated by zoneid), then we must
1241676Sjpk  * determine which actual zone gets the segment.  This is used only in a
1251676Sjpk  * labeled environment.  The matching rules are:
1261676Sjpk  *
1271676Sjpk  *	- If it's not a multilevel port, then the label on the packet selects
1281676Sjpk  *	  the zone.  Unlabeled packets are delivered to the global zone.
1291676Sjpk  *
1301676Sjpk  *	- If it's a multilevel port, then only the zone registered to receive
1311676Sjpk  *	  packets on that port matches.
1321676Sjpk  *
1331676Sjpk  * Also, in a labeled environment, packet labels need to be checked.  For fully
1341676Sjpk  * bound TCP connections, we can assume that the packet label was checked
1351676Sjpk  * during connection establishment, and doesn't need to be checked on each
1361676Sjpk  * packet.  For others, though, we need to check for strict equality or, for
1371676Sjpk  * multilevel ports, membership in the range or set.  This part currently does
1381676Sjpk  * a tnrh lookup on each packet, but could be optimized to use cached results
1391676Sjpk  * if that were necessary.  (SCTP doesn't come through here, but if it did,
1401676Sjpk  * we would apply the same rules as TCP.)
1411676Sjpk  *
1421676Sjpk  * An implication of the above is that fully-bound TCP sockets must always use
1431676Sjpk  * distinct 4-tuples; they can't be discriminated by label alone.
1441676Sjpk  *
1451676Sjpk  * Note that we cannot trust labels on packets sent to fully-bound UDP sockets,
1461676Sjpk  * as there's no connection set-up handshake and no shared state.
1471676Sjpk  *
1481676Sjpk  * Labels on looped-back packets within a single zone do not need to be
1491676Sjpk  * checked, as all processes in the same zone have the same label.
1501676Sjpk  *
1511676Sjpk  * Finally, for unlabeled packets received by a labeled system, special rules
1521676Sjpk  * apply.  We consider only the MLP if there is one.  Otherwise, we prefer a
1531676Sjpk  * socket in the zone whose label matches the default label of the sender, if
1541676Sjpk  * any.  In any event, the receiving socket must have SO_MAC_EXEMPT set and the
1551676Sjpk  * receiver's label must dominate the sender's default label.
1561676Sjpk  *
15711042SErik.Nordmark@Sun.COM  * conn_t *ipcl_tcp_lookup_reversed_ipv4(ipha_t *, tcpha_t *, int, ip_stack);
1583448Sdh155122  * conn_t *ipcl_tcp_lookup_reversed_ipv6(ip6_t *, tcpha_t *, int, uint_t,
1593448Sdh155122  *					 ip_stack);
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  *	Lookup routine to find a exact match for {src, dst, local port,
1620Sstevel@tonic-gate  *	remote port) for TCP connections in ipcl_conn_fanout. The address and
1630Sstevel@tonic-gate  *	ports are read from the IP and TCP header respectively.
1640Sstevel@tonic-gate  *
1653448Sdh155122  * conn_t	*ipcl_lookup_listener_v4(lport, laddr, protocol,
1663448Sdh155122  *					 zoneid, ip_stack);
1673448Sdh155122  * conn_t	*ipcl_lookup_listener_v6(lport, laddr, protocol, ifindex,
1683448Sdh155122  *					 zoneid, ip_stack);
1690Sstevel@tonic-gate  *
1700Sstevel@tonic-gate  * 	Lookup routine to find a listener with the tuple {lport, laddr,
1710Sstevel@tonic-gate  * 	protocol} in the ipcl_bind_fanout table. For IPv6, an additional
1720Sstevel@tonic-gate  * 	parameter interface index is also compared.
1730Sstevel@tonic-gate  *
1743448Sdh155122  * void ipcl_walk(func, arg, ip_stack)
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * 	Apply 'func' to every connection available. The 'func' is called as
1770Sstevel@tonic-gate  *	(*func)(connp, arg). The walk is non-atomic so connections may be
1780Sstevel@tonic-gate  *	created and destroyed during the walk. The CONN_CONDEMNED and
1790Sstevel@tonic-gate  *	CONN_INCIPIENT flags ensure that connections which are newly created
1800Sstevel@tonic-gate  *	or being destroyed are not selected by the walker.
1810Sstevel@tonic-gate  *
1820Sstevel@tonic-gate  * Table Updates
1830Sstevel@tonic-gate  * -------------
1840Sstevel@tonic-gate  *
18511042SErik.Nordmark@Sun.COM  * int ipcl_conn_insert(connp);
18611042SErik.Nordmark@Sun.COM  * int ipcl_conn_insert_v4(connp);
18711042SErik.Nordmark@Sun.COM  * int ipcl_conn_insert_v6(connp);
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  *
1930Sstevel@tonic-gate  *	Return value :
1940Sstevel@tonic-gate  *		0		if connp was inserted
1950Sstevel@tonic-gate  *		EADDRINUSE	if the connection with the same tuple
1960Sstevel@tonic-gate  *				already exists.
1970Sstevel@tonic-gate  *
19811042SErik.Nordmark@Sun.COM  * int ipcl_bind_insert(connp);
19911042SErik.Nordmark@Sun.COM  * int ipcl_bind_insert_v4(connp);
20011042SErik.Nordmark@Sun.COM  * int ipcl_bind_insert_v6(connp);
2010Sstevel@tonic-gate  *
2020Sstevel@tonic-gate  * 	Insert 'connp' in ipcl_bind_fanout.
2030Sstevel@tonic-gate  * 	Arguements :
2040Sstevel@tonic-gate  * 		connp		conn_t to be inserted
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  * void ipcl_hash_remove(connp);
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  * 	Removes the 'connp' from the connection fanout table.
2100Sstevel@tonic-gate  *
2110Sstevel@tonic-gate  * Connection Creation/Destruction
2120Sstevel@tonic-gate  * -------------------------------
2130Sstevel@tonic-gate  *
2143448Sdh155122  * conn_t *ipcl_conn_create(type, sleep, netstack_t *)
2150Sstevel@tonic-gate  *
2160Sstevel@tonic-gate  * 	Creates a new conn based on the type flag, inserts it into
2170Sstevel@tonic-gate  * 	globalhash table.
2180Sstevel@tonic-gate  *
2190Sstevel@tonic-gate  *	type:	This flag determines the type of conn_t which needs to be
2205240Snordmark  *		created i.e., which kmem_cache it comes from.
2210Sstevel@tonic-gate  *		IPCL_TCPCONN	indicates a TCP connection
2225240Snordmark  *		IPCL_SCTPCONN	indicates a SCTP connection
2235240Snordmark  *		IPCL_UDPCONN	indicates a UDP conn_t.
2245240Snordmark  *		IPCL_RAWIPCONN	indicates a RAWIP/ICMP conn_t.
2255240Snordmark  *		IPCL_RTSCONN	indicates a RTS conn_t.
2265240Snordmark  *		IPCL_IPCCONN	indicates all other connections.
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  * void ipcl_conn_destroy(connp)
2290Sstevel@tonic-gate  *
2300Sstevel@tonic-gate  * 	Destroys the connection state, removes it from the global
2310Sstevel@tonic-gate  * 	connection hash table and frees its memory.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate #include <sys/types.h>
2350Sstevel@tonic-gate #include <sys/stream.h>
2360Sstevel@tonic-gate #include <sys/stropts.h>
2370Sstevel@tonic-gate #include <sys/sysmacros.h>
2380Sstevel@tonic-gate #include <sys/strsubr.h>
2390Sstevel@tonic-gate #include <sys/strsun.h>
2400Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
2410Sstevel@tonic-gate #include <sys/ddi.h>
2420Sstevel@tonic-gate #include <sys/cmn_err.h>
2430Sstevel@tonic-gate #include <sys/debug.h>
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate #include <sys/systm.h>
2460Sstevel@tonic-gate #include <sys/param.h>
2470Sstevel@tonic-gate #include <sys/kmem.h>
2480Sstevel@tonic-gate #include <sys/isa_defs.h>
2490Sstevel@tonic-gate #include <inet/common.h>
2500Sstevel@tonic-gate #include <netinet/ip6.h>
2510Sstevel@tonic-gate #include <netinet/icmp6.h>
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate #include <inet/ip.h>
25411042SErik.Nordmark@Sun.COM #include <inet/ip_if.h>
25511042SErik.Nordmark@Sun.COM #include <inet/ip_ire.h>
2560Sstevel@tonic-gate #include <inet/ip6.h>
2570Sstevel@tonic-gate #include <inet/ip_ndp.h>
2588348SEric.Yu@Sun.COM #include <inet/ip_impl.h>
259741Smasputra #include <inet/udp_impl.h>
2600Sstevel@tonic-gate #include <inet/sctp_ip.h>
2613448Sdh155122 #include <inet/sctp/sctp_impl.h>
2625240Snordmark #include <inet/rawip_impl.h>
2635240Snordmark #include <inet/rts_impl.h>
26410616SSebastien.Roy@Sun.COM #include <inet/iptun/iptun_impl.h>
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate #include <sys/cpuvar.h>
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate #include <inet/ipclassifier.h>
2698348SEric.Yu@Sun.COM #include <inet/tcp.h>
2700Sstevel@tonic-gate #include <inet/ipsec_impl.h>
2710Sstevel@tonic-gate 
2721676Sjpk #include <sys/tsol/tnet.h>
2738348SEric.Yu@Sun.COM #include <sys/sockio.h>
2741676Sjpk 
2753448Sdh155122 /* Old value for compatibility. Setable in /etc/system */
2760Sstevel@tonic-gate uint_t tcp_conn_hash_size = 0;
2770Sstevel@tonic-gate 
2783448Sdh155122 /* New value. Zero means choose automatically.  Setable in /etc/system */
2790Sstevel@tonic-gate uint_t ipcl_conn_hash_size = 0;
2800Sstevel@tonic-gate uint_t ipcl_conn_hash_memfactor = 8192;
2810Sstevel@tonic-gate uint_t ipcl_conn_hash_maxsize = 82500;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /* bind/udp fanout table size */
2840Sstevel@tonic-gate uint_t ipcl_bind_fanout_size = 512;
2851503Sericheng uint_t ipcl_udp_fanout_size = 16384;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /* Raw socket fanout size.  Must be a power of 2. */
2880Sstevel@tonic-gate uint_t ipcl_raw_fanout_size = 256;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
29110616SSebastien.Roy@Sun.COM  * The IPCL_IPTUN_HASH() function works best with a prime table size.  We
29210616SSebastien.Roy@Sun.COM  * expect that most large deployments would have hundreds of tunnels, and
29310616SSebastien.Roy@Sun.COM  * thousands in the extreme case.
29410616SSebastien.Roy@Sun.COM  */
29510616SSebastien.Roy@Sun.COM uint_t ipcl_iptun_fanout_size = 6143;
29610616SSebastien.Roy@Sun.COM 
29710616SSebastien.Roy@Sun.COM /*
2980Sstevel@tonic-gate  * Power of 2^N Primes useful for hashing for N of 0-28,
2990Sstevel@tonic-gate  * these primes are the nearest prime <= 2^N - 2^(N-2).
3000Sstevel@tonic-gate  */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate #define	P2Ps() {0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,	\
3030Sstevel@tonic-gate 		6143, 12281, 24571, 49139, 98299, 196597, 393209,	\
3040Sstevel@tonic-gate 		786431, 1572853, 3145721, 6291449, 12582893, 25165813,	\
3050Sstevel@tonic-gate 		50331599, 100663291, 201326557, 0}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3085240Snordmark  * wrapper structure to ensure that conn and what follows it (tcp_t, etc)
3095240Snordmark  * are aligned on cache lines.
3100Sstevel@tonic-gate  */
3115240Snordmark typedef union itc_s {
3125240Snordmark 	conn_t	itc_conn;
3135240Snordmark 	char	itcu_filler[CACHE_ALIGN(conn_s)];
3140Sstevel@tonic-gate } itc_t;
3150Sstevel@tonic-gate 
3165240Snordmark struct kmem_cache  *tcp_conn_cache;
3175240Snordmark struct kmem_cache  *ip_conn_cache;
3180Sstevel@tonic-gate extern struct kmem_cache  *sctp_conn_cache;
3195240Snordmark struct kmem_cache  *udp_conn_cache;
3205240Snordmark struct kmem_cache  *rawip_conn_cache;
3215240Snordmark struct kmem_cache  *rts_conn_cache;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate extern void	tcp_timermp_free(tcp_t *);
3240Sstevel@tonic-gate extern mblk_t	*tcp_timermp_alloc(int);
3250Sstevel@tonic-gate 
3265240Snordmark static int	ip_conn_constructor(void *, void *, int);
3275240Snordmark static void	ip_conn_destructor(void *, void *);
3285240Snordmark 
3295240Snordmark static int	tcp_conn_constructor(void *, void *, int);
3305240Snordmark static void	tcp_conn_destructor(void *, void *);
3315240Snordmark 
3325240Snordmark static int	udp_conn_constructor(void *, void *, int);
3335240Snordmark static void	udp_conn_destructor(void *, void *);
3345240Snordmark 
3355240Snordmark static int	rawip_conn_constructor(void *, void *, int);
3365240Snordmark static void	rawip_conn_destructor(void *, void *);
3375240Snordmark 
3385240Snordmark static int	rts_conn_constructor(void *, void *, int);
3395240Snordmark static void	rts_conn_destructor(void *, void *);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3423448Sdh155122  * Global (for all stack instances) init routine
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate void
3453448Sdh155122 ipcl_g_init(void)
3460Sstevel@tonic-gate {
3475240Snordmark 	ip_conn_cache = kmem_cache_create("ip_conn_cache",
3480Sstevel@tonic-gate 	    sizeof (conn_t), CACHE_ALIGN_SIZE,
3495240Snordmark 	    ip_conn_constructor, ip_conn_destructor,
3505240Snordmark 	    NULL, NULL, NULL, 0);
3515240Snordmark 
3525240Snordmark 	tcp_conn_cache = kmem_cache_create("tcp_conn_cache",
3535240Snordmark 	    sizeof (itc_t) + sizeof (tcp_t), CACHE_ALIGN_SIZE,
3545240Snordmark 	    tcp_conn_constructor, tcp_conn_destructor,
35511303SKacheong.Poon@Sun.COM 	    tcp_conn_reclaim, NULL, NULL, 0);
3560Sstevel@tonic-gate 
3575240Snordmark 	udp_conn_cache = kmem_cache_create("udp_conn_cache",
3585240Snordmark 	    sizeof (itc_t) + sizeof (udp_t), CACHE_ALIGN_SIZE,
3595240Snordmark 	    udp_conn_constructor, udp_conn_destructor,
3605240Snordmark 	    NULL, NULL, NULL, 0);
3615240Snordmark 
3625240Snordmark 	rawip_conn_cache = kmem_cache_create("rawip_conn_cache",
3635240Snordmark 	    sizeof (itc_t) + sizeof (icmp_t), CACHE_ALIGN_SIZE,
3645240Snordmark 	    rawip_conn_constructor, rawip_conn_destructor,
3655240Snordmark 	    NULL, NULL, NULL, 0);
3665240Snordmark 
3675240Snordmark 	rts_conn_cache = kmem_cache_create("rts_conn_cache",
3685240Snordmark 	    sizeof (itc_t) + sizeof (rts_t), CACHE_ALIGN_SIZE,
3695240Snordmark 	    rts_conn_constructor, rts_conn_destructor,
3700Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
3713448Sdh155122 }
3723448Sdh155122 
3733448Sdh155122 /*
3743448Sdh155122  * ipclassifier intialization routine, sets up hash tables.
3753448Sdh155122  */
3763448Sdh155122 void
3773448Sdh155122 ipcl_init(ip_stack_t *ipst)
3783448Sdh155122 {
3793448Sdh155122 	int i;
3803448Sdh155122 	int sizes[] = P2Ps();
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3833448Sdh155122 	 * Calculate size of conn fanout table from /etc/system settings
3840Sstevel@tonic-gate 	 */
3850Sstevel@tonic-gate 	if (ipcl_conn_hash_size != 0) {
3863448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = ipcl_conn_hash_size;
3870Sstevel@tonic-gate 	} else if (tcp_conn_hash_size != 0) {
3883448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = tcp_conn_hash_size;
3890Sstevel@tonic-gate 	} else {
3900Sstevel@tonic-gate 		extern pgcnt_t freemem;
3910Sstevel@tonic-gate 
3923448Sdh155122 		ipst->ips_ipcl_conn_fanout_size =
3930Sstevel@tonic-gate 		    (freemem * PAGESIZE) / ipcl_conn_hash_memfactor;
3940Sstevel@tonic-gate 
3953448Sdh155122 		if (ipst->ips_ipcl_conn_fanout_size > ipcl_conn_hash_maxsize) {
3963448Sdh155122 			ipst->ips_ipcl_conn_fanout_size =
3973448Sdh155122 			    ipcl_conn_hash_maxsize;
3983448Sdh155122 		}
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	for (i = 9; i < sizeof (sizes) / sizeof (*sizes) - 1; i++) {
4023448Sdh155122 		if (sizes[i] >= ipst->ips_ipcl_conn_fanout_size) {
4030Sstevel@tonic-gate 			break;
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 	}
4063448Sdh155122 	if ((ipst->ips_ipcl_conn_fanout_size = sizes[i]) == 0) {
4070Sstevel@tonic-gate 		/* Out of range, use the 2^16 value */
4083448Sdh155122 		ipst->ips_ipcl_conn_fanout_size = sizes[16];
4090Sstevel@tonic-gate 	}
4103448Sdh155122 
4113448Sdh155122 	/* Take values from /etc/system */
4123448Sdh155122 	ipst->ips_ipcl_bind_fanout_size = ipcl_bind_fanout_size;
4133448Sdh155122 	ipst->ips_ipcl_udp_fanout_size = ipcl_udp_fanout_size;
4143448Sdh155122 	ipst->ips_ipcl_raw_fanout_size = ipcl_raw_fanout_size;
41510616SSebastien.Roy@Sun.COM 	ipst->ips_ipcl_iptun_fanout_size = ipcl_iptun_fanout_size;
4160Sstevel@tonic-gate 
4173448Sdh155122 	ASSERT(ipst->ips_ipcl_conn_fanout == NULL);
4183448Sdh155122 
4193448Sdh155122 	ipst->ips_ipcl_conn_fanout = kmem_zalloc(
4203448Sdh155122 	    ipst->ips_ipcl_conn_fanout_size * sizeof (connf_t), KM_SLEEP);
4213448Sdh155122 
4223448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
4233448Sdh155122 		mutex_init(&ipst->ips_ipcl_conn_fanout[i].connf_lock, NULL,
4240Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4273448Sdh155122 	ipst->ips_ipcl_bind_fanout = kmem_zalloc(
4283448Sdh155122 	    ipst->ips_ipcl_bind_fanout_size * sizeof (connf_t), KM_SLEEP);
4290Sstevel@tonic-gate 
4303448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
4313448Sdh155122 		mutex_init(&ipst->ips_ipcl_bind_fanout[i].connf_lock, NULL,
4320Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
43511042SErik.Nordmark@Sun.COM 	ipst->ips_ipcl_proto_fanout_v4 = kmem_zalloc(IPPROTO_MAX *
4363448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4373448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
43811042SErik.Nordmark@Sun.COM 		mutex_init(&ipst->ips_ipcl_proto_fanout_v4[i].connf_lock, NULL,
4390Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4400Sstevel@tonic-gate 	}
4413448Sdh155122 
4423448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = kmem_zalloc(IPPROTO_MAX *
4433448Sdh155122 	    sizeof (connf_t), KM_SLEEP);
4443448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
4453448Sdh155122 		mutex_init(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock, NULL,
4460Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 
4493448Sdh155122 	ipst->ips_rts_clients = kmem_zalloc(sizeof (connf_t), KM_SLEEP);
4503448Sdh155122 	mutex_init(&ipst->ips_rts_clients->connf_lock,
4513448Sdh155122 	    NULL, MUTEX_DEFAULT, NULL);
4520Sstevel@tonic-gate 
4533448Sdh155122 	ipst->ips_ipcl_udp_fanout = kmem_zalloc(
4543448Sdh155122 	    ipst->ips_ipcl_udp_fanout_size * sizeof (connf_t), KM_SLEEP);
4553448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
4563448Sdh155122 		mutex_init(&ipst->ips_ipcl_udp_fanout[i].connf_lock, NULL,
4570Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
46010616SSebastien.Roy@Sun.COM 	ipst->ips_ipcl_iptun_fanout = kmem_zalloc(
46110616SSebastien.Roy@Sun.COM 	    ipst->ips_ipcl_iptun_fanout_size * sizeof (connf_t), KM_SLEEP);
46210616SSebastien.Roy@Sun.COM 	for (i = 0; i < ipst->ips_ipcl_iptun_fanout_size; i++) {
46310616SSebastien.Roy@Sun.COM 		mutex_init(&ipst->ips_ipcl_iptun_fanout[i].connf_lock, NULL,
46410616SSebastien.Roy@Sun.COM 		    MUTEX_DEFAULT, NULL);
46510616SSebastien.Roy@Sun.COM 	}
46610616SSebastien.Roy@Sun.COM 
4673448Sdh155122 	ipst->ips_ipcl_raw_fanout = kmem_zalloc(
4683448Sdh155122 	    ipst->ips_ipcl_raw_fanout_size * sizeof (connf_t), KM_SLEEP);
4693448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
4703448Sdh155122 		mutex_init(&ipst->ips_ipcl_raw_fanout[i].connf_lock, NULL,
4710Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4743448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = kmem_zalloc(
4753448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE, KM_SLEEP);
4760Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
4773448Sdh155122 		mutex_init(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock,
4783448Sdh155122 		    NULL, MUTEX_DEFAULT, NULL);
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate void
4833448Sdh155122 ipcl_g_destroy(void)
4840Sstevel@tonic-gate {
4855240Snordmark 	kmem_cache_destroy(ip_conn_cache);
4865240Snordmark 	kmem_cache_destroy(tcp_conn_cache);
4875240Snordmark 	kmem_cache_destroy(udp_conn_cache);
4885240Snordmark 	kmem_cache_destroy(rawip_conn_cache);
4895240Snordmark 	kmem_cache_destroy(rts_conn_cache);
4903448Sdh155122 }
4913448Sdh155122 
4923448Sdh155122 /*
4933448Sdh155122  * All user-level and kernel use of the stack must be gone
4943448Sdh155122  * by now.
4953448Sdh155122  */
4963448Sdh155122 void
4973448Sdh155122 ipcl_destroy(ip_stack_t *ipst)
4983448Sdh155122 {
4993448Sdh155122 	int i;
5003448Sdh155122 
5013448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) {
5023448Sdh155122 		ASSERT(ipst->ips_ipcl_conn_fanout[i].connf_head == NULL);
5033448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_conn_fanout[i].connf_lock);
5043448Sdh155122 	}
5053448Sdh155122 	kmem_free(ipst->ips_ipcl_conn_fanout, ipst->ips_ipcl_conn_fanout_size *
5063448Sdh155122 	    sizeof (connf_t));
5073448Sdh155122 	ipst->ips_ipcl_conn_fanout = NULL;
5083448Sdh155122 
5093448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) {
5103448Sdh155122 		ASSERT(ipst->ips_ipcl_bind_fanout[i].connf_head == NULL);
5113448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_bind_fanout[i].connf_lock);
5123448Sdh155122 	}
5133448Sdh155122 	kmem_free(ipst->ips_ipcl_bind_fanout, ipst->ips_ipcl_bind_fanout_size *
5143448Sdh155122 	    sizeof (connf_t));
5153448Sdh155122 	ipst->ips_ipcl_bind_fanout = NULL;
5163448Sdh155122 
5173448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
51811042SErik.Nordmark@Sun.COM 		ASSERT(ipst->ips_ipcl_proto_fanout_v4[i].connf_head == NULL);
51911042SErik.Nordmark@Sun.COM 		mutex_destroy(&ipst->ips_ipcl_proto_fanout_v4[i].connf_lock);
5203448Sdh155122 	}
52111042SErik.Nordmark@Sun.COM 	kmem_free(ipst->ips_ipcl_proto_fanout_v4,
52211042SErik.Nordmark@Sun.COM 	    IPPROTO_MAX * sizeof (connf_t));
52311042SErik.Nordmark@Sun.COM 	ipst->ips_ipcl_proto_fanout_v4 = NULL;
5240Sstevel@tonic-gate 
5253448Sdh155122 	for (i = 0; i < IPPROTO_MAX; i++) {
5263448Sdh155122 		ASSERT(ipst->ips_ipcl_proto_fanout_v6[i].connf_head == NULL);
5273448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_proto_fanout_v6[i].connf_lock);
5283448Sdh155122 	}
5293448Sdh155122 	kmem_free(ipst->ips_ipcl_proto_fanout_v6,
5303448Sdh155122 	    IPPROTO_MAX * sizeof (connf_t));
5313448Sdh155122 	ipst->ips_ipcl_proto_fanout_v6 = NULL;
5323448Sdh155122 
5333448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_udp_fanout_size; i++) {
5343448Sdh155122 		ASSERT(ipst->ips_ipcl_udp_fanout[i].connf_head == NULL);
5353448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_udp_fanout[i].connf_lock);
5363448Sdh155122 	}
5373448Sdh155122 	kmem_free(ipst->ips_ipcl_udp_fanout, ipst->ips_ipcl_udp_fanout_size *
5383448Sdh155122 	    sizeof (connf_t));
5393448Sdh155122 	ipst->ips_ipcl_udp_fanout = NULL;
5400Sstevel@tonic-gate 
54110616SSebastien.Roy@Sun.COM 	for (i = 0; i < ipst->ips_ipcl_iptun_fanout_size; i++) {
54210616SSebastien.Roy@Sun.COM 		ASSERT(ipst->ips_ipcl_iptun_fanout[i].connf_head == NULL);
54310616SSebastien.Roy@Sun.COM 		mutex_destroy(&ipst->ips_ipcl_iptun_fanout[i].connf_lock);
54410616SSebastien.Roy@Sun.COM 	}
54510616SSebastien.Roy@Sun.COM 	kmem_free(ipst->ips_ipcl_iptun_fanout,
54610616SSebastien.Roy@Sun.COM 	    ipst->ips_ipcl_iptun_fanout_size * sizeof (connf_t));
54710616SSebastien.Roy@Sun.COM 	ipst->ips_ipcl_iptun_fanout = NULL;
54810616SSebastien.Roy@Sun.COM 
5493448Sdh155122 	for (i = 0; i < ipst->ips_ipcl_raw_fanout_size; i++) {
5503448Sdh155122 		ASSERT(ipst->ips_ipcl_raw_fanout[i].connf_head == NULL);
5513448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_raw_fanout[i].connf_lock);
5523448Sdh155122 	}
5533448Sdh155122 	kmem_free(ipst->ips_ipcl_raw_fanout, ipst->ips_ipcl_raw_fanout_size *
5543448Sdh155122 	    sizeof (connf_t));
5553448Sdh155122 	ipst->ips_ipcl_raw_fanout = NULL;
5560Sstevel@tonic-gate 
5573448Sdh155122 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
5583448Sdh155122 		ASSERT(ipst->ips_ipcl_globalhash_fanout[i].connf_head == NULL);
5593448Sdh155122 		mutex_destroy(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
5603448Sdh155122 	}
5613448Sdh155122 	kmem_free(ipst->ips_ipcl_globalhash_fanout,
5623448Sdh155122 	    sizeof (connf_t) * CONN_G_HASH_SIZE);
5633448Sdh155122 	ipst->ips_ipcl_globalhash_fanout = NULL;
5640Sstevel@tonic-gate 
5653448Sdh155122 	ASSERT(ipst->ips_rts_clients->connf_head == NULL);
5663448Sdh155122 	mutex_destroy(&ipst->ips_rts_clients->connf_lock);
5673448Sdh155122 	kmem_free(ipst->ips_rts_clients, sizeof (connf_t));
5683448Sdh155122 	ipst->ips_rts_clients = NULL;
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate  * conn creation routine. initialize the conn, sets the reference
5730Sstevel@tonic-gate  * and inserts it in the global hash table.
5740Sstevel@tonic-gate  */
5750Sstevel@tonic-gate conn_t *
5763448Sdh155122 ipcl_conn_create(uint32_t type, int sleep, netstack_t *ns)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	conn_t	*connp;
5795240Snordmark 	struct kmem_cache *conn_cache;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	switch (type) {
5820Sstevel@tonic-gate 	case IPCL_SCTPCONN:
5830Sstevel@tonic-gate 		if ((connp = kmem_cache_alloc(sctp_conn_cache, sleep)) == NULL)
5840Sstevel@tonic-gate 			return (NULL);
5854691Skcpoon 		sctp_conn_init(connp);
5863448Sdh155122 		netstack_hold(ns);
5873448Sdh155122 		connp->conn_netstack = ns;
58811042SErik.Nordmark@Sun.COM 		connp->conn_ixa->ixa_ipst = ns->netstack_ip;
589*12507SAlan.Maguire@Sun.COM 		connp->conn_ixa->ixa_conn_id = (long)connp;
59011042SErik.Nordmark@Sun.COM 		ipcl_globalhash_insert(connp);
5915240Snordmark 		return (connp);
5925240Snordmark 
5935240Snordmark 	case IPCL_TCPCONN:
5945240Snordmark 		conn_cache = tcp_conn_cache;
5950Sstevel@tonic-gate 		break;
5965240Snordmark 
5975240Snordmark 	case IPCL_UDPCONN:
5985240Snordmark 		conn_cache = udp_conn_cache;
5995240Snordmark 		break;
6005240Snordmark 
6015240Snordmark 	case IPCL_RAWIPCONN:
6025240Snordmark 		conn_cache = rawip_conn_cache;
6035240Snordmark 		break;
6045240Snordmark 
6055240Snordmark 	case IPCL_RTSCONN:
6065240Snordmark 		conn_cache = rts_conn_cache;
6075240Snordmark 		break;
6085240Snordmark 
6090Sstevel@tonic-gate 	case IPCL_IPCCONN:
6105240Snordmark 		conn_cache = ip_conn_cache;
6110Sstevel@tonic-gate 		break;
6125240Snordmark 
613741Smasputra 	default:
614741Smasputra 		connp = NULL;
615741Smasputra 		ASSERT(0);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6185240Snordmark 	if ((connp = kmem_cache_alloc(conn_cache, sleep)) == NULL)
6195240Snordmark 		return (NULL);
6205240Snordmark 
6215240Snordmark 	connp->conn_ref = 1;
6225240Snordmark 	netstack_hold(ns);
6235240Snordmark 	connp->conn_netstack = ns;
62411042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_ipst = ns->netstack_ip;
625*12507SAlan.Maguire@Sun.COM 	connp->conn_ixa->ixa_conn_id = (long)connp;
6265240Snordmark 	ipcl_globalhash_insert(connp);
6270Sstevel@tonic-gate 	return (connp);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate void
6310Sstevel@tonic-gate ipcl_conn_destroy(conn_t *connp)
6320Sstevel@tonic-gate {
6330Sstevel@tonic-gate 	mblk_t	*mp;
6343448Sdh155122 	netstack_t	*ns = connp->conn_netstack;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&connp->conn_lock));
6370Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6380Sstevel@tonic-gate 
6397502Saruna@cs.umn.edu 	DTRACE_PROBE1(conn__destroy, conn_t *, connp);
6407502Saruna@cs.umn.edu 
6411676Sjpk 	if (connp->conn_cred != NULL) {
6421676Sjpk 		crfree(connp->conn_cred);
6431676Sjpk 		connp->conn_cred = NULL;
64411680SErik.Nordmark@Sun.COM 		/* ixa_cred done in ipcl_conn_cleanup below */
6451676Sjpk 	}
6461676Sjpk 
64711042SErik.Nordmark@Sun.COM 	if (connp->conn_ht_iphc != NULL) {
64811042SErik.Nordmark@Sun.COM 		kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
64911042SErik.Nordmark@Sun.COM 		connp->conn_ht_iphc = NULL;
65011042SErik.Nordmark@Sun.COM 		connp->conn_ht_iphc_allocated = 0;
65111042SErik.Nordmark@Sun.COM 		connp->conn_ht_iphc_len = 0;
65211042SErik.Nordmark@Sun.COM 		connp->conn_ht_ulp = NULL;
65311042SErik.Nordmark@Sun.COM 		connp->conn_ht_ulp_len = 0;
65411042SErik.Nordmark@Sun.COM 	}
65511042SErik.Nordmark@Sun.COM 	ip_pkt_free(&connp->conn_xmit_ipp);
65611042SErik.Nordmark@Sun.COM 
6570Sstevel@tonic-gate 	ipcl_globalhash_remove(connp);
6580Sstevel@tonic-gate 
65911042SErik.Nordmark@Sun.COM 	if (connp->conn_latch != NULL) {
66011042SErik.Nordmark@Sun.COM 		IPLATCH_REFRELE(connp->conn_latch);
66111042SErik.Nordmark@Sun.COM 		connp->conn_latch = NULL;
66211042SErik.Nordmark@Sun.COM 	}
66311042SErik.Nordmark@Sun.COM 	if (connp->conn_latch_in_policy != NULL) {
66411042SErik.Nordmark@Sun.COM 		IPPOL_REFRELE(connp->conn_latch_in_policy);
66511042SErik.Nordmark@Sun.COM 		connp->conn_latch_in_policy = NULL;
66611042SErik.Nordmark@Sun.COM 	}
66711042SErik.Nordmark@Sun.COM 	if (connp->conn_latch_in_action != NULL) {
66811042SErik.Nordmark@Sun.COM 		IPACT_REFRELE(connp->conn_latch_in_action);
66911042SErik.Nordmark@Sun.COM 		connp->conn_latch_in_action = NULL;
67011042SErik.Nordmark@Sun.COM 	}
67111042SErik.Nordmark@Sun.COM 	if (connp->conn_policy != NULL) {
67211042SErik.Nordmark@Sun.COM 		IPPH_REFRELE(connp->conn_policy, ns);
67311042SErik.Nordmark@Sun.COM 		connp->conn_policy = NULL;
67411042SErik.Nordmark@Sun.COM 	}
6753448Sdh155122 
67611042SErik.Nordmark@Sun.COM 	if (connp->conn_ipsec_opt_mp != NULL) {
67711042SErik.Nordmark@Sun.COM 		freemsg(connp->conn_ipsec_opt_mp);
67811042SErik.Nordmark@Sun.COM 		connp->conn_ipsec_opt_mp = NULL;
67911042SErik.Nordmark@Sun.COM 	}
68011042SErik.Nordmark@Sun.COM 
68111042SErik.Nordmark@Sun.COM 	if (connp->conn_flags & IPCL_TCPCONN) {
68211042SErik.Nordmark@Sun.COM 		tcp_t *tcp = connp->conn_tcp;
683741Smasputra 
6840Sstevel@tonic-gate 		tcp_free(tcp);
6850Sstevel@tonic-gate 		mp = tcp->tcp_timercache;
68611042SErik.Nordmark@Sun.COM 
68711042SErik.Nordmark@Sun.COM 		tcp->tcp_tcps = NULL;
6880Sstevel@tonic-gate 
6898014SKacheong.Poon@Sun.COM 		/*
6908014SKacheong.Poon@Sun.COM 		 * tcp_rsrv_mp can be NULL if tcp_get_conn() fails to allocate
6918014SKacheong.Poon@Sun.COM 		 * the mblk.
6928014SKacheong.Poon@Sun.COM 		 */
6938014SKacheong.Poon@Sun.COM 		if (tcp->tcp_rsrv_mp != NULL) {
6948014SKacheong.Poon@Sun.COM 			freeb(tcp->tcp_rsrv_mp);
6958014SKacheong.Poon@Sun.COM 			tcp->tcp_rsrv_mp = NULL;
6968014SKacheong.Poon@Sun.COM 			mutex_destroy(&tcp->tcp_rsrv_mp_lock);
6978014SKacheong.Poon@Sun.COM 		}
6988014SKacheong.Poon@Sun.COM 
69911042SErik.Nordmark@Sun.COM 		ipcl_conn_cleanup(connp);
70011042SErik.Nordmark@Sun.COM 		connp->conn_flags = IPCL_TCPCONN;
7013448Sdh155122 		if (ns != NULL) {
7023448Sdh155122 			ASSERT(tcp->tcp_tcps == NULL);
7033448Sdh155122 			connp->conn_netstack = NULL;
70411042SErik.Nordmark@Sun.COM 			connp->conn_ixa->ixa_ipst = NULL;
7053448Sdh155122 			netstack_rele(ns);
7063448Sdh155122 		}
7075240Snordmark 
7085240Snordmark 		bzero(tcp, sizeof (tcp_t));
7095240Snordmark 
7105240Snordmark 		tcp->tcp_timercache = mp;
7115240Snordmark 		tcp->tcp_connp = connp;
7125240Snordmark 		kmem_cache_free(tcp_conn_cache, connp);
7135240Snordmark 		return;
7145240Snordmark 	}
7155240Snordmark 
7165240Snordmark 	if (connp->conn_flags & IPCL_SCTPCONN) {
7173448Sdh155122 		ASSERT(ns != NULL);
7180Sstevel@tonic-gate 		sctp_free(connp);
7195240Snordmark 		return;
7205240Snordmark 	}
7215240Snordmark 
72211042SErik.Nordmark@Sun.COM 	ipcl_conn_cleanup(connp);
7235240Snordmark 	if (ns != NULL) {
7245240Snordmark 		connp->conn_netstack = NULL;
72511042SErik.Nordmark@Sun.COM 		connp->conn_ixa->ixa_ipst = NULL;
7265240Snordmark 		netstack_rele(ns);
7275240Snordmark 	}
7288348SEric.Yu@Sun.COM 
7295240Snordmark 	/* leave conn_priv aka conn_udp, conn_icmp, etc in place. */
7305240Snordmark 	if (connp->conn_flags & IPCL_UDPCONN) {
7315240Snordmark 		connp->conn_flags = IPCL_UDPCONN;
7325240Snordmark 		kmem_cache_free(udp_conn_cache, connp);
7335240Snordmark 	} else if (connp->conn_flags & IPCL_RAWIPCONN) {
7345240Snordmark 		connp->conn_flags = IPCL_RAWIPCONN;
73511042SErik.Nordmark@Sun.COM 		connp->conn_proto = IPPROTO_ICMP;
73611042SErik.Nordmark@Sun.COM 		connp->conn_ixa->ixa_protocol = connp->conn_proto;
7375240Snordmark 		kmem_cache_free(rawip_conn_cache, connp);
7385240Snordmark 	} else if (connp->conn_flags & IPCL_RTSCONN) {
7395240Snordmark 		connp->conn_flags = IPCL_RTSCONN;
7405240Snordmark 		kmem_cache_free(rts_conn_cache, connp);
7410Sstevel@tonic-gate 	} else {
7425240Snordmark 		connp->conn_flags = IPCL_IPCCONN;
7435240Snordmark 		ASSERT(connp->conn_flags & IPCL_IPCCONN);
7445240Snordmark 		ASSERT(connp->conn_priv == NULL);
7455240Snordmark 		kmem_cache_free(ip_conn_cache, connp);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate  * Running in cluster mode - deregister listener information
7510Sstevel@tonic-gate  */
7520Sstevel@tonic-gate static void
7530Sstevel@tonic-gate ipcl_conn_unlisten(conn_t *connp)
7540Sstevel@tonic-gate {
7550Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) != 0);
7560Sstevel@tonic-gate 	ASSERT(connp->conn_lport != 0);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	if (cl_inet_unlisten != NULL) {
7590Sstevel@tonic-gate 		sa_family_t	addr_family;
7600Sstevel@tonic-gate 		uint8_t		*laddrp;
7610Sstevel@tonic-gate 
76211042SErik.Nordmark@Sun.COM 		if (connp->conn_ipversion == IPV6_VERSION) {
7630Sstevel@tonic-gate 			addr_family = AF_INET6;
76411042SErik.Nordmark@Sun.COM 			laddrp = (uint8_t *)&connp->conn_bound_addr_v6;
7650Sstevel@tonic-gate 		} else {
7660Sstevel@tonic-gate 			addr_family = AF_INET;
76711042SErik.Nordmark@Sun.COM 			laddrp = (uint8_t *)&connp->conn_bound_addr_v4;
7680Sstevel@tonic-gate 		}
7698392SHuafeng.Lv@Sun.COM 		(*cl_inet_unlisten)(connp->conn_netstack->netstack_stackid,
7708392SHuafeng.Lv@Sun.COM 		    IPPROTO_TCP, addr_family, laddrp, connp->conn_lport, NULL);
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 	connp->conn_flags &= ~IPCL_CL_LISTENER;
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate  * We set the IPCL_REMOVED flag (instead of clearing the flag indicating
7770Sstevel@tonic-gate  * which table the conn belonged to). So for debugging we can see which hash
7780Sstevel@tonic-gate  * table this connection was in.
7790Sstevel@tonic-gate  */
7800Sstevel@tonic-gate #define	IPCL_HASH_REMOVE(connp)	{					\
7810Sstevel@tonic-gate 	connf_t	*connfp = (connp)->conn_fanout;				\
7820Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&((connp)->conn_lock)));			\
7830Sstevel@tonic-gate 	if (connfp != NULL) {						\
7840Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);			\
7850Sstevel@tonic-gate 		if ((connp)->conn_next != NULL)				\
7860Sstevel@tonic-gate 			(connp)->conn_next->conn_prev =			\
7870Sstevel@tonic-gate 			    (connp)->conn_prev;				\
7880Sstevel@tonic-gate 		if ((connp)->conn_prev != NULL)				\
7890Sstevel@tonic-gate 			(connp)->conn_prev->conn_next =			\
7900Sstevel@tonic-gate 			    (connp)->conn_next;				\
7910Sstevel@tonic-gate 		else							\
7920Sstevel@tonic-gate 			connfp->connf_head = (connp)->conn_next;	\
7930Sstevel@tonic-gate 		(connp)->conn_fanout = NULL;				\
7940Sstevel@tonic-gate 		(connp)->conn_next = NULL;				\
7950Sstevel@tonic-gate 		(connp)->conn_prev = NULL;				\
7960Sstevel@tonic-gate 		(connp)->conn_flags |= IPCL_REMOVED;			\
7970Sstevel@tonic-gate 		if (((connp)->conn_flags & IPCL_CL_LISTENER) != 0)	\
7980Sstevel@tonic-gate 			ipcl_conn_unlisten((connp));			\
7990Sstevel@tonic-gate 		CONN_DEC_REF((connp));					\
8000Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);			\
8010Sstevel@tonic-gate 	}								\
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate void
8050Sstevel@tonic-gate ipcl_hash_remove(conn_t *connp)
8060Sstevel@tonic-gate {
80711042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
80811042SErik.Nordmark@Sun.COM 
8090Sstevel@tonic-gate 	IPCL_HASH_REMOVE(connp);
81011042SErik.Nordmark@Sun.COM 	if (protocol == IPPROTO_RSVP)
81111042SErik.Nordmark@Sun.COM 		ill_set_inputfn_all(connp->conn_netstack->netstack_ip);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate  * The whole purpose of this function is allow removal of
8160Sstevel@tonic-gate  * a conn_t from the connected hash for timewait reclaim.
8170Sstevel@tonic-gate  * This is essentially a TW reclaim fastpath where timewait
8180Sstevel@tonic-gate  * collector checks under fanout lock (so no one else can
8190Sstevel@tonic-gate  * get access to the conn_t) that refcnt is 2 i.e. one for
8200Sstevel@tonic-gate  * TCP and one for the classifier hash list. If ref count
8210Sstevel@tonic-gate  * is indeed 2, we can just remove the conn under lock and
8220Sstevel@tonic-gate  * avoid cleaning up the conn under squeue. This gives us
8230Sstevel@tonic-gate  * improved performance.
8240Sstevel@tonic-gate  */
8250Sstevel@tonic-gate void
8260Sstevel@tonic-gate ipcl_hash_remove_locked(conn_t *connp, connf_t	*connfp)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connfp->connf_lock));
8290Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
8300Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) == 0);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if ((connp)->conn_next != NULL) {
8334691Skcpoon 		(connp)->conn_next->conn_prev = (connp)->conn_prev;
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	if ((connp)->conn_prev != NULL) {
8364691Skcpoon 		(connp)->conn_prev->conn_next = (connp)->conn_next;
8370Sstevel@tonic-gate 	} else {
8380Sstevel@tonic-gate 		connfp->connf_head = (connp)->conn_next;
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 	(connp)->conn_fanout = NULL;
8410Sstevel@tonic-gate 	(connp)->conn_next = NULL;
8420Sstevel@tonic-gate 	(connp)->conn_prev = NULL;
8430Sstevel@tonic-gate 	(connp)->conn_flags |= IPCL_REMOVED;
8440Sstevel@tonic-gate 	ASSERT((connp)->conn_ref == 2);
8450Sstevel@tonic-gate 	(connp)->conn_ref--;
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp) {		\
8490Sstevel@tonic-gate 	ASSERT((connp)->conn_fanout == NULL);				\
8500Sstevel@tonic-gate 	ASSERT((connp)->conn_next == NULL);				\
8510Sstevel@tonic-gate 	ASSERT((connp)->conn_prev == NULL);				\
8520Sstevel@tonic-gate 	if ((connfp)->connf_head != NULL) {				\
8530Sstevel@tonic-gate 		(connfp)->connf_head->conn_prev = (connp);		\
8540Sstevel@tonic-gate 		(connp)->conn_next = (connfp)->connf_head;		\
8550Sstevel@tonic-gate 	}								\
8560Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8570Sstevel@tonic-gate 	(connfp)->connf_head = (connp);					\
8580Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8590Sstevel@tonic-gate 	    IPCL_CONNECTED;						\
8600Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED(connfp, connp) {			\
8640Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8650Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8660Sstevel@tonic-gate 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);		\
8670Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate #define	IPCL_HASH_INSERT_BOUND(connfp, connp) {				\
8710Sstevel@tonic-gate 	conn_t *pconnp = NULL, *nconnp;					\
8720Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8730Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8740Sstevel@tonic-gate 	nconnp = (connfp)->connf_head;					\
875153Sethindra 	while (nconnp != NULL &&					\
87611042SErik.Nordmark@Sun.COM 	    !_IPCL_V4_MATCH_ANY(nconnp->conn_laddr_v6)) {		\
877153Sethindra 		pconnp = nconnp;					\
878153Sethindra 		nconnp = nconnp->conn_next;				\
8790Sstevel@tonic-gate 	}								\
8800Sstevel@tonic-gate 	if (pconnp != NULL) {						\
8810Sstevel@tonic-gate 		pconnp->conn_next = (connp);				\
8820Sstevel@tonic-gate 		(connp)->conn_prev = pconnp;				\
8830Sstevel@tonic-gate 	} else {							\
8840Sstevel@tonic-gate 		(connfp)->connf_head = (connp);				\
8850Sstevel@tonic-gate 	}								\
8860Sstevel@tonic-gate 	if (nconnp != NULL) {						\
8870Sstevel@tonic-gate 		(connp)->conn_next = nconnp;				\
8880Sstevel@tonic-gate 		nconnp->conn_prev = (connp);				\
8890Sstevel@tonic-gate 	}								\
8900Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8910Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8920Sstevel@tonic-gate 	    IPCL_BOUND;							\
8930Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
8940Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate #define	IPCL_HASH_INSERT_WILDCARD(connfp, connp) {			\
8980Sstevel@tonic-gate 	conn_t **list, *prev, *next;					\
8990Sstevel@tonic-gate 	boolean_t isv4mapped =						\
90011042SErik.Nordmark@Sun.COM 	    IN6_IS_ADDR_V4MAPPED(&(connp)->conn_laddr_v6);		\
9010Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
9020Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
9030Sstevel@tonic-gate 	list = &(connfp)->connf_head;					\
9040Sstevel@tonic-gate 	prev = NULL;							\
9050Sstevel@tonic-gate 	while ((next = *list) != NULL) {				\
9060Sstevel@tonic-gate 		if (isv4mapped &&					\
90711042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_UNSPECIFIED(&next->conn_laddr_v6) &&	\
9080Sstevel@tonic-gate 		    connp->conn_zoneid == next->conn_zoneid) {		\
9090Sstevel@tonic-gate 			(connp)->conn_next = next;			\
9100Sstevel@tonic-gate 			if (prev != NULL)				\
9110Sstevel@tonic-gate 				prev = next->conn_prev;			\
9120Sstevel@tonic-gate 			next->conn_prev = (connp);			\
9130Sstevel@tonic-gate 			break;						\
9140Sstevel@tonic-gate 		}							\
9150Sstevel@tonic-gate 		list = &next->conn_next;				\
9160Sstevel@tonic-gate 		prev = next;						\
9170Sstevel@tonic-gate 	}								\
9180Sstevel@tonic-gate 	(connp)->conn_prev = prev;					\
9190Sstevel@tonic-gate 	*list = (connp);						\
9200Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9210Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9220Sstevel@tonic-gate 	    IPCL_BOUND;							\
9230Sstevel@tonic-gate 	CONN_INC_REF((connp));						\
9240Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate void
9280Sstevel@tonic-gate ipcl_hash_insert_wildcard(connf_t *connfp, conn_t *connp)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate /*
93410616SSebastien.Roy@Sun.COM  * Because the classifier is used to classify inbound packets, the destination
93510616SSebastien.Roy@Sun.COM  * address is meant to be our local tunnel address (tunnel source), and the
93610616SSebastien.Roy@Sun.COM  * source the remote tunnel address (tunnel destination).
93711042SErik.Nordmark@Sun.COM  *
93811042SErik.Nordmark@Sun.COM  * Note that conn_proto can't be used for fanout since the upper protocol
93911042SErik.Nordmark@Sun.COM  * can be both 41 and 4 when IPv6 and IPv4 are over the same tunnel.
94010616SSebastien.Roy@Sun.COM  */
94110616SSebastien.Roy@Sun.COM conn_t *
94210616SSebastien.Roy@Sun.COM ipcl_iptun_classify_v4(ipaddr_t *src, ipaddr_t *dst, ip_stack_t *ipst)
94310616SSebastien.Roy@Sun.COM {
94410616SSebastien.Roy@Sun.COM 	connf_t	*connfp;
94510616SSebastien.Roy@Sun.COM 	conn_t	*connp;
94610616SSebastien.Roy@Sun.COM 
94710616SSebastien.Roy@Sun.COM 	/* first look for IPv4 tunnel links */
94810616SSebastien.Roy@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH(*dst, *src)];
94910616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
95010616SSebastien.Roy@Sun.COM 	for (connp = connfp->connf_head; connp != NULL;
95110616SSebastien.Roy@Sun.COM 	    connp = connp->conn_next) {
95210616SSebastien.Roy@Sun.COM 		if (IPCL_IPTUN_MATCH(connp, *dst, *src))
95310616SSebastien.Roy@Sun.COM 			break;
95410616SSebastien.Roy@Sun.COM 	}
95510616SSebastien.Roy@Sun.COM 	if (connp != NULL)
95610616SSebastien.Roy@Sun.COM 		goto done;
95710616SSebastien.Roy@Sun.COM 
95810616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
95910616SSebastien.Roy@Sun.COM 
96010616SSebastien.Roy@Sun.COM 	/* We didn't find an IPv4 tunnel, try a 6to4 tunnel */
96110616SSebastien.Roy@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH(*dst,
96210616SSebastien.Roy@Sun.COM 	    INADDR_ANY)];
96310616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
96410616SSebastien.Roy@Sun.COM 	for (connp = connfp->connf_head; connp != NULL;
96510616SSebastien.Roy@Sun.COM 	    connp = connp->conn_next) {
96610616SSebastien.Roy@Sun.COM 		if (IPCL_IPTUN_MATCH(connp, *dst, INADDR_ANY))
96710616SSebastien.Roy@Sun.COM 			break;
96810616SSebastien.Roy@Sun.COM 	}
96910616SSebastien.Roy@Sun.COM done:
97010616SSebastien.Roy@Sun.COM 	if (connp != NULL)
97110616SSebastien.Roy@Sun.COM 		CONN_INC_REF(connp);
97210616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
97310616SSebastien.Roy@Sun.COM 	return (connp);
97410616SSebastien.Roy@Sun.COM }
97510616SSebastien.Roy@Sun.COM 
97610616SSebastien.Roy@Sun.COM conn_t *
97710616SSebastien.Roy@Sun.COM ipcl_iptun_classify_v6(in6_addr_t *src, in6_addr_t *dst, ip_stack_t *ipst)
97810616SSebastien.Roy@Sun.COM {
97910616SSebastien.Roy@Sun.COM 	connf_t	*connfp;
98010616SSebastien.Roy@Sun.COM 	conn_t	*connp;
98110616SSebastien.Roy@Sun.COM 
98210616SSebastien.Roy@Sun.COM 	/* Look for an IPv6 tunnel link */
98310616SSebastien.Roy@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH_V6(dst, src)];
98410616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
98510616SSebastien.Roy@Sun.COM 	for (connp = connfp->connf_head; connp != NULL;
98610616SSebastien.Roy@Sun.COM 	    connp = connp->conn_next) {
98710616SSebastien.Roy@Sun.COM 		if (IPCL_IPTUN_MATCH_V6(connp, dst, src)) {
98810616SSebastien.Roy@Sun.COM 			CONN_INC_REF(connp);
98910616SSebastien.Roy@Sun.COM 			break;
99010616SSebastien.Roy@Sun.COM 		}
99110616SSebastien.Roy@Sun.COM 	}
99210616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
99310616SSebastien.Roy@Sun.COM 	return (connp);
99410616SSebastien.Roy@Sun.COM }
99510616SSebastien.Roy@Sun.COM 
99610616SSebastien.Roy@Sun.COM /*
9970Sstevel@tonic-gate  * This function is used only for inserting SCTP raw socket now.
9980Sstevel@tonic-gate  * This may change later.
9990Sstevel@tonic-gate  *
10000Sstevel@tonic-gate  * Note that only one raw socket can be bound to a port.  The param
10010Sstevel@tonic-gate  * lport is in network byte order.
10020Sstevel@tonic-gate  */
10030Sstevel@tonic-gate static int
10040Sstevel@tonic-gate ipcl_sctp_hash_insert(conn_t *connp, in_port_t lport)
10050Sstevel@tonic-gate {
10060Sstevel@tonic-gate 	connf_t	*connfp;
10070Sstevel@tonic-gate 	conn_t	*oconnp;
10083448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
10090Sstevel@tonic-gate 
10103448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	/* Check for existing raw socket already bound to the port. */
10130Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
10140Sstevel@tonic-gate 	for (oconnp = connfp->connf_head; oconnp != NULL;
1015409Skcpoon 	    oconnp = oconnp->conn_next) {
10160Sstevel@tonic-gate 		if (oconnp->conn_lport == lport &&
10170Sstevel@tonic-gate 		    oconnp->conn_zoneid == connp->conn_zoneid &&
101811042SErik.Nordmark@Sun.COM 		    oconnp->conn_family == connp->conn_family &&
101911042SErik.Nordmark@Sun.COM 		    ((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6) ||
102011042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_UNSPECIFIED(&oconnp->conn_laddr_v6) ||
102111042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_laddr_v6) ||
102211042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_V4MAPPED_ANY(&oconnp->conn_laddr_v6)) ||
102311042SErik.Nordmark@Sun.COM 		    IN6_ARE_ADDR_EQUAL(&oconnp->conn_laddr_v6,
102411042SErik.Nordmark@Sun.COM 		    &connp->conn_laddr_v6))) {
10250Sstevel@tonic-gate 			break;
10260Sstevel@tonic-gate 		}
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
10290Sstevel@tonic-gate 	if (oconnp != NULL)
10300Sstevel@tonic-gate 		return (EADDRNOTAVAIL);
10310Sstevel@tonic-gate 
103211042SErik.Nordmark@Sun.COM 	if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6) ||
103311042SErik.Nordmark@Sun.COM 	    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_faddr_v6)) {
103411042SErik.Nordmark@Sun.COM 		if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6) ||
103511042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_laddr_v6)) {
10360Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10370Sstevel@tonic-gate 		} else {
10380Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
10390Sstevel@tonic-gate 		}
10400Sstevel@tonic-gate 	} else {
10410Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED(connfp, connp);
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate 	return (0);
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
104610616SSebastien.Roy@Sun.COM static int
104711042SErik.Nordmark@Sun.COM ipcl_iptun_hash_insert(conn_t *connp, ip_stack_t *ipst)
104810616SSebastien.Roy@Sun.COM {
104910616SSebastien.Roy@Sun.COM 	connf_t	*connfp;
105010616SSebastien.Roy@Sun.COM 	conn_t	*tconnp;
105111042SErik.Nordmark@Sun.COM 	ipaddr_t laddr = connp->conn_laddr_v4;
105211042SErik.Nordmark@Sun.COM 	ipaddr_t faddr = connp->conn_faddr_v4;
105310616SSebastien.Roy@Sun.COM 
105411042SErik.Nordmark@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH(laddr, faddr)];
105510616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
105610616SSebastien.Roy@Sun.COM 	for (tconnp = connfp->connf_head; tconnp != NULL;
105710616SSebastien.Roy@Sun.COM 	    tconnp = tconnp->conn_next) {
105811042SErik.Nordmark@Sun.COM 		if (IPCL_IPTUN_MATCH(tconnp, laddr, faddr)) {
105910616SSebastien.Roy@Sun.COM 			/* A tunnel is already bound to these addresses. */
106010616SSebastien.Roy@Sun.COM 			mutex_exit(&connfp->connf_lock);
106110616SSebastien.Roy@Sun.COM 			return (EADDRINUSE);
106210616SSebastien.Roy@Sun.COM 		}
106310616SSebastien.Roy@Sun.COM 	}
106410616SSebastien.Roy@Sun.COM 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
106510616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
106610616SSebastien.Roy@Sun.COM 	return (0);
106710616SSebastien.Roy@Sun.COM }
106810616SSebastien.Roy@Sun.COM 
106910616SSebastien.Roy@Sun.COM static int
107011042SErik.Nordmark@Sun.COM ipcl_iptun_hash_insert_v6(conn_t *connp, ip_stack_t *ipst)
107110616SSebastien.Roy@Sun.COM {
107210616SSebastien.Roy@Sun.COM 	connf_t	*connfp;
107310616SSebastien.Roy@Sun.COM 	conn_t	*tconnp;
107411042SErik.Nordmark@Sun.COM 	in6_addr_t *laddr = &connp->conn_laddr_v6;
107511042SErik.Nordmark@Sun.COM 	in6_addr_t *faddr = &connp->conn_faddr_v6;
107610616SSebastien.Roy@Sun.COM 
107711042SErik.Nordmark@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH_V6(laddr, faddr)];
107810616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
107910616SSebastien.Roy@Sun.COM 	for (tconnp = connfp->connf_head; tconnp != NULL;
108010616SSebastien.Roy@Sun.COM 	    tconnp = tconnp->conn_next) {
108111042SErik.Nordmark@Sun.COM 		if (IPCL_IPTUN_MATCH_V6(tconnp, laddr, faddr)) {
108210616SSebastien.Roy@Sun.COM 			/* A tunnel is already bound to these addresses. */
108310616SSebastien.Roy@Sun.COM 			mutex_exit(&connfp->connf_lock);
108410616SSebastien.Roy@Sun.COM 			return (EADDRINUSE);
108510616SSebastien.Roy@Sun.COM 		}
108610616SSebastien.Roy@Sun.COM 	}
108710616SSebastien.Roy@Sun.COM 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
108810616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
108910616SSebastien.Roy@Sun.COM 	return (0);
109010616SSebastien.Roy@Sun.COM }
109110616SSebastien.Roy@Sun.COM 
10920Sstevel@tonic-gate /*
10931676Sjpk  * Check for a MAC exemption conflict on a labeled system.  Note that for
10941676Sjpk  * protocols that use port numbers (UDP, TCP, SCTP), we do this check up in the
10951676Sjpk  * transport layer.  This check is for binding all other protocols.
10961676Sjpk  *
10971676Sjpk  * Returns true if there's a conflict.
10981676Sjpk  */
10991676Sjpk static boolean_t
11003448Sdh155122 check_exempt_conflict_v4(conn_t *connp, ip_stack_t *ipst)
11011676Sjpk {
11021676Sjpk 	connf_t	*connfp;
11031676Sjpk 	conn_t *tconn;
11041676Sjpk 
110511042SErik.Nordmark@Sun.COM 	connfp = &ipst->ips_ipcl_proto_fanout_v4[connp->conn_proto];
11061676Sjpk 	mutex_enter(&connfp->connf_lock);
11071676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
11081676Sjpk 	    tconn = tconn->conn_next) {
11091676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
111011042SErik.Nordmark@Sun.COM 		if (connp->conn_family != tconn->conn_family)
11111676Sjpk 			continue;
11121676Sjpk 		/* If neither is exempt, then there's no conflict */
111310934Ssommerfeld@sun.com 		if ((connp->conn_mac_mode == CONN_MAC_DEFAULT) &&
111410934Ssommerfeld@sun.com 		    (tconn->conn_mac_mode == CONN_MAC_DEFAULT))
11151676Sjpk 			continue;
11169710SKen.Powell@Sun.COM 		/* We are only concerned about sockets for a different zone */
11179710SKen.Powell@Sun.COM 		if (connp->conn_zoneid == tconn->conn_zoneid)
11189710SKen.Powell@Sun.COM 			continue;
11191676Sjpk 		/* If both are bound to different specific addrs, ok */
112011042SErik.Nordmark@Sun.COM 		if (connp->conn_laddr_v4 != INADDR_ANY &&
112111042SErik.Nordmark@Sun.COM 		    tconn->conn_laddr_v4 != INADDR_ANY &&
112211042SErik.Nordmark@Sun.COM 		    connp->conn_laddr_v4 != tconn->conn_laddr_v4)
11231676Sjpk 			continue;
11241676Sjpk 		/* These two conflict; fail */
11251676Sjpk 		break;
11261676Sjpk 	}
11271676Sjpk 	mutex_exit(&connfp->connf_lock);
11281676Sjpk 	return (tconn != NULL);
11291676Sjpk }
11301676Sjpk 
11311676Sjpk static boolean_t
11323448Sdh155122 check_exempt_conflict_v6(conn_t *connp, ip_stack_t *ipst)
11331676Sjpk {
11341676Sjpk 	connf_t	*connfp;
11351676Sjpk 	conn_t *tconn;
11361676Sjpk 
113711042SErik.Nordmark@Sun.COM 	connfp = &ipst->ips_ipcl_proto_fanout_v6[connp->conn_proto];
11381676Sjpk 	mutex_enter(&connfp->connf_lock);
11391676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
11401676Sjpk 	    tconn = tconn->conn_next) {
11411676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
114211042SErik.Nordmark@Sun.COM 		if (connp->conn_family != tconn->conn_family)
11431676Sjpk 			continue;
11441676Sjpk 		/* If neither is exempt, then there's no conflict */
114510934Ssommerfeld@sun.com 		if ((connp->conn_mac_mode == CONN_MAC_DEFAULT) &&
114610934Ssommerfeld@sun.com 		    (tconn->conn_mac_mode == CONN_MAC_DEFAULT))
11471676Sjpk 			continue;
11489710SKen.Powell@Sun.COM 		/* We are only concerned about sockets for a different zone */
11499710SKen.Powell@Sun.COM 		if (connp->conn_zoneid == tconn->conn_zoneid)
11509710SKen.Powell@Sun.COM 			continue;
11511676Sjpk 		/* If both are bound to different addrs, ok */
115211042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6) &&
115311042SErik.Nordmark@Sun.COM 		    !IN6_IS_ADDR_UNSPECIFIED(&tconn->conn_laddr_v6) &&
115411042SErik.Nordmark@Sun.COM 		    !IN6_ARE_ADDR_EQUAL(&connp->conn_laddr_v6,
115511042SErik.Nordmark@Sun.COM 		    &tconn->conn_laddr_v6))
11561676Sjpk 			continue;
11571676Sjpk 		/* These two conflict; fail */
11581676Sjpk 		break;
11591676Sjpk 	}
11601676Sjpk 	mutex_exit(&connfp->connf_lock);
11611676Sjpk 	return (tconn != NULL);
11621676Sjpk }
11631676Sjpk 
11641676Sjpk /*
11650Sstevel@tonic-gate  * (v4, v6) bind hash insertion routines
116611042SErik.Nordmark@Sun.COM  * The caller has already setup the conn (conn_proto, conn_laddr_v6, conn_lport)
11670Sstevel@tonic-gate  */
116811042SErik.Nordmark@Sun.COM 
11690Sstevel@tonic-gate int
117011042SErik.Nordmark@Sun.COM ipcl_bind_insert(conn_t *connp)
117111042SErik.Nordmark@Sun.COM {
117211042SErik.Nordmark@Sun.COM 	if (connp->conn_ipversion == IPV6_VERSION)
117311042SErik.Nordmark@Sun.COM 		return (ipcl_bind_insert_v6(connp));
117411042SErik.Nordmark@Sun.COM 	else
117511042SErik.Nordmark@Sun.COM 		return (ipcl_bind_insert_v4(connp));
117611042SErik.Nordmark@Sun.COM }
117711042SErik.Nordmark@Sun.COM 
117811042SErik.Nordmark@Sun.COM int
117911042SErik.Nordmark@Sun.COM ipcl_bind_insert_v4(conn_t *connp)
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate 	connf_t	*connfp;
11820Sstevel@tonic-gate 	int	ret = 0;
11833448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
118411042SErik.Nordmark@Sun.COM 	uint16_t	lport = connp->conn_lport;
118511042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
11860Sstevel@tonic-gate 
118710616SSebastien.Roy@Sun.COM 	if (IPCL_IS_IPTUN(connp))
118811042SErik.Nordmark@Sun.COM 		return (ipcl_iptun_hash_insert(connp, ipst));
118910616SSebastien.Roy@Sun.COM 
11900Sstevel@tonic-gate 	switch (protocol) {
11911676Sjpk 	default:
11923448Sdh155122 		if (is_system_labeled() &&
11933448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
11941676Sjpk 			return (EADDRINUSE);
11951676Sjpk 		/* FALLTHROUGH */
11960Sstevel@tonic-gate 	case IPPROTO_UDP:
11970Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
11983448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
11993448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
12000Sstevel@tonic-gate 		} else {
120111042SErik.Nordmark@Sun.COM 			connfp = &ipst->ips_ipcl_proto_fanout_v4[protocol];
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate 
120411042SErik.Nordmark@Sun.COM 		if (connp->conn_faddr_v4 != INADDR_ANY) {
12050Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
120611042SErik.Nordmark@Sun.COM 		} else if (connp->conn_laddr_v4 != INADDR_ANY) {
12070Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12080Sstevel@tonic-gate 		} else {
12090Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12100Sstevel@tonic-gate 		}
121111042SErik.Nordmark@Sun.COM 		if (protocol == IPPROTO_RSVP)
121211042SErik.Nordmark@Sun.COM 			ill_set_inputfn_all(ipst);
12130Sstevel@tonic-gate 		break;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	case IPPROTO_TCP:
12160Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
12171676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
12183448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
12193448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
122011042SErik.Nordmark@Sun.COM 		if (connp->conn_laddr_v4 != INADDR_ANY) {
12210Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12220Sstevel@tonic-gate 		} else {
12230Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12240Sstevel@tonic-gate 		}
12250Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
122611042SErik.Nordmark@Sun.COM 			ASSERT(connp->conn_ipversion == IPV4_VERSION);
12270Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
12288392SHuafeng.Lv@Sun.COM 			(*cl_inet_listen)(
12298392SHuafeng.Lv@Sun.COM 			    connp->conn_netstack->netstack_stackid,
12308392SHuafeng.Lv@Sun.COM 			    IPPROTO_TCP, AF_INET,
123111042SErik.Nordmark@Sun.COM 			    (uint8_t *)&connp->conn_bound_addr_v4, lport, NULL);
12320Sstevel@tonic-gate 		}
12330Sstevel@tonic-gate 		break;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	case IPPROTO_SCTP:
12360Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
12370Sstevel@tonic-gate 		break;
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	return (ret);
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate int
124411042SErik.Nordmark@Sun.COM ipcl_bind_insert_v6(conn_t *connp)
12450Sstevel@tonic-gate {
124610616SSebastien.Roy@Sun.COM 	connf_t		*connfp;
124710616SSebastien.Roy@Sun.COM 	int		ret = 0;
12483448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
124911042SErik.Nordmark@Sun.COM 	uint16_t	lport = connp->conn_lport;
125011042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
12510Sstevel@tonic-gate 
125210616SSebastien.Roy@Sun.COM 	if (IPCL_IS_IPTUN(connp)) {
125311042SErik.Nordmark@Sun.COM 		return (ipcl_iptun_hash_insert_v6(connp, ipst));
125410616SSebastien.Roy@Sun.COM 	}
125510616SSebastien.Roy@Sun.COM 
12560Sstevel@tonic-gate 	switch (protocol) {
12571676Sjpk 	default:
12583448Sdh155122 		if (is_system_labeled() &&
12593448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
12601676Sjpk 			return (EADDRINUSE);
12611676Sjpk 		/* FALLTHROUGH */
12620Sstevel@tonic-gate 	case IPPROTO_UDP:
12630Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
12643448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
12653448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
12660Sstevel@tonic-gate 		} else {
12673448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 
127011042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6)) {
12710Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
127211042SErik.Nordmark@Sun.COM 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
12730Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12740Sstevel@tonic-gate 		} else {
12750Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12760Sstevel@tonic-gate 		}
12770Sstevel@tonic-gate 		break;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	case IPPROTO_TCP:
12800Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
12811676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
12823448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
12833448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
128411042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
12850Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12860Sstevel@tonic-gate 		} else {
12870Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
12900Sstevel@tonic-gate 			sa_family_t	addr_family;
12910Sstevel@tonic-gate 			uint8_t		*laddrp;
12920Sstevel@tonic-gate 
129311042SErik.Nordmark@Sun.COM 			if (connp->conn_ipversion == IPV6_VERSION) {
12940Sstevel@tonic-gate 				addr_family = AF_INET6;
12950Sstevel@tonic-gate 				laddrp =
129611042SErik.Nordmark@Sun.COM 				    (uint8_t *)&connp->conn_bound_addr_v6;
12970Sstevel@tonic-gate 			} else {
12980Sstevel@tonic-gate 				addr_family = AF_INET;
129911042SErik.Nordmark@Sun.COM 				laddrp = (uint8_t *)&connp->conn_bound_addr_v4;
13000Sstevel@tonic-gate 			}
13010Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
13028392SHuafeng.Lv@Sun.COM 			(*cl_inet_listen)(
13038392SHuafeng.Lv@Sun.COM 			    connp->conn_netstack->netstack_stackid,
13048392SHuafeng.Lv@Sun.COM 			    IPPROTO_TCP, addr_family, laddrp, lport, NULL);
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 		break;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	case IPPROTO_SCTP:
13090Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
13100Sstevel@tonic-gate 		break;
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	return (ret);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate /*
13170Sstevel@tonic-gate  * ipcl_conn_hash insertion routines.
131811042SErik.Nordmark@Sun.COM  * The caller has already set conn_proto and the addresses/ports in the conn_t.
13190Sstevel@tonic-gate  */
132011042SErik.Nordmark@Sun.COM 
13210Sstevel@tonic-gate int
132211042SErik.Nordmark@Sun.COM ipcl_conn_insert(conn_t *connp)
132311042SErik.Nordmark@Sun.COM {
132411042SErik.Nordmark@Sun.COM 	if (connp->conn_ipversion == IPV6_VERSION)
132511042SErik.Nordmark@Sun.COM 		return (ipcl_conn_insert_v6(connp));
132611042SErik.Nordmark@Sun.COM 	else
132711042SErik.Nordmark@Sun.COM 		return (ipcl_conn_insert_v4(connp));
132811042SErik.Nordmark@Sun.COM }
132911042SErik.Nordmark@Sun.COM 
133011042SErik.Nordmark@Sun.COM int
133111042SErik.Nordmark@Sun.COM ipcl_conn_insert_v4(conn_t *connp)
13320Sstevel@tonic-gate {
13330Sstevel@tonic-gate 	connf_t		*connfp;
13340Sstevel@tonic-gate 	conn_t		*tconnp;
13350Sstevel@tonic-gate 	int		ret = 0;
13363448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
133711042SErik.Nordmark@Sun.COM 	uint16_t	lport = connp->conn_lport;
133811042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
13390Sstevel@tonic-gate 
134010616SSebastien.Roy@Sun.COM 	if (IPCL_IS_IPTUN(connp))
134111042SErik.Nordmark@Sun.COM 		return (ipcl_iptun_hash_insert(connp, ipst));
134210616SSebastien.Roy@Sun.COM 
13430Sstevel@tonic-gate 	switch (protocol) {
13440Sstevel@tonic-gate 	case IPPROTO_TCP:
13458432SJonathan.Anderson@Sun.COM 		/*
134611042SErik.Nordmark@Sun.COM 		 * For TCP, we check whether the connection tuple already
13478432SJonathan.Anderson@Sun.COM 		 * exists before allowing the connection to proceed.  We
13488432SJonathan.Anderson@Sun.COM 		 * also allow indexing on the zoneid. This is to allow
13498432SJonathan.Anderson@Sun.COM 		 * multiple shared stack zones to have the same tcp
13508432SJonathan.Anderson@Sun.COM 		 * connection tuple. In practice this only happens for
13518432SJonathan.Anderson@Sun.COM 		 * INADDR_LOOPBACK as it's the only local address which
13528432SJonathan.Anderson@Sun.COM 		 * doesn't have to be unique.
13538432SJonathan.Anderson@Sun.COM 		 */
13543448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
135511042SErik.Nordmark@Sun.COM 		    IPCL_CONN_HASH(connp->conn_faddr_v4,
13563448Sdh155122 		    connp->conn_ports, ipst)];
13570Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
13580Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
13590Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
136011042SErik.Nordmark@Sun.COM 			if (IPCL_CONN_MATCH(tconnp, connp->conn_proto,
136111042SErik.Nordmark@Sun.COM 			    connp->conn_faddr_v4, connp->conn_laddr_v4,
136211042SErik.Nordmark@Sun.COM 			    connp->conn_ports) &&
136311042SErik.Nordmark@Sun.COM 			    IPCL_ZONE_MATCH(tconnp, connp->conn_zoneid)) {
13640Sstevel@tonic-gate 				/* Already have a conn. bail out */
13650Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
13660Sstevel@tonic-gate 				return (EADDRINUSE);
13670Sstevel@tonic-gate 			}
13680Sstevel@tonic-gate 		}
13690Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
13700Sstevel@tonic-gate 			/*
13710Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
13720Sstevel@tonic-gate 			 * rebind. Let it happen.
13730Sstevel@tonic-gate 			 */
13740Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
13750Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
13760Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
13770Sstevel@tonic-gate 		}
13783104Sjprakash 
13793104Sjprakash 		ASSERT(connp->conn_recv != NULL);
138011042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_recvicmp != NULL);
13813104Sjprakash 
13820Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
13830Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
13840Sstevel@tonic-gate 		break;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	case IPPROTO_SCTP:
1387409Skcpoon 		/*
1388409Skcpoon 		 * The raw socket may have already been bound, remove it
1389409Skcpoon 		 * from the hash first.
1390409Skcpoon 		 */
1391409Skcpoon 		IPCL_HASH_REMOVE(connp);
13920Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
13930Sstevel@tonic-gate 		break;
13940Sstevel@tonic-gate 
13951676Sjpk 	default:
13961676Sjpk 		/*
13971676Sjpk 		 * Check for conflicts among MAC exempt bindings.  For
13981676Sjpk 		 * transports with port numbers, this is done by the upper
13991676Sjpk 		 * level per-transport binding logic.  For all others, it's
14001676Sjpk 		 * done here.
14011676Sjpk 		 */
14023448Sdh155122 		if (is_system_labeled() &&
14033448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
14041676Sjpk 			return (EADDRINUSE);
14051676Sjpk 		/* FALLTHROUGH */
14061676Sjpk 
14070Sstevel@tonic-gate 	case IPPROTO_UDP:
14080Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
14093448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
141011042SErik.Nordmark@Sun.COM 			    IPCL_UDP_HASH(lport, ipst)];
14110Sstevel@tonic-gate 		} else {
141211042SErik.Nordmark@Sun.COM 			connfp = &ipst->ips_ipcl_proto_fanout_v4[protocol];
14130Sstevel@tonic-gate 		}
14140Sstevel@tonic-gate 
141511042SErik.Nordmark@Sun.COM 		if (connp->conn_faddr_v4 != INADDR_ANY) {
14160Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
141711042SErik.Nordmark@Sun.COM 		} else if (connp->conn_laddr_v4 != INADDR_ANY) {
14180Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
14190Sstevel@tonic-gate 		} else {
14200Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
14210Sstevel@tonic-gate 		}
14220Sstevel@tonic-gate 		break;
14230Sstevel@tonic-gate 	}
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	return (ret);
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate int
142911042SErik.Nordmark@Sun.COM ipcl_conn_insert_v6(conn_t *connp)
14300Sstevel@tonic-gate {
14310Sstevel@tonic-gate 	connf_t		*connfp;
14320Sstevel@tonic-gate 	conn_t		*tconnp;
14330Sstevel@tonic-gate 	int		ret = 0;
14343448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
143511042SErik.Nordmark@Sun.COM 	uint16_t	lport = connp->conn_lport;
143611042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
143711042SErik.Nordmark@Sun.COM 	uint_t		ifindex = connp->conn_bound_if;
14380Sstevel@tonic-gate 
143910616SSebastien.Roy@Sun.COM 	if (IPCL_IS_IPTUN(connp))
144011042SErik.Nordmark@Sun.COM 		return (ipcl_iptun_hash_insert_v6(connp, ipst));
144110616SSebastien.Roy@Sun.COM 
14420Sstevel@tonic-gate 	switch (protocol) {
14430Sstevel@tonic-gate 	case IPPROTO_TCP:
14448432SJonathan.Anderson@Sun.COM 
14458432SJonathan.Anderson@Sun.COM 		/*
14468432SJonathan.Anderson@Sun.COM 		 * For tcp, we check whether the connection tuple already
14478432SJonathan.Anderson@Sun.COM 		 * exists before allowing the connection to proceed.  We
14488432SJonathan.Anderson@Sun.COM 		 * also allow indexing on the zoneid. This is to allow
14498432SJonathan.Anderson@Sun.COM 		 * multiple shared stack zones to have the same tcp
14508432SJonathan.Anderson@Sun.COM 		 * connection tuple. In practice this only happens for
14518432SJonathan.Anderson@Sun.COM 		 * ipv6_loopback as it's the only local address which
14528432SJonathan.Anderson@Sun.COM 		 * doesn't have to be unique.
14538432SJonathan.Anderson@Sun.COM 		 */
14543448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
145511042SErik.Nordmark@Sun.COM 		    IPCL_CONN_HASH_V6(connp->conn_faddr_v6, connp->conn_ports,
14563448Sdh155122 		    ipst)];
14570Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
14580Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
14590Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
146011042SErik.Nordmark@Sun.COM 			/* NOTE: need to match zoneid. Bug in onnv-gate */
146111042SErik.Nordmark@Sun.COM 			if (IPCL_CONN_MATCH_V6(tconnp, connp->conn_proto,
146211042SErik.Nordmark@Sun.COM 			    connp->conn_faddr_v6, connp->conn_laddr_v6,
14630Sstevel@tonic-gate 			    connp->conn_ports) &&
146411042SErik.Nordmark@Sun.COM 			    (tconnp->conn_bound_if == 0 ||
146511042SErik.Nordmark@Sun.COM 			    tconnp->conn_bound_if == ifindex) &&
146611042SErik.Nordmark@Sun.COM 			    IPCL_ZONE_MATCH(tconnp, connp->conn_zoneid)) {
14670Sstevel@tonic-gate 				/* Already have a conn. bail out */
14680Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
14690Sstevel@tonic-gate 				return (EADDRINUSE);
14700Sstevel@tonic-gate 			}
14710Sstevel@tonic-gate 		}
14720Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
14730Sstevel@tonic-gate 			/*
14740Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
14750Sstevel@tonic-gate 			 * rebind. Let it happen.
14760Sstevel@tonic-gate 			 */
14770Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
14780Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
14790Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
14800Sstevel@tonic-gate 		}
14810Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
14820Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
14830Sstevel@tonic-gate 		break;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	case IPPROTO_SCTP:
1486409Skcpoon 		IPCL_HASH_REMOVE(connp);
14870Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
14880Sstevel@tonic-gate 		break;
14890Sstevel@tonic-gate 
14901676Sjpk 	default:
14913448Sdh155122 		if (is_system_labeled() &&
14923448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
14931676Sjpk 			return (EADDRINUSE);
14941676Sjpk 		/* FALLTHROUGH */
14950Sstevel@tonic-gate 	case IPPROTO_UDP:
14960Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
14973448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
149811042SErik.Nordmark@Sun.COM 			    IPCL_UDP_HASH(lport, ipst)];
14990Sstevel@tonic-gate 		} else {
15003448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
15010Sstevel@tonic-gate 		}
15020Sstevel@tonic-gate 
150311042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6)) {
15040Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
150511042SErik.Nordmark@Sun.COM 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
15060Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
15070Sstevel@tonic-gate 		} else {
15080Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
15090Sstevel@tonic-gate 		}
15100Sstevel@tonic-gate 		break;
15110Sstevel@tonic-gate 	}
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	return (ret);
15140Sstevel@tonic-gate }
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate /*
15170Sstevel@tonic-gate  * v4 packet classifying function. looks up the fanout table to
15180Sstevel@tonic-gate  * find the conn, the packet belongs to. returns the conn with
15190Sstevel@tonic-gate  * the reference held, null otherwise.
15201676Sjpk  *
15211676Sjpk  * If zoneid is ALL_ZONES, then the search rules described in the "Connection
15221676Sjpk  * Lookup" comment block are applied.  Labels are also checked as described
15231676Sjpk  * above.  If the packet is from the inside (looped back), and is from the same
15241676Sjpk  * zone, then label checks are omitted.
15250Sstevel@tonic-gate  */
15260Sstevel@tonic-gate conn_t *
152711042SErik.Nordmark@Sun.COM ipcl_classify_v4(mblk_t *mp, uint8_t protocol, uint_t hdr_len,
152811042SErik.Nordmark@Sun.COM     ip_recv_attr_t *ira, ip_stack_t *ipst)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate 	ipha_t	*ipha;
15310Sstevel@tonic-gate 	connf_t	*connfp, *bind_connfp;
15320Sstevel@tonic-gate 	uint16_t lport;
15330Sstevel@tonic-gate 	uint16_t fport;
15340Sstevel@tonic-gate 	uint32_t ports;
15350Sstevel@tonic-gate 	conn_t	*connp;
15360Sstevel@tonic-gate 	uint16_t  *up;
153711042SErik.Nordmark@Sun.COM 	zoneid_t	zoneid = ira->ira_zoneid;
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
15400Sstevel@tonic-gate 	up = (uint16_t *)((uchar_t *)ipha + hdr_len + TCP_PORTS_OFFSET);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	switch (protocol) {
15430Sstevel@tonic-gate 	case IPPROTO_TCP:
15440Sstevel@tonic-gate 		ports = *(uint32_t *)up;
15450Sstevel@tonic-gate 		connfp =
15463448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_src,
15473448Sdh155122 		    ports, ipst)];
15480Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
15490Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
15500Sstevel@tonic-gate 		    connp = connp->conn_next) {
155111042SErik.Nordmark@Sun.COM 			if (IPCL_CONN_MATCH(connp, protocol,
155211042SErik.Nordmark@Sun.COM 			    ipha->ipha_src, ipha->ipha_dst, ports) &&
155311042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
155411042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
155511042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
155611042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
155711042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
15580Sstevel@tonic-gate 				break;
15590Sstevel@tonic-gate 		}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 		if (connp != NULL) {
15621676Sjpk 			/*
15631676Sjpk 			 * We have a fully-bound TCP connection.
15641676Sjpk 			 *
15651676Sjpk 			 * For labeled systems, there's no need to check the
15661676Sjpk 			 * label here.  It's known to be good as we checked
15671676Sjpk 			 * before allowing the connection to become bound.
15681676Sjpk 			 */
15690Sstevel@tonic-gate 			CONN_INC_REF(connp);
15700Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
15710Sstevel@tonic-gate 			return (connp);
15720Sstevel@tonic-gate 		}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
15750Sstevel@tonic-gate 		lport = up[1];
15763448Sdh155122 		bind_connfp =
15773448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
15780Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
15790Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
15800Sstevel@tonic-gate 		    connp = connp->conn_next) {
15811676Sjpk 			if (IPCL_BIND_MATCH(connp, protocol, ipha->ipha_dst,
158211042SErik.Nordmark@Sun.COM 			    lport) &&
158311042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
158411042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
158511042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
158611042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
158711042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
15880Sstevel@tonic-gate 				break;
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 
15911676Sjpk 		/*
15921676Sjpk 		 * If the matching connection is SLP on a private address, then
15931676Sjpk 		 * the label on the packet must match the local zone's label.
15941676Sjpk 		 * Otherwise, it must be in the label range defined by tnrh.
159511042SErik.Nordmark@Sun.COM 		 * This is ensured by tsol_receive_local.
159611042SErik.Nordmark@Sun.COM 		 *
159711042SErik.Nordmark@Sun.COM 		 * Note that we don't check tsol_receive_local for
159811042SErik.Nordmark@Sun.COM 		 * the connected case.
15991676Sjpk 		 */
160011042SErik.Nordmark@Sun.COM 		if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
16011676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
160211042SErik.Nordmark@Sun.COM 		    ira, connp)) {
160311042SErik.Nordmark@Sun.COM 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp,
160411042SErik.Nordmark@Sun.COM 			    char *, "connp(1) could not receive mp(2)",
160511042SErik.Nordmark@Sun.COM 			    conn_t *, connp, mblk_t *, mp);
16061676Sjpk 			connp = NULL;
16071676Sjpk 		}
16081676Sjpk 
16090Sstevel@tonic-gate 		if (connp != NULL) {
16101676Sjpk 			/* Have a listener at least */
16110Sstevel@tonic-gate 			CONN_INC_REF(connp);
16120Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
16130Sstevel@tonic-gate 			return (connp);
16140Sstevel@tonic-gate 		}
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
16170Sstevel@tonic-gate 		break;
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	case IPPROTO_UDP:
16200Sstevel@tonic-gate 		lport = up[1];
16210Sstevel@tonic-gate 		fport = up[0];
16223448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
16230Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16240Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16250Sstevel@tonic-gate 		    connp = connp->conn_next) {
16260Sstevel@tonic-gate 			if (IPCL_UDP_MATCH(connp, lport, ipha->ipha_dst,
16270Sstevel@tonic-gate 			    fport, ipha->ipha_src) &&
162811042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
162911042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
163011042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
163111042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE))))
16320Sstevel@tonic-gate 				break;
16330Sstevel@tonic-gate 		}
16340Sstevel@tonic-gate 
163511042SErik.Nordmark@Sun.COM 		if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
16361676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
163711042SErik.Nordmark@Sun.COM 		    ira, connp)) {
16381676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp,
16391676Sjpk 			    char *, "connp(1) could not receive mp(2)",
16401676Sjpk 			    conn_t *, connp, mblk_t *, mp);
16411676Sjpk 			connp = NULL;
16421676Sjpk 		}
16431676Sjpk 
16440Sstevel@tonic-gate 		if (connp != NULL) {
16450Sstevel@tonic-gate 			CONN_INC_REF(connp);
16460Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
16470Sstevel@tonic-gate 			return (connp);
16480Sstevel@tonic-gate 		}
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 		/*
16510Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
16520Sstevel@tonic-gate 		 */
16530Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
165411042SErik.Nordmark@Sun.COM 
16550Sstevel@tonic-gate 		break;
165610616SSebastien.Roy@Sun.COM 
165710616SSebastien.Roy@Sun.COM 	case IPPROTO_ENCAP:
165810616SSebastien.Roy@Sun.COM 	case IPPROTO_IPV6:
165910616SSebastien.Roy@Sun.COM 		return (ipcl_iptun_classify_v4(&ipha->ipha_src,
166010616SSebastien.Roy@Sun.COM 		    &ipha->ipha_dst, ipst));
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	return (NULL);
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate conn_t *
166711042SErik.Nordmark@Sun.COM ipcl_classify_v6(mblk_t *mp, uint8_t protocol, uint_t hdr_len,
166811042SErik.Nordmark@Sun.COM     ip_recv_attr_t *ira, ip_stack_t *ipst)
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	ip6_t		*ip6h;
16710Sstevel@tonic-gate 	connf_t		*connfp, *bind_connfp;
16720Sstevel@tonic-gate 	uint16_t	lport;
16730Sstevel@tonic-gate 	uint16_t	fport;
167411042SErik.Nordmark@Sun.COM 	tcpha_t		*tcpha;
16750Sstevel@tonic-gate 	uint32_t	ports;
16760Sstevel@tonic-gate 	conn_t		*connp;
16770Sstevel@tonic-gate 	uint16_t	*up;
167811042SErik.Nordmark@Sun.COM 	zoneid_t	zoneid = ira->ira_zoneid;
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	switch (protocol) {
16830Sstevel@tonic-gate 	case IPPROTO_TCP:
168411042SErik.Nordmark@Sun.COM 		tcpha = (tcpha_t *)&mp->b_rptr[hdr_len];
168511042SErik.Nordmark@Sun.COM 		up = &tcpha->tha_lport;
16860Sstevel@tonic-gate 		ports = *(uint32_t *)up;
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 		connfp =
16893448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_src,
16903448Sdh155122 		    ports, ipst)];
16910Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16920Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16930Sstevel@tonic-gate 		    connp = connp->conn_next) {
169411042SErik.Nordmark@Sun.COM 			if (IPCL_CONN_MATCH_V6(connp, protocol,
169511042SErik.Nordmark@Sun.COM 			    ip6h->ip6_src, ip6h->ip6_dst, ports) &&
169611042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
169711042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
169811042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
169911042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
170011042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
17010Sstevel@tonic-gate 				break;
17020Sstevel@tonic-gate 		}
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 		if (connp != NULL) {
17051676Sjpk 			/*
17061676Sjpk 			 * We have a fully-bound TCP connection.
17071676Sjpk 			 *
17081676Sjpk 			 * For labeled systems, there's no need to check the
17091676Sjpk 			 * label here.  It's known to be good as we checked
17101676Sjpk 			 * before allowing the connection to become bound.
17111676Sjpk 			 */
17120Sstevel@tonic-gate 			CONN_INC_REF(connp);
17130Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
17140Sstevel@tonic-gate 			return (connp);
17150Sstevel@tonic-gate 		}
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 		lport = up[1];
17203448Sdh155122 		bind_connfp =
17213448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
17220Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
17230Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
17240Sstevel@tonic-gate 		    connp = connp->conn_next) {
17250Sstevel@tonic-gate 			if (IPCL_BIND_MATCH_V6(connp, protocol,
17260Sstevel@tonic-gate 			    ip6h->ip6_dst, lport) &&
172711042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
172811042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
172911042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
173011042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
173111042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
17320Sstevel@tonic-gate 				break;
17330Sstevel@tonic-gate 		}
17340Sstevel@tonic-gate 
173511042SErik.Nordmark@Sun.COM 		if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
17361676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
173711042SErik.Nordmark@Sun.COM 		    ira, connp)) {
17381676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp6,
17391676Sjpk 			    char *, "connp(1) could not receive mp(2)",
17401676Sjpk 			    conn_t *, connp, mblk_t *, mp);
17411676Sjpk 			connp = NULL;
17421676Sjpk 		}
17431676Sjpk 
17440Sstevel@tonic-gate 		if (connp != NULL) {
17450Sstevel@tonic-gate 			/* Have a listner at least */
17460Sstevel@tonic-gate 			CONN_INC_REF(connp);
17470Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
17480Sstevel@tonic-gate 			return (connp);
17490Sstevel@tonic-gate 		}
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
17520Sstevel@tonic-gate 		break;
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	case IPPROTO_UDP:
17550Sstevel@tonic-gate 		up = (uint16_t *)&mp->b_rptr[hdr_len];
17560Sstevel@tonic-gate 		lport = up[1];
17570Sstevel@tonic-gate 		fport = up[0];
17583448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
17590Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
17600Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
17610Sstevel@tonic-gate 		    connp = connp->conn_next) {
17620Sstevel@tonic-gate 			if (IPCL_UDP_MATCH_V6(connp, lport, ip6h->ip6_dst,
17630Sstevel@tonic-gate 			    fport, ip6h->ip6_src) &&
176411042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
176511042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
176611042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
176711042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
176811042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
17690Sstevel@tonic-gate 				break;
17700Sstevel@tonic-gate 		}
17710Sstevel@tonic-gate 
177211042SErik.Nordmark@Sun.COM 		if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
17731676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
177411042SErik.Nordmark@Sun.COM 		    ira, connp)) {
17751676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp6,
17761676Sjpk 			    char *, "connp(1) could not receive mp(2)",
17771676Sjpk 			    conn_t *, connp, mblk_t *, mp);
17781676Sjpk 			connp = NULL;
17791676Sjpk 		}
17801676Sjpk 
17810Sstevel@tonic-gate 		if (connp != NULL) {
17820Sstevel@tonic-gate 			CONN_INC_REF(connp);
17830Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
17840Sstevel@tonic-gate 			return (connp);
17850Sstevel@tonic-gate 		}
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 		/*
17880Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
17890Sstevel@tonic-gate 		 */
17900Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
17910Sstevel@tonic-gate 		break;
179210616SSebastien.Roy@Sun.COM 	case IPPROTO_ENCAP:
179310616SSebastien.Roy@Sun.COM 	case IPPROTO_IPV6:
179410616SSebastien.Roy@Sun.COM 		return (ipcl_iptun_classify_v6(&ip6h->ip6_src,
179510616SSebastien.Roy@Sun.COM 		    &ip6h->ip6_dst, ipst));
17960Sstevel@tonic-gate 	}
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 	return (NULL);
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate /*
18020Sstevel@tonic-gate  * wrapper around ipcl_classify_(v4,v6) routines.
18030Sstevel@tonic-gate  */
18040Sstevel@tonic-gate conn_t *
180511042SErik.Nordmark@Sun.COM ipcl_classify(mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst)
18060Sstevel@tonic-gate {
180711042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_IS_IPV4) {
180811042SErik.Nordmark@Sun.COM 		return (ipcl_classify_v4(mp, ira->ira_protocol,
180911042SErik.Nordmark@Sun.COM 		    ira->ira_ip_hdr_length, ira, ipst));
181011042SErik.Nordmark@Sun.COM 	} else {
181111042SErik.Nordmark@Sun.COM 		return (ipcl_classify_v6(mp, ira->ira_protocol,
181211042SErik.Nordmark@Sun.COM 		    ira->ira_ip_hdr_length, ira, ipst));
18130Sstevel@tonic-gate 	}
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
181611042SErik.Nordmark@Sun.COM /*
181711042SErik.Nordmark@Sun.COM  * Only used to classify SCTP RAW sockets
181811042SErik.Nordmark@Sun.COM  */
18190Sstevel@tonic-gate conn_t *
182011042SErik.Nordmark@Sun.COM ipcl_classify_raw(mblk_t *mp, uint8_t protocol, uint32_t ports,
182111042SErik.Nordmark@Sun.COM     ipha_t *ipha, ip6_t *ip6h, ip_recv_attr_t *ira, ip_stack_t *ipst)
18220Sstevel@tonic-gate {
18231676Sjpk 	connf_t		*connfp;
18240Sstevel@tonic-gate 	conn_t		*connp;
18250Sstevel@tonic-gate 	in_port_t	lport;
182611042SErik.Nordmark@Sun.COM 	int		ipversion;
18271676Sjpk 	const void	*dst;
182811042SErik.Nordmark@Sun.COM 	zoneid_t	zoneid = ira->ira_zoneid;
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 	lport = ((uint16_t *)&ports)[1];
183111042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_IS_IPV4) {
183211042SErik.Nordmark@Sun.COM 		dst = (const void *)&ipha->ipha_dst;
183311042SErik.Nordmark@Sun.COM 		ipversion = IPV4_VERSION;
183411042SErik.Nordmark@Sun.COM 	} else {
183511042SErik.Nordmark@Sun.COM 		dst = (const void *)&ip6h->ip6_dst;
183611042SErik.Nordmark@Sun.COM 		ipversion = IPV6_VERSION;
18371676Sjpk 	}
18381676Sjpk 
18393448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
18400Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
18410Sstevel@tonic-gate 	for (connp = connfp->connf_head; connp != NULL;
18420Sstevel@tonic-gate 	    connp = connp->conn_next) {
18430Sstevel@tonic-gate 		/* We don't allow v4 fallback for v6 raw socket. */
184411042SErik.Nordmark@Sun.COM 		if (ipversion != connp->conn_ipversion)
18450Sstevel@tonic-gate 			continue;
184611042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6) &&
184711042SErik.Nordmark@Sun.COM 		    !IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_faddr_v6)) {
184811042SErik.Nordmark@Sun.COM 			if (ipversion == IPV4_VERSION) {
18491676Sjpk 				if (!IPCL_CONN_MATCH(connp, protocol,
185011042SErik.Nordmark@Sun.COM 				    ipha->ipha_src, ipha->ipha_dst, ports))
18511676Sjpk 					continue;
18520Sstevel@tonic-gate 			} else {
18531676Sjpk 				if (!IPCL_CONN_MATCH_V6(connp, protocol,
185411042SErik.Nordmark@Sun.COM 				    ip6h->ip6_src, ip6h->ip6_dst, ports))
18551676Sjpk 					continue;
18560Sstevel@tonic-gate 			}
18570Sstevel@tonic-gate 		} else {
185811042SErik.Nordmark@Sun.COM 			if (ipversion == IPV4_VERSION) {
18591676Sjpk 				if (!IPCL_BIND_MATCH(connp, protocol,
186011042SErik.Nordmark@Sun.COM 				    ipha->ipha_dst, lport))
18611676Sjpk 					continue;
18620Sstevel@tonic-gate 			} else {
18631676Sjpk 				if (!IPCL_BIND_MATCH_V6(connp, protocol,
186411042SErik.Nordmark@Sun.COM 				    ip6h->ip6_dst, lport))
18651676Sjpk 					continue;
18660Sstevel@tonic-gate 			}
18670Sstevel@tonic-gate 		}
18681676Sjpk 
186911042SErik.Nordmark@Sun.COM 		if (connp->conn_zoneid == zoneid ||
187011042SErik.Nordmark@Sun.COM 		    connp->conn_allzones ||
187111042SErik.Nordmark@Sun.COM 		    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
187211042SErik.Nordmark@Sun.COM 		    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
187311042SErik.Nordmark@Sun.COM 		    (ira->ira_flags & IRAF_TX_SHARED_ADDR)))
18741676Sjpk 			break;
18751676Sjpk 	}
187611042SErik.Nordmark@Sun.COM 
187711042SErik.Nordmark@Sun.COM 	if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
187811042SErik.Nordmark@Sun.COM 	    !tsol_receive_local(mp, dst, ipversion, ira, connp)) {
18791676Sjpk 		DTRACE_PROBE3(tx__ip__log__info__classify__rawip,
18801676Sjpk 		    char *, "connp(1) could not receive mp(2)",
18811676Sjpk 		    conn_t *, connp, mblk_t *, mp);
18821676Sjpk 		connp = NULL;
18830Sstevel@tonic-gate 	}
1884409Skcpoon 
1885409Skcpoon 	if (connp != NULL)
1886409Skcpoon 		goto found;
1887409Skcpoon 	mutex_exit(&connfp->connf_lock);
1888409Skcpoon 
188911042SErik.Nordmark@Sun.COM 	/* Try to look for a wildcard SCTP RAW socket match. */
18903448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(0, ipst)];
1891409Skcpoon 	mutex_enter(&connfp->connf_lock);
1892409Skcpoon 	for (connp = connfp->connf_head; connp != NULL;
1893409Skcpoon 	    connp = connp->conn_next) {
1894409Skcpoon 		/* We don't allow v4 fallback for v6 raw socket. */
189511042SErik.Nordmark@Sun.COM 		if (ipversion != connp->conn_ipversion)
189611042SErik.Nordmark@Sun.COM 			continue;
189711042SErik.Nordmark@Sun.COM 		if (!IPCL_ZONE_MATCH(connp, zoneid))
1898409Skcpoon 			continue;
189911042SErik.Nordmark@Sun.COM 
190011042SErik.Nordmark@Sun.COM 		if (ipversion == IPV4_VERSION) {
190111042SErik.Nordmark@Sun.COM 			if (IPCL_RAW_MATCH(connp, protocol, ipha->ipha_dst))
1902409Skcpoon 				break;
1903409Skcpoon 		} else {
190411042SErik.Nordmark@Sun.COM 			if (IPCL_RAW_MATCH_V6(connp, protocol, ip6h->ip6_dst)) {
1905409Skcpoon 				break;
1906409Skcpoon 			}
1907409Skcpoon 		}
19080Sstevel@tonic-gate 	}
1909409Skcpoon 
1910409Skcpoon 	if (connp != NULL)
1911409Skcpoon 		goto found;
1912409Skcpoon 
19130Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
19140Sstevel@tonic-gate 	return (NULL);
1915409Skcpoon 
1916409Skcpoon found:
1917409Skcpoon 	ASSERT(connp != NULL);
1918409Skcpoon 	CONN_INC_REF(connp);
1919409Skcpoon 	mutex_exit(&connfp->connf_lock);
1920409Skcpoon 	return (connp);
19210Sstevel@tonic-gate }
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate /* ARGSUSED */
19240Sstevel@tonic-gate static int
19255240Snordmark tcp_conn_constructor(void *buf, void *cdrarg, int kmflags)
19260Sstevel@tonic-gate {
19270Sstevel@tonic-gate 	itc_t	*itc = (itc_t *)buf;
19280Sstevel@tonic-gate 	conn_t 	*connp = &itc->itc_conn;
19295240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
19305240Snordmark 
19315240Snordmark 	bzero(connp, sizeof (conn_t));
19325240Snordmark 	bzero(tcp, sizeof (tcp_t));
19335240Snordmark 
19345240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
19355240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
19368348SEric.Yu@Sun.COM 	cv_init(&connp->conn_sq_cv, NULL, CV_DEFAULT, NULL);
193711042SErik.Nordmark@Sun.COM 	tcp->tcp_timercache = tcp_timermp_alloc(kmflags);
193811042SErik.Nordmark@Sun.COM 	if (tcp->tcp_timercache == NULL)
193911042SErik.Nordmark@Sun.COM 		return (ENOMEM);
19400Sstevel@tonic-gate 	connp->conn_tcp = tcp;
19410Sstevel@tonic-gate 	connp->conn_flags = IPCL_TCPCONN;
194211042SErik.Nordmark@Sun.COM 	connp->conn_proto = IPPROTO_TCP;
19430Sstevel@tonic-gate 	tcp->tcp_connp = connp;
194411042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
194511042SErik.Nordmark@Sun.COM 
194611042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
194711042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL) {
194811042SErik.Nordmark@Sun.COM 		tcp_timermp_free(tcp);
194911042SErik.Nordmark@Sun.COM 		return (ENOMEM);
195011042SErik.Nordmark@Sun.COM 	}
195111042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
195211042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
195311042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
19540Sstevel@tonic-gate 	return (0);
19550Sstevel@tonic-gate }
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate /* ARGSUSED */
19580Sstevel@tonic-gate static void
19595240Snordmark tcp_conn_destructor(void *buf, void *cdrarg)
19605240Snordmark {
19615240Snordmark 	itc_t	*itc = (itc_t *)buf;
19625240Snordmark 	conn_t 	*connp = &itc->itc_conn;
19635240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
19645240Snordmark 
19655240Snordmark 	ASSERT(connp->conn_flags & IPCL_TCPCONN);
19665240Snordmark 	ASSERT(tcp->tcp_connp == connp);
19675240Snordmark 	ASSERT(connp->conn_tcp == tcp);
19685240Snordmark 	tcp_timermp_free(tcp);
19695240Snordmark 	mutex_destroy(&connp->conn_lock);
19705240Snordmark 	cv_destroy(&connp->conn_cv);
19718348SEric.Yu@Sun.COM 	cv_destroy(&connp->conn_sq_cv);
197211042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
197311042SErik.Nordmark@Sun.COM 
197411042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
197511042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
197611042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
197711042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
197811042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
197911042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
198011042SErik.Nordmark@Sun.COM 	}
19815240Snordmark }
19825240Snordmark 
19835240Snordmark /* ARGSUSED */
19845240Snordmark static int
19855240Snordmark ip_conn_constructor(void *buf, void *cdrarg, int kmflags)
19865240Snordmark {
19875240Snordmark 	itc_t	*itc = (itc_t *)buf;
19885240Snordmark 	conn_t 	*connp = &itc->itc_conn;
19895240Snordmark 
19905240Snordmark 	bzero(connp, sizeof (conn_t));
19915240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
19925240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
19935240Snordmark 	connp->conn_flags = IPCL_IPCCONN;
199411042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
19955240Snordmark 
199611042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
199711042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL)
199811042SErik.Nordmark@Sun.COM 		return (ENOMEM);
199911042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
200011042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
20015240Snordmark 	return (0);
20025240Snordmark }
20035240Snordmark 
20045240Snordmark /* ARGSUSED */
20055240Snordmark static void
20065240Snordmark ip_conn_destructor(void *buf, void *cdrarg)
20075240Snordmark {
20085240Snordmark 	itc_t	*itc = (itc_t *)buf;
20095240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20105240Snordmark 
20115240Snordmark 	ASSERT(connp->conn_flags & IPCL_IPCCONN);
20125240Snordmark 	ASSERT(connp->conn_priv == NULL);
20135240Snordmark 	mutex_destroy(&connp->conn_lock);
20145240Snordmark 	cv_destroy(&connp->conn_cv);
201511042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
201611042SErik.Nordmark@Sun.COM 
201711042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
201811042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
201911042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
202011042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
202111042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
202211042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
202311042SErik.Nordmark@Sun.COM 	}
20245240Snordmark }
20255240Snordmark 
20265240Snordmark /* ARGSUSED */
20275240Snordmark static int
20285240Snordmark udp_conn_constructor(void *buf, void *cdrarg, int kmflags)
20295240Snordmark {
20305240Snordmark 	itc_t	*itc = (itc_t *)buf;
20315240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20325240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
20335240Snordmark 
20345240Snordmark 	bzero(connp, sizeof (conn_t));
20355240Snordmark 	bzero(udp, sizeof (udp_t));
20365240Snordmark 
20375240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20385240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20395240Snordmark 	connp->conn_udp = udp;
20405240Snordmark 	connp->conn_flags = IPCL_UDPCONN;
204111042SErik.Nordmark@Sun.COM 	connp->conn_proto = IPPROTO_UDP;
20425240Snordmark 	udp->udp_connp = connp;
204311042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
204411042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
204511042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL)
204611042SErik.Nordmark@Sun.COM 		return (ENOMEM);
204711042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
204811042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
204911042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
20505240Snordmark 	return (0);
20515240Snordmark }
20525240Snordmark 
20535240Snordmark /* ARGSUSED */
20545240Snordmark static void
20555240Snordmark udp_conn_destructor(void *buf, void *cdrarg)
20565240Snordmark {
20575240Snordmark 	itc_t	*itc = (itc_t *)buf;
20585240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20595240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
20605240Snordmark 
20615240Snordmark 	ASSERT(connp->conn_flags & IPCL_UDPCONN);
20625240Snordmark 	ASSERT(udp->udp_connp == connp);
20635240Snordmark 	ASSERT(connp->conn_udp == udp);
20645240Snordmark 	mutex_destroy(&connp->conn_lock);
20655240Snordmark 	cv_destroy(&connp->conn_cv);
206611042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
206711042SErik.Nordmark@Sun.COM 
206811042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
206911042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
207011042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
207111042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
207211042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
207311042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
207411042SErik.Nordmark@Sun.COM 	}
20755240Snordmark }
20765240Snordmark 
20775240Snordmark /* ARGSUSED */
20785240Snordmark static int
20795240Snordmark rawip_conn_constructor(void *buf, void *cdrarg, int kmflags)
20800Sstevel@tonic-gate {
20815240Snordmark 	itc_t	*itc = (itc_t *)buf;
20825240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20835240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
20845240Snordmark 
20855240Snordmark 	bzero(connp, sizeof (conn_t));
20865240Snordmark 	bzero(icmp, sizeof (icmp_t));
20875240Snordmark 
20885240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20895240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20905240Snordmark 	connp->conn_icmp = icmp;
20915240Snordmark 	connp->conn_flags = IPCL_RAWIPCONN;
209211042SErik.Nordmark@Sun.COM 	connp->conn_proto = IPPROTO_ICMP;
20935240Snordmark 	icmp->icmp_connp = connp;
209411042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
209511042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
209611042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL)
209711042SErik.Nordmark@Sun.COM 		return (ENOMEM);
209811042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
209911042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
210011042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
21015240Snordmark 	return (0);
21025240Snordmark }
21035240Snordmark 
21045240Snordmark /* ARGSUSED */
21055240Snordmark static void
21065240Snordmark rawip_conn_destructor(void *buf, void *cdrarg)
21075240Snordmark {
21085240Snordmark 	itc_t	*itc = (itc_t *)buf;
21095240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21105240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
21115240Snordmark 
21125240Snordmark 	ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
21135240Snordmark 	ASSERT(icmp->icmp_connp == connp);
21145240Snordmark 	ASSERT(connp->conn_icmp == icmp);
21155240Snordmark 	mutex_destroy(&connp->conn_lock);
21165240Snordmark 	cv_destroy(&connp->conn_cv);
211711042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
211811042SErik.Nordmark@Sun.COM 
211911042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
212011042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
212111042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
212211042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
212311042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
212411042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
212511042SErik.Nordmark@Sun.COM 	}
21265240Snordmark }
21275240Snordmark 
21285240Snordmark /* ARGSUSED */
21295240Snordmark static int
21305240Snordmark rts_conn_constructor(void *buf, void *cdrarg, int kmflags)
21315240Snordmark {
21325240Snordmark 	itc_t	*itc = (itc_t *)buf;
21335240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21345240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21355240Snordmark 
21365240Snordmark 	bzero(connp, sizeof (conn_t));
21375240Snordmark 	bzero(rts, sizeof (rts_t));
21385240Snordmark 
21395240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21405240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21415240Snordmark 	connp->conn_rts = rts;
21425240Snordmark 	connp->conn_flags = IPCL_RTSCONN;
21435240Snordmark 	rts->rts_connp = connp;
214411042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
214511042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
214611042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL)
214711042SErik.Nordmark@Sun.COM 		return (ENOMEM);
214811042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
214911042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
21505240Snordmark 	return (0);
21515240Snordmark }
21525240Snordmark 
21535240Snordmark /* ARGSUSED */
21545240Snordmark static void
21555240Snordmark rts_conn_destructor(void *buf, void *cdrarg)
21565240Snordmark {
21575240Snordmark 	itc_t	*itc = (itc_t *)buf;
21585240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21595240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21605240Snordmark 
21615240Snordmark 	ASSERT(connp->conn_flags & IPCL_RTSCONN);
21625240Snordmark 	ASSERT(rts->rts_connp == connp);
21635240Snordmark 	ASSERT(connp->conn_rts == rts);
21645240Snordmark 	mutex_destroy(&connp->conn_lock);
21655240Snordmark 	cv_destroy(&connp->conn_cv);
216611042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
21678444SRao.Shoaib@Sun.COM 
216811042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
216911042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
217011042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
217111042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
217211042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
217311042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
21748348SEric.Yu@Sun.COM 	}
21758348SEric.Yu@Sun.COM }
21768348SEric.Yu@Sun.COM 
21775240Snordmark /*
21785240Snordmark  * Called as part of ipcl_conn_destroy to assert and clear any pointers
21795240Snordmark  * in the conn_t.
218011042SErik.Nordmark@Sun.COM  *
218111042SErik.Nordmark@Sun.COM  * Below we list all the pointers in the conn_t as a documentation aid.
218211042SErik.Nordmark@Sun.COM  * The ones that we can not ASSERT to be NULL are #ifdef'ed out.
218311042SErik.Nordmark@Sun.COM  * If you add any pointers to the conn_t please add an ASSERT here
218411042SErik.Nordmark@Sun.COM  * and #ifdef it out if it can't be actually asserted to be NULL.
218511042SErik.Nordmark@Sun.COM  * In any case, we bzero most of the conn_t at the end of the function.
21865240Snordmark  */
21875240Snordmark void
21885240Snordmark ipcl_conn_cleanup(conn_t *connp)
21895240Snordmark {
219011042SErik.Nordmark@Sun.COM 	ip_xmit_attr_t	*ixa;
219111042SErik.Nordmark@Sun.COM 
21925240Snordmark 	ASSERT(connp->conn_latch == NULL);
219311042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_latch_in_policy == NULL);
219411042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_latch_in_action == NULL);
21955240Snordmark #ifdef notdef
21965240Snordmark 	ASSERT(connp->conn_rq == NULL);
21975240Snordmark 	ASSERT(connp->conn_wq == NULL);
21985240Snordmark #endif
21995240Snordmark 	ASSERT(connp->conn_cred == NULL);
22005240Snordmark 	ASSERT(connp->conn_g_fanout == NULL);
22015240Snordmark 	ASSERT(connp->conn_g_next == NULL);
22025240Snordmark 	ASSERT(connp->conn_g_prev == NULL);
22035240Snordmark 	ASSERT(connp->conn_policy == NULL);
22045240Snordmark 	ASSERT(connp->conn_fanout == NULL);
22055240Snordmark 	ASSERT(connp->conn_next == NULL);
22065240Snordmark 	ASSERT(connp->conn_prev == NULL);
22075240Snordmark 	ASSERT(connp->conn_oper_pending_ill == NULL);
22085240Snordmark 	ASSERT(connp->conn_ilg == NULL);
22095240Snordmark 	ASSERT(connp->conn_drain_next == NULL);
22105240Snordmark 	ASSERT(connp->conn_drain_prev == NULL);
22115277Snordmark #ifdef notdef
22125277Snordmark 	/* conn_idl is not cleared when removed from idl list */
22135240Snordmark 	ASSERT(connp->conn_idl == NULL);
22145277Snordmark #endif
22155240Snordmark 	ASSERT(connp->conn_ipsec_opt_mp == NULL);
221611042SErik.Nordmark@Sun.COM #ifdef notdef
221711042SErik.Nordmark@Sun.COM 	/* conn_netstack is cleared by the caller; needed by ixa_cleanup */
22185240Snordmark 	ASSERT(connp->conn_netstack == NULL);
221911042SErik.Nordmark@Sun.COM #endif
22205240Snordmark 
22218348SEric.Yu@Sun.COM 	ASSERT(connp->conn_helper_info == NULL);
222211042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_ixa != NULL);
222311042SErik.Nordmark@Sun.COM 	ixa = connp->conn_ixa;
222411042SErik.Nordmark@Sun.COM 	ASSERT(ixa->ixa_refcnt == 1);
222511042SErik.Nordmark@Sun.COM 	/* Need to preserve ixa_protocol */
222611042SErik.Nordmark@Sun.COM 	ixa_cleanup(ixa);
222711042SErik.Nordmark@Sun.COM 	ixa->ixa_flags = 0;
222811042SErik.Nordmark@Sun.COM 
22295240Snordmark 	/* Clear out the conn_t fields that are not preserved */
22305240Snordmark 	bzero(&connp->conn_start_clr,
22315240Snordmark 	    sizeof (conn_t) -
22325240Snordmark 	    ((uchar_t *)&connp->conn_start_clr - (uchar_t *)connp));
22330Sstevel@tonic-gate }
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate /*
22360Sstevel@tonic-gate  * All conns are inserted in a global multi-list for the benefit of
22370Sstevel@tonic-gate  * walkers. The walk is guaranteed to walk all open conns at the time
22380Sstevel@tonic-gate  * of the start of the walk exactly once. This property is needed to
22390Sstevel@tonic-gate  * achieve some cleanups during unplumb of interfaces. This is achieved
22400Sstevel@tonic-gate  * as follows.
22410Sstevel@tonic-gate  *
22420Sstevel@tonic-gate  * ipcl_conn_create and ipcl_conn_destroy are the only functions that
22430Sstevel@tonic-gate  * call the insert and delete functions below at creation and deletion
22440Sstevel@tonic-gate  * time respectively. The conn never moves or changes its position in this
22450Sstevel@tonic-gate  * multi-list during its lifetime. CONN_CONDEMNED ensures that the refcnt
22460Sstevel@tonic-gate  * won't increase due to walkers, once the conn deletion has started. Note
22470Sstevel@tonic-gate  * that we can't remove the conn from the global list and then wait for
22480Sstevel@tonic-gate  * the refcnt to drop to zero, since walkers would then see a truncated
22490Sstevel@tonic-gate  * list. CONN_INCIPIENT ensures that walkers don't start looking at
22500Sstevel@tonic-gate  * conns until ip_open is ready to make them globally visible.
22510Sstevel@tonic-gate  * The global round robin multi-list locks are held only to get the
22520Sstevel@tonic-gate  * next member/insertion/deletion and contention should be negligible
22530Sstevel@tonic-gate  * if the multi-list is much greater than the number of cpus.
22540Sstevel@tonic-gate  */
22550Sstevel@tonic-gate void
22560Sstevel@tonic-gate ipcl_globalhash_insert(conn_t *connp)
22570Sstevel@tonic-gate {
22580Sstevel@tonic-gate 	int	index;
22593448Sdh155122 	struct connf_s	*connfp;
22603448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	/*
22630Sstevel@tonic-gate 	 * No need for atomic here. Approximate even distribution
22640Sstevel@tonic-gate 	 * in the global lists is sufficient.
22650Sstevel@tonic-gate 	 */
22663448Sdh155122 	ipst->ips_conn_g_index++;
22673448Sdh155122 	index = ipst->ips_conn_g_index & (CONN_G_HASH_SIZE - 1);
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
22700Sstevel@tonic-gate 	/*
22710Sstevel@tonic-gate 	 * Mark as INCIPIENT, so that walkers will ignore this
22720Sstevel@tonic-gate 	 * for now, till ip_open is ready to make it visible globally.
22730Sstevel@tonic-gate 	 */
22740Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_INCIPIENT;
22750Sstevel@tonic-gate 
22763448Sdh155122 	connfp = &ipst->ips_ipcl_globalhash_fanout[index];
22770Sstevel@tonic-gate 	/* Insert at the head of the list */
22783448Sdh155122 	mutex_enter(&connfp->connf_lock);
22793448Sdh155122 	connp->conn_g_next = connfp->connf_head;
22800Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
22810Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp;
22823448Sdh155122 	connfp->connf_head = connp;
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	/* The fanout bucket this conn points to */
22853448Sdh155122 	connp->conn_g_fanout = connfp;
22860Sstevel@tonic-gate 
22873448Sdh155122 	mutex_exit(&connfp->connf_lock);
22880Sstevel@tonic-gate }
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate void
22910Sstevel@tonic-gate ipcl_globalhash_remove(conn_t *connp)
22920Sstevel@tonic-gate {
22933448Sdh155122 	struct connf_s	*connfp;
22943448Sdh155122 
22950Sstevel@tonic-gate 	/*
22960Sstevel@tonic-gate 	 * We were never inserted in the global multi list.
22970Sstevel@tonic-gate 	 * IPCL_NONE variety is never inserted in the global multilist
22980Sstevel@tonic-gate 	 * since it is presumed to not need any cleanup and is transient.
22990Sstevel@tonic-gate 	 */
23000Sstevel@tonic-gate 	if (connp->conn_g_fanout == NULL)
23010Sstevel@tonic-gate 		return;
23020Sstevel@tonic-gate 
23033448Sdh155122 	connfp = connp->conn_g_fanout;
23043448Sdh155122 	mutex_enter(&connfp->connf_lock);
23050Sstevel@tonic-gate 	if (connp->conn_g_prev != NULL)
23060Sstevel@tonic-gate 		connp->conn_g_prev->conn_g_next = connp->conn_g_next;
23070Sstevel@tonic-gate 	else
23083448Sdh155122 		connfp->connf_head = connp->conn_g_next;
23090Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
23100Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp->conn_g_prev;
23113448Sdh155122 	mutex_exit(&connfp->connf_lock);
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	/* Better to stumble on a null pointer than to corrupt memory */
23140Sstevel@tonic-gate 	connp->conn_g_next = NULL;
23150Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
23165240Snordmark 	connp->conn_g_fanout = NULL;
23170Sstevel@tonic-gate }
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate /*
23200Sstevel@tonic-gate  * Walk the list of all conn_t's in the system, calling the function provided
232111042SErik.Nordmark@Sun.COM  * With the specified argument for each.
23220Sstevel@tonic-gate  * Applies to both IPv4 and IPv6.
23230Sstevel@tonic-gate  *
232411042SErik.Nordmark@Sun.COM  * CONNs may hold pointers to ills (conn_dhcpinit_ill and
232511042SErik.Nordmark@Sun.COM  * conn_oper_pending_ill). To guard against stale pointers
23260Sstevel@tonic-gate  * ipcl_walk() is called to cleanup the conn_t's, typically when an interface is
23270Sstevel@tonic-gate  * unplumbed or removed. New conn_t's that are created while we are walking
23280Sstevel@tonic-gate  * may be missed by this walk, because they are not necessarily inserted
23290Sstevel@tonic-gate  * at the tail of the list. They are new conn_t's and thus don't have any
23300Sstevel@tonic-gate  * stale pointers. The CONN_CLOSING flag ensures that no new reference
23310Sstevel@tonic-gate  * is created to the struct that is going away.
23320Sstevel@tonic-gate  */
23330Sstevel@tonic-gate void
23343448Sdh155122 ipcl_walk(pfv_t func, void *arg, ip_stack_t *ipst)
23350Sstevel@tonic-gate {
23360Sstevel@tonic-gate 	int	i;
23370Sstevel@tonic-gate 	conn_t	*connp;
23380Sstevel@tonic-gate 	conn_t	*prev_connp;
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
23413448Sdh155122 		mutex_enter(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23420Sstevel@tonic-gate 		prev_connp = NULL;
23433448Sdh155122 		connp = ipst->ips_ipcl_globalhash_fanout[i].connf_head;
23440Sstevel@tonic-gate 		while (connp != NULL) {
23450Sstevel@tonic-gate 			mutex_enter(&connp->conn_lock);
23460Sstevel@tonic-gate 			if (connp->conn_state_flags &
23470Sstevel@tonic-gate 			    (CONN_CONDEMNED | CONN_INCIPIENT)) {
23480Sstevel@tonic-gate 				mutex_exit(&connp->conn_lock);
23490Sstevel@tonic-gate 				connp = connp->conn_g_next;
23500Sstevel@tonic-gate 				continue;
23510Sstevel@tonic-gate 			}
23520Sstevel@tonic-gate 			CONN_INC_REF_LOCKED(connp);
23530Sstevel@tonic-gate 			mutex_exit(&connp->conn_lock);
23543448Sdh155122 			mutex_exit(
23553448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23560Sstevel@tonic-gate 			(*func)(connp, arg);
23570Sstevel@tonic-gate 			if (prev_connp != NULL)
23580Sstevel@tonic-gate 				CONN_DEC_REF(prev_connp);
23593448Sdh155122 			mutex_enter(
23603448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23610Sstevel@tonic-gate 			prev_connp = connp;
23620Sstevel@tonic-gate 			connp = connp->conn_g_next;
23630Sstevel@tonic-gate 		}
23643448Sdh155122 		mutex_exit(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23650Sstevel@tonic-gate 		if (prev_connp != NULL)
23660Sstevel@tonic-gate 			CONN_DEC_REF(prev_connp);
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate /*
23710Sstevel@tonic-gate  * Search for a peer TCP/IPv4 loopback conn by doing a reverse lookup on
23720Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
23730Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
23742323Sethindra  * (peer tcp in ESTABLISHED state).
23750Sstevel@tonic-gate  */
23760Sstevel@tonic-gate conn_t *
237711042SErik.Nordmark@Sun.COM ipcl_conn_tcp_lookup_reversed_ipv4(conn_t *connp, ipha_t *ipha, tcpha_t *tcpha,
23783448Sdh155122     ip_stack_t *ipst)
23790Sstevel@tonic-gate {
23800Sstevel@tonic-gate 	uint32_t ports;
23810Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
23820Sstevel@tonic-gate 	connf_t	*connfp;
23830Sstevel@tonic-gate 	conn_t	*tconnp;
23840Sstevel@tonic-gate 	boolean_t zone_chk;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	/*
23870Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
23880Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
23890Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
23900Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.
23910Sstevel@tonic-gate 	 */
23920Sstevel@tonic-gate 	zone_chk = (ipha->ipha_src == htonl(INADDR_LOOPBACK) ||
23930Sstevel@tonic-gate 	    ipha->ipha_dst == htonl(INADDR_LOOPBACK));
23940Sstevel@tonic-gate 
239511042SErik.Nordmark@Sun.COM 	pports[0] = tcpha->tha_fport;
239611042SErik.Nordmark@Sun.COM 	pports[1] = tcpha->tha_lport;
23970Sstevel@tonic-gate 
23983448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
23993448Sdh155122 	    ports, ipst)];
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24020Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24030Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
24060Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
24072323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24080Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24110Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24120Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24130Sstevel@tonic-gate 			return (tconnp);
24140Sstevel@tonic-gate 		}
24150Sstevel@tonic-gate 	}
24160Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
24170Sstevel@tonic-gate 	return (NULL);
24180Sstevel@tonic-gate }
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate /*
24210Sstevel@tonic-gate  * Search for a peer TCP/IPv6 loopback conn by doing a reverse lookup on
24220Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
24230Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
24242323Sethindra  * (peer tcp in ESTABLISHED state).
24250Sstevel@tonic-gate  */
24260Sstevel@tonic-gate conn_t *
242711042SErik.Nordmark@Sun.COM ipcl_conn_tcp_lookup_reversed_ipv6(conn_t *connp, ip6_t *ip6h, tcpha_t *tcpha,
24283448Sdh155122     ip_stack_t *ipst)
24290Sstevel@tonic-gate {
24300Sstevel@tonic-gate 	uint32_t ports;
24310Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
24320Sstevel@tonic-gate 	connf_t	*connfp;
24330Sstevel@tonic-gate 	conn_t	*tconnp;
24340Sstevel@tonic-gate 	boolean_t zone_chk;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	/*
24370Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
24380Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
24390Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
24400Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.  We
24410Sstevel@tonic-gate 	 * don't do Zone check for link local address(es) because the
24420Sstevel@tonic-gate 	 * current Zone implementation treats each link local address as
24430Sstevel@tonic-gate 	 * being unique per system node, i.e. they belong to global Zone.
24440Sstevel@tonic-gate 	 */
24450Sstevel@tonic-gate 	zone_chk = (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src) ||
24460Sstevel@tonic-gate 	    IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst));
24470Sstevel@tonic-gate 
244811042SErik.Nordmark@Sun.COM 	pports[0] = tcpha->tha_fport;
244911042SErik.Nordmark@Sun.COM 	pports[1] = tcpha->tha_lport;
24500Sstevel@tonic-gate 
24513448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
24523448Sdh155122 	    ports, ipst)];
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24550Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24560Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24570Sstevel@tonic-gate 
245811042SErik.Nordmark@Sun.COM 		/* We skip conn_bound_if check here as this is loopback tcp */
24590Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
24600Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
24612323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24620Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24650Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24660Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24670Sstevel@tonic-gate 			return (tconnp);
24680Sstevel@tonic-gate 		}
24690Sstevel@tonic-gate 	}
24700Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
24710Sstevel@tonic-gate 	return (NULL);
24720Sstevel@tonic-gate }
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate /*
24750Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
24760Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
24770Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
24780Sstevel@tonic-gate  */
24790Sstevel@tonic-gate conn_t *
248011042SErik.Nordmark@Sun.COM ipcl_tcp_lookup_reversed_ipv4(ipha_t *ipha, tcpha_t *tcpha, int min_state,
24813448Sdh155122     ip_stack_t *ipst)
24820Sstevel@tonic-gate {
24830Sstevel@tonic-gate 	uint32_t ports;
24840Sstevel@tonic-gate 	uint16_t *pports;
24850Sstevel@tonic-gate 	connf_t	*connfp;
24860Sstevel@tonic-gate 	conn_t	*tconnp;
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
248911042SErik.Nordmark@Sun.COM 	pports[0] = tcpha->tha_fport;
249011042SErik.Nordmark@Sun.COM 	pports[1] = tcpha->tha_lport;
24910Sstevel@tonic-gate 
24923448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
24934691Skcpoon 	    ports, ipst)];
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24960Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24970Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
25000Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
25010Sstevel@tonic-gate 		    tconnp->conn_tcp->tcp_state >= min_state) {
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25040Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25050Sstevel@tonic-gate 			return (tconnp);
25060Sstevel@tonic-gate 		}
25070Sstevel@tonic-gate 	}
25080Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25090Sstevel@tonic-gate 	return (NULL);
25100Sstevel@tonic-gate }
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate /*
25130Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
25140Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
25150Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
25160Sstevel@tonic-gate  * Match on ifindex in addition to addresses.
25170Sstevel@tonic-gate  */
25180Sstevel@tonic-gate conn_t *
25190Sstevel@tonic-gate ipcl_tcp_lookup_reversed_ipv6(ip6_t *ip6h, tcpha_t *tcpha, int min_state,
25203448Sdh155122     uint_t ifindex, ip_stack_t *ipst)
25210Sstevel@tonic-gate {
25220Sstevel@tonic-gate 	tcp_t	*tcp;
25230Sstevel@tonic-gate 	uint32_t ports;
25240Sstevel@tonic-gate 	uint16_t *pports;
25250Sstevel@tonic-gate 	connf_t	*connfp;
25260Sstevel@tonic-gate 	conn_t	*tconnp;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
25290Sstevel@tonic-gate 	pports[0] = tcpha->tha_fport;
25300Sstevel@tonic-gate 	pports[1] = tcpha->tha_lport;
25310Sstevel@tonic-gate 
25323448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
25334691Skcpoon 	    ports, ipst)];
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
25360Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
25370Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 		tcp = tconnp->conn_tcp;
25400Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
25410Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
25420Sstevel@tonic-gate 		    tcp->tcp_state >= min_state &&
254311042SErik.Nordmark@Sun.COM 		    (tconnp->conn_bound_if == 0 ||
254411042SErik.Nordmark@Sun.COM 		    tconnp->conn_bound_if == ifindex)) {
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25470Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25480Sstevel@tonic-gate 			return (tconnp);
25490Sstevel@tonic-gate 		}
25500Sstevel@tonic-gate 	}
25510Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25520Sstevel@tonic-gate 	return (NULL);
25530Sstevel@tonic-gate }
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate /*
25561676Sjpk  * Finds a TCP/IPv4 listening connection; called by tcp_disconnect to locate
25571676Sjpk  * a listener when changing state.
25580Sstevel@tonic-gate  */
25590Sstevel@tonic-gate conn_t *
25603448Sdh155122 ipcl_lookup_listener_v4(uint16_t lport, ipaddr_t laddr, zoneid_t zoneid,
25613448Sdh155122     ip_stack_t *ipst)
25620Sstevel@tonic-gate {
25630Sstevel@tonic-gate 	connf_t		*bind_connfp;
25640Sstevel@tonic-gate 	conn_t		*connp;
25650Sstevel@tonic-gate 	tcp_t		*tcp;
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 	/*
25680Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
25690Sstevel@tonic-gate 	 * all zeros.
25700Sstevel@tonic-gate 	 */
25710Sstevel@tonic-gate 	if (laddr == 0)
25720Sstevel@tonic-gate 		return (NULL);
25730Sstevel@tonic-gate 
25741676Sjpk 	ASSERT(zoneid != ALL_ZONES);
25751676Sjpk 
25763448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
25770Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
25780Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
25790Sstevel@tonic-gate 	    connp = connp->conn_next) {
25800Sstevel@tonic-gate 		tcp = connp->conn_tcp;
25810Sstevel@tonic-gate 		if (IPCL_BIND_MATCH(connp, IPPROTO_TCP, laddr, lport) &&
25822263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
25830Sstevel@tonic-gate 		    (tcp->tcp_listener == NULL)) {
25840Sstevel@tonic-gate 			CONN_INC_REF(connp);
25850Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
25860Sstevel@tonic-gate 			return (connp);
25870Sstevel@tonic-gate 		}
25880Sstevel@tonic-gate 	}
25890Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
25900Sstevel@tonic-gate 	return (NULL);
25910Sstevel@tonic-gate }
25920Sstevel@tonic-gate 
25931676Sjpk /*
25941676Sjpk  * Finds a TCP/IPv6 listening connection; called by tcp_disconnect to locate
25951676Sjpk  * a listener when changing state.
25961676Sjpk  */
25970Sstevel@tonic-gate conn_t *
25980Sstevel@tonic-gate ipcl_lookup_listener_v6(uint16_t lport, in6_addr_t *laddr, uint_t ifindex,
25993448Sdh155122     zoneid_t zoneid, ip_stack_t *ipst)
26000Sstevel@tonic-gate {
26010Sstevel@tonic-gate 	connf_t		*bind_connfp;
26020Sstevel@tonic-gate 	conn_t		*connp = NULL;
26030Sstevel@tonic-gate 	tcp_t		*tcp;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	/*
26060Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
26070Sstevel@tonic-gate 	 * all zeros.
26080Sstevel@tonic-gate 	 */
26090Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(laddr))
26100Sstevel@tonic-gate 		return (NULL);
26110Sstevel@tonic-gate 
26121676Sjpk 	ASSERT(zoneid != ALL_ZONES);
26130Sstevel@tonic-gate 
26143448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
26150Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
26160Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
26170Sstevel@tonic-gate 	    connp = connp->conn_next) {
26180Sstevel@tonic-gate 		tcp = connp->conn_tcp;
26190Sstevel@tonic-gate 		if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP, *laddr, lport) &&
26202263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
262111042SErik.Nordmark@Sun.COM 		    (connp->conn_bound_if == 0 ||
262211042SErik.Nordmark@Sun.COM 		    connp->conn_bound_if == ifindex) &&
26230Sstevel@tonic-gate 		    tcp->tcp_listener == NULL) {
26240Sstevel@tonic-gate 			CONN_INC_REF(connp);
26250Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
26260Sstevel@tonic-gate 			return (connp);
26270Sstevel@tonic-gate 		}
26280Sstevel@tonic-gate 	}
26290Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
26300Sstevel@tonic-gate 	return (NULL);
26310Sstevel@tonic-gate }
26320Sstevel@tonic-gate 
2633741Smasputra /*
2634741Smasputra  * ipcl_get_next_conn
2635741Smasputra  *	get the next entry in the conn global list
2636741Smasputra  *	and put a reference on the next_conn.
2637741Smasputra  *	decrement the reference on the current conn.
2638741Smasputra  *
2639741Smasputra  * This is an iterator based walker function that also provides for
2640741Smasputra  * some selection by the caller. It walks through the conn_hash bucket
2641741Smasputra  * searching for the next valid connp in the list, and selects connections
2642741Smasputra  * that are neither closed nor condemned. It also REFHOLDS the conn
2643741Smasputra  * thus ensuring that the conn exists when the caller uses the conn.
2644741Smasputra  */
2645741Smasputra conn_t *
2646741Smasputra ipcl_get_next_conn(connf_t *connfp, conn_t *connp, uint32_t conn_flags)
2647741Smasputra {
2648741Smasputra 	conn_t	*next_connp;
2649741Smasputra 
2650741Smasputra 	if (connfp == NULL)
2651741Smasputra 		return (NULL);
2652741Smasputra 
2653741Smasputra 	mutex_enter(&connfp->connf_lock);
2654741Smasputra 
2655741Smasputra 	next_connp = (connp == NULL) ?
2656741Smasputra 	    connfp->connf_head : connp->conn_g_next;
2657741Smasputra 
2658741Smasputra 	while (next_connp != NULL) {
2659741Smasputra 		mutex_enter(&next_connp->conn_lock);
2660741Smasputra 		if (!(next_connp->conn_flags & conn_flags) ||
2661741Smasputra 		    (next_connp->conn_state_flags &
2662741Smasputra 		    (CONN_CONDEMNED | CONN_INCIPIENT))) {
2663741Smasputra 			/*
2664741Smasputra 			 * This conn has been condemned or
2665741Smasputra 			 * is closing, or the flags don't match
2666741Smasputra 			 */
2667741Smasputra 			mutex_exit(&next_connp->conn_lock);
2668741Smasputra 			next_connp = next_connp->conn_g_next;
2669741Smasputra 			continue;
2670741Smasputra 		}
2671741Smasputra 		CONN_INC_REF_LOCKED(next_connp);
2672741Smasputra 		mutex_exit(&next_connp->conn_lock);
2673741Smasputra 		break;
2674741Smasputra 	}
2675741Smasputra 
2676741Smasputra 	mutex_exit(&connfp->connf_lock);
2677741Smasputra 
2678741Smasputra 	if (connp != NULL)
2679741Smasputra 		CONN_DEC_REF(connp);
2680741Smasputra 
2681741Smasputra 	return (next_connp);
2682741Smasputra }
2683741Smasputra 
26840Sstevel@tonic-gate #ifdef CONN_DEBUG
26850Sstevel@tonic-gate /*
26860Sstevel@tonic-gate  * Trace of the last NBUF refhold/refrele
26870Sstevel@tonic-gate  */
26880Sstevel@tonic-gate int
26890Sstevel@tonic-gate conn_trace_ref(conn_t *connp)
26900Sstevel@tonic-gate {
26910Sstevel@tonic-gate 	int	last;
26920Sstevel@tonic-gate 	conn_trace_t	*ctb;
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
26950Sstevel@tonic-gate 	last = connp->conn_trace_last;
26960Sstevel@tonic-gate 	last++;
26970Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
26980Sstevel@tonic-gate 		last = 0;
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
27015023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27020Sstevel@tonic-gate 	connp->conn_trace_last = last;
27030Sstevel@tonic-gate 	return (1);
27040Sstevel@tonic-gate }
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate int
27070Sstevel@tonic-gate conn_untrace_ref(conn_t *connp)
27080Sstevel@tonic-gate {
27090Sstevel@tonic-gate 	int	last;
27100Sstevel@tonic-gate 	conn_trace_t	*ctb;
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
27130Sstevel@tonic-gate 	last = connp->conn_trace_last;
27140Sstevel@tonic-gate 	last++;
27150Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
27160Sstevel@tonic-gate 		last = 0;
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
27195023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27200Sstevel@tonic-gate 	connp->conn_trace_last = last;
27210Sstevel@tonic-gate 	return (1);
27220Sstevel@tonic-gate }
27230Sstevel@tonic-gate #endif
2724