xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_addr.c (revision 852:e823a5cfc1c2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23252Svi117747  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/list.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <netinet/in.h>
400Sstevel@tonic-gate #include <netinet/ip6.h>
410Sstevel@tonic-gate #include <netinet/sctp.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <inet/common.h>
440Sstevel@tonic-gate #include <inet/ip.h>
450Sstevel@tonic-gate #include <inet/ip6.h>
460Sstevel@tonic-gate #include <inet/ip_if.h>
470Sstevel@tonic-gate #include <inet/ipclassifier.h>
480Sstevel@tonic-gate #include <inet/sctp_ip.h>
490Sstevel@tonic-gate #include "sctp_impl.h"
500Sstevel@tonic-gate #include "sctp_addr.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
530Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
54*852Svi117747 			    zoneid_t zoneid, uint_t);
550Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
56*852Svi117747 int			sctp_valid_addr_list(sctp_t *, const void *, uint32_t,
57*852Svi117747 			    uchar_t *, size_t);
580Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_ipif_lookup(sctp_t *, uint_t);
59432Svi117747 static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
60432Svi117747 			    boolean_t dontsrc);
610Sstevel@tonic-gate static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
620Sstevel@tonic-gate static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
630Sstevel@tonic-gate 			    sctp_ipif_hash_t *);
640Sstevel@tonic-gate int			sctp_compare_saddrs(sctp_t *, sctp_t *);
650Sstevel@tonic-gate static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
660Sstevel@tonic-gate int			sctp_dup_saddrs(sctp_t *, sctp_t *, int);
670Sstevel@tonic-gate void			sctp_free_saddrs(sctp_t *);
680Sstevel@tonic-gate void			sctp_update_ill(ill_t *, int);
690Sstevel@tonic-gate void			sctp_update_ipif(ipif_t *, int);
700Sstevel@tonic-gate void			sctp_move_ipif(ipif_t *, ill_t *, ill_t *);
710Sstevel@tonic-gate void			sctp_del_saddr(sctp_t *, sctp_saddr_ipif_t *);
720Sstevel@tonic-gate void			sctp_del_saddr_list(sctp_t *, const void *, int,
730Sstevel@tonic-gate 			    boolean_t);
74*852Svi117747 sctp_saddr_ipif_t	*sctp_saddr_lookup(sctp_t *, in6_addr_t *, uint_t);
750Sstevel@tonic-gate in6_addr_t		sctp_get_valid_addr(sctp_t *, boolean_t);
760Sstevel@tonic-gate int			sctp_getmyaddrs(void *, void *, int *);
770Sstevel@tonic-gate void			sctp_saddr_init();
780Sstevel@tonic-gate void			sctp_saddr_fini();
79432Svi117747 
800Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
810Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
82432Svi117747 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
83432Svi117747 
84432Svi117747 #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
85432Svi117747 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
86432Svi117747 
87*852Svi117747 #define	SCTP_IS_IPIF_LOOPBACK(ipif)		\
88*852Svi117747 	((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK)
89*852Svi117747 
90*852Svi117747 #define	SCTP_IS_IPIF_LINKLOCAL(ipif)		\
91*852Svi117747 	((ipif)->sctp_ipif_isv6 && 		\
92*852Svi117747 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr))
93432Svi117747 
94432Svi117747 #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
95432Svi117747 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
96432Svi117747 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
990Sstevel@tonic-gate #define	SCTP_IPIF_HASH_FN(seqid)	((seqid) % SCTP_IPIF_HASH)
1000Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /* Global list of SCTP ILLs */
1030Sstevel@tonic-gate sctp_ill_hash_t	sctp_g_ills[SCTP_ILL_HASH];
1040Sstevel@tonic-gate uint32_t	sctp_ills_count = 0;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /* Global list of SCTP IPIFs */
1070Sstevel@tonic-gate sctp_ipif_hash_t	sctp_g_ipifs[SCTP_IPIF_HASH];
108*852Svi117747 uint32_t		sctp_g_ipifs_count = 0;
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  *
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1190Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1200Sstevel@tonic-gate  * Called with no locks held.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate static void
1230Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
1260Sstevel@tonic-gate 	uint_t		ipif_index;
1270Sstevel@tonic-gate 	uint_t		ill_index;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
1300Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(sctp_ipif->sctp_ipif_id);
1330Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1340Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1350Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1360Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1370Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
1380Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
1390Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
1400Sstevel@tonic-gate 		return;
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 	list_remove(&sctp_g_ipifs[ipif_index].sctp_ipif_list, sctp_ipif);
1430Sstevel@tonic-gate 	sctp_g_ipifs[ipif_index].ipif_count--;
1440Sstevel@tonic-gate 	sctp_g_ipifs_count--;
1450Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1460Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1490Sstevel@tonic-gate 	if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
1500Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
1510Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1520Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1530Sstevel@tonic-gate 			list_remove(&sctp_g_ills[ill_index].sctp_ill_list,
1540Sstevel@tonic-gate 			    (void *)sctp_ill);
1550Sstevel@tonic-gate 			sctp_g_ills[ill_index].ill_count--;
1560Sstevel@tonic-gate 			sctp_ills_count--;
1570Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1580Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1590Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1600Sstevel@tonic-gate 		}
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
1630Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
1680Sstevel@tonic-gate  * Called with no locks held.
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate static sctp_ipif_t *
171*852Svi117747 sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid,
172*852Svi117747     uint_t ifindex)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	int		i;
1750Sstevel@tonic-gate 	int		j;
1760Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
1790Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1800Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
1810Sstevel@tonic-gate 			continue;
1820Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
1830Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
1840Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1850Sstevel@tonic-gate 			if (zoneid == sctp_ipif->sctp_ipif_zoneid &&
1860Sstevel@tonic-gate 			    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) &&
187*852Svi117747 			    (ifindex == 0 || ifindex ==
188*852Svi117747 			    sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
1890Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1900Sstevel@tonic-gate 			    addr)) {
1910Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1920Sstevel@tonic-gate 				if (refhold)
1930Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
1940Sstevel@tonic-gate 				rw_exit(&sctp_g_ipifs_lock);
1950Sstevel@tonic-gate 				return (sctp_ipif);
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1980Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
1990Sstevel@tonic-gate 			    sctp_ipif);
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2030Sstevel@tonic-gate 	return (NULL);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
2080Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
2090Sstevel@tonic-gate  * Called with no locks held.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static int
2120Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
2150Sstevel@tonic-gate 	int			i;
2160Sstevel@tonic-gate 	int			j;
2170Sstevel@tonic-gate 	int			error = 0;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
2200Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
2210Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
2220Sstevel@tonic-gate 			continue;
2230Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
2240Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
2250Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
226432Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
2270Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
228432Svi117747 			    sctp_ipif->sctp_ipif_zoneid != sctp->sctp_zoneid ||
2290Sstevel@tonic-gate 			    (sctp->sctp_ipversion == IPV4_VERSION &&
230432Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
2310Sstevel@tonic-gate 			    (sctp->sctp_connp->conn_ipv6_v6only &&
232432Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
2330Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2340Sstevel@tonic-gate 				sctp_ipif = list_next(
2350Sstevel@tonic-gate 				    &sctp_g_ipifs[i].sctp_ipif_list, sctp_ipif);
2360Sstevel@tonic-gate 				continue;
2370Sstevel@tonic-gate 			}
2380Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2390Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
240432Svi117747 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
241432Svi117747 			    B_FALSE);
2420Sstevel@tonic-gate 			if (error != 0)
2430Sstevel@tonic-gate 				goto free_stuff;
2440Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
2450Sstevel@tonic-gate 			    sctp_ipif);
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2490Sstevel@tonic-gate 	return (0);
2500Sstevel@tonic-gate free_stuff:
2510Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2520Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2530Sstevel@tonic-gate 	return (ENOMEM);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2580Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
259*852Svi117747  * or error. If the caller wants the list of addresses, it sends a pre-allocated
260*852Svi117747  * buffer - list. Currently, this list is only used on a clustered node when
261*852Svi117747  * the SCTP is in the listen state (from sctp_bind_add()). When called on a
262*852Svi117747  * clustered node, the input is always a list of addresses (even if the
263*852Svi117747  * original bind() was to INADDR_ANY).
2640Sstevel@tonic-gate  * Called with no locks held.
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate int
267*852Svi117747 sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
268*852Svi117747     uchar_t *list, size_t lsize)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2710Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2720Sstevel@tonic-gate 	struct in_addr		*addr4;
2730Sstevel@tonic-gate 	in6_addr_t		addr;
2740Sstevel@tonic-gate 	int			cnt;
2750Sstevel@tonic-gate 	int			err = 0;
2760Sstevel@tonic-gate 	int			saddr_cnt = 0;
2770Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
2780Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
2790Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
2800Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
281*852Svi117747 	uchar_t			*p = list;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/*
2840Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
2850Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
2860Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
2870Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
2880Sstevel@tonic-gate 	 * addresses.
2890Sstevel@tonic-gate 	 */
2900Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
2910Sstevel@tonic-gate 		check_lport = B_TRUE;
2920Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
2930Sstevel@tonic-gate 			check_addrs = B_TRUE;
2940Sstevel@tonic-gate 	}
295*852Svi117747 
2960Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
2970Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
2980Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
2990Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
3000Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
3010Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
302*852Svi117747 		uint_t		ifindex = 0;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		switch (sctp->sctp_family) {
3050Sstevel@tonic-gate 		case AF_INET:
3060Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
3070Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
3080Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
3090Sstevel@tonic-gate 				err = EINVAL;
3100Sstevel@tonic-gate 				goto free_ret;
3110Sstevel@tonic-gate 			}
3120Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
3130Sstevel@tonic-gate 			if (check_addrs &&
3140Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
3150Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
3160Sstevel@tonic-gate 			    IN_MULTICAST(addr4->s_addr))) {
3170Sstevel@tonic-gate 				err = EINVAL;
3180Sstevel@tonic-gate 				goto free_ret;
3190Sstevel@tonic-gate 			}
3200Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
3210Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
3220Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3230Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3240Sstevel@tonic-gate 			}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 			break;
3270Sstevel@tonic-gate 		case AF_INET6:
3280Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3290Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
3300Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
3310Sstevel@tonic-gate 				err = EINVAL;
3320Sstevel@tonic-gate 				goto free_ret;
3330Sstevel@tonic-gate 			}
3340Sstevel@tonic-gate 			addr = sin6->sin6_addr;
335*852Svi117747 			/* Contains the interface index */
336*852Svi117747 			ifindex = sin6->sin6_scope_id;
3370Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
3380Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3390Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3400Sstevel@tonic-gate 				goto free_ret;
3410Sstevel@tonic-gate 			}
3420Sstevel@tonic-gate 			if (check_addrs &&
3430Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3440Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3450Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3460Sstevel@tonic-gate 				err = EINVAL;
3470Sstevel@tonic-gate 				goto free_ret;
3480Sstevel@tonic-gate 			}
3490Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3500Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3510Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3520Sstevel@tonic-gate 			}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			break;
3550Sstevel@tonic-gate 		default:
3560Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3570Sstevel@tonic-gate 			goto free_ret;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 		if (lookup_saddr) {
3600Sstevel@tonic-gate 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
361*852Svi117747 			    sctp->sctp_zoneid, ifindex);
3620Sstevel@tonic-gate 			if (ipif == NULL) {
3630Sstevel@tonic-gate 				/* Address not in the list */
3640Sstevel@tonic-gate 				err = EINVAL;
3650Sstevel@tonic-gate 				goto free_ret;
366*852Svi117747 			} else if (check_addrs && SCTP_IS_IPIF_LOOPBACK(ipif) &&
367*852Svi117747 			    cl_sctp_check_addrs == NULL) {
3680Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3690Sstevel@tonic-gate 				err = EINVAL;
3700Sstevel@tonic-gate 				goto free_ret;
3710Sstevel@tonic-gate 			}
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 		if (!bind_to_all) {
374432Svi117747 			/*
375432Svi117747 			 * If an address is added after association setup,
376432Svi117747 			 * we need to wait for the peer to send us an ASCONF
377432Svi117747 			 * ACK before we can start using it.
378432Svi117747 			 * saddr_ipif_dontsrc will be reset (to 0) when we
379432Svi117747 			 * get the ASCONF ACK for this address.
380432Svi117747 			 */
381432Svi117747 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
382432Svi117747 			    check_addrs ? B_TRUE : B_FALSE);
3830Sstevel@tonic-gate 			if (err != 0) {
3840Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3850Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
3860Sstevel@tonic-gate 					err = EADDRINUSE;
3870Sstevel@tonic-gate 				goto free_ret;
3880Sstevel@tonic-gate 			}
3890Sstevel@tonic-gate 			saddr_cnt++;
390*852Svi117747 			if (lsize >= sizeof (addr)) {
391*852Svi117747 				bcopy(&addr, p, sizeof (addr));
392*852Svi117747 				p += sizeof (addr);
393*852Svi117747 				lsize -= sizeof (addr);
394*852Svi117747 			}
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	if (bind_to_all) {
3980Sstevel@tonic-gate 		/*
3990Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
4000Sstevel@tonic-gate 		 * inaddr_any.
4010Sstevel@tonic-gate 		 */
4020Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
4030Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
4040Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
4050Sstevel@tonic-gate 		}
4060Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
4070Sstevel@tonic-gate 		if (err != 0)
4080Sstevel@tonic-gate 			return (err);
4090Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4120Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4130Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4140Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4150Sstevel@tonic-gate 	return (0);
4160Sstevel@tonic-gate free_ret:
4170Sstevel@tonic-gate 	if (saddr_cnt != 0)
4180Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
4190Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4200Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4210Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4220Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4230Sstevel@tonic-gate 	return (err);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate sctp_saddr_ipif_t *
4270Sstevel@tonic-gate sctp_ipif_lookup(sctp_t *sctp, uint_t ipif_index)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	int			cnt;
4300Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif_index);
4310Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (sctp->sctp_saddrs[seqid].ipif_count == 0)
4340Sstevel@tonic-gate 		return (NULL);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4370Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4380Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif_index)
4390Sstevel@tonic-gate 			return (ipif_obj);
4400Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4410Sstevel@tonic-gate 		    ipif_obj);
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 	return (NULL);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate static int
447432Svi117747 sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
448432Svi117747     boolean_t dontsrc)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	int			cnt;
4510Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4520Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4550Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4560Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id)
4570Sstevel@tonic-gate 			return (EALREADY);
4580Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4590Sstevel@tonic-gate 		    ipif_obj);
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4620Sstevel@tonic-gate 	if (ipif_obj == NULL) {
4630Sstevel@tonic-gate 		/* Need to do something */
4640Sstevel@tonic-gate 		return (ENOMEM);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
467432Svi117747 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
4680Sstevel@tonic-gate 	list_insert_tail(&sctp->sctp_saddrs[seqid].sctp_ipif_list, ipif_obj);
4690Sstevel@tonic-gate 	sctp->sctp_saddrs[seqid].ipif_count++;
4700Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
4710Sstevel@tonic-gate 	return (0);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate static void
4750Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	int			cnt;
4780Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4790Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4820Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4830Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) {
4840Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4850Sstevel@tonic-gate 			    ipif_obj);
4860Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
4870Sstevel@tonic-gate 			sctp->sctp_saddrs[seqid].ipif_count--;
4880Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
4890Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
4900Sstevel@tonic-gate 			break;
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4930Sstevel@tonic-gate 		    ipif_obj);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate static int
4980Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	int			i;
5010Sstevel@tonic-gate 	int			j;
5020Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
5030Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
5040Sstevel@tonic-gate 	int			overlap = 0;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
5070Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5080Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
5090Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
5100Sstevel@tonic-gate 			if (obj1->saddr_ipifp->sctp_ipif_id ==
5110Sstevel@tonic-gate 			    obj2->saddr_ipifp->sctp_ipif_id) {
5120Sstevel@tonic-gate 				overlap++;
5130Sstevel@tonic-gate 				break;
5140Sstevel@tonic-gate 			}
5150Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
5160Sstevel@tonic-gate 			    obj2);
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	return (overlap);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate int
5240Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	int		i;
5270Sstevel@tonic-gate 	int		overlap = 0;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5300Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
5310Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5350Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5360Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
5400Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (overlap > 0)
5430Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate static int
5490Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	int			i;
5520Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5530Sstevel@tonic-gate 	int			error = 0;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
5560Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5570Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
558432Svi117747 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
559432Svi117747 		    B_FALSE);
5600Sstevel@tonic-gate 		if (error != 0)
5610Sstevel@tonic-gate 			return (error);
5620Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 	return (error);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate int
5680Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate 	int	error = 0;
5710Sstevel@tonic-gate 	int	i;
5720Sstevel@tonic-gate 
573432Svi117747 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
5740Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5770Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
5780Sstevel@tonic-gate 			continue;
5790Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
5800Sstevel@tonic-gate 		if (error != 0) {
5810Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
5820Sstevel@tonic-gate 			return (error);
5830Sstevel@tonic-gate 		}
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 	return (0);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate void
5890Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	int			i;
5920Sstevel@tonic-gate 	int			l;
5930Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
5960Sstevel@tonic-gate 		return;
5970Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5980Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
5990Sstevel@tonic-gate 			continue;
6000Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6010Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
6020Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
6030Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
6040Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
6050Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
6060Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6070Sstevel@tonic-gate 		}
6080Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
6090Sstevel@tonic-gate 	}
610432Svi117747 	if (sctp->sctp_bound_to_all == 1)
611432Svi117747 		sctp->sctp_bound_to_all = 0;
6120Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
6170Sstevel@tonic-gate  * held.
6180Sstevel@tonic-gate  */
6190Sstevel@tonic-gate void
6200Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	int		i;
6230Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
6240Sstevel@tonic-gate 	uint_t		index;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	ip2dbg(("sctp_update_ill: %s\n", ill->ill_name));
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_WRITER);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
6310Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
6320Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
6330Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
6340Sstevel@tonic-gate 			break;
6350Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
6360Sstevel@tonic-gate 		    sctp_ill);
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	switch (op) {
6400Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
6410Sstevel@tonic-gate 		if (sctp_ill != NULL) {
6420Sstevel@tonic-gate 			/* Unmark it if it is condemned */
6430Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
6440Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
6450Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6460Sstevel@tonic-gate 			return;
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
6490Sstevel@tonic-gate 		/* Need to re-try? */
6500Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6510Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6520Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6530Sstevel@tonic-gate 			return;
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 		sctp_ill->sctp_ill_name =
6560Sstevel@tonic-gate 		    kmem_zalloc(ill->ill_name_length, KM_NOSLEEP);
6570Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
6580Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6590Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6600Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6610Sstevel@tonic-gate 			return;
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
6640Sstevel@tonic-gate 		    ill->ill_name_length);
6650Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
6660Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
6670Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
6680Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ills[index].sctp_ill_list,
6690Sstevel@tonic-gate 		    (void *)sctp_ill);
6700Sstevel@tonic-gate 		sctp_g_ills[index].ill_count++;
6710Sstevel@tonic-gate 		sctp_ills_count++;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		break;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6780Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6790Sstevel@tonic-gate 			return;
6800Sstevel@tonic-gate 		}
6810Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
6820Sstevel@tonic-gate 			list_remove(&sctp_g_ills[index].sctp_ill_list,
6830Sstevel@tonic-gate 			    (void *)sctp_ill);
6840Sstevel@tonic-gate 			sctp_g_ills[index].ill_count--;
6850Sstevel@tonic-gate 			sctp_ills_count--;
6860Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
6870Sstevel@tonic-gate 			    ill->ill_name_length);
6880Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6890Sstevel@tonic-gate 		} else {
6900Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		break;
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
6990Sstevel@tonic-gate void
7000Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
7030Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
7040Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7050Sstevel@tonic-gate 	uint_t		index;
7060Sstevel@tonic-gate 	int		i;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
7090Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
7120Sstevel@tonic-gate 	fsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
7130Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
7140Sstevel@tonic-gate 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill))
7150Sstevel@tonic-gate 			break;
7160Sstevel@tonic-gate 		fsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
7170Sstevel@tonic-gate 		    fsctp_ill);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
7210Sstevel@tonic-gate 	tsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
7220Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
7230Sstevel@tonic-gate 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill))
7240Sstevel@tonic-gate 			break;
7250Sstevel@tonic-gate 		tsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
7260Sstevel@tonic-gate 		    tsctp_ill);
7270Sstevel@tonic-gate 	}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7300Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[index].sctp_ipif_list);
7310Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[index].ipif_count; i++) {
7320Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7330Sstevel@tonic-gate 			break;
7340Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[index].sctp_ipif_list,
7350Sstevel@tonic-gate 		    sctp_ipif);
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 	/* Should be an ASSERT? */
7380Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
7390Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
7400Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
7410Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7420Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7430Sstevel@tonic-gate 		return;
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
7460Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
7470Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
7480Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
7490Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
7500Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
7510Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
7520Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
7560Sstevel@tonic-gate void
7570Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
7600Sstevel@tonic-gate 	int		i;
7610Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
7620Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7630Sstevel@tonic-gate 	uint_t		ill_index;
7640Sstevel@tonic-gate 	uint_t		ipif_index;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
7690Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
7720Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[ill_index].sctp_ill_list);
7730Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[ill_index].ill_count; i++) {
7740Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
7750Sstevel@tonic-gate 			break;
7760Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[ill_index].sctp_ill_list,
7770Sstevel@tonic-gate 		    sctp_ill);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 	if (sctp_ill == NULL) {
7800Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7810Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7820Sstevel@tonic-gate 		return;
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7860Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[ipif_index].sctp_ipif_list);
7870Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[ipif_index].ipif_count; i++) {
7880Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7890Sstevel@tonic-gate 			break;
7900Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
7910Sstevel@tonic-gate 		    sctp_ipif);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 	if (op != SCTP_IPIF_INSERT && sctp_ipif == NULL) {
7940Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
7950Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7960Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7970Sstevel@tonic-gate 		return;
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate #ifdef	DEBUG
8000Sstevel@tonic-gate 	if (sctp_ipif != NULL)
8010Sstevel@tonic-gate 		ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
8020Sstevel@tonic-gate #endif
8030Sstevel@tonic-gate 	switch (op) {
8040Sstevel@tonic-gate 	case SCTP_IPIF_INSERT:
8050Sstevel@tonic-gate 		if (sctp_ipif != NULL) {
8060Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED)
807252Svi117747 				sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
8080Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8090Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8100Sstevel@tonic-gate 			return;
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 		sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
8130Sstevel@tonic-gate 		/* Try again? */
8140Sstevel@tonic-gate 		if (sctp_ipif == NULL) {
8150Sstevel@tonic-gate 			ip1dbg(("sctp_ipif_insert: mem failure..\n"));
8160Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8170Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8180Sstevel@tonic-gate 			return;
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
8210Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_ill = sctp_ill;
822252Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
8230Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8240Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
8250Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
826432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8270Sstevel@tonic-gate 		rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
8280Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
8290Sstevel@tonic-gate 		    (void *)sctp_ipif);
8300Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count++;
8310Sstevel@tonic-gate 		sctp_g_ipifs_count++;
8320Sstevel@tonic-gate 		atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		break;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
8370Sstevel@tonic-gate 	{
8380Sstevel@tonic-gate 		list_t		*ipif_list;
8390Sstevel@tonic-gate 		list_t		*ill_list;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		ill_list = &sctp_g_ills[ill_index].sctp_ill_list;
8420Sstevel@tonic-gate 		ipif_list = &sctp_g_ipifs[ipif_index].sctp_ipif_list;
8430Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
8440Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
8450Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8460Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8470Sstevel@tonic-gate 			return;
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
8500Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count--;
8510Sstevel@tonic-gate 		sctp_g_ipifs_count--;
8520Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
8530Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
8540Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
8550Sstevel@tonic-gate 		if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
8560Sstevel@tonic-gate 			rw_downgrade(&sctp_g_ipifs_lock);
8570Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
8580Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
8590Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
8600Sstevel@tonic-gate 				sctp_ills_count--;
8610Sstevel@tonic-gate 				sctp_g_ills[ill_index].ill_count--;
8620Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
8630Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
8640Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
8650Sstevel@tonic-gate 			}
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate 		break;
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	case SCTP_IPIF_UP:
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8730Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8740Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
8750Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
876432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
877432Svi117747 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8780Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 		break;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8850Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8860Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8870Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
8880Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
889432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8900Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 		break;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8970Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8980Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
8990Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		break;
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
9040Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate  *
9090Sstevel@tonic-gate  *
9100Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
9110Sstevel@tonic-gate  * sctp locking by the caller.
9120Sstevel@tonic-gate  *
9130Sstevel@tonic-gate  *
9140Sstevel@tonic-gate  */
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate /* Remove a specific saddr from the list */
9170Sstevel@tonic-gate void
9180Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9210Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9240Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
9270Sstevel@tonic-gate 
928432Svi117747 	if (sctp->sctp_bound_to_all == 1)
9290Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9320Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9350Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate /*
9390Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
9400Sstevel@tonic-gate  * Called with no locks held.
9410Sstevel@tonic-gate  */
9420Sstevel@tonic-gate void
9430Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
9440Sstevel@tonic-gate     boolean_t fanout_locked)
9450Sstevel@tonic-gate {
9460Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
9470Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
9480Sstevel@tonic-gate 	int			cnt;
9490Sstevel@tonic-gate 	in6_addr_t		addr;
9500Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
951*852Svi117747 	int			ifindex = 0;
9520Sstevel@tonic-gate 
953*852Svi117747 	ASSERT(sctp->sctp_nsaddrs >= addcnt);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	if (!fanout_locked) {
9560Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9570Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9580Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9590Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9600Sstevel@tonic-gate 	}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
9630Sstevel@tonic-gate 		switch (sctp->sctp_family) {
9640Sstevel@tonic-gate 		case AF_INET:
9650Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
9660Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
9670Sstevel@tonic-gate 			break;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 		case AF_INET6:
9700Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
9710Sstevel@tonic-gate 			addr = sin6->sin6_addr;
972*852Svi117747 			ifindex = sin6->sin6_scope_id;
9730Sstevel@tonic-gate 			break;
9740Sstevel@tonic-gate 		}
9750Sstevel@tonic-gate 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
976*852Svi117747 		    sctp->sctp_zoneid, ifindex);
9770Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
9780Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
9790Sstevel@tonic-gate 	}
980432Svi117747 	if (sctp->sctp_bound_to_all == 1)
9810Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	if (!fanout_locked) {
9840Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9850Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9860Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9870Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
9930Sstevel@tonic-gate  * Called with no locks held.
9940Sstevel@tonic-gate  */
9950Sstevel@tonic-gate sctp_saddr_ipif_t *
996*852Svi117747 sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	sctp_saddr_ipif_t	*saddr_ipifs;
9990Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
10000Sstevel@tonic-gate 
1001*852Svi117747 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_FALSE, sctp->sctp_zoneid,
1002*852Svi117747 	    ifindex);
10030Sstevel@tonic-gate 	if (sctp_ipif == NULL)
10040Sstevel@tonic-gate 		return (NULL);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	saddr_ipifs = sctp_ipif_lookup(sctp, sctp_ipif->sctp_ipif_id);
10070Sstevel@tonic-gate 	return (saddr_ipifs);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate 
1010432Svi117747 /* Given an address, add it to the source address list */
1011432Svi117747 int
1012*852Svi117747 sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1013432Svi117747 {
1014432Svi117747 	sctp_ipif_t		*sctp_ipif;
1015432Svi117747 
1016*852Svi117747 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp->sctp_zoneid,
1017*852Svi117747 	    ifindex);
1018432Svi117747 	if (sctp_ipif == NULL)
1019432Svi117747 		return (EINVAL);
1020432Svi117747 
1021432Svi117747 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE) != 0) {
1022432Svi117747 		SCTP_IPIF_REFRELE(sctp_ipif);
1023432Svi117747 		return (EINVAL);
1024432Svi117747 	}
1025432Svi117747 	return (0);
1026432Svi117747 }
1027432Svi117747 
1028432Svi117747 /*
1029432Svi117747  * Remove or mark as dontsrc addresses that are currently not part of the
1030432Svi117747  * association. One would delete addresses when processing an INIT and
1031432Svi117747  * mark as dontsrc when processing an INIT-ACK.
1032432Svi117747  */
1033432Svi117747 void
1034432Svi117747 sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete)
1035432Svi117747 {
1036432Svi117747 	int			i;
1037432Svi117747 	int			l;
1038432Svi117747 	sctp_saddr_ipif_t	*obj;
1039432Svi117747 	int			scanned = 0;
1040432Svi117747 	int			naddr;
1041432Svi117747 	int			nsaddr;
1042432Svi117747 
1043432Svi117747 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1044432Svi117747 
1045432Svi117747 	/*
1046432Svi117747 	 * Irregardless of the supported address in the INIT, v4
1047432Svi117747 	 * must be supported.
1048432Svi117747 	 */
1049432Svi117747 	if (sctp->sctp_family == AF_INET)
1050432Svi117747 		supp_af = PARM_SUPP_V4;
1051432Svi117747 
1052432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
1053432Svi117747 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1054432Svi117747 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1055432Svi117747 			continue;
1056432Svi117747 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1057432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1058432Svi117747 		for (l = 0; l < naddr; l++) {
1059432Svi117747 			sctp_ipif_t	*ipif;
1060432Svi117747 
1061432Svi117747 			ipif = obj->saddr_ipifp;
1062432Svi117747 			scanned++;
1063432Svi117747 
1064432Svi117747 			/*
1065432Svi117747 			 * Delete/mark dontsrc loopback/linklocal addresses and
1066432Svi117747 			 * unsupported address.
1067*852Svi117747 			 * On a clustered node, we trust the clustering module
1068*852Svi117747 			 * to do the right thing w.r.t loopback addresses, so
1069*852Svi117747 			 * we ignore loopback addresses in this check.
1070432Svi117747 			 */
1071*852Svi117747 			if ((SCTP_IS_IPIF_LOOPBACK(ipif) &&
1072*852Svi117747 			    cl_sctp_check_addrs == NULL) ||
1073*852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(ipif) ||
1074432Svi117747 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1075432Svi117747 				if (!delete) {
1076432Svi117747 					obj->saddr_ipif_unconfirmed = 1;
1077432Svi117747 					goto next_obj;
1078432Svi117747 				}
1079432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1080432Svi117747 					sctp->sctp_bound_to_all = 0;
1081432Svi117747 				if (scanned < nsaddr) {
1082432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1083432Svi117747 					    sctp_ipif_list, obj);
1084432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1085432Svi117747 					continue;
1086432Svi117747 				}
1087432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1088432Svi117747 			}
1089432Svi117747 	next_obj:
1090432Svi117747 			if (scanned >= nsaddr)
1091432Svi117747 				return;
1092432Svi117747 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1093432Svi117747 			    obj);
1094432Svi117747 		}
1095432Svi117747 	}
1096432Svi117747 }
1097432Svi117747 
1098432Svi117747 
10990Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
11000Sstevel@tonic-gate in6_addr_t
11010Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate 	int			i;
11040Sstevel@tonic-gate 	int			l;
11050Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
11060Sstevel@tonic-gate 	int			scanned = 0;
11070Sstevel@tonic-gate 	in6_addr_t		addr;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11100Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11110Sstevel@tonic-gate 			continue;
11120Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11130Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11140Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1117432Svi117747 			if (!SCTP_DONT_SRC(obj) &&
11180Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
1119432Svi117747 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
11200Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
11210Sstevel@tonic-gate 			}
11220Sstevel@tonic-gate 			scanned++;
11230Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
11240Sstevel@tonic-gate 				goto got_none;
11250Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
11260Sstevel@tonic-gate 			    obj);
11270Sstevel@tonic-gate 		}
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate got_none:
11300Sstevel@tonic-gate 	/* Need to double check this */
11310Sstevel@tonic-gate 	if (isv6 == B_TRUE)
11320Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
11330Sstevel@tonic-gate 	else
11340Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	return (addr);
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate /*
11400Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
11410Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
11420Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
11430Sstevel@tonic-gate  */
11440Sstevel@tonic-gate int
11450Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate 	int			i;
11480Sstevel@tonic-gate 	int			l;
11490Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
11500Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
11510Sstevel@tonic-gate 	int			family = sctp->sctp_family;
11520Sstevel@tonic-gate 	int			max = *addrcnt;
11530Sstevel@tonic-gate 	size_t			added = 0;
11540Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
11550Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
11560Sstevel@tonic-gate 	int			scanned = 0;
11570Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
11600Sstevel@tonic-gate 		return (EINVAL);
11610Sstevel@tonic-gate 
1162*852Svi117747 	/*
1163*852Svi117747 	 * Skip loopback addresses for non-loopback assoc., ignore
1164*852Svi117747 	 * this on a clustered node.
1165*852Svi117747 	 */
1166*852Svi117747 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback &&
1167*852Svi117747 	    (cl_sctp_check_addrs == NULL)) {
11680Sstevel@tonic-gate 		skip_lback = B_TRUE;
1169*852Svi117747 	}
1170*852Svi117747 
11710Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11720Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11730Sstevel@tonic-gate 			continue;
11740Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11750Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11760Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
11770Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 			scanned++;
11800Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1181432Svi117747 			    SCTP_DONT_SRC(obj) ||
1182*852Svi117747 			    (SCTP_IS_IPIF_LOOPBACK(ipif) && skip_lback)) {
11830Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
11840Sstevel@tonic-gate 					goto done;
11850Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
11860Sstevel@tonic-gate 				    sctp_ipif_list, obj);
11870Sstevel@tonic-gate 				continue;
11880Sstevel@tonic-gate 			}
11890Sstevel@tonic-gate 			switch (family) {
11900Sstevel@tonic-gate 			case AF_INET:
11910Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
11920Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
11930Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
11940Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
11950Sstevel@tonic-gate 				break;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 			case AF_INET6:
11980Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
11990Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
12000Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
12010Sstevel@tonic-gate 				sin6->sin6_addr = addr;
12020Sstevel@tonic-gate 				break;
12030Sstevel@tonic-gate 			}
12040Sstevel@tonic-gate 			added++;
12050Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
12060Sstevel@tonic-gate 				goto done;
12070Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
12080Sstevel@tonic-gate 			    obj);
12090Sstevel@tonic-gate 		}
12100Sstevel@tonic-gate 	}
12110Sstevel@tonic-gate done:
12120Sstevel@tonic-gate 	*addrcnt = added;
12130Sstevel@tonic-gate 	return (0);
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate /*
1217252Svi117747  * Given the supported address family, walk through the source address list
1218252Svi117747  * and return the total length of the available addresses. If 'p' is not
1219252Svi117747  * null, construct the parameter list for the addresses in 'p'.
1220432Svi117747  * 'modify' will only be set when we want the source address list to
1221432Svi117747  * be modified. The source address list will be modified only when
1222432Svi117747  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1223432Svi117747  * be false since the 'sctp' will be that of the listener.
12240Sstevel@tonic-gate  */
12250Sstevel@tonic-gate size_t
1226432Svi117747 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
12270Sstevel@tonic-gate {
12280Sstevel@tonic-gate 	int			i;
12290Sstevel@tonic-gate 	int			l;
12300Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1231252Svi117747 	size_t			paramlen = 0;
12320Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
12330Sstevel@tonic-gate 	int			scanned = 0;
1234432Svi117747 	int			naddr;
1235432Svi117747 	int			nsaddr;
1236*852Svi117747 	boolean_t		del_ll = B_FALSE;
1237*852Svi117747 	boolean_t		del_lb = B_FALSE;
1238*852Svi117747 
12390Sstevel@tonic-gate 
1240*852Svi117747 	/*
1241*852Svi117747 	 * On a clustered node don't bother changing anything
1242*852Svi117747 	 * on the loopback interface.
1243*852Svi117747 	 */
1244*852Svi117747 	if (modify && !sctp->sctp_loopback && (cl_sctp_check_addrs == NULL))
1245*852Svi117747 		del_lb = B_TRUE;
1246*852Svi117747 
1247*852Svi117747 	if (modify && !sctp->sctp_linklocal)
1248*852Svi117747 		del_ll = B_TRUE;
1249432Svi117747 
1250432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
12510Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
12520Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
12530Sstevel@tonic-gate 			continue;
12540Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1255432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1256432Svi117747 		for (l = 0; l < naddr; l++) {
12570Sstevel@tonic-gate 			in6_addr_t	addr;
12580Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
1259*852Svi117747 			boolean_t	ipif_lb;
1260*852Svi117747 			boolean_t	ipif_ll;
1261432Svi117747 			boolean_t	unsupp_af;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
12640Sstevel@tonic-gate 			scanned++;
1265432Svi117747 
1266*852Svi117747 			ipif_lb = SCTP_IS_IPIF_LOOPBACK(ipif);
1267*852Svi117747 			ipif_ll = SCTP_IS_IPIF_LINKLOCAL(ipif);
1268432Svi117747 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1269432Svi117747 			/*
1270432Svi117747 			 * We need to either delete or skip loopback/linklocal
1271*852Svi117747 			 * or unsupported addresses, if required.
1272432Svi117747 			 */
1273*852Svi117747 			if ((ipif_ll && del_ll) || (ipif_lb && del_lb) ||
1274*852Svi117747 			    (unsupp_af && modify)) {
1275432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1276432Svi117747 					sctp->sctp_bound_to_all = 0;
1277432Svi117747 				if (scanned < nsaddr) {
1278432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1279432Svi117747 					    sctp_ipif_list, obj);
1280432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1281432Svi117747 					continue;
1282432Svi117747 				}
1283432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1284432Svi117747 				goto next_addr;
1285*852Svi117747 			} else if (ipif_ll || unsupp_af ||
1286*852Svi117747 			    (ipif_lb && (cl_sctp_check_addrs == NULL))) {
1287252Svi117747 				goto next_addr;
12880Sstevel@tonic-gate 			}
1289432Svi117747 
1290432Svi117747 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1291432Svi117747 				goto next_addr;
1292252Svi117747 			if (p != NULL)
1293252Svi117747 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
12940Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1295432Svi117747 			if (!ipif->sctp_ipif_isv6) {
12960Sstevel@tonic-gate 				struct in_addr	*v4;
12970Sstevel@tonic-gate 
1298252Svi117747 				if (p != NULL) {
1299252Svi117747 					hdr->sph_type = htons(PARM_ADDR4);
1300252Svi117747 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1301252Svi117747 					v4 = (struct in_addr *)(hdr + 1);
1302252Svi117747 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1303252Svi117747 				}
1304252Svi117747 				paramlen += PARM_ADDR4_LEN;
1305432Svi117747 			} else {
1306252Svi117747 				if (p != NULL) {
1307252Svi117747 					hdr->sph_type = htons(PARM_ADDR6);
1308252Svi117747 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1309252Svi117747 					bcopy(&addr, hdr + 1, sizeof (addr));
1310252Svi117747 				}
1311252Svi117747 				paramlen += PARM_ADDR6_LEN;
13120Sstevel@tonic-gate 			}
1313252Svi117747 next_addr:
1314432Svi117747 			if (scanned >= nsaddr)
1315252Svi117747 				return (paramlen);
13160Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
13170Sstevel@tonic-gate 			    obj);
13180Sstevel@tonic-gate 		}
13190Sstevel@tonic-gate 	}
1320252Svi117747 	return (paramlen);
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate 
1323*852Svi117747 /*
1324*852Svi117747  * This is used on a clustered node to obtain a list of addresses, the list
1325*852Svi117747  * consists of sockaddr_in structs for v4 and sockaddr_in6 for v6. The list
1326*852Svi117747  * is then passed onto the clustering module which sends back the correct
1327*852Svi117747  * list based on the port info. Regardless of the input, i.e INADDR_ANY
1328*852Svi117747  * or specific address(es), we create the list since it could be modified by
1329*852Svi117747  * the clustering module. When given a list of addresses, we simply
1330*852Svi117747  * create the list of sockaddr_in or sockaddr_in6 structs using those
1331*852Svi117747  * addresses. If there is an INADDR_ANY in the input list, or if the
1332*852Svi117747  * input is INADDR_ANY, we create a list of sockaddr_in or sockaddr_in6
1333*852Svi117747  * structs consisting all the addresses in the global interface list
1334*852Svi117747  * except those that are hosted on the loopback interface. We create
1335*852Svi117747  * a list of sockaddr_in[6] structs just so that it can be directly input
1336*852Svi117747  * to sctp_valid_addr_list() once the clustering module has processed it.
1337*852Svi117747  */
1338*852Svi117747 int
1339*852Svi117747 sctp_get_addrlist(sctp_t *sctp, const void *addrs, uint32_t *addrcnt,
1340*852Svi117747     uchar_t **addrlist, int *uspec, size_t *size)
1341*852Svi117747 {
1342*852Svi117747 	int			cnt;
1343*852Svi117747 	int			icnt;
1344*852Svi117747 	sctp_ipif_t		*sctp_ipif;
1345*852Svi117747 	struct sockaddr_in	*s4;
1346*852Svi117747 	struct sockaddr_in6	*s6;
1347*852Svi117747 	uchar_t			*p;
1348*852Svi117747 	int			err = 0;
1349*852Svi117747 
1350*852Svi117747 	*addrlist = NULL;
1351*852Svi117747 	*size = 0;
1352*852Svi117747 
1353*852Svi117747 	/*
1354*852Svi117747 	 * Create a list of sockaddr_in[6] structs using the input list.
1355*852Svi117747 	 */
1356*852Svi117747 	if (sctp->sctp_family == AF_INET) {
1357*852Svi117747 		*size = sizeof (struct sockaddr_in) * *addrcnt;
1358*852Svi117747 		*addrlist = kmem_zalloc(*size,  KM_SLEEP);
1359*852Svi117747 		p = *addrlist;
1360*852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1361*852Svi117747 			s4 = (struct sockaddr_in *)addrs + cnt;
1362*852Svi117747 			/*
1363*852Svi117747 			 * We need to create a list of all the available
1364*852Svi117747 			 * addresses if there is an INADDR_ANY. However,
1365*852Svi117747 			 * if we are beyond LISTEN, then this is invalid
1366*852Svi117747 			 * (see sctp_valid_addr_list(). So, we just fail
1367*852Svi117747 			 * it here rather than wait till it fails in
1368*852Svi117747 			 * sctp_valid_addr_list().
1369*852Svi117747 			 */
1370*852Svi117747 			if (s4->sin_addr.s_addr == INADDR_ANY) {
1371*852Svi117747 				kmem_free(*addrlist, *size);
1372*852Svi117747 				*addrlist = NULL;
1373*852Svi117747 				*size = 0;
1374*852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1375*852Svi117747 					*addrcnt = 0;
1376*852Svi117747 					return (EINVAL);
1377*852Svi117747 				}
1378*852Svi117747 				if (uspec != NULL)
1379*852Svi117747 					*uspec = 1;
1380*852Svi117747 				goto get_all_addrs;
1381*852Svi117747 			} else {
1382*852Svi117747 				bcopy(s4, p, sizeof (*s4));
1383*852Svi117747 				p += sizeof (*s4);
1384*852Svi117747 			}
1385*852Svi117747 		}
1386*852Svi117747 	} else {
1387*852Svi117747 		*size = sizeof (struct sockaddr_in6) * *addrcnt;
1388*852Svi117747 		*addrlist = kmem_zalloc(*size, KM_SLEEP);
1389*852Svi117747 		p = *addrlist;
1390*852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1391*852Svi117747 			s6 = (struct sockaddr_in6 *)addrs + cnt;
1392*852Svi117747 			/*
1393*852Svi117747 			 * Comments for INADDR_ANY, above, apply here too.
1394*852Svi117747 			 */
1395*852Svi117747 			if (IN6_IS_ADDR_UNSPECIFIED(&s6->sin6_addr)) {
1396*852Svi117747 				kmem_free(*addrlist, *size);
1397*852Svi117747 				*size = 0;
1398*852Svi117747 				*addrlist = NULL;
1399*852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1400*852Svi117747 					*addrcnt = 0;
1401*852Svi117747 					return (EINVAL);
1402*852Svi117747 				}
1403*852Svi117747 				if (uspec != NULL)
1404*852Svi117747 					*uspec = 1;
1405*852Svi117747 				goto get_all_addrs;
1406*852Svi117747 			} else {
1407*852Svi117747 				bcopy(addrs, p, sizeof (*s6));
1408*852Svi117747 				p += sizeof (*s6);
1409*852Svi117747 			}
1410*852Svi117747 		}
1411*852Svi117747 	}
1412*852Svi117747 	return (err);
1413*852Svi117747 get_all_addrs:
1414*852Svi117747 
1415*852Svi117747 	/*
1416*852Svi117747 	 * Allocate max possible size. We allocate the max. size here because
1417*852Svi117747 	 * the clustering module could end up adding addresses to the list.
1418*852Svi117747 	 * We allocate upfront so that the clustering module need to bother
1419*852Svi117747 	 * re-sizing the list.
1420*852Svi117747 	 */
1421*852Svi117747 	if (sctp->sctp_family == AF_INET)
1422*852Svi117747 		*size = sizeof (struct sockaddr_in) * sctp_g_ipifs_count;
1423*852Svi117747 	else
1424*852Svi117747 		*size = sizeof (struct sockaddr_in6) * sctp_g_ipifs_count;
1425*852Svi117747 
1426*852Svi117747 	*addrlist = kmem_zalloc(*size, KM_SLEEP);
1427*852Svi117747 	*addrcnt = 0;
1428*852Svi117747 	p = *addrlist;
1429*852Svi117747 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
1430*852Svi117747 
1431*852Svi117747 	/*
1432*852Svi117747 	 * Walk through the global interface list and add all addresses,
1433*852Svi117747 	 * except those that are hosted on loopback interfaces.
1434*852Svi117747 	 */
1435*852Svi117747 	for (cnt = 0; cnt <  SCTP_IPIF_HASH; cnt++) {
1436*852Svi117747 		if (sctp_g_ipifs[cnt].ipif_count == 0)
1437*852Svi117747 			continue;
1438*852Svi117747 		sctp_ipif = list_head(&sctp_g_ipifs[cnt].sctp_ipif_list);
1439*852Svi117747 		for (icnt = 0; icnt < sctp_g_ipifs[cnt].ipif_count; icnt++) {
1440*852Svi117747 			in6_addr_t	addr;
1441*852Svi117747 
1442*852Svi117747 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1443*852Svi117747 			addr = sctp_ipif->sctp_ipif_saddr;
1444*852Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
1445*852Svi117747 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
1446*852Svi117747 			    SCTP_IS_IPIF_LOOPBACK(sctp_ipif) ||
1447*852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(sctp_ipif) ||
1448*852Svi117747 			    sctp_ipif->sctp_ipif_zoneid != sctp->sctp_zoneid ||
1449*852Svi117747 			    (sctp->sctp_ipversion == IPV4_VERSION &&
1450*852Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
1451*852Svi117747 			    (sctp->sctp_connp->conn_ipv6_v6only &&
1452*852Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
1453*852Svi117747 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1454*852Svi117747 				sctp_ipif = list_next(
1455*852Svi117747 				    &sctp_g_ipifs[cnt].sctp_ipif_list,
1456*852Svi117747 				    sctp_ipif);
1457*852Svi117747 				continue;
1458*852Svi117747 			}
1459*852Svi117747 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1460*852Svi117747 			if (sctp->sctp_family == AF_INET) {
1461*852Svi117747 				s4 = (struct sockaddr_in *)p;
1462*852Svi117747 				IN6_V4MAPPED_TO_INADDR(&addr, &s4->sin_addr);
1463*852Svi117747 				s4->sin_family = AF_INET;
1464*852Svi117747 				p += sizeof (*s4);
1465*852Svi117747 			} else {
1466*852Svi117747 				s6 = (struct sockaddr_in6 *)p;
1467*852Svi117747 				s6->sin6_addr = addr;
1468*852Svi117747 				s6->sin6_family = AF_INET6;
1469*852Svi117747 				s6->sin6_scope_id =
1470*852Svi117747 				    sctp_ipif->sctp_ipif_ill->sctp_ill_index;
1471*852Svi117747 				p += sizeof (*s6);
1472*852Svi117747 			}
1473*852Svi117747 			(*addrcnt)++;
1474*852Svi117747 			sctp_ipif = list_next(&sctp_g_ipifs[cnt].sctp_ipif_list,
1475*852Svi117747 			    sctp_ipif);
1476*852Svi117747 		}
1477*852Svi117747 	}
1478*852Svi117747 	rw_exit(&sctp_g_ipifs_lock);
1479*852Svi117747 	return (err);
1480*852Svi117747 }
1481*852Svi117747 
1482*852Svi117747 /*
1483*852Svi117747  * Get a list of addresses from the source address list. The  caller is
1484*852Svi117747  * responsible for allocating sufficient buffer for this.
1485*852Svi117747  */
1486*852Svi117747 void
1487*852Svi117747 sctp_get_saddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1488*852Svi117747 {
1489*852Svi117747 	int			cnt;
1490*852Svi117747 	int			icnt;
1491*852Svi117747 	sctp_saddr_ipif_t	*obj;
1492*852Svi117747 	int			naddr;
1493*852Svi117747 	int			scanned = 0;
1494*852Svi117747 
1495*852Svi117747 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1496*852Svi117747 		if (sctp->sctp_saddrs[cnt].ipif_count == 0)
1497*852Svi117747 			continue;
1498*852Svi117747 		obj = list_head(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
1499*852Svi117747 		naddr = sctp->sctp_saddrs[cnt].ipif_count;
1500*852Svi117747 		for (icnt = 0; icnt < naddr; icnt++) {
1501*852Svi117747 			sctp_ipif_t	*ipif;
1502*852Svi117747 
1503*852Svi117747 			if (psize < sizeof (ipif->sctp_ipif_saddr))
1504*852Svi117747 				return;
1505*852Svi117747 
1506*852Svi117747 			scanned++;
1507*852Svi117747 			ipif = obj->saddr_ipifp;
1508*852Svi117747 			bcopy(&ipif->sctp_ipif_saddr, p,
1509*852Svi117747 			    sizeof (ipif->sctp_ipif_saddr));
1510*852Svi117747 			p += sizeof (ipif->sctp_ipif_saddr);
1511*852Svi117747 			psize -= sizeof (ipif->sctp_ipif_saddr);
1512*852Svi117747 			if (scanned >= sctp->sctp_nsaddrs)
1513*852Svi117747 				return;
1514*852Svi117747 			obj = list_next(&sctp->sctp_saddrs[icnt].sctp_ipif_list,
1515*852Svi117747 			    obj);
1516*852Svi117747 		}
1517*852Svi117747 	}
1518*852Svi117747 }
1519*852Svi117747 
1520*852Svi117747 /*
1521*852Svi117747  * Get a list of addresses from the remote address list. The  caller is
1522*852Svi117747  * responsible for allocating sufficient buffer for this.
1523*852Svi117747  */
1524*852Svi117747 void
1525*852Svi117747 sctp_get_faddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1526*852Svi117747 {
1527*852Svi117747 	sctp_faddr_t	*fp;
1528*852Svi117747 
1529*852Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1530*852Svi117747 		if (psize < sizeof (fp->faddr))
1531*852Svi117747 			return;
1532*852Svi117747 		bcopy(&fp->faddr, p, sizeof (fp->faddr));
1533*852Svi117747 		p += sizeof (fp->faddr);
1534*852Svi117747 		psize -= sizeof (fp->faddr);
1535*852Svi117747 	}
1536*852Svi117747 }
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
15390Sstevel@tonic-gate void
15400Sstevel@tonic-gate sctp_saddr_init()
15410Sstevel@tonic-gate {
15420Sstevel@tonic-gate 	int	i;
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	rw_init(&sctp_g_ills_lock, NULL, RW_DEFAULT, NULL);
15450Sstevel@tonic-gate 	rw_init(&sctp_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
15480Sstevel@tonic-gate 		sctp_g_ills[i].ill_count = 0;
15490Sstevel@tonic-gate 		list_create(&sctp_g_ills[i].sctp_ill_list, sizeof (sctp_ill_t),
15500Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
15530Sstevel@tonic-gate 		sctp_g_ipifs[i].ipif_count = 0;
15540Sstevel@tonic-gate 		list_create(&sctp_g_ipifs[i].sctp_ipif_list,
15550Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
15560Sstevel@tonic-gate 	}
15570Sstevel@tonic-gate }
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate void
15600Sstevel@tonic-gate sctp_saddr_fini()
15610Sstevel@tonic-gate {
15620Sstevel@tonic-gate 	int	i;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	rw_destroy(&sctp_g_ills_lock);
15650Sstevel@tonic-gate 	rw_destroy(&sctp_g_ipifs_lock);
15660Sstevel@tonic-gate 	ASSERT(sctp_ills_count == 0 && sctp_g_ipifs_count == 0);
15670Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
15680Sstevel@tonic-gate 		list_destroy(&sctp_g_ills[i].sctp_ill_list);
15690Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
15700Sstevel@tonic-gate 		list_destroy(&sctp_g_ipifs[i].sctp_ipif_list);
15710Sstevel@tonic-gate }
1572