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 /*
221676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/ddi.h>
320Sstevel@tonic-gate #include <sys/sunddi.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/list.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <netinet/in.h>
390Sstevel@tonic-gate #include <netinet/ip6.h>
400Sstevel@tonic-gate #include <netinet/sctp.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <inet/common.h>
430Sstevel@tonic-gate #include <inet/ip.h>
440Sstevel@tonic-gate #include <inet/ip6.h>
450Sstevel@tonic-gate #include <inet/ip_if.h>
460Sstevel@tonic-gate #include <inet/ipclassifier.h>
470Sstevel@tonic-gate #include <inet/sctp_ip.h>
480Sstevel@tonic-gate #include "sctp_impl.h"
490Sstevel@tonic-gate #include "sctp_addr.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
520Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
53*2263Ssommerfe 			    sctp_t *, uint_t);
540Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
55852Svi117747 int			sctp_valid_addr_list(sctp_t *, const void *, uint32_t,
56852Svi117747 			    uchar_t *, size_t);
570Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_ipif_lookup(sctp_t *, uint_t);
58432Svi117747 static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
59432Svi117747 			    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);
73852Svi117747 sctp_saddr_ipif_t	*sctp_saddr_lookup(sctp_t *, in6_addr_t *, uint_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();
78432Svi117747 
790Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
800Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
81432Svi117747 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
82432Svi117747 
83432Svi117747 #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
84432Svi117747 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
85432Svi117747 
86852Svi117747 #define	SCTP_IS_IPIF_LOOPBACK(ipif)		\
87852Svi117747 	((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK)
88852Svi117747 
89852Svi117747 #define	SCTP_IS_IPIF_LINKLOCAL(ipif)		\
90852Svi117747 	((ipif)->sctp_ipif_isv6 && 		\
91852Svi117747 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr))
92432Svi117747 
93432Svi117747 #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
94432Svi117747 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
95432Svi117747 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
960Sstevel@tonic-gate 
97*2263Ssommerfe #define	SCTP_IPIF_ZONE_MATCH(sctp, ipif) 				\
98*2263Ssommerfe 	IPCL_ZONE_MATCH((sctp)->sctp_connp, (ipif)->sctp_ipif_zoneid)
99*2263Ssommerfe 
1000Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
1010Sstevel@tonic-gate #define	SCTP_IPIF_HASH_FN(seqid)	((seqid) % SCTP_IPIF_HASH)
1020Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* Global list of SCTP ILLs */
1050Sstevel@tonic-gate sctp_ill_hash_t	sctp_g_ills[SCTP_ILL_HASH];
1060Sstevel@tonic-gate uint32_t	sctp_ills_count = 0;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /* Global list of SCTP IPIFs */
1090Sstevel@tonic-gate sctp_ipif_hash_t	sctp_g_ipifs[SCTP_IPIF_HASH];
110852Svi117747 uint32_t		sctp_g_ipifs_count = 0;
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1210Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1220Sstevel@tonic-gate  * Called with no locks held.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate static void
1250Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
1280Sstevel@tonic-gate 	uint_t		ipif_index;
1290Sstevel@tonic-gate 	uint_t		ill_index;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
1320Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(sctp_ipif->sctp_ipif_id);
1350Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1360Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1370Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1380Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1390Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
1400Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
1410Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
1420Sstevel@tonic-gate 		return;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	list_remove(&sctp_g_ipifs[ipif_index].sctp_ipif_list, sctp_ipif);
1450Sstevel@tonic-gate 	sctp_g_ipifs[ipif_index].ipif_count--;
1460Sstevel@tonic-gate 	sctp_g_ipifs_count--;
1470Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1480Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1510Sstevel@tonic-gate 	if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
1520Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
1530Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1540Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1550Sstevel@tonic-gate 			list_remove(&sctp_g_ills[ill_index].sctp_ill_list,
1560Sstevel@tonic-gate 			    (void *)sctp_ill);
1570Sstevel@tonic-gate 			sctp_g_ills[ill_index].ill_count--;
1580Sstevel@tonic-gate 			sctp_ills_count--;
1590Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1600Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1610Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
1650Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
1700Sstevel@tonic-gate  * Called with no locks held.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate static sctp_ipif_t *
173*2263Ssommerfe sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, sctp_t *sctp,
174852Svi117747     uint_t ifindex)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	int		i;
1770Sstevel@tonic-gate 	int		j;
1780Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
1790Sstevel@tonic-gate 
180*2263Ssommerfe 	ASSERT(sctp->sctp_zoneid != ALL_ZONES);
1810Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
1820Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1830Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
1840Sstevel@tonic-gate 			continue;
1850Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
1860Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
1870Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
188*2263Ssommerfe 			if (SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) &&
1890Sstevel@tonic-gate 			    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) &&
190852Svi117747 			    (ifindex == 0 || ifindex ==
191852Svi117747 			    sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
1920Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1930Sstevel@tonic-gate 			    addr)) {
1940Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1950Sstevel@tonic-gate 				if (refhold)
1960Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
1970Sstevel@tonic-gate 				rw_exit(&sctp_g_ipifs_lock);
1980Sstevel@tonic-gate 				return (sctp_ipif);
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2010Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
2020Sstevel@tonic-gate 			    sctp_ipif);
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2060Sstevel@tonic-gate 	return (NULL);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
2110Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
2120Sstevel@tonic-gate  * Called with no locks held.
2130Sstevel@tonic-gate  */
2140Sstevel@tonic-gate static int
2150Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
2180Sstevel@tonic-gate 	int			i;
2190Sstevel@tonic-gate 	int			j;
2200Sstevel@tonic-gate 	int			error = 0;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
2230Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
2240Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
2250Sstevel@tonic-gate 			continue;
2260Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
2270Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
2280Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
229432Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
2300Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
231*2263Ssommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
2320Sstevel@tonic-gate 			    (sctp->sctp_ipversion == IPV4_VERSION &&
233432Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
2340Sstevel@tonic-gate 			    (sctp->sctp_connp->conn_ipv6_v6only &&
235432Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
2360Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2370Sstevel@tonic-gate 				sctp_ipif = list_next(
2380Sstevel@tonic-gate 				    &sctp_g_ipifs[i].sctp_ipif_list, sctp_ipif);
2390Sstevel@tonic-gate 				continue;
2400Sstevel@tonic-gate 			}
2410Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2420Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
243432Svi117747 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
244432Svi117747 			    B_FALSE);
2450Sstevel@tonic-gate 			if (error != 0)
2460Sstevel@tonic-gate 				goto free_stuff;
2470Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
2480Sstevel@tonic-gate 			    sctp_ipif);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2520Sstevel@tonic-gate 	return (0);
2530Sstevel@tonic-gate free_stuff:
2540Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2550Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2560Sstevel@tonic-gate 	return (ENOMEM);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2610Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
262852Svi117747  * or error. If the caller wants the list of addresses, it sends a pre-allocated
263852Svi117747  * buffer - list. Currently, this list is only used on a clustered node when
264852Svi117747  * the SCTP is in the listen state (from sctp_bind_add()). When called on a
265852Svi117747  * clustered node, the input is always a list of addresses (even if the
266852Svi117747  * original bind() was to INADDR_ANY).
2670Sstevel@tonic-gate  * Called with no locks held.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate int
270852Svi117747 sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
271852Svi117747     uchar_t *list, size_t lsize)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2740Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2750Sstevel@tonic-gate 	struct in_addr		*addr4;
2760Sstevel@tonic-gate 	in6_addr_t		addr;
2770Sstevel@tonic-gate 	int			cnt;
2780Sstevel@tonic-gate 	int			err = 0;
2790Sstevel@tonic-gate 	int			saddr_cnt = 0;
2800Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
2810Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
2820Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
2830Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
284852Svi117747 	uchar_t			*p = list;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
2880Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
2890Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
2900Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
2910Sstevel@tonic-gate 	 * addresses.
2920Sstevel@tonic-gate 	 */
2930Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
2940Sstevel@tonic-gate 		check_lport = B_TRUE;
2950Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
2960Sstevel@tonic-gate 			check_addrs = B_TRUE;
2970Sstevel@tonic-gate 	}
298852Svi117747 
2990Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3000Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
3010Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3020Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
3030Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
3040Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
305852Svi117747 		uint_t		ifindex = 0;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		switch (sctp->sctp_family) {
3080Sstevel@tonic-gate 		case AF_INET:
3090Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
3100Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
3110Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
3120Sstevel@tonic-gate 				err = EINVAL;
3130Sstevel@tonic-gate 				goto free_ret;
3140Sstevel@tonic-gate 			}
3150Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
3160Sstevel@tonic-gate 			if (check_addrs &&
3170Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
3180Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
3190Sstevel@tonic-gate 			    IN_MULTICAST(addr4->s_addr))) {
3200Sstevel@tonic-gate 				err = EINVAL;
3210Sstevel@tonic-gate 				goto free_ret;
3220Sstevel@tonic-gate 			}
3230Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
3240Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
3250Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3260Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3270Sstevel@tonic-gate 			}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 			break;
3300Sstevel@tonic-gate 		case AF_INET6:
3310Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3320Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
3330Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
3340Sstevel@tonic-gate 				err = EINVAL;
3350Sstevel@tonic-gate 				goto free_ret;
3360Sstevel@tonic-gate 			}
3370Sstevel@tonic-gate 			addr = sin6->sin6_addr;
338852Svi117747 			/* Contains the interface index */
339852Svi117747 			ifindex = sin6->sin6_scope_id;
3400Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
3410Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3420Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3430Sstevel@tonic-gate 				goto free_ret;
3440Sstevel@tonic-gate 			}
3450Sstevel@tonic-gate 			if (check_addrs &&
3460Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3470Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3480Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3490Sstevel@tonic-gate 				err = EINVAL;
3500Sstevel@tonic-gate 				goto free_ret;
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3530Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3540Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3550Sstevel@tonic-gate 			}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 			break;
3580Sstevel@tonic-gate 		default:
3590Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3600Sstevel@tonic-gate 			goto free_ret;
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 		if (lookup_saddr) {
363*2263Ssommerfe 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE, sctp,
364*2263Ssommerfe 			    ifindex);
3650Sstevel@tonic-gate 			if (ipif == NULL) {
3660Sstevel@tonic-gate 				/* Address not in the list */
3670Sstevel@tonic-gate 				err = EINVAL;
3680Sstevel@tonic-gate 				goto free_ret;
369852Svi117747 			} else if (check_addrs && SCTP_IS_IPIF_LOOPBACK(ipif) &&
370852Svi117747 			    cl_sctp_check_addrs == NULL) {
3710Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3720Sstevel@tonic-gate 				err = EINVAL;
3730Sstevel@tonic-gate 				goto free_ret;
3740Sstevel@tonic-gate 			}
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		if (!bind_to_all) {
377432Svi117747 			/*
378432Svi117747 			 * If an address is added after association setup,
379432Svi117747 			 * we need to wait for the peer to send us an ASCONF
380432Svi117747 			 * ACK before we can start using it.
381432Svi117747 			 * saddr_ipif_dontsrc will be reset (to 0) when we
382432Svi117747 			 * get the ASCONF ACK for this address.
383432Svi117747 			 */
384432Svi117747 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
385432Svi117747 			    check_addrs ? B_TRUE : B_FALSE);
3860Sstevel@tonic-gate 			if (err != 0) {
3870Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3880Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
3890Sstevel@tonic-gate 					err = EADDRINUSE;
3900Sstevel@tonic-gate 				goto free_ret;
3910Sstevel@tonic-gate 			}
3920Sstevel@tonic-gate 			saddr_cnt++;
393852Svi117747 			if (lsize >= sizeof (addr)) {
394852Svi117747 				bcopy(&addr, p, sizeof (addr));
395852Svi117747 				p += sizeof (addr);
396852Svi117747 				lsize -= sizeof (addr);
397852Svi117747 			}
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	if (bind_to_all) {
4010Sstevel@tonic-gate 		/*
4020Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
4030Sstevel@tonic-gate 		 * inaddr_any.
4040Sstevel@tonic-gate 		 */
4050Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
4060Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
4070Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
4100Sstevel@tonic-gate 		if (err != 0)
4110Sstevel@tonic-gate 			return (err);
4120Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4150Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4160Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4170Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4180Sstevel@tonic-gate 	return (0);
4190Sstevel@tonic-gate free_ret:
4200Sstevel@tonic-gate 	if (saddr_cnt != 0)
4210Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
4220Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4230Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4240Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4250Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4260Sstevel@tonic-gate 	return (err);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate sctp_saddr_ipif_t *
4300Sstevel@tonic-gate sctp_ipif_lookup(sctp_t *sctp, uint_t ipif_index)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	int			cnt;
4330Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif_index);
4340Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (sctp->sctp_saddrs[seqid].ipif_count == 0)
4370Sstevel@tonic-gate 		return (NULL);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4400Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4410Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif_index)
4420Sstevel@tonic-gate 			return (ipif_obj);
4430Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4440Sstevel@tonic-gate 		    ipif_obj);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	return (NULL);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate static int
450432Svi117747 sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
451432Svi117747     boolean_t dontsrc)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	int			cnt;
4540Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4550Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4580Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4590Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id)
4600Sstevel@tonic-gate 			return (EALREADY);
4610Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4620Sstevel@tonic-gate 		    ipif_obj);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4650Sstevel@tonic-gate 	if (ipif_obj == NULL) {
4660Sstevel@tonic-gate 		/* Need to do something */
4670Sstevel@tonic-gate 		return (ENOMEM);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
470432Svi117747 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
4710Sstevel@tonic-gate 	list_insert_tail(&sctp->sctp_saddrs[seqid].sctp_ipif_list, ipif_obj);
4720Sstevel@tonic-gate 	sctp->sctp_saddrs[seqid].ipif_count++;
4730Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
4740Sstevel@tonic-gate 	return (0);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate static void
4780Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	int			cnt;
4810Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4820Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4850Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4860Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) {
4870Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4880Sstevel@tonic-gate 			    ipif_obj);
4890Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
4900Sstevel@tonic-gate 			sctp->sctp_saddrs[seqid].ipif_count--;
4910Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
4920Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
4930Sstevel@tonic-gate 			break;
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4960Sstevel@tonic-gate 		    ipif_obj);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate static int
5010Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate 	int			i;
5040Sstevel@tonic-gate 	int			j;
5050Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
5060Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
5070Sstevel@tonic-gate 	int			overlap = 0;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
5100Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5110Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
5120Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
5130Sstevel@tonic-gate 			if (obj1->saddr_ipifp->sctp_ipif_id ==
5140Sstevel@tonic-gate 			    obj2->saddr_ipifp->sctp_ipif_id) {
5150Sstevel@tonic-gate 				overlap++;
5160Sstevel@tonic-gate 				break;
5170Sstevel@tonic-gate 			}
5180Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
5190Sstevel@tonic-gate 			    obj2);
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 	return (overlap);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate int
5270Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	int		i;
5300Sstevel@tonic-gate 	int		overlap = 0;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5330Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
5340Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5380Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5390Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
5430Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (overlap > 0)
5460Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate static int
5520Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	int			i;
5550Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5560Sstevel@tonic-gate 	int			error = 0;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
5590Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5600Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
561432Svi117747 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
562432Svi117747 		    B_FALSE);
5630Sstevel@tonic-gate 		if (error != 0)
5640Sstevel@tonic-gate 			return (error);
5650Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 	return (error);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate int
5710Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	int	error = 0;
5740Sstevel@tonic-gate 	int	i;
5750Sstevel@tonic-gate 
576432Svi117747 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
5770Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5800Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
5810Sstevel@tonic-gate 			continue;
5820Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
5830Sstevel@tonic-gate 		if (error != 0) {
5840Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
5850Sstevel@tonic-gate 			return (error);
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 	return (0);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate void
5920Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 	int			i;
5950Sstevel@tonic-gate 	int			l;
5960Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
5990Sstevel@tonic-gate 		return;
6000Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
6010Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
6020Sstevel@tonic-gate 			continue;
6030Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6040Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
6050Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
6060Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
6070Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
6080Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
6090Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
6120Sstevel@tonic-gate 	}
613432Svi117747 	if (sctp->sctp_bound_to_all == 1)
614432Svi117747 		sctp->sctp_bound_to_all = 0;
6150Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate /*
6190Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
6200Sstevel@tonic-gate  * held.
6210Sstevel@tonic-gate  */
6220Sstevel@tonic-gate void
6230Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
6240Sstevel@tonic-gate {
6250Sstevel@tonic-gate 	int		i;
6260Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
6270Sstevel@tonic-gate 	uint_t		index;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	ip2dbg(("sctp_update_ill: %s\n", ill->ill_name));
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_WRITER);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
6340Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
6350Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
6360Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
6370Sstevel@tonic-gate 			break;
6380Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
6390Sstevel@tonic-gate 		    sctp_ill);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	switch (op) {
6430Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
6440Sstevel@tonic-gate 		if (sctp_ill != NULL) {
6450Sstevel@tonic-gate 			/* Unmark it if it is condemned */
6460Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
6470Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
6480Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6490Sstevel@tonic-gate 			return;
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
6520Sstevel@tonic-gate 		/* Need to re-try? */
6530Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6540Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6550Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6560Sstevel@tonic-gate 			return;
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		sctp_ill->sctp_ill_name =
6590Sstevel@tonic-gate 		    kmem_zalloc(ill->ill_name_length, KM_NOSLEEP);
6600Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
6610Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6620Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6630Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6640Sstevel@tonic-gate 			return;
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
6670Sstevel@tonic-gate 		    ill->ill_name_length);
6680Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
6690Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
6700Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
6710Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ills[index].sctp_ill_list,
6720Sstevel@tonic-gate 		    (void *)sctp_ill);
6730Sstevel@tonic-gate 		sctp_g_ills[index].ill_count++;
6740Sstevel@tonic-gate 		sctp_ills_count++;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		break;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6810Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6820Sstevel@tonic-gate 			return;
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
6850Sstevel@tonic-gate 			list_remove(&sctp_g_ills[index].sctp_ill_list,
6860Sstevel@tonic-gate 			    (void *)sctp_ill);
6870Sstevel@tonic-gate 			sctp_g_ills[index].ill_count--;
6880Sstevel@tonic-gate 			sctp_ills_count--;
6890Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
6900Sstevel@tonic-gate 			    ill->ill_name_length);
6910Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6920Sstevel@tonic-gate 		} else {
6930Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		break;
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
7020Sstevel@tonic-gate void
7030Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
7060Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
7070Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7080Sstevel@tonic-gate 	uint_t		index;
7090Sstevel@tonic-gate 	int		i;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
7120Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
7150Sstevel@tonic-gate 	fsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
7160Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
7170Sstevel@tonic-gate 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill))
7180Sstevel@tonic-gate 			break;
7190Sstevel@tonic-gate 		fsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
7200Sstevel@tonic-gate 		    fsctp_ill);
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
7240Sstevel@tonic-gate 	tsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
7250Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
7260Sstevel@tonic-gate 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill))
7270Sstevel@tonic-gate 			break;
7280Sstevel@tonic-gate 		tsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
7290Sstevel@tonic-gate 		    tsctp_ill);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7330Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[index].sctp_ipif_list);
7340Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[index].ipif_count; i++) {
7350Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7360Sstevel@tonic-gate 			break;
7370Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[index].sctp_ipif_list,
7380Sstevel@tonic-gate 		    sctp_ipif);
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 	/* Should be an ASSERT? */
7410Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
7420Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
7430Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
7440Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7450Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7460Sstevel@tonic-gate 		return;
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
7490Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
7500Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
7510Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
7520Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
7530Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
7540Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
7550Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
7590Sstevel@tonic-gate void
7600Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
7630Sstevel@tonic-gate 	int		i;
7640Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
7650Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7660Sstevel@tonic-gate 	uint_t		ill_index;
7670Sstevel@tonic-gate 	uint_t		ipif_index;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
7720Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
7750Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[ill_index].sctp_ill_list);
7760Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[ill_index].ill_count; i++) {
7770Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
7780Sstevel@tonic-gate 			break;
7790Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[ill_index].sctp_ill_list,
7800Sstevel@tonic-gate 		    sctp_ill);
7810Sstevel@tonic-gate 	}
7820Sstevel@tonic-gate 	if (sctp_ill == NULL) {
7830Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7840Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7850Sstevel@tonic-gate 		return;
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7890Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[ipif_index].sctp_ipif_list);
7900Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[ipif_index].ipif_count; i++) {
7910Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7920Sstevel@tonic-gate 			break;
7930Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
7940Sstevel@tonic-gate 		    sctp_ipif);
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 	if (op != SCTP_IPIF_INSERT && sctp_ipif == NULL) {
7970Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
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 #ifdef	DEBUG
8030Sstevel@tonic-gate 	if (sctp_ipif != NULL)
8040Sstevel@tonic-gate 		ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
8050Sstevel@tonic-gate #endif
8060Sstevel@tonic-gate 	switch (op) {
8070Sstevel@tonic-gate 	case SCTP_IPIF_INSERT:
8080Sstevel@tonic-gate 		if (sctp_ipif != NULL) {
8090Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED)
810252Svi117747 				sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
8110Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8120Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8130Sstevel@tonic-gate 			return;
8140Sstevel@tonic-gate 		}
8150Sstevel@tonic-gate 		sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
8160Sstevel@tonic-gate 		/* Try again? */
8170Sstevel@tonic-gate 		if (sctp_ipif == NULL) {
8180Sstevel@tonic-gate 			ip1dbg(("sctp_ipif_insert: mem failure..\n"));
8190Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8200Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8210Sstevel@tonic-gate 			return;
8220Sstevel@tonic-gate 		}
8230Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
8240Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_ill = sctp_ill;
825252Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
8260Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8270Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
8280Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
829432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8300Sstevel@tonic-gate 		rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
8310Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
8320Sstevel@tonic-gate 		    (void *)sctp_ipif);
8330Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count++;
8340Sstevel@tonic-gate 		sctp_g_ipifs_count++;
8350Sstevel@tonic-gate 		atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 		break;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
8400Sstevel@tonic-gate 	{
8410Sstevel@tonic-gate 		list_t		*ipif_list;
8420Sstevel@tonic-gate 		list_t		*ill_list;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		ill_list = &sctp_g_ills[ill_index].sctp_ill_list;
8450Sstevel@tonic-gate 		ipif_list = &sctp_g_ipifs[ipif_index].sctp_ipif_list;
8460Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
8470Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
8480Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8490Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8500Sstevel@tonic-gate 			return;
8510Sstevel@tonic-gate 		}
8520Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
8530Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count--;
8540Sstevel@tonic-gate 		sctp_g_ipifs_count--;
8550Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
8560Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
8570Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
8580Sstevel@tonic-gate 		if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
8590Sstevel@tonic-gate 			rw_downgrade(&sctp_g_ipifs_lock);
8600Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
8610Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
8620Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
8630Sstevel@tonic-gate 				sctp_ills_count--;
8640Sstevel@tonic-gate 				sctp_g_ills[ill_index].ill_count--;
8650Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
8660Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
8670Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
8680Sstevel@tonic-gate 			}
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 		break;
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	case SCTP_IPIF_UP:
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8760Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8770Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
8780Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
879432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
880432Svi117747 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8810Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		break;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8880Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8890Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8900Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
8910Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
892432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8930Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		break;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
9000Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
9010Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
9020Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 		break;
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
9070Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate /*
9110Sstevel@tonic-gate  *
9120Sstevel@tonic-gate  *
9130Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
9140Sstevel@tonic-gate  * sctp locking by the caller.
9150Sstevel@tonic-gate  *
9160Sstevel@tonic-gate  *
9170Sstevel@tonic-gate  */
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate /* Remove a specific saddr from the list */
9200Sstevel@tonic-gate void
9210Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
9220Sstevel@tonic-gate {
9230Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9240Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9270Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
9300Sstevel@tonic-gate 
931432Svi117747 	if (sctp->sctp_bound_to_all == 1)
9320Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9350Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9380Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate /*
9420Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
9430Sstevel@tonic-gate  * Called with no locks held.
9440Sstevel@tonic-gate  */
9450Sstevel@tonic-gate void
9460Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
9470Sstevel@tonic-gate     boolean_t fanout_locked)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
9500Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
9510Sstevel@tonic-gate 	int			cnt;
9520Sstevel@tonic-gate 	in6_addr_t		addr;
9530Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
954852Svi117747 	int			ifindex = 0;
9550Sstevel@tonic-gate 
956852Svi117747 	ASSERT(sctp->sctp_nsaddrs >= addcnt);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	if (!fanout_locked) {
9590Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9600Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9610Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9620Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
9660Sstevel@tonic-gate 		switch (sctp->sctp_family) {
9670Sstevel@tonic-gate 		case AF_INET:
9680Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
9690Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
9700Sstevel@tonic-gate 			break;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		case AF_INET6:
9730Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
9740Sstevel@tonic-gate 			addr = sin6->sin6_addr;
975852Svi117747 			ifindex = sin6->sin6_scope_id;
9760Sstevel@tonic-gate 			break;
9770Sstevel@tonic-gate 		}
978*2263Ssommerfe 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE, sctp,
979*2263Ssommerfe 		    ifindex);
9800Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
9810Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
9820Sstevel@tonic-gate 	}
983432Svi117747 	if (sctp->sctp_bound_to_all == 1)
9840Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	if (!fanout_locked) {
9870Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9880Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9890Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9900Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /*
9950Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
9960Sstevel@tonic-gate  * Called with no locks held.
9970Sstevel@tonic-gate  */
9980Sstevel@tonic-gate sctp_saddr_ipif_t *
999852Svi117747 sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	sctp_saddr_ipif_t	*saddr_ipifs;
10020Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
10030Sstevel@tonic-gate 
1004*2263Ssommerfe 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_FALSE, sctp, ifindex);
10050Sstevel@tonic-gate 	if (sctp_ipif == NULL)
10060Sstevel@tonic-gate 		return (NULL);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	saddr_ipifs = sctp_ipif_lookup(sctp, sctp_ipif->sctp_ipif_id);
10090Sstevel@tonic-gate 	return (saddr_ipifs);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate 
1012432Svi117747 /* Given an address, add it to the source address list */
1013432Svi117747 int
1014852Svi117747 sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1015432Svi117747 {
1016432Svi117747 	sctp_ipif_t		*sctp_ipif;
1017432Svi117747 
1018*2263Ssommerfe 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp, ifindex);
1019432Svi117747 	if (sctp_ipif == NULL)
1020432Svi117747 		return (EINVAL);
1021432Svi117747 
1022432Svi117747 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE) != 0) {
1023432Svi117747 		SCTP_IPIF_REFRELE(sctp_ipif);
1024432Svi117747 		return (EINVAL);
1025432Svi117747 	}
1026432Svi117747 	return (0);
1027432Svi117747 }
1028432Svi117747 
1029432Svi117747 /*
1030432Svi117747  * Remove or mark as dontsrc addresses that are currently not part of the
1031432Svi117747  * association. One would delete addresses when processing an INIT and
1032432Svi117747  * mark as dontsrc when processing an INIT-ACK.
1033432Svi117747  */
1034432Svi117747 void
1035432Svi117747 sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete)
1036432Svi117747 {
1037432Svi117747 	int			i;
1038432Svi117747 	int			l;
1039432Svi117747 	sctp_saddr_ipif_t	*obj;
1040432Svi117747 	int			scanned = 0;
1041432Svi117747 	int			naddr;
1042432Svi117747 	int			nsaddr;
1043432Svi117747 
1044432Svi117747 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1045432Svi117747 
1046432Svi117747 	/*
1047432Svi117747 	 * Irregardless of the supported address in the INIT, v4
1048432Svi117747 	 * must be supported.
1049432Svi117747 	 */
1050432Svi117747 	if (sctp->sctp_family == AF_INET)
1051432Svi117747 		supp_af = PARM_SUPP_V4;
1052432Svi117747 
1053432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
1054432Svi117747 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1055432Svi117747 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1056432Svi117747 			continue;
1057432Svi117747 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1058432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1059432Svi117747 		for (l = 0; l < naddr; l++) {
1060432Svi117747 			sctp_ipif_t	*ipif;
1061432Svi117747 
1062432Svi117747 			ipif = obj->saddr_ipifp;
1063432Svi117747 			scanned++;
1064432Svi117747 
1065432Svi117747 			/*
1066432Svi117747 			 * Delete/mark dontsrc loopback/linklocal addresses and
1067432Svi117747 			 * unsupported address.
1068852Svi117747 			 * On a clustered node, we trust the clustering module
1069852Svi117747 			 * to do the right thing w.r.t loopback addresses, so
1070852Svi117747 			 * we ignore loopback addresses in this check.
1071432Svi117747 			 */
1072852Svi117747 			if ((SCTP_IS_IPIF_LOOPBACK(ipif) &&
1073852Svi117747 			    cl_sctp_check_addrs == NULL) ||
1074852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(ipif) ||
1075432Svi117747 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1076432Svi117747 				if (!delete) {
1077432Svi117747 					obj->saddr_ipif_unconfirmed = 1;
1078432Svi117747 					goto next_obj;
1079432Svi117747 				}
1080432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1081432Svi117747 					sctp->sctp_bound_to_all = 0;
1082432Svi117747 				if (scanned < nsaddr) {
1083432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1084432Svi117747 					    sctp_ipif_list, obj);
1085432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1086432Svi117747 					continue;
1087432Svi117747 				}
1088432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1089432Svi117747 			}
1090432Svi117747 	next_obj:
1091432Svi117747 			if (scanned >= nsaddr)
1092432Svi117747 				return;
1093432Svi117747 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1094432Svi117747 			    obj);
1095432Svi117747 		}
1096432Svi117747 	}
1097432Svi117747 }
1098432Svi117747 
1099432Svi117747 
11000Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
11010Sstevel@tonic-gate in6_addr_t
11020Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6)
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate 	int			i;
11050Sstevel@tonic-gate 	int			l;
11060Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
11070Sstevel@tonic-gate 	int			scanned = 0;
11080Sstevel@tonic-gate 	in6_addr_t		addr;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11110Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11120Sstevel@tonic-gate 			continue;
11130Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11140Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11150Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1118432Svi117747 			if (!SCTP_DONT_SRC(obj) &&
11190Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
1120432Svi117747 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
11210Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
11220Sstevel@tonic-gate 			}
11230Sstevel@tonic-gate 			scanned++;
11240Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
11250Sstevel@tonic-gate 				goto got_none;
11260Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
11270Sstevel@tonic-gate 			    obj);
11280Sstevel@tonic-gate 		}
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate got_none:
11310Sstevel@tonic-gate 	/* Need to double check this */
11320Sstevel@tonic-gate 	if (isv6 == B_TRUE)
11330Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
11340Sstevel@tonic-gate 	else
11350Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	return (addr);
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate /*
11410Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
11420Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
11430Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
11440Sstevel@tonic-gate  */
11450Sstevel@tonic-gate int
11460Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
11470Sstevel@tonic-gate {
11480Sstevel@tonic-gate 	int			i;
11490Sstevel@tonic-gate 	int			l;
11500Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
11510Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
11520Sstevel@tonic-gate 	int			family = sctp->sctp_family;
11530Sstevel@tonic-gate 	int			max = *addrcnt;
11540Sstevel@tonic-gate 	size_t			added = 0;
11550Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
11560Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
11570Sstevel@tonic-gate 	int			scanned = 0;
11580Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
11610Sstevel@tonic-gate 		return (EINVAL);
11620Sstevel@tonic-gate 
1163852Svi117747 	/*
1164852Svi117747 	 * Skip loopback addresses for non-loopback assoc., ignore
1165852Svi117747 	 * this on a clustered node.
1166852Svi117747 	 */
1167852Svi117747 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback &&
1168852Svi117747 	    (cl_sctp_check_addrs == NULL)) {
11690Sstevel@tonic-gate 		skip_lback = B_TRUE;
1170852Svi117747 	}
1171852Svi117747 
11720Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11730Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11740Sstevel@tonic-gate 			continue;
11750Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11760Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11770Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
11780Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 			scanned++;
11810Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1182432Svi117747 			    SCTP_DONT_SRC(obj) ||
1183852Svi117747 			    (SCTP_IS_IPIF_LOOPBACK(ipif) && skip_lback)) {
11840Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
11850Sstevel@tonic-gate 					goto done;
11860Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
11870Sstevel@tonic-gate 				    sctp_ipif_list, obj);
11880Sstevel@tonic-gate 				continue;
11890Sstevel@tonic-gate 			}
11900Sstevel@tonic-gate 			switch (family) {
11910Sstevel@tonic-gate 			case AF_INET:
11920Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
11930Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
11940Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
11950Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
11960Sstevel@tonic-gate 				break;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 			case AF_INET6:
11990Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
12000Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
12010Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
12020Sstevel@tonic-gate 				sin6->sin6_addr = addr;
12030Sstevel@tonic-gate 				break;
12040Sstevel@tonic-gate 			}
12050Sstevel@tonic-gate 			added++;
12060Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
12070Sstevel@tonic-gate 				goto done;
12080Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
12090Sstevel@tonic-gate 			    obj);
12100Sstevel@tonic-gate 		}
12110Sstevel@tonic-gate 	}
12120Sstevel@tonic-gate done:
12130Sstevel@tonic-gate 	*addrcnt = added;
12140Sstevel@tonic-gate 	return (0);
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate /*
1218252Svi117747  * Given the supported address family, walk through the source address list
1219252Svi117747  * and return the total length of the available addresses. If 'p' is not
1220252Svi117747  * null, construct the parameter list for the addresses in 'p'.
1221432Svi117747  * 'modify' will only be set when we want the source address list to
1222432Svi117747  * be modified. The source address list will be modified only when
1223432Svi117747  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1224432Svi117747  * be false since the 'sctp' will be that of the listener.
12250Sstevel@tonic-gate  */
12260Sstevel@tonic-gate size_t
1227432Svi117747 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate 	int			i;
12300Sstevel@tonic-gate 	int			l;
12310Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1232252Svi117747 	size_t			paramlen = 0;
12330Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
12340Sstevel@tonic-gate 	int			scanned = 0;
1235432Svi117747 	int			naddr;
1236432Svi117747 	int			nsaddr;
1237852Svi117747 	boolean_t		del_ll = B_FALSE;
1238852Svi117747 	boolean_t		del_lb = B_FALSE;
1239852Svi117747 
12400Sstevel@tonic-gate 
1241852Svi117747 	/*
1242852Svi117747 	 * On a clustered node don't bother changing anything
1243852Svi117747 	 * on the loopback interface.
1244852Svi117747 	 */
1245852Svi117747 	if (modify && !sctp->sctp_loopback && (cl_sctp_check_addrs == NULL))
1246852Svi117747 		del_lb = B_TRUE;
1247852Svi117747 
1248852Svi117747 	if (modify && !sctp->sctp_linklocal)
1249852Svi117747 		del_ll = B_TRUE;
1250432Svi117747 
1251432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
12520Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
12530Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
12540Sstevel@tonic-gate 			continue;
12550Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1256432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1257432Svi117747 		for (l = 0; l < naddr; l++) {
12580Sstevel@tonic-gate 			in6_addr_t	addr;
12590Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
1260852Svi117747 			boolean_t	ipif_lb;
1261852Svi117747 			boolean_t	ipif_ll;
1262432Svi117747 			boolean_t	unsupp_af;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
12650Sstevel@tonic-gate 			scanned++;
1266432Svi117747 
1267852Svi117747 			ipif_lb = SCTP_IS_IPIF_LOOPBACK(ipif);
1268852Svi117747 			ipif_ll = SCTP_IS_IPIF_LINKLOCAL(ipif);
1269432Svi117747 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1270432Svi117747 			/*
1271432Svi117747 			 * We need to either delete or skip loopback/linklocal
1272852Svi117747 			 * or unsupported addresses, if required.
1273432Svi117747 			 */
1274852Svi117747 			if ((ipif_ll && del_ll) || (ipif_lb && del_lb) ||
1275852Svi117747 			    (unsupp_af && modify)) {
1276432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1277432Svi117747 					sctp->sctp_bound_to_all = 0;
1278432Svi117747 				if (scanned < nsaddr) {
1279432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1280432Svi117747 					    sctp_ipif_list, obj);
1281432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1282432Svi117747 					continue;
1283432Svi117747 				}
1284432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1285432Svi117747 				goto next_addr;
1286852Svi117747 			} else if (ipif_ll || unsupp_af ||
1287852Svi117747 			    (ipif_lb && (cl_sctp_check_addrs == NULL))) {
1288252Svi117747 				goto next_addr;
12890Sstevel@tonic-gate 			}
1290432Svi117747 
1291432Svi117747 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1292432Svi117747 				goto next_addr;
1293252Svi117747 			if (p != NULL)
1294252Svi117747 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
12950Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1296432Svi117747 			if (!ipif->sctp_ipif_isv6) {
12970Sstevel@tonic-gate 				struct in_addr	*v4;
12980Sstevel@tonic-gate 
1299252Svi117747 				if (p != NULL) {
1300252Svi117747 					hdr->sph_type = htons(PARM_ADDR4);
1301252Svi117747 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1302252Svi117747 					v4 = (struct in_addr *)(hdr + 1);
1303252Svi117747 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1304252Svi117747 				}
1305252Svi117747 				paramlen += PARM_ADDR4_LEN;
1306432Svi117747 			} else {
1307252Svi117747 				if (p != NULL) {
1308252Svi117747 					hdr->sph_type = htons(PARM_ADDR6);
1309252Svi117747 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1310252Svi117747 					bcopy(&addr, hdr + 1, sizeof (addr));
1311252Svi117747 				}
1312252Svi117747 				paramlen += PARM_ADDR6_LEN;
13130Sstevel@tonic-gate 			}
1314252Svi117747 next_addr:
1315432Svi117747 			if (scanned >= nsaddr)
1316252Svi117747 				return (paramlen);
13170Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
13180Sstevel@tonic-gate 			    obj);
13190Sstevel@tonic-gate 		}
13200Sstevel@tonic-gate 	}
1321252Svi117747 	return (paramlen);
13220Sstevel@tonic-gate }
13230Sstevel@tonic-gate 
1324852Svi117747 /*
1325852Svi117747  * This is used on a clustered node to obtain a list of addresses, the list
1326852Svi117747  * consists of sockaddr_in structs for v4 and sockaddr_in6 for v6. The list
1327852Svi117747  * is then passed onto the clustering module which sends back the correct
1328852Svi117747  * list based on the port info. Regardless of the input, i.e INADDR_ANY
1329852Svi117747  * or specific address(es), we create the list since it could be modified by
1330852Svi117747  * the clustering module. When given a list of addresses, we simply
1331852Svi117747  * create the list of sockaddr_in or sockaddr_in6 structs using those
1332852Svi117747  * addresses. If there is an INADDR_ANY in the input list, or if the
1333852Svi117747  * input is INADDR_ANY, we create a list of sockaddr_in or sockaddr_in6
1334852Svi117747  * structs consisting all the addresses in the global interface list
1335852Svi117747  * except those that are hosted on the loopback interface. We create
1336852Svi117747  * a list of sockaddr_in[6] structs just so that it can be directly input
1337852Svi117747  * to sctp_valid_addr_list() once the clustering module has processed it.
1338852Svi117747  */
1339852Svi117747 int
1340852Svi117747 sctp_get_addrlist(sctp_t *sctp, const void *addrs, uint32_t *addrcnt,
1341852Svi117747     uchar_t **addrlist, int *uspec, size_t *size)
1342852Svi117747 {
1343852Svi117747 	int			cnt;
1344852Svi117747 	int			icnt;
1345852Svi117747 	sctp_ipif_t		*sctp_ipif;
1346852Svi117747 	struct sockaddr_in	*s4;
1347852Svi117747 	struct sockaddr_in6	*s6;
1348852Svi117747 	uchar_t			*p;
1349852Svi117747 	int			err = 0;
1350852Svi117747 
1351852Svi117747 	*addrlist = NULL;
1352852Svi117747 	*size = 0;
1353852Svi117747 
1354852Svi117747 	/*
1355852Svi117747 	 * Create a list of sockaddr_in[6] structs using the input list.
1356852Svi117747 	 */
1357852Svi117747 	if (sctp->sctp_family == AF_INET) {
1358852Svi117747 		*size = sizeof (struct sockaddr_in) * *addrcnt;
1359852Svi117747 		*addrlist = kmem_zalloc(*size,  KM_SLEEP);
1360852Svi117747 		p = *addrlist;
1361852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1362852Svi117747 			s4 = (struct sockaddr_in *)addrs + cnt;
1363852Svi117747 			/*
1364852Svi117747 			 * We need to create a list of all the available
1365852Svi117747 			 * addresses if there is an INADDR_ANY. However,
1366852Svi117747 			 * if we are beyond LISTEN, then this is invalid
1367852Svi117747 			 * (see sctp_valid_addr_list(). So, we just fail
1368852Svi117747 			 * it here rather than wait till it fails in
1369852Svi117747 			 * sctp_valid_addr_list().
1370852Svi117747 			 */
1371852Svi117747 			if (s4->sin_addr.s_addr == INADDR_ANY) {
1372852Svi117747 				kmem_free(*addrlist, *size);
1373852Svi117747 				*addrlist = NULL;
1374852Svi117747 				*size = 0;
1375852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1376852Svi117747 					*addrcnt = 0;
1377852Svi117747 					return (EINVAL);
1378852Svi117747 				}
1379852Svi117747 				if (uspec != NULL)
1380852Svi117747 					*uspec = 1;
1381852Svi117747 				goto get_all_addrs;
1382852Svi117747 			} else {
1383852Svi117747 				bcopy(s4, p, sizeof (*s4));
1384852Svi117747 				p += sizeof (*s4);
1385852Svi117747 			}
1386852Svi117747 		}
1387852Svi117747 	} else {
1388852Svi117747 		*size = sizeof (struct sockaddr_in6) * *addrcnt;
1389852Svi117747 		*addrlist = kmem_zalloc(*size, KM_SLEEP);
1390852Svi117747 		p = *addrlist;
1391852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1392852Svi117747 			s6 = (struct sockaddr_in6 *)addrs + cnt;
1393852Svi117747 			/*
1394852Svi117747 			 * Comments for INADDR_ANY, above, apply here too.
1395852Svi117747 			 */
1396852Svi117747 			if (IN6_IS_ADDR_UNSPECIFIED(&s6->sin6_addr)) {
1397852Svi117747 				kmem_free(*addrlist, *size);
1398852Svi117747 				*size = 0;
1399852Svi117747 				*addrlist = NULL;
1400852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1401852Svi117747 					*addrcnt = 0;
1402852Svi117747 					return (EINVAL);
1403852Svi117747 				}
1404852Svi117747 				if (uspec != NULL)
1405852Svi117747 					*uspec = 1;
1406852Svi117747 				goto get_all_addrs;
1407852Svi117747 			} else {
1408852Svi117747 				bcopy(addrs, p, sizeof (*s6));
1409852Svi117747 				p += sizeof (*s6);
1410852Svi117747 			}
1411852Svi117747 		}
1412852Svi117747 	}
1413852Svi117747 	return (err);
1414852Svi117747 get_all_addrs:
1415852Svi117747 
1416852Svi117747 	/*
1417852Svi117747 	 * Allocate max possible size. We allocate the max. size here because
1418852Svi117747 	 * the clustering module could end up adding addresses to the list.
1419852Svi117747 	 * We allocate upfront so that the clustering module need to bother
1420852Svi117747 	 * re-sizing the list.
1421852Svi117747 	 */
1422852Svi117747 	if (sctp->sctp_family == AF_INET)
1423852Svi117747 		*size = sizeof (struct sockaddr_in) * sctp_g_ipifs_count;
1424852Svi117747 	else
1425852Svi117747 		*size = sizeof (struct sockaddr_in6) * sctp_g_ipifs_count;
1426852Svi117747 
1427852Svi117747 	*addrlist = kmem_zalloc(*size, KM_SLEEP);
1428852Svi117747 	*addrcnt = 0;
1429852Svi117747 	p = *addrlist;
1430852Svi117747 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
1431852Svi117747 
1432852Svi117747 	/*
1433852Svi117747 	 * Walk through the global interface list and add all addresses,
1434852Svi117747 	 * except those that are hosted on loopback interfaces.
1435852Svi117747 	 */
1436852Svi117747 	for (cnt = 0; cnt <  SCTP_IPIF_HASH; cnt++) {
1437852Svi117747 		if (sctp_g_ipifs[cnt].ipif_count == 0)
1438852Svi117747 			continue;
1439852Svi117747 		sctp_ipif = list_head(&sctp_g_ipifs[cnt].sctp_ipif_list);
1440852Svi117747 		for (icnt = 0; icnt < sctp_g_ipifs[cnt].ipif_count; icnt++) {
1441852Svi117747 			in6_addr_t	addr;
1442852Svi117747 
1443852Svi117747 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1444852Svi117747 			addr = sctp_ipif->sctp_ipif_saddr;
1445852Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
1446852Svi117747 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
1447852Svi117747 			    SCTP_IS_IPIF_LOOPBACK(sctp_ipif) ||
1448852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(sctp_ipif) ||
1449*2263Ssommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
1450852Svi117747 			    (sctp->sctp_ipversion == IPV4_VERSION &&
1451852Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
1452852Svi117747 			    (sctp->sctp_connp->conn_ipv6_v6only &&
1453852Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
1454852Svi117747 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1455852Svi117747 				sctp_ipif = list_next(
1456852Svi117747 				    &sctp_g_ipifs[cnt].sctp_ipif_list,
1457852Svi117747 				    sctp_ipif);
1458852Svi117747 				continue;
1459852Svi117747 			}
1460852Svi117747 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1461852Svi117747 			if (sctp->sctp_family == AF_INET) {
1462852Svi117747 				s4 = (struct sockaddr_in *)p;
1463852Svi117747 				IN6_V4MAPPED_TO_INADDR(&addr, &s4->sin_addr);
1464852Svi117747 				s4->sin_family = AF_INET;
1465852Svi117747 				p += sizeof (*s4);
1466852Svi117747 			} else {
1467852Svi117747 				s6 = (struct sockaddr_in6 *)p;
1468852Svi117747 				s6->sin6_addr = addr;
1469852Svi117747 				s6->sin6_family = AF_INET6;
1470852Svi117747 				s6->sin6_scope_id =
1471852Svi117747 				    sctp_ipif->sctp_ipif_ill->sctp_ill_index;
1472852Svi117747 				p += sizeof (*s6);
1473852Svi117747 			}
1474852Svi117747 			(*addrcnt)++;
1475852Svi117747 			sctp_ipif = list_next(&sctp_g_ipifs[cnt].sctp_ipif_list,
1476852Svi117747 			    sctp_ipif);
1477852Svi117747 		}
1478852Svi117747 	}
1479852Svi117747 	rw_exit(&sctp_g_ipifs_lock);
1480852Svi117747 	return (err);
1481852Svi117747 }
1482852Svi117747 
1483852Svi117747 /*
1484852Svi117747  * Get a list of addresses from the source address list. The  caller is
1485852Svi117747  * responsible for allocating sufficient buffer for this.
1486852Svi117747  */
1487852Svi117747 void
1488852Svi117747 sctp_get_saddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1489852Svi117747 {
1490852Svi117747 	int			cnt;
1491852Svi117747 	int			icnt;
1492852Svi117747 	sctp_saddr_ipif_t	*obj;
1493852Svi117747 	int			naddr;
1494852Svi117747 	int			scanned = 0;
1495852Svi117747 
1496852Svi117747 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1497852Svi117747 		if (sctp->sctp_saddrs[cnt].ipif_count == 0)
1498852Svi117747 			continue;
1499852Svi117747 		obj = list_head(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
1500852Svi117747 		naddr = sctp->sctp_saddrs[cnt].ipif_count;
1501852Svi117747 		for (icnt = 0; icnt < naddr; icnt++) {
1502852Svi117747 			sctp_ipif_t	*ipif;
1503852Svi117747 
1504852Svi117747 			if (psize < sizeof (ipif->sctp_ipif_saddr))
1505852Svi117747 				return;
1506852Svi117747 
1507852Svi117747 			scanned++;
1508852Svi117747 			ipif = obj->saddr_ipifp;
1509852Svi117747 			bcopy(&ipif->sctp_ipif_saddr, p,
1510852Svi117747 			    sizeof (ipif->sctp_ipif_saddr));
1511852Svi117747 			p += sizeof (ipif->sctp_ipif_saddr);
1512852Svi117747 			psize -= sizeof (ipif->sctp_ipif_saddr);
1513852Svi117747 			if (scanned >= sctp->sctp_nsaddrs)
1514852Svi117747 				return;
1515852Svi117747 			obj = list_next(&sctp->sctp_saddrs[icnt].sctp_ipif_list,
1516852Svi117747 			    obj);
1517852Svi117747 		}
1518852Svi117747 	}
1519852Svi117747 }
1520852Svi117747 
1521852Svi117747 /*
1522852Svi117747  * Get a list of addresses from the remote address list. The  caller is
1523852Svi117747  * responsible for allocating sufficient buffer for this.
1524852Svi117747  */
1525852Svi117747 void
1526852Svi117747 sctp_get_faddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1527852Svi117747 {
1528852Svi117747 	sctp_faddr_t	*fp;
1529852Svi117747 
1530852Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1531852Svi117747 		if (psize < sizeof (fp->faddr))
1532852Svi117747 			return;
1533852Svi117747 		bcopy(&fp->faddr, p, sizeof (fp->faddr));
1534852Svi117747 		p += sizeof (fp->faddr);
1535852Svi117747 		psize -= sizeof (fp->faddr);
1536852Svi117747 	}
1537852Svi117747 }
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
15400Sstevel@tonic-gate void
15410Sstevel@tonic-gate sctp_saddr_init()
15420Sstevel@tonic-gate {
15430Sstevel@tonic-gate 	int	i;
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	rw_init(&sctp_g_ills_lock, NULL, RW_DEFAULT, NULL);
15460Sstevel@tonic-gate 	rw_init(&sctp_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
15490Sstevel@tonic-gate 		sctp_g_ills[i].ill_count = 0;
15500Sstevel@tonic-gate 		list_create(&sctp_g_ills[i].sctp_ill_list, sizeof (sctp_ill_t),
15510Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
15520Sstevel@tonic-gate 	}
15530Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
15540Sstevel@tonic-gate 		sctp_g_ipifs[i].ipif_count = 0;
15550Sstevel@tonic-gate 		list_create(&sctp_g_ipifs[i].sctp_ipif_list,
15560Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
15570Sstevel@tonic-gate 	}
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate void
15610Sstevel@tonic-gate sctp_saddr_fini()
15620Sstevel@tonic-gate {
15630Sstevel@tonic-gate 	int	i;
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	rw_destroy(&sctp_g_ills_lock);
15660Sstevel@tonic-gate 	rw_destroy(&sctp_g_ipifs_lock);
15670Sstevel@tonic-gate 	ASSERT(sctp_ills_count == 0 && sctp_g_ipifs_count == 0);
15680Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
15690Sstevel@tonic-gate 		list_destroy(&sctp_g_ills[i].sctp_ill_list);
15700Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
15710Sstevel@tonic-gate 		list_destroy(&sctp_g_ipifs[i].sctp_ipif_list);
15720Sstevel@tonic-gate }
1573