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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23252Svi117747  * Copyright 2005 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/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/list.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <netinet/in.h>
400Sstevel@tonic-gate #include <netinet/ip6.h>
410Sstevel@tonic-gate #include <netinet/sctp.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <inet/common.h>
440Sstevel@tonic-gate #include <inet/ip.h>
450Sstevel@tonic-gate #include <inet/ip6.h>
460Sstevel@tonic-gate #include <inet/ip_if.h>
470Sstevel@tonic-gate #include <inet/ipclassifier.h>
480Sstevel@tonic-gate #include <inet/sctp_ip.h>
490Sstevel@tonic-gate #include "sctp_impl.h"
500Sstevel@tonic-gate #include "sctp_addr.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
530Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
540Sstevel@tonic-gate 			    zoneid_t zoneid);
550Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
560Sstevel@tonic-gate int			sctp_valid_addr_list(sctp_t *, const void *, uint32_t);
570Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_ipif_lookup(sctp_t *, uint_t);
58*432Svi117747 static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
59*432Svi117747 			    boolean_t dontsrc);
600Sstevel@tonic-gate static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
610Sstevel@tonic-gate static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
620Sstevel@tonic-gate 			    sctp_ipif_hash_t *);
630Sstevel@tonic-gate int			sctp_compare_saddrs(sctp_t *, sctp_t *);
640Sstevel@tonic-gate static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
650Sstevel@tonic-gate int			sctp_dup_saddrs(sctp_t *, sctp_t *, int);
660Sstevel@tonic-gate void			sctp_free_saddrs(sctp_t *);
670Sstevel@tonic-gate void			sctp_update_ill(ill_t *, int);
680Sstevel@tonic-gate void			sctp_update_ipif(ipif_t *, int);
690Sstevel@tonic-gate void			sctp_move_ipif(ipif_t *, ill_t *, ill_t *);
700Sstevel@tonic-gate void			sctp_del_saddr(sctp_t *, sctp_saddr_ipif_t *);
710Sstevel@tonic-gate void			sctp_del_saddr_list(sctp_t *, const void *, int,
720Sstevel@tonic-gate 			    boolean_t);
730Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_saddr_lookup(sctp_t *, in6_addr_t *);
740Sstevel@tonic-gate in6_addr_t		sctp_get_valid_addr(sctp_t *, boolean_t);
750Sstevel@tonic-gate int			sctp_getmyaddrs(void *, void *, int *);
760Sstevel@tonic-gate void			sctp_saddr_init();
770Sstevel@tonic-gate void			sctp_saddr_fini();
78*432Svi117747 
790Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
800Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
81*432Svi117747 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
82*432Svi117747 
83*432Svi117747 #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
84*432Svi117747 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
85*432Svi117747 
86*432Svi117747 /* True if this is a loopback ILL or a LINKLOCAL addr */
87*432Svi117747 #define	SCTP_IS_LL_LB(ill, ipif)	\
88*432Svi117747 	(((ill)->sctp_ill_flags & PHYI_LOOPBACK) ||	\
89*432Svi117747 	((ipif)->sctp_ipif_isv6 && 			\
90*432Svi117747 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr)))
91*432Svi117747 
92*432Svi117747 #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
93*432Svi117747 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
94*432Svi117747 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
970Sstevel@tonic-gate #define	SCTP_IPIF_HASH_FN(seqid)	((seqid) % SCTP_IPIF_HASH)
980Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /* Global list of SCTP ILLs */
1010Sstevel@tonic-gate sctp_ill_hash_t	sctp_g_ills[SCTP_ILL_HASH];
1020Sstevel@tonic-gate uint32_t	sctp_ills_count = 0;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* Global list of SCTP IPIFs */
1050Sstevel@tonic-gate sctp_ipif_hash_t	sctp_g_ipifs[SCTP_IPIF_HASH];
1060Sstevel@tonic-gate uint32_t		sctp_g_ipifs_count;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  *
1110Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1180Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1190Sstevel@tonic-gate  * Called with no locks held.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate static void
1220Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
1250Sstevel@tonic-gate 	uint_t		ipif_index;
1260Sstevel@tonic-gate 	uint_t		ill_index;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
1290Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(sctp_ipif->sctp_ipif_id);
1320Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1330Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1340Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1350Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1360Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
1370Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
1380Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
1390Sstevel@tonic-gate 		return;
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	list_remove(&sctp_g_ipifs[ipif_index].sctp_ipif_list, sctp_ipif);
1420Sstevel@tonic-gate 	sctp_g_ipifs[ipif_index].ipif_count--;
1430Sstevel@tonic-gate 	sctp_g_ipifs_count--;
1440Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1450Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1480Sstevel@tonic-gate 	if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
1490Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
1500Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1510Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1520Sstevel@tonic-gate 			list_remove(&sctp_g_ills[ill_index].sctp_ill_list,
1530Sstevel@tonic-gate 			    (void *)sctp_ill);
1540Sstevel@tonic-gate 			sctp_g_ills[ill_index].ill_count--;
1550Sstevel@tonic-gate 			sctp_ills_count--;
1560Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1570Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1580Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
1620Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
1670Sstevel@tonic-gate  * Called with no locks held.
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate static sctp_ipif_t *
1700Sstevel@tonic-gate sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	int		i;
1730Sstevel@tonic-gate 	int		j;
1740Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
1770Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1780Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
1790Sstevel@tonic-gate 			continue;
1800Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
1810Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
1820Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1830Sstevel@tonic-gate 			if (zoneid == sctp_ipif->sctp_ipif_zoneid &&
1840Sstevel@tonic-gate 			    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) &&
1850Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1860Sstevel@tonic-gate 			    addr)) {
1870Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1880Sstevel@tonic-gate 				if (refhold)
1890Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
1900Sstevel@tonic-gate 				rw_exit(&sctp_g_ipifs_lock);
1910Sstevel@tonic-gate 				return (sctp_ipif);
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1940Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
1950Sstevel@tonic-gate 			    sctp_ipif);
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
1990Sstevel@tonic-gate 	return (NULL);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
2040Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
2050Sstevel@tonic-gate  * Called with no locks held.
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate static int
2080Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
2110Sstevel@tonic-gate 	int			i;
2120Sstevel@tonic-gate 	int			j;
2130Sstevel@tonic-gate 	int			error = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
2160Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
2170Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
2180Sstevel@tonic-gate 			continue;
2190Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
2200Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
2210Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
222*432Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
2230Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
224*432Svi117747 			    sctp_ipif->sctp_ipif_zoneid != sctp->sctp_zoneid ||
2250Sstevel@tonic-gate 			    (sctp->sctp_ipversion == IPV4_VERSION &&
226*432Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
2270Sstevel@tonic-gate 			    (sctp->sctp_connp->conn_ipv6_v6only &&
228*432Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
2290Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2300Sstevel@tonic-gate 				sctp_ipif = list_next(
2310Sstevel@tonic-gate 				    &sctp_g_ipifs[i].sctp_ipif_list, sctp_ipif);
2320Sstevel@tonic-gate 				continue;
2330Sstevel@tonic-gate 			}
2340Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2350Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
236*432Svi117747 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
237*432Svi117747 			    B_FALSE);
2380Sstevel@tonic-gate 			if (error != 0)
2390Sstevel@tonic-gate 				goto free_stuff;
2400Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
2410Sstevel@tonic-gate 			    sctp_ipif);
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2450Sstevel@tonic-gate 	return (0);
2460Sstevel@tonic-gate free_stuff:
2470Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2480Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2490Sstevel@tonic-gate 	return (ENOMEM);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2540Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
2550Sstevel@tonic-gate  * or error.
2560Sstevel@tonic-gate  * Called with no locks held.
2570Sstevel@tonic-gate  */
2580Sstevel@tonic-gate int
2590Sstevel@tonic-gate sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2620Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2630Sstevel@tonic-gate 	struct in_addr		*addr4;
2640Sstevel@tonic-gate 	in6_addr_t		addr;
2650Sstevel@tonic-gate 	int			cnt;
2660Sstevel@tonic-gate 	int			err = 0;
2670Sstevel@tonic-gate 	int			saddr_cnt = 0;
2680Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
2690Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
2700Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
2710Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/*
2740Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
2750Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
2760Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
2770Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
2780Sstevel@tonic-gate 	 * addresses.
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
2810Sstevel@tonic-gate 		check_lport = B_TRUE;
2820Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
2830Sstevel@tonic-gate 			check_addrs = B_TRUE;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
2860Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
2870Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
2880Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
2890Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
2900Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		switch (sctp->sctp_family) {
2930Sstevel@tonic-gate 		case AF_INET:
2940Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
2950Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
2960Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
2970Sstevel@tonic-gate 				err = EINVAL;
2980Sstevel@tonic-gate 				goto free_ret;
2990Sstevel@tonic-gate 			}
3000Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
3010Sstevel@tonic-gate 			if (check_addrs &&
3020Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
3030Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
3040Sstevel@tonic-gate 			    IN_MULTICAST(addr4->s_addr))) {
3050Sstevel@tonic-gate 				err = EINVAL;
3060Sstevel@tonic-gate 				goto free_ret;
3070Sstevel@tonic-gate 			}
3080Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
3090Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
3100Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3110Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3120Sstevel@tonic-gate 			}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 			break;
3150Sstevel@tonic-gate 		case AF_INET6:
3160Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3170Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
3180Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
3190Sstevel@tonic-gate 				err = EINVAL;
3200Sstevel@tonic-gate 				goto free_ret;
3210Sstevel@tonic-gate 			}
3220Sstevel@tonic-gate 			addr = sin6->sin6_addr;
3230Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
3240Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3250Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3260Sstevel@tonic-gate 				goto free_ret;
3270Sstevel@tonic-gate 			}
3280Sstevel@tonic-gate 			if (check_addrs &&
3290Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3300Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3310Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3320Sstevel@tonic-gate 				err = EINVAL;
3330Sstevel@tonic-gate 				goto free_ret;
3340Sstevel@tonic-gate 			}
3350Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3360Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3370Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 			break;
3410Sstevel@tonic-gate 		default:
3420Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3430Sstevel@tonic-gate 			goto free_ret;
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 		if (lookup_saddr) {
3460Sstevel@tonic-gate 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
3470Sstevel@tonic-gate 			    sctp->sctp_zoneid);
3480Sstevel@tonic-gate 			if (ipif == NULL) {
3490Sstevel@tonic-gate 				/* Address not in the list */
3500Sstevel@tonic-gate 				err = EINVAL;
3510Sstevel@tonic-gate 				goto free_ret;
3520Sstevel@tonic-gate 			} else if (check_addrs &&
3530Sstevel@tonic-gate 			    (ipif->sctp_ipif_ill->sctp_ill_flags &
3540Sstevel@tonic-gate 			    PHYI_LOOPBACK)) {
3550Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3560Sstevel@tonic-gate 				err = EINVAL;
3570Sstevel@tonic-gate 				goto free_ret;
3580Sstevel@tonic-gate 			}
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 		if (!bind_to_all) {
361*432Svi117747 			/*
362*432Svi117747 			 * If an address is added after association setup,
363*432Svi117747 			 * we need to wait for the peer to send us an ASCONF
364*432Svi117747 			 * ACK before we can start using it.
365*432Svi117747 			 * saddr_ipif_dontsrc will be reset (to 0) when we
366*432Svi117747 			 * get the ASCONF ACK for this address.
367*432Svi117747 			 */
368*432Svi117747 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
369*432Svi117747 			    check_addrs ? B_TRUE : B_FALSE);
3700Sstevel@tonic-gate 			if (err != 0) {
3710Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3720Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
3730Sstevel@tonic-gate 					err = EADDRINUSE;
3740Sstevel@tonic-gate 				goto free_ret;
3750Sstevel@tonic-gate 			}
3760Sstevel@tonic-gate 			saddr_cnt++;
3770Sstevel@tonic-gate 		}
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	if (bind_to_all) {
3800Sstevel@tonic-gate 		/*
3810Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
3820Sstevel@tonic-gate 		 * inaddr_any.
3830Sstevel@tonic-gate 		 */
3840Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
3850Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
3860Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
3890Sstevel@tonic-gate 		if (err != 0)
3900Sstevel@tonic-gate 			return (err);
3910Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3940Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
3950Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3960Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
3970Sstevel@tonic-gate 	return (0);
3980Sstevel@tonic-gate free_ret:
3990Sstevel@tonic-gate 	if (saddr_cnt != 0)
4000Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
4010Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4020Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4030Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4040Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4050Sstevel@tonic-gate 	return (err);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate sctp_saddr_ipif_t *
4090Sstevel@tonic-gate sctp_ipif_lookup(sctp_t *sctp, uint_t ipif_index)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	int			cnt;
4120Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif_index);
4130Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (sctp->sctp_saddrs[seqid].ipif_count == 0)
4160Sstevel@tonic-gate 		return (NULL);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4190Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4200Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif_index)
4210Sstevel@tonic-gate 			return (ipif_obj);
4220Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4230Sstevel@tonic-gate 		    ipif_obj);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 	return (NULL);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate static int
429*432Svi117747 sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
430*432Svi117747     boolean_t dontsrc)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	int			cnt;
4330Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4340Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4370Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4380Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id)
4390Sstevel@tonic-gate 			return (EALREADY);
4400Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4410Sstevel@tonic-gate 		    ipif_obj);
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4440Sstevel@tonic-gate 	if (ipif_obj == NULL) {
4450Sstevel@tonic-gate 		/* Need to do something */
4460Sstevel@tonic-gate 		return (ENOMEM);
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
449*432Svi117747 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
4500Sstevel@tonic-gate 	list_insert_tail(&sctp->sctp_saddrs[seqid].sctp_ipif_list, ipif_obj);
4510Sstevel@tonic-gate 	sctp->sctp_saddrs[seqid].ipif_count++;
4520Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
4530Sstevel@tonic-gate 	return (0);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate static void
4570Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	int			cnt;
4600Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4610Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4640Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4650Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) {
4660Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4670Sstevel@tonic-gate 			    ipif_obj);
4680Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
4690Sstevel@tonic-gate 			sctp->sctp_saddrs[seqid].ipif_count--;
4700Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
4710Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
4720Sstevel@tonic-gate 			break;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4750Sstevel@tonic-gate 		    ipif_obj);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate static int
4800Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	int			i;
4830Sstevel@tonic-gate 	int			j;
4840Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
4850Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
4860Sstevel@tonic-gate 	int			overlap = 0;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
4890Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
4900Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
4910Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
4920Sstevel@tonic-gate 			if (obj1->saddr_ipifp->sctp_ipif_id ==
4930Sstevel@tonic-gate 			    obj2->saddr_ipifp->sctp_ipif_id) {
4940Sstevel@tonic-gate 				overlap++;
4950Sstevel@tonic-gate 				break;
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
4980Sstevel@tonic-gate 			    obj2);
4990Sstevel@tonic-gate 		}
5000Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 	return (overlap);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate int
5060Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	int		i;
5090Sstevel@tonic-gate 	int		overlap = 0;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5120Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
5130Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5170Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5180Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
5220Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (overlap > 0)
5250Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate static int
5310Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate 	int			i;
5340Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5350Sstevel@tonic-gate 	int			error = 0;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
5380Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5390Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
540*432Svi117747 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
541*432Svi117747 		    B_FALSE);
5420Sstevel@tonic-gate 		if (error != 0)
5430Sstevel@tonic-gate 			return (error);
5440Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 	return (error);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate int
5500Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	int	error = 0;
5530Sstevel@tonic-gate 	int	i;
5540Sstevel@tonic-gate 
555*432Svi117747 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
5560Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5590Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
5600Sstevel@tonic-gate 			continue;
5610Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
5620Sstevel@tonic-gate 		if (error != 0) {
5630Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
5640Sstevel@tonic-gate 			return (error);
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 	return (0);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate void
5710Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	int			i;
5740Sstevel@tonic-gate 	int			l;
5750Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
5780Sstevel@tonic-gate 		return;
5790Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5800Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
5810Sstevel@tonic-gate 			continue;
5820Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
5830Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
5840Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
5850Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
5860Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
5870Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
5880Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
5890Sstevel@tonic-gate 		}
5900Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
5910Sstevel@tonic-gate 	}
592*432Svi117747 	if (sctp->sctp_bound_to_all == 1)
593*432Svi117747 		sctp->sctp_bound_to_all = 0;
5940Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate /*
5980Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
5990Sstevel@tonic-gate  * held.
6000Sstevel@tonic-gate  */
6010Sstevel@tonic-gate void
6020Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	int		i;
6050Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
6060Sstevel@tonic-gate 	uint_t		index;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	ip2dbg(("sctp_update_ill: %s\n", ill->ill_name));
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_WRITER);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
6130Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
6140Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
6150Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
6160Sstevel@tonic-gate 			break;
6170Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
6180Sstevel@tonic-gate 		    sctp_ill);
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	switch (op) {
6220Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
6230Sstevel@tonic-gate 		if (sctp_ill != NULL) {
6240Sstevel@tonic-gate 			/* Unmark it if it is condemned */
6250Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
6260Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
6270Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6280Sstevel@tonic-gate 			return;
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
6310Sstevel@tonic-gate 		/* Need to re-try? */
6320Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6330Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6340Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6350Sstevel@tonic-gate 			return;
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 		sctp_ill->sctp_ill_name =
6380Sstevel@tonic-gate 		    kmem_zalloc(ill->ill_name_length, KM_NOSLEEP);
6390Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
6400Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6410Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6420Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6430Sstevel@tonic-gate 			return;
6440Sstevel@tonic-gate 		}
6450Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
6460Sstevel@tonic-gate 		    ill->ill_name_length);
6470Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
6480Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
6490Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
6500Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ills[index].sctp_ill_list,
6510Sstevel@tonic-gate 		    (void *)sctp_ill);
6520Sstevel@tonic-gate 		sctp_g_ills[index].ill_count++;
6530Sstevel@tonic-gate 		sctp_ills_count++;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		break;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6600Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6610Sstevel@tonic-gate 			return;
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
6640Sstevel@tonic-gate 			list_remove(&sctp_g_ills[index].sctp_ill_list,
6650Sstevel@tonic-gate 			    (void *)sctp_ill);
6660Sstevel@tonic-gate 			sctp_g_ills[index].ill_count--;
6670Sstevel@tonic-gate 			sctp_ills_count--;
6680Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
6690Sstevel@tonic-gate 			    ill->ill_name_length);
6700Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6710Sstevel@tonic-gate 		} else {
6720Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		break;
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
6810Sstevel@tonic-gate void
6820Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
6850Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
6860Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
6870Sstevel@tonic-gate 	uint_t		index;
6880Sstevel@tonic-gate 	int		i;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
6910Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
6940Sstevel@tonic-gate 	fsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
6950Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
6960Sstevel@tonic-gate 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill))
6970Sstevel@tonic-gate 			break;
6980Sstevel@tonic-gate 		fsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
6990Sstevel@tonic-gate 		    fsctp_ill);
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
7030Sstevel@tonic-gate 	tsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
7040Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
7050Sstevel@tonic-gate 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill))
7060Sstevel@tonic-gate 			break;
7070Sstevel@tonic-gate 		tsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
7080Sstevel@tonic-gate 		    tsctp_ill);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7120Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[index].sctp_ipif_list);
7130Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[index].ipif_count; i++) {
7140Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7150Sstevel@tonic-gate 			break;
7160Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[index].sctp_ipif_list,
7170Sstevel@tonic-gate 		    sctp_ipif);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 	/* Should be an ASSERT? */
7200Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
7210Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
7220Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
7230Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7240Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7250Sstevel@tonic-gate 		return;
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
7280Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
7290Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
7300Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
7310Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
7320Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
7330Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
7340Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
7380Sstevel@tonic-gate void
7390Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
7420Sstevel@tonic-gate 	int		i;
7430Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
7440Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7450Sstevel@tonic-gate 	uint_t		ill_index;
7460Sstevel@tonic-gate 	uint_t		ipif_index;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
7510Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
7540Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[ill_index].sctp_ill_list);
7550Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[ill_index].ill_count; i++) {
7560Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
7570Sstevel@tonic-gate 			break;
7580Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[ill_index].sctp_ill_list,
7590Sstevel@tonic-gate 		    sctp_ill);
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate 	if (sctp_ill == NULL) {
7620Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7630Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7640Sstevel@tonic-gate 		return;
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7680Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[ipif_index].sctp_ipif_list);
7690Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[ipif_index].ipif_count; i++) {
7700Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7710Sstevel@tonic-gate 			break;
7720Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
7730Sstevel@tonic-gate 		    sctp_ipif);
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 	if (op != SCTP_IPIF_INSERT && sctp_ipif == NULL) {
7760Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
7770Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7780Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7790Sstevel@tonic-gate 		return;
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate #ifdef	DEBUG
7820Sstevel@tonic-gate 	if (sctp_ipif != NULL)
7830Sstevel@tonic-gate 		ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
7840Sstevel@tonic-gate #endif
7850Sstevel@tonic-gate 	switch (op) {
7860Sstevel@tonic-gate 	case SCTP_IPIF_INSERT:
7870Sstevel@tonic-gate 		if (sctp_ipif != NULL) {
7880Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED)
789252Svi117747 				sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
7900Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
7910Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
7920Sstevel@tonic-gate 			return;
7930Sstevel@tonic-gate 		}
7940Sstevel@tonic-gate 		sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
7950Sstevel@tonic-gate 		/* Try again? */
7960Sstevel@tonic-gate 		if (sctp_ipif == NULL) {
7970Sstevel@tonic-gate 			ip1dbg(("sctp_ipif_insert: mem failure..\n"));
7980Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
7990Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8000Sstevel@tonic-gate 			return;
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
8030Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_ill = sctp_ill;
804252Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
8050Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8060Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
8070Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
808*432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8090Sstevel@tonic-gate 		rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
8100Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
8110Sstevel@tonic-gate 		    (void *)sctp_ipif);
8120Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count++;
8130Sstevel@tonic-gate 		sctp_g_ipifs_count++;
8140Sstevel@tonic-gate 		atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 		break;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
8190Sstevel@tonic-gate 	{
8200Sstevel@tonic-gate 		list_t		*ipif_list;
8210Sstevel@tonic-gate 		list_t		*ill_list;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 		ill_list = &sctp_g_ills[ill_index].sctp_ill_list;
8240Sstevel@tonic-gate 		ipif_list = &sctp_g_ipifs[ipif_index].sctp_ipif_list;
8250Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
8260Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
8270Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8280Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8290Sstevel@tonic-gate 			return;
8300Sstevel@tonic-gate 		}
8310Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
8320Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count--;
8330Sstevel@tonic-gate 		sctp_g_ipifs_count--;
8340Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
8350Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
8360Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
8370Sstevel@tonic-gate 		if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
8380Sstevel@tonic-gate 			rw_downgrade(&sctp_g_ipifs_lock);
8390Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
8400Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
8410Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
8420Sstevel@tonic-gate 				sctp_ills_count--;
8430Sstevel@tonic-gate 				sctp_g_ills[ill_index].ill_count--;
8440Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
8450Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
8460Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
8470Sstevel@tonic-gate 			}
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 		break;
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	case SCTP_IPIF_UP:
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8550Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8560Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
8570Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
858*432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
859*432Svi117747 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8600Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		break;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8670Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8680Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8690Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
8700Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
871*432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8720Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 		break;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8790Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8800Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
8810Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		break;
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
8860Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate /*
8900Sstevel@tonic-gate  *
8910Sstevel@tonic-gate  *
8920Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
8930Sstevel@tonic-gate  * sctp locking by the caller.
8940Sstevel@tonic-gate  *
8950Sstevel@tonic-gate  *
8960Sstevel@tonic-gate  */
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate /* Remove a specific saddr from the list */
8990Sstevel@tonic-gate void
9000Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
9010Sstevel@tonic-gate {
9020Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9030Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9060Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
9090Sstevel@tonic-gate 
910*432Svi117747 	if (sctp->sctp_bound_to_all == 1)
9110Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9140Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9170Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
9220Sstevel@tonic-gate  * Called with no locks held.
9230Sstevel@tonic-gate  */
9240Sstevel@tonic-gate void
9250Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
9260Sstevel@tonic-gate     boolean_t fanout_locked)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
9290Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
9300Sstevel@tonic-gate 	int			cnt;
9310Sstevel@tonic-gate 	in6_addr_t		addr;
9320Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > addcnt);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	if (!fanout_locked) {
9370Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9380Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9390Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9400Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
9440Sstevel@tonic-gate 		switch (sctp->sctp_family) {
9450Sstevel@tonic-gate 		case AF_INET:
9460Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
9470Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
9480Sstevel@tonic-gate 			break;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		case AF_INET6:
9510Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
9520Sstevel@tonic-gate 			addr = sin6->sin6_addr;
9530Sstevel@tonic-gate 			break;
9540Sstevel@tonic-gate 		}
9550Sstevel@tonic-gate 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
9560Sstevel@tonic-gate 		    sctp->sctp_zoneid);
9570Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
9580Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
9590Sstevel@tonic-gate 	}
960*432Svi117747 	if (sctp->sctp_bound_to_all == 1)
9610Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	if (!fanout_locked) {
9640Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9650Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9660Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9670Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate /*
9720Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
9730Sstevel@tonic-gate  * Called with no locks held.
9740Sstevel@tonic-gate  */
9750Sstevel@tonic-gate sctp_saddr_ipif_t *
9760Sstevel@tonic-gate sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	sctp_saddr_ipif_t	*saddr_ipifs;
9790Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_FALSE, sctp->sctp_zoneid);
9820Sstevel@tonic-gate 	if (sctp_ipif == NULL)
9830Sstevel@tonic-gate 		return (NULL);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	saddr_ipifs = sctp_ipif_lookup(sctp, sctp_ipif->sctp_ipif_id);
9860Sstevel@tonic-gate 	return (saddr_ipifs);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate 
989*432Svi117747 /* Given an address, add it to the source address list */
990*432Svi117747 int
991*432Svi117747 sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr)
992*432Svi117747 {
993*432Svi117747 	sctp_ipif_t		*sctp_ipif;
994*432Svi117747 
995*432Svi117747 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp->sctp_zoneid);
996*432Svi117747 	if (sctp_ipif == NULL)
997*432Svi117747 		return (EINVAL);
998*432Svi117747 
999*432Svi117747 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE) != 0) {
1000*432Svi117747 		SCTP_IPIF_REFRELE(sctp_ipif);
1001*432Svi117747 		return (EINVAL);
1002*432Svi117747 	}
1003*432Svi117747 	return (0);
1004*432Svi117747 }
1005*432Svi117747 
1006*432Svi117747 /*
1007*432Svi117747  * Remove or mark as dontsrc addresses that are currently not part of the
1008*432Svi117747  * association. One would delete addresses when processing an INIT and
1009*432Svi117747  * mark as dontsrc when processing an INIT-ACK.
1010*432Svi117747  */
1011*432Svi117747 void
1012*432Svi117747 sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete)
1013*432Svi117747 {
1014*432Svi117747 	int			i;
1015*432Svi117747 	int			l;
1016*432Svi117747 	sctp_saddr_ipif_t	*obj;
1017*432Svi117747 	int			scanned = 0;
1018*432Svi117747 	int			naddr;
1019*432Svi117747 	int			nsaddr;
1020*432Svi117747 
1021*432Svi117747 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1022*432Svi117747 
1023*432Svi117747 	/*
1024*432Svi117747 	 * Irregardless of the supported address in the INIT, v4
1025*432Svi117747 	 * must be supported.
1026*432Svi117747 	 */
1027*432Svi117747 	if (sctp->sctp_family == AF_INET)
1028*432Svi117747 		supp_af = PARM_SUPP_V4;
1029*432Svi117747 
1030*432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
1031*432Svi117747 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1032*432Svi117747 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1033*432Svi117747 			continue;
1034*432Svi117747 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1035*432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1036*432Svi117747 		for (l = 0; l < naddr; l++) {
1037*432Svi117747 			sctp_ipif_t	*ipif;
1038*432Svi117747 			sctp_ill_t	*ill;
1039*432Svi117747 
1040*432Svi117747 			ipif = obj->saddr_ipifp;
1041*432Svi117747 			ill = ipif->sctp_ipif_ill;
1042*432Svi117747 			scanned++;
1043*432Svi117747 
1044*432Svi117747 			/*
1045*432Svi117747 			 * Delete/mark dontsrc loopback/linklocal addresses and
1046*432Svi117747 			 * unsupported address.
1047*432Svi117747 			 */
1048*432Svi117747 			if (SCTP_IS_LL_LB(ill, ipif) ||
1049*432Svi117747 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1050*432Svi117747 				if (!delete) {
1051*432Svi117747 					obj->saddr_ipif_unconfirmed = 1;
1052*432Svi117747 					goto next_obj;
1053*432Svi117747 				}
1054*432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1055*432Svi117747 					sctp->sctp_bound_to_all = 0;
1056*432Svi117747 				if (scanned < nsaddr) {
1057*432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1058*432Svi117747 					    sctp_ipif_list, obj);
1059*432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1060*432Svi117747 					continue;
1061*432Svi117747 				}
1062*432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1063*432Svi117747 			}
1064*432Svi117747 	next_obj:
1065*432Svi117747 			if (scanned >= nsaddr)
1066*432Svi117747 				return;
1067*432Svi117747 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1068*432Svi117747 			    obj);
1069*432Svi117747 		}
1070*432Svi117747 	}
1071*432Svi117747 }
1072*432Svi117747 
1073*432Svi117747 
10740Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
10750Sstevel@tonic-gate in6_addr_t
10760Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6)
10770Sstevel@tonic-gate {
10780Sstevel@tonic-gate 	int			i;
10790Sstevel@tonic-gate 	int			l;
10800Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
10810Sstevel@tonic-gate 	int			scanned = 0;
10820Sstevel@tonic-gate 	in6_addr_t		addr;
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
10850Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
10860Sstevel@tonic-gate 			continue;
10870Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
10880Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
10890Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1092*432Svi117747 			if (!SCTP_DONT_SRC(obj) &&
10930Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
1094*432Svi117747 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
10950Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
10960Sstevel@tonic-gate 			}
10970Sstevel@tonic-gate 			scanned++;
10980Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
10990Sstevel@tonic-gate 				goto got_none;
11000Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
11010Sstevel@tonic-gate 			    obj);
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate got_none:
11050Sstevel@tonic-gate 	/* Need to double check this */
11060Sstevel@tonic-gate 	if (isv6 == B_TRUE)
11070Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
11080Sstevel@tonic-gate 	else
11090Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	return (addr);
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate /*
11150Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
11160Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
11170Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
11180Sstevel@tonic-gate  */
11190Sstevel@tonic-gate int
11200Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
11210Sstevel@tonic-gate {
11220Sstevel@tonic-gate 	int			i;
11230Sstevel@tonic-gate 	int			l;
11240Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
11250Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
11260Sstevel@tonic-gate 	int			family = sctp->sctp_family;
11270Sstevel@tonic-gate 	int			max = *addrcnt;
11280Sstevel@tonic-gate 	size_t			added = 0;
11290Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
11300Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
11310Sstevel@tonic-gate 	int			scanned = 0;
11320Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
11350Sstevel@tonic-gate 		return (EINVAL);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	/* Skip loopback addresses for non-loopback assoc. */
11380Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback)
11390Sstevel@tonic-gate 		skip_lback = B_TRUE;
11400Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11410Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11420Sstevel@tonic-gate 			continue;
11430Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11440Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11450Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
11460Sstevel@tonic-gate 			sctp_ill_t	*ill = ipif->sctp_ipif_ill;
11470Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 			scanned++;
11500Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1151*432Svi117747 			    SCTP_DONT_SRC(obj) ||
11520Sstevel@tonic-gate 			    ((ill->sctp_ill_flags & PHYI_LOOPBACK) &&
11530Sstevel@tonic-gate 			    skip_lback)) {
11540Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
11550Sstevel@tonic-gate 					goto done;
11560Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
11570Sstevel@tonic-gate 				    sctp_ipif_list, obj);
11580Sstevel@tonic-gate 				continue;
11590Sstevel@tonic-gate 			}
11600Sstevel@tonic-gate 			switch (family) {
11610Sstevel@tonic-gate 			case AF_INET:
11620Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
11630Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
11640Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
11650Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
11660Sstevel@tonic-gate 				break;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 			case AF_INET6:
11690Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
11700Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
11710Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
11720Sstevel@tonic-gate 				sin6->sin6_addr = addr;
11730Sstevel@tonic-gate 				break;
11740Sstevel@tonic-gate 			}
11750Sstevel@tonic-gate 			added++;
11760Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
11770Sstevel@tonic-gate 				goto done;
11780Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
11790Sstevel@tonic-gate 			    obj);
11800Sstevel@tonic-gate 		}
11810Sstevel@tonic-gate 	}
11820Sstevel@tonic-gate done:
11830Sstevel@tonic-gate 	*addrcnt = added;
11840Sstevel@tonic-gate 	return (0);
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate /*
1188252Svi117747  * Given the supported address family, walk through the source address list
1189252Svi117747  * and return the total length of the available addresses. If 'p' is not
1190252Svi117747  * null, construct the parameter list for the addresses in 'p'.
1191*432Svi117747  * 'modify' will only be set when we want the source address list to
1192*432Svi117747  * be modified. The source address list will be modified only when
1193*432Svi117747  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1194*432Svi117747  * be false since the 'sctp' will be that of the listener.
11950Sstevel@tonic-gate  */
11960Sstevel@tonic-gate size_t
1197*432Svi117747 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
11980Sstevel@tonic-gate {
11990Sstevel@tonic-gate 	int			i;
12000Sstevel@tonic-gate 	int			l;
12010Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1202252Svi117747 	size_t			paramlen = 0;
12030Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
12040Sstevel@tonic-gate 	int			scanned = 0;
1205*432Svi117747 	int			naddr;
1206*432Svi117747 	int			nsaddr;
1207*432Svi117747 	boolean_t		del_ll_lb = B_FALSE;
12080Sstevel@tonic-gate 
1209*432Svi117747 	if (modify && !sctp->sctp_loopback && !sctp->sctp_linklocal)
1210*432Svi117747 		del_ll_lb = B_TRUE;
1211*432Svi117747 
1212*432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
12130Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
12140Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
12150Sstevel@tonic-gate 			continue;
12160Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1217*432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1218*432Svi117747 		for (l = 0; l < naddr; l++) {
12190Sstevel@tonic-gate 			in6_addr_t	addr;
12200Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
12210Sstevel@tonic-gate 			sctp_ill_t	*ill;
1222*432Svi117747 			boolean_t	ll_lb;
1223*432Svi117747 			boolean_t	unsupp_af;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
12260Sstevel@tonic-gate 			ill = ipif->sctp_ipif_ill;
12270Sstevel@tonic-gate 			scanned++;
1228*432Svi117747 
1229*432Svi117747 			ll_lb = SCTP_IS_LL_LB(ill, ipif);
1230*432Svi117747 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1231*432Svi117747 			/*
1232*432Svi117747 			 * We need to either delete or skip loopback/linklocal
1233*432Svi117747 			 * or unsupported addresses.
1234*432Svi117747 			 */
1235*432Svi117747 			if ((ll_lb && del_ll_lb) || (unsupp_af && modify)) {
1236*432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1237*432Svi117747 					sctp->sctp_bound_to_all = 0;
1238*432Svi117747 				if (scanned < nsaddr) {
1239*432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1240*432Svi117747 					    sctp_ipif_list, obj);
1241*432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1242*432Svi117747 					continue;
1243*432Svi117747 				}
1244*432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1245*432Svi117747 				goto next_addr;
1246*432Svi117747 			} else if (ll_lb || unsupp_af) {
1247252Svi117747 				goto next_addr;
12480Sstevel@tonic-gate 			}
1249*432Svi117747 
1250*432Svi117747 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1251*432Svi117747 				goto next_addr;
1252252Svi117747 			if (p != NULL)
1253252Svi117747 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
12540Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1255*432Svi117747 			if (!ipif->sctp_ipif_isv6) {
12560Sstevel@tonic-gate 				struct in_addr	*v4;
12570Sstevel@tonic-gate 
1258252Svi117747 				if (p != NULL) {
1259252Svi117747 					hdr->sph_type = htons(PARM_ADDR4);
1260252Svi117747 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1261252Svi117747 					v4 = (struct in_addr *)(hdr + 1);
1262252Svi117747 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1263252Svi117747 				}
1264252Svi117747 				paramlen += PARM_ADDR4_LEN;
1265*432Svi117747 			} else {
1266252Svi117747 				if (p != NULL) {
1267252Svi117747 					hdr->sph_type = htons(PARM_ADDR6);
1268252Svi117747 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1269252Svi117747 					bcopy(&addr, hdr + 1, sizeof (addr));
1270252Svi117747 				}
1271252Svi117747 				paramlen += PARM_ADDR6_LEN;
12720Sstevel@tonic-gate 			}
1273252Svi117747 next_addr:
1274*432Svi117747 			if (scanned >= nsaddr)
1275252Svi117747 				return (paramlen);
12760Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
12770Sstevel@tonic-gate 			    obj);
12780Sstevel@tonic-gate 		}
12790Sstevel@tonic-gate 	}
1280252Svi117747 	return (paramlen);
12810Sstevel@tonic-gate }
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
12850Sstevel@tonic-gate void
12860Sstevel@tonic-gate sctp_saddr_init()
12870Sstevel@tonic-gate {
12880Sstevel@tonic-gate 	int	i;
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	rw_init(&sctp_g_ills_lock, NULL, RW_DEFAULT, NULL);
12910Sstevel@tonic-gate 	rw_init(&sctp_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
12940Sstevel@tonic-gate 		sctp_g_ills[i].ill_count = 0;
12950Sstevel@tonic-gate 		list_create(&sctp_g_ills[i].sctp_ill_list, sizeof (sctp_ill_t),
12960Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
12970Sstevel@tonic-gate 	}
12980Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
12990Sstevel@tonic-gate 		sctp_g_ipifs[i].ipif_count = 0;
13000Sstevel@tonic-gate 		list_create(&sctp_g_ipifs[i].sctp_ipif_list,
13010Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate void
13060Sstevel@tonic-gate sctp_saddr_fini()
13070Sstevel@tonic-gate {
13080Sstevel@tonic-gate 	int	i;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	rw_destroy(&sctp_g_ills_lock);
13110Sstevel@tonic-gate 	rw_destroy(&sctp_g_ipifs_lock);
13120Sstevel@tonic-gate 	ASSERT(sctp_ills_count == 0 && sctp_g_ipifs_count == 0);
13130Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
13140Sstevel@tonic-gate 		list_destroy(&sctp_g_ills[i].sctp_ill_list);
13150Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
13160Sstevel@tonic-gate 		list_destroy(&sctp_g_ipifs[i].sctp_ipif_list);
13170Sstevel@tonic-gate }
1318