xref: /onnv-gate/usr/src/uts/common/inet/ip/ipclassifier.c (revision 12056:4811a59c20b7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51503Sericheng  * Common Development and Distribution License (the "License").
61503Sericheng  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12056SKacheong.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;
58911042SErik.Nordmark@Sun.COM 		ipcl_globalhash_insert(connp);
5905240Snordmark 		return (connp);
5915240Snordmark 
5925240Snordmark 	case IPCL_TCPCONN:
5935240Snordmark 		conn_cache = tcp_conn_cache;
5940Sstevel@tonic-gate 		break;
5955240Snordmark 
5965240Snordmark 	case IPCL_UDPCONN:
5975240Snordmark 		conn_cache = udp_conn_cache;
5985240Snordmark 		break;
5995240Snordmark 
6005240Snordmark 	case IPCL_RAWIPCONN:
6015240Snordmark 		conn_cache = rawip_conn_cache;
6025240Snordmark 		break;
6035240Snordmark 
6045240Snordmark 	case IPCL_RTSCONN:
6055240Snordmark 		conn_cache = rts_conn_cache;
6065240Snordmark 		break;
6075240Snordmark 
6080Sstevel@tonic-gate 	case IPCL_IPCCONN:
6095240Snordmark 		conn_cache = ip_conn_cache;
6100Sstevel@tonic-gate 		break;
6115240Snordmark 
612741Smasputra 	default:
613741Smasputra 		connp = NULL;
614741Smasputra 		ASSERT(0);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6175240Snordmark 	if ((connp = kmem_cache_alloc(conn_cache, sleep)) == NULL)
6185240Snordmark 		return (NULL);
6195240Snordmark 
6205240Snordmark 	connp->conn_ref = 1;
6215240Snordmark 	netstack_hold(ns);
6225240Snordmark 	connp->conn_netstack = ns;
62311042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_ipst = ns->netstack_ip;
6245240Snordmark 	ipcl_globalhash_insert(connp);
6250Sstevel@tonic-gate 	return (connp);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate void
6290Sstevel@tonic-gate ipcl_conn_destroy(conn_t *connp)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate 	mblk_t	*mp;
6323448Sdh155122 	netstack_t	*ns = connp->conn_netstack;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&connp->conn_lock));
6350Sstevel@tonic-gate 	ASSERT(connp->conn_ref == 0);
6360Sstevel@tonic-gate 
6377502Saruna@cs.umn.edu 	DTRACE_PROBE1(conn__destroy, conn_t *, connp);
6387502Saruna@cs.umn.edu 
6391676Sjpk 	if (connp->conn_cred != NULL) {
6401676Sjpk 		crfree(connp->conn_cred);
6411676Sjpk 		connp->conn_cred = NULL;
64211680SErik.Nordmark@Sun.COM 		/* ixa_cred done in ipcl_conn_cleanup below */
6431676Sjpk 	}
6441676Sjpk 
64511042SErik.Nordmark@Sun.COM 	if (connp->conn_ht_iphc != NULL) {
64611042SErik.Nordmark@Sun.COM 		kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
64711042SErik.Nordmark@Sun.COM 		connp->conn_ht_iphc = NULL;
64811042SErik.Nordmark@Sun.COM 		connp->conn_ht_iphc_allocated = 0;
64911042SErik.Nordmark@Sun.COM 		connp->conn_ht_iphc_len = 0;
65011042SErik.Nordmark@Sun.COM 		connp->conn_ht_ulp = NULL;
65111042SErik.Nordmark@Sun.COM 		connp->conn_ht_ulp_len = 0;
65211042SErik.Nordmark@Sun.COM 	}
65311042SErik.Nordmark@Sun.COM 	ip_pkt_free(&connp->conn_xmit_ipp);
65411042SErik.Nordmark@Sun.COM 
6550Sstevel@tonic-gate 	ipcl_globalhash_remove(connp);
6560Sstevel@tonic-gate 
65711042SErik.Nordmark@Sun.COM 	if (connp->conn_latch != NULL) {
65811042SErik.Nordmark@Sun.COM 		IPLATCH_REFRELE(connp->conn_latch);
65911042SErik.Nordmark@Sun.COM 		connp->conn_latch = NULL;
66011042SErik.Nordmark@Sun.COM 	}
66111042SErik.Nordmark@Sun.COM 	if (connp->conn_latch_in_policy != NULL) {
66211042SErik.Nordmark@Sun.COM 		IPPOL_REFRELE(connp->conn_latch_in_policy);
66311042SErik.Nordmark@Sun.COM 		connp->conn_latch_in_policy = NULL;
66411042SErik.Nordmark@Sun.COM 	}
66511042SErik.Nordmark@Sun.COM 	if (connp->conn_latch_in_action != NULL) {
66611042SErik.Nordmark@Sun.COM 		IPACT_REFRELE(connp->conn_latch_in_action);
66711042SErik.Nordmark@Sun.COM 		connp->conn_latch_in_action = NULL;
66811042SErik.Nordmark@Sun.COM 	}
66911042SErik.Nordmark@Sun.COM 	if (connp->conn_policy != NULL) {
67011042SErik.Nordmark@Sun.COM 		IPPH_REFRELE(connp->conn_policy, ns);
67111042SErik.Nordmark@Sun.COM 		connp->conn_policy = NULL;
67211042SErik.Nordmark@Sun.COM 	}
6733448Sdh155122 
67411042SErik.Nordmark@Sun.COM 	if (connp->conn_ipsec_opt_mp != NULL) {
67511042SErik.Nordmark@Sun.COM 		freemsg(connp->conn_ipsec_opt_mp);
67611042SErik.Nordmark@Sun.COM 		connp->conn_ipsec_opt_mp = NULL;
67711042SErik.Nordmark@Sun.COM 	}
67811042SErik.Nordmark@Sun.COM 
67911042SErik.Nordmark@Sun.COM 	if (connp->conn_flags & IPCL_TCPCONN) {
68011042SErik.Nordmark@Sun.COM 		tcp_t *tcp = connp->conn_tcp;
681741Smasputra 
6820Sstevel@tonic-gate 		tcp_free(tcp);
6830Sstevel@tonic-gate 		mp = tcp->tcp_timercache;
68411042SErik.Nordmark@Sun.COM 
68511042SErik.Nordmark@Sun.COM 		tcp->tcp_tcps = NULL;
6860Sstevel@tonic-gate 
6878014SKacheong.Poon@Sun.COM 		/*
6888014SKacheong.Poon@Sun.COM 		 * tcp_rsrv_mp can be NULL if tcp_get_conn() fails to allocate
6898014SKacheong.Poon@Sun.COM 		 * the mblk.
6908014SKacheong.Poon@Sun.COM 		 */
6918014SKacheong.Poon@Sun.COM 		if (tcp->tcp_rsrv_mp != NULL) {
6928014SKacheong.Poon@Sun.COM 			freeb(tcp->tcp_rsrv_mp);
6938014SKacheong.Poon@Sun.COM 			tcp->tcp_rsrv_mp = NULL;
6948014SKacheong.Poon@Sun.COM 			mutex_destroy(&tcp->tcp_rsrv_mp_lock);
6958014SKacheong.Poon@Sun.COM 		}
6968014SKacheong.Poon@Sun.COM 
69711042SErik.Nordmark@Sun.COM 		ipcl_conn_cleanup(connp);
69811042SErik.Nordmark@Sun.COM 		connp->conn_flags = IPCL_TCPCONN;
6993448Sdh155122 		if (ns != NULL) {
7003448Sdh155122 			ASSERT(tcp->tcp_tcps == NULL);
7013448Sdh155122 			connp->conn_netstack = NULL;
70211042SErik.Nordmark@Sun.COM 			connp->conn_ixa->ixa_ipst = NULL;
7033448Sdh155122 			netstack_rele(ns);
7043448Sdh155122 		}
7055240Snordmark 
7065240Snordmark 		bzero(tcp, sizeof (tcp_t));
7075240Snordmark 
7085240Snordmark 		tcp->tcp_timercache = mp;
7095240Snordmark 		tcp->tcp_connp = connp;
7105240Snordmark 		kmem_cache_free(tcp_conn_cache, connp);
7115240Snordmark 		return;
7125240Snordmark 	}
7135240Snordmark 
7145240Snordmark 	if (connp->conn_flags & IPCL_SCTPCONN) {
7153448Sdh155122 		ASSERT(ns != NULL);
7160Sstevel@tonic-gate 		sctp_free(connp);
7175240Snordmark 		return;
7185240Snordmark 	}
7195240Snordmark 
72011042SErik.Nordmark@Sun.COM 	ipcl_conn_cleanup(connp);
7215240Snordmark 	if (ns != NULL) {
7225240Snordmark 		connp->conn_netstack = NULL;
72311042SErik.Nordmark@Sun.COM 		connp->conn_ixa->ixa_ipst = NULL;
7245240Snordmark 		netstack_rele(ns);
7255240Snordmark 	}
7268348SEric.Yu@Sun.COM 
7275240Snordmark 	/* leave conn_priv aka conn_udp, conn_icmp, etc in place. */
7285240Snordmark 	if (connp->conn_flags & IPCL_UDPCONN) {
7295240Snordmark 		connp->conn_flags = IPCL_UDPCONN;
7305240Snordmark 		kmem_cache_free(udp_conn_cache, connp);
7315240Snordmark 	} else if (connp->conn_flags & IPCL_RAWIPCONN) {
7325240Snordmark 		connp->conn_flags = IPCL_RAWIPCONN;
73311042SErik.Nordmark@Sun.COM 		connp->conn_proto = IPPROTO_ICMP;
73411042SErik.Nordmark@Sun.COM 		connp->conn_ixa->ixa_protocol = connp->conn_proto;
7355240Snordmark 		kmem_cache_free(rawip_conn_cache, connp);
7365240Snordmark 	} else if (connp->conn_flags & IPCL_RTSCONN) {
7375240Snordmark 		connp->conn_flags = IPCL_RTSCONN;
7385240Snordmark 		kmem_cache_free(rts_conn_cache, connp);
7390Sstevel@tonic-gate 	} else {
7405240Snordmark 		connp->conn_flags = IPCL_IPCCONN;
7415240Snordmark 		ASSERT(connp->conn_flags & IPCL_IPCCONN);
7425240Snordmark 		ASSERT(connp->conn_priv == NULL);
7435240Snordmark 		kmem_cache_free(ip_conn_cache, connp);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate  * Running in cluster mode - deregister listener information
7490Sstevel@tonic-gate  */
7500Sstevel@tonic-gate static void
7510Sstevel@tonic-gate ipcl_conn_unlisten(conn_t *connp)
7520Sstevel@tonic-gate {
7530Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) != 0);
7540Sstevel@tonic-gate 	ASSERT(connp->conn_lport != 0);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	if (cl_inet_unlisten != NULL) {
7570Sstevel@tonic-gate 		sa_family_t	addr_family;
7580Sstevel@tonic-gate 		uint8_t		*laddrp;
7590Sstevel@tonic-gate 
76011042SErik.Nordmark@Sun.COM 		if (connp->conn_ipversion == IPV6_VERSION) {
7610Sstevel@tonic-gate 			addr_family = AF_INET6;
76211042SErik.Nordmark@Sun.COM 			laddrp = (uint8_t *)&connp->conn_bound_addr_v6;
7630Sstevel@tonic-gate 		} else {
7640Sstevel@tonic-gate 			addr_family = AF_INET;
76511042SErik.Nordmark@Sun.COM 			laddrp = (uint8_t *)&connp->conn_bound_addr_v4;
7660Sstevel@tonic-gate 		}
7678392SHuafeng.Lv@Sun.COM 		(*cl_inet_unlisten)(connp->conn_netstack->netstack_stackid,
7688392SHuafeng.Lv@Sun.COM 		    IPPROTO_TCP, addr_family, laddrp, connp->conn_lport, NULL);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	connp->conn_flags &= ~IPCL_CL_LISTENER;
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * We set the IPCL_REMOVED flag (instead of clearing the flag indicating
7750Sstevel@tonic-gate  * which table the conn belonged to). So for debugging we can see which hash
7760Sstevel@tonic-gate  * table this connection was in.
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate #define	IPCL_HASH_REMOVE(connp)	{					\
7790Sstevel@tonic-gate 	connf_t	*connfp = (connp)->conn_fanout;				\
7800Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&((connp)->conn_lock)));			\
7810Sstevel@tonic-gate 	if (connfp != NULL) {						\
7820Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);			\
7830Sstevel@tonic-gate 		if ((connp)->conn_next != NULL)				\
7840Sstevel@tonic-gate 			(connp)->conn_next->conn_prev =			\
7850Sstevel@tonic-gate 			    (connp)->conn_prev;				\
7860Sstevel@tonic-gate 		if ((connp)->conn_prev != NULL)				\
7870Sstevel@tonic-gate 			(connp)->conn_prev->conn_next =			\
7880Sstevel@tonic-gate 			    (connp)->conn_next;				\
7890Sstevel@tonic-gate 		else							\
7900Sstevel@tonic-gate 			connfp->connf_head = (connp)->conn_next;	\
7910Sstevel@tonic-gate 		(connp)->conn_fanout = NULL;				\
7920Sstevel@tonic-gate 		(connp)->conn_next = NULL;				\
7930Sstevel@tonic-gate 		(connp)->conn_prev = NULL;				\
7940Sstevel@tonic-gate 		(connp)->conn_flags |= IPCL_REMOVED;			\
7950Sstevel@tonic-gate 		if (((connp)->conn_flags & IPCL_CL_LISTENER) != 0)	\
7960Sstevel@tonic-gate 			ipcl_conn_unlisten((connp));			\
7970Sstevel@tonic-gate 		CONN_DEC_REF((connp));					\
7980Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);			\
7990Sstevel@tonic-gate 	}								\
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate void
8030Sstevel@tonic-gate ipcl_hash_remove(conn_t *connp)
8040Sstevel@tonic-gate {
80511042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
80611042SErik.Nordmark@Sun.COM 
8070Sstevel@tonic-gate 	IPCL_HASH_REMOVE(connp);
80811042SErik.Nordmark@Sun.COM 	if (protocol == IPPROTO_RSVP)
80911042SErik.Nordmark@Sun.COM 		ill_set_inputfn_all(connp->conn_netstack->netstack_ip);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate /*
8130Sstevel@tonic-gate  * The whole purpose of this function is allow removal of
8140Sstevel@tonic-gate  * a conn_t from the connected hash for timewait reclaim.
8150Sstevel@tonic-gate  * This is essentially a TW reclaim fastpath where timewait
8160Sstevel@tonic-gate  * collector checks under fanout lock (so no one else can
8170Sstevel@tonic-gate  * get access to the conn_t) that refcnt is 2 i.e. one for
8180Sstevel@tonic-gate  * TCP and one for the classifier hash list. If ref count
8190Sstevel@tonic-gate  * is indeed 2, we can just remove the conn under lock and
8200Sstevel@tonic-gate  * avoid cleaning up the conn under squeue. This gives us
8210Sstevel@tonic-gate  * improved performance.
8220Sstevel@tonic-gate  */
8230Sstevel@tonic-gate void
8240Sstevel@tonic-gate ipcl_hash_remove_locked(conn_t *connp, connf_t	*connfp)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connfp->connf_lock));
8270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
8280Sstevel@tonic-gate 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) == 0);
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	if ((connp)->conn_next != NULL) {
8314691Skcpoon 		(connp)->conn_next->conn_prev = (connp)->conn_prev;
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 	if ((connp)->conn_prev != NULL) {
8344691Skcpoon 		(connp)->conn_prev->conn_next = (connp)->conn_next;
8350Sstevel@tonic-gate 	} else {
8360Sstevel@tonic-gate 		connfp->connf_head = (connp)->conn_next;
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 	(connp)->conn_fanout = NULL;
8390Sstevel@tonic-gate 	(connp)->conn_next = NULL;
8400Sstevel@tonic-gate 	(connp)->conn_prev = NULL;
8410Sstevel@tonic-gate 	(connp)->conn_flags |= IPCL_REMOVED;
8420Sstevel@tonic-gate 	ASSERT((connp)->conn_ref == 2);
8430Sstevel@tonic-gate 	(connp)->conn_ref--;
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp) {		\
8470Sstevel@tonic-gate 	ASSERT((connp)->conn_fanout == NULL);				\
8480Sstevel@tonic-gate 	ASSERT((connp)->conn_next == NULL);				\
8490Sstevel@tonic-gate 	ASSERT((connp)->conn_prev == NULL);				\
8500Sstevel@tonic-gate 	if ((connfp)->connf_head != NULL) {				\
8510Sstevel@tonic-gate 		(connfp)->connf_head->conn_prev = (connp);		\
8520Sstevel@tonic-gate 		(connp)->conn_next = (connfp)->connf_head;		\
8530Sstevel@tonic-gate 	}								\
8540Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8550Sstevel@tonic-gate 	(connfp)->connf_head = (connp);					\
8560Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8570Sstevel@tonic-gate 	    IPCL_CONNECTED;						\
8580Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate #define	IPCL_HASH_INSERT_CONNECTED(connfp, connp) {			\
8620Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8630Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8640Sstevel@tonic-gate 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);		\
8650Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate #define	IPCL_HASH_INSERT_BOUND(connfp, connp) {				\
8690Sstevel@tonic-gate 	conn_t *pconnp = NULL, *nconnp;					\
8700Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
8710Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
8720Sstevel@tonic-gate 	nconnp = (connfp)->connf_head;					\
873153Sethindra 	while (nconnp != NULL &&					\
87411042SErik.Nordmark@Sun.COM 	    !_IPCL_V4_MATCH_ANY(nconnp->conn_laddr_v6)) {		\
875153Sethindra 		pconnp = nconnp;					\
876153Sethindra 		nconnp = nconnp->conn_next;				\
8770Sstevel@tonic-gate 	}								\
8780Sstevel@tonic-gate 	if (pconnp != NULL) {						\
8790Sstevel@tonic-gate 		pconnp->conn_next = (connp);				\
8800Sstevel@tonic-gate 		(connp)->conn_prev = pconnp;				\
8810Sstevel@tonic-gate 	} else {							\
8820Sstevel@tonic-gate 		(connfp)->connf_head = (connp);				\
8830Sstevel@tonic-gate 	}								\
8840Sstevel@tonic-gate 	if (nconnp != NULL) {						\
8850Sstevel@tonic-gate 		(connp)->conn_next = nconnp;				\
8860Sstevel@tonic-gate 		nconnp->conn_prev = (connp);				\
8870Sstevel@tonic-gate 	}								\
8880Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
8890Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
8900Sstevel@tonic-gate 	    IPCL_BOUND;							\
8910Sstevel@tonic-gate 	CONN_INC_REF(connp);						\
8920Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate #define	IPCL_HASH_INSERT_WILDCARD(connfp, connp) {			\
8960Sstevel@tonic-gate 	conn_t **list, *prev, *next;					\
8970Sstevel@tonic-gate 	boolean_t isv4mapped =						\
89811042SErik.Nordmark@Sun.COM 	    IN6_IS_ADDR_V4MAPPED(&(connp)->conn_laddr_v6);		\
8990Sstevel@tonic-gate 	IPCL_HASH_REMOVE((connp));					\
9000Sstevel@tonic-gate 	mutex_enter(&(connfp)->connf_lock);				\
9010Sstevel@tonic-gate 	list = &(connfp)->connf_head;					\
9020Sstevel@tonic-gate 	prev = NULL;							\
9030Sstevel@tonic-gate 	while ((next = *list) != NULL) {				\
9040Sstevel@tonic-gate 		if (isv4mapped &&					\
90511042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_UNSPECIFIED(&next->conn_laddr_v6) &&	\
9060Sstevel@tonic-gate 		    connp->conn_zoneid == next->conn_zoneid) {		\
9070Sstevel@tonic-gate 			(connp)->conn_next = next;			\
9080Sstevel@tonic-gate 			if (prev != NULL)				\
9090Sstevel@tonic-gate 				prev = next->conn_prev;			\
9100Sstevel@tonic-gate 			next->conn_prev = (connp);			\
9110Sstevel@tonic-gate 			break;						\
9120Sstevel@tonic-gate 		}							\
9130Sstevel@tonic-gate 		list = &next->conn_next;				\
9140Sstevel@tonic-gate 		prev = next;						\
9150Sstevel@tonic-gate 	}								\
9160Sstevel@tonic-gate 	(connp)->conn_prev = prev;					\
9170Sstevel@tonic-gate 	*list = (connp);						\
9180Sstevel@tonic-gate 	(connp)->conn_fanout = (connfp);				\
9190Sstevel@tonic-gate 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
9200Sstevel@tonic-gate 	    IPCL_BOUND;							\
9210Sstevel@tonic-gate 	CONN_INC_REF((connp));						\
9220Sstevel@tonic-gate 	mutex_exit(&(connfp)->connf_lock);				\
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate void
9260Sstevel@tonic-gate ipcl_hash_insert_wildcard(connf_t *connfp, conn_t *connp)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate /*
93210616SSebastien.Roy@Sun.COM  * Because the classifier is used to classify inbound packets, the destination
93310616SSebastien.Roy@Sun.COM  * address is meant to be our local tunnel address (tunnel source), and the
93410616SSebastien.Roy@Sun.COM  * source the remote tunnel address (tunnel destination).
93511042SErik.Nordmark@Sun.COM  *
93611042SErik.Nordmark@Sun.COM  * Note that conn_proto can't be used for fanout since the upper protocol
93711042SErik.Nordmark@Sun.COM  * can be both 41 and 4 when IPv6 and IPv4 are over the same tunnel.
93810616SSebastien.Roy@Sun.COM  */
93910616SSebastien.Roy@Sun.COM conn_t *
94010616SSebastien.Roy@Sun.COM ipcl_iptun_classify_v4(ipaddr_t *src, ipaddr_t *dst, ip_stack_t *ipst)
94110616SSebastien.Roy@Sun.COM {
94210616SSebastien.Roy@Sun.COM 	connf_t	*connfp;
94310616SSebastien.Roy@Sun.COM 	conn_t	*connp;
94410616SSebastien.Roy@Sun.COM 
94510616SSebastien.Roy@Sun.COM 	/* first look for IPv4 tunnel links */
94610616SSebastien.Roy@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH(*dst, *src)];
94710616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
94810616SSebastien.Roy@Sun.COM 	for (connp = connfp->connf_head; connp != NULL;
94910616SSebastien.Roy@Sun.COM 	    connp = connp->conn_next) {
95010616SSebastien.Roy@Sun.COM 		if (IPCL_IPTUN_MATCH(connp, *dst, *src))
95110616SSebastien.Roy@Sun.COM 			break;
95210616SSebastien.Roy@Sun.COM 	}
95310616SSebastien.Roy@Sun.COM 	if (connp != NULL)
95410616SSebastien.Roy@Sun.COM 		goto done;
95510616SSebastien.Roy@Sun.COM 
95610616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
95710616SSebastien.Roy@Sun.COM 
95810616SSebastien.Roy@Sun.COM 	/* We didn't find an IPv4 tunnel, try a 6to4 tunnel */
95910616SSebastien.Roy@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH(*dst,
96010616SSebastien.Roy@Sun.COM 	    INADDR_ANY)];
96110616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
96210616SSebastien.Roy@Sun.COM 	for (connp = connfp->connf_head; connp != NULL;
96310616SSebastien.Roy@Sun.COM 	    connp = connp->conn_next) {
96410616SSebastien.Roy@Sun.COM 		if (IPCL_IPTUN_MATCH(connp, *dst, INADDR_ANY))
96510616SSebastien.Roy@Sun.COM 			break;
96610616SSebastien.Roy@Sun.COM 	}
96710616SSebastien.Roy@Sun.COM done:
96810616SSebastien.Roy@Sun.COM 	if (connp != NULL)
96910616SSebastien.Roy@Sun.COM 		CONN_INC_REF(connp);
97010616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
97110616SSebastien.Roy@Sun.COM 	return (connp);
97210616SSebastien.Roy@Sun.COM }
97310616SSebastien.Roy@Sun.COM 
97410616SSebastien.Roy@Sun.COM conn_t *
97510616SSebastien.Roy@Sun.COM ipcl_iptun_classify_v6(in6_addr_t *src, in6_addr_t *dst, ip_stack_t *ipst)
97610616SSebastien.Roy@Sun.COM {
97710616SSebastien.Roy@Sun.COM 	connf_t	*connfp;
97810616SSebastien.Roy@Sun.COM 	conn_t	*connp;
97910616SSebastien.Roy@Sun.COM 
98010616SSebastien.Roy@Sun.COM 	/* Look for an IPv6 tunnel link */
98110616SSebastien.Roy@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH_V6(dst, src)];
98210616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
98310616SSebastien.Roy@Sun.COM 	for (connp = connfp->connf_head; connp != NULL;
98410616SSebastien.Roy@Sun.COM 	    connp = connp->conn_next) {
98510616SSebastien.Roy@Sun.COM 		if (IPCL_IPTUN_MATCH_V6(connp, dst, src)) {
98610616SSebastien.Roy@Sun.COM 			CONN_INC_REF(connp);
98710616SSebastien.Roy@Sun.COM 			break;
98810616SSebastien.Roy@Sun.COM 		}
98910616SSebastien.Roy@Sun.COM 	}
99010616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
99110616SSebastien.Roy@Sun.COM 	return (connp);
99210616SSebastien.Roy@Sun.COM }
99310616SSebastien.Roy@Sun.COM 
99410616SSebastien.Roy@Sun.COM /*
9950Sstevel@tonic-gate  * This function is used only for inserting SCTP raw socket now.
9960Sstevel@tonic-gate  * This may change later.
9970Sstevel@tonic-gate  *
9980Sstevel@tonic-gate  * Note that only one raw socket can be bound to a port.  The param
9990Sstevel@tonic-gate  * lport is in network byte order.
10000Sstevel@tonic-gate  */
10010Sstevel@tonic-gate static int
10020Sstevel@tonic-gate ipcl_sctp_hash_insert(conn_t *connp, in_port_t lport)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate 	connf_t	*connfp;
10050Sstevel@tonic-gate 	conn_t	*oconnp;
10063448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
10070Sstevel@tonic-gate 
10083448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/* Check for existing raw socket already bound to the port. */
10110Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
10120Sstevel@tonic-gate 	for (oconnp = connfp->connf_head; oconnp != NULL;
1013409Skcpoon 	    oconnp = oconnp->conn_next) {
10140Sstevel@tonic-gate 		if (oconnp->conn_lport == lport &&
10150Sstevel@tonic-gate 		    oconnp->conn_zoneid == connp->conn_zoneid &&
101611042SErik.Nordmark@Sun.COM 		    oconnp->conn_family == connp->conn_family &&
101711042SErik.Nordmark@Sun.COM 		    ((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6) ||
101811042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_UNSPECIFIED(&oconnp->conn_laddr_v6) ||
101911042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_laddr_v6) ||
102011042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_V4MAPPED_ANY(&oconnp->conn_laddr_v6)) ||
102111042SErik.Nordmark@Sun.COM 		    IN6_ARE_ADDR_EQUAL(&oconnp->conn_laddr_v6,
102211042SErik.Nordmark@Sun.COM 		    &connp->conn_laddr_v6))) {
10230Sstevel@tonic-gate 			break;
10240Sstevel@tonic-gate 		}
10250Sstevel@tonic-gate 	}
10260Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
10270Sstevel@tonic-gate 	if (oconnp != NULL)
10280Sstevel@tonic-gate 		return (EADDRNOTAVAIL);
10290Sstevel@tonic-gate 
103011042SErik.Nordmark@Sun.COM 	if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6) ||
103111042SErik.Nordmark@Sun.COM 	    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_faddr_v6)) {
103211042SErik.Nordmark@Sun.COM 		if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6) ||
103311042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_laddr_v6)) {
10340Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
10350Sstevel@tonic-gate 		} else {
10360Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 	} else {
10390Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED(connfp, connp);
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 	return (0);
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
104410616SSebastien.Roy@Sun.COM static int
104511042SErik.Nordmark@Sun.COM ipcl_iptun_hash_insert(conn_t *connp, ip_stack_t *ipst)
104610616SSebastien.Roy@Sun.COM {
104710616SSebastien.Roy@Sun.COM 	connf_t	*connfp;
104810616SSebastien.Roy@Sun.COM 	conn_t	*tconnp;
104911042SErik.Nordmark@Sun.COM 	ipaddr_t laddr = connp->conn_laddr_v4;
105011042SErik.Nordmark@Sun.COM 	ipaddr_t faddr = connp->conn_faddr_v4;
105110616SSebastien.Roy@Sun.COM 
105211042SErik.Nordmark@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH(laddr, faddr)];
105310616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
105410616SSebastien.Roy@Sun.COM 	for (tconnp = connfp->connf_head; tconnp != NULL;
105510616SSebastien.Roy@Sun.COM 	    tconnp = tconnp->conn_next) {
105611042SErik.Nordmark@Sun.COM 		if (IPCL_IPTUN_MATCH(tconnp, laddr, faddr)) {
105710616SSebastien.Roy@Sun.COM 			/* A tunnel is already bound to these addresses. */
105810616SSebastien.Roy@Sun.COM 			mutex_exit(&connfp->connf_lock);
105910616SSebastien.Roy@Sun.COM 			return (EADDRINUSE);
106010616SSebastien.Roy@Sun.COM 		}
106110616SSebastien.Roy@Sun.COM 	}
106210616SSebastien.Roy@Sun.COM 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
106310616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
106410616SSebastien.Roy@Sun.COM 	return (0);
106510616SSebastien.Roy@Sun.COM }
106610616SSebastien.Roy@Sun.COM 
106710616SSebastien.Roy@Sun.COM static int
106811042SErik.Nordmark@Sun.COM ipcl_iptun_hash_insert_v6(conn_t *connp, ip_stack_t *ipst)
106910616SSebastien.Roy@Sun.COM {
107010616SSebastien.Roy@Sun.COM 	connf_t	*connfp;
107110616SSebastien.Roy@Sun.COM 	conn_t	*tconnp;
107211042SErik.Nordmark@Sun.COM 	in6_addr_t *laddr = &connp->conn_laddr_v6;
107311042SErik.Nordmark@Sun.COM 	in6_addr_t *faddr = &connp->conn_faddr_v6;
107410616SSebastien.Roy@Sun.COM 
107511042SErik.Nordmark@Sun.COM 	connfp = &ipst->ips_ipcl_iptun_fanout[IPCL_IPTUN_HASH_V6(laddr, faddr)];
107610616SSebastien.Roy@Sun.COM 	mutex_enter(&connfp->connf_lock);
107710616SSebastien.Roy@Sun.COM 	for (tconnp = connfp->connf_head; tconnp != NULL;
107810616SSebastien.Roy@Sun.COM 	    tconnp = tconnp->conn_next) {
107911042SErik.Nordmark@Sun.COM 		if (IPCL_IPTUN_MATCH_V6(tconnp, laddr, faddr)) {
108010616SSebastien.Roy@Sun.COM 			/* A tunnel is already bound to these addresses. */
108110616SSebastien.Roy@Sun.COM 			mutex_exit(&connfp->connf_lock);
108210616SSebastien.Roy@Sun.COM 			return (EADDRINUSE);
108310616SSebastien.Roy@Sun.COM 		}
108410616SSebastien.Roy@Sun.COM 	}
108510616SSebastien.Roy@Sun.COM 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
108610616SSebastien.Roy@Sun.COM 	mutex_exit(&connfp->connf_lock);
108710616SSebastien.Roy@Sun.COM 	return (0);
108810616SSebastien.Roy@Sun.COM }
108910616SSebastien.Roy@Sun.COM 
10900Sstevel@tonic-gate /*
10911676Sjpk  * Check for a MAC exemption conflict on a labeled system.  Note that for
10921676Sjpk  * protocols that use port numbers (UDP, TCP, SCTP), we do this check up in the
10931676Sjpk  * transport layer.  This check is for binding all other protocols.
10941676Sjpk  *
10951676Sjpk  * Returns true if there's a conflict.
10961676Sjpk  */
10971676Sjpk static boolean_t
10983448Sdh155122 check_exempt_conflict_v4(conn_t *connp, ip_stack_t *ipst)
10991676Sjpk {
11001676Sjpk 	connf_t	*connfp;
11011676Sjpk 	conn_t *tconn;
11021676Sjpk 
110311042SErik.Nordmark@Sun.COM 	connfp = &ipst->ips_ipcl_proto_fanout_v4[connp->conn_proto];
11041676Sjpk 	mutex_enter(&connfp->connf_lock);
11051676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
11061676Sjpk 	    tconn = tconn->conn_next) {
11071676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
110811042SErik.Nordmark@Sun.COM 		if (connp->conn_family != tconn->conn_family)
11091676Sjpk 			continue;
11101676Sjpk 		/* If neither is exempt, then there's no conflict */
111110934Ssommerfeld@sun.com 		if ((connp->conn_mac_mode == CONN_MAC_DEFAULT) &&
111210934Ssommerfeld@sun.com 		    (tconn->conn_mac_mode == CONN_MAC_DEFAULT))
11131676Sjpk 			continue;
11149710SKen.Powell@Sun.COM 		/* We are only concerned about sockets for a different zone */
11159710SKen.Powell@Sun.COM 		if (connp->conn_zoneid == tconn->conn_zoneid)
11169710SKen.Powell@Sun.COM 			continue;
11171676Sjpk 		/* If both are bound to different specific addrs, ok */
111811042SErik.Nordmark@Sun.COM 		if (connp->conn_laddr_v4 != INADDR_ANY &&
111911042SErik.Nordmark@Sun.COM 		    tconn->conn_laddr_v4 != INADDR_ANY &&
112011042SErik.Nordmark@Sun.COM 		    connp->conn_laddr_v4 != tconn->conn_laddr_v4)
11211676Sjpk 			continue;
11221676Sjpk 		/* These two conflict; fail */
11231676Sjpk 		break;
11241676Sjpk 	}
11251676Sjpk 	mutex_exit(&connfp->connf_lock);
11261676Sjpk 	return (tconn != NULL);
11271676Sjpk }
11281676Sjpk 
11291676Sjpk static boolean_t
11303448Sdh155122 check_exempt_conflict_v6(conn_t *connp, ip_stack_t *ipst)
11311676Sjpk {
11321676Sjpk 	connf_t	*connfp;
11331676Sjpk 	conn_t *tconn;
11341676Sjpk 
113511042SErik.Nordmark@Sun.COM 	connfp = &ipst->ips_ipcl_proto_fanout_v6[connp->conn_proto];
11361676Sjpk 	mutex_enter(&connfp->connf_lock);
11371676Sjpk 	for (tconn = connfp->connf_head; tconn != NULL;
11381676Sjpk 	    tconn = tconn->conn_next) {
11391676Sjpk 		/* We don't allow v4 fallback for v6 raw socket */
114011042SErik.Nordmark@Sun.COM 		if (connp->conn_family != tconn->conn_family)
11411676Sjpk 			continue;
11421676Sjpk 		/* If neither is exempt, then there's no conflict */
114310934Ssommerfeld@sun.com 		if ((connp->conn_mac_mode == CONN_MAC_DEFAULT) &&
114410934Ssommerfeld@sun.com 		    (tconn->conn_mac_mode == CONN_MAC_DEFAULT))
11451676Sjpk 			continue;
11469710SKen.Powell@Sun.COM 		/* We are only concerned about sockets for a different zone */
11479710SKen.Powell@Sun.COM 		if (connp->conn_zoneid == tconn->conn_zoneid)
11489710SKen.Powell@Sun.COM 			continue;
11491676Sjpk 		/* If both are bound to different addrs, ok */
115011042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6) &&
115111042SErik.Nordmark@Sun.COM 		    !IN6_IS_ADDR_UNSPECIFIED(&tconn->conn_laddr_v6) &&
115211042SErik.Nordmark@Sun.COM 		    !IN6_ARE_ADDR_EQUAL(&connp->conn_laddr_v6,
115311042SErik.Nordmark@Sun.COM 		    &tconn->conn_laddr_v6))
11541676Sjpk 			continue;
11551676Sjpk 		/* These two conflict; fail */
11561676Sjpk 		break;
11571676Sjpk 	}
11581676Sjpk 	mutex_exit(&connfp->connf_lock);
11591676Sjpk 	return (tconn != NULL);
11601676Sjpk }
11611676Sjpk 
11621676Sjpk /*
11630Sstevel@tonic-gate  * (v4, v6) bind hash insertion routines
116411042SErik.Nordmark@Sun.COM  * The caller has already setup the conn (conn_proto, conn_laddr_v6, conn_lport)
11650Sstevel@tonic-gate  */
116611042SErik.Nordmark@Sun.COM 
11670Sstevel@tonic-gate int
116811042SErik.Nordmark@Sun.COM ipcl_bind_insert(conn_t *connp)
116911042SErik.Nordmark@Sun.COM {
117011042SErik.Nordmark@Sun.COM 	if (connp->conn_ipversion == IPV6_VERSION)
117111042SErik.Nordmark@Sun.COM 		return (ipcl_bind_insert_v6(connp));
117211042SErik.Nordmark@Sun.COM 	else
117311042SErik.Nordmark@Sun.COM 		return (ipcl_bind_insert_v4(connp));
117411042SErik.Nordmark@Sun.COM }
117511042SErik.Nordmark@Sun.COM 
117611042SErik.Nordmark@Sun.COM int
117711042SErik.Nordmark@Sun.COM ipcl_bind_insert_v4(conn_t *connp)
11780Sstevel@tonic-gate {
11790Sstevel@tonic-gate 	connf_t	*connfp;
11800Sstevel@tonic-gate 	int	ret = 0;
11813448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
118211042SErik.Nordmark@Sun.COM 	uint16_t	lport = connp->conn_lport;
118311042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
11840Sstevel@tonic-gate 
118510616SSebastien.Roy@Sun.COM 	if (IPCL_IS_IPTUN(connp))
118611042SErik.Nordmark@Sun.COM 		return (ipcl_iptun_hash_insert(connp, ipst));
118710616SSebastien.Roy@Sun.COM 
11880Sstevel@tonic-gate 	switch (protocol) {
11891676Sjpk 	default:
11903448Sdh155122 		if (is_system_labeled() &&
11913448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
11921676Sjpk 			return (EADDRINUSE);
11931676Sjpk 		/* FALLTHROUGH */
11940Sstevel@tonic-gate 	case IPPROTO_UDP:
11950Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
11963448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
11973448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
11980Sstevel@tonic-gate 		} else {
119911042SErik.Nordmark@Sun.COM 			connfp = &ipst->ips_ipcl_proto_fanout_v4[protocol];
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate 
120211042SErik.Nordmark@Sun.COM 		if (connp->conn_faddr_v4 != INADDR_ANY) {
12030Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
120411042SErik.Nordmark@Sun.COM 		} else if (connp->conn_laddr_v4 != INADDR_ANY) {
12050Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12060Sstevel@tonic-gate 		} else {
12070Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12080Sstevel@tonic-gate 		}
120911042SErik.Nordmark@Sun.COM 		if (protocol == IPPROTO_RSVP)
121011042SErik.Nordmark@Sun.COM 			ill_set_inputfn_all(ipst);
12110Sstevel@tonic-gate 		break;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	case IPPROTO_TCP:
12140Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
12151676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
12163448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
12173448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
121811042SErik.Nordmark@Sun.COM 		if (connp->conn_laddr_v4 != INADDR_ANY) {
12190Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12200Sstevel@tonic-gate 		} else {
12210Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
122411042SErik.Nordmark@Sun.COM 			ASSERT(connp->conn_ipversion == IPV4_VERSION);
12250Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
12268392SHuafeng.Lv@Sun.COM 			(*cl_inet_listen)(
12278392SHuafeng.Lv@Sun.COM 			    connp->conn_netstack->netstack_stackid,
12288392SHuafeng.Lv@Sun.COM 			    IPPROTO_TCP, AF_INET,
122911042SErik.Nordmark@Sun.COM 			    (uint8_t *)&connp->conn_bound_addr_v4, lport, NULL);
12300Sstevel@tonic-gate 		}
12310Sstevel@tonic-gate 		break;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	case IPPROTO_SCTP:
12340Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
12350Sstevel@tonic-gate 		break;
12360Sstevel@tonic-gate 	}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	return (ret);
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate int
124211042SErik.Nordmark@Sun.COM ipcl_bind_insert_v6(conn_t *connp)
12430Sstevel@tonic-gate {
124410616SSebastien.Roy@Sun.COM 	connf_t		*connfp;
124510616SSebastien.Roy@Sun.COM 	int		ret = 0;
12463448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
124711042SErik.Nordmark@Sun.COM 	uint16_t	lport = connp->conn_lport;
124811042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
12490Sstevel@tonic-gate 
125010616SSebastien.Roy@Sun.COM 	if (IPCL_IS_IPTUN(connp)) {
125111042SErik.Nordmark@Sun.COM 		return (ipcl_iptun_hash_insert_v6(connp, ipst));
125210616SSebastien.Roy@Sun.COM 	}
125310616SSebastien.Roy@Sun.COM 
12540Sstevel@tonic-gate 	switch (protocol) {
12551676Sjpk 	default:
12563448Sdh155122 		if (is_system_labeled() &&
12573448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
12581676Sjpk 			return (EADDRINUSE);
12591676Sjpk 		/* FALLTHROUGH */
12600Sstevel@tonic-gate 	case IPPROTO_UDP:
12610Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
12623448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
12633448Sdh155122 			    IPCL_UDP_HASH(lport, ipst)];
12640Sstevel@tonic-gate 		} else {
12653448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
12660Sstevel@tonic-gate 		}
12670Sstevel@tonic-gate 
126811042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6)) {
12690Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
127011042SErik.Nordmark@Sun.COM 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
12710Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12720Sstevel@tonic-gate 		} else {
12730Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12740Sstevel@tonic-gate 		}
12750Sstevel@tonic-gate 		break;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	case IPPROTO_TCP:
12780Sstevel@tonic-gate 		/* Insert it in the Bind Hash */
12791676Sjpk 		ASSERT(connp->conn_zoneid != ALL_ZONES);
12803448Sdh155122 		connfp = &ipst->ips_ipcl_bind_fanout[
12813448Sdh155122 		    IPCL_BIND_HASH(lport, ipst)];
128211042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
12830Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
12840Sstevel@tonic-gate 		} else {
12850Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 		if (cl_inet_listen != NULL) {
12880Sstevel@tonic-gate 			sa_family_t	addr_family;
12890Sstevel@tonic-gate 			uint8_t		*laddrp;
12900Sstevel@tonic-gate 
129111042SErik.Nordmark@Sun.COM 			if (connp->conn_ipversion == IPV6_VERSION) {
12920Sstevel@tonic-gate 				addr_family = AF_INET6;
12930Sstevel@tonic-gate 				laddrp =
129411042SErik.Nordmark@Sun.COM 				    (uint8_t *)&connp->conn_bound_addr_v6;
12950Sstevel@tonic-gate 			} else {
12960Sstevel@tonic-gate 				addr_family = AF_INET;
129711042SErik.Nordmark@Sun.COM 				laddrp = (uint8_t *)&connp->conn_bound_addr_v4;
12980Sstevel@tonic-gate 			}
12990Sstevel@tonic-gate 			connp->conn_flags |= IPCL_CL_LISTENER;
13008392SHuafeng.Lv@Sun.COM 			(*cl_inet_listen)(
13018392SHuafeng.Lv@Sun.COM 			    connp->conn_netstack->netstack_stackid,
13028392SHuafeng.Lv@Sun.COM 			    IPPROTO_TCP, addr_family, laddrp, lport, NULL);
13030Sstevel@tonic-gate 		}
13040Sstevel@tonic-gate 		break;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	case IPPROTO_SCTP:
13070Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
13080Sstevel@tonic-gate 		break;
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	return (ret);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate /*
13150Sstevel@tonic-gate  * ipcl_conn_hash insertion routines.
131611042SErik.Nordmark@Sun.COM  * The caller has already set conn_proto and the addresses/ports in the conn_t.
13170Sstevel@tonic-gate  */
131811042SErik.Nordmark@Sun.COM 
13190Sstevel@tonic-gate int
132011042SErik.Nordmark@Sun.COM ipcl_conn_insert(conn_t *connp)
132111042SErik.Nordmark@Sun.COM {
132211042SErik.Nordmark@Sun.COM 	if (connp->conn_ipversion == IPV6_VERSION)
132311042SErik.Nordmark@Sun.COM 		return (ipcl_conn_insert_v6(connp));
132411042SErik.Nordmark@Sun.COM 	else
132511042SErik.Nordmark@Sun.COM 		return (ipcl_conn_insert_v4(connp));
132611042SErik.Nordmark@Sun.COM }
132711042SErik.Nordmark@Sun.COM 
132811042SErik.Nordmark@Sun.COM int
132911042SErik.Nordmark@Sun.COM ipcl_conn_insert_v4(conn_t *connp)
13300Sstevel@tonic-gate {
13310Sstevel@tonic-gate 	connf_t		*connfp;
13320Sstevel@tonic-gate 	conn_t		*tconnp;
13330Sstevel@tonic-gate 	int		ret = 0;
13343448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
133511042SErik.Nordmark@Sun.COM 	uint16_t	lport = connp->conn_lport;
133611042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
13370Sstevel@tonic-gate 
133810616SSebastien.Roy@Sun.COM 	if (IPCL_IS_IPTUN(connp))
133911042SErik.Nordmark@Sun.COM 		return (ipcl_iptun_hash_insert(connp, ipst));
134010616SSebastien.Roy@Sun.COM 
13410Sstevel@tonic-gate 	switch (protocol) {
13420Sstevel@tonic-gate 	case IPPROTO_TCP:
13438432SJonathan.Anderson@Sun.COM 		/*
134411042SErik.Nordmark@Sun.COM 		 * For TCP, we check whether the connection tuple already
13458432SJonathan.Anderson@Sun.COM 		 * exists before allowing the connection to proceed.  We
13468432SJonathan.Anderson@Sun.COM 		 * also allow indexing on the zoneid. This is to allow
13478432SJonathan.Anderson@Sun.COM 		 * multiple shared stack zones to have the same tcp
13488432SJonathan.Anderson@Sun.COM 		 * connection tuple. In practice this only happens for
13498432SJonathan.Anderson@Sun.COM 		 * INADDR_LOOPBACK as it's the only local address which
13508432SJonathan.Anderson@Sun.COM 		 * doesn't have to be unique.
13518432SJonathan.Anderson@Sun.COM 		 */
13523448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
135311042SErik.Nordmark@Sun.COM 		    IPCL_CONN_HASH(connp->conn_faddr_v4,
13543448Sdh155122 		    connp->conn_ports, ipst)];
13550Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
13560Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
13570Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
135811042SErik.Nordmark@Sun.COM 			if (IPCL_CONN_MATCH(tconnp, connp->conn_proto,
135911042SErik.Nordmark@Sun.COM 			    connp->conn_faddr_v4, connp->conn_laddr_v4,
136011042SErik.Nordmark@Sun.COM 			    connp->conn_ports) &&
136111042SErik.Nordmark@Sun.COM 			    IPCL_ZONE_MATCH(tconnp, connp->conn_zoneid)) {
13620Sstevel@tonic-gate 				/* Already have a conn. bail out */
13630Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
13640Sstevel@tonic-gate 				return (EADDRINUSE);
13650Sstevel@tonic-gate 			}
13660Sstevel@tonic-gate 		}
13670Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
13680Sstevel@tonic-gate 			/*
13690Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
13700Sstevel@tonic-gate 			 * rebind. Let it happen.
13710Sstevel@tonic-gate 			 */
13720Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
13730Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
13740Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
13750Sstevel@tonic-gate 		}
13763104Sjprakash 
13773104Sjprakash 		ASSERT(connp->conn_recv != NULL);
137811042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_recvicmp != NULL);
13793104Sjprakash 
13800Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
13810Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
13820Sstevel@tonic-gate 		break;
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	case IPPROTO_SCTP:
1385409Skcpoon 		/*
1386409Skcpoon 		 * The raw socket may have already been bound, remove it
1387409Skcpoon 		 * from the hash first.
1388409Skcpoon 		 */
1389409Skcpoon 		IPCL_HASH_REMOVE(connp);
13900Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
13910Sstevel@tonic-gate 		break;
13920Sstevel@tonic-gate 
13931676Sjpk 	default:
13941676Sjpk 		/*
13951676Sjpk 		 * Check for conflicts among MAC exempt bindings.  For
13961676Sjpk 		 * transports with port numbers, this is done by the upper
13971676Sjpk 		 * level per-transport binding logic.  For all others, it's
13981676Sjpk 		 * done here.
13991676Sjpk 		 */
14003448Sdh155122 		if (is_system_labeled() &&
14013448Sdh155122 		    check_exempt_conflict_v4(connp, ipst))
14021676Sjpk 			return (EADDRINUSE);
14031676Sjpk 		/* FALLTHROUGH */
14041676Sjpk 
14050Sstevel@tonic-gate 	case IPPROTO_UDP:
14060Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
14073448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
140811042SErik.Nordmark@Sun.COM 			    IPCL_UDP_HASH(lport, ipst)];
14090Sstevel@tonic-gate 		} else {
141011042SErik.Nordmark@Sun.COM 			connfp = &ipst->ips_ipcl_proto_fanout_v4[protocol];
14110Sstevel@tonic-gate 		}
14120Sstevel@tonic-gate 
141311042SErik.Nordmark@Sun.COM 		if (connp->conn_faddr_v4 != INADDR_ANY) {
14140Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
141511042SErik.Nordmark@Sun.COM 		} else if (connp->conn_laddr_v4 != INADDR_ANY) {
14160Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
14170Sstevel@tonic-gate 		} else {
14180Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
14190Sstevel@tonic-gate 		}
14200Sstevel@tonic-gate 		break;
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	return (ret);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate int
142711042SErik.Nordmark@Sun.COM ipcl_conn_insert_v6(conn_t *connp)
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate 	connf_t		*connfp;
14300Sstevel@tonic-gate 	conn_t		*tconnp;
14310Sstevel@tonic-gate 	int		ret = 0;
14323448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
143311042SErik.Nordmark@Sun.COM 	uint16_t	lport = connp->conn_lport;
143411042SErik.Nordmark@Sun.COM 	uint8_t		protocol = connp->conn_proto;
143511042SErik.Nordmark@Sun.COM 	uint_t		ifindex = connp->conn_bound_if;
14360Sstevel@tonic-gate 
143710616SSebastien.Roy@Sun.COM 	if (IPCL_IS_IPTUN(connp))
143811042SErik.Nordmark@Sun.COM 		return (ipcl_iptun_hash_insert_v6(connp, ipst));
143910616SSebastien.Roy@Sun.COM 
14400Sstevel@tonic-gate 	switch (protocol) {
14410Sstevel@tonic-gate 	case IPPROTO_TCP:
14428432SJonathan.Anderson@Sun.COM 
14438432SJonathan.Anderson@Sun.COM 		/*
14448432SJonathan.Anderson@Sun.COM 		 * For tcp, we check whether the connection tuple already
14458432SJonathan.Anderson@Sun.COM 		 * exists before allowing the connection to proceed.  We
14468432SJonathan.Anderson@Sun.COM 		 * also allow indexing on the zoneid. This is to allow
14478432SJonathan.Anderson@Sun.COM 		 * multiple shared stack zones to have the same tcp
14488432SJonathan.Anderson@Sun.COM 		 * connection tuple. In practice this only happens for
14498432SJonathan.Anderson@Sun.COM 		 * ipv6_loopback as it's the only local address which
14508432SJonathan.Anderson@Sun.COM 		 * doesn't have to be unique.
14518432SJonathan.Anderson@Sun.COM 		 */
14523448Sdh155122 		connfp = &ipst->ips_ipcl_conn_fanout[
145311042SErik.Nordmark@Sun.COM 		    IPCL_CONN_HASH_V6(connp->conn_faddr_v6, connp->conn_ports,
14543448Sdh155122 		    ipst)];
14550Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
14560Sstevel@tonic-gate 		for (tconnp = connfp->connf_head; tconnp != NULL;
14570Sstevel@tonic-gate 		    tconnp = tconnp->conn_next) {
145811042SErik.Nordmark@Sun.COM 			/* NOTE: need to match zoneid. Bug in onnv-gate */
145911042SErik.Nordmark@Sun.COM 			if (IPCL_CONN_MATCH_V6(tconnp, connp->conn_proto,
146011042SErik.Nordmark@Sun.COM 			    connp->conn_faddr_v6, connp->conn_laddr_v6,
14610Sstevel@tonic-gate 			    connp->conn_ports) &&
146211042SErik.Nordmark@Sun.COM 			    (tconnp->conn_bound_if == 0 ||
146311042SErik.Nordmark@Sun.COM 			    tconnp->conn_bound_if == ifindex) &&
146411042SErik.Nordmark@Sun.COM 			    IPCL_ZONE_MATCH(tconnp, connp->conn_zoneid)) {
14650Sstevel@tonic-gate 				/* Already have a conn. bail out */
14660Sstevel@tonic-gate 				mutex_exit(&connfp->connf_lock);
14670Sstevel@tonic-gate 				return (EADDRINUSE);
14680Sstevel@tonic-gate 			}
14690Sstevel@tonic-gate 		}
14700Sstevel@tonic-gate 		if (connp->conn_fanout != NULL) {
14710Sstevel@tonic-gate 			/*
14720Sstevel@tonic-gate 			 * Probably a XTI/TLI application trying to do a
14730Sstevel@tonic-gate 			 * rebind. Let it happen.
14740Sstevel@tonic-gate 			 */
14750Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
14760Sstevel@tonic-gate 			IPCL_HASH_REMOVE(connp);
14770Sstevel@tonic-gate 			mutex_enter(&connfp->connf_lock);
14780Sstevel@tonic-gate 		}
14790Sstevel@tonic-gate 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
14800Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
14810Sstevel@tonic-gate 		break;
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	case IPPROTO_SCTP:
1484409Skcpoon 		IPCL_HASH_REMOVE(connp);
14850Sstevel@tonic-gate 		ret = ipcl_sctp_hash_insert(connp, lport);
14860Sstevel@tonic-gate 		break;
14870Sstevel@tonic-gate 
14881676Sjpk 	default:
14893448Sdh155122 		if (is_system_labeled() &&
14903448Sdh155122 		    check_exempt_conflict_v6(connp, ipst))
14911676Sjpk 			return (EADDRINUSE);
14921676Sjpk 		/* FALLTHROUGH */
14930Sstevel@tonic-gate 	case IPPROTO_UDP:
14940Sstevel@tonic-gate 		if (protocol == IPPROTO_UDP) {
14953448Sdh155122 			connfp = &ipst->ips_ipcl_udp_fanout[
149611042SErik.Nordmark@Sun.COM 			    IPCL_UDP_HASH(lport, ipst)];
14970Sstevel@tonic-gate 		} else {
14983448Sdh155122 			connfp = &ipst->ips_ipcl_proto_fanout_v6[protocol];
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 
150111042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6)) {
15020Sstevel@tonic-gate 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
150311042SErik.Nordmark@Sun.COM 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
15040Sstevel@tonic-gate 			IPCL_HASH_INSERT_BOUND(connfp, connp);
15050Sstevel@tonic-gate 		} else {
15060Sstevel@tonic-gate 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
15070Sstevel@tonic-gate 		}
15080Sstevel@tonic-gate 		break;
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	return (ret);
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate /*
15150Sstevel@tonic-gate  * v4 packet classifying function. looks up the fanout table to
15160Sstevel@tonic-gate  * find the conn, the packet belongs to. returns the conn with
15170Sstevel@tonic-gate  * the reference held, null otherwise.
15181676Sjpk  *
15191676Sjpk  * If zoneid is ALL_ZONES, then the search rules described in the "Connection
15201676Sjpk  * Lookup" comment block are applied.  Labels are also checked as described
15211676Sjpk  * above.  If the packet is from the inside (looped back), and is from the same
15221676Sjpk  * zone, then label checks are omitted.
15230Sstevel@tonic-gate  */
15240Sstevel@tonic-gate conn_t *
152511042SErik.Nordmark@Sun.COM ipcl_classify_v4(mblk_t *mp, uint8_t protocol, uint_t hdr_len,
152611042SErik.Nordmark@Sun.COM     ip_recv_attr_t *ira, ip_stack_t *ipst)
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	ipha_t	*ipha;
15290Sstevel@tonic-gate 	connf_t	*connfp, *bind_connfp;
15300Sstevel@tonic-gate 	uint16_t lport;
15310Sstevel@tonic-gate 	uint16_t fport;
15320Sstevel@tonic-gate 	uint32_t ports;
15330Sstevel@tonic-gate 	conn_t	*connp;
15340Sstevel@tonic-gate 	uint16_t  *up;
153511042SErik.Nordmark@Sun.COM 	zoneid_t	zoneid = ira->ira_zoneid;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
15380Sstevel@tonic-gate 	up = (uint16_t *)((uchar_t *)ipha + hdr_len + TCP_PORTS_OFFSET);
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	switch (protocol) {
15410Sstevel@tonic-gate 	case IPPROTO_TCP:
15420Sstevel@tonic-gate 		ports = *(uint32_t *)up;
15430Sstevel@tonic-gate 		connfp =
15443448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_src,
15453448Sdh155122 		    ports, ipst)];
15460Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
15470Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
15480Sstevel@tonic-gate 		    connp = connp->conn_next) {
154911042SErik.Nordmark@Sun.COM 			if (IPCL_CONN_MATCH(connp, protocol,
155011042SErik.Nordmark@Sun.COM 			    ipha->ipha_src, ipha->ipha_dst, ports) &&
155111042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
155211042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
155311042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
155411042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
155511042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
15560Sstevel@tonic-gate 				break;
15570Sstevel@tonic-gate 		}
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 		if (connp != NULL) {
15601676Sjpk 			/*
15611676Sjpk 			 * We have a fully-bound TCP connection.
15621676Sjpk 			 *
15631676Sjpk 			 * For labeled systems, there's no need to check the
15641676Sjpk 			 * label here.  It's known to be good as we checked
15651676Sjpk 			 * before allowing the connection to become bound.
15661676Sjpk 			 */
15670Sstevel@tonic-gate 			CONN_INC_REF(connp);
15680Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
15690Sstevel@tonic-gate 			return (connp);
15700Sstevel@tonic-gate 		}
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
15730Sstevel@tonic-gate 		lport = up[1];
15743448Sdh155122 		bind_connfp =
15753448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
15760Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
15770Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
15780Sstevel@tonic-gate 		    connp = connp->conn_next) {
15791676Sjpk 			if (IPCL_BIND_MATCH(connp, protocol, ipha->ipha_dst,
158011042SErik.Nordmark@Sun.COM 			    lport) &&
158111042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
158211042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
158311042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
158411042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
158511042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
15860Sstevel@tonic-gate 				break;
15870Sstevel@tonic-gate 		}
15880Sstevel@tonic-gate 
15891676Sjpk 		/*
15901676Sjpk 		 * If the matching connection is SLP on a private address, then
15911676Sjpk 		 * the label on the packet must match the local zone's label.
15921676Sjpk 		 * Otherwise, it must be in the label range defined by tnrh.
159311042SErik.Nordmark@Sun.COM 		 * This is ensured by tsol_receive_local.
159411042SErik.Nordmark@Sun.COM 		 *
159511042SErik.Nordmark@Sun.COM 		 * Note that we don't check tsol_receive_local for
159611042SErik.Nordmark@Sun.COM 		 * the connected case.
15971676Sjpk 		 */
159811042SErik.Nordmark@Sun.COM 		if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
15991676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
160011042SErik.Nordmark@Sun.COM 		    ira, connp)) {
160111042SErik.Nordmark@Sun.COM 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp,
160211042SErik.Nordmark@Sun.COM 			    char *, "connp(1) could not receive mp(2)",
160311042SErik.Nordmark@Sun.COM 			    conn_t *, connp, mblk_t *, mp);
16041676Sjpk 			connp = NULL;
16051676Sjpk 		}
16061676Sjpk 
16070Sstevel@tonic-gate 		if (connp != NULL) {
16081676Sjpk 			/* Have a listener at least */
16090Sstevel@tonic-gate 			CONN_INC_REF(connp);
16100Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
16110Sstevel@tonic-gate 			return (connp);
16120Sstevel@tonic-gate 		}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
16150Sstevel@tonic-gate 		break;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	case IPPROTO_UDP:
16180Sstevel@tonic-gate 		lport = up[1];
16190Sstevel@tonic-gate 		fport = up[0];
16203448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
16210Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16220Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16230Sstevel@tonic-gate 		    connp = connp->conn_next) {
16240Sstevel@tonic-gate 			if (IPCL_UDP_MATCH(connp, lport, ipha->ipha_dst,
16250Sstevel@tonic-gate 			    fport, ipha->ipha_src) &&
162611042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
162711042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
162811042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
162911042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE))))
16300Sstevel@tonic-gate 				break;
16310Sstevel@tonic-gate 		}
16320Sstevel@tonic-gate 
163311042SErik.Nordmark@Sun.COM 		if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
16341676Sjpk 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
163511042SErik.Nordmark@Sun.COM 		    ira, connp)) {
16361676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp,
16371676Sjpk 			    char *, "connp(1) could not receive mp(2)",
16381676Sjpk 			    conn_t *, connp, mblk_t *, mp);
16391676Sjpk 			connp = NULL;
16401676Sjpk 		}
16411676Sjpk 
16420Sstevel@tonic-gate 		if (connp != NULL) {
16430Sstevel@tonic-gate 			CONN_INC_REF(connp);
16440Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
16450Sstevel@tonic-gate 			return (connp);
16460Sstevel@tonic-gate 		}
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 		/*
16490Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
16500Sstevel@tonic-gate 		 */
16510Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
165211042SErik.Nordmark@Sun.COM 
16530Sstevel@tonic-gate 		break;
165410616SSebastien.Roy@Sun.COM 
165510616SSebastien.Roy@Sun.COM 	case IPPROTO_ENCAP:
165610616SSebastien.Roy@Sun.COM 	case IPPROTO_IPV6:
165710616SSebastien.Roy@Sun.COM 		return (ipcl_iptun_classify_v4(&ipha->ipha_src,
165810616SSebastien.Roy@Sun.COM 		    &ipha->ipha_dst, ipst));
16590Sstevel@tonic-gate 	}
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	return (NULL);
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate conn_t *
166511042SErik.Nordmark@Sun.COM ipcl_classify_v6(mblk_t *mp, uint8_t protocol, uint_t hdr_len,
166611042SErik.Nordmark@Sun.COM     ip_recv_attr_t *ira, ip_stack_t *ipst)
16670Sstevel@tonic-gate {
16680Sstevel@tonic-gate 	ip6_t		*ip6h;
16690Sstevel@tonic-gate 	connf_t		*connfp, *bind_connfp;
16700Sstevel@tonic-gate 	uint16_t	lport;
16710Sstevel@tonic-gate 	uint16_t	fport;
167211042SErik.Nordmark@Sun.COM 	tcpha_t		*tcpha;
16730Sstevel@tonic-gate 	uint32_t	ports;
16740Sstevel@tonic-gate 	conn_t		*connp;
16750Sstevel@tonic-gate 	uint16_t	*up;
167611042SErik.Nordmark@Sun.COM 	zoneid_t	zoneid = ira->ira_zoneid;
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	ip6h = (ip6_t *)mp->b_rptr;
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	switch (protocol) {
16810Sstevel@tonic-gate 	case IPPROTO_TCP:
168211042SErik.Nordmark@Sun.COM 		tcpha = (tcpha_t *)&mp->b_rptr[hdr_len];
168311042SErik.Nordmark@Sun.COM 		up = &tcpha->tha_lport;
16840Sstevel@tonic-gate 		ports = *(uint32_t *)up;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 		connfp =
16873448Sdh155122 		    &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_src,
16883448Sdh155122 		    ports, ipst)];
16890Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
16900Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
16910Sstevel@tonic-gate 		    connp = connp->conn_next) {
169211042SErik.Nordmark@Sun.COM 			if (IPCL_CONN_MATCH_V6(connp, protocol,
169311042SErik.Nordmark@Sun.COM 			    ip6h->ip6_src, ip6h->ip6_dst, ports) &&
169411042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
169511042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
169611042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
169711042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
169811042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
16990Sstevel@tonic-gate 				break;
17000Sstevel@tonic-gate 		}
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 		if (connp != NULL) {
17031676Sjpk 			/*
17041676Sjpk 			 * We have a fully-bound TCP connection.
17051676Sjpk 			 *
17061676Sjpk 			 * For labeled systems, there's no need to check the
17071676Sjpk 			 * label here.  It's known to be good as we checked
17081676Sjpk 			 * before allowing the connection to become bound.
17091676Sjpk 			 */
17100Sstevel@tonic-gate 			CONN_INC_REF(connp);
17110Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
17120Sstevel@tonic-gate 			return (connp);
17130Sstevel@tonic-gate 		}
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 		lport = up[1];
17183448Sdh155122 		bind_connfp =
17193448Sdh155122 		    &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
17200Sstevel@tonic-gate 		mutex_enter(&bind_connfp->connf_lock);
17210Sstevel@tonic-gate 		for (connp = bind_connfp->connf_head; connp != NULL;
17220Sstevel@tonic-gate 		    connp = connp->conn_next) {
17230Sstevel@tonic-gate 			if (IPCL_BIND_MATCH_V6(connp, protocol,
17240Sstevel@tonic-gate 			    ip6h->ip6_dst, lport) &&
172511042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
172611042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
172711042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
172811042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
172911042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
17300Sstevel@tonic-gate 				break;
17310Sstevel@tonic-gate 		}
17320Sstevel@tonic-gate 
173311042SErik.Nordmark@Sun.COM 		if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
17341676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
173511042SErik.Nordmark@Sun.COM 		    ira, connp)) {
17361676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp6,
17371676Sjpk 			    char *, "connp(1) could not receive mp(2)",
17381676Sjpk 			    conn_t *, connp, mblk_t *, mp);
17391676Sjpk 			connp = NULL;
17401676Sjpk 		}
17411676Sjpk 
17420Sstevel@tonic-gate 		if (connp != NULL) {
17430Sstevel@tonic-gate 			/* Have a listner at least */
17440Sstevel@tonic-gate 			CONN_INC_REF(connp);
17450Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
17460Sstevel@tonic-gate 			return (connp);
17470Sstevel@tonic-gate 		}
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 		mutex_exit(&bind_connfp->connf_lock);
17500Sstevel@tonic-gate 		break;
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	case IPPROTO_UDP:
17530Sstevel@tonic-gate 		up = (uint16_t *)&mp->b_rptr[hdr_len];
17540Sstevel@tonic-gate 		lport = up[1];
17550Sstevel@tonic-gate 		fport = up[0];
17563448Sdh155122 		connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
17570Sstevel@tonic-gate 		mutex_enter(&connfp->connf_lock);
17580Sstevel@tonic-gate 		for (connp = connfp->connf_head; connp != NULL;
17590Sstevel@tonic-gate 		    connp = connp->conn_next) {
17600Sstevel@tonic-gate 			if (IPCL_UDP_MATCH_V6(connp, lport, ip6h->ip6_dst,
17610Sstevel@tonic-gate 			    fport, ip6h->ip6_src) &&
176211042SErik.Nordmark@Sun.COM 			    (connp->conn_zoneid == zoneid ||
176311042SErik.Nordmark@Sun.COM 			    connp->conn_allzones ||
176411042SErik.Nordmark@Sun.COM 			    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
176511042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
176611042SErik.Nordmark@Sun.COM 			    (ira->ira_flags & IRAF_TX_SHARED_ADDR))))
17670Sstevel@tonic-gate 				break;
17680Sstevel@tonic-gate 		}
17690Sstevel@tonic-gate 
177011042SErik.Nordmark@Sun.COM 		if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
17711676Sjpk 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
177211042SErik.Nordmark@Sun.COM 		    ira, connp)) {
17731676Sjpk 			DTRACE_PROBE3(tx__ip__log__info__classify__udp6,
17741676Sjpk 			    char *, "connp(1) could not receive mp(2)",
17751676Sjpk 			    conn_t *, connp, mblk_t *, mp);
17761676Sjpk 			connp = NULL;
17771676Sjpk 		}
17781676Sjpk 
17790Sstevel@tonic-gate 		if (connp != NULL) {
17800Sstevel@tonic-gate 			CONN_INC_REF(connp);
17810Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
17820Sstevel@tonic-gate 			return (connp);
17830Sstevel@tonic-gate 		}
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 		/*
17860Sstevel@tonic-gate 		 * We shouldn't come here for multicast/broadcast packets
17870Sstevel@tonic-gate 		 */
17880Sstevel@tonic-gate 		mutex_exit(&connfp->connf_lock);
17890Sstevel@tonic-gate 		break;
179010616SSebastien.Roy@Sun.COM 	case IPPROTO_ENCAP:
179110616SSebastien.Roy@Sun.COM 	case IPPROTO_IPV6:
179210616SSebastien.Roy@Sun.COM 		return (ipcl_iptun_classify_v6(&ip6h->ip6_src,
179310616SSebastien.Roy@Sun.COM 		    &ip6h->ip6_dst, ipst));
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	return (NULL);
17970Sstevel@tonic-gate }
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate /*
18000Sstevel@tonic-gate  * wrapper around ipcl_classify_(v4,v6) routines.
18010Sstevel@tonic-gate  */
18020Sstevel@tonic-gate conn_t *
180311042SErik.Nordmark@Sun.COM ipcl_classify(mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst)
18040Sstevel@tonic-gate {
180511042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_IS_IPV4) {
180611042SErik.Nordmark@Sun.COM 		return (ipcl_classify_v4(mp, ira->ira_protocol,
180711042SErik.Nordmark@Sun.COM 		    ira->ira_ip_hdr_length, ira, ipst));
180811042SErik.Nordmark@Sun.COM 	} else {
180911042SErik.Nordmark@Sun.COM 		return (ipcl_classify_v6(mp, ira->ira_protocol,
181011042SErik.Nordmark@Sun.COM 		    ira->ira_ip_hdr_length, ira, ipst));
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate }
18130Sstevel@tonic-gate 
181411042SErik.Nordmark@Sun.COM /*
181511042SErik.Nordmark@Sun.COM  * Only used to classify SCTP RAW sockets
181611042SErik.Nordmark@Sun.COM  */
18170Sstevel@tonic-gate conn_t *
181811042SErik.Nordmark@Sun.COM ipcl_classify_raw(mblk_t *mp, uint8_t protocol, uint32_t ports,
181911042SErik.Nordmark@Sun.COM     ipha_t *ipha, ip6_t *ip6h, ip_recv_attr_t *ira, ip_stack_t *ipst)
18200Sstevel@tonic-gate {
18211676Sjpk 	connf_t		*connfp;
18220Sstevel@tonic-gate 	conn_t		*connp;
18230Sstevel@tonic-gate 	in_port_t	lport;
182411042SErik.Nordmark@Sun.COM 	int		ipversion;
18251676Sjpk 	const void	*dst;
182611042SErik.Nordmark@Sun.COM 	zoneid_t	zoneid = ira->ira_zoneid;
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	lport = ((uint16_t *)&ports)[1];
182911042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_IS_IPV4) {
183011042SErik.Nordmark@Sun.COM 		dst = (const void *)&ipha->ipha_dst;
183111042SErik.Nordmark@Sun.COM 		ipversion = IPV4_VERSION;
183211042SErik.Nordmark@Sun.COM 	} else {
183311042SErik.Nordmark@Sun.COM 		dst = (const void *)&ip6h->ip6_dst;
183411042SErik.Nordmark@Sun.COM 		ipversion = IPV6_VERSION;
18351676Sjpk 	}
18361676Sjpk 
18373448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport), ipst)];
18380Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
18390Sstevel@tonic-gate 	for (connp = connfp->connf_head; connp != NULL;
18400Sstevel@tonic-gate 	    connp = connp->conn_next) {
18410Sstevel@tonic-gate 		/* We don't allow v4 fallback for v6 raw socket. */
184211042SErik.Nordmark@Sun.COM 		if (ipversion != connp->conn_ipversion)
18430Sstevel@tonic-gate 			continue;
184411042SErik.Nordmark@Sun.COM 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6) &&
184511042SErik.Nordmark@Sun.COM 		    !IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_faddr_v6)) {
184611042SErik.Nordmark@Sun.COM 			if (ipversion == IPV4_VERSION) {
18471676Sjpk 				if (!IPCL_CONN_MATCH(connp, protocol,
184811042SErik.Nordmark@Sun.COM 				    ipha->ipha_src, ipha->ipha_dst, ports))
18491676Sjpk 					continue;
18500Sstevel@tonic-gate 			} else {
18511676Sjpk 				if (!IPCL_CONN_MATCH_V6(connp, protocol,
185211042SErik.Nordmark@Sun.COM 				    ip6h->ip6_src, ip6h->ip6_dst, ports))
18531676Sjpk 					continue;
18540Sstevel@tonic-gate 			}
18550Sstevel@tonic-gate 		} else {
185611042SErik.Nordmark@Sun.COM 			if (ipversion == IPV4_VERSION) {
18571676Sjpk 				if (!IPCL_BIND_MATCH(connp, protocol,
185811042SErik.Nordmark@Sun.COM 				    ipha->ipha_dst, lport))
18591676Sjpk 					continue;
18600Sstevel@tonic-gate 			} else {
18611676Sjpk 				if (!IPCL_BIND_MATCH_V6(connp, protocol,
186211042SErik.Nordmark@Sun.COM 				    ip6h->ip6_dst, lport))
18631676Sjpk 					continue;
18640Sstevel@tonic-gate 			}
18650Sstevel@tonic-gate 		}
18661676Sjpk 
186711042SErik.Nordmark@Sun.COM 		if (connp->conn_zoneid == zoneid ||
186811042SErik.Nordmark@Sun.COM 		    connp->conn_allzones ||
186911042SErik.Nordmark@Sun.COM 		    ((connp->conn_mac_mode != CONN_MAC_DEFAULT) &&
187011042SErik.Nordmark@Sun.COM 		    (ira->ira_flags & IRAF_TX_MAC_EXEMPTABLE) &&
187111042SErik.Nordmark@Sun.COM 		    (ira->ira_flags & IRAF_TX_SHARED_ADDR)))
18721676Sjpk 			break;
18731676Sjpk 	}
187411042SErik.Nordmark@Sun.COM 
187511042SErik.Nordmark@Sun.COM 	if (connp != NULL && (ira->ira_flags & IRAF_SYSTEM_LABELED) &&
187611042SErik.Nordmark@Sun.COM 	    !tsol_receive_local(mp, dst, ipversion, ira, connp)) {
18771676Sjpk 		DTRACE_PROBE3(tx__ip__log__info__classify__rawip,
18781676Sjpk 		    char *, "connp(1) could not receive mp(2)",
18791676Sjpk 		    conn_t *, connp, mblk_t *, mp);
18801676Sjpk 		connp = NULL;
18810Sstevel@tonic-gate 	}
1882409Skcpoon 
1883409Skcpoon 	if (connp != NULL)
1884409Skcpoon 		goto found;
1885409Skcpoon 	mutex_exit(&connfp->connf_lock);
1886409Skcpoon 
188711042SErik.Nordmark@Sun.COM 	/* Try to look for a wildcard SCTP RAW socket match. */
18883448Sdh155122 	connfp = &ipst->ips_ipcl_raw_fanout[IPCL_RAW_HASH(0, ipst)];
1889409Skcpoon 	mutex_enter(&connfp->connf_lock);
1890409Skcpoon 	for (connp = connfp->connf_head; connp != NULL;
1891409Skcpoon 	    connp = connp->conn_next) {
1892409Skcpoon 		/* We don't allow v4 fallback for v6 raw socket. */
189311042SErik.Nordmark@Sun.COM 		if (ipversion != connp->conn_ipversion)
189411042SErik.Nordmark@Sun.COM 			continue;
189511042SErik.Nordmark@Sun.COM 		if (!IPCL_ZONE_MATCH(connp, zoneid))
1896409Skcpoon 			continue;
189711042SErik.Nordmark@Sun.COM 
189811042SErik.Nordmark@Sun.COM 		if (ipversion == IPV4_VERSION) {
189911042SErik.Nordmark@Sun.COM 			if (IPCL_RAW_MATCH(connp, protocol, ipha->ipha_dst))
1900409Skcpoon 				break;
1901409Skcpoon 		} else {
190211042SErik.Nordmark@Sun.COM 			if (IPCL_RAW_MATCH_V6(connp, protocol, ip6h->ip6_dst)) {
1903409Skcpoon 				break;
1904409Skcpoon 			}
1905409Skcpoon 		}
19060Sstevel@tonic-gate 	}
1907409Skcpoon 
1908409Skcpoon 	if (connp != NULL)
1909409Skcpoon 		goto found;
1910409Skcpoon 
19110Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
19120Sstevel@tonic-gate 	return (NULL);
1913409Skcpoon 
1914409Skcpoon found:
1915409Skcpoon 	ASSERT(connp != NULL);
1916409Skcpoon 	CONN_INC_REF(connp);
1917409Skcpoon 	mutex_exit(&connfp->connf_lock);
1918409Skcpoon 	return (connp);
19190Sstevel@tonic-gate }
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate /* ARGSUSED */
19220Sstevel@tonic-gate static int
19235240Snordmark tcp_conn_constructor(void *buf, void *cdrarg, int kmflags)
19240Sstevel@tonic-gate {
19250Sstevel@tonic-gate 	itc_t	*itc = (itc_t *)buf;
19260Sstevel@tonic-gate 	conn_t 	*connp = &itc->itc_conn;
19275240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
19285240Snordmark 
19295240Snordmark 	bzero(connp, sizeof (conn_t));
19305240Snordmark 	bzero(tcp, sizeof (tcp_t));
19315240Snordmark 
19325240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
19335240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
19348348SEric.Yu@Sun.COM 	cv_init(&connp->conn_sq_cv, NULL, CV_DEFAULT, NULL);
193511042SErik.Nordmark@Sun.COM 	tcp->tcp_timercache = tcp_timermp_alloc(kmflags);
193611042SErik.Nordmark@Sun.COM 	if (tcp->tcp_timercache == NULL)
193711042SErik.Nordmark@Sun.COM 		return (ENOMEM);
19380Sstevel@tonic-gate 	connp->conn_tcp = tcp;
19390Sstevel@tonic-gate 	connp->conn_flags = IPCL_TCPCONN;
194011042SErik.Nordmark@Sun.COM 	connp->conn_proto = IPPROTO_TCP;
19410Sstevel@tonic-gate 	tcp->tcp_connp = connp;
194211042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
194311042SErik.Nordmark@Sun.COM 
194411042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
194511042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL) {
194611042SErik.Nordmark@Sun.COM 		tcp_timermp_free(tcp);
194711042SErik.Nordmark@Sun.COM 		return (ENOMEM);
194811042SErik.Nordmark@Sun.COM 	}
194911042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
195011042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
195111042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
19520Sstevel@tonic-gate 	return (0);
19530Sstevel@tonic-gate }
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate /* ARGSUSED */
19560Sstevel@tonic-gate static void
19575240Snordmark tcp_conn_destructor(void *buf, void *cdrarg)
19585240Snordmark {
19595240Snordmark 	itc_t	*itc = (itc_t *)buf;
19605240Snordmark 	conn_t 	*connp = &itc->itc_conn;
19615240Snordmark 	tcp_t	*tcp = (tcp_t *)&itc[1];
19625240Snordmark 
19635240Snordmark 	ASSERT(connp->conn_flags & IPCL_TCPCONN);
19645240Snordmark 	ASSERT(tcp->tcp_connp == connp);
19655240Snordmark 	ASSERT(connp->conn_tcp == tcp);
19665240Snordmark 	tcp_timermp_free(tcp);
19675240Snordmark 	mutex_destroy(&connp->conn_lock);
19685240Snordmark 	cv_destroy(&connp->conn_cv);
19698348SEric.Yu@Sun.COM 	cv_destroy(&connp->conn_sq_cv);
197011042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
197111042SErik.Nordmark@Sun.COM 
197211042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
197311042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
197411042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
197511042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
197611042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
197711042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
197811042SErik.Nordmark@Sun.COM 	}
19795240Snordmark }
19805240Snordmark 
19815240Snordmark /* ARGSUSED */
19825240Snordmark static int
19835240Snordmark ip_conn_constructor(void *buf, void *cdrarg, int kmflags)
19845240Snordmark {
19855240Snordmark 	itc_t	*itc = (itc_t *)buf;
19865240Snordmark 	conn_t 	*connp = &itc->itc_conn;
19875240Snordmark 
19885240Snordmark 	bzero(connp, sizeof (conn_t));
19895240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
19905240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
19915240Snordmark 	connp->conn_flags = IPCL_IPCCONN;
199211042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
19935240Snordmark 
199411042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
199511042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL)
199611042SErik.Nordmark@Sun.COM 		return (ENOMEM);
199711042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
199811042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
19995240Snordmark 	return (0);
20005240Snordmark }
20015240Snordmark 
20025240Snordmark /* ARGSUSED */
20035240Snordmark static void
20045240Snordmark ip_conn_destructor(void *buf, void *cdrarg)
20055240Snordmark {
20065240Snordmark 	itc_t	*itc = (itc_t *)buf;
20075240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20085240Snordmark 
20095240Snordmark 	ASSERT(connp->conn_flags & IPCL_IPCCONN);
20105240Snordmark 	ASSERT(connp->conn_priv == NULL);
20115240Snordmark 	mutex_destroy(&connp->conn_lock);
20125240Snordmark 	cv_destroy(&connp->conn_cv);
201311042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
201411042SErik.Nordmark@Sun.COM 
201511042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
201611042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
201711042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
201811042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
201911042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
202011042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
202111042SErik.Nordmark@Sun.COM 	}
20225240Snordmark }
20235240Snordmark 
20245240Snordmark /* ARGSUSED */
20255240Snordmark static int
20265240Snordmark udp_conn_constructor(void *buf, void *cdrarg, int kmflags)
20275240Snordmark {
20285240Snordmark 	itc_t	*itc = (itc_t *)buf;
20295240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20305240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
20315240Snordmark 
20325240Snordmark 	bzero(connp, sizeof (conn_t));
20335240Snordmark 	bzero(udp, sizeof (udp_t));
20345240Snordmark 
20355240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20365240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20375240Snordmark 	connp->conn_udp = udp;
20385240Snordmark 	connp->conn_flags = IPCL_UDPCONN;
203911042SErik.Nordmark@Sun.COM 	connp->conn_proto = IPPROTO_UDP;
20405240Snordmark 	udp->udp_connp = connp;
204111042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
204211042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
204311042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL)
204411042SErik.Nordmark@Sun.COM 		return (ENOMEM);
204511042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
204611042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
204711042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
20485240Snordmark 	return (0);
20495240Snordmark }
20505240Snordmark 
20515240Snordmark /* ARGSUSED */
20525240Snordmark static void
20535240Snordmark udp_conn_destructor(void *buf, void *cdrarg)
20545240Snordmark {
20555240Snordmark 	itc_t	*itc = (itc_t *)buf;
20565240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20575240Snordmark 	udp_t	*udp = (udp_t *)&itc[1];
20585240Snordmark 
20595240Snordmark 	ASSERT(connp->conn_flags & IPCL_UDPCONN);
20605240Snordmark 	ASSERT(udp->udp_connp == connp);
20615240Snordmark 	ASSERT(connp->conn_udp == udp);
20625240Snordmark 	mutex_destroy(&connp->conn_lock);
20635240Snordmark 	cv_destroy(&connp->conn_cv);
206411042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
206511042SErik.Nordmark@Sun.COM 
206611042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
206711042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
206811042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
206911042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
207011042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
207111042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
207211042SErik.Nordmark@Sun.COM 	}
20735240Snordmark }
20745240Snordmark 
20755240Snordmark /* ARGSUSED */
20765240Snordmark static int
20775240Snordmark rawip_conn_constructor(void *buf, void *cdrarg, int kmflags)
20780Sstevel@tonic-gate {
20795240Snordmark 	itc_t	*itc = (itc_t *)buf;
20805240Snordmark 	conn_t 	*connp = &itc->itc_conn;
20815240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
20825240Snordmark 
20835240Snordmark 	bzero(connp, sizeof (conn_t));
20845240Snordmark 	bzero(icmp, sizeof (icmp_t));
20855240Snordmark 
20865240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
20875240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
20885240Snordmark 	connp->conn_icmp = icmp;
20895240Snordmark 	connp->conn_flags = IPCL_RAWIPCONN;
209011042SErik.Nordmark@Sun.COM 	connp->conn_proto = IPPROTO_ICMP;
20915240Snordmark 	icmp->icmp_connp = connp;
209211042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
209311042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
209411042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL)
209511042SErik.Nordmark@Sun.COM 		return (ENOMEM);
209611042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
209711042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_protocol = connp->conn_proto;
209811042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
20995240Snordmark 	return (0);
21005240Snordmark }
21015240Snordmark 
21025240Snordmark /* ARGSUSED */
21035240Snordmark static void
21045240Snordmark rawip_conn_destructor(void *buf, void *cdrarg)
21055240Snordmark {
21065240Snordmark 	itc_t	*itc = (itc_t *)buf;
21075240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21085240Snordmark 	icmp_t	*icmp = (icmp_t *)&itc[1];
21095240Snordmark 
21105240Snordmark 	ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
21115240Snordmark 	ASSERT(icmp->icmp_connp == connp);
21125240Snordmark 	ASSERT(connp->conn_icmp == icmp);
21135240Snordmark 	mutex_destroy(&connp->conn_lock);
21145240Snordmark 	cv_destroy(&connp->conn_cv);
211511042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
211611042SErik.Nordmark@Sun.COM 
211711042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
211811042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
211911042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
212011042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
212111042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
212211042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
212311042SErik.Nordmark@Sun.COM 	}
21245240Snordmark }
21255240Snordmark 
21265240Snordmark /* ARGSUSED */
21275240Snordmark static int
21285240Snordmark rts_conn_constructor(void *buf, void *cdrarg, int kmflags)
21295240Snordmark {
21305240Snordmark 	itc_t	*itc = (itc_t *)buf;
21315240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21325240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21335240Snordmark 
21345240Snordmark 	bzero(connp, sizeof (conn_t));
21355240Snordmark 	bzero(rts, sizeof (rts_t));
21365240Snordmark 
21375240Snordmark 	mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
21385240Snordmark 	cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
21395240Snordmark 	connp->conn_rts = rts;
21405240Snordmark 	connp->conn_flags = IPCL_RTSCONN;
21415240Snordmark 	rts->rts_connp = connp;
214211042SErik.Nordmark@Sun.COM 	rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
214311042SErik.Nordmark@Sun.COM 	connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
214411042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa == NULL)
214511042SErik.Nordmark@Sun.COM 		return (ENOMEM);
214611042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_refcnt = 1;
214711042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
21485240Snordmark 	return (0);
21495240Snordmark }
21505240Snordmark 
21515240Snordmark /* ARGSUSED */
21525240Snordmark static void
21535240Snordmark rts_conn_destructor(void *buf, void *cdrarg)
21545240Snordmark {
21555240Snordmark 	itc_t	*itc = (itc_t *)buf;
21565240Snordmark 	conn_t 	*connp = &itc->itc_conn;
21575240Snordmark 	rts_t	*rts = (rts_t *)&itc[1];
21585240Snordmark 
21595240Snordmark 	ASSERT(connp->conn_flags & IPCL_RTSCONN);
21605240Snordmark 	ASSERT(rts->rts_connp == connp);
21615240Snordmark 	ASSERT(connp->conn_rts == rts);
21625240Snordmark 	mutex_destroy(&connp->conn_lock);
21635240Snordmark 	cv_destroy(&connp->conn_cv);
216411042SErik.Nordmark@Sun.COM 	rw_destroy(&connp->conn_ilg_lock);
21658444SRao.Shoaib@Sun.COM 
216611042SErik.Nordmark@Sun.COM 	/* Can be NULL if constructor failed */
216711042SErik.Nordmark@Sun.COM 	if (connp->conn_ixa != NULL) {
216811042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_refcnt == 1);
216911042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_ire == NULL);
217011042SErik.Nordmark@Sun.COM 		ASSERT(connp->conn_ixa->ixa_nce == NULL);
217111042SErik.Nordmark@Sun.COM 		ixa_refrele(connp->conn_ixa);
21728348SEric.Yu@Sun.COM 	}
21738348SEric.Yu@Sun.COM }
21748348SEric.Yu@Sun.COM 
21755240Snordmark /*
21765240Snordmark  * Called as part of ipcl_conn_destroy to assert and clear any pointers
21775240Snordmark  * in the conn_t.
217811042SErik.Nordmark@Sun.COM  *
217911042SErik.Nordmark@Sun.COM  * Below we list all the pointers in the conn_t as a documentation aid.
218011042SErik.Nordmark@Sun.COM  * The ones that we can not ASSERT to be NULL are #ifdef'ed out.
218111042SErik.Nordmark@Sun.COM  * If you add any pointers to the conn_t please add an ASSERT here
218211042SErik.Nordmark@Sun.COM  * and #ifdef it out if it can't be actually asserted to be NULL.
218311042SErik.Nordmark@Sun.COM  * In any case, we bzero most of the conn_t at the end of the function.
21845240Snordmark  */
21855240Snordmark void
21865240Snordmark ipcl_conn_cleanup(conn_t *connp)
21875240Snordmark {
218811042SErik.Nordmark@Sun.COM 	ip_xmit_attr_t	*ixa;
218911042SErik.Nordmark@Sun.COM 
21905240Snordmark 	ASSERT(connp->conn_latch == NULL);
219111042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_latch_in_policy == NULL);
219211042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_latch_in_action == NULL);
21935240Snordmark #ifdef notdef
21945240Snordmark 	ASSERT(connp->conn_rq == NULL);
21955240Snordmark 	ASSERT(connp->conn_wq == NULL);
21965240Snordmark #endif
21975240Snordmark 	ASSERT(connp->conn_cred == NULL);
21985240Snordmark 	ASSERT(connp->conn_g_fanout == NULL);
21995240Snordmark 	ASSERT(connp->conn_g_next == NULL);
22005240Snordmark 	ASSERT(connp->conn_g_prev == NULL);
22015240Snordmark 	ASSERT(connp->conn_policy == NULL);
22025240Snordmark 	ASSERT(connp->conn_fanout == NULL);
22035240Snordmark 	ASSERT(connp->conn_next == NULL);
22045240Snordmark 	ASSERT(connp->conn_prev == NULL);
22055240Snordmark 	ASSERT(connp->conn_oper_pending_ill == NULL);
22065240Snordmark 	ASSERT(connp->conn_ilg == NULL);
22075240Snordmark 	ASSERT(connp->conn_drain_next == NULL);
22085240Snordmark 	ASSERT(connp->conn_drain_prev == NULL);
22095277Snordmark #ifdef notdef
22105277Snordmark 	/* conn_idl is not cleared when removed from idl list */
22115240Snordmark 	ASSERT(connp->conn_idl == NULL);
22125277Snordmark #endif
22135240Snordmark 	ASSERT(connp->conn_ipsec_opt_mp == NULL);
221411042SErik.Nordmark@Sun.COM #ifdef notdef
221511042SErik.Nordmark@Sun.COM 	/* conn_netstack is cleared by the caller; needed by ixa_cleanup */
22165240Snordmark 	ASSERT(connp->conn_netstack == NULL);
221711042SErik.Nordmark@Sun.COM #endif
22185240Snordmark 
22198348SEric.Yu@Sun.COM 	ASSERT(connp->conn_helper_info == NULL);
222011042SErik.Nordmark@Sun.COM 	ASSERT(connp->conn_ixa != NULL);
222111042SErik.Nordmark@Sun.COM 	ixa = connp->conn_ixa;
222211042SErik.Nordmark@Sun.COM 	ASSERT(ixa->ixa_refcnt == 1);
222311042SErik.Nordmark@Sun.COM 	/* Need to preserve ixa_protocol */
222411042SErik.Nordmark@Sun.COM 	ixa_cleanup(ixa);
222511042SErik.Nordmark@Sun.COM 	ixa->ixa_flags = 0;
222611042SErik.Nordmark@Sun.COM 
22275240Snordmark 	/* Clear out the conn_t fields that are not preserved */
22285240Snordmark 	bzero(&connp->conn_start_clr,
22295240Snordmark 	    sizeof (conn_t) -
22305240Snordmark 	    ((uchar_t *)&connp->conn_start_clr - (uchar_t *)connp));
22310Sstevel@tonic-gate }
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate /*
22340Sstevel@tonic-gate  * All conns are inserted in a global multi-list for the benefit of
22350Sstevel@tonic-gate  * walkers. The walk is guaranteed to walk all open conns at the time
22360Sstevel@tonic-gate  * of the start of the walk exactly once. This property is needed to
22370Sstevel@tonic-gate  * achieve some cleanups during unplumb of interfaces. This is achieved
22380Sstevel@tonic-gate  * as follows.
22390Sstevel@tonic-gate  *
22400Sstevel@tonic-gate  * ipcl_conn_create and ipcl_conn_destroy are the only functions that
22410Sstevel@tonic-gate  * call the insert and delete functions below at creation and deletion
22420Sstevel@tonic-gate  * time respectively. The conn never moves or changes its position in this
22430Sstevel@tonic-gate  * multi-list during its lifetime. CONN_CONDEMNED ensures that the refcnt
22440Sstevel@tonic-gate  * won't increase due to walkers, once the conn deletion has started. Note
22450Sstevel@tonic-gate  * that we can't remove the conn from the global list and then wait for
22460Sstevel@tonic-gate  * the refcnt to drop to zero, since walkers would then see a truncated
22470Sstevel@tonic-gate  * list. CONN_INCIPIENT ensures that walkers don't start looking at
22480Sstevel@tonic-gate  * conns until ip_open is ready to make them globally visible.
22490Sstevel@tonic-gate  * The global round robin multi-list locks are held only to get the
22500Sstevel@tonic-gate  * next member/insertion/deletion and contention should be negligible
22510Sstevel@tonic-gate  * if the multi-list is much greater than the number of cpus.
22520Sstevel@tonic-gate  */
22530Sstevel@tonic-gate void
22540Sstevel@tonic-gate ipcl_globalhash_insert(conn_t *connp)
22550Sstevel@tonic-gate {
22560Sstevel@tonic-gate 	int	index;
22573448Sdh155122 	struct connf_s	*connfp;
22583448Sdh155122 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	/*
22610Sstevel@tonic-gate 	 * No need for atomic here. Approximate even distribution
22620Sstevel@tonic-gate 	 * in the global lists is sufficient.
22630Sstevel@tonic-gate 	 */
22643448Sdh155122 	ipst->ips_conn_g_index++;
22653448Sdh155122 	index = ipst->ips_conn_g_index & (CONN_G_HASH_SIZE - 1);
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
22680Sstevel@tonic-gate 	/*
22690Sstevel@tonic-gate 	 * Mark as INCIPIENT, so that walkers will ignore this
22700Sstevel@tonic-gate 	 * for now, till ip_open is ready to make it visible globally.
22710Sstevel@tonic-gate 	 */
22720Sstevel@tonic-gate 	connp->conn_state_flags |= CONN_INCIPIENT;
22730Sstevel@tonic-gate 
22743448Sdh155122 	connfp = &ipst->ips_ipcl_globalhash_fanout[index];
22750Sstevel@tonic-gate 	/* Insert at the head of the list */
22763448Sdh155122 	mutex_enter(&connfp->connf_lock);
22773448Sdh155122 	connp->conn_g_next = connfp->connf_head;
22780Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
22790Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp;
22803448Sdh155122 	connfp->connf_head = connp;
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	/* The fanout bucket this conn points to */
22833448Sdh155122 	connp->conn_g_fanout = connfp;
22840Sstevel@tonic-gate 
22853448Sdh155122 	mutex_exit(&connfp->connf_lock);
22860Sstevel@tonic-gate }
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate void
22890Sstevel@tonic-gate ipcl_globalhash_remove(conn_t *connp)
22900Sstevel@tonic-gate {
22913448Sdh155122 	struct connf_s	*connfp;
22923448Sdh155122 
22930Sstevel@tonic-gate 	/*
22940Sstevel@tonic-gate 	 * We were never inserted in the global multi list.
22950Sstevel@tonic-gate 	 * IPCL_NONE variety is never inserted in the global multilist
22960Sstevel@tonic-gate 	 * since it is presumed to not need any cleanup and is transient.
22970Sstevel@tonic-gate 	 */
22980Sstevel@tonic-gate 	if (connp->conn_g_fanout == NULL)
22990Sstevel@tonic-gate 		return;
23000Sstevel@tonic-gate 
23013448Sdh155122 	connfp = connp->conn_g_fanout;
23023448Sdh155122 	mutex_enter(&connfp->connf_lock);
23030Sstevel@tonic-gate 	if (connp->conn_g_prev != NULL)
23040Sstevel@tonic-gate 		connp->conn_g_prev->conn_g_next = connp->conn_g_next;
23050Sstevel@tonic-gate 	else
23063448Sdh155122 		connfp->connf_head = connp->conn_g_next;
23070Sstevel@tonic-gate 	if (connp->conn_g_next != NULL)
23080Sstevel@tonic-gate 		connp->conn_g_next->conn_g_prev = connp->conn_g_prev;
23093448Sdh155122 	mutex_exit(&connfp->connf_lock);
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	/* Better to stumble on a null pointer than to corrupt memory */
23120Sstevel@tonic-gate 	connp->conn_g_next = NULL;
23130Sstevel@tonic-gate 	connp->conn_g_prev = NULL;
23145240Snordmark 	connp->conn_g_fanout = NULL;
23150Sstevel@tonic-gate }
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate /*
23180Sstevel@tonic-gate  * Walk the list of all conn_t's in the system, calling the function provided
231911042SErik.Nordmark@Sun.COM  * With the specified argument for each.
23200Sstevel@tonic-gate  * Applies to both IPv4 and IPv6.
23210Sstevel@tonic-gate  *
232211042SErik.Nordmark@Sun.COM  * CONNs may hold pointers to ills (conn_dhcpinit_ill and
232311042SErik.Nordmark@Sun.COM  * conn_oper_pending_ill). To guard against stale pointers
23240Sstevel@tonic-gate  * ipcl_walk() is called to cleanup the conn_t's, typically when an interface is
23250Sstevel@tonic-gate  * unplumbed or removed. New conn_t's that are created while we are walking
23260Sstevel@tonic-gate  * may be missed by this walk, because they are not necessarily inserted
23270Sstevel@tonic-gate  * at the tail of the list. They are new conn_t's and thus don't have any
23280Sstevel@tonic-gate  * stale pointers. The CONN_CLOSING flag ensures that no new reference
23290Sstevel@tonic-gate  * is created to the struct that is going away.
23300Sstevel@tonic-gate  */
23310Sstevel@tonic-gate void
23323448Sdh155122 ipcl_walk(pfv_t func, void *arg, ip_stack_t *ipst)
23330Sstevel@tonic-gate {
23340Sstevel@tonic-gate 	int	i;
23350Sstevel@tonic-gate 	conn_t	*connp;
23360Sstevel@tonic-gate 	conn_t	*prev_connp;
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
23393448Sdh155122 		mutex_enter(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23400Sstevel@tonic-gate 		prev_connp = NULL;
23413448Sdh155122 		connp = ipst->ips_ipcl_globalhash_fanout[i].connf_head;
23420Sstevel@tonic-gate 		while (connp != NULL) {
23430Sstevel@tonic-gate 			mutex_enter(&connp->conn_lock);
23440Sstevel@tonic-gate 			if (connp->conn_state_flags &
23450Sstevel@tonic-gate 			    (CONN_CONDEMNED | CONN_INCIPIENT)) {
23460Sstevel@tonic-gate 				mutex_exit(&connp->conn_lock);
23470Sstevel@tonic-gate 				connp = connp->conn_g_next;
23480Sstevel@tonic-gate 				continue;
23490Sstevel@tonic-gate 			}
23500Sstevel@tonic-gate 			CONN_INC_REF_LOCKED(connp);
23510Sstevel@tonic-gate 			mutex_exit(&connp->conn_lock);
23523448Sdh155122 			mutex_exit(
23533448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23540Sstevel@tonic-gate 			(*func)(connp, arg);
23550Sstevel@tonic-gate 			if (prev_connp != NULL)
23560Sstevel@tonic-gate 				CONN_DEC_REF(prev_connp);
23573448Sdh155122 			mutex_enter(
23583448Sdh155122 			    &ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23590Sstevel@tonic-gate 			prev_connp = connp;
23600Sstevel@tonic-gate 			connp = connp->conn_g_next;
23610Sstevel@tonic-gate 		}
23623448Sdh155122 		mutex_exit(&ipst->ips_ipcl_globalhash_fanout[i].connf_lock);
23630Sstevel@tonic-gate 		if (prev_connp != NULL)
23640Sstevel@tonic-gate 			CONN_DEC_REF(prev_connp);
23650Sstevel@tonic-gate 	}
23660Sstevel@tonic-gate }
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate /*
23690Sstevel@tonic-gate  * Search for a peer TCP/IPv4 loopback conn by doing a reverse lookup on
23700Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
23710Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
23722323Sethindra  * (peer tcp in ESTABLISHED state).
23730Sstevel@tonic-gate  */
23740Sstevel@tonic-gate conn_t *
237511042SErik.Nordmark@Sun.COM ipcl_conn_tcp_lookup_reversed_ipv4(conn_t *connp, ipha_t *ipha, tcpha_t *tcpha,
23763448Sdh155122     ip_stack_t *ipst)
23770Sstevel@tonic-gate {
23780Sstevel@tonic-gate 	uint32_t ports;
23790Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
23800Sstevel@tonic-gate 	connf_t	*connfp;
23810Sstevel@tonic-gate 	conn_t	*tconnp;
23820Sstevel@tonic-gate 	boolean_t zone_chk;
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	/*
23850Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
23860Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
23870Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
23880Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.
23890Sstevel@tonic-gate 	 */
23900Sstevel@tonic-gate 	zone_chk = (ipha->ipha_src == htonl(INADDR_LOOPBACK) ||
23910Sstevel@tonic-gate 	    ipha->ipha_dst == htonl(INADDR_LOOPBACK));
23920Sstevel@tonic-gate 
239311042SErik.Nordmark@Sun.COM 	pports[0] = tcpha->tha_fport;
239411042SErik.Nordmark@Sun.COM 	pports[1] = tcpha->tha_lport;
23950Sstevel@tonic-gate 
23963448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
23973448Sdh155122 	    ports, ipst)];
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24000Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24010Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
24040Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
24052323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24060Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24090Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24100Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24110Sstevel@tonic-gate 			return (tconnp);
24120Sstevel@tonic-gate 		}
24130Sstevel@tonic-gate 	}
24140Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
24150Sstevel@tonic-gate 	return (NULL);
24160Sstevel@tonic-gate }
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate /*
24190Sstevel@tonic-gate  * Search for a peer TCP/IPv6 loopback conn by doing a reverse lookup on
24200Sstevel@tonic-gate  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
24210Sstevel@tonic-gate  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
24222323Sethindra  * (peer tcp in ESTABLISHED state).
24230Sstevel@tonic-gate  */
24240Sstevel@tonic-gate conn_t *
242511042SErik.Nordmark@Sun.COM ipcl_conn_tcp_lookup_reversed_ipv6(conn_t *connp, ip6_t *ip6h, tcpha_t *tcpha,
24263448Sdh155122     ip_stack_t *ipst)
24270Sstevel@tonic-gate {
24280Sstevel@tonic-gate 	uint32_t ports;
24290Sstevel@tonic-gate 	uint16_t *pports = (uint16_t *)&ports;
24300Sstevel@tonic-gate 	connf_t	*connfp;
24310Sstevel@tonic-gate 	conn_t	*tconnp;
24320Sstevel@tonic-gate 	boolean_t zone_chk;
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	/*
24350Sstevel@tonic-gate 	 * If either the source of destination address is loopback, then
24360Sstevel@tonic-gate 	 * both endpoints must be in the same Zone.  Otherwise, both of
24370Sstevel@tonic-gate 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
24380Sstevel@tonic-gate 	 * state) and the endpoints may reside in different Zones.  We
24390Sstevel@tonic-gate 	 * don't do Zone check for link local address(es) because the
24400Sstevel@tonic-gate 	 * current Zone implementation treats each link local address as
24410Sstevel@tonic-gate 	 * being unique per system node, i.e. they belong to global Zone.
24420Sstevel@tonic-gate 	 */
24430Sstevel@tonic-gate 	zone_chk = (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src) ||
24440Sstevel@tonic-gate 	    IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst));
24450Sstevel@tonic-gate 
244611042SErik.Nordmark@Sun.COM 	pports[0] = tcpha->tha_fport;
244711042SErik.Nordmark@Sun.COM 	pports[1] = tcpha->tha_lport;
24480Sstevel@tonic-gate 
24493448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
24503448Sdh155122 	    ports, ipst)];
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24530Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24540Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24550Sstevel@tonic-gate 
245611042SErik.Nordmark@Sun.COM 		/* We skip conn_bound_if check here as this is loopback tcp */
24570Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
24580Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
24592323Sethindra 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
24600Sstevel@tonic-gate 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 			ASSERT(tconnp != connp);
24630Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
24640Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
24650Sstevel@tonic-gate 			return (tconnp);
24660Sstevel@tonic-gate 		}
24670Sstevel@tonic-gate 	}
24680Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
24690Sstevel@tonic-gate 	return (NULL);
24700Sstevel@tonic-gate }
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate /*
24730Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
24740Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
24750Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
24760Sstevel@tonic-gate  */
24770Sstevel@tonic-gate conn_t *
247811042SErik.Nordmark@Sun.COM ipcl_tcp_lookup_reversed_ipv4(ipha_t *ipha, tcpha_t *tcpha, int min_state,
24793448Sdh155122     ip_stack_t *ipst)
24800Sstevel@tonic-gate {
24810Sstevel@tonic-gate 	uint32_t ports;
24820Sstevel@tonic-gate 	uint16_t *pports;
24830Sstevel@tonic-gate 	connf_t	*connfp;
24840Sstevel@tonic-gate 	conn_t	*tconnp;
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
248711042SErik.Nordmark@Sun.COM 	pports[0] = tcpha->tha_fport;
248811042SErik.Nordmark@Sun.COM 	pports[1] = tcpha->tha_lport;
24890Sstevel@tonic-gate 
24903448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst,
24914691Skcpoon 	    ports, ipst)];
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
24940Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
24950Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
24980Sstevel@tonic-gate 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
24990Sstevel@tonic-gate 		    tconnp->conn_tcp->tcp_state >= min_state) {
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25020Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25030Sstevel@tonic-gate 			return (tconnp);
25040Sstevel@tonic-gate 		}
25050Sstevel@tonic-gate 	}
25060Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25070Sstevel@tonic-gate 	return (NULL);
25080Sstevel@tonic-gate }
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate /*
25110Sstevel@tonic-gate  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
25120Sstevel@tonic-gate  * Returns with conn reference held. Caller must call CONN_DEC_REF.
25130Sstevel@tonic-gate  * Only checks for connected entries i.e. no INADDR_ANY checks.
25140Sstevel@tonic-gate  * Match on ifindex in addition to addresses.
25150Sstevel@tonic-gate  */
25160Sstevel@tonic-gate conn_t *
25170Sstevel@tonic-gate ipcl_tcp_lookup_reversed_ipv6(ip6_t *ip6h, tcpha_t *tcpha, int min_state,
25183448Sdh155122     uint_t ifindex, ip_stack_t *ipst)
25190Sstevel@tonic-gate {
25200Sstevel@tonic-gate 	tcp_t	*tcp;
25210Sstevel@tonic-gate 	uint32_t ports;
25220Sstevel@tonic-gate 	uint16_t *pports;
25230Sstevel@tonic-gate 	connf_t	*connfp;
25240Sstevel@tonic-gate 	conn_t	*tconnp;
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	pports = (uint16_t *)&ports;
25270Sstevel@tonic-gate 	pports[0] = tcpha->tha_fport;
25280Sstevel@tonic-gate 	pports[1] = tcpha->tha_lport;
25290Sstevel@tonic-gate 
25303448Sdh155122 	connfp = &ipst->ips_ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst,
25314691Skcpoon 	    ports, ipst)];
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	mutex_enter(&connfp->connf_lock);
25340Sstevel@tonic-gate 	for (tconnp = connfp->connf_head; tconnp != NULL;
25350Sstevel@tonic-gate 	    tconnp = tconnp->conn_next) {
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 		tcp = tconnp->conn_tcp;
25380Sstevel@tonic-gate 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
25390Sstevel@tonic-gate 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
25400Sstevel@tonic-gate 		    tcp->tcp_state >= min_state &&
254111042SErik.Nordmark@Sun.COM 		    (tconnp->conn_bound_if == 0 ||
254211042SErik.Nordmark@Sun.COM 		    tconnp->conn_bound_if == ifindex)) {
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 			CONN_INC_REF(tconnp);
25450Sstevel@tonic-gate 			mutex_exit(&connfp->connf_lock);
25460Sstevel@tonic-gate 			return (tconnp);
25470Sstevel@tonic-gate 		}
25480Sstevel@tonic-gate 	}
25490Sstevel@tonic-gate 	mutex_exit(&connfp->connf_lock);
25500Sstevel@tonic-gate 	return (NULL);
25510Sstevel@tonic-gate }
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate /*
25541676Sjpk  * Finds a TCP/IPv4 listening connection; called by tcp_disconnect to locate
25551676Sjpk  * a listener when changing state.
25560Sstevel@tonic-gate  */
25570Sstevel@tonic-gate conn_t *
25583448Sdh155122 ipcl_lookup_listener_v4(uint16_t lport, ipaddr_t laddr, zoneid_t zoneid,
25593448Sdh155122     ip_stack_t *ipst)
25600Sstevel@tonic-gate {
25610Sstevel@tonic-gate 	connf_t		*bind_connfp;
25620Sstevel@tonic-gate 	conn_t		*connp;
25630Sstevel@tonic-gate 	tcp_t		*tcp;
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	/*
25660Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
25670Sstevel@tonic-gate 	 * all zeros.
25680Sstevel@tonic-gate 	 */
25690Sstevel@tonic-gate 	if (laddr == 0)
25700Sstevel@tonic-gate 		return (NULL);
25710Sstevel@tonic-gate 
25721676Sjpk 	ASSERT(zoneid != ALL_ZONES);
25731676Sjpk 
25743448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
25750Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
25760Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
25770Sstevel@tonic-gate 	    connp = connp->conn_next) {
25780Sstevel@tonic-gate 		tcp = connp->conn_tcp;
25790Sstevel@tonic-gate 		if (IPCL_BIND_MATCH(connp, IPPROTO_TCP, laddr, lport) &&
25802263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
25810Sstevel@tonic-gate 		    (tcp->tcp_listener == NULL)) {
25820Sstevel@tonic-gate 			CONN_INC_REF(connp);
25830Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
25840Sstevel@tonic-gate 			return (connp);
25850Sstevel@tonic-gate 		}
25860Sstevel@tonic-gate 	}
25870Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
25880Sstevel@tonic-gate 	return (NULL);
25890Sstevel@tonic-gate }
25900Sstevel@tonic-gate 
25911676Sjpk /*
25921676Sjpk  * Finds a TCP/IPv6 listening connection; called by tcp_disconnect to locate
25931676Sjpk  * a listener when changing state.
25941676Sjpk  */
25950Sstevel@tonic-gate conn_t *
25960Sstevel@tonic-gate ipcl_lookup_listener_v6(uint16_t lport, in6_addr_t *laddr, uint_t ifindex,
25973448Sdh155122     zoneid_t zoneid, ip_stack_t *ipst)
25980Sstevel@tonic-gate {
25990Sstevel@tonic-gate 	connf_t		*bind_connfp;
26000Sstevel@tonic-gate 	conn_t		*connp = NULL;
26010Sstevel@tonic-gate 	tcp_t		*tcp;
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 	/*
26040Sstevel@tonic-gate 	 * Avoid false matches for packets sent to an IP destination of
26050Sstevel@tonic-gate 	 * all zeros.
26060Sstevel@tonic-gate 	 */
26070Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(laddr))
26080Sstevel@tonic-gate 		return (NULL);
26090Sstevel@tonic-gate 
26101676Sjpk 	ASSERT(zoneid != ALL_ZONES);
26110Sstevel@tonic-gate 
26123448Sdh155122 	bind_connfp = &ipst->ips_ipcl_bind_fanout[IPCL_BIND_HASH(lport, ipst)];
26130Sstevel@tonic-gate 	mutex_enter(&bind_connfp->connf_lock);
26140Sstevel@tonic-gate 	for (connp = bind_connfp->connf_head; connp != NULL;
26150Sstevel@tonic-gate 	    connp = connp->conn_next) {
26160Sstevel@tonic-gate 		tcp = connp->conn_tcp;
26170Sstevel@tonic-gate 		if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP, *laddr, lport) &&
26182263Ssommerfe 		    IPCL_ZONE_MATCH(connp, zoneid) &&
261911042SErik.Nordmark@Sun.COM 		    (connp->conn_bound_if == 0 ||
262011042SErik.Nordmark@Sun.COM 		    connp->conn_bound_if == ifindex) &&
26210Sstevel@tonic-gate 		    tcp->tcp_listener == NULL) {
26220Sstevel@tonic-gate 			CONN_INC_REF(connp);
26230Sstevel@tonic-gate 			mutex_exit(&bind_connfp->connf_lock);
26240Sstevel@tonic-gate 			return (connp);
26250Sstevel@tonic-gate 		}
26260Sstevel@tonic-gate 	}
26270Sstevel@tonic-gate 	mutex_exit(&bind_connfp->connf_lock);
26280Sstevel@tonic-gate 	return (NULL);
26290Sstevel@tonic-gate }
26300Sstevel@tonic-gate 
2631741Smasputra /*
2632741Smasputra  * ipcl_get_next_conn
2633741Smasputra  *	get the next entry in the conn global list
2634741Smasputra  *	and put a reference on the next_conn.
2635741Smasputra  *	decrement the reference on the current conn.
2636741Smasputra  *
2637741Smasputra  * This is an iterator based walker function that also provides for
2638741Smasputra  * some selection by the caller. It walks through the conn_hash bucket
2639741Smasputra  * searching for the next valid connp in the list, and selects connections
2640741Smasputra  * that are neither closed nor condemned. It also REFHOLDS the conn
2641741Smasputra  * thus ensuring that the conn exists when the caller uses the conn.
2642741Smasputra  */
2643741Smasputra conn_t *
2644741Smasputra ipcl_get_next_conn(connf_t *connfp, conn_t *connp, uint32_t conn_flags)
2645741Smasputra {
2646741Smasputra 	conn_t	*next_connp;
2647741Smasputra 
2648741Smasputra 	if (connfp == NULL)
2649741Smasputra 		return (NULL);
2650741Smasputra 
2651741Smasputra 	mutex_enter(&connfp->connf_lock);
2652741Smasputra 
2653741Smasputra 	next_connp = (connp == NULL) ?
2654741Smasputra 	    connfp->connf_head : connp->conn_g_next;
2655741Smasputra 
2656741Smasputra 	while (next_connp != NULL) {
2657741Smasputra 		mutex_enter(&next_connp->conn_lock);
2658741Smasputra 		if (!(next_connp->conn_flags & conn_flags) ||
2659741Smasputra 		    (next_connp->conn_state_flags &
2660741Smasputra 		    (CONN_CONDEMNED | CONN_INCIPIENT))) {
2661741Smasputra 			/*
2662741Smasputra 			 * This conn has been condemned or
2663741Smasputra 			 * is closing, or the flags don't match
2664741Smasputra 			 */
2665741Smasputra 			mutex_exit(&next_connp->conn_lock);
2666741Smasputra 			next_connp = next_connp->conn_g_next;
2667741Smasputra 			continue;
2668741Smasputra 		}
2669741Smasputra 		CONN_INC_REF_LOCKED(next_connp);
2670741Smasputra 		mutex_exit(&next_connp->conn_lock);
2671741Smasputra 		break;
2672741Smasputra 	}
2673741Smasputra 
2674741Smasputra 	mutex_exit(&connfp->connf_lock);
2675741Smasputra 
2676741Smasputra 	if (connp != NULL)
2677741Smasputra 		CONN_DEC_REF(connp);
2678741Smasputra 
2679741Smasputra 	return (next_connp);
2680741Smasputra }
2681741Smasputra 
26820Sstevel@tonic-gate #ifdef CONN_DEBUG
26830Sstevel@tonic-gate /*
26840Sstevel@tonic-gate  * Trace of the last NBUF refhold/refrele
26850Sstevel@tonic-gate  */
26860Sstevel@tonic-gate int
26870Sstevel@tonic-gate conn_trace_ref(conn_t *connp)
26880Sstevel@tonic-gate {
26890Sstevel@tonic-gate 	int	last;
26900Sstevel@tonic-gate 	conn_trace_t	*ctb;
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
26930Sstevel@tonic-gate 	last = connp->conn_trace_last;
26940Sstevel@tonic-gate 	last++;
26950Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
26960Sstevel@tonic-gate 		last = 0;
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
26995023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27000Sstevel@tonic-gate 	connp->conn_trace_last = last;
27010Sstevel@tonic-gate 	return (1);
27020Sstevel@tonic-gate }
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate int
27050Sstevel@tonic-gate conn_untrace_ref(conn_t *connp)
27060Sstevel@tonic-gate {
27070Sstevel@tonic-gate 	int	last;
27080Sstevel@tonic-gate 	conn_trace_t	*ctb;
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&connp->conn_lock));
27110Sstevel@tonic-gate 	last = connp->conn_trace_last;
27120Sstevel@tonic-gate 	last++;
27130Sstevel@tonic-gate 	if (last == CONN_TRACE_MAX)
27140Sstevel@tonic-gate 		last = 0;
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	ctb = &connp->conn_trace_buf[last];
27175023Scarlsonj 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, CONN_STACK_DEPTH);
27180Sstevel@tonic-gate 	connp->conn_trace_last = last;
27190Sstevel@tonic-gate 	return (1);
27200Sstevel@tonic-gate }
27210Sstevel@tonic-gate #endif
2722