10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228485SPeter.Memishian@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/systm.h>
280Sstevel@tonic-gate #include <sys/stream.h>
294311Svi117747 #include <sys/cmn_err.h>
300Sstevel@tonic-gate #include <sys/ddi.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/socket.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/list.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <netinet/in.h>
380Sstevel@tonic-gate #include <netinet/ip6.h>
390Sstevel@tonic-gate #include <netinet/sctp.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <inet/common.h>
420Sstevel@tonic-gate #include <inet/ip.h>
430Sstevel@tonic-gate #include <inet/ip6.h>
440Sstevel@tonic-gate #include <inet/ip_if.h>
450Sstevel@tonic-gate #include <inet/ipclassifier.h>
460Sstevel@tonic-gate #include <inet/sctp_ip.h>
470Sstevel@tonic-gate #include "sctp_impl.h"
480Sstevel@tonic-gate #include "sctp_addr.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
510Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
523510Svi117747 			    zoneid_t, boolean_t, uint_t, uint_t, boolean_t,
533510Svi117747 			    sctp_stack_t *);
540Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
55432Svi117747 static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
563510Svi117747 			    boolean_t, boolean_t);
570Sstevel@tonic-gate static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
584818Skcpoon static void		sctp_fix_saddr(sctp_t *, in6_addr_t *);
590Sstevel@tonic-gate static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
600Sstevel@tonic-gate 			    sctp_ipif_hash_t *);
610Sstevel@tonic-gate static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
62432Svi117747 
633510Svi117747 #define	SCTP_ADDR4_HASH(addr)	\
643510Svi117747 	(((addr) ^ ((addr) >> 8) ^ ((addr) >> 16) ^ ((addr) >> 24)) &	\
653510Svi117747 	(SCTP_IPIF_HASH - 1))
663510Svi117747 
673510Svi117747 #define	SCTP_ADDR6_HASH(addr)	\
683510Svi117747 	(((addr).s6_addr32[3] ^						\
693510Svi117747 	(((addr).s6_addr32[3] ^ (addr).s6_addr32[2]) >> 12)) &		\
703510Svi117747 	(SCTP_IPIF_HASH - 1))
713510Svi117747 
723510Svi117747 #define	SCTP_IPIF_ADDR_HASH(addr, isv6)					\
733510Svi117747 	((isv6) ? SCTP_ADDR6_HASH((addr)) : 				\
743510Svi117747 	SCTP_ADDR4_HASH((addr)._S6_un._S6_u32[3]))
753510Svi117747 
760Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
770Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
78432Svi117747 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
79432Svi117747 
80432Svi117747 #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
81432Svi117747 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
82432Svi117747 
83852Svi117747 #define	SCTP_IS_IPIF_LOOPBACK(ipif)		\
84852Svi117747 	((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK)
85852Svi117747 
86852Svi117747 #define	SCTP_IS_IPIF_LINKLOCAL(ipif)		\
87852Svi117747 	((ipif)->sctp_ipif_isv6 && 		\
88852Svi117747 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr))
89432Svi117747 
90432Svi117747 #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
91432Svi117747 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
92432Svi117747 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
930Sstevel@tonic-gate 
942263Ssommerfe #define	SCTP_IPIF_ZONE_MATCH(sctp, ipif) 				\
952263Ssommerfe 	IPCL_ZONE_MATCH((sctp)->sctp_connp, (ipif)->sctp_ipif_zoneid)
962263Ssommerfe 
970Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
980Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1060Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate static void
1090Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
1123510Svi117747 	uint_t		hindex;
1130Sstevel@tonic-gate 	uint_t		ill_index;
1143448Sdh155122 	sctp_stack_t	*sctps = sctp_ipif->sctp_ipif_ill->
1153448Sdh155122 	    sctp_ill_netstack->netstack_sctp;
1160Sstevel@tonic-gate 
1173448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
1183448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
1190Sstevel@tonic-gate 
1203510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(sctp_ipif->sctp_ipif_saddr,
1213510Svi117747 	    sctp_ipif->sctp_ipif_isv6);
1223510Svi117747 
1230Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1240Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1250Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1260Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1270Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
1283448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
1293448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
1300Sstevel@tonic-gate 		return;
1310Sstevel@tonic-gate 	}
1323510Svi117747 	list_remove(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
1333448Sdh155122 	    sctp_ipif);
1343510Svi117747 	sctps->sctps_g_ipifs[hindex].ipif_count--;
1353448Sdh155122 	sctps->sctps_g_ipifs_count--;
1360Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1370Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1403448Sdh155122 	if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
1413448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1420Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1430Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1443448Sdh155122 			list_remove(&sctps->sctps_g_ills[ill_index].
1453448Sdh155122 			    sctp_ill_list, (void *)sctp_ill);
1463448Sdh155122 			sctps->sctps_g_ills[ill_index].ill_count--;
1473448Sdh155122 			sctps->sctps_ills_count--;
1480Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1490Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1500Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1510Sstevel@tonic-gate 		}
1520Sstevel@tonic-gate 	}
1533448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
1543448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
1593510Svi117747  * We are either looking for a IPIF with the given address before
1603510Svi117747  * inserting it into the global list or looking for an IPIF for an
1613510Svi117747  * address given an SCTP. In the former case we always check the zoneid,
1623510Svi117747  * but for the latter case, check_zid could be B_FALSE if the connp
1633510Svi117747  * for the sctp has conn_all_zones set. When looking for an address we
1643510Svi117747  * give preference to one that is up, so even though we may find one that
1653510Svi117747  * is not up we keep looking if there is one up, we hold the down addr
1663510Svi117747  * in backup_ipif in case we don't find one that is up - i.e. we return
1673510Svi117747  * the backup_ipif in that case. Note that if we are looking for. If we
1683510Svi117747  * are specifically looking for an up address, then usable will be set
1693510Svi117747  * to true.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate static sctp_ipif_t *
1723510Svi117747 sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid,
1733510Svi117747     boolean_t check_zid, uint_t ifindex, uint_t seqid, boolean_t usable,
1743510Svi117747     sctp_stack_t *sctps)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	int		j;
1770Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
1783510Svi117747 	sctp_ipif_t	*backup_ipif = NULL;
1793510Svi117747 	int		hindex;
1800Sstevel@tonic-gate 
1813510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
1823510Svi117747 
1833448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
1843510Svi117747 	if (sctps->sctps_g_ipifs[hindex].ipif_count == 0) {
1853510Svi117747 		rw_exit(&sctps->sctps_g_ipifs_lock);
1863510Svi117747 		return (NULL);
1873510Svi117747 	}
1883510Svi117747 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
1893510Svi117747 	for (j = 0; j < sctps->sctps_g_ipifs[hindex].ipif_count; j++) {
1903510Svi117747 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1913510Svi117747 		if ((!check_zid ||
1923510Svi117747 		    (sctp_ipif->sctp_ipif_zoneid == ALL_ZONES ||
1933510Svi117747 		    zoneid == sctp_ipif->sctp_ipif_zoneid)) &&
1943510Svi117747 		    (ifindex == 0 || ifindex ==
1953510Svi117747 		    sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
1963510Svi117747 		    ((seqid != 0 && seqid == sctp_ipif->sctp_ipif_id) ||
1973510Svi117747 		    (IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1983510Svi117747 		    addr)))) {
1993510Svi117747 			if (!usable || sctp_ipif->sctp_ipif_state ==
2003510Svi117747 			    SCTP_IPIFS_UP) {
2010Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2020Sstevel@tonic-gate 				if (refhold)
2030Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
2043448Sdh155122 				rw_exit(&sctps->sctps_g_ipifs_lock);
2050Sstevel@tonic-gate 				return (sctp_ipif);
2063510Svi117747 			} else if (sctp_ipif->sctp_ipif_state ==
2073510Svi117747 			    SCTP_IPIFS_DOWN && backup_ipif == NULL) {
2083510Svi117747 				backup_ipif = sctp_ipif;
2090Sstevel@tonic-gate 			}
2100Sstevel@tonic-gate 		}
2113510Svi117747 		rw_exit(&sctp_ipif->sctp_ipif_lock);
2123510Svi117747 		sctp_ipif = list_next(
2133510Svi117747 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list, sctp_ipif);
2143510Svi117747 	}
2153510Svi117747 	if (backup_ipif != NULL) {
2163510Svi117747 		if (refhold)
2173510Svi117747 			SCTP_IPIF_REFHOLD(backup_ipif);
2183510Svi117747 		rw_exit(&sctps->sctps_g_ipifs_lock);
2193510Svi117747 		return (backup_ipif);
2200Sstevel@tonic-gate 	}
2213448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2220Sstevel@tonic-gate 	return (NULL);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
2270Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
2280Sstevel@tonic-gate  * Called with no locks held.
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate static int
2310Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
2340Sstevel@tonic-gate 	int			i;
2350Sstevel@tonic-gate 	int			j;
2360Sstevel@tonic-gate 	int			error = 0;
2378903SVenu.Iyer@Sun.COM 	sctp_stack_t		*sctps = sctp->sctp_sctps;
2388903SVenu.Iyer@Sun.COM 	boolean_t		isv6;
2390Sstevel@tonic-gate 
2403448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
2410Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
2423448Sdh155122 		if (sctps->sctps_g_ipifs[i].ipif_count == 0)
2430Sstevel@tonic-gate 			continue;
2443448Sdh155122 		sctp_ipif = list_head(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
2453448Sdh155122 		for (j = 0; j < sctps->sctps_g_ipifs[i].ipif_count; j++) {
2460Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
2478903SVenu.Iyer@Sun.COM 			isv6 = sctp_ipif->sctp_ipif_isv6;
248432Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
2490Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
2502263Ssommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
2518903SVenu.Iyer@Sun.COM 			    SCTP_IS_ADDR_UNSPEC(!isv6,
2528903SVenu.Iyer@Sun.COM 			    sctp_ipif->sctp_ipif_saddr) ||
2538903SVenu.Iyer@Sun.COM 			    (sctp->sctp_ipversion == IPV4_VERSION && isv6) ||
2548903SVenu.Iyer@Sun.COM 			    (sctp->sctp_connp->conn_ipv6_v6only && !isv6)) {
2550Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2560Sstevel@tonic-gate 				sctp_ipif = list_next(
2573448Sdh155122 				    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
2583448Sdh155122 				    sctp_ipif);
2590Sstevel@tonic-gate 				continue;
2600Sstevel@tonic-gate 			}
2610Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2620Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
263432Svi117747 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
2643510Svi117747 			    B_FALSE, B_FALSE);
2653510Svi117747 			if (error != 0 && error != EALREADY)
2660Sstevel@tonic-gate 				goto free_stuff;
2673448Sdh155122 			sctp_ipif = list_next(
2683448Sdh155122 			    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
2690Sstevel@tonic-gate 			    sctp_ipif);
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 	}
2723448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2730Sstevel@tonic-gate 	return (0);
2740Sstevel@tonic-gate free_stuff:
2753448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2760Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2770Sstevel@tonic-gate 	return (ENOMEM);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2820Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
283852Svi117747  * or error. If the caller wants the list of addresses, it sends a pre-allocated
284852Svi117747  * buffer - list. Currently, this list is only used on a clustered node when
285852Svi117747  * the SCTP is in the listen state (from sctp_bind_add()). When called on a
286852Svi117747  * clustered node, the input is always a list of addresses (even if the
287852Svi117747  * original bind() was to INADDR_ANY).
2880Sstevel@tonic-gate  * Called with no locks held.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate int
291852Svi117747 sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
292852Svi117747     uchar_t *list, size_t lsize)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2950Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2960Sstevel@tonic-gate 	struct in_addr		*addr4;
2970Sstevel@tonic-gate 	in6_addr_t		addr;
2980Sstevel@tonic-gate 	int			cnt;
2990Sstevel@tonic-gate 	int			err = 0;
3000Sstevel@tonic-gate 	int			saddr_cnt = 0;
3010Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
3020Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
3030Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
3040Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
305852Svi117747 	uchar_t			*p = list;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	/*
3080Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
3090Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
3100Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
3110Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
3120Sstevel@tonic-gate 	 * addresses.
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
3150Sstevel@tonic-gate 		check_lport = B_TRUE;
3160Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
3170Sstevel@tonic-gate 			check_addrs = B_TRUE;
3180Sstevel@tonic-gate 	}
319852Svi117747 
3200Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3210Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
3220Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3230Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
3240Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
3250Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
326852Svi117747 		uint_t		ifindex = 0;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		switch (sctp->sctp_family) {
3290Sstevel@tonic-gate 		case AF_INET:
3300Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
3310Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
3320Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
3330Sstevel@tonic-gate 				err = EINVAL;
3340Sstevel@tonic-gate 				goto free_ret;
3350Sstevel@tonic-gate 			}
3360Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
3370Sstevel@tonic-gate 			if (check_addrs &&
3380Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
3390Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
3405215Skcpoon 			    CLASSD(addr4->s_addr))) {
3410Sstevel@tonic-gate 				err = EINVAL;
3420Sstevel@tonic-gate 				goto free_ret;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
3450Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
3460Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3470Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3480Sstevel@tonic-gate 			}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 			break;
3510Sstevel@tonic-gate 		case AF_INET6:
3520Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3530Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
3540Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
3550Sstevel@tonic-gate 				err = EINVAL;
3560Sstevel@tonic-gate 				goto free_ret;
3570Sstevel@tonic-gate 			}
3580Sstevel@tonic-gate 			addr = sin6->sin6_addr;
359852Svi117747 			/* Contains the interface index */
360852Svi117747 			ifindex = sin6->sin6_scope_id;
3610Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
3620Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3630Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3640Sstevel@tonic-gate 				goto free_ret;
3650Sstevel@tonic-gate 			}
3660Sstevel@tonic-gate 			if (check_addrs &&
3670Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3680Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3690Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3700Sstevel@tonic-gate 				err = EINVAL;
3710Sstevel@tonic-gate 				goto free_ret;
3720Sstevel@tonic-gate 			}
3730Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3740Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3750Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3760Sstevel@tonic-gate 			}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 			break;
3790Sstevel@tonic-gate 		default:
3800Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3810Sstevel@tonic-gate 			goto free_ret;
3820Sstevel@tonic-gate 		}
3830Sstevel@tonic-gate 		if (lookup_saddr) {
3843510Svi117747 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
3853510Svi117747 			    sctp->sctp_zoneid, !sctp->sctp_connp->conn_allzones,
3863510Svi117747 			    ifindex, 0, B_TRUE, sctp->sctp_sctps);
3870Sstevel@tonic-gate 			if (ipif == NULL) {
3880Sstevel@tonic-gate 				/* Address not in the list */
3890Sstevel@tonic-gate 				err = EINVAL;
3900Sstevel@tonic-gate 				goto free_ret;
391852Svi117747 			} else if (check_addrs && SCTP_IS_IPIF_LOOPBACK(ipif) &&
392852Svi117747 			    cl_sctp_check_addrs == NULL) {
3930Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3940Sstevel@tonic-gate 				err = EINVAL;
3950Sstevel@tonic-gate 				goto free_ret;
3960Sstevel@tonic-gate 			}
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 		if (!bind_to_all) {
399432Svi117747 			/*
400432Svi117747 			 * If an address is added after association setup,
401432Svi117747 			 * we need to wait for the peer to send us an ASCONF
402432Svi117747 			 * ACK before we can start using it.
403432Svi117747 			 * saddr_ipif_dontsrc will be reset (to 0) when we
404432Svi117747 			 * get the ASCONF ACK for this address.
405432Svi117747 			 */
406432Svi117747 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
4073510Svi117747 			    check_addrs ? B_TRUE : B_FALSE, B_FALSE);
4080Sstevel@tonic-gate 			if (err != 0) {
4090Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
4100Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
4110Sstevel@tonic-gate 					err = EADDRINUSE;
4120Sstevel@tonic-gate 				goto free_ret;
4130Sstevel@tonic-gate 			}
4140Sstevel@tonic-gate 			saddr_cnt++;
415852Svi117747 			if (lsize >= sizeof (addr)) {
416852Svi117747 				bcopy(&addr, p, sizeof (addr));
417852Svi117747 				p += sizeof (addr);
418852Svi117747 				lsize -= sizeof (addr);
419852Svi117747 			}
4200Sstevel@tonic-gate 		}
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 	if (bind_to_all) {
4230Sstevel@tonic-gate 		/*
4240Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
4250Sstevel@tonic-gate 		 * inaddr_any.
4260Sstevel@tonic-gate 		 */
4270Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
4280Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
4290Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
4300Sstevel@tonic-gate 		}
4310Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
4320Sstevel@tonic-gate 		if (err != 0)
4330Sstevel@tonic-gate 			return (err);
4340Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4370Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4380Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4390Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4400Sstevel@tonic-gate 	return (0);
4410Sstevel@tonic-gate free_ret:
4420Sstevel@tonic-gate 	if (saddr_cnt != 0)
4430Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
4440Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4450Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4460Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4470Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4480Sstevel@tonic-gate 	return (err);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate static int
452432Svi117747 sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
4533510Svi117747     boolean_t dontsrc, boolean_t allow_dup)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	int			cnt;
4560Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4573510Svi117747 	int			hindex;
4580Sstevel@tonic-gate 
4593510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->sctp_ipif_saddr,
4603510Svi117747 	    ipif->sctp_ipif_isv6);
4613510Svi117747 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
4623510Svi117747 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
4633510Svi117747 		if (IN6_ARE_ADDR_EQUAL(&ipif_obj->saddr_ipifp->sctp_ipif_saddr,
4643510Svi117747 		    &ipif->sctp_ipif_saddr)) {
4653510Svi117747 			if (ipif->sctp_ipif_id !=
4663510Svi117747 			    ipif_obj->saddr_ipifp->sctp_ipif_id &&
4673510Svi117747 			    ipif_obj->saddr_ipifp->sctp_ipif_state ==
4683510Svi117747 			    SCTP_IPIFS_DOWN && ipif->sctp_ipif_state ==
4693510Svi117747 			    SCTP_IPIFS_UP) {
4703510Svi117747 				SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
4713510Svi117747 				ipif_obj->saddr_ipifp = ipif;
4723510Svi117747 				ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
4733510Svi117747 				return (0);
4743510Svi117747 			} else if (!allow_dup || ipif->sctp_ipif_id ==
4753510Svi117747 			    ipif_obj->saddr_ipifp->sctp_ipif_id) {
4763510Svi117747 				return (EALREADY);
4773510Svi117747 			}
4783510Svi117747 		}
4793510Svi117747 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
4800Sstevel@tonic-gate 		    ipif_obj);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4830Sstevel@tonic-gate 	if (ipif_obj == NULL) {
4840Sstevel@tonic-gate 		/* Need to do something */
4850Sstevel@tonic-gate 		return (ENOMEM);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
488432Svi117747 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
4893510Svi117747 	list_insert_tail(&sctp->sctp_saddrs[hindex].sctp_ipif_list, ipif_obj);
4903510Svi117747 	sctp->sctp_saddrs[hindex].ipif_count++;
4910Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
4920Sstevel@tonic-gate 	return (0);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4954818Skcpoon /*
4964818Skcpoon  * Given a source address, walk through the peer address list to see
4974818Skcpoon  * if the source address is being used.  If it is, reset that.
4984818Skcpoon  */
4994818Skcpoon static void
5004818Skcpoon sctp_fix_saddr(sctp_t *sctp, in6_addr_t *saddr)
5014818Skcpoon {
5024818Skcpoon 	sctp_faddr_t	*fp;
5034818Skcpoon 
5044818Skcpoon 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
5054818Skcpoon 		if (!IN6_ARE_ADDR_EQUAL(&fp->saddr, saddr))
5064818Skcpoon 			continue;
5074818Skcpoon 		if (fp->ire != NULL) {
5084818Skcpoon 			IRE_REFRELE_NOTR(fp->ire);
5094818Skcpoon 			fp->ire = NULL;
5104818Skcpoon 		}
5114818Skcpoon 		V6_SET_ZERO(fp->saddr);
5124818Skcpoon 	}
5134818Skcpoon }
5144818Skcpoon 
5150Sstevel@tonic-gate static void
5160Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	int			cnt;
5190Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
5203510Svi117747 	int			hindex;
5210Sstevel@tonic-gate 
5223510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->sctp_ipif_saddr,
5233510Svi117747 	    ipif->sctp_ipif_isv6);
5243510Svi117747 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
5253510Svi117747 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
5263510Svi117747 		if (IN6_ARE_ADDR_EQUAL(&ipif_obj->saddr_ipifp->sctp_ipif_saddr,
5273510Svi117747 		    &ipif->sctp_ipif_saddr)) {
5283510Svi117747 			list_remove(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
5290Sstevel@tonic-gate 			    ipif_obj);
5303510Svi117747 			sctp->sctp_saddrs[hindex].ipif_count--;
5310Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
5324818Skcpoon 			sctp_fix_saddr(sctp, &ipif->sctp_ipif_saddr);
5330Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
5340Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
5350Sstevel@tonic-gate 			break;
5360Sstevel@tonic-gate 		}
5373510Svi117747 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
5380Sstevel@tonic-gate 		    ipif_obj);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate static int
5430Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	int			i;
5460Sstevel@tonic-gate 	int			j;
5470Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
5480Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
5490Sstevel@tonic-gate 	int			overlap = 0;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
5520Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5530Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
5540Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
5553510Svi117747 			if (IN6_ARE_ADDR_EQUAL(
5563510Svi117747 			    &obj1->saddr_ipifp->sctp_ipif_saddr,
5573510Svi117747 			    &obj2->saddr_ipifp->sctp_ipif_saddr)) {
5580Sstevel@tonic-gate 				overlap++;
5590Sstevel@tonic-gate 				break;
5600Sstevel@tonic-gate 			}
5610Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
5620Sstevel@tonic-gate 			    obj2);
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 	return (overlap);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate int
5700Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	int		i;
5730Sstevel@tonic-gate 	int		overlap = 0;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5760Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
5770Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5810Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5820Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
5860Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if (overlap > 0)
5890Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate static int
5950Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	int			i;
5980Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5990Sstevel@tonic-gate 	int			error = 0;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
6020Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
6030Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
604432Svi117747 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
6053510Svi117747 		    B_FALSE, B_FALSE);
6063510Svi117747 		ASSERT(error != EALREADY);
6070Sstevel@tonic-gate 		if (error != 0)
6080Sstevel@tonic-gate 			return (error);
6090Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 	return (error);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate int
6150Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate 	int	error = 0;
6180Sstevel@tonic-gate 	int	i;
6190Sstevel@tonic-gate 
620432Svi117747 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
6210Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
6240Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
6250Sstevel@tonic-gate 			continue;
6260Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
6270Sstevel@tonic-gate 		if (error != 0) {
6280Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
6290Sstevel@tonic-gate 			return (error);
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 	return (0);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate void
6360Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	int			i;
6390Sstevel@tonic-gate 	int			l;
6400Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
6430Sstevel@tonic-gate 		return;
6440Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
6450Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
6460Sstevel@tonic-gate 			continue;
6470Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6480Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
6490Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
6500Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
6510Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
6520Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
6530Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
6560Sstevel@tonic-gate 	}
657432Svi117747 	if (sctp->sctp_bound_to_all == 1)
658432Svi117747 		sctp->sctp_bound_to_all = 0;
6590Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate /*
6630Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
6640Sstevel@tonic-gate  * held.
6650Sstevel@tonic-gate  */
6660Sstevel@tonic-gate void
6670Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	int		i;
6700Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
6710Sstevel@tonic-gate 	uint_t		index;
6723448Sdh155122 	netstack_t	*ns = ill->ill_ipst->ips_netstack;
6733448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
6740Sstevel@tonic-gate 
6753448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_WRITER);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
6783448Sdh155122 	sctp_ill = list_head(&sctps->sctps_g_ills[index].sctp_ill_list);
6793448Sdh155122 	for (i = 0; i < sctps->sctps_g_ills[index].ill_count; i++) {
6804311Svi117747 		if ((sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill)) &&
6814311Svi117747 		    (sctp_ill->sctp_ill_isv6 == ill->ill_isv6)) {
6820Sstevel@tonic-gate 			break;
6834311Svi117747 		}
6843448Sdh155122 		sctp_ill = list_next(&sctps->sctps_g_ills[index].sctp_ill_list,
6850Sstevel@tonic-gate 		    sctp_ill);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	switch (op) {
6890Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
6900Sstevel@tonic-gate 		if (sctp_ill != NULL) {
6910Sstevel@tonic-gate 			/* Unmark it if it is condemned */
6920Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
6930Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
6943448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
6950Sstevel@tonic-gate 			return;
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
6980Sstevel@tonic-gate 		/* Need to re-try? */
6990Sstevel@tonic-gate 		if (sctp_ill == NULL) {
7004311Svi117747 			cmn_err(CE_WARN, "sctp_update_ill: error adding "
7014311Svi117747 			    "ILL %p to SCTP's ILL list", (void *)ill);
7023448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
7030Sstevel@tonic-gate 			return;
7040Sstevel@tonic-gate 		}
7053510Svi117747 		sctp_ill->sctp_ill_name = kmem_zalloc(ill->ill_name_length,
7063510Svi117747 		    KM_NOSLEEP);
7070Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
7084311Svi117747 			cmn_err(CE_WARN, "sctp_update_ill: error adding "
7094311Svi117747 			    "ILL %p to SCTP's ILL list", (void *)ill);
7100Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
7113448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
7120Sstevel@tonic-gate 			return;
7130Sstevel@tonic-gate 		}
7140Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
7150Sstevel@tonic-gate 		    ill->ill_name_length);
7160Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
7170Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
7180Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
7193448Sdh155122 		sctp_ill->sctp_ill_netstack = ns;	/* No netstack_hold */
7204311Svi117747 		sctp_ill->sctp_ill_isv6 = ill->ill_isv6;
7213448Sdh155122 		list_insert_tail(&sctps->sctps_g_ills[index].sctp_ill_list,
7220Sstevel@tonic-gate 		    (void *)sctp_ill);
7233448Sdh155122 		sctps->sctps_g_ills[index].ill_count++;
7243448Sdh155122 		sctps->sctps_ills_count++;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		break;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 		if (sctp_ill == NULL) {
7313448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
7320Sstevel@tonic-gate 			return;
7330Sstevel@tonic-gate 		}
7340Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
7353448Sdh155122 			list_remove(&sctps->sctps_g_ills[index].sctp_ill_list,
7360Sstevel@tonic-gate 			    (void *)sctp_ill);
7373448Sdh155122 			sctps->sctps_g_ills[index].ill_count--;
7383448Sdh155122 			sctps->sctps_ills_count--;
7390Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
7400Sstevel@tonic-gate 			    ill->ill_name_length);
7410Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
7420Sstevel@tonic-gate 		} else {
7430Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
7440Sstevel@tonic-gate 		}
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 		break;
7470Sstevel@tonic-gate 	}
7483448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate 
7514311Svi117747 /*
7524311Svi117747  * The ILL's index is being changed, just remove it from the old list,
7534311Svi117747  * change the SCTP ILL's index and re-insert using the new index.
7544311Svi117747  */
7554311Svi117747 void
7564311Svi117747 sctp_ill_reindex(ill_t *ill, uint_t orig_ill_index)
7574311Svi117747 {
7584311Svi117747 	sctp_ill_t	*sctp_ill = NULL;
7594311Svi117747 	sctp_ill_t	*nxt_sill;
7604311Svi117747 	uint_t		indx;
7614311Svi117747 	uint_t		nindx;
7624311Svi117747 	boolean_t	once = B_FALSE;
7634311Svi117747 	netstack_t	*ns = ill->ill_ipst->ips_netstack;
7644311Svi117747 	sctp_stack_t	*sctps = ns->netstack_sctp;
7654311Svi117747 
7664311Svi117747 	rw_enter(&sctps->sctps_g_ills_lock, RW_WRITER);
7674311Svi117747 
7684311Svi117747 	indx = SCTP_ILL_HASH_FN(orig_ill_index);
7694311Svi117747 	nindx = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
7704311Svi117747 	sctp_ill = list_head(&sctps->sctps_g_ills[indx].sctp_ill_list);
7714311Svi117747 	while (sctp_ill != NULL) {
7724311Svi117747 		nxt_sill = list_next(&sctps->sctps_g_ills[indx].sctp_ill_list,
7734311Svi117747 		    sctp_ill);
7744311Svi117747 		if (sctp_ill->sctp_ill_index == orig_ill_index) {
7754311Svi117747 			sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
7764311Svi117747 			/*
7774311Svi117747 			 * if the new index hashes to the same value, all's
7784311Svi117747 			 * done.
7794311Svi117747 			 */
7804311Svi117747 			if (nindx != indx) {
7814311Svi117747 				list_remove(
7824311Svi117747 				    &sctps->sctps_g_ills[indx].sctp_ill_list,
7834311Svi117747 				    (void *)sctp_ill);
7844311Svi117747 				sctps->sctps_g_ills[indx].ill_count--;
7854311Svi117747 				list_insert_tail(
7864311Svi117747 				    &sctps->sctps_g_ills[nindx].sctp_ill_list,
7874311Svi117747 				    (void *)sctp_ill);
7884311Svi117747 				sctps->sctps_g_ills[nindx].ill_count++;
7894311Svi117747 			}
7904311Svi117747 			if (once)
7914311Svi117747 				break;
7924311Svi117747 			/* We might have one for v4 and for v6 */
7934311Svi117747 			once = B_TRUE;
7944311Svi117747 		}
7954311Svi117747 		sctp_ill = nxt_sill;
7964311Svi117747 	}
7974311Svi117747 	rw_exit(&sctps->sctps_g_ills_lock);
7984311Svi117747 }
7994311Svi117747 
8000Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
8010Sstevel@tonic-gate void
8020Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
8050Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
8060Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
8073510Svi117747 	uint_t		hindex;
8080Sstevel@tonic-gate 	int		i;
8093448Sdh155122 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
8103448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
8110Sstevel@tonic-gate 
8123448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
8133448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
8140Sstevel@tonic-gate 
8153510Svi117747 	hindex = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
8163510Svi117747 	fsctp_ill = list_head(&sctps->sctps_g_ills[hindex].sctp_ill_list);
8173510Svi117747 	for (i = 0; i < sctps->sctps_g_ills[hindex].ill_count; i++) {
8184311Svi117747 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill) &&
8194311Svi117747 		    fsctp_ill->sctp_ill_isv6 == f_ill->ill_isv6) {
8200Sstevel@tonic-gate 			break;
8214311Svi117747 		}
8223510Svi117747 		fsctp_ill = list_next(
8233510Svi117747 		    &sctps->sctps_g_ills[hindex].sctp_ill_list, fsctp_ill);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8263510Svi117747 	hindex = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
8273510Svi117747 	tsctp_ill = list_head(&sctps->sctps_g_ills[hindex].sctp_ill_list);
8283510Svi117747 	for (i = 0; i < sctps->sctps_g_ills[hindex].ill_count; i++) {
8294311Svi117747 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill) &&
8304311Svi117747 		    tsctp_ill->sctp_ill_isv6 == t_ill->ill_isv6) {
8310Sstevel@tonic-gate 			break;
8324311Svi117747 		}
8333510Svi117747 		tsctp_ill = list_next(
8343510Svi117747 		    &sctps->sctps_g_ills[hindex].sctp_ill_list, tsctp_ill);
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 
8373510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr,
8383510Svi117747 	    ipif->ipif_ill->ill_isv6);
8393510Svi117747 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
8403510Svi117747 	for (i = 0; i < sctps->sctps_g_ipifs[hindex].ipif_count; i++) {
8410Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
8420Sstevel@tonic-gate 			break;
8433448Sdh155122 		sctp_ipif = list_next(
8443510Svi117747 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list, sctp_ipif);
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 	/* Should be an ASSERT? */
8470Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
8480Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
8490Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
8503448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
8513448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
8520Sstevel@tonic-gate 		return;
8530Sstevel@tonic-gate 	}
8540Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8550Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
8560Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
8570Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
8580Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
8590Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
8603448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
8613448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8643510Svi117747 /*
8653510Svi117747  * Walk the list of SCTPs and find each that has oipif in it's saddr list, and
8663510Svi117747  * if so replace it with nipif.
8673510Svi117747  */
8683510Svi117747 void
8693510Svi117747 sctp_update_saddrs(sctp_ipif_t *oipif, sctp_ipif_t *nipif, int idx,
8703510Svi117747     sctp_stack_t *sctps)
8713510Svi117747 {
8723510Svi117747 	sctp_t			*sctp;
8733510Svi117747 	sctp_t			*sctp_prev = NULL;
8743510Svi117747 	sctp_saddr_ipif_t	*sobj;
8753510Svi117747 	int			count;
8763510Svi117747 
8773510Svi117747 	sctp = sctps->sctps_gsctp;
8783510Svi117747 	mutex_enter(&sctps->sctps_g_lock);
8793510Svi117747 	while (sctp != NULL && oipif->sctp_ipif_refcnt > 0) {
8803510Svi117747 		mutex_enter(&sctp->sctp_reflock);
8813510Svi117747 		if (sctp->sctp_condemned ||
8823510Svi117747 		    sctp->sctp_saddrs[idx].ipif_count <= 0) {
8833510Svi117747 			mutex_exit(&sctp->sctp_reflock);
8843510Svi117747 			sctp = list_next(&sctps->sctps_g_list, sctp);
8853510Svi117747 			continue;
8863510Svi117747 		}
8873510Svi117747 		sctp->sctp_refcnt++;
8883510Svi117747 		mutex_exit(&sctp->sctp_reflock);
8893510Svi117747 		mutex_exit(&sctps->sctps_g_lock);
8903510Svi117747 		if (sctp_prev != NULL)
8913510Svi117747 			SCTP_REFRELE(sctp_prev);
8923510Svi117747 
8933510Svi117747 		RUN_SCTP(sctp);
8943510Svi117747 		sobj = list_head(&sctp->sctp_saddrs[idx].sctp_ipif_list);
8953510Svi117747 		for (count = 0; count <
8963510Svi117747 		    sctp->sctp_saddrs[idx].ipif_count; count++) {
8973510Svi117747 			if (sobj->saddr_ipifp == oipif) {
8983510Svi117747 				SCTP_IPIF_REFHOLD(nipif);
8993510Svi117747 				sobj->saddr_ipifp = nipif;
9003510Svi117747 				ASSERT(oipif->sctp_ipif_refcnt > 0);
9013510Svi117747 				/* We have the writer lock */
9023510Svi117747 				oipif->sctp_ipif_refcnt--;
9033510Svi117747 				/*
9043510Svi117747 				 * Can't have more than one referring
9053510Svi117747 				 * to the same sctp_ipif.
9063510Svi117747 				 */
9073510Svi117747 				break;
9083510Svi117747 			}
9093510Svi117747 			sobj = list_next(&sctp->sctp_saddrs[idx].sctp_ipif_list,
9103510Svi117747 			    sobj);
9113510Svi117747 		}
9123510Svi117747 		WAKE_SCTP(sctp);
9133510Svi117747 		sctp_prev = sctp;
9143510Svi117747 		mutex_enter(&sctps->sctps_g_lock);
9153510Svi117747 		sctp = list_next(&sctps->sctps_g_list, sctp);
9163510Svi117747 	}
9173510Svi117747 	mutex_exit(&sctps->sctps_g_lock);
9183510Svi117747 	if (sctp_prev != NULL)
9193510Svi117747 		SCTP_REFRELE(sctp_prev);
9203510Svi117747 }
9213510Svi117747 
9223510Svi117747 /*
9233510Svi117747  * Given an ipif, walk the hash list in the global ipif table and for
9243510Svi117747  * any other SCTP ipif with the same address and non-zero reference, walk
9253510Svi117747  * the SCTP list and update the saddr list, if required, to point to the
926*9705SVenu.Iyer@Sun.COM  * new SCTP ipif. If it is a loopback interface, then there could be
927*9705SVenu.Iyer@Sun.COM  * multiple interfaces with 127.0.0.1 if there are zones configured, so
928*9705SVenu.Iyer@Sun.COM  * check the zoneid in addition to the address.
9293510Svi117747  */
9303510Svi117747 void
9313510Svi117747 sctp_chk_and_updt_saddr(int hindex, sctp_ipif_t *ipif, sctp_stack_t *sctps)
9323510Svi117747 {
9333510Svi117747 	int		cnt;
9343510Svi117747 	sctp_ipif_t	*sipif;
9353510Svi117747 
9363510Svi117747 	ASSERT(sctps->sctps_g_ipifs[hindex].ipif_count > 0);
9373510Svi117747 	ASSERT(ipif->sctp_ipif_state == SCTP_IPIFS_UP);
9383510Svi117747 
9393510Svi117747 	sipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
9403510Svi117747 	for (cnt = 0; cnt < sctps->sctps_g_ipifs[hindex].ipif_count; cnt++) {
9413510Svi117747 		rw_enter(&sipif->sctp_ipif_lock, RW_WRITER);
9423510Svi117747 		if (sipif->sctp_ipif_id != ipif->sctp_ipif_id &&
9433510Svi117747 		    IN6_ARE_ADDR_EQUAL(&sipif->sctp_ipif_saddr,
944*9705SVenu.Iyer@Sun.COM 		    &ipif->sctp_ipif_saddr) && sipif->sctp_ipif_refcnt > 0 &&
945*9705SVenu.Iyer@Sun.COM 		    (!SCTP_IS_IPIF_LOOPBACK(ipif) || ipif->sctp_ipif_zoneid ==
946*9705SVenu.Iyer@Sun.COM 		    sipif->sctp_ipif_zoneid)) {
9473510Svi117747 			/*
9483510Svi117747 			 * There can only be one address up at any time
9493510Svi117747 			 * and we are here because ipif has been brought
9503510Svi117747 			 * up.
9513510Svi117747 			 */
9523510Svi117747 			ASSERT(sipif->sctp_ipif_state != SCTP_IPIFS_UP);
9533510Svi117747 			/*
9543510Svi117747 			 * Someone has a reference to this we need to update to
9553510Svi117747 			 * point to the new sipif.
9563510Svi117747 			 */
9573510Svi117747 			sctp_update_saddrs(sipif, ipif, hindex, sctps);
9583510Svi117747 		}
9593510Svi117747 		rw_exit(&sipif->sctp_ipif_lock);
9603510Svi117747 		sipif = list_next(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
9613510Svi117747 		    sipif);
9623510Svi117747 	}
9633510Svi117747 }
9643510Svi117747 
9653510Svi117747 /*
9663510Svi117747  * Insert a new SCTP ipif using 'ipif'. v6addr is the address that existed
9673510Svi117747  * prior to the current address in 'ipif'. Only when an existing address
9683510Svi117747  * is changed on an IPIF, will v6addr be specified. If the IPIF already
9693510Svi117747  * exists in the global SCTP ipif table, then we either removed it, if
9703510Svi117747  * it doesn't have any existing reference, or mark it condemned otherwise.
9713510Svi117747  * If an address is being brought up (IPIF_UP), then we need to scan
9723510Svi117747  * the SCTP list to check if there is any SCTP that points to the *same*
9733510Svi117747  * address on a different SCTP ipif and update in that case.
9743510Svi117747  */
9753510Svi117747 void
9763510Svi117747 sctp_update_ipif_addr(ipif_t *ipif, in6_addr_t v6addr)
9773510Svi117747 {
9783510Svi117747 	ill_t		*ill = ipif->ipif_ill;
9793510Svi117747 	int		i;
9803510Svi117747 	sctp_ill_t	*sctp_ill;
9813510Svi117747 	sctp_ill_t	*osctp_ill;
9823510Svi117747 	sctp_ipif_t	*sctp_ipif = NULL;
9833510Svi117747 	sctp_ipif_t	*osctp_ipif = NULL;
9843510Svi117747 	uint_t		ill_index;
9853510Svi117747 	int		hindex;
9863510Svi117747 	sctp_stack_t	*sctps;
9873510Svi117747 
9883510Svi117747 	sctps = ipif->ipif_ill->ill_ipst->ips_netstack->netstack_sctp;
9893510Svi117747 
9903510Svi117747 	/* Index for new address */
9913510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr, ill->ill_isv6);
9923510Svi117747 
9933510Svi117747 	/*
9943510Svi117747 	 * The address on this IPIF is changing, we need to look for
9953510Svi117747 	 * this old address and mark it condemned, before creating
9963510Svi117747 	 * one for the new address.
9973510Svi117747 	 */
9983510Svi117747 	osctp_ipif = sctp_lookup_ipif_addr(&v6addr, B_FALSE,
9993510Svi117747 	    ipif->ipif_zoneid, B_TRUE, SCTP_ILL_TO_PHYINDEX(ill),
10003510Svi117747 	    ipif->ipif_seqid, B_FALSE, sctps);
10013510Svi117747 
10023510Svi117747 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
10033510Svi117747 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
10043510Svi117747 
10053510Svi117747 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
10063510Svi117747 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
10073510Svi117747 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
10084311Svi117747 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill) &&
10094311Svi117747 		    sctp_ill->sctp_ill_isv6 == ill->ill_isv6) {
10103510Svi117747 			break;
10114311Svi117747 		}
10123510Svi117747 		sctp_ill = list_next(
10133510Svi117747 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
10143510Svi117747 	}
10153510Svi117747 
10163510Svi117747 	if (sctp_ill == NULL) {
10174311Svi117747 		ip1dbg(("sctp_update_ipif_addr: ill not found ..\n"));
10183510Svi117747 		rw_exit(&sctps->sctps_g_ipifs_lock);
10193510Svi117747 		rw_exit(&sctps->sctps_g_ills_lock);
10204311Svi117747 		return;
10213510Svi117747 	}
10223510Svi117747 
10233510Svi117747 	if (osctp_ipif != NULL) {
10243510Svi117747 
10253510Svi117747 		/* The address is the same? */
10263510Svi117747 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &v6addr)) {
10273510Svi117747 			boolean_t	chk_n_updt = B_FALSE;
10283510Svi117747 
10293510Svi117747 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
10303510Svi117747 			rw_enter(&osctp_ipif->sctp_ipif_lock, RW_WRITER);
10313510Svi117747 			if (ipif->ipif_flags & IPIF_UP &&
10323510Svi117747 			    osctp_ipif->sctp_ipif_state != SCTP_IPIFS_UP) {
10333510Svi117747 				osctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
10343510Svi117747 				chk_n_updt = B_TRUE;
10353510Svi117747 			} else {
10363510Svi117747 				osctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
10373510Svi117747 			}
10383510Svi117747 			osctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
10393510Svi117747 			rw_exit(&osctp_ipif->sctp_ipif_lock);
10403510Svi117747 			if (chk_n_updt) {
10413510Svi117747 				sctp_chk_and_updt_saddr(hindex, osctp_ipif,
10423510Svi117747 				    sctps);
10433510Svi117747 			}
10443510Svi117747 			rw_exit(&sctps->sctps_g_ipifs_lock);
10453510Svi117747 			rw_exit(&sctps->sctps_g_ills_lock);
10463510Svi117747 			return;
10473510Svi117747 		}
10483510Svi117747 		/*
10493510Svi117747 		 * We are effectively removing this address from the ILL.
10503510Svi117747 		 */
10513510Svi117747 		if (osctp_ipif->sctp_ipif_refcnt != 0) {
10523510Svi117747 			osctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
10533510Svi117747 		} else {
10543510Svi117747 			list_t		*ipif_list;
10553510Svi117747 			int		ohindex;
10563510Svi117747 
10573510Svi117747 			osctp_ill = osctp_ipif->sctp_ipif_ill;
10583510Svi117747 			/* hash index for the old one */
10593510Svi117747 			ohindex = SCTP_IPIF_ADDR_HASH(
10603510Svi117747 			    osctp_ipif->sctp_ipif_saddr,
10613510Svi117747 			    osctp_ipif->sctp_ipif_isv6);
10623510Svi117747 
10633510Svi117747 			ipif_list =
10643510Svi117747 			    &sctps->sctps_g_ipifs[ohindex].sctp_ipif_list;
10653510Svi117747 
10663510Svi117747 			list_remove(ipif_list, (void *)osctp_ipif);
10673510Svi117747 			sctps->sctps_g_ipifs[ohindex].ipif_count--;
10683510Svi117747 			sctps->sctps_g_ipifs_count--;
10693510Svi117747 			rw_destroy(&osctp_ipif->sctp_ipif_lock);
10703510Svi117747 			kmem_free(osctp_ipif, sizeof (sctp_ipif_t));
10713510Svi117747 			(void) atomic_add_32_nv(&osctp_ill->sctp_ill_ipifcnt,
10723510Svi117747 			    -1);
10733510Svi117747 		}
10743510Svi117747 	}
10753510Svi117747 
10763510Svi117747 	sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
10773510Svi117747 	/* Try again? */
10783510Svi117747 	if (sctp_ipif == NULL) {
10794311Svi117747 		cmn_err(CE_WARN, "sctp_update_ipif_addr: error adding "
10804311Svi117747 		    "IPIF %p to SCTP's IPIF list", (void *)ipif);
10813510Svi117747 		rw_exit(&sctps->sctps_g_ipifs_lock);
10823510Svi117747 		rw_exit(&sctps->sctps_g_ills_lock);
10833510Svi117747 		return;
10843510Svi117747 	}
10853510Svi117747 	sctps->sctps_g_ipifs_count++;
10863510Svi117747 	rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
10873510Svi117747 	sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
10883510Svi117747 	sctp_ipif->sctp_ipif_ill = sctp_ill;
10893510Svi117747 	sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
10903510Svi117747 	sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
10913510Svi117747 	sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
10923510Svi117747 	if (ipif->ipif_flags & IPIF_UP)
10933510Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
10943510Svi117747 	else
10953510Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
10963510Svi117747 	sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
10973510Svi117747 	/*
10983510Svi117747 	 * We add it to the head so that it is quicker to find good/recent
10993510Svi117747 	 * additions.
11003510Svi117747 	 */
11013510Svi117747 	list_insert_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
11023510Svi117747 	    (void *)sctp_ipif);
11033510Svi117747 	sctps->sctps_g_ipifs[hindex].ipif_count++;
11043510Svi117747 	atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
11053510Svi117747 	if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_UP)
11063510Svi117747 		sctp_chk_and_updt_saddr(hindex, sctp_ipif, sctps);
11073510Svi117747 	rw_exit(&sctps->sctps_g_ipifs_lock);
11083510Svi117747 	rw_exit(&sctps->sctps_g_ills_lock);
11093510Svi117747 }
11103510Svi117747 
11110Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
11120Sstevel@tonic-gate void
11130Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
11140Sstevel@tonic-gate {
11150Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
11160Sstevel@tonic-gate 	int		i;
11170Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
11180Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
11190Sstevel@tonic-gate 	uint_t		ill_index;
11203510Svi117747 	uint_t		hindex;
11213448Sdh155122 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
11223448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
11250Sstevel@tonic-gate 
11263448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
11273448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
11303448Sdh155122 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
11313448Sdh155122 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
11324311Svi117747 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill) &&
11334311Svi117747 		    sctp_ill->sctp_ill_isv6 == ill->ill_isv6) {
11340Sstevel@tonic-gate 			break;
11354311Svi117747 		}
11363448Sdh155122 		sctp_ill = list_next(
11373448Sdh155122 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
11380Sstevel@tonic-gate 	}
11390Sstevel@tonic-gate 	if (sctp_ill == NULL) {
11403448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
11413448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
11420Sstevel@tonic-gate 		return;
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11453510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr,
11463510Svi117747 	    ipif->ipif_ill->ill_isv6);
11473510Svi117747 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
11483510Svi117747 	for (i = 0; i < sctps->sctps_g_ipifs[hindex].ipif_count; i++) {
11493510Svi117747 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid) {
11503510Svi117747 			ASSERT(IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
11513510Svi117747 			    &ipif->ipif_v6lcl_addr));
11520Sstevel@tonic-gate 			break;
11533510Svi117747 		}
11543448Sdh155122 		sctp_ipif = list_next(
11553510Svi117747 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
11560Sstevel@tonic-gate 		    sctp_ipif);
11570Sstevel@tonic-gate 	}
11583510Svi117747 	if (sctp_ipif == NULL) {
11590Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
11603448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
11613448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
11620Sstevel@tonic-gate 		return;
11630Sstevel@tonic-gate 	}
11643510Svi117747 	ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
11650Sstevel@tonic-gate 	switch (op) {
11660Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
11670Sstevel@tonic-gate 	{
11680Sstevel@tonic-gate 		list_t		*ipif_list;
11690Sstevel@tonic-gate 		list_t		*ill_list;
11700Sstevel@tonic-gate 
11713448Sdh155122 		ill_list = &sctps->sctps_g_ills[ill_index].sctp_ill_list;
11723510Svi117747 		ipif_list = &sctps->sctps_g_ipifs[hindex].sctp_ipif_list;
11730Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
11740Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
11753448Sdh155122 			rw_exit(&sctps->sctps_g_ipifs_lock);
11763448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
11770Sstevel@tonic-gate 			return;
11780Sstevel@tonic-gate 		}
11790Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
11803510Svi117747 		sctps->sctps_g_ipifs[hindex].ipif_count--;
11813448Sdh155122 		sctps->sctps_g_ipifs_count--;
11820Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
11830Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
11840Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
11853448Sdh155122 		if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
11863448Sdh155122 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
11870Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
11880Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
11890Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
11903448Sdh155122 				sctps->sctps_ills_count--;
11913448Sdh155122 				sctps->sctps_g_ills[ill_index].ill_count--;
11920Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
11930Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
11940Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
11950Sstevel@tonic-gate 			}
11960Sstevel@tonic-gate 		}
11970Sstevel@tonic-gate 		break;
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	case SCTP_IPIF_UP:
12010Sstevel@tonic-gate 
12023448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
12030Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
12040Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
12053510Svi117747 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1206432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
12070Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
12083510Svi117747 		sctp_chk_and_updt_saddr(hindex, sctp_ipif,
12093510Svi117747 		    ipif->ipif_ill->ill_ipst->ips_netstack->netstack_sctp);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 		break;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
12140Sstevel@tonic-gate 
12153448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
12160Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
12170Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
12180Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
1219432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
12200Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 		break;
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
12250Sstevel@tonic-gate 
12263448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
12270Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
12280Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
12293510Svi117747 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
12303510Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
12310Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 		break;
12340Sstevel@tonic-gate 	}
12353448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
12363448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate /*
12400Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
12410Sstevel@tonic-gate  * sctp locking by the caller.
12420Sstevel@tonic-gate  */
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate /* Remove a specific saddr from the list */
12450Sstevel@tonic-gate void
12460Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
12490Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
12520Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
12550Sstevel@tonic-gate 
1256432Svi117747 	if (sctp->sctp_bound_to_all == 1)
12570Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
12600Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
12630Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate /*
12670Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
12680Sstevel@tonic-gate  * Called with no locks held.
12690Sstevel@tonic-gate  */
12700Sstevel@tonic-gate void
12710Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
12720Sstevel@tonic-gate     boolean_t fanout_locked)
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
12750Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
12760Sstevel@tonic-gate 	int			cnt;
12770Sstevel@tonic-gate 	in6_addr_t		addr;
12780Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
1279852Svi117747 	int			ifindex = 0;
12800Sstevel@tonic-gate 
1281852Svi117747 	ASSERT(sctp->sctp_nsaddrs >= addcnt);
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	if (!fanout_locked) {
12840Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
12850Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
12860Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
12870Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
12880Sstevel@tonic-gate 	}
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
12910Sstevel@tonic-gate 		switch (sctp->sctp_family) {
12920Sstevel@tonic-gate 		case AF_INET:
12930Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
12940Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
12950Sstevel@tonic-gate 			break;
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 		case AF_INET6:
12980Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
12990Sstevel@tonic-gate 			addr = sin6->sin6_addr;
1300852Svi117747 			ifindex = sin6->sin6_scope_id;
13010Sstevel@tonic-gate 			break;
13020Sstevel@tonic-gate 		}
13033510Svi117747 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
13043510Svi117747 		    sctp->sctp_zoneid, !sctp->sctp_connp->conn_allzones,
13053510Svi117747 		    ifindex, 0, B_TRUE, sctp->sctp_sctps);
13060Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
13070Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
13080Sstevel@tonic-gate 	}
1309432Svi117747 	if (sctp->sctp_bound_to_all == 1)
13100Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	if (!fanout_locked) {
13130Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
13140Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
13150Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
13160Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate /*
13210Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
13220Sstevel@tonic-gate  * Called with no locks held.
13230Sstevel@tonic-gate  */
13240Sstevel@tonic-gate sctp_saddr_ipif_t *
1325852Svi117747 sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
13260Sstevel@tonic-gate {
13273510Svi117747 	int			cnt;
13283510Svi117747 	sctp_saddr_ipif_t	*ipif_obj;
13293510Svi117747 	int			hindex;
13300Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
13310Sstevel@tonic-gate 
13323510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
13333510Svi117747 	if (sctp->sctp_saddrs[hindex].ipif_count == 0)
13340Sstevel@tonic-gate 		return (NULL);
13350Sstevel@tonic-gate 
13363510Svi117747 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
13373510Svi117747 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
13383510Svi117747 		sctp_ipif = ipif_obj->saddr_ipifp;
13393510Svi117747 		/*
13403510Svi117747 		 * Zone check shouldn't be needed.
13413510Svi117747 		 */
13423510Svi117747 		if (IN6_ARE_ADDR_EQUAL(addr, &sctp_ipif->sctp_ipif_saddr) &&
13433510Svi117747 		    (ifindex == 0 ||
13443510Svi117747 		    ifindex == sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
13453510Svi117747 		    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state)) {
13463510Svi117747 			return (ipif_obj);
13473510Svi117747 		}
13483510Svi117747 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
13493510Svi117747 		    ipif_obj);
13503510Svi117747 	}
13513510Svi117747 	return (NULL);
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate 
1354432Svi117747 /* Given an address, add it to the source address list */
1355432Svi117747 int
1356852Svi117747 sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1357432Svi117747 {
1358432Svi117747 	sctp_ipif_t		*sctp_ipif;
1359432Svi117747 
13603510Svi117747 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp->sctp_zoneid,
13613510Svi117747 	    !sctp->sctp_connp->conn_allzones, ifindex, 0, B_TRUE,
13623510Svi117747 	    sctp->sctp_sctps);
1363432Svi117747 	if (sctp_ipif == NULL)
1364432Svi117747 		return (EINVAL);
1365432Svi117747 
13663510Svi117747 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE,
13673510Svi117747 	    B_FALSE) != 0) {
1368432Svi117747 		SCTP_IPIF_REFRELE(sctp_ipif);
1369432Svi117747 		return (EINVAL);
1370432Svi117747 	}
1371432Svi117747 	return (0);
1372432Svi117747 }
1373432Svi117747 
1374432Svi117747 /*
1375432Svi117747  * Remove or mark as dontsrc addresses that are currently not part of the
1376432Svi117747  * association. One would delete addresses when processing an INIT and
1377432Svi117747  * mark as dontsrc when processing an INIT-ACK.
1378432Svi117747  */
1379432Svi117747 void
13804818Skcpoon sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete,
13814818Skcpoon     in6_addr_t *no_del_addr)
1382432Svi117747 {
1383432Svi117747 	int			i;
1384432Svi117747 	int			l;
1385432Svi117747 	sctp_saddr_ipif_t	*obj;
1386432Svi117747 	int			scanned = 0;
1387432Svi117747 	int			naddr;
1388432Svi117747 	int			nsaddr;
1389432Svi117747 
1390432Svi117747 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1391432Svi117747 
1392432Svi117747 	/*
1393432Svi117747 	 * Irregardless of the supported address in the INIT, v4
1394432Svi117747 	 * must be supported.
1395432Svi117747 	 */
1396432Svi117747 	if (sctp->sctp_family == AF_INET)
1397432Svi117747 		supp_af = PARM_SUPP_V4;
1398432Svi117747 
1399432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
1400432Svi117747 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1401432Svi117747 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1402432Svi117747 			continue;
1403432Svi117747 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1404432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1405432Svi117747 		for (l = 0; l < naddr; l++) {
1406432Svi117747 			sctp_ipif_t	*ipif;
1407432Svi117747 
1408432Svi117747 			ipif = obj->saddr_ipifp;
1409432Svi117747 			scanned++;
1410432Svi117747 
14114818Skcpoon 			if (IN6_ARE_ADDR_EQUAL(&ipif->sctp_ipif_saddr,
14124818Skcpoon 			    no_del_addr)) {
14134818Skcpoon 				goto next_obj;
14144818Skcpoon 			}
14154818Skcpoon 
1416432Svi117747 			/*
1417432Svi117747 			 * Delete/mark dontsrc loopback/linklocal addresses and
1418432Svi117747 			 * unsupported address.
1419852Svi117747 			 * On a clustered node, we trust the clustering module
1420852Svi117747 			 * to do the right thing w.r.t loopback addresses, so
1421852Svi117747 			 * we ignore loopback addresses in this check.
1422432Svi117747 			 */
1423852Svi117747 			if ((SCTP_IS_IPIF_LOOPBACK(ipif) &&
1424852Svi117747 			    cl_sctp_check_addrs == NULL) ||
1425852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(ipif) ||
1426432Svi117747 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1427432Svi117747 				if (!delete) {
1428432Svi117747 					obj->saddr_ipif_unconfirmed = 1;
1429432Svi117747 					goto next_obj;
1430432Svi117747 				}
1431432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1432432Svi117747 					sctp->sctp_bound_to_all = 0;
1433432Svi117747 				if (scanned < nsaddr) {
1434432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1435432Svi117747 					    sctp_ipif_list, obj);
1436432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1437432Svi117747 					continue;
1438432Svi117747 				}
1439432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1440432Svi117747 			}
1441432Svi117747 	next_obj:
1442432Svi117747 			if (scanned >= nsaddr)
1443432Svi117747 				return;
1444432Svi117747 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1445432Svi117747 			    obj);
1446432Svi117747 		}
1447432Svi117747 	}
1448432Svi117747 }
1449432Svi117747 
1450432Svi117747 
14510Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
14520Sstevel@tonic-gate in6_addr_t
14534818Skcpoon sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6, boolean_t *addr_set)
14540Sstevel@tonic-gate {
14550Sstevel@tonic-gate 	int			i;
14560Sstevel@tonic-gate 	int			l;
14570Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
14580Sstevel@tonic-gate 	int			scanned = 0;
14590Sstevel@tonic-gate 	in6_addr_t		addr;
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
14620Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
14630Sstevel@tonic-gate 			continue;
14640Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
14650Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
14660Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1469432Svi117747 			if (!SCTP_DONT_SRC(obj) &&
14700Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
1471432Svi117747 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
14724818Skcpoon 				*addr_set = B_TRUE;
14730Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
14740Sstevel@tonic-gate 			}
14750Sstevel@tonic-gate 			scanned++;
14760Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
14770Sstevel@tonic-gate 				goto got_none;
14780Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
14790Sstevel@tonic-gate 			    obj);
14800Sstevel@tonic-gate 		}
14810Sstevel@tonic-gate 	}
14820Sstevel@tonic-gate got_none:
14830Sstevel@tonic-gate 	/* Need to double check this */
14840Sstevel@tonic-gate 	if (isv6 == B_TRUE)
14850Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
14860Sstevel@tonic-gate 	else
14870Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
14884818Skcpoon 	*addr_set = B_FALSE;
14890Sstevel@tonic-gate 	return (addr);
14900Sstevel@tonic-gate }
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate /*
14930Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
14940Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
14950Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
14960Sstevel@tonic-gate  */
14970Sstevel@tonic-gate int
14980Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
14990Sstevel@tonic-gate {
15000Sstevel@tonic-gate 	int			i;
15010Sstevel@tonic-gate 	int			l;
15020Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
15030Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
15040Sstevel@tonic-gate 	int			family = sctp->sctp_family;
15050Sstevel@tonic-gate 	int			max = *addrcnt;
15060Sstevel@tonic-gate 	size_t			added = 0;
15070Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
15080Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
15090Sstevel@tonic-gate 	int			scanned = 0;
15100Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
15130Sstevel@tonic-gate 		return (EINVAL);
15140Sstevel@tonic-gate 
1515852Svi117747 	/*
1516852Svi117747 	 * Skip loopback addresses for non-loopback assoc., ignore
1517852Svi117747 	 * this on a clustered node.
1518852Svi117747 	 */
1519852Svi117747 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback &&
1520852Svi117747 	    (cl_sctp_check_addrs == NULL)) {
15210Sstevel@tonic-gate 		skip_lback = B_TRUE;
1522852Svi117747 	}
1523852Svi117747 
15240Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
15250Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
15260Sstevel@tonic-gate 			continue;
15270Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
15280Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
15290Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
15300Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 			scanned++;
15330Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1534432Svi117747 			    SCTP_DONT_SRC(obj) ||
1535852Svi117747 			    (SCTP_IS_IPIF_LOOPBACK(ipif) && skip_lback)) {
15360Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
15370Sstevel@tonic-gate 					goto done;
15380Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
15390Sstevel@tonic-gate 				    sctp_ipif_list, obj);
15400Sstevel@tonic-gate 				continue;
15410Sstevel@tonic-gate 			}
15420Sstevel@tonic-gate 			switch (family) {
15430Sstevel@tonic-gate 			case AF_INET:
15440Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
15450Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
15460Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
15470Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
15480Sstevel@tonic-gate 				break;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 			case AF_INET6:
15510Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
15520Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
15530Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
15540Sstevel@tonic-gate 				sin6->sin6_addr = addr;
15550Sstevel@tonic-gate 				break;
15560Sstevel@tonic-gate 			}
15570Sstevel@tonic-gate 			added++;
15580Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
15590Sstevel@tonic-gate 				goto done;
15600Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
15610Sstevel@tonic-gate 			    obj);
15620Sstevel@tonic-gate 		}
15630Sstevel@tonic-gate 	}
15640Sstevel@tonic-gate done:
15650Sstevel@tonic-gate 	*addrcnt = added;
15660Sstevel@tonic-gate 	return (0);
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate /*
1570252Svi117747  * Given the supported address family, walk through the source address list
1571252Svi117747  * and return the total length of the available addresses. If 'p' is not
1572252Svi117747  * null, construct the parameter list for the addresses in 'p'.
1573432Svi117747  * 'modify' will only be set when we want the source address list to
1574432Svi117747  * be modified. The source address list will be modified only when
1575432Svi117747  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1576432Svi117747  * be false since the 'sctp' will be that of the listener.
15770Sstevel@tonic-gate  */
15780Sstevel@tonic-gate size_t
1579432Svi117747 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate 	int			i;
15820Sstevel@tonic-gate 	int			l;
15830Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1584252Svi117747 	size_t			paramlen = 0;
15850Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
15860Sstevel@tonic-gate 	int			scanned = 0;
1587432Svi117747 	int			naddr;
1588432Svi117747 	int			nsaddr;
1589852Svi117747 	boolean_t		del_ll = B_FALSE;
1590852Svi117747 	boolean_t		del_lb = B_FALSE;
1591852Svi117747 
15920Sstevel@tonic-gate 
1593852Svi117747 	/*
1594852Svi117747 	 * On a clustered node don't bother changing anything
1595852Svi117747 	 * on the loopback interface.
1596852Svi117747 	 */
1597852Svi117747 	if (modify && !sctp->sctp_loopback && (cl_sctp_check_addrs == NULL))
1598852Svi117747 		del_lb = B_TRUE;
1599852Svi117747 
1600852Svi117747 	if (modify && !sctp->sctp_linklocal)
1601852Svi117747 		del_ll = B_TRUE;
1602432Svi117747 
1603432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
16040Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
16050Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
16060Sstevel@tonic-gate 			continue;
16070Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1608432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1609432Svi117747 		for (l = 0; l < naddr; l++) {
16100Sstevel@tonic-gate 			in6_addr_t	addr;
16110Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
1612852Svi117747 			boolean_t	ipif_lb;
1613852Svi117747 			boolean_t	ipif_ll;
1614432Svi117747 			boolean_t	unsupp_af;
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
16170Sstevel@tonic-gate 			scanned++;
1618432Svi117747 
1619852Svi117747 			ipif_lb = SCTP_IS_IPIF_LOOPBACK(ipif);
1620852Svi117747 			ipif_ll = SCTP_IS_IPIF_LINKLOCAL(ipif);
1621432Svi117747 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1622432Svi117747 			/*
1623432Svi117747 			 * We need to either delete or skip loopback/linklocal
1624852Svi117747 			 * or unsupported addresses, if required.
1625432Svi117747 			 */
1626852Svi117747 			if ((ipif_ll && del_ll) || (ipif_lb && del_lb) ||
1627852Svi117747 			    (unsupp_af && modify)) {
1628432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1629432Svi117747 					sctp->sctp_bound_to_all = 0;
1630432Svi117747 				if (scanned < nsaddr) {
1631432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1632432Svi117747 					    sctp_ipif_list, obj);
1633432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1634432Svi117747 					continue;
1635432Svi117747 				}
1636432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1637432Svi117747 				goto next_addr;
1638852Svi117747 			} else if (ipif_ll || unsupp_af ||
1639852Svi117747 			    (ipif_lb && (cl_sctp_check_addrs == NULL))) {
1640252Svi117747 				goto next_addr;
16410Sstevel@tonic-gate 			}
1642432Svi117747 
1643432Svi117747 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1644432Svi117747 				goto next_addr;
1645252Svi117747 			if (p != NULL)
1646252Svi117747 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
16470Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1648432Svi117747 			if (!ipif->sctp_ipif_isv6) {
16490Sstevel@tonic-gate 				struct in_addr	*v4;
16500Sstevel@tonic-gate 
1651252Svi117747 				if (p != NULL) {
1652252Svi117747 					hdr->sph_type = htons(PARM_ADDR4);
1653252Svi117747 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1654252Svi117747 					v4 = (struct in_addr *)(hdr + 1);
1655252Svi117747 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1656252Svi117747 				}
1657252Svi117747 				paramlen += PARM_ADDR4_LEN;
1658432Svi117747 			} else {
1659252Svi117747 				if (p != NULL) {
1660252Svi117747 					hdr->sph_type = htons(PARM_ADDR6);
1661252Svi117747 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1662252Svi117747 					bcopy(&addr, hdr + 1, sizeof (addr));
1663252Svi117747 				}
1664252Svi117747 				paramlen += PARM_ADDR6_LEN;
16650Sstevel@tonic-gate 			}
1666252Svi117747 next_addr:
1667432Svi117747 			if (scanned >= nsaddr)
1668252Svi117747 				return (paramlen);
16690Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
16700Sstevel@tonic-gate 			    obj);
16710Sstevel@tonic-gate 		}
16720Sstevel@tonic-gate 	}
1673252Svi117747 	return (paramlen);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate 
1676852Svi117747 /*
1677852Svi117747  * This is used on a clustered node to obtain a list of addresses, the list
1678852Svi117747  * consists of sockaddr_in structs for v4 and sockaddr_in6 for v6. The list
1679852Svi117747  * is then passed onto the clustering module which sends back the correct
1680852Svi117747  * list based on the port info. Regardless of the input, i.e INADDR_ANY
1681852Svi117747  * or specific address(es), we create the list since it could be modified by
1682852Svi117747  * the clustering module. When given a list of addresses, we simply
1683852Svi117747  * create the list of sockaddr_in or sockaddr_in6 structs using those
1684852Svi117747  * addresses. If there is an INADDR_ANY in the input list, or if the
1685852Svi117747  * input is INADDR_ANY, we create a list of sockaddr_in or sockaddr_in6
1686852Svi117747  * structs consisting all the addresses in the global interface list
1687852Svi117747  * except those that are hosted on the loopback interface. We create
1688852Svi117747  * a list of sockaddr_in[6] structs just so that it can be directly input
1689852Svi117747  * to sctp_valid_addr_list() once the clustering module has processed it.
1690852Svi117747  */
1691852Svi117747 int
1692852Svi117747 sctp_get_addrlist(sctp_t *sctp, const void *addrs, uint32_t *addrcnt,
1693852Svi117747     uchar_t **addrlist, int *uspec, size_t *size)
1694852Svi117747 {
1695852Svi117747 	int			cnt;
1696852Svi117747 	int			icnt;
1697852Svi117747 	sctp_ipif_t		*sctp_ipif;
1698852Svi117747 	struct sockaddr_in	*s4;
1699852Svi117747 	struct sockaddr_in6	*s6;
1700852Svi117747 	uchar_t			*p;
1701852Svi117747 	int			err = 0;
17023448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
1703852Svi117747 
1704852Svi117747 	*addrlist = NULL;
1705852Svi117747 	*size = 0;
1706852Svi117747 
1707852Svi117747 	/*
1708852Svi117747 	 * Create a list of sockaddr_in[6] structs using the input list.
1709852Svi117747 	 */
1710852Svi117747 	if (sctp->sctp_family == AF_INET) {
1711852Svi117747 		*size = sizeof (struct sockaddr_in) * *addrcnt;
1712852Svi117747 		*addrlist = kmem_zalloc(*size,  KM_SLEEP);
1713852Svi117747 		p = *addrlist;
1714852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1715852Svi117747 			s4 = (struct sockaddr_in *)addrs + cnt;
1716852Svi117747 			/*
1717852Svi117747 			 * We need to create a list of all the available
1718852Svi117747 			 * addresses if there is an INADDR_ANY. However,
1719852Svi117747 			 * if we are beyond LISTEN, then this is invalid
1720852Svi117747 			 * (see sctp_valid_addr_list(). So, we just fail
1721852Svi117747 			 * it here rather than wait till it fails in
1722852Svi117747 			 * sctp_valid_addr_list().
1723852Svi117747 			 */
1724852Svi117747 			if (s4->sin_addr.s_addr == INADDR_ANY) {
1725852Svi117747 				kmem_free(*addrlist, *size);
1726852Svi117747 				*addrlist = NULL;
1727852Svi117747 				*size = 0;
1728852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1729852Svi117747 					*addrcnt = 0;
1730852Svi117747 					return (EINVAL);
1731852Svi117747 				}
1732852Svi117747 				if (uspec != NULL)
1733852Svi117747 					*uspec = 1;
1734852Svi117747 				goto get_all_addrs;
1735852Svi117747 			} else {
1736852Svi117747 				bcopy(s4, p, sizeof (*s4));
1737852Svi117747 				p += sizeof (*s4);
1738852Svi117747 			}
1739852Svi117747 		}
1740852Svi117747 	} else {
1741852Svi117747 		*size = sizeof (struct sockaddr_in6) * *addrcnt;
1742852Svi117747 		*addrlist = kmem_zalloc(*size, KM_SLEEP);
1743852Svi117747 		p = *addrlist;
1744852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1745852Svi117747 			s6 = (struct sockaddr_in6 *)addrs + cnt;
1746852Svi117747 			/*
1747852Svi117747 			 * Comments for INADDR_ANY, above, apply here too.
1748852Svi117747 			 */
1749852Svi117747 			if (IN6_IS_ADDR_UNSPECIFIED(&s6->sin6_addr)) {
1750852Svi117747 				kmem_free(*addrlist, *size);
1751852Svi117747 				*size = 0;
1752852Svi117747 				*addrlist = NULL;
1753852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1754852Svi117747 					*addrcnt = 0;
1755852Svi117747 					return (EINVAL);
1756852Svi117747 				}
1757852Svi117747 				if (uspec != NULL)
1758852Svi117747 					*uspec = 1;
1759852Svi117747 				goto get_all_addrs;
1760852Svi117747 			} else {
1761852Svi117747 				bcopy(addrs, p, sizeof (*s6));
1762852Svi117747 				p += sizeof (*s6);
1763852Svi117747 			}
1764852Svi117747 		}
1765852Svi117747 	}
1766852Svi117747 	return (err);
1767852Svi117747 get_all_addrs:
1768852Svi117747 
1769852Svi117747 	/*
1770852Svi117747 	 * Allocate max possible size. We allocate the max. size here because
1771852Svi117747 	 * the clustering module could end up adding addresses to the list.
1772852Svi117747 	 * We allocate upfront so that the clustering module need to bother
1773852Svi117747 	 * re-sizing the list.
1774852Svi117747 	 */
17753448Sdh155122 	if (sctp->sctp_family == AF_INET) {
17763448Sdh155122 		*size = sizeof (struct sockaddr_in) *
17773448Sdh155122 		    sctps->sctps_g_ipifs_count;
17783448Sdh155122 	} else {
17793448Sdh155122 		*size = sizeof (struct sockaddr_in6) *
17803448Sdh155122 		    sctps->sctps_g_ipifs_count;
17813448Sdh155122 	}
1782852Svi117747 	*addrlist = kmem_zalloc(*size, KM_SLEEP);
1783852Svi117747 	*addrcnt = 0;
1784852Svi117747 	p = *addrlist;
17853448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
1786852Svi117747 
1787852Svi117747 	/*
1788852Svi117747 	 * Walk through the global interface list and add all addresses,
1789852Svi117747 	 * except those that are hosted on loopback interfaces.
1790852Svi117747 	 */
1791852Svi117747 	for (cnt = 0; cnt <  SCTP_IPIF_HASH; cnt++) {
17923448Sdh155122 		if (sctps->sctps_g_ipifs[cnt].ipif_count == 0)
1793852Svi117747 			continue;
17943448Sdh155122 		sctp_ipif = list_head(
17953448Sdh155122 		    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list);
17963448Sdh155122 		for (icnt = 0;
17973448Sdh155122 		    icnt < sctps->sctps_g_ipifs[cnt].ipif_count;
17983448Sdh155122 		    icnt++) {
1799852Svi117747 			in6_addr_t	addr;
1800852Svi117747 
1801852Svi117747 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1802852Svi117747 			addr = sctp_ipif->sctp_ipif_saddr;
1803852Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
1804852Svi117747 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
1805852Svi117747 			    SCTP_IS_IPIF_LOOPBACK(sctp_ipif) ||
1806852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(sctp_ipif) ||
18072263Ssommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
1808852Svi117747 			    (sctp->sctp_ipversion == IPV4_VERSION &&
1809852Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
1810852Svi117747 			    (sctp->sctp_connp->conn_ipv6_v6only &&
1811852Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
1812852Svi117747 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1813852Svi117747 				sctp_ipif = list_next(
18143448Sdh155122 				    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1815852Svi117747 				    sctp_ipif);
1816852Svi117747 				continue;
1817852Svi117747 			}
1818852Svi117747 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1819852Svi117747 			if (sctp->sctp_family == AF_INET) {
1820852Svi117747 				s4 = (struct sockaddr_in *)p;
1821852Svi117747 				IN6_V4MAPPED_TO_INADDR(&addr, &s4->sin_addr);
1822852Svi117747 				s4->sin_family = AF_INET;
1823852Svi117747 				p += sizeof (*s4);
1824852Svi117747 			} else {
1825852Svi117747 				s6 = (struct sockaddr_in6 *)p;
1826852Svi117747 				s6->sin6_addr = addr;
1827852Svi117747 				s6->sin6_family = AF_INET6;
1828852Svi117747 				s6->sin6_scope_id =
1829852Svi117747 				    sctp_ipif->sctp_ipif_ill->sctp_ill_index;
1830852Svi117747 				p += sizeof (*s6);
1831852Svi117747 			}
1832852Svi117747 			(*addrcnt)++;
18333448Sdh155122 			sctp_ipif = list_next(
18343448Sdh155122 			    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1835852Svi117747 			    sctp_ipif);
1836852Svi117747 		}
1837852Svi117747 	}
18383448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
1839852Svi117747 	return (err);
1840852Svi117747 }
1841852Svi117747 
1842852Svi117747 /*
1843852Svi117747  * Get a list of addresses from the source address list. The  caller is
1844852Svi117747  * responsible for allocating sufficient buffer for this.
1845852Svi117747  */
1846852Svi117747 void
1847852Svi117747 sctp_get_saddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1848852Svi117747 {
1849852Svi117747 	int			cnt;
1850852Svi117747 	int			icnt;
1851852Svi117747 	sctp_saddr_ipif_t	*obj;
1852852Svi117747 	int			naddr;
1853852Svi117747 	int			scanned = 0;
1854852Svi117747 
1855852Svi117747 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1856852Svi117747 		if (sctp->sctp_saddrs[cnt].ipif_count == 0)
1857852Svi117747 			continue;
1858852Svi117747 		obj = list_head(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
1859852Svi117747 		naddr = sctp->sctp_saddrs[cnt].ipif_count;
1860852Svi117747 		for (icnt = 0; icnt < naddr; icnt++) {
1861852Svi117747 			sctp_ipif_t	*ipif;
1862852Svi117747 
1863852Svi117747 			if (psize < sizeof (ipif->sctp_ipif_saddr))
1864852Svi117747 				return;
1865852Svi117747 
1866852Svi117747 			scanned++;
1867852Svi117747 			ipif = obj->saddr_ipifp;
1868852Svi117747 			bcopy(&ipif->sctp_ipif_saddr, p,
1869852Svi117747 			    sizeof (ipif->sctp_ipif_saddr));
1870852Svi117747 			p += sizeof (ipif->sctp_ipif_saddr);
1871852Svi117747 			psize -= sizeof (ipif->sctp_ipif_saddr);
1872852Svi117747 			if (scanned >= sctp->sctp_nsaddrs)
1873852Svi117747 				return;
18743448Sdh155122 			obj = list_next(
18753448Sdh155122 			    &sctp->sctp_saddrs[icnt].sctp_ipif_list,
1876852Svi117747 			    obj);
1877852Svi117747 		}
1878852Svi117747 	}
1879852Svi117747 }
1880852Svi117747 
1881852Svi117747 /*
1882852Svi117747  * Get a list of addresses from the remote address list. The  caller is
1883852Svi117747  * responsible for allocating sufficient buffer for this.
1884852Svi117747  */
1885852Svi117747 void
1886852Svi117747 sctp_get_faddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1887852Svi117747 {
1888852Svi117747 	sctp_faddr_t	*fp;
1889852Svi117747 
1890852Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1891852Svi117747 		if (psize < sizeof (fp->faddr))
1892852Svi117747 			return;
1893852Svi117747 		bcopy(&fp->faddr, p, sizeof (fp->faddr));
1894852Svi117747 		p += sizeof (fp->faddr);
1895852Svi117747 		psize -= sizeof (fp->faddr);
1896852Svi117747 	}
1897852Svi117747 }
18980Sstevel@tonic-gate 
18993448Sdh155122 static void
19003448Sdh155122 sctp_free_ills(sctp_stack_t *sctps)
19013448Sdh155122 {
19023448Sdh155122 	int			i;
19033448Sdh155122 	int			l;
19043448Sdh155122 	sctp_ill_t	*sctp_ill;
19053448Sdh155122 
19063448Sdh155122 	if (sctps->sctps_ills_count == 0)
19073448Sdh155122 		return;
19083448Sdh155122 
19093448Sdh155122 	for (i = 0; i < SCTP_ILL_HASH; i++) {
19103448Sdh155122 		sctp_ill = list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
19113448Sdh155122 		for (l = 0; l < sctps->sctps_g_ills[i].ill_count; l++) {
19123448Sdh155122 			ASSERT(sctp_ill->sctp_ill_ipifcnt == 0);
19133448Sdh155122 			list_remove(&sctps->sctps_g_ills[i].sctp_ill_list,
19143448Sdh155122 			    sctp_ill);
19153448Sdh155122 			sctps->sctps_ills_count--;
19163448Sdh155122 			kmem_free(sctp_ill->sctp_ill_name,
19173448Sdh155122 			    sctp_ill->sctp_ill_name_length);
19183448Sdh155122 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
19193448Sdh155122 			sctp_ill =
19203448Sdh155122 			    list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
19213448Sdh155122 		}
19223448Sdh155122 		sctps->sctps_g_ills[i].ill_count = 0;
19233448Sdh155122 	}
19243448Sdh155122 	ASSERT(sctps->sctps_ills_count == 0);
19253448Sdh155122 }
19263448Sdh155122 
19273448Sdh155122 static void
19283448Sdh155122 sctp_free_ipifs(sctp_stack_t *sctps)
19293448Sdh155122 {
19303448Sdh155122 	int			i;
19313448Sdh155122 	int			l;
19323448Sdh155122 	sctp_ipif_t	*sctp_ipif;
19333448Sdh155122 	sctp_ill_t	*sctp_ill;
19343448Sdh155122 
19353448Sdh155122 	if (sctps->sctps_g_ipifs_count == 0)
19363448Sdh155122 		return;
19373448Sdh155122 
19383448Sdh155122 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
19393448Sdh155122 		sctp_ipif = list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
19403448Sdh155122 		for (l = 0; l < sctps->sctps_g_ipifs[i].ipif_count; l++) {
19413448Sdh155122 			sctp_ill = sctp_ipif->sctp_ipif_ill;
19423448Sdh155122 
19433448Sdh155122 			list_remove(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
19443448Sdh155122 			    sctp_ipif);
19453448Sdh155122 			sctps->sctps_g_ipifs_count--;
19463448Sdh155122 			(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt,
19473448Sdh155122 			    -1);
19483448Sdh155122 			kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
19493448Sdh155122 			sctp_ipif =
19503448Sdh155122 			    list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
19513448Sdh155122 		}
19523448Sdh155122 		sctps->sctps_g_ipifs[i].ipif_count = 0;
19533448Sdh155122 	}
19543448Sdh155122 	ASSERT(sctps->sctps_g_ipifs_count == 0);
19553448Sdh155122 }
19563448Sdh155122 
19573448Sdh155122 
19580Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
19590Sstevel@tonic-gate void
19603448Sdh155122 sctp_saddr_init(sctp_stack_t *sctps)
19610Sstevel@tonic-gate {
19620Sstevel@tonic-gate 	int	i;
19630Sstevel@tonic-gate 
19643448Sdh155122 	sctps->sctps_g_ills = kmem_zalloc(sizeof (sctp_ill_hash_t) *
19653448Sdh155122 	    SCTP_ILL_HASH, KM_SLEEP);
19663448Sdh155122 	sctps->sctps_g_ipifs = kmem_zalloc(sizeof (sctp_ipif_hash_t) *
19673448Sdh155122 	    SCTP_IPIF_HASH, KM_SLEEP);
19683448Sdh155122 
19693448Sdh155122 	rw_init(&sctps->sctps_g_ills_lock, NULL, RW_DEFAULT, NULL);
19703448Sdh155122 	rw_init(&sctps->sctps_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
19733448Sdh155122 		sctps->sctps_g_ills[i].ill_count = 0;
19743448Sdh155122 		list_create(&sctps->sctps_g_ills[i].sctp_ill_list,
19753448Sdh155122 		    sizeof (sctp_ill_t),
19760Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
19770Sstevel@tonic-gate 	}
19780Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
19793448Sdh155122 		sctps->sctps_g_ipifs[i].ipif_count = 0;
19803448Sdh155122 		list_create(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
19810Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
19820Sstevel@tonic-gate 	}
19830Sstevel@tonic-gate }
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate void
19863448Sdh155122 sctp_saddr_fini(sctp_stack_t *sctps)
19870Sstevel@tonic-gate {
19880Sstevel@tonic-gate 	int	i;
19890Sstevel@tonic-gate 
19903448Sdh155122 	sctp_free_ipifs(sctps);
19913448Sdh155122 	sctp_free_ills(sctps);
19923448Sdh155122 
19930Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
19943448Sdh155122 		list_destroy(&sctps->sctps_g_ills[i].sctp_ill_list);
19950Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
19963448Sdh155122 		list_destroy(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
19973448Sdh155122 
19983448Sdh155122 	ASSERT(sctps->sctps_ills_count == 0 && sctps->sctps_g_ipifs_count == 0);
19993448Sdh155122 	kmem_free(sctps->sctps_g_ills, sizeof (sctp_ill_hash_t) *
20003448Sdh155122 	    SCTP_ILL_HASH);
20013448Sdh155122 	sctps->sctps_g_ills = NULL;
20023448Sdh155122 	kmem_free(sctps->sctps_g_ipifs, sizeof (sctp_ipif_hash_t) *
20033448Sdh155122 	    SCTP_IPIF_HASH);
20043448Sdh155122 	sctps->sctps_g_ipifs = NULL;
20053448Sdh155122 	rw_destroy(&sctps->sctps_g_ills_lock);
20063448Sdh155122 	rw_destroy(&sctps->sctps_g_ipifs_lock);
20070Sstevel@tonic-gate }
2008