xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_addr.c (revision 252:8fc692a5cbf7)
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 /*
23*252Svi117747  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/list.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <netinet/in.h>
400Sstevel@tonic-gate #include <netinet/ip6.h>
410Sstevel@tonic-gate #include <netinet/sctp.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <inet/common.h>
440Sstevel@tonic-gate #include <inet/ip.h>
450Sstevel@tonic-gate #include <inet/ip6.h>
460Sstevel@tonic-gate #include <inet/ip_if.h>
470Sstevel@tonic-gate #include <inet/ipclassifier.h>
480Sstevel@tonic-gate #include <inet/sctp_ip.h>
490Sstevel@tonic-gate #include "sctp_impl.h"
500Sstevel@tonic-gate #include "sctp_addr.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
530Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
540Sstevel@tonic-gate 			    zoneid_t zoneid);
550Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
560Sstevel@tonic-gate int			sctp_valid_addr_list(sctp_t *, const void *, uint32_t);
570Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_ipif_lookup(sctp_t *, uint_t);
580Sstevel@tonic-gate static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int);
590Sstevel@tonic-gate static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
600Sstevel@tonic-gate static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
610Sstevel@tonic-gate 			    sctp_ipif_hash_t *);
620Sstevel@tonic-gate int			sctp_compare_saddrs(sctp_t *, sctp_t *);
630Sstevel@tonic-gate static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
640Sstevel@tonic-gate int			sctp_dup_saddrs(sctp_t *, sctp_t *, int);
650Sstevel@tonic-gate void			sctp_free_saddrs(sctp_t *);
660Sstevel@tonic-gate void			sctp_update_ill(ill_t *, int);
670Sstevel@tonic-gate void			sctp_update_ipif(ipif_t *, int);
680Sstevel@tonic-gate void			sctp_move_ipif(ipif_t *, ill_t *, ill_t *);
690Sstevel@tonic-gate void			sctp_del_saddr(sctp_t *, sctp_saddr_ipif_t *);
700Sstevel@tonic-gate void			sctp_del_saddr_list(sctp_t *, const void *, int,
710Sstevel@tonic-gate 			    boolean_t);
720Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_saddr_lookup(sctp_t *, in6_addr_t *);
730Sstevel@tonic-gate in6_addr_t		sctp_get_valid_addr(sctp_t *, boolean_t);
740Sstevel@tonic-gate int			sctp_getmyaddrs(void *, void *, int *);
750Sstevel@tonic-gate void			sctp_saddr_init();
760Sstevel@tonic-gate void			sctp_saddr_fini();
770Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
780Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
790Sstevel@tonic-gate 	((sctp_ipif_state) ==  SCTP_IPIFS_DOWN))
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
820Sstevel@tonic-gate #define	SCTP_IPIF_HASH_FN(seqid)	((seqid) % SCTP_IPIF_HASH)
830Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /* Global list of SCTP ILLs */
860Sstevel@tonic-gate sctp_ill_hash_t	sctp_g_ills[SCTP_ILL_HASH];
870Sstevel@tonic-gate uint32_t	sctp_ills_count = 0;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* Global list of SCTP IPIFs */
900Sstevel@tonic-gate sctp_ipif_hash_t	sctp_g_ipifs[SCTP_IPIF_HASH];
910Sstevel@tonic-gate uint32_t		sctp_g_ipifs_count;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1030Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1040Sstevel@tonic-gate  * Called with no locks held.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate static void
1070Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
1100Sstevel@tonic-gate 	uint_t		ipif_index;
1110Sstevel@tonic-gate 	uint_t		ill_index;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
1140Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(sctp_ipif->sctp_ipif_id);
1170Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1180Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1190Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1200Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1210Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
1220Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
1230Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
1240Sstevel@tonic-gate 		return;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 	list_remove(&sctp_g_ipifs[ipif_index].sctp_ipif_list, sctp_ipif);
1270Sstevel@tonic-gate 	sctp_g_ipifs[ipif_index].ipif_count--;
1280Sstevel@tonic-gate 	sctp_g_ipifs_count--;
1290Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1300Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1330Sstevel@tonic-gate 	if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
1340Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
1350Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1360Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1370Sstevel@tonic-gate 			list_remove(&sctp_g_ills[ill_index].sctp_ill_list,
1380Sstevel@tonic-gate 			    (void *)sctp_ill);
1390Sstevel@tonic-gate 			sctp_g_ills[ill_index].ill_count--;
1400Sstevel@tonic-gate 			sctp_ills_count--;
1410Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1420Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1430Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
1470Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
1520Sstevel@tonic-gate  * Called with no locks held.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate static sctp_ipif_t *
1550Sstevel@tonic-gate sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	int		i;
1580Sstevel@tonic-gate 	int		j;
1590Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
1620Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1630Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
1640Sstevel@tonic-gate 			continue;
1650Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
1660Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
1670Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1680Sstevel@tonic-gate 			if (zoneid == sctp_ipif->sctp_ipif_zoneid &&
1690Sstevel@tonic-gate 			    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) &&
1700Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1710Sstevel@tonic-gate 			    addr)) {
1720Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1730Sstevel@tonic-gate 				if (refhold)
1740Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
1750Sstevel@tonic-gate 				rw_exit(&sctp_g_ipifs_lock);
1760Sstevel@tonic-gate 				return (sctp_ipif);
1770Sstevel@tonic-gate 			}
1780Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1790Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
1800Sstevel@tonic-gate 			    sctp_ipif);
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
1840Sstevel@tonic-gate 	return (NULL);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
1890Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
1900Sstevel@tonic-gate  * Called with no locks held.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate static int
1930Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
1960Sstevel@tonic-gate 	int			i;
1970Sstevel@tonic-gate 	int			j;
1980Sstevel@tonic-gate 	int			error = 0;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
2010Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
2020Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
2030Sstevel@tonic-gate 			continue;
2040Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
2050Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
2060Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
2070Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_zoneid != sctp->sctp_zoneid ||
2080Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
2090Sstevel@tonic-gate 			    (sctp->sctp_ipversion == IPV4_VERSION &&
2100Sstevel@tonic-gate 			    !IN6_IS_ADDR_V4MAPPED(
2110Sstevel@tonic-gate 			    &sctp_ipif->sctp_ipif_saddr)) ||
2120Sstevel@tonic-gate 			    (sctp->sctp_connp->conn_ipv6_v6only &&
2130Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(
2140Sstevel@tonic-gate 			    &sctp_ipif->sctp_ipif_saddr))) {
2150Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2160Sstevel@tonic-gate 				sctp_ipif = list_next(
2170Sstevel@tonic-gate 				    &sctp_g_ipifs[i].sctp_ipif_list, sctp_ipif);
2180Sstevel@tonic-gate 				continue;
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2210Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
2220Sstevel@tonic-gate 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep);
2230Sstevel@tonic-gate 			if (error != 0)
2240Sstevel@tonic-gate 				goto free_stuff;
2250Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
2260Sstevel@tonic-gate 			    sctp_ipif);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2300Sstevel@tonic-gate 	return (0);
2310Sstevel@tonic-gate free_stuff:
2320Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
2330Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2340Sstevel@tonic-gate 	return (ENOMEM);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2390Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
2400Sstevel@tonic-gate  * or error.
2410Sstevel@tonic-gate  * Called with no locks held.
2420Sstevel@tonic-gate  */
2430Sstevel@tonic-gate int
2440Sstevel@tonic-gate sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2470Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2480Sstevel@tonic-gate 	struct in_addr		*addr4;
2490Sstevel@tonic-gate 	in6_addr_t		addr;
2500Sstevel@tonic-gate 	int			cnt;
2510Sstevel@tonic-gate 	int			err = 0;
2520Sstevel@tonic-gate 	int			saddr_cnt = 0;
2530Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
2540Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
2550Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
2560Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
2600Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
2610Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
2620Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
2630Sstevel@tonic-gate 	 * addresses.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
2660Sstevel@tonic-gate 		check_lport = B_TRUE;
2670Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
2680Sstevel@tonic-gate 			check_addrs = B_TRUE;
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
2710Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
2720Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
2730Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
2740Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
2750Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		switch (sctp->sctp_family) {
2780Sstevel@tonic-gate 		case AF_INET:
2790Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
2800Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
2810Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
2820Sstevel@tonic-gate 				err = EINVAL;
2830Sstevel@tonic-gate 				goto free_ret;
2840Sstevel@tonic-gate 			}
2850Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
2860Sstevel@tonic-gate 			if (check_addrs &&
2870Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
2880Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
2890Sstevel@tonic-gate 			    IN_MULTICAST(addr4->s_addr))) {
2900Sstevel@tonic-gate 				err = EINVAL;
2910Sstevel@tonic-gate 				goto free_ret;
2920Sstevel@tonic-gate 			}
2930Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
2940Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
2950Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
2960Sstevel@tonic-gate 				bind_to_all = B_TRUE;
2970Sstevel@tonic-gate 			}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 			break;
3000Sstevel@tonic-gate 		case AF_INET6:
3010Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3020Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
3030Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
3040Sstevel@tonic-gate 				err = EINVAL;
3050Sstevel@tonic-gate 				goto free_ret;
3060Sstevel@tonic-gate 			}
3070Sstevel@tonic-gate 			addr = sin6->sin6_addr;
3080Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
3090Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3100Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3110Sstevel@tonic-gate 				goto free_ret;
3120Sstevel@tonic-gate 			}
3130Sstevel@tonic-gate 			if (check_addrs &&
3140Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3150Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3160Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3170Sstevel@tonic-gate 				err = EINVAL;
3180Sstevel@tonic-gate 				goto free_ret;
3190Sstevel@tonic-gate 			}
3200Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3210Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3220Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3230Sstevel@tonic-gate 			}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 			break;
3260Sstevel@tonic-gate 		default:
3270Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3280Sstevel@tonic-gate 			goto free_ret;
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 		if (lookup_saddr) {
3310Sstevel@tonic-gate 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
3320Sstevel@tonic-gate 			    sctp->sctp_zoneid);
3330Sstevel@tonic-gate 			if (ipif == NULL) {
3340Sstevel@tonic-gate 				/* Address not in the list */
3350Sstevel@tonic-gate 				err = EINVAL;
3360Sstevel@tonic-gate 				goto free_ret;
3370Sstevel@tonic-gate 			} else if (check_addrs &&
3380Sstevel@tonic-gate 			    (ipif->sctp_ipif_ill->sctp_ill_flags &
3390Sstevel@tonic-gate 			    PHYI_LOOPBACK)) {
3400Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3410Sstevel@tonic-gate 				err = EINVAL;
3420Sstevel@tonic-gate 				goto free_ret;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 		if (!bind_to_all) {
3460Sstevel@tonic-gate 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP);
3470Sstevel@tonic-gate 			if (err != 0) {
3480Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3490Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
3500Sstevel@tonic-gate 					err = EADDRINUSE;
3510Sstevel@tonic-gate 				goto free_ret;
3520Sstevel@tonic-gate 			}
3530Sstevel@tonic-gate 			saddr_cnt++;
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 	if (bind_to_all) {
3570Sstevel@tonic-gate 		/*
3580Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
3590Sstevel@tonic-gate 		 * inaddr_any.
3600Sstevel@tonic-gate 		 */
3610Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
3620Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
3630Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
3660Sstevel@tonic-gate 		if (err != 0)
3670Sstevel@tonic-gate 			return (err);
3680Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3710Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
3720Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3730Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
3740Sstevel@tonic-gate 	return (0);
3750Sstevel@tonic-gate free_ret:
3760Sstevel@tonic-gate 	if (saddr_cnt != 0)
3770Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
3780Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3790Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
3800Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3810Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
3820Sstevel@tonic-gate 	return (err);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate sctp_saddr_ipif_t *
3860Sstevel@tonic-gate sctp_ipif_lookup(sctp_t *sctp, uint_t ipif_index)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	int			cnt;
3890Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif_index);
3900Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (sctp->sctp_saddrs[seqid].ipif_count == 0)
3930Sstevel@tonic-gate 		return (NULL);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
3960Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
3970Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif_index)
3980Sstevel@tonic-gate 			return (ipif_obj);
3990Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4000Sstevel@tonic-gate 		    ipif_obj);
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 	return (NULL);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate static int
4060Sstevel@tonic-gate sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	int			cnt;
4090Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4100Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4130Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4140Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id)
4150Sstevel@tonic-gate 			return (EALREADY);
4160Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4170Sstevel@tonic-gate 		    ipif_obj);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4200Sstevel@tonic-gate 	if (ipif_obj == NULL) {
4210Sstevel@tonic-gate 		/* Need to do something */
4220Sstevel@tonic-gate 		return (ENOMEM);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
425*252Svi117747 	/*
426*252Svi117747 	 * If an address is added after association setup, we need to wait
427*252Svi117747 	 * for the peer to send us an ASCONF ACK before we can start using it.
428*252Svi117747 	 * saddr_ipif_dontsrc will be reset (to 0) when we get the ASCONF
429*252Svi117747 	 * ACK for this address.
430*252Svi117747 	 */
431*252Svi117747 	if (sctp->sctp_state > SCTP_LISTEN)
432*252Svi117747 		ipif_obj->saddr_ipif_dontsrc = 1;
4330Sstevel@tonic-gate 	list_insert_tail(&sctp->sctp_saddrs[seqid].sctp_ipif_list, ipif_obj);
4340Sstevel@tonic-gate 	sctp->sctp_saddrs[seqid].ipif_count++;
4350Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
4360Sstevel@tonic-gate 	return (0);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate static void
4400Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate 	int			cnt;
4430Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4440Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4470Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4480Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) {
4490Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4500Sstevel@tonic-gate 			    ipif_obj);
4510Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
4520Sstevel@tonic-gate 			sctp->sctp_saddrs[seqid].ipif_count--;
4530Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
4540Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
4550Sstevel@tonic-gate 			break;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4580Sstevel@tonic-gate 		    ipif_obj);
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate static int
4630Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	int			i;
4660Sstevel@tonic-gate 	int			j;
4670Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
4680Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
4690Sstevel@tonic-gate 	int			overlap = 0;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
4720Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
4730Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
4740Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
4750Sstevel@tonic-gate 			if (obj1->saddr_ipifp->sctp_ipif_id ==
4760Sstevel@tonic-gate 			    obj2->saddr_ipifp->sctp_ipif_id) {
4770Sstevel@tonic-gate 				overlap++;
4780Sstevel@tonic-gate 				break;
4790Sstevel@tonic-gate 			}
4800Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
4810Sstevel@tonic-gate 			    obj2);
4820Sstevel@tonic-gate 		}
4830Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 	return (overlap);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate int
4890Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	int		i;
4920Sstevel@tonic-gate 	int		overlap = 0;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
4950Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
4960Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5000Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5010Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
5050Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (overlap > 0)
5080Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate static int
5140Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	int			i;
5170Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5180Sstevel@tonic-gate 	int			error = 0;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
5210Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5220Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
5230Sstevel@tonic-gate 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep);
5240Sstevel@tonic-gate 		if (error != 0)
5250Sstevel@tonic-gate 			return (error);
5260Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	return (error);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate int
5320Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	int	error = 0;
5350Sstevel@tonic-gate 	int	i;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if (sctp1 == NULL || sctp1->sctp_bound_to_all)
5380Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5410Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
5420Sstevel@tonic-gate 			continue;
5430Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
5440Sstevel@tonic-gate 		if (error != 0) {
5450Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
5460Sstevel@tonic-gate 			return (error);
5470Sstevel@tonic-gate 		}
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 	return (0);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate void
5530Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	int			i;
5560Sstevel@tonic-gate 	int			l;
5570Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
5600Sstevel@tonic-gate 		return;
5610Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5620Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
5630Sstevel@tonic-gate 			continue;
5640Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
5650Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
5660Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
5670Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
5680Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
5690Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
5700Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
5790Sstevel@tonic-gate  * held.
5800Sstevel@tonic-gate  */
5810Sstevel@tonic-gate void
5820Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	int		i;
5850Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
5860Sstevel@tonic-gate 	uint_t		index;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	ip2dbg(("sctp_update_ill: %s\n", ill->ill_name));
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_WRITER);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
5930Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
5940Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
5950Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
5960Sstevel@tonic-gate 			break;
5970Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
5980Sstevel@tonic-gate 		    sctp_ill);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	switch (op) {
6020Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
6030Sstevel@tonic-gate 		if (sctp_ill != NULL) {
6040Sstevel@tonic-gate 			/* Unmark it if it is condemned */
6050Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
6060Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
6070Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6080Sstevel@tonic-gate 			return;
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
6110Sstevel@tonic-gate 		/* Need to re-try? */
6120Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6130Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6140Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6150Sstevel@tonic-gate 			return;
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 		sctp_ill->sctp_ill_name =
6180Sstevel@tonic-gate 		    kmem_zalloc(ill->ill_name_length, KM_NOSLEEP);
6190Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
6200Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6210Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6220Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6230Sstevel@tonic-gate 			return;
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
6260Sstevel@tonic-gate 		    ill->ill_name_length);
6270Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
6280Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
6290Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
6300Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ills[index].sctp_ill_list,
6310Sstevel@tonic-gate 		    (void *)sctp_ill);
6320Sstevel@tonic-gate 		sctp_g_ills[index].ill_count++;
6330Sstevel@tonic-gate 		sctp_ills_count++;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		break;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6400Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
6410Sstevel@tonic-gate 			return;
6420Sstevel@tonic-gate 		}
6430Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
6440Sstevel@tonic-gate 			list_remove(&sctp_g_ills[index].sctp_ill_list,
6450Sstevel@tonic-gate 			    (void *)sctp_ill);
6460Sstevel@tonic-gate 			sctp_g_ills[index].ill_count--;
6470Sstevel@tonic-gate 			sctp_ills_count--;
6480Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
6490Sstevel@tonic-gate 			    ill->ill_name_length);
6500Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6510Sstevel@tonic-gate 		} else {
6520Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		break;
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
6610Sstevel@tonic-gate void
6620Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
6650Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
6660Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
6670Sstevel@tonic-gate 	uint_t		index;
6680Sstevel@tonic-gate 	int		i;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
6710Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
6740Sstevel@tonic-gate 	fsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
6750Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
6760Sstevel@tonic-gate 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill))
6770Sstevel@tonic-gate 			break;
6780Sstevel@tonic-gate 		fsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
6790Sstevel@tonic-gate 		    fsctp_ill);
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
6830Sstevel@tonic-gate 	tsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
6840Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
6850Sstevel@tonic-gate 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill))
6860Sstevel@tonic-gate 			break;
6870Sstevel@tonic-gate 		tsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
6880Sstevel@tonic-gate 		    tsctp_ill);
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
6920Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[index].sctp_ipif_list);
6930Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[index].ipif_count; i++) {
6940Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
6950Sstevel@tonic-gate 			break;
6960Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[index].sctp_ipif_list,
6970Sstevel@tonic-gate 		    sctp_ipif);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	/* Should be an ASSERT? */
7000Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
7010Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
7020Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
7030Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7040Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7050Sstevel@tonic-gate 		return;
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
7080Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
7090Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
7100Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
7110Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
7120Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
7130Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
7140Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
7180Sstevel@tonic-gate void
7190Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
7220Sstevel@tonic-gate 	int		i;
7230Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
7240Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7250Sstevel@tonic-gate 	uint_t		ill_index;
7260Sstevel@tonic-gate 	uint_t		ipif_index;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
7310Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
7340Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[ill_index].sctp_ill_list);
7350Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[ill_index].ill_count; i++) {
7360Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
7370Sstevel@tonic-gate 			break;
7380Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[ill_index].sctp_ill_list,
7390Sstevel@tonic-gate 		    sctp_ill);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 	if (sctp_ill == NULL) {
7420Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7430Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7440Sstevel@tonic-gate 		return;
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
7480Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[ipif_index].sctp_ipif_list);
7490Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[ipif_index].ipif_count; i++) {
7500Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7510Sstevel@tonic-gate 			break;
7520Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
7530Sstevel@tonic-gate 		    sctp_ipif);
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 	if (op != SCTP_IPIF_INSERT && sctp_ipif == NULL) {
7560Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
7570Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
7580Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
7590Sstevel@tonic-gate 		return;
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate #ifdef	DEBUG
7620Sstevel@tonic-gate 	if (sctp_ipif != NULL)
7630Sstevel@tonic-gate 		ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
7640Sstevel@tonic-gate #endif
7650Sstevel@tonic-gate 	switch (op) {
7660Sstevel@tonic-gate 	case SCTP_IPIF_INSERT:
7670Sstevel@tonic-gate 		if (sctp_ipif != NULL) {
7680Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED)
769*252Svi117747 				sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
7700Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
7710Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
7720Sstevel@tonic-gate 			return;
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 		sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
7750Sstevel@tonic-gate 		/* Try again? */
7760Sstevel@tonic-gate 		if (sctp_ipif == NULL) {
7770Sstevel@tonic-gate 			ip1dbg(("sctp_ipif_insert: mem failure..\n"));
7780Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
7790Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
7800Sstevel@tonic-gate 			return;
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
7830Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_ill = sctp_ill;
784*252Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
7850Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
7860Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
7870Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
7880Sstevel@tonic-gate 		rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
7890Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
7900Sstevel@tonic-gate 		    (void *)sctp_ipif);
7910Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count++;
7920Sstevel@tonic-gate 		sctp_g_ipifs_count++;
7930Sstevel@tonic-gate 		atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 		break;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
7980Sstevel@tonic-gate 	{
7990Sstevel@tonic-gate 		list_t		*ipif_list;
8000Sstevel@tonic-gate 		list_t		*ill_list;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		ill_list = &sctp_g_ills[ill_index].sctp_ill_list;
8030Sstevel@tonic-gate 		ipif_list = &sctp_g_ipifs[ipif_index].sctp_ipif_list;
8040Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
8050Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
8060Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
8070Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
8080Sstevel@tonic-gate 			return;
8090Sstevel@tonic-gate 		}
8100Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
8110Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count--;
8120Sstevel@tonic-gate 		sctp_g_ipifs_count--;
8130Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
8140Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
8150Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
8160Sstevel@tonic-gate 		if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
8170Sstevel@tonic-gate 			rw_downgrade(&sctp_g_ipifs_lock);
8180Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
8190Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
8200Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
8210Sstevel@tonic-gate 				sctp_ills_count--;
8220Sstevel@tonic-gate 				sctp_g_ills[ill_index].ill_count--;
8230Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
8240Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
8250Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
8260Sstevel@tonic-gate 			}
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 		break;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	case SCTP_IPIF_UP:
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8340Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8350Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
8360Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
8370Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		break;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8440Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8450Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8460Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
8470Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
8480Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 		break;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
8550Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8560Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
8570Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		break;
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
8620Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate  *
8670Sstevel@tonic-gate  *
8680Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
8690Sstevel@tonic-gate  * sctp locking by the caller.
8700Sstevel@tonic-gate  *
8710Sstevel@tonic-gate  *
8720Sstevel@tonic-gate  */
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate /* Remove a specific saddr from the list */
8750Sstevel@tonic-gate void
8760Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
8770Sstevel@tonic-gate {
8780Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
8790Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
8820Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if (sctp->sctp_bound_to_all)
8870Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
8900Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
8930Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate /*
8970Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
8980Sstevel@tonic-gate  * Called with no locks held.
8990Sstevel@tonic-gate  */
9000Sstevel@tonic-gate void
9010Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
9020Sstevel@tonic-gate     boolean_t fanout_locked)
9030Sstevel@tonic-gate {
9040Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
9050Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
9060Sstevel@tonic-gate 	int			cnt;
9070Sstevel@tonic-gate 	in6_addr_t		addr;
9080Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > addcnt);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if (!fanout_locked) {
9130Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9140Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9150Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9160Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
9200Sstevel@tonic-gate 		switch (sctp->sctp_family) {
9210Sstevel@tonic-gate 		case AF_INET:
9220Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
9230Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
9240Sstevel@tonic-gate 			break;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		case AF_INET6:
9270Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
9280Sstevel@tonic-gate 			addr = sin6->sin6_addr;
9290Sstevel@tonic-gate 			break;
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
9320Sstevel@tonic-gate 		    sctp->sctp_zoneid);
9330Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
9340Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 	if (sctp->sctp_bound_to_all)
9370Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	if (!fanout_locked) {
9400Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9410Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9420Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9430Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate /*
9480Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
9490Sstevel@tonic-gate  * Called with no locks held.
9500Sstevel@tonic-gate  */
9510Sstevel@tonic-gate sctp_saddr_ipif_t *
9520Sstevel@tonic-gate sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr)
9530Sstevel@tonic-gate {
9540Sstevel@tonic-gate 	sctp_saddr_ipif_t	*saddr_ipifs;
9550Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_FALSE, sctp->sctp_zoneid);
9580Sstevel@tonic-gate 	if (sctp_ipif == NULL)
9590Sstevel@tonic-gate 		return (NULL);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	saddr_ipifs = sctp_ipif_lookup(sctp, sctp_ipif->sctp_ipif_id);
9620Sstevel@tonic-gate 	return (saddr_ipifs);
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
9660Sstevel@tonic-gate in6_addr_t
9670Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6)
9680Sstevel@tonic-gate {
9690Sstevel@tonic-gate 	int			i;
9700Sstevel@tonic-gate 	int			l;
9710Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
9720Sstevel@tonic-gate 	int			scanned = 0;
9730Sstevel@tonic-gate 	in6_addr_t		addr;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
9760Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
9770Sstevel@tonic-gate 			continue;
9780Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
9790Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
9800Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
9810Sstevel@tonic-gate 			sctp_ill_t	*ill;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
9840Sstevel@tonic-gate 			ill = ipif->sctp_ipif_ill;
9850Sstevel@tonic-gate 			if (!obj->saddr_ipif_dontsrc &&
9860Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
9870Sstevel@tonic-gate 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP &&
988*252Svi117747 			    !(ill->sctp_ill_flags & PHYI_LOOPBACK) &&
989*252Svi117747 			    (!ipif->sctp_ipif_isv6 ||
990*252Svi117747 			    !IN6_IS_ADDR_LINKLOCAL(&ipif->sctp_ipif_saddr))) {
9910Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
9920Sstevel@tonic-gate 			}
9930Sstevel@tonic-gate 			scanned++;
9940Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
9950Sstevel@tonic-gate 				goto got_none;
9960Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
9970Sstevel@tonic-gate 			    obj);
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate got_none:
10010Sstevel@tonic-gate 	/* Need to double check this */
10020Sstevel@tonic-gate 	if (isv6 == B_TRUE)
10030Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
10040Sstevel@tonic-gate 	else
10050Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	return (addr);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate /*
10110Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
10120Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
10130Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
10140Sstevel@tonic-gate  */
10150Sstevel@tonic-gate int
10160Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
10170Sstevel@tonic-gate {
10180Sstevel@tonic-gate 	int			i;
10190Sstevel@tonic-gate 	int			l;
10200Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
10210Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
10220Sstevel@tonic-gate 	int			family = sctp->sctp_family;
10230Sstevel@tonic-gate 	int			max = *addrcnt;
10240Sstevel@tonic-gate 	size_t			added = 0;
10250Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
10260Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
10270Sstevel@tonic-gate 	int			scanned = 0;
10280Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
10310Sstevel@tonic-gate 		return (EINVAL);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	/* Skip loopback addresses for non-loopback assoc. */
10340Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback)
10350Sstevel@tonic-gate 		skip_lback = B_TRUE;
10360Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
10370Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
10380Sstevel@tonic-gate 			continue;
10390Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
10400Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
10410Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
10420Sstevel@tonic-gate 			sctp_ill_t	*ill = ipif->sctp_ipif_ill;
10430Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 			scanned++;
10460Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
10470Sstevel@tonic-gate 			    obj->saddr_ipif_dontsrc ||
10480Sstevel@tonic-gate 			    ((ill->sctp_ill_flags & PHYI_LOOPBACK) &&
10490Sstevel@tonic-gate 			    skip_lback)) {
10500Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
10510Sstevel@tonic-gate 					goto done;
10520Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
10530Sstevel@tonic-gate 				    sctp_ipif_list, obj);
10540Sstevel@tonic-gate 				continue;
10550Sstevel@tonic-gate 			}
10560Sstevel@tonic-gate 			switch (family) {
10570Sstevel@tonic-gate 			case AF_INET:
10580Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
10590Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
10600Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
10610Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
10620Sstevel@tonic-gate 				break;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 			case AF_INET6:
10650Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
10660Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
10670Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
10680Sstevel@tonic-gate 				sin6->sin6_addr = addr;
10690Sstevel@tonic-gate 				break;
10700Sstevel@tonic-gate 			}
10710Sstevel@tonic-gate 			added++;
10720Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
10730Sstevel@tonic-gate 				goto done;
10740Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
10750Sstevel@tonic-gate 			    obj);
10760Sstevel@tonic-gate 		}
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate done:
10790Sstevel@tonic-gate 	*addrcnt = added;
10800Sstevel@tonic-gate 	return (0);
10810Sstevel@tonic-gate }
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate /*
1084*252Svi117747  * Given the supported address family, walk through the source address list
1085*252Svi117747  * and return the total length of the available addresses. If 'p' is not
1086*252Svi117747  * null, construct the parameter list for the addresses in 'p'.
10870Sstevel@tonic-gate  */
10880Sstevel@tonic-gate size_t
1089*252Svi117747 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p)
10900Sstevel@tonic-gate {
10910Sstevel@tonic-gate 	int			i;
10920Sstevel@tonic-gate 	int			l;
10930Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1094*252Svi117747 	size_t			paramlen = 0;
10950Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
10960Sstevel@tonic-gate 	int			scanned = 0;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
10990Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11000Sstevel@tonic-gate 			continue;
11010Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11020Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11030Sstevel@tonic-gate 			in6_addr_t	addr;
11040Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
11050Sstevel@tonic-gate 			sctp_ill_t	*ill;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
11080Sstevel@tonic-gate 			ill = ipif->sctp_ipif_ill;
11090Sstevel@tonic-gate 			scanned++;
1110*252Svi117747 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state) ||
11110Sstevel@tonic-gate 			    (ill->sctp_ill_flags & PHYI_LOOPBACK)) {
1112*252Svi117747 				goto next_addr;
11130Sstevel@tonic-gate 			}
1114*252Svi117747 			if (p != NULL)
1115*252Svi117747 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
11160Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
11170Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&addr)) {
11180Sstevel@tonic-gate 				struct in_addr	*v4;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 				/* The other side does not support v4 */
11210Sstevel@tonic-gate 				if (!(supp_af & PARM_SUPP_V4))
1122*252Svi117747 					goto next_addr;
11230Sstevel@tonic-gate 
1124*252Svi117747 				if (p != NULL) {
1125*252Svi117747 					hdr->sph_type = htons(PARM_ADDR4);
1126*252Svi117747 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1127*252Svi117747 					v4 = (struct in_addr *)(hdr + 1);
1128*252Svi117747 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1129*252Svi117747 				}
1130*252Svi117747 				paramlen += PARM_ADDR4_LEN;
11310Sstevel@tonic-gate 			} else if (!IN6_IS_ADDR_LINKLOCAL(&addr) &&
11320Sstevel@tonic-gate 			    (supp_af & PARM_SUPP_V6)) {
1133*252Svi117747 				if (p != NULL) {
1134*252Svi117747 					hdr->sph_type = htons(PARM_ADDR6);
1135*252Svi117747 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1136*252Svi117747 					bcopy(&addr, hdr + 1, sizeof (addr));
1137*252Svi117747 				}
1138*252Svi117747 				paramlen += PARM_ADDR6_LEN;
11390Sstevel@tonic-gate 			}
1140*252Svi117747 next_addr:
11410Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
1142*252Svi117747 				return (paramlen);
11430Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
11440Sstevel@tonic-gate 			    obj);
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 	}
1147*252Svi117747 	return (paramlen);
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
11520Sstevel@tonic-gate void
11530Sstevel@tonic-gate sctp_saddr_init()
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 	int	i;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	rw_init(&sctp_g_ills_lock, NULL, RW_DEFAULT, NULL);
11580Sstevel@tonic-gate 	rw_init(&sctp_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
11610Sstevel@tonic-gate 		sctp_g_ills[i].ill_count = 0;
11620Sstevel@tonic-gate 		list_create(&sctp_g_ills[i].sctp_ill_list, sizeof (sctp_ill_t),
11630Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11660Sstevel@tonic-gate 		sctp_g_ipifs[i].ipif_count = 0;
11670Sstevel@tonic-gate 		list_create(&sctp_g_ipifs[i].sctp_ipif_list,
11680Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate void
11730Sstevel@tonic-gate sctp_saddr_fini()
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate 	int	i;
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	rw_destroy(&sctp_g_ills_lock);
11780Sstevel@tonic-gate 	rw_destroy(&sctp_g_ipifs_lock);
11790Sstevel@tonic-gate 	ASSERT(sctp_ills_count == 0 && sctp_g_ipifs_count == 0);
11800Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
11810Sstevel@tonic-gate 		list_destroy(&sctp_g_ills[i].sctp_ill_list);
11820Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
11830Sstevel@tonic-gate 		list_destroy(&sctp_g_ipifs[i].sctp_ipif_list);
11840Sstevel@tonic-gate }
1185