xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_addr.c (revision 3448:aaf16568054b)
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 /*
22*3448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/ddi.h>
320Sstevel@tonic-gate #include <sys/sunddi.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/list.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <netinet/in.h>
390Sstevel@tonic-gate #include <netinet/ip6.h>
400Sstevel@tonic-gate #include <netinet/sctp.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <inet/common.h>
430Sstevel@tonic-gate #include <inet/ip.h>
440Sstevel@tonic-gate #include <inet/ip6.h>
450Sstevel@tonic-gate #include <inet/ip_if.h>
460Sstevel@tonic-gate #include <inet/ipclassifier.h>
470Sstevel@tonic-gate #include <inet/sctp_ip.h>
480Sstevel@tonic-gate #include "sctp_impl.h"
490Sstevel@tonic-gate #include "sctp_addr.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
520Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
532263Ssommerfe 			    sctp_t *, uint_t);
540Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
55852Svi117747 int			sctp_valid_addr_list(sctp_t *, const void *, uint32_t,
56852Svi117747 			    uchar_t *, size_t);
570Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_ipif_lookup(sctp_t *, uint_t);
58432Svi117747 static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
59432Svi117747 			    boolean_t dontsrc);
600Sstevel@tonic-gate static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
610Sstevel@tonic-gate static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
620Sstevel@tonic-gate 			    sctp_ipif_hash_t *);
630Sstevel@tonic-gate int			sctp_compare_saddrs(sctp_t *, sctp_t *);
640Sstevel@tonic-gate static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
650Sstevel@tonic-gate int			sctp_dup_saddrs(sctp_t *, sctp_t *, int);
660Sstevel@tonic-gate void			sctp_free_saddrs(sctp_t *);
670Sstevel@tonic-gate void			sctp_update_ill(ill_t *, int);
680Sstevel@tonic-gate void			sctp_update_ipif(ipif_t *, int);
690Sstevel@tonic-gate void			sctp_move_ipif(ipif_t *, ill_t *, ill_t *);
700Sstevel@tonic-gate void			sctp_del_saddr(sctp_t *, sctp_saddr_ipif_t *);
710Sstevel@tonic-gate void			sctp_del_saddr_list(sctp_t *, const void *, int,
720Sstevel@tonic-gate 			    boolean_t);
73852Svi117747 sctp_saddr_ipif_t	*sctp_saddr_lookup(sctp_t *, in6_addr_t *, uint_t);
740Sstevel@tonic-gate in6_addr_t		sctp_get_valid_addr(sctp_t *, boolean_t);
750Sstevel@tonic-gate int			sctp_getmyaddrs(void *, void *, int *);
76*3448Sdh155122 void			sctp_saddr_init(sctp_stack_t *);
77*3448Sdh155122 void			sctp_saddr_fini(sctp_stack_t *);
78432Svi117747 
790Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
800Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
81432Svi117747 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
82432Svi117747 
83432Svi117747 #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
84432Svi117747 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
85432Svi117747 
86852Svi117747 #define	SCTP_IS_IPIF_LOOPBACK(ipif)		\
87852Svi117747 	((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK)
88852Svi117747 
89852Svi117747 #define	SCTP_IS_IPIF_LINKLOCAL(ipif)		\
90852Svi117747 	((ipif)->sctp_ipif_isv6 && 		\
91852Svi117747 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr))
92432Svi117747 
93432Svi117747 #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
94432Svi117747 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
95432Svi117747 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
960Sstevel@tonic-gate 
972263Ssommerfe #define	SCTP_IPIF_ZONE_MATCH(sctp, ipif) 				\
982263Ssommerfe 	IPCL_ZONE_MATCH((sctp)->sctp_connp, (ipif)->sctp_ipif_zoneid)
992263Ssommerfe 
1000Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
1010Sstevel@tonic-gate #define	SCTP_IPIF_HASH_FN(seqid)	((seqid) % SCTP_IPIF_HASH)
1020Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  *
1070Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1140Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1150Sstevel@tonic-gate  * Called with no locks held.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate static void
1180Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
1210Sstevel@tonic-gate 	uint_t		ipif_index;
1220Sstevel@tonic-gate 	uint_t		ill_index;
123*3448Sdh155122 	sctp_stack_t	*sctps = sctp_ipif->sctp_ipif_ill->
124*3448Sdh155122 	    sctp_ill_netstack->netstack_sctp;
1250Sstevel@tonic-gate 
126*3448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
127*3448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(sctp_ipif->sctp_ipif_id);
1300Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1310Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1320Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1330Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1340Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
135*3448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
136*3448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
1370Sstevel@tonic-gate 		return;
1380Sstevel@tonic-gate 	}
139*3448Sdh155122 	list_remove(&sctps->sctps_g_ipifs[ipif_index].sctp_ipif_list,
140*3448Sdh155122 	    sctp_ipif);
141*3448Sdh155122 	sctps->sctps_g_ipifs[ipif_index].ipif_count--;
142*3448Sdh155122 	sctps->sctps_g_ipifs_count--;
1430Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1440Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
147*3448Sdh155122 	if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
148*3448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1490Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1500Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
151*3448Sdh155122 			list_remove(&sctps->sctps_g_ills[ill_index].
152*3448Sdh155122 			    sctp_ill_list, (void *)sctp_ill);
153*3448Sdh155122 			sctps->sctps_g_ills[ill_index].ill_count--;
154*3448Sdh155122 			sctps->sctps_ills_count--;
1550Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1560Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1570Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 	}
160*3448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
161*3448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
1660Sstevel@tonic-gate  * Called with no locks held.
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate static sctp_ipif_t *
1692263Ssommerfe sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, sctp_t *sctp,
170852Svi117747     uint_t ifindex)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	int		i;
1730Sstevel@tonic-gate 	int		j;
1740Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
175*3448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1760Sstevel@tonic-gate 
1772263Ssommerfe 	ASSERT(sctp->sctp_zoneid != ALL_ZONES);
178*3448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
1790Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
180*3448Sdh155122 		if (sctps->sctps_g_ipifs[i].ipif_count == 0)
1810Sstevel@tonic-gate 			continue;
182*3448Sdh155122 		sctp_ipif = list_head(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
183*3448Sdh155122 		for (j = 0; j < sctps->sctps_g_ipifs[i].ipif_count; j++) {
1840Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1852263Ssommerfe 			if (SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) &&
1860Sstevel@tonic-gate 			    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) &&
187852Svi117747 			    (ifindex == 0 || ifindex ==
188852Svi117747 			    sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
1890Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1900Sstevel@tonic-gate 			    addr)) {
1910Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1920Sstevel@tonic-gate 				if (refhold)
1930Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
194*3448Sdh155122 				rw_exit(&sctps->sctps_g_ipifs_lock);
1950Sstevel@tonic-gate 				return (sctp_ipif);
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
198*3448Sdh155122 			sctp_ipif = list_next(
199*3448Sdh155122 			    &sctps->sctps_g_ipifs[i].sctp_ipif_list, sctp_ipif);
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 	}
202*3448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2030Sstevel@tonic-gate 	return (NULL);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
2080Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
2090Sstevel@tonic-gate  * Called with no locks held.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static int
2120Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
2150Sstevel@tonic-gate 	int			i;
2160Sstevel@tonic-gate 	int			j;
2170Sstevel@tonic-gate 	int			error = 0;
218*3448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2190Sstevel@tonic-gate 
220*3448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
2210Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
222*3448Sdh155122 		if (sctps->sctps_g_ipifs[i].ipif_count == 0)
2230Sstevel@tonic-gate 			continue;
224*3448Sdh155122 		sctp_ipif = list_head(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
225*3448Sdh155122 		for (j = 0; j < sctps->sctps_g_ipifs[i].ipif_count; j++) {
2260Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
227432Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
2280Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
2292263Ssommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
2300Sstevel@tonic-gate 			    (sctp->sctp_ipversion == IPV4_VERSION &&
231432Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
2320Sstevel@tonic-gate 			    (sctp->sctp_connp->conn_ipv6_v6only &&
233432Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
2340Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2350Sstevel@tonic-gate 				sctp_ipif = list_next(
236*3448Sdh155122 				    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
237*3448Sdh155122 				    sctp_ipif);
2380Sstevel@tonic-gate 				continue;
2390Sstevel@tonic-gate 			}
2400Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2410Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
242432Svi117747 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
243432Svi117747 			    B_FALSE);
2440Sstevel@tonic-gate 			if (error != 0)
2450Sstevel@tonic-gate 				goto free_stuff;
246*3448Sdh155122 			sctp_ipif = list_next(
247*3448Sdh155122 			    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
2480Sstevel@tonic-gate 			    sctp_ipif);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 	}
251*3448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2520Sstevel@tonic-gate 	return (0);
2530Sstevel@tonic-gate free_stuff:
254*3448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2550Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2560Sstevel@tonic-gate 	return (ENOMEM);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2610Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
262852Svi117747  * or error. If the caller wants the list of addresses, it sends a pre-allocated
263852Svi117747  * buffer - list. Currently, this list is only used on a clustered node when
264852Svi117747  * the SCTP is in the listen state (from sctp_bind_add()). When called on a
265852Svi117747  * clustered node, the input is always a list of addresses (even if the
266852Svi117747  * original bind() was to INADDR_ANY).
2670Sstevel@tonic-gate  * Called with no locks held.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate int
270852Svi117747 sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
271852Svi117747     uchar_t *list, size_t lsize)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2740Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2750Sstevel@tonic-gate 	struct in_addr		*addr4;
2760Sstevel@tonic-gate 	in6_addr_t		addr;
2770Sstevel@tonic-gate 	int			cnt;
2780Sstevel@tonic-gate 	int			err = 0;
2790Sstevel@tonic-gate 	int			saddr_cnt = 0;
2800Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
2810Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
2820Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
2830Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
284852Svi117747 	uchar_t			*p = list;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
2880Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
2890Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
2900Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
2910Sstevel@tonic-gate 	 * addresses.
2920Sstevel@tonic-gate 	 */
2930Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
2940Sstevel@tonic-gate 		check_lport = B_TRUE;
2950Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
2960Sstevel@tonic-gate 			check_addrs = B_TRUE;
2970Sstevel@tonic-gate 	}
298852Svi117747 
2990Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3000Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
3010Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3020Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
3030Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
3040Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
305852Svi117747 		uint_t		ifindex = 0;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		switch (sctp->sctp_family) {
3080Sstevel@tonic-gate 		case AF_INET:
3090Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
3100Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
3110Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
3120Sstevel@tonic-gate 				err = EINVAL;
3130Sstevel@tonic-gate 				goto free_ret;
3140Sstevel@tonic-gate 			}
3150Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
3160Sstevel@tonic-gate 			if (check_addrs &&
3170Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
3180Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
3190Sstevel@tonic-gate 			    IN_MULTICAST(addr4->s_addr))) {
3200Sstevel@tonic-gate 				err = EINVAL;
3210Sstevel@tonic-gate 				goto free_ret;
3220Sstevel@tonic-gate 			}
3230Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
3240Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
3250Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3260Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3270Sstevel@tonic-gate 			}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 			break;
3300Sstevel@tonic-gate 		case AF_INET6:
3310Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3320Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
3330Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
3340Sstevel@tonic-gate 				err = EINVAL;
3350Sstevel@tonic-gate 				goto free_ret;
3360Sstevel@tonic-gate 			}
3370Sstevel@tonic-gate 			addr = sin6->sin6_addr;
338852Svi117747 			/* Contains the interface index */
339852Svi117747 			ifindex = sin6->sin6_scope_id;
3400Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
3410Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3420Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3430Sstevel@tonic-gate 				goto free_ret;
3440Sstevel@tonic-gate 			}
3450Sstevel@tonic-gate 			if (check_addrs &&
3460Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3470Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3480Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3490Sstevel@tonic-gate 				err = EINVAL;
3500Sstevel@tonic-gate 				goto free_ret;
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3530Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3540Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3550Sstevel@tonic-gate 			}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 			break;
3580Sstevel@tonic-gate 		default:
3590Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3600Sstevel@tonic-gate 			goto free_ret;
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 		if (lookup_saddr) {
3632263Ssommerfe 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE, sctp,
3642263Ssommerfe 			    ifindex);
3650Sstevel@tonic-gate 			if (ipif == NULL) {
3660Sstevel@tonic-gate 				/* Address not in the list */
3670Sstevel@tonic-gate 				err = EINVAL;
3680Sstevel@tonic-gate 				goto free_ret;
369852Svi117747 			} else if (check_addrs && SCTP_IS_IPIF_LOOPBACK(ipif) &&
370852Svi117747 			    cl_sctp_check_addrs == NULL) {
3710Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3720Sstevel@tonic-gate 				err = EINVAL;
3730Sstevel@tonic-gate 				goto free_ret;
3740Sstevel@tonic-gate 			}
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		if (!bind_to_all) {
377432Svi117747 			/*
378432Svi117747 			 * If an address is added after association setup,
379432Svi117747 			 * we need to wait for the peer to send us an ASCONF
380432Svi117747 			 * ACK before we can start using it.
381432Svi117747 			 * saddr_ipif_dontsrc will be reset (to 0) when we
382432Svi117747 			 * get the ASCONF ACK for this address.
383432Svi117747 			 */
384432Svi117747 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
385432Svi117747 			    check_addrs ? B_TRUE : B_FALSE);
3860Sstevel@tonic-gate 			if (err != 0) {
3870Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3880Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
3890Sstevel@tonic-gate 					err = EADDRINUSE;
3900Sstevel@tonic-gate 				goto free_ret;
3910Sstevel@tonic-gate 			}
3920Sstevel@tonic-gate 			saddr_cnt++;
393852Svi117747 			if (lsize >= sizeof (addr)) {
394852Svi117747 				bcopy(&addr, p, sizeof (addr));
395852Svi117747 				p += sizeof (addr);
396852Svi117747 				lsize -= sizeof (addr);
397852Svi117747 			}
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	if (bind_to_all) {
4010Sstevel@tonic-gate 		/*
4020Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
4030Sstevel@tonic-gate 		 * inaddr_any.
4040Sstevel@tonic-gate 		 */
4050Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
4060Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
4070Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
4100Sstevel@tonic-gate 		if (err != 0)
4110Sstevel@tonic-gate 			return (err);
4120Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4150Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4160Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4170Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4180Sstevel@tonic-gate 	return (0);
4190Sstevel@tonic-gate free_ret:
4200Sstevel@tonic-gate 	if (saddr_cnt != 0)
4210Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
4220Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4230Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4240Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4250Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4260Sstevel@tonic-gate 	return (err);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate sctp_saddr_ipif_t *
4300Sstevel@tonic-gate sctp_ipif_lookup(sctp_t *sctp, uint_t ipif_index)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	int			cnt;
4330Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif_index);
4340Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (sctp->sctp_saddrs[seqid].ipif_count == 0)
4370Sstevel@tonic-gate 		return (NULL);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4400Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4410Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif_index)
4420Sstevel@tonic-gate 			return (ipif_obj);
4430Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4440Sstevel@tonic-gate 		    ipif_obj);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	return (NULL);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate static int
450432Svi117747 sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
451432Svi117747     boolean_t dontsrc)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	int			cnt;
4540Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4550Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4580Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4590Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id)
4600Sstevel@tonic-gate 			return (EALREADY);
4610Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4620Sstevel@tonic-gate 		    ipif_obj);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4650Sstevel@tonic-gate 	if (ipif_obj == NULL) {
4660Sstevel@tonic-gate 		/* Need to do something */
4670Sstevel@tonic-gate 		return (ENOMEM);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
470432Svi117747 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
4710Sstevel@tonic-gate 	list_insert_tail(&sctp->sctp_saddrs[seqid].sctp_ipif_list, ipif_obj);
4720Sstevel@tonic-gate 	sctp->sctp_saddrs[seqid].ipif_count++;
4730Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
4740Sstevel@tonic-gate 	return (0);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate static void
4780Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	int			cnt;
4810Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
4820Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
4850Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
4860Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) {
4870Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4880Sstevel@tonic-gate 			    ipif_obj);
4890Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
4900Sstevel@tonic-gate 			sctp->sctp_saddrs[seqid].ipif_count--;
4910Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
4920Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
4930Sstevel@tonic-gate 			break;
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
4960Sstevel@tonic-gate 		    ipif_obj);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate static int
5010Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate 	int			i;
5040Sstevel@tonic-gate 	int			j;
5050Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
5060Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
5070Sstevel@tonic-gate 	int			overlap = 0;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
5100Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5110Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
5120Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
5130Sstevel@tonic-gate 			if (obj1->saddr_ipifp->sctp_ipif_id ==
5140Sstevel@tonic-gate 			    obj2->saddr_ipifp->sctp_ipif_id) {
5150Sstevel@tonic-gate 				overlap++;
5160Sstevel@tonic-gate 				break;
5170Sstevel@tonic-gate 			}
5180Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
5190Sstevel@tonic-gate 			    obj2);
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 	return (overlap);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate int
5270Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	int		i;
5300Sstevel@tonic-gate 	int		overlap = 0;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5330Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
5340Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5380Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5390Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
5430Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (overlap > 0)
5460Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate static int
5520Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	int			i;
5550Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5560Sstevel@tonic-gate 	int			error = 0;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
5590Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5600Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
561432Svi117747 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
562432Svi117747 		    B_FALSE);
5630Sstevel@tonic-gate 		if (error != 0)
5640Sstevel@tonic-gate 			return (error);
5650Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 	return (error);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate int
5710Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	int	error = 0;
5740Sstevel@tonic-gate 	int	i;
5750Sstevel@tonic-gate 
576432Svi117747 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
5770Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5800Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
5810Sstevel@tonic-gate 			continue;
5820Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
5830Sstevel@tonic-gate 		if (error != 0) {
5840Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
5850Sstevel@tonic-gate 			return (error);
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 	return (0);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate void
5920Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 	int			i;
5950Sstevel@tonic-gate 	int			l;
5960Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
5990Sstevel@tonic-gate 		return;
6000Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
6010Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
6020Sstevel@tonic-gate 			continue;
6030Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6040Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
6050Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
6060Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
6070Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
6080Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
6090Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
6120Sstevel@tonic-gate 	}
613432Svi117747 	if (sctp->sctp_bound_to_all == 1)
614432Svi117747 		sctp->sctp_bound_to_all = 0;
6150Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate /*
6190Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
6200Sstevel@tonic-gate  * held.
6210Sstevel@tonic-gate  */
6220Sstevel@tonic-gate void
6230Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
6240Sstevel@tonic-gate {
6250Sstevel@tonic-gate 	int		i;
6260Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
6270Sstevel@tonic-gate 	uint_t		index;
628*3448Sdh155122 	netstack_t	*ns = ill->ill_ipst->ips_netstack;
629*3448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	ip2dbg(("sctp_update_ill: %s\n", ill->ill_name));
6320Sstevel@tonic-gate 
633*3448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_WRITER);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
636*3448Sdh155122 	sctp_ill = list_head(&sctps->sctps_g_ills[index].sctp_ill_list);
637*3448Sdh155122 	for (i = 0; i < sctps->sctps_g_ills[index].ill_count; i++) {
6380Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
6390Sstevel@tonic-gate 			break;
640*3448Sdh155122 		sctp_ill = list_next(&sctps->sctps_g_ills[index].sctp_ill_list,
6410Sstevel@tonic-gate 		    sctp_ill);
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	switch (op) {
6450Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
6460Sstevel@tonic-gate 		if (sctp_ill != NULL) {
6470Sstevel@tonic-gate 			/* Unmark it if it is condemned */
6480Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
6490Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
650*3448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
6510Sstevel@tonic-gate 			return;
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
6540Sstevel@tonic-gate 		/* Need to re-try? */
6550Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6560Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
657*3448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
6580Sstevel@tonic-gate 			return;
6590Sstevel@tonic-gate 		}
6600Sstevel@tonic-gate 		sctp_ill->sctp_ill_name =
6610Sstevel@tonic-gate 		    kmem_zalloc(ill->ill_name_length, KM_NOSLEEP);
6620Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
6630Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6640Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
665*3448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
6660Sstevel@tonic-gate 			return;
6670Sstevel@tonic-gate 		}
6680Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
6690Sstevel@tonic-gate 		    ill->ill_name_length);
6700Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
6710Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
6720Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
673*3448Sdh155122 		sctp_ill->sctp_ill_netstack = ns;	/* No netstack_hold */
674*3448Sdh155122 		list_insert_tail(&sctps->sctps_g_ills[index].sctp_ill_list,
6750Sstevel@tonic-gate 		    (void *)sctp_ill);
676*3448Sdh155122 		sctps->sctps_g_ills[index].ill_count++;
677*3448Sdh155122 		sctps->sctps_ills_count++;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		break;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		if (sctp_ill == NULL) {
684*3448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
6850Sstevel@tonic-gate 			return;
6860Sstevel@tonic-gate 		}
6870Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
688*3448Sdh155122 			list_remove(&sctps->sctps_g_ills[index].sctp_ill_list,
6890Sstevel@tonic-gate 			    (void *)sctp_ill);
690*3448Sdh155122 			sctps->sctps_g_ills[index].ill_count--;
691*3448Sdh155122 			sctps->sctps_ills_count--;
6920Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
6930Sstevel@tonic-gate 			    ill->ill_name_length);
6940Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
6950Sstevel@tonic-gate 		} else {
6960Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
6970Sstevel@tonic-gate 		}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		break;
7000Sstevel@tonic-gate 	}
701*3448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
7050Sstevel@tonic-gate void
7060Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
7090Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
7100Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7110Sstevel@tonic-gate 	uint_t		index;
7120Sstevel@tonic-gate 	int		i;
713*3448Sdh155122 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
714*3448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
7150Sstevel@tonic-gate 
716*3448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
717*3448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
720*3448Sdh155122 	fsctp_ill = list_head(&sctps->sctps_g_ills[index].sctp_ill_list);
721*3448Sdh155122 	for (i = 0; i < sctps->sctps_g_ills[index].ill_count; i++) {
7220Sstevel@tonic-gate 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill))
7230Sstevel@tonic-gate 			break;
724*3448Sdh155122 		fsctp_ill = list_next(&sctps->sctps_g_ills[index].sctp_ill_list,
7250Sstevel@tonic-gate 		    fsctp_ill);
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
729*3448Sdh155122 	tsctp_ill = list_head(&sctps->sctps_g_ills[index].sctp_ill_list);
730*3448Sdh155122 	for (i = 0; i < sctps->sctps_g_ills[index].ill_count; i++) {
7310Sstevel@tonic-gate 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill))
7320Sstevel@tonic-gate 			break;
733*3448Sdh155122 		tsctp_ill = list_next(&sctps->sctps_g_ills[index].sctp_ill_list,
7340Sstevel@tonic-gate 		    tsctp_ill);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
738*3448Sdh155122 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[index].sctp_ipif_list);
739*3448Sdh155122 	for (i = 0; i < sctps->sctps_g_ipifs[index].ipif_count; i++) {
7400Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7410Sstevel@tonic-gate 			break;
742*3448Sdh155122 		sctp_ipif = list_next(
743*3448Sdh155122 		    &sctps->sctps_g_ipifs[index].sctp_ipif_list, sctp_ipif);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	/* Should be an ASSERT? */
7460Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
7470Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
7480Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
749*3448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
750*3448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
7510Sstevel@tonic-gate 		return;
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
7540Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
7550Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
7560Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
7570Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
7580Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
759*3448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
760*3448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
7640Sstevel@tonic-gate void
7650Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
7680Sstevel@tonic-gate 	int		i;
7690Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
7700Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
7710Sstevel@tonic-gate 	uint_t		ill_index;
7720Sstevel@tonic-gate 	uint_t		ipif_index;
773*3448Sdh155122 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
774*3448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
7770Sstevel@tonic-gate 
778*3448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
779*3448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
782*3448Sdh155122 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
783*3448Sdh155122 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
7840Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
7850Sstevel@tonic-gate 			break;
786*3448Sdh155122 		sctp_ill = list_next(
787*3448Sdh155122 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	if (sctp_ill == NULL) {
790*3448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
791*3448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
7920Sstevel@tonic-gate 		return;
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
796*3448Sdh155122 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[ipif_index].sctp_ipif_list);
797*3448Sdh155122 	for (i = 0; i < sctps->sctps_g_ipifs[ipif_index].ipif_count; i++) {
7980Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7990Sstevel@tonic-gate 			break;
800*3448Sdh155122 		sctp_ipif = list_next(
801*3448Sdh155122 		    &sctps->sctps_g_ipifs[ipif_index].sctp_ipif_list,
8020Sstevel@tonic-gate 		    sctp_ipif);
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate 	if (op != SCTP_IPIF_INSERT && sctp_ipif == NULL) {
8050Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
806*3448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
807*3448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
8080Sstevel@tonic-gate 		return;
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate #ifdef	DEBUG
8110Sstevel@tonic-gate 	if (sctp_ipif != NULL)
8120Sstevel@tonic-gate 		ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
8130Sstevel@tonic-gate #endif
8140Sstevel@tonic-gate 	switch (op) {
8150Sstevel@tonic-gate 	case SCTP_IPIF_INSERT:
8160Sstevel@tonic-gate 		if (sctp_ipif != NULL) {
8170Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED)
818252Svi117747 				sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
819*3448Sdh155122 			rw_exit(&sctps->sctps_g_ipifs_lock);
820*3448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
8210Sstevel@tonic-gate 			return;
8220Sstevel@tonic-gate 		}
8230Sstevel@tonic-gate 		sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
8240Sstevel@tonic-gate 		/* Try again? */
8250Sstevel@tonic-gate 		if (sctp_ipif == NULL) {
8260Sstevel@tonic-gate 			ip1dbg(("sctp_ipif_insert: mem failure..\n"));
827*3448Sdh155122 			rw_exit(&sctps->sctps_g_ipifs_lock);
828*3448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
8290Sstevel@tonic-gate 			return;
8300Sstevel@tonic-gate 		}
8310Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
8320Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_ill = sctp_ill;
833252Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID;
8340Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8350Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
8360Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
837432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
8380Sstevel@tonic-gate 		rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
839*3448Sdh155122 		list_insert_tail(
840*3448Sdh155122 		    &sctps->sctps_g_ipifs[ipif_index].sctp_ipif_list,
8410Sstevel@tonic-gate 		    (void *)sctp_ipif);
842*3448Sdh155122 		sctps->sctps_g_ipifs[ipif_index].ipif_count++;
843*3448Sdh155122 		sctps->sctps_g_ipifs_count++;
8440Sstevel@tonic-gate 		atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		break;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
8490Sstevel@tonic-gate 	{
8500Sstevel@tonic-gate 		list_t		*ipif_list;
8510Sstevel@tonic-gate 		list_t		*ill_list;
8520Sstevel@tonic-gate 
853*3448Sdh155122 		ill_list = &sctps->sctps_g_ills[ill_index].sctp_ill_list;
854*3448Sdh155122 		ipif_list = &sctps->sctps_g_ipifs[ipif_index].sctp_ipif_list;
8550Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
8560Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
857*3448Sdh155122 			rw_exit(&sctps->sctps_g_ipifs_lock);
858*3448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
8590Sstevel@tonic-gate 			return;
8600Sstevel@tonic-gate 		}
8610Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
862*3448Sdh155122 		sctps->sctps_g_ipifs[ipif_index].ipif_count--;
863*3448Sdh155122 		sctps->sctps_g_ipifs_count--;
8640Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
8650Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
8660Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
867*3448Sdh155122 		if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
868*3448Sdh155122 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
8690Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
8700Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
8710Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
872*3448Sdh155122 				sctps->sctps_ills_count--;
873*3448Sdh155122 				sctps->sctps_g_ills[ill_index].ill_count--;
8740Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
8750Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
8760Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
8770Sstevel@tonic-gate 			}
8780Sstevel@tonic-gate 		}
8790Sstevel@tonic-gate 		break;
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	case SCTP_IPIF_UP:
8830Sstevel@tonic-gate 
884*3448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
8850Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8860Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
8870Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
888432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
889432Svi117747 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8900Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 		break;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
8950Sstevel@tonic-gate 
896*3448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
8970Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
8980Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
8990Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
9000Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
901432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
9020Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 		break;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
9070Sstevel@tonic-gate 
908*3448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
9090Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
9100Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
9110Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		break;
9140Sstevel@tonic-gate 	}
915*3448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
916*3448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate /*
9200Sstevel@tonic-gate  *
9210Sstevel@tonic-gate  *
9220Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
9230Sstevel@tonic-gate  * sctp locking by the caller.
9240Sstevel@tonic-gate  *
9250Sstevel@tonic-gate  *
9260Sstevel@tonic-gate  */
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate /* Remove a specific saddr from the list */
9290Sstevel@tonic-gate void
9300Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
9310Sstevel@tonic-gate {
9320Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9330Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9360Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
9390Sstevel@tonic-gate 
940432Svi117747 	if (sctp->sctp_bound_to_all == 1)
9410Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
9440Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
9470Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate /*
9510Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
9520Sstevel@tonic-gate  * Called with no locks held.
9530Sstevel@tonic-gate  */
9540Sstevel@tonic-gate void
9550Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
9560Sstevel@tonic-gate     boolean_t fanout_locked)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
9590Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
9600Sstevel@tonic-gate 	int			cnt;
9610Sstevel@tonic-gate 	in6_addr_t		addr;
9620Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
963852Svi117747 	int			ifindex = 0;
9640Sstevel@tonic-gate 
965852Svi117747 	ASSERT(sctp->sctp_nsaddrs >= addcnt);
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	if (!fanout_locked) {
9680Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9690Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
9700Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9710Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
9750Sstevel@tonic-gate 		switch (sctp->sctp_family) {
9760Sstevel@tonic-gate 		case AF_INET:
9770Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
9780Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
9790Sstevel@tonic-gate 			break;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 		case AF_INET6:
9820Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
9830Sstevel@tonic-gate 			addr = sin6->sin6_addr;
984852Svi117747 			ifindex = sin6->sin6_scope_id;
9850Sstevel@tonic-gate 			break;
9860Sstevel@tonic-gate 		}
9872263Ssommerfe 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE, sctp,
9882263Ssommerfe 		    ifindex);
9890Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
9900Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
9910Sstevel@tonic-gate 	}
992432Svi117747 	if (sctp->sctp_bound_to_all == 1)
9930Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	if (!fanout_locked) {
9960Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
9970Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
9980Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
9990Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate /*
10040Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
10050Sstevel@tonic-gate  * Called with no locks held.
10060Sstevel@tonic-gate  */
10070Sstevel@tonic-gate sctp_saddr_ipif_t *
1008852Svi117747 sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate 	sctp_saddr_ipif_t	*saddr_ipifs;
10110Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
10120Sstevel@tonic-gate 
10132263Ssommerfe 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_FALSE, sctp, ifindex);
10140Sstevel@tonic-gate 	if (sctp_ipif == NULL)
10150Sstevel@tonic-gate 		return (NULL);
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	saddr_ipifs = sctp_ipif_lookup(sctp, sctp_ipif->sctp_ipif_id);
10180Sstevel@tonic-gate 	return (saddr_ipifs);
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate 
1021432Svi117747 /* Given an address, add it to the source address list */
1022432Svi117747 int
1023852Svi117747 sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1024432Svi117747 {
1025432Svi117747 	sctp_ipif_t		*sctp_ipif;
1026432Svi117747 
10272263Ssommerfe 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp, ifindex);
1028432Svi117747 	if (sctp_ipif == NULL)
1029432Svi117747 		return (EINVAL);
1030432Svi117747 
1031432Svi117747 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE) != 0) {
1032432Svi117747 		SCTP_IPIF_REFRELE(sctp_ipif);
1033432Svi117747 		return (EINVAL);
1034432Svi117747 	}
1035432Svi117747 	return (0);
1036432Svi117747 }
1037432Svi117747 
1038432Svi117747 /*
1039432Svi117747  * Remove or mark as dontsrc addresses that are currently not part of the
1040432Svi117747  * association. One would delete addresses when processing an INIT and
1041432Svi117747  * mark as dontsrc when processing an INIT-ACK.
1042432Svi117747  */
1043432Svi117747 void
1044432Svi117747 sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete)
1045432Svi117747 {
1046432Svi117747 	int			i;
1047432Svi117747 	int			l;
1048432Svi117747 	sctp_saddr_ipif_t	*obj;
1049432Svi117747 	int			scanned = 0;
1050432Svi117747 	int			naddr;
1051432Svi117747 	int			nsaddr;
1052432Svi117747 
1053432Svi117747 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1054432Svi117747 
1055432Svi117747 	/*
1056432Svi117747 	 * Irregardless of the supported address in the INIT, v4
1057432Svi117747 	 * must be supported.
1058432Svi117747 	 */
1059432Svi117747 	if (sctp->sctp_family == AF_INET)
1060432Svi117747 		supp_af = PARM_SUPP_V4;
1061432Svi117747 
1062432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
1063432Svi117747 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1064432Svi117747 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1065432Svi117747 			continue;
1066432Svi117747 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1067432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1068432Svi117747 		for (l = 0; l < naddr; l++) {
1069432Svi117747 			sctp_ipif_t	*ipif;
1070432Svi117747 
1071432Svi117747 			ipif = obj->saddr_ipifp;
1072432Svi117747 			scanned++;
1073432Svi117747 
1074432Svi117747 			/*
1075432Svi117747 			 * Delete/mark dontsrc loopback/linklocal addresses and
1076432Svi117747 			 * unsupported address.
1077852Svi117747 			 * On a clustered node, we trust the clustering module
1078852Svi117747 			 * to do the right thing w.r.t loopback addresses, so
1079852Svi117747 			 * we ignore loopback addresses in this check.
1080432Svi117747 			 */
1081852Svi117747 			if ((SCTP_IS_IPIF_LOOPBACK(ipif) &&
1082852Svi117747 			    cl_sctp_check_addrs == NULL) ||
1083852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(ipif) ||
1084432Svi117747 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1085432Svi117747 				if (!delete) {
1086432Svi117747 					obj->saddr_ipif_unconfirmed = 1;
1087432Svi117747 					goto next_obj;
1088432Svi117747 				}
1089432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1090432Svi117747 					sctp->sctp_bound_to_all = 0;
1091432Svi117747 				if (scanned < nsaddr) {
1092432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1093432Svi117747 					    sctp_ipif_list, obj);
1094432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1095432Svi117747 					continue;
1096432Svi117747 				}
1097432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1098432Svi117747 			}
1099432Svi117747 	next_obj:
1100432Svi117747 			if (scanned >= nsaddr)
1101432Svi117747 				return;
1102432Svi117747 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1103432Svi117747 			    obj);
1104432Svi117747 		}
1105432Svi117747 	}
1106432Svi117747 }
1107432Svi117747 
1108432Svi117747 
11090Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
11100Sstevel@tonic-gate in6_addr_t
11110Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6)
11120Sstevel@tonic-gate {
11130Sstevel@tonic-gate 	int			i;
11140Sstevel@tonic-gate 	int			l;
11150Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
11160Sstevel@tonic-gate 	int			scanned = 0;
11170Sstevel@tonic-gate 	in6_addr_t		addr;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11200Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11210Sstevel@tonic-gate 			continue;
11220Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11230Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11240Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1127432Svi117747 			if (!SCTP_DONT_SRC(obj) &&
11280Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
1129432Svi117747 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
11300Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
11310Sstevel@tonic-gate 			}
11320Sstevel@tonic-gate 			scanned++;
11330Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
11340Sstevel@tonic-gate 				goto got_none;
11350Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
11360Sstevel@tonic-gate 			    obj);
11370Sstevel@tonic-gate 		}
11380Sstevel@tonic-gate 	}
11390Sstevel@tonic-gate got_none:
11400Sstevel@tonic-gate 	/* Need to double check this */
11410Sstevel@tonic-gate 	if (isv6 == B_TRUE)
11420Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
11430Sstevel@tonic-gate 	else
11440Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	return (addr);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /*
11500Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
11510Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
11520Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
11530Sstevel@tonic-gate  */
11540Sstevel@tonic-gate int
11550Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate 	int			i;
11580Sstevel@tonic-gate 	int			l;
11590Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
11600Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
11610Sstevel@tonic-gate 	int			family = sctp->sctp_family;
11620Sstevel@tonic-gate 	int			max = *addrcnt;
11630Sstevel@tonic-gate 	size_t			added = 0;
11640Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
11650Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
11660Sstevel@tonic-gate 	int			scanned = 0;
11670Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
11700Sstevel@tonic-gate 		return (EINVAL);
11710Sstevel@tonic-gate 
1172852Svi117747 	/*
1173852Svi117747 	 * Skip loopback addresses for non-loopback assoc., ignore
1174852Svi117747 	 * this on a clustered node.
1175852Svi117747 	 */
1176852Svi117747 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback &&
1177852Svi117747 	    (cl_sctp_check_addrs == NULL)) {
11780Sstevel@tonic-gate 		skip_lback = B_TRUE;
1179852Svi117747 	}
1180852Svi117747 
11810Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
11820Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
11830Sstevel@tonic-gate 			continue;
11840Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
11850Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
11860Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
11870Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 			scanned++;
11900Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1191432Svi117747 			    SCTP_DONT_SRC(obj) ||
1192852Svi117747 			    (SCTP_IS_IPIF_LOOPBACK(ipif) && skip_lback)) {
11930Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
11940Sstevel@tonic-gate 					goto done;
11950Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
11960Sstevel@tonic-gate 				    sctp_ipif_list, obj);
11970Sstevel@tonic-gate 				continue;
11980Sstevel@tonic-gate 			}
11990Sstevel@tonic-gate 			switch (family) {
12000Sstevel@tonic-gate 			case AF_INET:
12010Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
12020Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
12030Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
12040Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
12050Sstevel@tonic-gate 				break;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 			case AF_INET6:
12080Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
12090Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
12100Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
12110Sstevel@tonic-gate 				sin6->sin6_addr = addr;
12120Sstevel@tonic-gate 				break;
12130Sstevel@tonic-gate 			}
12140Sstevel@tonic-gate 			added++;
12150Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
12160Sstevel@tonic-gate 				goto done;
12170Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
12180Sstevel@tonic-gate 			    obj);
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate done:
12220Sstevel@tonic-gate 	*addrcnt = added;
12230Sstevel@tonic-gate 	return (0);
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate /*
1227252Svi117747  * Given the supported address family, walk through the source address list
1228252Svi117747  * and return the total length of the available addresses. If 'p' is not
1229252Svi117747  * null, construct the parameter list for the addresses in 'p'.
1230432Svi117747  * 'modify' will only be set when we want the source address list to
1231432Svi117747  * be modified. The source address list will be modified only when
1232432Svi117747  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1233432Svi117747  * be false since the 'sctp' will be that of the listener.
12340Sstevel@tonic-gate  */
12350Sstevel@tonic-gate size_t
1236432Svi117747 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
12370Sstevel@tonic-gate {
12380Sstevel@tonic-gate 	int			i;
12390Sstevel@tonic-gate 	int			l;
12400Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1241252Svi117747 	size_t			paramlen = 0;
12420Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
12430Sstevel@tonic-gate 	int			scanned = 0;
1244432Svi117747 	int			naddr;
1245432Svi117747 	int			nsaddr;
1246852Svi117747 	boolean_t		del_ll = B_FALSE;
1247852Svi117747 	boolean_t		del_lb = B_FALSE;
1248852Svi117747 
12490Sstevel@tonic-gate 
1250852Svi117747 	/*
1251852Svi117747 	 * On a clustered node don't bother changing anything
1252852Svi117747 	 * on the loopback interface.
1253852Svi117747 	 */
1254852Svi117747 	if (modify && !sctp->sctp_loopback && (cl_sctp_check_addrs == NULL))
1255852Svi117747 		del_lb = B_TRUE;
1256852Svi117747 
1257852Svi117747 	if (modify && !sctp->sctp_linklocal)
1258852Svi117747 		del_ll = B_TRUE;
1259432Svi117747 
1260432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
12610Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
12620Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
12630Sstevel@tonic-gate 			continue;
12640Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1265432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1266432Svi117747 		for (l = 0; l < naddr; l++) {
12670Sstevel@tonic-gate 			in6_addr_t	addr;
12680Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
1269852Svi117747 			boolean_t	ipif_lb;
1270852Svi117747 			boolean_t	ipif_ll;
1271432Svi117747 			boolean_t	unsupp_af;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
12740Sstevel@tonic-gate 			scanned++;
1275432Svi117747 
1276852Svi117747 			ipif_lb = SCTP_IS_IPIF_LOOPBACK(ipif);
1277852Svi117747 			ipif_ll = SCTP_IS_IPIF_LINKLOCAL(ipif);
1278432Svi117747 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1279432Svi117747 			/*
1280432Svi117747 			 * We need to either delete or skip loopback/linklocal
1281852Svi117747 			 * or unsupported addresses, if required.
1282432Svi117747 			 */
1283852Svi117747 			if ((ipif_ll && del_ll) || (ipif_lb && del_lb) ||
1284852Svi117747 			    (unsupp_af && modify)) {
1285432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1286432Svi117747 					sctp->sctp_bound_to_all = 0;
1287432Svi117747 				if (scanned < nsaddr) {
1288432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1289432Svi117747 					    sctp_ipif_list, obj);
1290432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1291432Svi117747 					continue;
1292432Svi117747 				}
1293432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1294432Svi117747 				goto next_addr;
1295852Svi117747 			} else if (ipif_ll || unsupp_af ||
1296852Svi117747 			    (ipif_lb && (cl_sctp_check_addrs == NULL))) {
1297252Svi117747 				goto next_addr;
12980Sstevel@tonic-gate 			}
1299432Svi117747 
1300432Svi117747 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1301432Svi117747 				goto next_addr;
1302252Svi117747 			if (p != NULL)
1303252Svi117747 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
13040Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1305432Svi117747 			if (!ipif->sctp_ipif_isv6) {
13060Sstevel@tonic-gate 				struct in_addr	*v4;
13070Sstevel@tonic-gate 
1308252Svi117747 				if (p != NULL) {
1309252Svi117747 					hdr->sph_type = htons(PARM_ADDR4);
1310252Svi117747 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1311252Svi117747 					v4 = (struct in_addr *)(hdr + 1);
1312252Svi117747 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1313252Svi117747 				}
1314252Svi117747 				paramlen += PARM_ADDR4_LEN;
1315432Svi117747 			} else {
1316252Svi117747 				if (p != NULL) {
1317252Svi117747 					hdr->sph_type = htons(PARM_ADDR6);
1318252Svi117747 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1319252Svi117747 					bcopy(&addr, hdr + 1, sizeof (addr));
1320252Svi117747 				}
1321252Svi117747 				paramlen += PARM_ADDR6_LEN;
13220Sstevel@tonic-gate 			}
1323252Svi117747 next_addr:
1324432Svi117747 			if (scanned >= nsaddr)
1325252Svi117747 				return (paramlen);
13260Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
13270Sstevel@tonic-gate 			    obj);
13280Sstevel@tonic-gate 		}
13290Sstevel@tonic-gate 	}
1330252Svi117747 	return (paramlen);
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate 
1333852Svi117747 /*
1334852Svi117747  * This is used on a clustered node to obtain a list of addresses, the list
1335852Svi117747  * consists of sockaddr_in structs for v4 and sockaddr_in6 for v6. The list
1336852Svi117747  * is then passed onto the clustering module which sends back the correct
1337852Svi117747  * list based on the port info. Regardless of the input, i.e INADDR_ANY
1338852Svi117747  * or specific address(es), we create the list since it could be modified by
1339852Svi117747  * the clustering module. When given a list of addresses, we simply
1340852Svi117747  * create the list of sockaddr_in or sockaddr_in6 structs using those
1341852Svi117747  * addresses. If there is an INADDR_ANY in the input list, or if the
1342852Svi117747  * input is INADDR_ANY, we create a list of sockaddr_in or sockaddr_in6
1343852Svi117747  * structs consisting all the addresses in the global interface list
1344852Svi117747  * except those that are hosted on the loopback interface. We create
1345852Svi117747  * a list of sockaddr_in[6] structs just so that it can be directly input
1346852Svi117747  * to sctp_valid_addr_list() once the clustering module has processed it.
1347852Svi117747  */
1348852Svi117747 int
1349852Svi117747 sctp_get_addrlist(sctp_t *sctp, const void *addrs, uint32_t *addrcnt,
1350852Svi117747     uchar_t **addrlist, int *uspec, size_t *size)
1351852Svi117747 {
1352852Svi117747 	int			cnt;
1353852Svi117747 	int			icnt;
1354852Svi117747 	sctp_ipif_t		*sctp_ipif;
1355852Svi117747 	struct sockaddr_in	*s4;
1356852Svi117747 	struct sockaddr_in6	*s6;
1357852Svi117747 	uchar_t			*p;
1358852Svi117747 	int			err = 0;
1359*3448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
1360852Svi117747 
1361852Svi117747 	*addrlist = NULL;
1362852Svi117747 	*size = 0;
1363852Svi117747 
1364852Svi117747 	/*
1365852Svi117747 	 * Create a list of sockaddr_in[6] structs using the input list.
1366852Svi117747 	 */
1367852Svi117747 	if (sctp->sctp_family == AF_INET) {
1368852Svi117747 		*size = sizeof (struct sockaddr_in) * *addrcnt;
1369852Svi117747 		*addrlist = kmem_zalloc(*size,  KM_SLEEP);
1370852Svi117747 		p = *addrlist;
1371852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1372852Svi117747 			s4 = (struct sockaddr_in *)addrs + cnt;
1373852Svi117747 			/*
1374852Svi117747 			 * We need to create a list of all the available
1375852Svi117747 			 * addresses if there is an INADDR_ANY. However,
1376852Svi117747 			 * if we are beyond LISTEN, then this is invalid
1377852Svi117747 			 * (see sctp_valid_addr_list(). So, we just fail
1378852Svi117747 			 * it here rather than wait till it fails in
1379852Svi117747 			 * sctp_valid_addr_list().
1380852Svi117747 			 */
1381852Svi117747 			if (s4->sin_addr.s_addr == INADDR_ANY) {
1382852Svi117747 				kmem_free(*addrlist, *size);
1383852Svi117747 				*addrlist = NULL;
1384852Svi117747 				*size = 0;
1385852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1386852Svi117747 					*addrcnt = 0;
1387852Svi117747 					return (EINVAL);
1388852Svi117747 				}
1389852Svi117747 				if (uspec != NULL)
1390852Svi117747 					*uspec = 1;
1391852Svi117747 				goto get_all_addrs;
1392852Svi117747 			} else {
1393852Svi117747 				bcopy(s4, p, sizeof (*s4));
1394852Svi117747 				p += sizeof (*s4);
1395852Svi117747 			}
1396852Svi117747 		}
1397852Svi117747 	} else {
1398852Svi117747 		*size = sizeof (struct sockaddr_in6) * *addrcnt;
1399852Svi117747 		*addrlist = kmem_zalloc(*size, KM_SLEEP);
1400852Svi117747 		p = *addrlist;
1401852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1402852Svi117747 			s6 = (struct sockaddr_in6 *)addrs + cnt;
1403852Svi117747 			/*
1404852Svi117747 			 * Comments for INADDR_ANY, above, apply here too.
1405852Svi117747 			 */
1406852Svi117747 			if (IN6_IS_ADDR_UNSPECIFIED(&s6->sin6_addr)) {
1407852Svi117747 				kmem_free(*addrlist, *size);
1408852Svi117747 				*size = 0;
1409852Svi117747 				*addrlist = NULL;
1410852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1411852Svi117747 					*addrcnt = 0;
1412852Svi117747 					return (EINVAL);
1413852Svi117747 				}
1414852Svi117747 				if (uspec != NULL)
1415852Svi117747 					*uspec = 1;
1416852Svi117747 				goto get_all_addrs;
1417852Svi117747 			} else {
1418852Svi117747 				bcopy(addrs, p, sizeof (*s6));
1419852Svi117747 				p += sizeof (*s6);
1420852Svi117747 			}
1421852Svi117747 		}
1422852Svi117747 	}
1423852Svi117747 	return (err);
1424852Svi117747 get_all_addrs:
1425852Svi117747 
1426852Svi117747 	/*
1427852Svi117747 	 * Allocate max possible size. We allocate the max. size here because
1428852Svi117747 	 * the clustering module could end up adding addresses to the list.
1429852Svi117747 	 * We allocate upfront so that the clustering module need to bother
1430852Svi117747 	 * re-sizing the list.
1431852Svi117747 	 */
1432*3448Sdh155122 	if (sctp->sctp_family == AF_INET) {
1433*3448Sdh155122 		*size = sizeof (struct sockaddr_in) *
1434*3448Sdh155122 		    sctps->sctps_g_ipifs_count;
1435*3448Sdh155122 	} else {
1436*3448Sdh155122 		*size = sizeof (struct sockaddr_in6) *
1437*3448Sdh155122 		    sctps->sctps_g_ipifs_count;
1438*3448Sdh155122 	}
1439852Svi117747 	*addrlist = kmem_zalloc(*size, KM_SLEEP);
1440852Svi117747 	*addrcnt = 0;
1441852Svi117747 	p = *addrlist;
1442*3448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
1443852Svi117747 
1444852Svi117747 	/*
1445852Svi117747 	 * Walk through the global interface list and add all addresses,
1446852Svi117747 	 * except those that are hosted on loopback interfaces.
1447852Svi117747 	 */
1448852Svi117747 	for (cnt = 0; cnt <  SCTP_IPIF_HASH; cnt++) {
1449*3448Sdh155122 		if (sctps->sctps_g_ipifs[cnt].ipif_count == 0)
1450852Svi117747 			continue;
1451*3448Sdh155122 		sctp_ipif = list_head(
1452*3448Sdh155122 		    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list);
1453*3448Sdh155122 		for (icnt = 0;
1454*3448Sdh155122 		    icnt < sctps->sctps_g_ipifs[cnt].ipif_count;
1455*3448Sdh155122 		    icnt++) {
1456852Svi117747 			in6_addr_t	addr;
1457852Svi117747 
1458852Svi117747 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1459852Svi117747 			addr = sctp_ipif->sctp_ipif_saddr;
1460852Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
1461852Svi117747 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
1462852Svi117747 			    SCTP_IS_IPIF_LOOPBACK(sctp_ipif) ||
1463852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(sctp_ipif) ||
14642263Ssommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
1465852Svi117747 			    (sctp->sctp_ipversion == IPV4_VERSION &&
1466852Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
1467852Svi117747 			    (sctp->sctp_connp->conn_ipv6_v6only &&
1468852Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
1469852Svi117747 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1470852Svi117747 				sctp_ipif = list_next(
1471*3448Sdh155122 				    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1472852Svi117747 				    sctp_ipif);
1473852Svi117747 				continue;
1474852Svi117747 			}
1475852Svi117747 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1476852Svi117747 			if (sctp->sctp_family == AF_INET) {
1477852Svi117747 				s4 = (struct sockaddr_in *)p;
1478852Svi117747 				IN6_V4MAPPED_TO_INADDR(&addr, &s4->sin_addr);
1479852Svi117747 				s4->sin_family = AF_INET;
1480852Svi117747 				p += sizeof (*s4);
1481852Svi117747 			} else {
1482852Svi117747 				s6 = (struct sockaddr_in6 *)p;
1483852Svi117747 				s6->sin6_addr = addr;
1484852Svi117747 				s6->sin6_family = AF_INET6;
1485852Svi117747 				s6->sin6_scope_id =
1486852Svi117747 				    sctp_ipif->sctp_ipif_ill->sctp_ill_index;
1487852Svi117747 				p += sizeof (*s6);
1488852Svi117747 			}
1489852Svi117747 			(*addrcnt)++;
1490*3448Sdh155122 			sctp_ipif = list_next(
1491*3448Sdh155122 			    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1492852Svi117747 			    sctp_ipif);
1493852Svi117747 		}
1494852Svi117747 	}
1495*3448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
1496852Svi117747 	return (err);
1497852Svi117747 }
1498852Svi117747 
1499852Svi117747 /*
1500852Svi117747  * Get a list of addresses from the source address list. The  caller is
1501852Svi117747  * responsible for allocating sufficient buffer for this.
1502852Svi117747  */
1503852Svi117747 void
1504852Svi117747 sctp_get_saddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1505852Svi117747 {
1506852Svi117747 	int			cnt;
1507852Svi117747 	int			icnt;
1508852Svi117747 	sctp_saddr_ipif_t	*obj;
1509852Svi117747 	int			naddr;
1510852Svi117747 	int			scanned = 0;
1511852Svi117747 
1512852Svi117747 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1513852Svi117747 		if (sctp->sctp_saddrs[cnt].ipif_count == 0)
1514852Svi117747 			continue;
1515852Svi117747 		obj = list_head(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
1516852Svi117747 		naddr = sctp->sctp_saddrs[cnt].ipif_count;
1517852Svi117747 		for (icnt = 0; icnt < naddr; icnt++) {
1518852Svi117747 			sctp_ipif_t	*ipif;
1519852Svi117747 
1520852Svi117747 			if (psize < sizeof (ipif->sctp_ipif_saddr))
1521852Svi117747 				return;
1522852Svi117747 
1523852Svi117747 			scanned++;
1524852Svi117747 			ipif = obj->saddr_ipifp;
1525852Svi117747 			bcopy(&ipif->sctp_ipif_saddr, p,
1526852Svi117747 			    sizeof (ipif->sctp_ipif_saddr));
1527852Svi117747 			p += sizeof (ipif->sctp_ipif_saddr);
1528852Svi117747 			psize -= sizeof (ipif->sctp_ipif_saddr);
1529852Svi117747 			if (scanned >= sctp->sctp_nsaddrs)
1530852Svi117747 				return;
1531*3448Sdh155122 			obj = list_next(
1532*3448Sdh155122 			    &sctp->sctp_saddrs[icnt].sctp_ipif_list,
1533852Svi117747 			    obj);
1534852Svi117747 		}
1535852Svi117747 	}
1536852Svi117747 }
1537852Svi117747 
1538852Svi117747 /*
1539852Svi117747  * Get a list of addresses from the remote address list. The  caller is
1540852Svi117747  * responsible for allocating sufficient buffer for this.
1541852Svi117747  */
1542852Svi117747 void
1543852Svi117747 sctp_get_faddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1544852Svi117747 {
1545852Svi117747 	sctp_faddr_t	*fp;
1546852Svi117747 
1547852Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1548852Svi117747 		if (psize < sizeof (fp->faddr))
1549852Svi117747 			return;
1550852Svi117747 		bcopy(&fp->faddr, p, sizeof (fp->faddr));
1551852Svi117747 		p += sizeof (fp->faddr);
1552852Svi117747 		psize -= sizeof (fp->faddr);
1553852Svi117747 	}
1554852Svi117747 }
15550Sstevel@tonic-gate 
1556*3448Sdh155122 static void
1557*3448Sdh155122 sctp_free_ills(sctp_stack_t *sctps)
1558*3448Sdh155122 {
1559*3448Sdh155122 	int			i;
1560*3448Sdh155122 	int			l;
1561*3448Sdh155122 	sctp_ill_t	*sctp_ill;
1562*3448Sdh155122 
1563*3448Sdh155122 	if (sctps->sctps_ills_count == 0)
1564*3448Sdh155122 		return;
1565*3448Sdh155122 
1566*3448Sdh155122 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1567*3448Sdh155122 		sctp_ill = list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
1568*3448Sdh155122 		for (l = 0; l < sctps->sctps_g_ills[i].ill_count; l++) {
1569*3448Sdh155122 			ASSERT(sctp_ill->sctp_ill_ipifcnt == 0);
1570*3448Sdh155122 			list_remove(&sctps->sctps_g_ills[i].sctp_ill_list,
1571*3448Sdh155122 			    sctp_ill);
1572*3448Sdh155122 			sctps->sctps_ills_count--;
1573*3448Sdh155122 			kmem_free(sctp_ill->sctp_ill_name,
1574*3448Sdh155122 			    sctp_ill->sctp_ill_name_length);
1575*3448Sdh155122 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1576*3448Sdh155122 			sctp_ill =
1577*3448Sdh155122 			    list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
1578*3448Sdh155122 		}
1579*3448Sdh155122 		sctps->sctps_g_ills[i].ill_count = 0;
1580*3448Sdh155122 	}
1581*3448Sdh155122 	ASSERT(sctps->sctps_ills_count == 0);
1582*3448Sdh155122 }
1583*3448Sdh155122 
1584*3448Sdh155122 static void
1585*3448Sdh155122 sctp_free_ipifs(sctp_stack_t *sctps)
1586*3448Sdh155122 {
1587*3448Sdh155122 	int			i;
1588*3448Sdh155122 	int			l;
1589*3448Sdh155122 	sctp_ipif_t	*sctp_ipif;
1590*3448Sdh155122 	sctp_ill_t	*sctp_ill;
1591*3448Sdh155122 
1592*3448Sdh155122 	if (sctps->sctps_g_ipifs_count == 0)
1593*3448Sdh155122 		return;
1594*3448Sdh155122 
1595*3448Sdh155122 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1596*3448Sdh155122 		sctp_ipif = list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1597*3448Sdh155122 		for (l = 0; l < sctps->sctps_g_ipifs[i].ipif_count; l++) {
1598*3448Sdh155122 			sctp_ill = sctp_ipif->sctp_ipif_ill;
1599*3448Sdh155122 
1600*3448Sdh155122 			list_remove(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
1601*3448Sdh155122 			    sctp_ipif);
1602*3448Sdh155122 			sctps->sctps_g_ipifs_count--;
1603*3448Sdh155122 			(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt,
1604*3448Sdh155122 			    -1);
1605*3448Sdh155122 			kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1606*3448Sdh155122 			sctp_ipif =
1607*3448Sdh155122 			    list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1608*3448Sdh155122 		}
1609*3448Sdh155122 		sctps->sctps_g_ipifs[i].ipif_count = 0;
1610*3448Sdh155122 	}
1611*3448Sdh155122 	ASSERT(sctps->sctps_g_ipifs_count == 0);
1612*3448Sdh155122 }
1613*3448Sdh155122 
1614*3448Sdh155122 
16150Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
16160Sstevel@tonic-gate void
1617*3448Sdh155122 sctp_saddr_init(sctp_stack_t *sctps)
16180Sstevel@tonic-gate {
16190Sstevel@tonic-gate 	int	i;
16200Sstevel@tonic-gate 
1621*3448Sdh155122 	sctps->sctps_g_ills = kmem_zalloc(sizeof (sctp_ill_hash_t) *
1622*3448Sdh155122 	    SCTP_ILL_HASH, KM_SLEEP);
1623*3448Sdh155122 	sctps->sctps_g_ipifs = kmem_zalloc(sizeof (sctp_ipif_hash_t) *
1624*3448Sdh155122 	    SCTP_IPIF_HASH, KM_SLEEP);
1625*3448Sdh155122 
1626*3448Sdh155122 	rw_init(&sctps->sctps_g_ills_lock, NULL, RW_DEFAULT, NULL);
1627*3448Sdh155122 	rw_init(&sctps->sctps_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1630*3448Sdh155122 		sctps->sctps_g_ills[i].ill_count = 0;
1631*3448Sdh155122 		list_create(&sctps->sctps_g_ills[i].sctp_ill_list,
1632*3448Sdh155122 		    sizeof (sctp_ill_t),
16330Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1636*3448Sdh155122 		sctps->sctps_g_ipifs[i].ipif_count = 0;
1637*3448Sdh155122 		list_create(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
16380Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
16390Sstevel@tonic-gate 	}
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate void
1643*3448Sdh155122 sctp_saddr_fini(sctp_stack_t *sctps)
16440Sstevel@tonic-gate {
16450Sstevel@tonic-gate 	int	i;
16460Sstevel@tonic-gate 
1647*3448Sdh155122 	sctp_free_ipifs(sctps);
1648*3448Sdh155122 	sctp_free_ills(sctps);
1649*3448Sdh155122 
16500Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
1651*3448Sdh155122 		list_destroy(&sctps->sctps_g_ills[i].sctp_ill_list);
16520Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
1653*3448Sdh155122 		list_destroy(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1654*3448Sdh155122 
1655*3448Sdh155122 	ASSERT(sctps->sctps_ills_count == 0 && sctps->sctps_g_ipifs_count == 0);
1656*3448Sdh155122 	kmem_free(sctps->sctps_g_ills, sizeof (sctp_ill_hash_t) *
1657*3448Sdh155122 	    SCTP_ILL_HASH);
1658*3448Sdh155122 	sctps->sctps_g_ills = NULL;
1659*3448Sdh155122 	kmem_free(sctps->sctps_g_ipifs, sizeof (sctp_ipif_hash_t) *
1660*3448Sdh155122 	    SCTP_IPIF_HASH);
1661*3448Sdh155122 	sctps->sctps_g_ipifs = NULL;
1662*3448Sdh155122 	rw_destroy(&sctps->sctps_g_ills_lock);
1663*3448Sdh155122 	rw_destroy(&sctps->sctps_g_ipifs_lock);
16640Sstevel@tonic-gate }
1665