17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23df19b344Svi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/stream.h>
327c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/list.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <netinet/in.h>
407c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
417c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <inet/common.h>
447c478bd9Sstevel@tonic-gate #include <inet/ip.h>
457c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
467c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
477c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
487c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
497c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
507c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
537c478bd9Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
547c478bd9Sstevel@tonic-gate 			    zoneid_t zoneid);
557c478bd9Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
567c478bd9Sstevel@tonic-gate int			sctp_valid_addr_list(sctp_t *, const void *, uint32_t);
577c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_ipif_lookup(sctp_t *, uint_t);
58*f551bb10Svi static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
59*f551bb10Svi 			    boolean_t dontsrc);
607c478bd9Sstevel@tonic-gate static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
617c478bd9Sstevel@tonic-gate static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
627c478bd9Sstevel@tonic-gate 			    sctp_ipif_hash_t *);
637c478bd9Sstevel@tonic-gate int			sctp_compare_saddrs(sctp_t *, sctp_t *);
647c478bd9Sstevel@tonic-gate static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
657c478bd9Sstevel@tonic-gate int			sctp_dup_saddrs(sctp_t *, sctp_t *, int);
667c478bd9Sstevel@tonic-gate void			sctp_free_saddrs(sctp_t *);
677c478bd9Sstevel@tonic-gate void			sctp_update_ill(ill_t *, int);
687c478bd9Sstevel@tonic-gate void			sctp_update_ipif(ipif_t *, int);
697c478bd9Sstevel@tonic-gate void			sctp_move_ipif(ipif_t *, ill_t *, ill_t *);
707c478bd9Sstevel@tonic-gate void			sctp_del_saddr(sctp_t *, sctp_saddr_ipif_t *);
717c478bd9Sstevel@tonic-gate void			sctp_del_saddr_list(sctp_t *, const void *, int,
727c478bd9Sstevel@tonic-gate 			    boolean_t);
737c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_saddr_lookup(sctp_t *, in6_addr_t *);
747c478bd9Sstevel@tonic-gate in6_addr_t		sctp_get_valid_addr(sctp_t *, boolean_t);
757c478bd9Sstevel@tonic-gate int			sctp_getmyaddrs(void *, void *, int *);
767c478bd9Sstevel@tonic-gate void			sctp_saddr_init();
777c478bd9Sstevel@tonic-gate void			sctp_saddr_fini();
78*f551bb10Svi 
797c478bd9Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
807c478bd9Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
81*f551bb10Svi 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
82*f551bb10Svi 
83*f551bb10Svi #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
84*f551bb10Svi 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
85*f551bb10Svi 
86*f551bb10Svi /* True if this is a loopback ILL or a LINKLOCAL addr */
87*f551bb10Svi #define	SCTP_IS_LL_LB(ill, ipif)	\
88*f551bb10Svi 	(((ill)->sctp_ill_flags & PHYI_LOOPBACK) ||	\
89*f551bb10Svi 	((ipif)->sctp_ipif_isv6 && 			\
90*f551bb10Svi 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr)))
91*f551bb10Svi 
92*f551bb10Svi #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
93*f551bb10Svi 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
94*f551bb10Svi 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
977c478bd9Sstevel@tonic-gate #define	SCTP_IPIF_HASH_FN(seqid)	((seqid) % SCTP_IPIF_HASH)
987c478bd9Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /* Global list of SCTP ILLs */
1017c478bd9Sstevel@tonic-gate sctp_ill_hash_t	sctp_g_ills[SCTP_ILL_HASH];
1027c478bd9Sstevel@tonic-gate uint32_t	sctp_ills_count = 0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* Global list of SCTP IPIFs */
1057c478bd9Sstevel@tonic-gate sctp_ipif_hash_t	sctp_g_ipifs[SCTP_IPIF_HASH];
1067c478bd9Sstevel@tonic-gate uint32_t		sctp_g_ipifs_count;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1187c478bd9Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1197c478bd9Sstevel@tonic-gate  * Called with no locks held.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate static void
1227c478bd9Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
1257c478bd9Sstevel@tonic-gate 	uint_t		ipif_index;
1267c478bd9Sstevel@tonic-gate 	uint_t		ill_index;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
1297c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(sctp_ipif->sctp_ipif_id);
1327c478bd9Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1337c478bd9Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1347c478bd9Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1357c478bd9Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1367c478bd9Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
1377c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
1387c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
1397c478bd9Sstevel@tonic-gate 		return;
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	list_remove(&sctp_g_ipifs[ipif_index].sctp_ipif_list, sctp_ipif);
1427c478bd9Sstevel@tonic-gate 	sctp_g_ipifs[ipif_index].ipif_count--;
1437c478bd9Sstevel@tonic-gate 	sctp_g_ipifs_count--;
1447c478bd9Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1457c478bd9Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1487c478bd9Sstevel@tonic-gate 	if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
1497c478bd9Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
1507c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1517c478bd9Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1527c478bd9Sstevel@tonic-gate 			list_remove(&sctp_g_ills[ill_index].sctp_ill_list,
1537c478bd9Sstevel@tonic-gate 			    (void *)sctp_ill);
1547c478bd9Sstevel@tonic-gate 			sctp_g_ills[ill_index].ill_count--;
1557c478bd9Sstevel@tonic-gate 			sctp_ills_count--;
1567c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1577c478bd9Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1587c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
1627c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
1677c478bd9Sstevel@tonic-gate  * Called with no locks held.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate static sctp_ipif_t *
1707c478bd9Sstevel@tonic-gate sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	int		i;
1737c478bd9Sstevel@tonic-gate 	int		j;
1747c478bd9Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
1777c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1787c478bd9Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
1797c478bd9Sstevel@tonic-gate 			continue;
1807c478bd9Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
1817c478bd9Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
1827c478bd9Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1837c478bd9Sstevel@tonic-gate 			if (zoneid == sctp_ipif->sctp_ipif_zoneid &&
1847c478bd9Sstevel@tonic-gate 			    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) &&
1857c478bd9Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1867c478bd9Sstevel@tonic-gate 			    addr)) {
1877c478bd9Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1887c478bd9Sstevel@tonic-gate 				if (refhold)
1897c478bd9Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
1907c478bd9Sstevel@tonic-gate 				rw_exit(&sctp_g_ipifs_lock);
1917c478bd9Sstevel@tonic-gate 				return (sctp_ipif);
1927c478bd9Sstevel@tonic-gate 			}
1937c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1947c478bd9Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
1957c478bd9Sstevel@tonic-gate 			    sctp_ipif);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
1997c478bd9Sstevel@tonic-gate 	return (NULL);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
2047c478bd9Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
2057c478bd9Sstevel@tonic-gate  * Called with no locks held.
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate static int
2087c478bd9Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
2117c478bd9Sstevel@tonic-gate 	int			i;
2127c478bd9Sstevel@tonic-gate 	int			j;
2137c478bd9Sstevel@tonic-gate 	int			error = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
2167c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
2177c478bd9Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
2187c478bd9Sstevel@tonic-gate 			continue;
2197c478bd9Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
2207c478bd9Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
2217c478bd9Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
222*f551bb10Svi 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
2237c478bd9Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
224*f551bb10Svi 			    sctp_ipif->sctp_ipif_zoneid != sctp->sctp_zoneid ||
2257c478bd9Sstevel@tonic-gate 			    (sctp->sctp_ipversion == IPV4_VERSION &&
226*f551bb10Svi 			    sctp_ipif->sctp_ipif_isv6) ||
2277c478bd9Sstevel@tonic-gate 			    (sctp->sctp_connp->conn_ipv6_v6only &&
228*f551bb10Svi 			    !sctp_ipif->sctp_ipif_isv6)) {
2297c478bd9Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2307c478bd9Sstevel@tonic-gate 				sctp_ipif = list_next(
2317c478bd9Sstevel@tonic-gate 				    &sctp_g_ipifs[i].sctp_ipif_list, sctp_ipif);
2327c478bd9Sstevel@tonic-gate 				continue;
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2357c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
236*f551bb10Svi 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
237*f551bb10Svi 			    B_FALSE);
2387c478bd9Sstevel@tonic-gate 			if (error != 0)
2397c478bd9Sstevel@tonic-gate 				goto free_stuff;
2407c478bd9Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
2417c478bd9Sstevel@tonic-gate 			    sctp_ipif);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2457c478bd9Sstevel@tonic-gate 	return (0);
2467c478bd9Sstevel@tonic-gate free_stuff:
2477c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2487c478bd9Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2497c478bd9Sstevel@tonic-gate 	return (ENOMEM);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2547c478bd9Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
2557c478bd9Sstevel@tonic-gate  * or error.
2567c478bd9Sstevel@tonic-gate  * Called with no locks held.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate int
2597c478bd9Sstevel@tonic-gate sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2627c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2637c478bd9Sstevel@tonic-gate 	struct in_addr		*addr4;
2647c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
2657c478bd9Sstevel@tonic-gate 	int			cnt;
2667c478bd9Sstevel@tonic-gate 	int			err = 0;
2677c478bd9Sstevel@tonic-gate 	int			saddr_cnt = 0;
2687c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
2697c478bd9Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
2707c478bd9Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
2717c478bd9Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
2757c478bd9Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
2767c478bd9Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
2777c478bd9Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
2787c478bd9Sstevel@tonic-gate 	 * addresses.
2797c478bd9Sstevel@tonic-gate 	 */
2807c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
2817c478bd9Sstevel@tonic-gate 		check_lport = B_TRUE;
2827c478bd9Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
2837c478bd9Sstevel@tonic-gate 			check_addrs = B_TRUE;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
2867c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
2877c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
2887c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
2897c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
2907c478bd9Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		switch (sctp->sctp_family) {
2937c478bd9Sstevel@tonic-gate 		case AF_INET:
2947c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
2957c478bd9Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
2967c478bd9Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
2977c478bd9Sstevel@tonic-gate 				err = EINVAL;
2987c478bd9Sstevel@tonic-gate 				goto free_ret;
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
3017c478bd9Sstevel@tonic-gate 			if (check_addrs &&
3027c478bd9Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
3037c478bd9Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
3047c478bd9Sstevel@tonic-gate 			    IN_MULTICAST(addr4->s_addr))) {
3057c478bd9Sstevel@tonic-gate 				err = EINVAL;
3067c478bd9Sstevel@tonic-gate 				goto free_ret;
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
3097c478bd9Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
3107c478bd9Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3117c478bd9Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 		case AF_INET6:
3167c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3177c478bd9Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
3187c478bd9Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
3197c478bd9Sstevel@tonic-gate 				err = EINVAL;
3207c478bd9Sstevel@tonic-gate 				goto free_ret;
3217c478bd9Sstevel@tonic-gate 			}
3227c478bd9Sstevel@tonic-gate 			addr = sin6->sin6_addr;
3237c478bd9Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
3247c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3257c478bd9Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3267c478bd9Sstevel@tonic-gate 				goto free_ret;
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 			if (check_addrs &&
3297c478bd9Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3307c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3317c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3327c478bd9Sstevel@tonic-gate 				err = EINVAL;
3337c478bd9Sstevel@tonic-gate 				goto free_ret;
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3367c478bd9Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3377c478bd9Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 		default:
3427c478bd9Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3437c478bd9Sstevel@tonic-gate 			goto free_ret;
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 		if (lookup_saddr) {
3467c478bd9Sstevel@tonic-gate 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
3477c478bd9Sstevel@tonic-gate 			    sctp->sctp_zoneid);
3487c478bd9Sstevel@tonic-gate 			if (ipif == NULL) {
3497c478bd9Sstevel@tonic-gate 				/* Address not in the list */
3507c478bd9Sstevel@tonic-gate 				err = EINVAL;
3517c478bd9Sstevel@tonic-gate 				goto free_ret;
3527c478bd9Sstevel@tonic-gate 			} else if (check_addrs &&
3537c478bd9Sstevel@tonic-gate 			    (ipif->sctp_ipif_ill->sctp_ill_flags &
3547c478bd9Sstevel@tonic-gate 			    PHYI_LOOPBACK)) {
3557c478bd9Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3567c478bd9Sstevel@tonic-gate 				err = EINVAL;
3577c478bd9Sstevel@tonic-gate 				goto free_ret;
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 		if (!bind_to_all) {
361*f551bb10Svi 			/*
362*f551bb10Svi 			 * If an address is added after association setup,
363*f551bb10Svi 			 * we need to wait for the peer to send us an ASCONF
364*f551bb10Svi 			 * ACK before we can start using it.
365*f551bb10Svi 			 * saddr_ipif_dontsrc will be reset (to 0) when we
366*f551bb10Svi 			 * get the ASCONF ACK for this address.
367*f551bb10Svi 			 */
368*f551bb10Svi 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
369*f551bb10Svi 			    check_addrs ? B_TRUE : B_FALSE);
3707c478bd9Sstevel@tonic-gate 			if (err != 0) {
3717c478bd9Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3727c478bd9Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
3737c478bd9Sstevel@tonic-gate 					err = EADDRINUSE;
3747c478bd9Sstevel@tonic-gate 				goto free_ret;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			saddr_cnt++;
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	if (bind_to_all) {
3807c478bd9Sstevel@tonic-gate 		/*
3817c478bd9Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
3827c478bd9Sstevel@tonic-gate 		 * inaddr_any.
3837c478bd9Sstevel@tonic-gate 		 */
3847c478bd9Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
3857c478bd9Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
3867c478bd9Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
3897c478bd9Sstevel@tonic-gate 		if (err != 0)
3907c478bd9Sstevel@tonic-gate 			return (err);
3917c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3947c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
3957c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3967c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
3977c478bd9Sstevel@tonic-gate 	return (0);
3987c478bd9Sstevel@tonic-gate free_ret:
3997c478bd9Sstevel@tonic-gate 	if (saddr_cnt != 0)
4007c478bd9Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
4017c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4027c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4037c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4047c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4057c478bd9Sstevel@tonic-gate 	return (err);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *
4097c478bd9Sstevel@tonic-gate sctp_ipif_lookup(sctp_t *sctp, uint_t ipif_index)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	int			cnt;
4127c478bd9Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif_index);
4137c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (sctp->sctp_saddrs[seqid].ipif_count == 0)
4167c478bd9Sstevel@tonic-gate 		return (NULL);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4197c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4207c478bd9Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif_index)
4217c478bd9Sstevel@tonic-gate 			return (ipif_obj);
4227c478bd9Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4237c478bd9Sstevel@tonic-gate 		    ipif_obj);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	return (NULL);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate static int
429*f551bb10Svi sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
430*f551bb10Svi     boolean_t dontsrc)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	int			cnt;
4337c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4347c478bd9Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4377c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4387c478bd9Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id)
4397c478bd9Sstevel@tonic-gate 			return (EALREADY);
4407c478bd9Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4417c478bd9Sstevel@tonic-gate 		    ipif_obj);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4447c478bd9Sstevel@tonic-gate 	if (ipif_obj == NULL) {
4457c478bd9Sstevel@tonic-gate 		/* Need to do something */
4467c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
449*f551bb10Svi 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
4507c478bd9Sstevel@tonic-gate 	list_insert_tail(&sctp->sctp_saddrs[seqid].sctp_ipif_list, ipif_obj);
4517c478bd9Sstevel@tonic-gate 	sctp->sctp_saddrs[seqid].ipif_count++;
4527c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
4537c478bd9Sstevel@tonic-gate 	return (0);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate static void
4577c478bd9Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	int			cnt;
4607c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4617c478bd9Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4647c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4657c478bd9Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) {
4667c478bd9Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4677c478bd9Sstevel@tonic-gate 			    ipif_obj);
4687c478bd9Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
4697c478bd9Sstevel@tonic-gate 			sctp->sctp_saddrs[seqid].ipif_count--;
4707c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
4717c478bd9Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
4727c478bd9Sstevel@tonic-gate 			break;
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4757c478bd9Sstevel@tonic-gate 		    ipif_obj);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate static int
4807c478bd9Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	int			i;
4837c478bd9Sstevel@tonic-gate 	int			j;
4847c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
4857c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
4867c478bd9Sstevel@tonic-gate 	int			overlap = 0;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
4897c478bd9Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
4907c478bd9Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
4917c478bd9Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
4927c478bd9Sstevel@tonic-gate 			if (obj1->saddr_ipifp->sctp_ipif_id ==
4937c478bd9Sstevel@tonic-gate 			    obj2->saddr_ipifp->sctp_ipif_id) {
4947c478bd9Sstevel@tonic-gate 				overlap++;
4957c478bd9Sstevel@tonic-gate 				break;
4967c478bd9Sstevel@tonic-gate 			}
4977c478bd9Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
4987c478bd9Sstevel@tonic-gate 			    obj2);
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	return (overlap);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate int
5067c478bd9Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	int		i;
5097c478bd9Sstevel@tonic-gate 	int		overlap = 0;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5127c478bd9Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
5137c478bd9Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5177c478bd9Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5187c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
5227c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if (overlap > 0)
5257c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate static int
5317c478bd9Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	int			i;
5347c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5357c478bd9Sstevel@tonic-gate 	int			error = 0;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
5387c478bd9Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5397c478bd9Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
540*f551bb10Svi 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
541*f551bb10Svi 		    B_FALSE);
5427c478bd9Sstevel@tonic-gate 		if (error != 0)
5437c478bd9Sstevel@tonic-gate 			return (error);
5447c478bd9Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	return (error);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate int
5507c478bd9Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	int	error = 0;
5537c478bd9Sstevel@tonic-gate 	int	i;
5547c478bd9Sstevel@tonic-gate 
555*f551bb10Svi 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
5567c478bd9Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5597c478bd9Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
5607c478bd9Sstevel@tonic-gate 			continue;
5617c478bd9Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
5627c478bd9Sstevel@tonic-gate 		if (error != 0) {
5637c478bd9Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
5647c478bd9Sstevel@tonic-gate 			return (error);
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	return (0);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate void
5717c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	int			i;
5747c478bd9Sstevel@tonic-gate 	int			l;
5757c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
5787c478bd9Sstevel@tonic-gate 		return;
5797c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5807c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
5817c478bd9Sstevel@tonic-gate 			continue;
5827c478bd9Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
5837c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
5847c478bd9Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
5857c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
5867c478bd9Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
5877c478bd9Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
5887c478bd9Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
5917c478bd9Sstevel@tonic-gate 	}
592*f551bb10Svi 	if (sctp->sctp_bound_to_all == 1)
593*f551bb10Svi 		sctp->sctp_bound_to_all = 0;
5947c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
5997c478bd9Sstevel@tonic-gate  * held.
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate void
6027c478bd9Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	int		i;
6057c478bd9Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
6067c478bd9Sstevel@tonic-gate 	uint_t		index;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	ip2dbg(("sctp_update_ill: %s\n", ill->ill_name));
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_WRITER);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
6137c478bd9Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
6147c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
6157c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
6167c478bd9Sstevel@tonic-gate 			break;
6177c478bd9Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
6187c478bd9Sstevel@tonic-gate 		    sctp_ill);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	switch (op) {
6227c478bd9Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
6237c478bd9Sstevel@tonic-gate 		if (sctp_ill != NULL) {
6247c478bd9Sstevel@tonic-gate 			/* Unmark it if it is condemned */
6257c478bd9Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
6267c478bd9Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
6277c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6287c478bd9Sstevel@tonic-gate 			return;
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
6317c478bd9Sstevel@tonic-gate 		/* Need to re-try? */
6327c478bd9Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6337c478bd9Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6347c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6357c478bd9Sstevel@tonic-gate 			return;
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 		sctp_ill->sctp_ill_name =
6387c478bd9Sstevel@tonic-gate 		    kmem_zalloc(ill->ill_name_length, KM_NOSLEEP);
6397c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
6407c478bd9Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6417c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6427c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6437c478bd9Sstevel@tonic-gate 			return;
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
6467c478bd9Sstevel@tonic-gate 		    ill->ill_name_length);
6477c478bd9Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
6487c478bd9Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
6497c478bd9Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
6507c478bd9Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ills[index].sctp_ill_list,
6517c478bd9Sstevel@tonic-gate 		    (void *)sctp_ill);
6527c478bd9Sstevel@tonic-gate 		sctp_g_ills[index].ill_count++;
6537c478bd9Sstevel@tonic-gate 		sctp_ills_count++;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 		break;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6607c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6617c478bd9Sstevel@tonic-gate 			return;
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
6647c478bd9Sstevel@tonic-gate 			list_remove(&sctp_g_ills[index].sctp_ill_list,
6657c478bd9Sstevel@tonic-gate 			    (void *)sctp_ill);
6667c478bd9Sstevel@tonic-gate 			sctp_g_ills[index].ill_count--;
6677c478bd9Sstevel@tonic-gate 			sctp_ills_count--;
6687c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
6697c478bd9Sstevel@tonic-gate 			    ill->ill_name_length);
6707c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6717c478bd9Sstevel@tonic-gate 		} else {
6727c478bd9Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		break;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
6817c478bd9Sstevel@tonic-gate void
6827c478bd9Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
6857c478bd9Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
6867c478bd9Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
6877c478bd9Sstevel@tonic-gate 	uint_t		index;
6887c478bd9Sstevel@tonic-gate 	int		i;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
6917c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
6947c478bd9Sstevel@tonic-gate 	fsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
6957c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
6967c478bd9Sstevel@tonic-gate 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill))
6977c478bd9Sstevel@tonic-gate 			break;
6987c478bd9Sstevel@tonic-gate 		fsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
6997c478bd9Sstevel@tonic-gate 		    fsctp_ill);
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
7037c478bd9Sstevel@tonic-gate 	tsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
7047c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
7057c478bd9Sstevel@tonic-gate 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill))
7067c478bd9Sstevel@tonic-gate 			break;
7077c478bd9Sstevel@tonic-gate 		tsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
7087c478bd9Sstevel@tonic-gate 		    tsctp_ill);
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7127c478bd9Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[index].sctp_ipif_list);
7137c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[index].ipif_count; i++) {
7147c478bd9Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7157c478bd9Sstevel@tonic-gate 			break;
7167c478bd9Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[index].sctp_ipif_list,
7177c478bd9Sstevel@tonic-gate 		    sctp_ipif);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	/* Should be an ASSERT? */
7207c478bd9Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
7217c478bd9Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
7227c478bd9Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
7237c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7247c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7257c478bd9Sstevel@tonic-gate 		return;
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
7287c478bd9Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
7297c478bd9Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
7307c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
7317c478bd9Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
7327c478bd9Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
7337c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
7347c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
7387c478bd9Sstevel@tonic-gate void
7397c478bd9Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
7427c478bd9Sstevel@tonic-gate 	int		i;
7437c478bd9Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
7447c478bd9Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7457c478bd9Sstevel@tonic-gate 	uint_t		ill_index;
7467c478bd9Sstevel@tonic-gate 	uint_t		ipif_index;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
7517c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
7547c478bd9Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[ill_index].sctp_ill_list);
7557c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[ill_index].ill_count; i++) {
7567c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
7577c478bd9Sstevel@tonic-gate 			break;
7587c478bd9Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[ill_index].sctp_ill_list,
7597c478bd9Sstevel@tonic-gate 		    sctp_ill);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	if (sctp_ill == NULL) {
7627c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7637c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7647c478bd9Sstevel@tonic-gate 		return;
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7687c478bd9Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[ipif_index].sctp_ipif_list);
7697c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[ipif_index].ipif_count; i++) {
7707c478bd9Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7717c478bd9Sstevel@tonic-gate 			break;
7727c478bd9Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
7737c478bd9Sstevel@tonic-gate 		    sctp_ipif);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	if (op != SCTP_IPIF_INSERT && sctp_ipif == NULL) {
7767c478bd9Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
7777c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7787c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7797c478bd9Sstevel@tonic-gate 		return;
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7827c478bd9Sstevel@tonic-gate 	if (sctp_ipif != NULL)
7837c478bd9Sstevel@tonic-gate 		ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
7847c478bd9Sstevel@tonic-gate #endif
7857c478bd9Sstevel@tonic-gate 	switch (op) {
7867c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_INSERT:
7877c478bd9Sstevel@tonic-gate 		if (sctp_ipif != NULL) {
7887c478bd9Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED)
789df19b344Svi 				sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
7907c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
7917c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
7927c478bd9Sstevel@tonic-gate 			return;
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 		sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
7957c478bd9Sstevel@tonic-gate 		/* Try again? */
7967c478bd9Sstevel@tonic-gate 		if (sctp_ipif == NULL) {
7977c478bd9Sstevel@tonic-gate 			ip1dbg(("sctp_ipif_insert: mem failure..\n"));
7987c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
7997c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8007c478bd9Sstevel@tonic-gate 			return;
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
8037c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_ill = sctp_ill;
804df19b344Svi 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
8057c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8067c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
8077c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
808*f551bb10Svi 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8097c478bd9Sstevel@tonic-gate 		rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
8107c478bd9Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
8117c478bd9Sstevel@tonic-gate 		    (void *)sctp_ipif);
8127c478bd9Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count++;
8137c478bd9Sstevel@tonic-gate 		sctp_g_ipifs_count++;
8147c478bd9Sstevel@tonic-gate 		atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 		break;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
8197c478bd9Sstevel@tonic-gate 	{
8207c478bd9Sstevel@tonic-gate 		list_t		*ipif_list;
8217c478bd9Sstevel@tonic-gate 		list_t		*ill_list;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		ill_list = &sctp_g_ills[ill_index].sctp_ill_list;
8247c478bd9Sstevel@tonic-gate 		ipif_list = &sctp_g_ipifs[ipif_index].sctp_ipif_list;
8257c478bd9Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
8267c478bd9Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
8277c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8287c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8297c478bd9Sstevel@tonic-gate 			return;
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
8327c478bd9Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count--;
8337c478bd9Sstevel@tonic-gate 		sctp_g_ipifs_count--;
8347c478bd9Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
8357c478bd9Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
8367c478bd9Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
8377c478bd9Sstevel@tonic-gate 		if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
8387c478bd9Sstevel@tonic-gate 			rw_downgrade(&sctp_g_ipifs_lock);
8397c478bd9Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
8407c478bd9Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
8417c478bd9Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
8427c478bd9Sstevel@tonic-gate 				sctp_ills_count--;
8437c478bd9Sstevel@tonic-gate 				sctp_g_ills[ill_index].ill_count--;
8447c478bd9Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
8457c478bd9Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
8467c478bd9Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 		break;
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_UP:
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8557c478bd9Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8567c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
8577c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
858*f551bb10Svi 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
859*f551bb10Svi 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8607c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		break;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8677c478bd9Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8687c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8697c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
8707c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
871*f551bb10Svi 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8727c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 		break;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8797c478bd9Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8807c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
8817c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 		break;
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
8867c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate  *
8917c478bd9Sstevel@tonic-gate  *
8927c478bd9Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
8937c478bd9Sstevel@tonic-gate  * sctp locking by the caller.
8947c478bd9Sstevel@tonic-gate  *
8957c478bd9Sstevel@tonic-gate  *
8967c478bd9Sstevel@tonic-gate  */
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate /* Remove a specific saddr from the list */
8997c478bd9Sstevel@tonic-gate void
9007c478bd9Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
9017c478bd9Sstevel@tonic-gate {
9027c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9037c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9067c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
9097c478bd9Sstevel@tonic-gate 
910*f551bb10Svi 	if (sctp->sctp_bound_to_all == 1)
9117c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9147c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9177c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
9227c478bd9Sstevel@tonic-gate  * Called with no locks held.
9237c478bd9Sstevel@tonic-gate  */
9247c478bd9Sstevel@tonic-gate void
9257c478bd9Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
9267c478bd9Sstevel@tonic-gate     boolean_t fanout_locked)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
9297c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
9307c478bd9Sstevel@tonic-gate 	int			cnt;
9317c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
9327c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > addcnt);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if (!fanout_locked) {
9377c478bd9Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9387c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9397c478bd9Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9407c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
9447c478bd9Sstevel@tonic-gate 		switch (sctp->sctp_family) {
9457c478bd9Sstevel@tonic-gate 		case AF_INET:
9467c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
9477c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
9487c478bd9Sstevel@tonic-gate 			break;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 		case AF_INET6:
9517c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
9527c478bd9Sstevel@tonic-gate 			addr = sin6->sin6_addr;
9537c478bd9Sstevel@tonic-gate 			break;
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
9567c478bd9Sstevel@tonic-gate 		    sctp->sctp_zoneid);
9577c478bd9Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
9587c478bd9Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
9597c478bd9Sstevel@tonic-gate 	}
960*f551bb10Svi 	if (sctp->sctp_bound_to_all == 1)
9617c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (!fanout_locked) {
9647c478bd9Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9657c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9667c478bd9Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9677c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate /*
9727c478bd9Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
9737c478bd9Sstevel@tonic-gate  * Called with no locks held.
9747c478bd9Sstevel@tonic-gate  */
9757c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *
9767c478bd9Sstevel@tonic-gate sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*saddr_ipifs;
9797c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_FALSE, sctp->sctp_zoneid);
9827c478bd9Sstevel@tonic-gate 	if (sctp_ipif == NULL)
9837c478bd9Sstevel@tonic-gate 		return (NULL);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	saddr_ipifs = sctp_ipif_lookup(sctp, sctp_ipif->sctp_ipif_id);
9867c478bd9Sstevel@tonic-gate 	return (saddr_ipifs);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
989*f551bb10Svi /* Given an address, add it to the source address list */
990*f551bb10Svi int
991*f551bb10Svi sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr)
992*f551bb10Svi {
993*f551bb10Svi 	sctp_ipif_t		*sctp_ipif;
994*f551bb10Svi 
995*f551bb10Svi 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp->sctp_zoneid);
996*f551bb10Svi 	if (sctp_ipif == NULL)
997*f551bb10Svi 		return (EINVAL);
998*f551bb10Svi 
999*f551bb10Svi 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE) != 0) {
1000*f551bb10Svi 		SCTP_IPIF_REFRELE(sctp_ipif);
1001*f551bb10Svi 		return (EINVAL);
1002*f551bb10Svi 	}
1003*f551bb10Svi 	return (0);
1004*f551bb10Svi }
1005*f551bb10Svi 
1006*f551bb10Svi /*
1007*f551bb10Svi  * Remove or mark as dontsrc addresses that are currently not part of the
1008*f551bb10Svi  * association. One would delete addresses when processing an INIT and
1009*f551bb10Svi  * mark as dontsrc when processing an INIT-ACK.
1010*f551bb10Svi  */
1011*f551bb10Svi void
1012*f551bb10Svi sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete)
1013*f551bb10Svi {
1014*f551bb10Svi 	int			i;
1015*f551bb10Svi 	int			l;
1016*f551bb10Svi 	sctp_saddr_ipif_t	*obj;
1017*f551bb10Svi 	int			scanned = 0;
1018*f551bb10Svi 	int			naddr;
1019*f551bb10Svi 	int			nsaddr;
1020*f551bb10Svi 
1021*f551bb10Svi 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1022*f551bb10Svi 
1023*f551bb10Svi 	/*
1024*f551bb10Svi 	 * Irregardless of the supported address in the INIT, v4
1025*f551bb10Svi 	 * must be supported.
1026*f551bb10Svi 	 */
1027*f551bb10Svi 	if (sctp->sctp_family == AF_INET)
1028*f551bb10Svi 		supp_af = PARM_SUPP_V4;
1029*f551bb10Svi 
1030*f551bb10Svi 	nsaddr = sctp->sctp_nsaddrs;
1031*f551bb10Svi 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1032*f551bb10Svi 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1033*f551bb10Svi 			continue;
1034*f551bb10Svi 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1035*f551bb10Svi 		naddr = sctp->sctp_saddrs[i].ipif_count;
1036*f551bb10Svi 		for (l = 0; l < naddr; l++) {
1037*f551bb10Svi 			sctp_ipif_t	*ipif;
1038*f551bb10Svi 			sctp_ill_t	*ill;
1039*f551bb10Svi 
1040*f551bb10Svi 			ipif = obj->saddr_ipifp;
1041*f551bb10Svi 			ill = ipif->sctp_ipif_ill;
1042*f551bb10Svi 			scanned++;
1043*f551bb10Svi 
1044*f551bb10Svi 			/*
1045*f551bb10Svi 			 * Delete/mark dontsrc loopback/linklocal addresses and
1046*f551bb10Svi 			 * unsupported address.
1047*f551bb10Svi 			 */
1048*f551bb10Svi 			if (SCTP_IS_LL_LB(ill, ipif) ||
1049*f551bb10Svi 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1050*f551bb10Svi 				if (!delete) {
1051*f551bb10Svi 					obj->saddr_ipif_unconfirmed = 1;
1052*f551bb10Svi 					goto next_obj;
1053*f551bb10Svi 				}
1054*f551bb10Svi 				if (sctp->sctp_bound_to_all == 1)
1055*f551bb10Svi 					sctp->sctp_bound_to_all = 0;
1056*f551bb10Svi 				if (scanned < nsaddr) {
1057*f551bb10Svi 					obj = list_next(&sctp->sctp_saddrs[i].
1058*f551bb10Svi 					    sctp_ipif_list, obj);
1059*f551bb10Svi 					sctp_ipif_hash_remove(sctp, ipif);
1060*f551bb10Svi 					continue;
1061*f551bb10Svi 				}
1062*f551bb10Svi 				sctp_ipif_hash_remove(sctp, ipif);
1063*f551bb10Svi 			}
1064*f551bb10Svi 	next_obj:
1065*f551bb10Svi 			if (scanned >= nsaddr)
1066*f551bb10Svi 				return;
1067*f551bb10Svi 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1068*f551bb10Svi 			    obj);
1069*f551bb10Svi 		}
1070*f551bb10Svi 	}
1071*f551bb10Svi }
1072*f551bb10Svi 
1073*f551bb10Svi 
10747c478bd9Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
10757c478bd9Sstevel@tonic-gate in6_addr_t
10767c478bd9Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	int			i;
10797c478bd9Sstevel@tonic-gate 	int			l;
10807c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
10817c478bd9Sstevel@tonic-gate 	int			scanned = 0;
10827c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
10857c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
10867c478bd9Sstevel@tonic-gate 			continue;
10877c478bd9Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
10887c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
10897c478bd9Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1092*f551bb10Svi 			if (!SCTP_DONT_SRC(obj) &&
10937c478bd9Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
1094*f551bb10Svi 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
10957c478bd9Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
10967c478bd9Sstevel@tonic-gate 			}
10977c478bd9Sstevel@tonic-gate 			scanned++;
10987c478bd9Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
10997c478bd9Sstevel@tonic-gate 				goto got_none;
11007c478bd9Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
11017c478bd9Sstevel@tonic-gate 			    obj);
11027c478bd9Sstevel@tonic-gate 		}
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate got_none:
11057c478bd9Sstevel@tonic-gate 	/* Need to double check this */
11067c478bd9Sstevel@tonic-gate 	if (isv6 == B_TRUE)
11077c478bd9Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
11087c478bd9Sstevel@tonic-gate 	else
11097c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	return (addr);
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate /*
11157c478bd9Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
11167c478bd9Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
11177c478bd9Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
11187c478bd9Sstevel@tonic-gate  */
11197c478bd9Sstevel@tonic-gate int
11207c478bd9Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	int			i;
11237c478bd9Sstevel@tonic-gate 	int			l;
11247c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
11257c478bd9Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
11267c478bd9Sstevel@tonic-gate 	int			family = sctp->sctp_family;
11277c478bd9Sstevel@tonic-gate 	int			max = *addrcnt;
11287c478bd9Sstevel@tonic-gate 	size_t			added = 0;
11297c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
11307c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
11317c478bd9Sstevel@tonic-gate 	int			scanned = 0;
11327c478bd9Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
11357c478bd9Sstevel@tonic-gate 		return (EINVAL);
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	/* Skip loopback addresses for non-loopback assoc. */
11387c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback)
11397c478bd9Sstevel@tonic-gate 		skip_lback = B_TRUE;
11407c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11417c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11427c478bd9Sstevel@tonic-gate 			continue;
11437c478bd9Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11447c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11457c478bd9Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
11467c478bd9Sstevel@tonic-gate 			sctp_ill_t	*ill = ipif->sctp_ipif_ill;
11477c478bd9Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 			scanned++;
11507c478bd9Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1151*f551bb10Svi 			    SCTP_DONT_SRC(obj) ||
11527c478bd9Sstevel@tonic-gate 			    ((ill->sctp_ill_flags & PHYI_LOOPBACK) &&
11537c478bd9Sstevel@tonic-gate 			    skip_lback)) {
11547c478bd9Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
11557c478bd9Sstevel@tonic-gate 					goto done;
11567c478bd9Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
11577c478bd9Sstevel@tonic-gate 				    sctp_ipif_list, obj);
11587c478bd9Sstevel@tonic-gate 				continue;
11597c478bd9Sstevel@tonic-gate 			}
11607c478bd9Sstevel@tonic-gate 			switch (family) {
11617c478bd9Sstevel@tonic-gate 			case AF_INET:
11627c478bd9Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
11637c478bd9Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
11647c478bd9Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
11657c478bd9Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
11667c478bd9Sstevel@tonic-gate 				break;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 			case AF_INET6:
11697c478bd9Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
11707c478bd9Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
11717c478bd9Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
11727c478bd9Sstevel@tonic-gate 				sin6->sin6_addr = addr;
11737c478bd9Sstevel@tonic-gate 				break;
11747c478bd9Sstevel@tonic-gate 			}
11757c478bd9Sstevel@tonic-gate 			added++;
11767c478bd9Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
11777c478bd9Sstevel@tonic-gate 				goto done;
11787c478bd9Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
11797c478bd9Sstevel@tonic-gate 			    obj);
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate done:
11837c478bd9Sstevel@tonic-gate 	*addrcnt = added;
11847c478bd9Sstevel@tonic-gate 	return (0);
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /*
1188df19b344Svi  * Given the supported address family, walk through the source address list
1189df19b344Svi  * and return the total length of the available addresses. If 'p' is not
1190df19b344Svi  * null, construct the parameter list for the addresses in 'p'.
1191*f551bb10Svi  * 'modify' will only be set when we want the source address list to
1192*f551bb10Svi  * be modified. The source address list will be modified only when
1193*f551bb10Svi  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1194*f551bb10Svi  * be false since the 'sctp' will be that of the listener.
11957c478bd9Sstevel@tonic-gate  */
11967c478bd9Sstevel@tonic-gate size_t
1197*f551bb10Svi sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	int			i;
12007c478bd9Sstevel@tonic-gate 	int			l;
12017c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1202df19b344Svi 	size_t			paramlen = 0;
12037c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
12047c478bd9Sstevel@tonic-gate 	int			scanned = 0;
1205*f551bb10Svi 	int			naddr;
1206*f551bb10Svi 	int			nsaddr;
1207*f551bb10Svi 	boolean_t		del_ll_lb = B_FALSE;
1208*f551bb10Svi 
1209*f551bb10Svi 	if (modify && !sctp->sctp_loopback && !sctp->sctp_linklocal)
1210*f551bb10Svi 		del_ll_lb = B_TRUE;
12117c478bd9Sstevel@tonic-gate 
1212*f551bb10Svi 	nsaddr = sctp->sctp_nsaddrs;
12137c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
12147c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
12157c478bd9Sstevel@tonic-gate 			continue;
12167c478bd9Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1217*f551bb10Svi 		naddr = sctp->sctp_saddrs[i].ipif_count;
1218*f551bb10Svi 		for (l = 0; l < naddr; l++) {
12197c478bd9Sstevel@tonic-gate 			in6_addr_t	addr;
12207c478bd9Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
12217c478bd9Sstevel@tonic-gate 			sctp_ill_t	*ill;
1222*f551bb10Svi 			boolean_t	ll_lb;
1223*f551bb10Svi 			boolean_t	unsupp_af;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
12267c478bd9Sstevel@tonic-gate 			ill = ipif->sctp_ipif_ill;
12277c478bd9Sstevel@tonic-gate 			scanned++;
1228*f551bb10Svi 
1229*f551bb10Svi 			ll_lb = SCTP_IS_LL_LB(ill, ipif);
1230*f551bb10Svi 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1231*f551bb10Svi 			/*
1232*f551bb10Svi 			 * We need to either delete or skip loopback/linklocal
1233*f551bb10Svi 			 * or unsupported addresses.
1234*f551bb10Svi 			 */
1235*f551bb10Svi 			if ((ll_lb && del_ll_lb) || (unsupp_af && modify)) {
1236*f551bb10Svi 				if (sctp->sctp_bound_to_all == 1)
1237*f551bb10Svi 					sctp->sctp_bound_to_all = 0;
1238*f551bb10Svi 				if (scanned < nsaddr) {
1239*f551bb10Svi 					obj = list_next(&sctp->sctp_saddrs[i].
1240*f551bb10Svi 					    sctp_ipif_list, obj);
1241*f551bb10Svi 					sctp_ipif_hash_remove(sctp, ipif);
1242*f551bb10Svi 					continue;
1243*f551bb10Svi 				}
1244*f551bb10Svi 				sctp_ipif_hash_remove(sctp, ipif);
1245*f551bb10Svi 				goto next_addr;
1246*f551bb10Svi 			} else if (ll_lb || unsupp_af) {
1247df19b344Svi 				goto next_addr;
12487c478bd9Sstevel@tonic-gate 			}
1249*f551bb10Svi 
1250*f551bb10Svi 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1251*f551bb10Svi 				goto next_addr;
1252df19b344Svi 			if (p != NULL)
1253df19b344Svi 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
12547c478bd9Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1255*f551bb10Svi 			if (!ipif->sctp_ipif_isv6) {
12567c478bd9Sstevel@tonic-gate 				struct in_addr	*v4;
12577c478bd9Sstevel@tonic-gate 
1258df19b344Svi 				if (p != NULL) {
1259df19b344Svi 					hdr->sph_type = htons(PARM_ADDR4);
1260df19b344Svi 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1261df19b344Svi 					v4 = (struct in_addr *)(hdr + 1);
1262df19b344Svi 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1263df19b344Svi 				}
1264df19b344Svi 				paramlen += PARM_ADDR4_LEN;
1265*f551bb10Svi 			} else {
1266df19b344Svi 				if (p != NULL) {
1267df19b344Svi 					hdr->sph_type = htons(PARM_ADDR6);
1268df19b344Svi 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1269df19b344Svi 					bcopy(&addr, hdr + 1, sizeof (addr));
1270df19b344Svi 				}
1271df19b344Svi 				paramlen += PARM_ADDR6_LEN;
12727c478bd9Sstevel@tonic-gate 			}
1273df19b344Svi next_addr:
1274*f551bb10Svi 			if (scanned >= nsaddr)
1275df19b344Svi 				return (paramlen);
12767c478bd9Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
12777c478bd9Sstevel@tonic-gate 			    obj);
12787c478bd9Sstevel@tonic-gate 		}
12797c478bd9Sstevel@tonic-gate 	}
1280df19b344Svi 	return (paramlen);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
12857c478bd9Sstevel@tonic-gate void
12867c478bd9Sstevel@tonic-gate sctp_saddr_init()
12877c478bd9Sstevel@tonic-gate {
12887c478bd9Sstevel@tonic-gate 	int	i;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	rw_init(&sctp_g_ills_lock, NULL, RW_DEFAULT, NULL);
12917c478bd9Sstevel@tonic-gate 	rw_init(&sctp_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
12947c478bd9Sstevel@tonic-gate 		sctp_g_ills[i].ill_count = 0;
12957c478bd9Sstevel@tonic-gate 		list_create(&sctp_g_ills[i].sctp_ill_list, sizeof (sctp_ill_t),
12967c478bd9Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
12997c478bd9Sstevel@tonic-gate 		sctp_g_ipifs[i].ipif_count = 0;
13007c478bd9Sstevel@tonic-gate 		list_create(&sctp_g_ipifs[i].sctp_ipif_list,
13017c478bd9Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate }
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate void
13067c478bd9Sstevel@tonic-gate sctp_saddr_fini()
13077c478bd9Sstevel@tonic-gate {
13087c478bd9Sstevel@tonic-gate 	int	i;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	rw_destroy(&sctp_g_ills_lock);
13117c478bd9Sstevel@tonic-gate 	rw_destroy(&sctp_g_ipifs_lock);
13127c478bd9Sstevel@tonic-gate 	ASSERT(sctp_ills_count == 0 && sctp_g_ipifs_count == 0);
13137c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
13147c478bd9Sstevel@tonic-gate 		list_destroy(&sctp_g_ills[i].sctp_ill_list);
13157c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
13167c478bd9Sstevel@tonic-gate 		list_destroy(&sctp_g_ipifs[i].sctp_ipif_list);
13177c478bd9Sstevel@tonic-gate }
1318