xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_hash.c (revision 3539:20bbd2b1048c)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * 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 
220Sstevel@tonic-gate /*
233448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/socket.h>
300Sstevel@tonic-gate #include <sys/ddi.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
321676Sjpk #include <sys/tsol/tndb.h>
331676Sjpk #include <sys/tsol/tnet.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <netinet/in.h>
360Sstevel@tonic-gate #include <netinet/ip6.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <inet/common.h>
390Sstevel@tonic-gate #include <inet/ip.h>
400Sstevel@tonic-gate #include <inet/ip6.h>
410Sstevel@tonic-gate #include <inet/ipclassifier.h>
420Sstevel@tonic-gate #include <inet/ipsec_impl.h>
430Sstevel@tonic-gate #include <inet/ipp_common.h>
440Sstevel@tonic-gate #include <inet/sctp_ip.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include "sctp_impl.h"
470Sstevel@tonic-gate #include "sctp_addr.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /* Default association hash size.  The size must be a power of 2. */
500Sstevel@tonic-gate #define	SCTP_CONN_HASH_SIZE	8192
510Sstevel@tonic-gate 
523448Sdh155122 uint_t		sctp_conn_hash_size = SCTP_CONN_HASH_SIZE; /* /etc/system */
530Sstevel@tonic-gate 
54852Svi117747 /*
55852Svi117747  * Cluster networking hook for traversing current assoc list.
56852Svi117747  * This routine is used to extract the current list of live associations
57852Svi117747  * which must continue to to be dispatched to this node.
58852Svi117747  */
59852Svi117747 int cl_sctp_walk_list(int (*cl_callback)(cl_sctp_info_t *, void *), void *,
60852Svi117747     boolean_t);
613448Sdh155122 static int cl_sctp_walk_list_stack(int (*cl_callback)(cl_sctp_info_t *,
623448Sdh155122     void *), void *arg, boolean_t cansleep, sctp_stack_t *sctps);
63852Svi117747 
640Sstevel@tonic-gate void
653448Sdh155122 sctp_hash_init(sctp_stack_t *sctps)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	int i;
680Sstevel@tonic-gate 
693448Sdh155122 	/* Start with /etc/system value */
703448Sdh155122 	sctps->sctps_conn_hash_size = sctp_conn_hash_size;
713448Sdh155122 
723448Sdh155122 	if (sctps->sctps_conn_hash_size & (sctps->sctps_conn_hash_size - 1)) {
730Sstevel@tonic-gate 		/* Not a power of two. Round up to nearest power of two */
740Sstevel@tonic-gate 		for (i = 0; i < 31; i++) {
753448Sdh155122 			if (sctps->sctps_conn_hash_size < (1 << i))
760Sstevel@tonic-gate 				break;
770Sstevel@tonic-gate 		}
783448Sdh155122 		sctps->sctps_conn_hash_size = 1 << i;
790Sstevel@tonic-gate 	}
803448Sdh155122 	if (sctps->sctps_conn_hash_size < SCTP_CONN_HASH_SIZE) {
813448Sdh155122 		sctps->sctps_conn_hash_size = SCTP_CONN_HASH_SIZE;
820Sstevel@tonic-gate 		cmn_err(CE_CONT, "using sctp_conn_hash_size = %u\n",
833448Sdh155122 		    sctps->sctps_conn_hash_size);
840Sstevel@tonic-gate 	}
853448Sdh155122 	sctps->sctps_conn_fanout =
863448Sdh155122 		(sctp_tf_t *)kmem_zalloc(sctps->sctps_conn_hash_size *
870Sstevel@tonic-gate 		    sizeof (sctp_tf_t),	KM_SLEEP);
883448Sdh155122 	for (i = 0; i < sctps->sctps_conn_hash_size; i++) {
893448Sdh155122 		mutex_init(&sctps->sctps_conn_fanout[i].tf_lock, NULL,
900Sstevel@tonic-gate 			    MUTEX_DEFAULT, NULL);
910Sstevel@tonic-gate 	}
923448Sdh155122 	sctps->sctps_listen_fanout = kmem_zalloc(SCTP_LISTEN_FANOUT_SIZE *
933448Sdh155122 	    sizeof (sctp_tf_t),	KM_SLEEP);
943448Sdh155122 	for (i = 0; i < SCTP_LISTEN_FANOUT_SIZE; i++) {
953448Sdh155122 		mutex_init(&sctps->sctps_listen_fanout[i].tf_lock, NULL,
960Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
970Sstevel@tonic-gate 	}
983448Sdh155122 	sctps->sctps_bind_fanout = kmem_zalloc(SCTP_BIND_FANOUT_SIZE *
993448Sdh155122 	    sizeof (sctp_tf_t),	KM_SLEEP);
1003448Sdh155122 	for (i = 0; i < SCTP_BIND_FANOUT_SIZE; i++) {
1013448Sdh155122 		mutex_init(&sctps->sctps_bind_fanout[i].tf_lock, NULL,
1020Sstevel@tonic-gate 		    MUTEX_DEFAULT, NULL);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate void
1073448Sdh155122 sctp_hash_destroy(sctp_stack_t *sctps)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	int i;
1100Sstevel@tonic-gate 
1113448Sdh155122 	for (i = 0; i < sctps->sctps_conn_hash_size; i++) {
1123448Sdh155122 		mutex_destroy(&sctps->sctps_conn_fanout[i].tf_lock);
1133448Sdh155122 	}
1143448Sdh155122 	kmem_free(sctps->sctps_conn_fanout, sctps->sctps_conn_hash_size *
1153448Sdh155122 	    sizeof (sctp_tf_t));
1163448Sdh155122 	sctps->sctps_conn_fanout = NULL;
1173448Sdh155122 
1183448Sdh155122 	for (i = 0; i < SCTP_LISTEN_FANOUT_SIZE; i++) {
1193448Sdh155122 		mutex_destroy(&sctps->sctps_listen_fanout[i].tf_lock);
1200Sstevel@tonic-gate 	}
1213448Sdh155122 	kmem_free(sctps->sctps_listen_fanout, SCTP_LISTEN_FANOUT_SIZE *
1223448Sdh155122 	    sizeof (sctp_tf_t));
1233448Sdh155122 	sctps->sctps_listen_fanout = NULL;
1243448Sdh155122 
1253448Sdh155122 	for (i = 0; i < SCTP_BIND_FANOUT_SIZE; i++) {
1263448Sdh155122 		mutex_destroy(&sctps->sctps_bind_fanout[i].tf_lock);
1270Sstevel@tonic-gate 	}
1283448Sdh155122 	kmem_free(sctps->sctps_bind_fanout, SCTP_BIND_FANOUT_SIZE *
1293448Sdh155122 	    sizeof (sctp_tf_t));
1303448Sdh155122 	sctps->sctps_bind_fanout = NULL;
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1331932Svi117747 /*
1341932Svi117747  * Walk the SCTP global list and refrele the ire for this ipif
1351932Svi117747  * This is called when an address goes down, so that we release any reference
1361932Svi117747  * to the ire associated with this address. Additionally, for any SCTP if
1371932Svi117747  * this was the only/last address in its source list, we don't kill the
1381932Svi117747  * assoc., if there is no address added subsequently, or if this does not
1391932Svi117747  * come up, then the assoc. will die a natural death (i.e. timeout).
1401932Svi117747  */
1410Sstevel@tonic-gate void
1420Sstevel@tonic-gate sctp_ire_cache_flush(ipif_t *ipif)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	sctp_t			*sctp;
1450Sstevel@tonic-gate 	sctp_t			*sctp_prev = NULL;
1460Sstevel@tonic-gate 	sctp_faddr_t		*fp;
1470Sstevel@tonic-gate 	conn_t			*connp;
1480Sstevel@tonic-gate 	ire_t			*ire;
1493448Sdh155122 	sctp_stack_t		*sctps = ipif->ipif_ill->ill_ipst->
1503448Sdh155122 	    ips_netstack->netstack_sctp;
1510Sstevel@tonic-gate 
1523448Sdh155122 	sctp = sctps->sctps_gsctp;
1533448Sdh155122 	mutex_enter(&sctps->sctps_g_lock);
1540Sstevel@tonic-gate 	while (sctp != NULL) {
1550Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_reflock);
1560Sstevel@tonic-gate 		if (sctp->sctp_condemned) {
1570Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_reflock);
1583448Sdh155122 			sctp = list_next(&sctps->sctps_g_list, sctp);
1590Sstevel@tonic-gate 			continue;
1600Sstevel@tonic-gate 		}
1610Sstevel@tonic-gate 		sctp->sctp_refcnt++;
1620Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_reflock);
1633448Sdh155122 		mutex_exit(&sctps->sctps_g_lock);
1640Sstevel@tonic-gate 		if (sctp_prev != NULL)
1650Sstevel@tonic-gate 			SCTP_REFRELE(sctp_prev);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		RUN_SCTP(sctp);
1680Sstevel@tonic-gate 		connp = sctp->sctp_connp;
1690Sstevel@tonic-gate 		mutex_enter(&connp->conn_lock);
1700Sstevel@tonic-gate 		ire = connp->conn_ire_cache;
1711932Svi117747 		if (ire != NULL && ire->ire_ipif == ipif) {
1720Sstevel@tonic-gate 			connp->conn_ire_cache = NULL;
1730Sstevel@tonic-gate 			mutex_exit(&connp->conn_lock);
1740Sstevel@tonic-gate 			IRE_REFRELE_NOTR(ire);
1750Sstevel@tonic-gate 		} else {
1760Sstevel@tonic-gate 			mutex_exit(&connp->conn_lock);
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 		/* check for ires cached in faddr */
1791932Svi117747 		for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1801932Svi117747 			/*
1811932Svi117747 			 * If this ipif is being used as the source address
1821932Svi117747 			 * we need to update it as well, else we will end
1831932Svi117747 			 * up using the dead source address.
1841932Svi117747 			 */
1850Sstevel@tonic-gate 			ire = fp->ire;
1861932Svi117747 			if (ire != NULL && ire->ire_ipif == ipif) {
1870Sstevel@tonic-gate 				fp->ire = NULL;
1880Sstevel@tonic-gate 				IRE_REFRELE_NOTR(ire);
1890Sstevel@tonic-gate 			}
1901932Svi117747 			/*
1911932Svi117747 			 * This may result in setting the fp as unreachable,
1921932Svi117747 			 * i.e. if all the source addresses are down. In
1931932Svi117747 			 * that case the assoc. would timeout.
1941932Svi117747 			 */
1951932Svi117747 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
1961932Svi117747 			    &fp->saddr)) {
1971932Svi117747 				sctp_set_saddr(sctp, fp);
1981932Svi117747 				if (fp == sctp->sctp_current &&
1991932Svi117747 				    fp->state != SCTP_FADDRS_UNREACH) {
2001932Svi117747 					sctp_set_faddr_current(sctp, fp);
2011932Svi117747 				}
2021932Svi117747 			}
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 		WAKE_SCTP(sctp);
2050Sstevel@tonic-gate 		sctp_prev = sctp;
2063448Sdh155122 		mutex_enter(&sctps->sctps_g_lock);
2073448Sdh155122 		sctp = list_next(&sctps->sctps_g_list, sctp);
2080Sstevel@tonic-gate 	}
2093448Sdh155122 	mutex_exit(&sctps->sctps_g_lock);
2100Sstevel@tonic-gate 	if (sctp_prev != NULL)
2110Sstevel@tonic-gate 		SCTP_REFRELE(sctp_prev);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
214852Svi117747 /*
215852Svi117747  * Exported routine for extracting active SCTP associations.
216852Svi117747  * Like TCP, we terminate the walk if the callback returns non-zero.
2173448Sdh155122  *
2183448Sdh155122  * Need to walk all sctp_stack_t instances since this clustering
2193448Sdh155122  * interface is assumed global for all instances
220852Svi117747  */
221852Svi117747 int
2223448Sdh155122 cl_sctp_walk_list(int (*cl_callback)(cl_sctp_info_t *, void *),
2233448Sdh155122     void *arg, boolean_t cansleep)
2243448Sdh155122 {
2253448Sdh155122 	netstack_handle_t nh;
2263448Sdh155122 	netstack_t *ns;
2273448Sdh155122 	int ret = 0;
2283448Sdh155122 
2293448Sdh155122 	netstack_next_init(&nh);
2303448Sdh155122 	while ((ns = netstack_next(&nh)) != NULL) {
2313448Sdh155122 		ret = cl_sctp_walk_list_stack(cl_callback, arg, cansleep,
2323448Sdh155122 		    ns->netstack_sctp);
2333448Sdh155122 		netstack_rele(ns);
2343448Sdh155122 	}
2353448Sdh155122 	netstack_next_fini(&nh);
2363448Sdh155122 	return (ret);
2373448Sdh155122 }
2383448Sdh155122 
2393448Sdh155122 static int
2403448Sdh155122 cl_sctp_walk_list_stack(int (*cl_callback)(cl_sctp_info_t *, void *),
2413448Sdh155122     void *arg, boolean_t cansleep, sctp_stack_t *sctps)
242852Svi117747 {
243852Svi117747 	sctp_t		*sctp;
244852Svi117747 	sctp_t		*sctp_prev;
245852Svi117747 	cl_sctp_info_t	cl_sctpi;
246852Svi117747 	uchar_t		*slist;
247852Svi117747 	uchar_t		*flist;
248852Svi117747 
2493448Sdh155122 	sctp = sctps->sctps_gsctp;
250852Svi117747 	sctp_prev = NULL;
2513448Sdh155122 	mutex_enter(&sctps->sctps_g_lock);
252852Svi117747 	while (sctp != NULL) {
253852Svi117747 		size_t	ssize;
254852Svi117747 		size_t	fsize;
255852Svi117747 
256852Svi117747 		mutex_enter(&sctp->sctp_reflock);
257852Svi117747 		if (sctp->sctp_condemned || sctp->sctp_state <= SCTPS_LISTEN) {
258852Svi117747 			mutex_exit(&sctp->sctp_reflock);
2593448Sdh155122 			sctp = list_next(&sctps->sctps_g_list, sctp);
260852Svi117747 			continue;
261852Svi117747 		}
262852Svi117747 		sctp->sctp_refcnt++;
263852Svi117747 		mutex_exit(&sctp->sctp_reflock);
2643448Sdh155122 		mutex_exit(&sctps->sctps_g_lock);
265852Svi117747 		if (sctp_prev != NULL)
266852Svi117747 			SCTP_REFRELE(sctp_prev);
267852Svi117747 		RUN_SCTP(sctp);
268852Svi117747 		ssize = sizeof (in6_addr_t) * sctp->sctp_nsaddrs;
269852Svi117747 		fsize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
270852Svi117747 
271852Svi117747 		slist = kmem_alloc(ssize, cansleep ? KM_SLEEP : KM_NOSLEEP);
272852Svi117747 		flist = kmem_alloc(fsize, cansleep ? KM_SLEEP : KM_NOSLEEP);
273852Svi117747 		if (slist == NULL || flist == NULL) {
274852Svi117747 			WAKE_SCTP(sctp);
275852Svi117747 			if (slist != NULL)
276852Svi117747 				kmem_free(slist, ssize);
277852Svi117747 			if (flist != NULL)
278852Svi117747 				kmem_free(flist, fsize);
279852Svi117747 			SCTP_REFRELE(sctp);
280852Svi117747 			return (1);
281852Svi117747 		}
282852Svi117747 		cl_sctpi.cl_sctpi_version = CL_SCTPI_V1;
283852Svi117747 		sctp_get_saddr_list(sctp, slist, ssize);
284852Svi117747 		sctp_get_faddr_list(sctp, flist, fsize);
285852Svi117747 		cl_sctpi.cl_sctpi_nladdr = sctp->sctp_nsaddrs;
286852Svi117747 		cl_sctpi.cl_sctpi_nfaddr = sctp->sctp_nfaddrs;
287852Svi117747 		cl_sctpi.cl_sctpi_family = sctp->sctp_family;
288852Svi117747 		cl_sctpi.cl_sctpi_ipversion = sctp->sctp_ipversion;
289852Svi117747 		cl_sctpi.cl_sctpi_state = sctp->sctp_state;
290852Svi117747 		cl_sctpi.cl_sctpi_lport = sctp->sctp_lport;
291852Svi117747 		cl_sctpi.cl_sctpi_fport = sctp->sctp_fport;
292852Svi117747 		cl_sctpi.cl_sctpi_handle = (cl_sctp_handle_t)sctp;
293852Svi117747 		WAKE_SCTP(sctp);
294852Svi117747 		cl_sctpi.cl_sctpi_laddrp = slist;
295852Svi117747 		cl_sctpi.cl_sctpi_faddrp = flist;
296852Svi117747 		if ((*cl_callback)(&cl_sctpi, arg) != 0) {
297852Svi117747 			kmem_free(slist, ssize);
298852Svi117747 			kmem_free(flist, fsize);
299852Svi117747 			SCTP_REFRELE(sctp);
300852Svi117747 			return (1);
301852Svi117747 		}
302852Svi117747 		/* list will be freed by cl_callback */
303852Svi117747 		sctp_prev = sctp;
3043448Sdh155122 		mutex_enter(&sctps->sctps_g_lock);
3053448Sdh155122 		sctp = list_next(&sctps->sctps_g_list, sctp);
306852Svi117747 	}
3073448Sdh155122 	mutex_exit(&sctps->sctps_g_lock);
308852Svi117747 	if (sctp_prev != NULL)
309852Svi117747 		SCTP_REFRELE(sctp_prev);
310852Svi117747 	return (0);
311852Svi117747 }
312852Svi117747 
3130Sstevel@tonic-gate sctp_t *
3140Sstevel@tonic-gate sctp_conn_match(in6_addr_t *faddr, in6_addr_t *laddr, uint32_t ports,
3153510Svi117747     zoneid_t zoneid, sctp_stack_t *sctps)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	sctp_tf_t		*tf;
3180Sstevel@tonic-gate 	sctp_t			*sctp;
3190Sstevel@tonic-gate 	sctp_faddr_t		*fp;
3200Sstevel@tonic-gate 
3213448Sdh155122 	tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]);
3220Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
3252263Ssommerfe 		if (ports != sctp->sctp_ports ||
3262263Ssommerfe 		    !IPCL_ZONE_MATCH(sctp->sctp_connp, zoneid)) {
3270Sstevel@tonic-gate 			continue;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 		/* check for faddr match */
3310Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
3320Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(faddr, &fp->faddr)) {
3330Sstevel@tonic-gate 				break;
3340Sstevel@tonic-gate 			}
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 
3373510Svi117747 		/* no faddr match; keep looking */
3383510Svi117747 		if (fp == NULL)
3390Sstevel@tonic-gate 			continue;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		/* check for laddr match */
3423510Svi117747 		if (sctp_saddr_lookup(sctp, laddr, 0) != NULL) {
3433510Svi117747 			SCTP_REFHOLD(sctp);
3443510Svi117747 			goto done;
3453510Svi117747 		}
3460Sstevel@tonic-gate 		/* no match; continue to the next in the chain */
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate done:
3500Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
3510Sstevel@tonic-gate 	return (sctp);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate static sctp_t *
3553510Svi117747 listen_match(in6_addr_t *laddr, uint32_t ports, zoneid_t zoneid,
3563510Svi117747     sctp_stack_t *sctps)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	sctp_t			*sctp;
3590Sstevel@tonic-gate 	sctp_tf_t		*tf;
3600Sstevel@tonic-gate 	uint16_t		lport;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	lport = ((uint16_t *)&ports)[1];
3630Sstevel@tonic-gate 
3643448Sdh155122 	tf = &(sctps->sctps_listen_fanout[SCTP_LISTEN_HASH(ntohs(lport))]);
3650Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_listen_hash_next) {
3682263Ssommerfe 		if (lport != sctp->sctp_lport ||
3692263Ssommerfe 		    !IPCL_ZONE_MATCH(sctp->sctp_connp, zoneid)) {
3700Sstevel@tonic-gate 			continue;
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 
3733510Svi117747 		if (sctp_saddr_lookup(sctp, laddr, 0) != NULL) {
3743510Svi117747 			SCTP_REFHOLD(sctp);
3753510Svi117747 			goto done;
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 		/* no match; continue to the next in the chain */
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate done:
3810Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
3820Sstevel@tonic-gate 	return (sctp);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate 
3851676Sjpk /* called by ipsec_sctp_pol */
3860Sstevel@tonic-gate conn_t *
3870Sstevel@tonic-gate sctp_find_conn(in6_addr_t *src, in6_addr_t *dst, uint32_t ports,
3883510Svi117747     zoneid_t zoneid, sctp_stack_t *sctps)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	sctp_t *sctp;
3910Sstevel@tonic-gate 
3923510Svi117747 	if ((sctp = sctp_conn_match(src, dst, ports, zoneid, sctps)) == NULL) {
3930Sstevel@tonic-gate 		/* Not in conn fanout; check listen fanout */
3943510Svi117747 		if ((sctp = listen_match(dst, ports, zoneid, sctps)) == NULL)
3950Sstevel@tonic-gate 			return (NULL);
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	return (sctp->sctp_connp);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4001676Sjpk conn_t *
4011676Sjpk sctp_fanout(in6_addr_t *src, in6_addr_t *dst, uint32_t ports,
4023510Svi117747     zoneid_t zoneid, mblk_t *mp, sctp_stack_t *sctps)
4033448Sdh155122 
4041676Sjpk {
4051676Sjpk 	sctp_t *sctp;
4062776Skp158701 	boolean_t shared_addr;
4071676Sjpk 
4083510Svi117747 	if ((sctp = sctp_conn_match(src, dst, ports, zoneid, sctps)) == NULL) {
4092776Skp158701 		shared_addr = (zoneid == ALL_ZONES);
4102776Skp158701 		if (shared_addr) {
4113448Sdh155122 			/*
4123448Sdh155122 			 * No need to handle exclusive-stack zones since
4133448Sdh155122 			 * ALL_ZONES only applies to the shared stack.
4143448Sdh155122 			 */
4151676Sjpk 			zoneid = tsol_mlp_findzone(IPPROTO_SCTP,
4161676Sjpk 			    htons(ntohl(ports) & 0xFFFF));
4171676Sjpk 			/*
4181676Sjpk 			 * If no shared MLP is found, tsol_mlp_findzone returns
4191676Sjpk 			 * ALL_ZONES.  In that case, we assume it's SLP, and
4201676Sjpk 			 * search for the zone based on the packet label.
4211676Sjpk 			 * That will also return ALL_ZONES on failure.
4221676Sjpk 			 */
4231676Sjpk 			if (zoneid == ALL_ZONES)
4241676Sjpk 				zoneid = tsol_packet_to_zoneid(mp);
4251676Sjpk 			if (zoneid == ALL_ZONES)
4261676Sjpk 				return (NULL);
4271676Sjpk 		}
4281676Sjpk 		/* Not in conn fanout; check listen fanout */
4293510Svi117747 		if ((sctp = listen_match(dst, ports, zoneid, sctps)) == NULL)
4301676Sjpk 			return (NULL);
4312776Skp158701 		/*
4322776Skp158701 		 * On systems running trusted extensions, check if dst
4332776Skp158701 		 * should accept the packet. "IPV6_VERSION" indicates
4342776Skp158701 		 * that dst is in 16 byte AF_INET6 format. IPv4-mapped
4352776Skp158701 		 * IPv6 addresses are supported.
4362776Skp158701 		 */
4372776Skp158701 		if (is_system_labeled() &&
4382776Skp158701 		    !tsol_receive_local(mp, dst, IPV6_VERSION,
4392776Skp158701 		    shared_addr, sctp->sctp_connp)) {
4402776Skp158701 			DTRACE_PROBE3(
4412776Skp158701 			    tx__ip__log__info__classify__sctp,
4422776Skp158701 			    char *,
4432776Skp158701 			    "connp(1) could not receive mp(2)",
4442776Skp158701 			    conn_t *, sctp->sctp_connp, mblk_t *, mp);
4452776Skp158701 			SCTP_REFRELE(sctp);
4462776Skp158701 			return (NULL);
4472776Skp158701 		}
4481676Sjpk 	}
4491676Sjpk 	return (sctp->sctp_connp);
4501676Sjpk }
4511676Sjpk 
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate  * Fanout for SCTP packets
4540Sstevel@tonic-gate  * The caller puts <fport, lport> in the ports parameter.
4550Sstevel@tonic-gate  */
4560Sstevel@tonic-gate /* ARGSUSED */
4570Sstevel@tonic-gate void
4580Sstevel@tonic-gate ip_fanout_sctp(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha,
4590Sstevel@tonic-gate     uint32_t ports, uint_t flags, boolean_t mctl_present, boolean_t ip_policy,
4603510Svi117747     zoneid_t zoneid)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	sctp_t *sctp;
4630Sstevel@tonic-gate 	boolean_t isv4;
4640Sstevel@tonic-gate 	conn_t *connp;
4650Sstevel@tonic-gate 	mblk_t *first_mp;
4660Sstevel@tonic-gate 	ip6_t *ip6h;
4670Sstevel@tonic-gate 	in6_addr_t map_src, map_dst;
4680Sstevel@tonic-gate 	in6_addr_t *src, *dst;
4693448Sdh155122 	ip_stack_t	*ipst;
4703448Sdh155122 	ipsec_stack_t	*ipss;
4713448Sdh155122 	sctp_stack_t	*sctps;
4723448Sdh155122 
4733448Sdh155122 	ASSERT(recv_ill != NULL);
4743448Sdh155122 	ipst = recv_ill->ill_ipst;
4753448Sdh155122 	sctps = ipst->ips_netstack->netstack_sctp;
4763448Sdh155122 	ipss = ipst->ips_netstack->netstack_ipsec;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	first_mp = mp;
4790Sstevel@tonic-gate 	if (mctl_present) {
4800Sstevel@tonic-gate 		mp = first_mp->b_cont;
4810Sstevel@tonic-gate 		ASSERT(mp != NULL);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/* Assume IP provides aligned packets - otherwise toss */
4850Sstevel@tonic-gate 	if (!OK_32PTR(mp->b_rptr)) {
4863284Sapersson 		BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
4870Sstevel@tonic-gate 		freemsg(first_mp);
4880Sstevel@tonic-gate 		return;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) {
4920Sstevel@tonic-gate 		ip6h = (ip6_t *)ipha;
4930Sstevel@tonic-gate 		src = &ip6h->ip6_src;
4940Sstevel@tonic-gate 		dst = &ip6h->ip6_dst;
4950Sstevel@tonic-gate 		isv4 = B_FALSE;
4960Sstevel@tonic-gate 	} else {
4970Sstevel@tonic-gate 		ip6h = NULL;
4980Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src);
4990Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst);
5000Sstevel@tonic-gate 		src = &map_src;
5010Sstevel@tonic-gate 		dst = &map_dst;
5020Sstevel@tonic-gate 		isv4 = B_TRUE;
5030Sstevel@tonic-gate 	}
504*3539Snordmark 	connp = sctp_fanout(src, dst, ports, zoneid, mp, sctps);
5053448Sdh155122 	if (connp == NULL) {
5062429Skcpoon 		ip_fanout_sctp_raw(first_mp, recv_ill, ipha, isv4,
5073510Svi117747 		    ports, mctl_present, flags, ip_policy, zoneid);
5080Sstevel@tonic-gate 		return;
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 	sctp = CONN2SCTP(connp);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/* Found a client; up it goes */
5133284Sapersson 	BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	/*
5160Sstevel@tonic-gate 	 * We check some fields in conn_t without holding a lock.
5170Sstevel@tonic-gate 	 * This should be fine.
5180Sstevel@tonic-gate 	 */
5193448Sdh155122 	if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) {
5200Sstevel@tonic-gate 		first_mp = ipsec_check_inbound_policy(first_mp, connp,
5210Sstevel@tonic-gate 		    ipha, NULL, mctl_present);
5220Sstevel@tonic-gate 		if (first_mp == NULL) {
5230Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
5240Sstevel@tonic-gate 			return;
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/* Initiate IPPF processing for fastpath */
5293448Sdh155122 	if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) {
5300Sstevel@tonic-gate 		ip_process(IPP_LOCAL_IN, &mp,
5310Sstevel@tonic-gate 		    recv_ill->ill_phyint->phyint_ifindex);
5320Sstevel@tonic-gate 		if (mp == NULL) {
5330Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
5340Sstevel@tonic-gate 			if (mctl_present)
5350Sstevel@tonic-gate 				freeb(first_mp);
5360Sstevel@tonic-gate 			return;
5370Sstevel@tonic-gate 		} else if (mctl_present) {
5380Sstevel@tonic-gate 			/*
5390Sstevel@tonic-gate 			 * ip_process might return a new mp.
5400Sstevel@tonic-gate 			 */
5410Sstevel@tonic-gate 			ASSERT(first_mp != mp);
5420Sstevel@tonic-gate 			first_mp->b_cont = mp;
5430Sstevel@tonic-gate 		} else {
5440Sstevel@tonic-gate 			first_mp = mp;
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	if (connp->conn_recvif || connp->conn_recvslla ||
5493318Srshoaib 	    connp->conn_ip_recvpktinfo) {
5500Sstevel@tonic-gate 		int in_flags = 0;
5510Sstevel@tonic-gate 
5523318Srshoaib 		if (connp->conn_recvif || connp->conn_ip_recvpktinfo) {
5530Sstevel@tonic-gate 			in_flags = IPF_RECVIF;
5540Sstevel@tonic-gate 		}
5550Sstevel@tonic-gate 		if (connp->conn_recvslla) {
5560Sstevel@tonic-gate 			in_flags |= IPF_RECVSLLA;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		if (isv4) {
5593318Srshoaib 			mp = ip_add_info(mp, recv_ill, in_flags,
5603448Sdh155122 			    IPCL_ZONEID(connp), ipst);
5610Sstevel@tonic-gate 		} else {
5620Sstevel@tonic-gate 			mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst);
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 		if (mp == NULL) {
5650Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
5660Sstevel@tonic-gate 			if (mctl_present)
5670Sstevel@tonic-gate 				freeb(first_mp);
5680Sstevel@tonic-gate 			return;
5690Sstevel@tonic-gate 		} else if (mctl_present) {
5700Sstevel@tonic-gate 			/*
5710Sstevel@tonic-gate 			 * ip_add_info might return a new mp.
5720Sstevel@tonic-gate 			 */
5730Sstevel@tonic-gate 			ASSERT(first_mp != mp);
5740Sstevel@tonic-gate 			first_mp->b_cont = mp;
5750Sstevel@tonic-gate 		} else {
5760Sstevel@tonic-gate 			first_mp = mp;
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_lock);
5810Sstevel@tonic-gate 	if (sctp->sctp_running) {
5820Sstevel@tonic-gate 		if (mctl_present)
5830Sstevel@tonic-gate 			mp->b_prev = first_mp;
5840Sstevel@tonic-gate 		if (!sctp_add_recvq(sctp, mp, B_FALSE)) {
5853284Sapersson 			BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
5860Sstevel@tonic-gate 			freemsg(first_mp);
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_lock);
5890Sstevel@tonic-gate 	} else {
5900Sstevel@tonic-gate 		sctp->sctp_running = B_TRUE;
5910Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_lock);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
5940Sstevel@tonic-gate 		if (sctp->sctp_recvq != NULL) {
5950Sstevel@tonic-gate 			if (mctl_present)
5960Sstevel@tonic-gate 				mp->b_prev = first_mp;
5970Sstevel@tonic-gate 			if (!sctp_add_recvq(sctp, mp, B_TRUE)) {
5983284Sapersson 				BUMP_MIB(recv_ill->ill_ip_mib,
5993284Sapersson 				    ipIfStatsInDiscards);
6000Sstevel@tonic-gate 				freemsg(first_mp);
6010Sstevel@tonic-gate 			}
6020Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_recvq_lock);
6030Sstevel@tonic-gate 			WAKE_SCTP(sctp);
6040Sstevel@tonic-gate 		} else {
6050Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_recvq_lock);
6060Sstevel@tonic-gate 			sctp_input_data(sctp, mp, (mctl_present ? first_mp :
6070Sstevel@tonic-gate 			    NULL));
6080Sstevel@tonic-gate 			WAKE_SCTP(sctp);
6090Sstevel@tonic-gate 			sctp_process_sendq(sctp);
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate void
6160Sstevel@tonic-gate sctp_conn_hash_remove(sctp_t *sctp)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate 	sctp_tf_t *tf = sctp->sctp_conn_tfp;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	if (!tf) {
6210Sstevel@tonic-gate 		return;
6220Sstevel@tonic-gate 	}
623852Svi117747 	/*
624852Svi117747 	 * On a clustered note send this notification to the clustering
625852Svi117747 	 * subsystem.
626852Svi117747 	 */
627852Svi117747 	if (cl_sctp_disconnect != NULL) {
628852Svi117747 		(*cl_sctp_disconnect)(sctp->sctp_family,
629852Svi117747 		    (cl_sctp_handle_t)sctp);
630852Svi117747 	}
631852Svi117747 
6320Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
6330Sstevel@tonic-gate 	ASSERT(tf->tf_sctp);
6340Sstevel@tonic-gate 	if (tf->tf_sctp == sctp) {
6350Sstevel@tonic-gate 		tf->tf_sctp = sctp->sctp_conn_hash_next;
6360Sstevel@tonic-gate 		if (sctp->sctp_conn_hash_next) {
6370Sstevel@tonic-gate 			ASSERT(tf->tf_sctp->sctp_conn_hash_prev == sctp);
6380Sstevel@tonic-gate 			tf->tf_sctp->sctp_conn_hash_prev = NULL;
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 	} else {
6410Sstevel@tonic-gate 		ASSERT(sctp->sctp_conn_hash_prev);
6420Sstevel@tonic-gate 		ASSERT(sctp->sctp_conn_hash_prev->sctp_conn_hash_next == sctp);
6430Sstevel@tonic-gate 		sctp->sctp_conn_hash_prev->sctp_conn_hash_next =
6440Sstevel@tonic-gate 		    sctp->sctp_conn_hash_next;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 		if (sctp->sctp_conn_hash_next) {
6470Sstevel@tonic-gate 			ASSERT(sctp->sctp_conn_hash_next->sctp_conn_hash_prev
6480Sstevel@tonic-gate 			    == sctp);
6490Sstevel@tonic-gate 			sctp->sctp_conn_hash_next->sctp_conn_hash_prev =
6500Sstevel@tonic-gate 			    sctp->sctp_conn_hash_prev;
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 	sctp->sctp_conn_hash_next = NULL;
6540Sstevel@tonic-gate 	sctp->sctp_conn_hash_prev = NULL;
6550Sstevel@tonic-gate 	sctp->sctp_conn_tfp = NULL;
6560Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate void
6600Sstevel@tonic-gate sctp_conn_hash_insert(sctp_tf_t *tf, sctp_t *sctp, int caller_holds_lock)
6610Sstevel@tonic-gate {
6620Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp) {
6630Sstevel@tonic-gate 		sctp_conn_hash_remove(sctp);
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (!caller_holds_lock) {
6670Sstevel@tonic-gate 		mutex_enter(&tf->tf_lock);
6680Sstevel@tonic-gate 	} else {
6690Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&tf->tf_lock));
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	sctp->sctp_conn_hash_next = tf->tf_sctp;
6730Sstevel@tonic-gate 	if (tf->tf_sctp) {
6740Sstevel@tonic-gate 		tf->tf_sctp->sctp_conn_hash_prev = sctp;
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 	sctp->sctp_conn_hash_prev = NULL;
6770Sstevel@tonic-gate 	tf->tf_sctp = sctp;
6780Sstevel@tonic-gate 	sctp->sctp_conn_tfp = tf;
6790Sstevel@tonic-gate 	if (!caller_holds_lock) {
6800Sstevel@tonic-gate 		mutex_exit(&tf->tf_lock);
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate void
6850Sstevel@tonic-gate sctp_listen_hash_remove(sctp_t *sctp)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	sctp_tf_t *tf = sctp->sctp_listen_tfp;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if (!tf) {
6900Sstevel@tonic-gate 		return;
6910Sstevel@tonic-gate 	}
692852Svi117747 	/*
693852Svi117747 	 * On a clustered note send this notification to the clustering
694852Svi117747 	 * subsystem.
695852Svi117747 	 */
696852Svi117747 	if (cl_sctp_unlisten != NULL) {
697852Svi117747 		uchar_t	*slist;
698852Svi117747 		ssize_t	ssize;
699852Svi117747 
700852Svi117747 		ssize = sizeof (in6_addr_t) * sctp->sctp_nsaddrs;
701852Svi117747 		slist = kmem_alloc(ssize, KM_SLEEP);
702852Svi117747 		sctp_get_saddr_list(sctp, slist, ssize);
703852Svi117747 		(*cl_sctp_unlisten)(sctp->sctp_family, slist,
704852Svi117747 		    sctp->sctp_nsaddrs, sctp->sctp_lport);
705852Svi117747 		/* list will be freed by the clustering module */
706852Svi117747 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
7090Sstevel@tonic-gate 	ASSERT(tf->tf_sctp);
7100Sstevel@tonic-gate 	if (tf->tf_sctp == sctp) {
7110Sstevel@tonic-gate 		tf->tf_sctp = sctp->sctp_listen_hash_next;
7120Sstevel@tonic-gate 		if (sctp->sctp_listen_hash_next) {
7130Sstevel@tonic-gate 			ASSERT(tf->tf_sctp->sctp_listen_hash_prev == sctp);
7140Sstevel@tonic-gate 			tf->tf_sctp->sctp_listen_hash_prev = NULL;
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 	} else {
7170Sstevel@tonic-gate 		ASSERT(sctp->sctp_listen_hash_prev);
7180Sstevel@tonic-gate 		ASSERT(sctp->sctp_listen_hash_prev->sctp_listen_hash_next ==
7190Sstevel@tonic-gate 		    sctp);
7200Sstevel@tonic-gate 		sctp->sctp_listen_hash_prev->sctp_listen_hash_next =
7210Sstevel@tonic-gate 		    sctp->sctp_listen_hash_next;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		if (sctp->sctp_listen_hash_next) {
7240Sstevel@tonic-gate 			ASSERT(
7250Sstevel@tonic-gate 			sctp->sctp_listen_hash_next->sctp_listen_hash_prev ==
7260Sstevel@tonic-gate 			    sctp);
7270Sstevel@tonic-gate 			sctp->sctp_listen_hash_next->sctp_listen_hash_prev =
7280Sstevel@tonic-gate 			    sctp->sctp_listen_hash_prev;
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 	sctp->sctp_listen_hash_next = NULL;
7320Sstevel@tonic-gate 	sctp->sctp_listen_hash_prev = NULL;
7330Sstevel@tonic-gate 	sctp->sctp_listen_tfp = NULL;
7340Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate void
7380Sstevel@tonic-gate sctp_listen_hash_insert(sctp_tf_t *tf, sctp_t *sctp)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp) {
7410Sstevel@tonic-gate 		sctp_listen_hash_remove(sctp);
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
7450Sstevel@tonic-gate 	sctp->sctp_listen_hash_next = tf->tf_sctp;
7460Sstevel@tonic-gate 	if (tf->tf_sctp) {
7470Sstevel@tonic-gate 		tf->tf_sctp->sctp_listen_hash_prev = sctp;
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 	sctp->sctp_listen_hash_prev = NULL;
7500Sstevel@tonic-gate 	tf->tf_sctp = sctp;
7510Sstevel@tonic-gate 	sctp->sctp_listen_tfp = tf;
7520Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
753852Svi117747 	/*
754852Svi117747 	 * On a clustered note send this notification to the clustering
755852Svi117747 	 * subsystem.
756852Svi117747 	 */
757852Svi117747 	if (cl_sctp_listen != NULL) {
758852Svi117747 		uchar_t	*slist;
759852Svi117747 		ssize_t	ssize;
760852Svi117747 
761852Svi117747 		ssize = sizeof (in6_addr_t) * sctp->sctp_nsaddrs;
762852Svi117747 		slist = kmem_alloc(ssize, KM_SLEEP);
763852Svi117747 		sctp_get_saddr_list(sctp, slist, ssize);
764852Svi117747 		(*cl_sctp_listen)(sctp->sctp_family, slist,
765852Svi117747 		    sctp->sctp_nsaddrs, sctp->sctp_lport);
766852Svi117747 		/* list will be freed by the clustering module */
767852Svi117747 	}
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate /*
7710Sstevel@tonic-gate  * Hash list insertion routine for sctp_t structures.
7720Sstevel@tonic-gate  * Inserts entries with the ones bound to a specific IP address first
7730Sstevel@tonic-gate  * followed by those bound to INADDR_ANY.
7740Sstevel@tonic-gate  */
7750Sstevel@tonic-gate void
7760Sstevel@tonic-gate sctp_bind_hash_insert(sctp_tf_t *tbf, sctp_t *sctp, int caller_holds_lock)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	sctp_t	**sctpp;
7790Sstevel@tonic-gate 	sctp_t	*sctpnext;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (sctp->sctp_ptpbhn != NULL) {
7820Sstevel@tonic-gate 		ASSERT(!caller_holds_lock);
7830Sstevel@tonic-gate 		sctp_bind_hash_remove(sctp);
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 	sctpp = &tbf->tf_sctp;
7860Sstevel@tonic-gate 	if (!caller_holds_lock) {
7870Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
7880Sstevel@tonic-gate 	} else {
7890Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&tbf->tf_lock));
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 	sctpnext = sctpp[0];
7920Sstevel@tonic-gate 	if (sctpnext) {
7930Sstevel@tonic-gate 		sctpnext->sctp_ptpbhn = &sctp->sctp_bind_hash;
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 	sctp->sctp_bind_hash = sctpnext;
7960Sstevel@tonic-gate 	sctp->sctp_ptpbhn = sctpp;
7970Sstevel@tonic-gate 	sctpp[0] = sctp;
7980Sstevel@tonic-gate 	/* For sctp_*_hash_remove */
7990Sstevel@tonic-gate 	sctp->sctp_bind_lockp = &tbf->tf_lock;
8000Sstevel@tonic-gate 	if (!caller_holds_lock)
8010Sstevel@tonic-gate 		mutex_exit(&tbf->tf_lock);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate  * Hash list removal routine for sctp_t structures.
8060Sstevel@tonic-gate  */
8070Sstevel@tonic-gate void
8080Sstevel@tonic-gate sctp_bind_hash_remove(sctp_t *sctp)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate 	sctp_t	*sctpnext;
8110Sstevel@tonic-gate 	kmutex_t *lockp;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	lockp = sctp->sctp_bind_lockp;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if (sctp->sctp_ptpbhn == NULL)
8160Sstevel@tonic-gate 		return;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	ASSERT(lockp != NULL);
8190Sstevel@tonic-gate 	mutex_enter(lockp);
8200Sstevel@tonic-gate 	if (sctp->sctp_ptpbhn) {
8210Sstevel@tonic-gate 		sctpnext = sctp->sctp_bind_hash;
8220Sstevel@tonic-gate 		if (sctpnext) {
8230Sstevel@tonic-gate 			sctpnext->sctp_ptpbhn = sctp->sctp_ptpbhn;
8240Sstevel@tonic-gate 			sctp->sctp_bind_hash = NULL;
8250Sstevel@tonic-gate 		}
8260Sstevel@tonic-gate 		*sctp->sctp_ptpbhn = sctpnext;
8270Sstevel@tonic-gate 		sctp->sctp_ptpbhn = NULL;
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 	mutex_exit(lockp);
8300Sstevel@tonic-gate 	sctp->sctp_bind_lockp = NULL;
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate  * Similar to but more general than ip_sctp's conn_match().
8350Sstevel@tonic-gate  *
8360Sstevel@tonic-gate  * Matches sets of addresses as follows: if the argument addr set is
8370Sstevel@tonic-gate  * a complete subset of the corresponding addr set in the sctp_t, it
8380Sstevel@tonic-gate  * is a match.
8390Sstevel@tonic-gate  *
8400Sstevel@tonic-gate  * Caller must hold tf->tf_lock.
8410Sstevel@tonic-gate  *
8420Sstevel@tonic-gate  * Returns with a SCTP_REFHOLD sctp structure. Caller must do a SCTP_REFRELE.
8430Sstevel@tonic-gate  */
8440Sstevel@tonic-gate sctp_t *
8450Sstevel@tonic-gate sctp_lookup(sctp_t *sctp1, in6_addr_t *faddr, sctp_tf_t *tf, uint32_t *ports,
8460Sstevel@tonic-gate     int min_state)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	sctp_t *sctp;
8500Sstevel@tonic-gate 	sctp_faddr_t *fp;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tf->tf_lock));
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
8550Sstevel@tonic-gate 		if (*ports != sctp->sctp_ports || sctp->sctp_state <
8560Sstevel@tonic-gate 		    min_state) {
8570Sstevel@tonic-gate 			continue;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		/* check for faddr match */
8610Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
8620Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(faddr, &fp->faddr)) {
8630Sstevel@tonic-gate 				break;
8640Sstevel@tonic-gate 			}
8650Sstevel@tonic-gate 		}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 		if (!fp) {
8680Sstevel@tonic-gate 			/* no faddr match; keep looking */
8690Sstevel@tonic-gate 			continue;
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		/* check for laddr subset match */
8730Sstevel@tonic-gate 		if (sctp_compare_saddrs(sctp1, sctp) <= SCTP_ADDR_SUBSET) {
8740Sstevel@tonic-gate 			goto done;
8750Sstevel@tonic-gate 		}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		/* no match; continue searching */
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate done:
8810Sstevel@tonic-gate 	if (sctp) {
8820Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 	return (sctp);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate boolean_t
8880Sstevel@tonic-gate ip_fanout_sctp_raw_match(conn_t *connp, uint32_t ports, ipha_t *ipha)
8890Sstevel@tonic-gate {
8900Sstevel@tonic-gate 	uint16_t lport;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if (connp->conn_fully_bound) {
8930Sstevel@tonic-gate 		return (IPCL_CONN_MATCH(connp, IPPROTO_SCTP, ipha->ipha_src,
8940Sstevel@tonic-gate 		    ipha->ipha_dst, ports));
8950Sstevel@tonic-gate 	} else {
8960Sstevel@tonic-gate 		lport = htons(ntohl(ports) & 0xFFFF);
8970Sstevel@tonic-gate 		return (IPCL_BIND_MATCH(connp, IPPROTO_SCTP, ipha->ipha_dst,
8980Sstevel@tonic-gate 		    lport));
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate boolean_t
9030Sstevel@tonic-gate ip_fanout_sctp_raw_match_v6(conn_t *connp, uint32_t ports, ip6_t *ip6h,
9040Sstevel@tonic-gate     boolean_t for_v4)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	uint16_t lport;
9070Sstevel@tonic-gate 	in6_addr_t	v6dst;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	if (!for_v4 && connp->conn_fully_bound) {
9100Sstevel@tonic-gate 		return (IPCL_CONN_MATCH_V6(connp, IPPROTO_SCTP, ip6h->ip6_src,
9110Sstevel@tonic-gate 		    ip6h->ip6_dst, ports));
9120Sstevel@tonic-gate 	} else {
9130Sstevel@tonic-gate 		lport = htons(ntohl(ports) & 0xFFFF);
9140Sstevel@tonic-gate 		if (for_v4)
9150Sstevel@tonic-gate 			v6dst = ipv6_all_zeros;
9160Sstevel@tonic-gate 		else
9170Sstevel@tonic-gate 			v6dst = ip6h->ip6_dst;
9180Sstevel@tonic-gate 		return (IPCL_BIND_MATCH_V6(connp, IPPROTO_SCTP, v6dst, lport));
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate }
921