xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_addr.c (revision 3510:c7025db1a5cd)
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 /*
223448Sdh155122  * 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,
53*3510Svi117747 			    zoneid_t, boolean_t, uint_t, uint_t, boolean_t,
54*3510Svi117747 			    sctp_stack_t *);
550Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
56852Svi117747 int			sctp_valid_addr_list(sctp_t *, const void *, uint32_t,
57852Svi117747 			    uchar_t *, size_t);
58432Svi117747 static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
59*3510Svi117747 			    boolean_t, boolean_t);
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 *);
763448Sdh155122 void			sctp_saddr_init(sctp_stack_t *);
773448Sdh155122 void			sctp_saddr_fini(sctp_stack_t *);
78432Svi117747 
79*3510Svi117747 #define	SCTP_ADDR4_HASH(addr)	\
80*3510Svi117747 	(((addr) ^ ((addr) >> 8) ^ ((addr) >> 16) ^ ((addr) >> 24)) &	\
81*3510Svi117747 	(SCTP_IPIF_HASH - 1))
82*3510Svi117747 
83*3510Svi117747 #define	SCTP_ADDR6_HASH(addr)	\
84*3510Svi117747 	(((addr).s6_addr32[3] ^						\
85*3510Svi117747 	(((addr).s6_addr32[3] ^ (addr).s6_addr32[2]) >> 12)) &		\
86*3510Svi117747 	(SCTP_IPIF_HASH - 1))
87*3510Svi117747 
88*3510Svi117747 #define	SCTP_IPIF_ADDR_HASH(addr, isv6)					\
89*3510Svi117747 	((isv6) ? SCTP_ADDR6_HASH((addr)) : 				\
90*3510Svi117747 	SCTP_ADDR4_HASH((addr)._S6_un._S6_u32[3]))
91*3510Svi117747 
920Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
930Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
94432Svi117747 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
95432Svi117747 
96432Svi117747 #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
97432Svi117747 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
98432Svi117747 
99852Svi117747 #define	SCTP_IS_IPIF_LOOPBACK(ipif)		\
100852Svi117747 	((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK)
101852Svi117747 
102852Svi117747 #define	SCTP_IS_IPIF_LINKLOCAL(ipif)		\
103852Svi117747 	((ipif)->sctp_ipif_isv6 && 		\
104852Svi117747 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr))
105432Svi117747 
106432Svi117747 #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
107432Svi117747 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
108432Svi117747 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
1090Sstevel@tonic-gate 
1102263Ssommerfe #define	SCTP_IPIF_ZONE_MATCH(sctp, ipif) 				\
1112263Ssommerfe 	IPCL_ZONE_MATCH((sctp)->sctp_connp, (ipif)->sctp_ipif_zoneid)
1122263Ssommerfe 
1130Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
1140Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1220Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate static void
1250Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
128*3510Svi117747 	uint_t		hindex;
1290Sstevel@tonic-gate 	uint_t		ill_index;
1303448Sdh155122 	sctp_stack_t	*sctps = sctp_ipif->sctp_ipif_ill->
1313448Sdh155122 	    sctp_ill_netstack->netstack_sctp;
1320Sstevel@tonic-gate 
1333448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
1343448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
1350Sstevel@tonic-gate 
136*3510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(sctp_ipif->sctp_ipif_saddr,
137*3510Svi117747 	    sctp_ipif->sctp_ipif_isv6);
138*3510Svi117747 
1390Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1400Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1410Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1420Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1430Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
1443448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
1453448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
1460Sstevel@tonic-gate 		return;
1470Sstevel@tonic-gate 	}
148*3510Svi117747 	list_remove(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
1493448Sdh155122 	    sctp_ipif);
150*3510Svi117747 	sctps->sctps_g_ipifs[hindex].ipif_count--;
1513448Sdh155122 	sctps->sctps_g_ipifs_count--;
1520Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1530Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1563448Sdh155122 	if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
1573448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1580Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1590Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1603448Sdh155122 			list_remove(&sctps->sctps_g_ills[ill_index].
1613448Sdh155122 			    sctp_ill_list, (void *)sctp_ill);
1623448Sdh155122 			sctps->sctps_g_ills[ill_index].ill_count--;
1633448Sdh155122 			sctps->sctps_ills_count--;
1640Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1650Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1660Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 	}
1693448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
1703448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
175*3510Svi117747  * We are either looking for a IPIF with the given address before
176*3510Svi117747  * inserting it into the global list or looking for an IPIF for an
177*3510Svi117747  * address given an SCTP. In the former case we always check the zoneid,
178*3510Svi117747  * but for the latter case, check_zid could be B_FALSE if the connp
179*3510Svi117747  * for the sctp has conn_all_zones set. When looking for an address we
180*3510Svi117747  * give preference to one that is up, so even though we may find one that
181*3510Svi117747  * is not up we keep looking if there is one up, we hold the down addr
182*3510Svi117747  * in backup_ipif in case we don't find one that is up - i.e. we return
183*3510Svi117747  * the backup_ipif in that case. Note that if we are looking for. If we
184*3510Svi117747  * are specifically looking for an up address, then usable will be set
185*3510Svi117747  * to true.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate static sctp_ipif_t *
188*3510Svi117747 sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid,
189*3510Svi117747     boolean_t check_zid, uint_t ifindex, uint_t seqid, boolean_t usable,
190*3510Svi117747     sctp_stack_t *sctps)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	int		j;
1930Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
194*3510Svi117747 	sctp_ipif_t	*backup_ipif = NULL;
195*3510Svi117747 	int		hindex;
1960Sstevel@tonic-gate 
197*3510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
198*3510Svi117747 
1993448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
200*3510Svi117747 	if (sctps->sctps_g_ipifs[hindex].ipif_count == 0) {
201*3510Svi117747 		rw_exit(&sctps->sctps_g_ipifs_lock);
202*3510Svi117747 		return (NULL);
203*3510Svi117747 	}
204*3510Svi117747 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
205*3510Svi117747 	for (j = 0; j < sctps->sctps_g_ipifs[hindex].ipif_count; j++) {
206*3510Svi117747 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
207*3510Svi117747 		if ((!check_zid ||
208*3510Svi117747 		    (sctp_ipif->sctp_ipif_zoneid == ALL_ZONES ||
209*3510Svi117747 		    zoneid == sctp_ipif->sctp_ipif_zoneid)) &&
210*3510Svi117747 		    (ifindex == 0 || ifindex ==
211*3510Svi117747 		    sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
212*3510Svi117747 		    ((seqid != 0 && seqid == sctp_ipif->sctp_ipif_id) ||
213*3510Svi117747 		    (IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
214*3510Svi117747 		    addr)))) {
215*3510Svi117747 			if (!usable || sctp_ipif->sctp_ipif_state ==
216*3510Svi117747 			    SCTP_IPIFS_UP) {
2170Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2180Sstevel@tonic-gate 				if (refhold)
2190Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
2203448Sdh155122 				rw_exit(&sctps->sctps_g_ipifs_lock);
2210Sstevel@tonic-gate 				return (sctp_ipif);
222*3510Svi117747 			} else if (sctp_ipif->sctp_ipif_state ==
223*3510Svi117747 			    SCTP_IPIFS_DOWN && backup_ipif == NULL) {
224*3510Svi117747 				backup_ipif = sctp_ipif;
2250Sstevel@tonic-gate 			}
2260Sstevel@tonic-gate 		}
227*3510Svi117747 		rw_exit(&sctp_ipif->sctp_ipif_lock);
228*3510Svi117747 		sctp_ipif = list_next(
229*3510Svi117747 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list, sctp_ipif);
230*3510Svi117747 	}
231*3510Svi117747 	if (backup_ipif != NULL) {
232*3510Svi117747 		if (refhold)
233*3510Svi117747 			SCTP_IPIF_REFHOLD(backup_ipif);
234*3510Svi117747 		rw_exit(&sctps->sctps_g_ipifs_lock);
235*3510Svi117747 		return (backup_ipif);
2360Sstevel@tonic-gate 	}
2373448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2380Sstevel@tonic-gate 	return (NULL);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
2430Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
2440Sstevel@tonic-gate  * Called with no locks held.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate static int
2470Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
2500Sstevel@tonic-gate 	int			i;
2510Sstevel@tonic-gate 	int			j;
2520Sstevel@tonic-gate 	int			error = 0;
2533448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2540Sstevel@tonic-gate 
2553448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
2560Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
2573448Sdh155122 		if (sctps->sctps_g_ipifs[i].ipif_count == 0)
2580Sstevel@tonic-gate 			continue;
2593448Sdh155122 		sctp_ipif = list_head(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
2603448Sdh155122 		for (j = 0; j < sctps->sctps_g_ipifs[i].ipif_count; j++) {
2610Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
262432Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
2630Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
2642263Ssommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
2650Sstevel@tonic-gate 			    (sctp->sctp_ipversion == IPV4_VERSION &&
266432Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
2670Sstevel@tonic-gate 			    (sctp->sctp_connp->conn_ipv6_v6only &&
268432Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
2690Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2700Sstevel@tonic-gate 				sctp_ipif = list_next(
2713448Sdh155122 				    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
2723448Sdh155122 				    sctp_ipif);
2730Sstevel@tonic-gate 				continue;
2740Sstevel@tonic-gate 			}
2750Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2760Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
277432Svi117747 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
278*3510Svi117747 			    B_FALSE, B_FALSE);
279*3510Svi117747 			if (error != 0 && error != EALREADY)
2800Sstevel@tonic-gate 				goto free_stuff;
2813448Sdh155122 			sctp_ipif = list_next(
2823448Sdh155122 			    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
2830Sstevel@tonic-gate 			    sctp_ipif);
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 	}
2863448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2870Sstevel@tonic-gate 	return (0);
2880Sstevel@tonic-gate free_stuff:
2893448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
2900Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2910Sstevel@tonic-gate 	return (ENOMEM);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2960Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
297852Svi117747  * or error. If the caller wants the list of addresses, it sends a pre-allocated
298852Svi117747  * buffer - list. Currently, this list is only used on a clustered node when
299852Svi117747  * the SCTP is in the listen state (from sctp_bind_add()). When called on a
300852Svi117747  * clustered node, the input is always a list of addresses (even if the
301852Svi117747  * original bind() was to INADDR_ANY).
3020Sstevel@tonic-gate  * Called with no locks held.
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate int
305852Svi117747 sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
306852Svi117747     uchar_t *list, size_t lsize)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
3090Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
3100Sstevel@tonic-gate 	struct in_addr		*addr4;
3110Sstevel@tonic-gate 	in6_addr_t		addr;
3120Sstevel@tonic-gate 	int			cnt;
3130Sstevel@tonic-gate 	int			err = 0;
3140Sstevel@tonic-gate 	int			saddr_cnt = 0;
3150Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
3160Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
3170Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
3180Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
319852Svi117747 	uchar_t			*p = list;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
3230Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
3240Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
3250Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
3260Sstevel@tonic-gate 	 * addresses.
3270Sstevel@tonic-gate 	 */
3280Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
3290Sstevel@tonic-gate 		check_lport = B_TRUE;
3300Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
3310Sstevel@tonic-gate 			check_addrs = B_TRUE;
3320Sstevel@tonic-gate 	}
333852Svi117747 
3340Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3350Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
3360Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3370Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
3380Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
3390Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
340852Svi117747 		uint_t		ifindex = 0;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		switch (sctp->sctp_family) {
3430Sstevel@tonic-gate 		case AF_INET:
3440Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
3450Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
3460Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
3470Sstevel@tonic-gate 				err = EINVAL;
3480Sstevel@tonic-gate 				goto free_ret;
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
3510Sstevel@tonic-gate 			if (check_addrs &&
3520Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
3530Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
3540Sstevel@tonic-gate 			    IN_MULTICAST(addr4->s_addr))) {
3550Sstevel@tonic-gate 				err = EINVAL;
3560Sstevel@tonic-gate 				goto free_ret;
3570Sstevel@tonic-gate 			}
3580Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
3590Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
3600Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3610Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3620Sstevel@tonic-gate 			}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 			break;
3650Sstevel@tonic-gate 		case AF_INET6:
3660Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3670Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
3680Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
3690Sstevel@tonic-gate 				err = EINVAL;
3700Sstevel@tonic-gate 				goto free_ret;
3710Sstevel@tonic-gate 			}
3720Sstevel@tonic-gate 			addr = sin6->sin6_addr;
373852Svi117747 			/* Contains the interface index */
374852Svi117747 			ifindex = sin6->sin6_scope_id;
3750Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
3760Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3770Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3780Sstevel@tonic-gate 				goto free_ret;
3790Sstevel@tonic-gate 			}
3800Sstevel@tonic-gate 			if (check_addrs &&
3810Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3820Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3830Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3840Sstevel@tonic-gate 				err = EINVAL;
3850Sstevel@tonic-gate 				goto free_ret;
3860Sstevel@tonic-gate 			}
3870Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3880Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3890Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3900Sstevel@tonic-gate 			}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 			break;
3930Sstevel@tonic-gate 		default:
3940Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3950Sstevel@tonic-gate 			goto free_ret;
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 		if (lookup_saddr) {
398*3510Svi117747 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
399*3510Svi117747 			    sctp->sctp_zoneid, !sctp->sctp_connp->conn_allzones,
400*3510Svi117747 			    ifindex, 0, B_TRUE, sctp->sctp_sctps);
4010Sstevel@tonic-gate 			if (ipif == NULL) {
4020Sstevel@tonic-gate 				/* Address not in the list */
4030Sstevel@tonic-gate 				err = EINVAL;
4040Sstevel@tonic-gate 				goto free_ret;
405852Svi117747 			} else if (check_addrs && SCTP_IS_IPIF_LOOPBACK(ipif) &&
406852Svi117747 			    cl_sctp_check_addrs == NULL) {
4070Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
4080Sstevel@tonic-gate 				err = EINVAL;
4090Sstevel@tonic-gate 				goto free_ret;
4100Sstevel@tonic-gate 			}
4110Sstevel@tonic-gate 		}
4120Sstevel@tonic-gate 		if (!bind_to_all) {
413432Svi117747 			/*
414432Svi117747 			 * If an address is added after association setup,
415432Svi117747 			 * we need to wait for the peer to send us an ASCONF
416432Svi117747 			 * ACK before we can start using it.
417432Svi117747 			 * saddr_ipif_dontsrc will be reset (to 0) when we
418432Svi117747 			 * get the ASCONF ACK for this address.
419432Svi117747 			 */
420432Svi117747 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
421*3510Svi117747 			    check_addrs ? B_TRUE : B_FALSE, B_FALSE);
4220Sstevel@tonic-gate 			if (err != 0) {
4230Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
4240Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
4250Sstevel@tonic-gate 					err = EADDRINUSE;
4260Sstevel@tonic-gate 				goto free_ret;
4270Sstevel@tonic-gate 			}
4280Sstevel@tonic-gate 			saddr_cnt++;
429852Svi117747 			if (lsize >= sizeof (addr)) {
430852Svi117747 				bcopy(&addr, p, sizeof (addr));
431852Svi117747 				p += sizeof (addr);
432852Svi117747 				lsize -= sizeof (addr);
433852Svi117747 			}
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 	if (bind_to_all) {
4370Sstevel@tonic-gate 		/*
4380Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
4390Sstevel@tonic-gate 		 * inaddr_any.
4400Sstevel@tonic-gate 		 */
4410Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
4420Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
4430Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
4460Sstevel@tonic-gate 		if (err != 0)
4470Sstevel@tonic-gate 			return (err);
4480Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4510Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4520Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4530Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4540Sstevel@tonic-gate 	return (0);
4550Sstevel@tonic-gate free_ret:
4560Sstevel@tonic-gate 	if (saddr_cnt != 0)
4570Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
4580Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4590Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4600Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4610Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4620Sstevel@tonic-gate 	return (err);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate static int
466432Svi117747 sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
467*3510Svi117747     boolean_t dontsrc, boolean_t allow_dup)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	int			cnt;
4700Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
471*3510Svi117747 	int			hindex;
4720Sstevel@tonic-gate 
473*3510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->sctp_ipif_saddr,
474*3510Svi117747 	    ipif->sctp_ipif_isv6);
475*3510Svi117747 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
476*3510Svi117747 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
477*3510Svi117747 		if (IN6_ARE_ADDR_EQUAL(&ipif_obj->saddr_ipifp->sctp_ipif_saddr,
478*3510Svi117747 		    &ipif->sctp_ipif_saddr)) {
479*3510Svi117747 			if (ipif->sctp_ipif_id !=
480*3510Svi117747 			    ipif_obj->saddr_ipifp->sctp_ipif_id &&
481*3510Svi117747 			    ipif_obj->saddr_ipifp->sctp_ipif_state ==
482*3510Svi117747 			    SCTP_IPIFS_DOWN && ipif->sctp_ipif_state ==
483*3510Svi117747 			    SCTP_IPIFS_UP) {
484*3510Svi117747 				SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
485*3510Svi117747 				ipif_obj->saddr_ipifp = ipif;
486*3510Svi117747 				ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
487*3510Svi117747 				return (0);
488*3510Svi117747 			} else if (!allow_dup || ipif->sctp_ipif_id ==
489*3510Svi117747 			    ipif_obj->saddr_ipifp->sctp_ipif_id) {
490*3510Svi117747 				return (EALREADY);
491*3510Svi117747 			}
492*3510Svi117747 		}
493*3510Svi117747 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
4940Sstevel@tonic-gate 		    ipif_obj);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4970Sstevel@tonic-gate 	if (ipif_obj == NULL) {
4980Sstevel@tonic-gate 		/* Need to do something */
4990Sstevel@tonic-gate 		return (ENOMEM);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
502432Svi117747 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
503*3510Svi117747 	list_insert_tail(&sctp->sctp_saddrs[hindex].sctp_ipif_list, ipif_obj);
504*3510Svi117747 	sctp->sctp_saddrs[hindex].ipif_count++;
5050Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
5060Sstevel@tonic-gate 	return (0);
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate static void
5100Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	int			cnt;
5130Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
514*3510Svi117747 	int			hindex;
5150Sstevel@tonic-gate 
516*3510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->sctp_ipif_saddr,
517*3510Svi117747 	    ipif->sctp_ipif_isv6);
518*3510Svi117747 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
519*3510Svi117747 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
520*3510Svi117747 		if (IN6_ARE_ADDR_EQUAL(&ipif_obj->saddr_ipifp->sctp_ipif_saddr,
521*3510Svi117747 		    &ipif->sctp_ipif_saddr)) {
522*3510Svi117747 			list_remove(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
5230Sstevel@tonic-gate 			    ipif_obj);
524*3510Svi117747 			sctp->sctp_saddrs[hindex].ipif_count--;
5250Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
5260Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
5270Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
5280Sstevel@tonic-gate 			break;
5290Sstevel@tonic-gate 		}
530*3510Svi117747 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
5310Sstevel@tonic-gate 		    ipif_obj);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate static int
5360Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate 	int			i;
5390Sstevel@tonic-gate 	int			j;
5400Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
5410Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
5420Sstevel@tonic-gate 	int			overlap = 0;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
5450Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5460Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
5470Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
548*3510Svi117747 			if (IN6_ARE_ADDR_EQUAL(
549*3510Svi117747 			    &obj1->saddr_ipifp->sctp_ipif_saddr,
550*3510Svi117747 			    &obj2->saddr_ipifp->sctp_ipif_saddr)) {
5510Sstevel@tonic-gate 				overlap++;
5520Sstevel@tonic-gate 				break;
5530Sstevel@tonic-gate 			}
5540Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
5550Sstevel@tonic-gate 			    obj2);
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 	return (overlap);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate int
5630Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	int		i;
5660Sstevel@tonic-gate 	int		overlap = 0;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5690Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
5700Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5740Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5750Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
5790Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (overlap > 0)
5820Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate static int
5880Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	int			i;
5910Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
5920Sstevel@tonic-gate 	int			error = 0;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
5950Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5960Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
597432Svi117747 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
598*3510Svi117747 		    B_FALSE, B_FALSE);
599*3510Svi117747 		ASSERT(error != EALREADY);
6000Sstevel@tonic-gate 		if (error != 0)
6010Sstevel@tonic-gate 			return (error);
6020Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 	return (error);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate int
6080Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate 	int	error = 0;
6110Sstevel@tonic-gate 	int	i;
6120Sstevel@tonic-gate 
613432Svi117747 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
6140Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
6170Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
6180Sstevel@tonic-gate 			continue;
6190Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
6200Sstevel@tonic-gate 		if (error != 0) {
6210Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
6220Sstevel@tonic-gate 			return (error);
6230Sstevel@tonic-gate 		}
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 	return (0);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate void
6290Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate 	int			i;
6320Sstevel@tonic-gate 	int			l;
6330Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
6360Sstevel@tonic-gate 		return;
6370Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
6380Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
6390Sstevel@tonic-gate 			continue;
6400Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6410Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
6420Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
6430Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
6440Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
6450Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
6460Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
6490Sstevel@tonic-gate 	}
650432Svi117747 	if (sctp->sctp_bound_to_all == 1)
651432Svi117747 		sctp->sctp_bound_to_all = 0;
6520Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate /*
6560Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
6570Sstevel@tonic-gate  * held.
6580Sstevel@tonic-gate  */
6590Sstevel@tonic-gate void
6600Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
6610Sstevel@tonic-gate {
6620Sstevel@tonic-gate 	int		i;
6630Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
6640Sstevel@tonic-gate 	uint_t		index;
6653448Sdh155122 	netstack_t	*ns = ill->ill_ipst->ips_netstack;
6663448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
6670Sstevel@tonic-gate 
6683448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_WRITER);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
6713448Sdh155122 	sctp_ill = list_head(&sctps->sctps_g_ills[index].sctp_ill_list);
6723448Sdh155122 	for (i = 0; i < sctps->sctps_g_ills[index].ill_count; i++) {
6730Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
6740Sstevel@tonic-gate 			break;
6753448Sdh155122 		sctp_ill = list_next(&sctps->sctps_g_ills[index].sctp_ill_list,
6760Sstevel@tonic-gate 		    sctp_ill);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	switch (op) {
6800Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
6810Sstevel@tonic-gate 		if (sctp_ill != NULL) {
6820Sstevel@tonic-gate 			/* Unmark it if it is condemned */
6830Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
6840Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
6853448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
6860Sstevel@tonic-gate 			return;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
6890Sstevel@tonic-gate 		/* Need to re-try? */
6900Sstevel@tonic-gate 		if (sctp_ill == NULL) {
6910Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6923448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
6930Sstevel@tonic-gate 			return;
6940Sstevel@tonic-gate 		}
695*3510Svi117747 		sctp_ill->sctp_ill_name = kmem_zalloc(ill->ill_name_length,
696*3510Svi117747 		    KM_NOSLEEP);
6970Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
6980Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
6990Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
7003448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
7010Sstevel@tonic-gate 			return;
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
7040Sstevel@tonic-gate 		    ill->ill_name_length);
7050Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
7060Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
7070Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
7083448Sdh155122 		sctp_ill->sctp_ill_netstack = ns;	/* No netstack_hold */
7093448Sdh155122 		list_insert_tail(&sctps->sctps_g_ills[index].sctp_ill_list,
7100Sstevel@tonic-gate 		    (void *)sctp_ill);
7113448Sdh155122 		sctps->sctps_g_ills[index].ill_count++;
7123448Sdh155122 		sctps->sctps_ills_count++;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		break;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		if (sctp_ill == NULL) {
7193448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
7200Sstevel@tonic-gate 			return;
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
7233448Sdh155122 			list_remove(&sctps->sctps_g_ills[index].sctp_ill_list,
7240Sstevel@tonic-gate 			    (void *)sctp_ill);
7253448Sdh155122 			sctps->sctps_g_ills[index].ill_count--;
7263448Sdh155122 			sctps->sctps_ills_count--;
7270Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
7280Sstevel@tonic-gate 			    ill->ill_name_length);
7290Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
7300Sstevel@tonic-gate 		} else {
7310Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 		break;
7350Sstevel@tonic-gate 	}
7363448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
7400Sstevel@tonic-gate void
7410Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
7440Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
7450Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
746*3510Svi117747 	uint_t		hindex;
7470Sstevel@tonic-gate 	int		i;
7483448Sdh155122 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
7493448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
7500Sstevel@tonic-gate 
7513448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
7523448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
7530Sstevel@tonic-gate 
754*3510Svi117747 	hindex = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
755*3510Svi117747 	fsctp_ill = list_head(&sctps->sctps_g_ills[hindex].sctp_ill_list);
756*3510Svi117747 	for (i = 0; i < sctps->sctps_g_ills[hindex].ill_count; i++) {
7570Sstevel@tonic-gate 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill))
7580Sstevel@tonic-gate 			break;
759*3510Svi117747 		fsctp_ill = list_next(
760*3510Svi117747 		    &sctps->sctps_g_ills[hindex].sctp_ill_list, fsctp_ill);
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
763*3510Svi117747 	hindex = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
764*3510Svi117747 	tsctp_ill = list_head(&sctps->sctps_g_ills[hindex].sctp_ill_list);
765*3510Svi117747 	for (i = 0; i < sctps->sctps_g_ills[hindex].ill_count; i++) {
7660Sstevel@tonic-gate 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill))
7670Sstevel@tonic-gate 			break;
768*3510Svi117747 		tsctp_ill = list_next(
769*3510Svi117747 		    &sctps->sctps_g_ills[hindex].sctp_ill_list, tsctp_ill);
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
772*3510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr,
773*3510Svi117747 	    ipif->ipif_ill->ill_isv6);
774*3510Svi117747 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
775*3510Svi117747 	for (i = 0; i < sctps->sctps_g_ipifs[hindex].ipif_count; i++) {
7760Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
7770Sstevel@tonic-gate 			break;
7783448Sdh155122 		sctp_ipif = list_next(
779*3510Svi117747 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list, sctp_ipif);
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 	/* Should be an ASSERT? */
7820Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
7830Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
7840Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
7853448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
7863448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
7870Sstevel@tonic-gate 		return;
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
7900Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
7910Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
7920Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
7930Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
7940Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
7953448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
7963448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
799*3510Svi117747 /*
800*3510Svi117747  * Walk the list of SCTPs and find each that has oipif in it's saddr list, and
801*3510Svi117747  * if so replace it with nipif.
802*3510Svi117747  */
803*3510Svi117747 void
804*3510Svi117747 sctp_update_saddrs(sctp_ipif_t *oipif, sctp_ipif_t *nipif, int idx,
805*3510Svi117747     sctp_stack_t *sctps)
806*3510Svi117747 {
807*3510Svi117747 	sctp_t			*sctp;
808*3510Svi117747 	sctp_t			*sctp_prev = NULL;
809*3510Svi117747 	sctp_saddr_ipif_t	*sobj;
810*3510Svi117747 	int			count;
811*3510Svi117747 
812*3510Svi117747 	sctp = sctps->sctps_gsctp;
813*3510Svi117747 	mutex_enter(&sctps->sctps_g_lock);
814*3510Svi117747 	while (sctp != NULL && oipif->sctp_ipif_refcnt > 0) {
815*3510Svi117747 		mutex_enter(&sctp->sctp_reflock);
816*3510Svi117747 		if (sctp->sctp_condemned ||
817*3510Svi117747 		    sctp->sctp_saddrs[idx].ipif_count <= 0) {
818*3510Svi117747 			mutex_exit(&sctp->sctp_reflock);
819*3510Svi117747 			sctp = list_next(&sctps->sctps_g_list, sctp);
820*3510Svi117747 			continue;
821*3510Svi117747 		}
822*3510Svi117747 		sctp->sctp_refcnt++;
823*3510Svi117747 		mutex_exit(&sctp->sctp_reflock);
824*3510Svi117747 		mutex_exit(&sctps->sctps_g_lock);
825*3510Svi117747 		if (sctp_prev != NULL)
826*3510Svi117747 			SCTP_REFRELE(sctp_prev);
827*3510Svi117747 
828*3510Svi117747 		RUN_SCTP(sctp);
829*3510Svi117747 		sobj = list_head(&sctp->sctp_saddrs[idx].sctp_ipif_list);
830*3510Svi117747 		for (count = 0; count <
831*3510Svi117747 		    sctp->sctp_saddrs[idx].ipif_count; count++) {
832*3510Svi117747 			if (sobj->saddr_ipifp == oipif) {
833*3510Svi117747 				SCTP_IPIF_REFHOLD(nipif);
834*3510Svi117747 				sobj->saddr_ipifp = nipif;
835*3510Svi117747 				ASSERT(oipif->sctp_ipif_refcnt > 0);
836*3510Svi117747 				/* We have the writer lock */
837*3510Svi117747 				oipif->sctp_ipif_refcnt--;
838*3510Svi117747 				/*
839*3510Svi117747 				 * Can't have more than one referring
840*3510Svi117747 				 * to the same sctp_ipif.
841*3510Svi117747 				 */
842*3510Svi117747 				break;
843*3510Svi117747 			}
844*3510Svi117747 			sobj = list_next(&sctp->sctp_saddrs[idx].sctp_ipif_list,
845*3510Svi117747 			    sobj);
846*3510Svi117747 		}
847*3510Svi117747 		WAKE_SCTP(sctp);
848*3510Svi117747 		sctp_prev = sctp;
849*3510Svi117747 		mutex_enter(&sctps->sctps_g_lock);
850*3510Svi117747 		sctp = list_next(&sctps->sctps_g_list, sctp);
851*3510Svi117747 	}
852*3510Svi117747 	mutex_exit(&sctps->sctps_g_lock);
853*3510Svi117747 	if (sctp_prev != NULL)
854*3510Svi117747 		SCTP_REFRELE(sctp_prev);
855*3510Svi117747 }
856*3510Svi117747 
857*3510Svi117747 /*
858*3510Svi117747  * Given an ipif, walk the hash list in the global ipif table and for
859*3510Svi117747  * any other SCTP ipif with the same address and non-zero reference, walk
860*3510Svi117747  * the SCTP list and update the saddr list, if required, to point to the
861*3510Svi117747  * new SCTP ipif.
862*3510Svi117747  */
863*3510Svi117747 void
864*3510Svi117747 sctp_chk_and_updt_saddr(int hindex, sctp_ipif_t *ipif, sctp_stack_t *sctps)
865*3510Svi117747 {
866*3510Svi117747 	int		cnt;
867*3510Svi117747 	sctp_ipif_t	*sipif;
868*3510Svi117747 
869*3510Svi117747 	ASSERT(sctps->sctps_g_ipifs[hindex].ipif_count > 0);
870*3510Svi117747 	ASSERT(ipif->sctp_ipif_state == SCTP_IPIFS_UP);
871*3510Svi117747 
872*3510Svi117747 	sipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
873*3510Svi117747 	for (cnt = 0; cnt < sctps->sctps_g_ipifs[hindex].ipif_count; cnt++) {
874*3510Svi117747 		rw_enter(&sipif->sctp_ipif_lock, RW_WRITER);
875*3510Svi117747 		if (sipif->sctp_ipif_id != ipif->sctp_ipif_id &&
876*3510Svi117747 		    IN6_ARE_ADDR_EQUAL(&sipif->sctp_ipif_saddr,
877*3510Svi117747 		    &ipif->sctp_ipif_saddr) && sipif->sctp_ipif_refcnt > 0) {
878*3510Svi117747 			/*
879*3510Svi117747 			 * There can only be one address up at any time
880*3510Svi117747 			 * and we are here because ipif has been brought
881*3510Svi117747 			 * up.
882*3510Svi117747 			 */
883*3510Svi117747 			ASSERT(sipif->sctp_ipif_state != SCTP_IPIFS_UP);
884*3510Svi117747 			/*
885*3510Svi117747 			 * Someone has a reference to this we need to update to
886*3510Svi117747 			 * point to the new sipif.
887*3510Svi117747 			 */
888*3510Svi117747 			sctp_update_saddrs(sipif, ipif, hindex, sctps);
889*3510Svi117747 		}
890*3510Svi117747 		rw_exit(&sipif->sctp_ipif_lock);
891*3510Svi117747 		sipif = list_next(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
892*3510Svi117747 		    sipif);
893*3510Svi117747 	}
894*3510Svi117747 }
895*3510Svi117747 
896*3510Svi117747 /*
897*3510Svi117747  * Insert a new SCTP ipif using 'ipif'. v6addr is the address that existed
898*3510Svi117747  * prior to the current address in 'ipif'. Only when an existing address
899*3510Svi117747  * is changed on an IPIF, will v6addr be specified. If the IPIF already
900*3510Svi117747  * exists in the global SCTP ipif table, then we either removed it, if
901*3510Svi117747  * it doesn't have any existing reference, or mark it condemned otherwise.
902*3510Svi117747  * If an address is being brought up (IPIF_UP), then we need to scan
903*3510Svi117747  * the SCTP list to check if there is any SCTP that points to the *same*
904*3510Svi117747  * address on a different SCTP ipif and update in that case.
905*3510Svi117747  */
906*3510Svi117747 void
907*3510Svi117747 sctp_update_ipif_addr(ipif_t *ipif, in6_addr_t v6addr)
908*3510Svi117747 {
909*3510Svi117747 	ill_t		*ill = ipif->ipif_ill;
910*3510Svi117747 	int		i;
911*3510Svi117747 	sctp_ill_t	*sctp_ill;
912*3510Svi117747 	sctp_ill_t	*osctp_ill;
913*3510Svi117747 	sctp_ipif_t	*sctp_ipif = NULL;
914*3510Svi117747 	sctp_ipif_t	*osctp_ipif = NULL;
915*3510Svi117747 	uint_t		ill_index;
916*3510Svi117747 	int		hindex;
917*3510Svi117747 	sctp_stack_t	*sctps;
918*3510Svi117747 
919*3510Svi117747 
920*3510Svi117747 	sctps = ipif->ipif_ill->ill_ipst->ips_netstack->netstack_sctp;
921*3510Svi117747 
922*3510Svi117747 	/* Index for new address */
923*3510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr, ill->ill_isv6);
924*3510Svi117747 
925*3510Svi117747 	/*
926*3510Svi117747 	 * The address on this IPIF is changing, we need to look for
927*3510Svi117747 	 * this old address and mark it condemned, before creating
928*3510Svi117747 	 * one for the new address.
929*3510Svi117747 	 */
930*3510Svi117747 	osctp_ipif = sctp_lookup_ipif_addr(&v6addr, B_FALSE,
931*3510Svi117747 	    ipif->ipif_zoneid, B_TRUE, SCTP_ILL_TO_PHYINDEX(ill),
932*3510Svi117747 	    ipif->ipif_seqid, B_FALSE, sctps);
933*3510Svi117747 
934*3510Svi117747 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
935*3510Svi117747 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
936*3510Svi117747 
937*3510Svi117747 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
938*3510Svi117747 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
939*3510Svi117747 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
940*3510Svi117747 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
941*3510Svi117747 			break;
942*3510Svi117747 		sctp_ill = list_next(
943*3510Svi117747 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
944*3510Svi117747 	}
945*3510Svi117747 
946*3510Svi117747 	if (sctp_ill == NULL) {
947*3510Svi117747 		ip1dbg(("sctp_ipif_insert: ill not found ..\n"));
948*3510Svi117747 		rw_exit(&sctps->sctps_g_ipifs_lock);
949*3510Svi117747 		rw_exit(&sctps->sctps_g_ills_lock);
950*3510Svi117747 	}
951*3510Svi117747 
952*3510Svi117747 	if (osctp_ipif != NULL) {
953*3510Svi117747 
954*3510Svi117747 		/* The address is the same? */
955*3510Svi117747 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &v6addr)) {
956*3510Svi117747 			boolean_t	chk_n_updt = B_FALSE;
957*3510Svi117747 
958*3510Svi117747 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
959*3510Svi117747 			rw_enter(&osctp_ipif->sctp_ipif_lock, RW_WRITER);
960*3510Svi117747 			if (ipif->ipif_flags & IPIF_UP &&
961*3510Svi117747 			    osctp_ipif->sctp_ipif_state != SCTP_IPIFS_UP) {
962*3510Svi117747 				osctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
963*3510Svi117747 				chk_n_updt = B_TRUE;
964*3510Svi117747 			} else {
965*3510Svi117747 				osctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
966*3510Svi117747 			}
967*3510Svi117747 			osctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
968*3510Svi117747 			rw_exit(&osctp_ipif->sctp_ipif_lock);
969*3510Svi117747 			if (chk_n_updt) {
970*3510Svi117747 				sctp_chk_and_updt_saddr(hindex, osctp_ipif,
971*3510Svi117747 				    sctps);
972*3510Svi117747 			}
973*3510Svi117747 			rw_exit(&sctps->sctps_g_ipifs_lock);
974*3510Svi117747 			rw_exit(&sctps->sctps_g_ills_lock);
975*3510Svi117747 			return;
976*3510Svi117747 		}
977*3510Svi117747 		/*
978*3510Svi117747 		 * We are effectively removing this address from the ILL.
979*3510Svi117747 		 */
980*3510Svi117747 		if (osctp_ipif->sctp_ipif_refcnt != 0) {
981*3510Svi117747 			osctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
982*3510Svi117747 		} else {
983*3510Svi117747 			list_t		*ipif_list;
984*3510Svi117747 			int		ohindex;
985*3510Svi117747 
986*3510Svi117747 			osctp_ill = osctp_ipif->sctp_ipif_ill;
987*3510Svi117747 			/* hash index for the old one */
988*3510Svi117747 			ohindex = SCTP_IPIF_ADDR_HASH(
989*3510Svi117747 			    osctp_ipif->sctp_ipif_saddr,
990*3510Svi117747 			    osctp_ipif->sctp_ipif_isv6);
991*3510Svi117747 
992*3510Svi117747 			ipif_list =
993*3510Svi117747 			    &sctps->sctps_g_ipifs[ohindex].sctp_ipif_list;
994*3510Svi117747 
995*3510Svi117747 			list_remove(ipif_list, (void *)osctp_ipif);
996*3510Svi117747 			sctps->sctps_g_ipifs[ohindex].ipif_count--;
997*3510Svi117747 			sctps->sctps_g_ipifs_count--;
998*3510Svi117747 			rw_destroy(&osctp_ipif->sctp_ipif_lock);
999*3510Svi117747 			kmem_free(osctp_ipif, sizeof (sctp_ipif_t));
1000*3510Svi117747 			(void) atomic_add_32_nv(&osctp_ill->sctp_ill_ipifcnt,
1001*3510Svi117747 			    -1);
1002*3510Svi117747 		}
1003*3510Svi117747 	}
1004*3510Svi117747 
1005*3510Svi117747 	sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
1006*3510Svi117747 	/* Try again? */
1007*3510Svi117747 	if (sctp_ipif == NULL) {
1008*3510Svi117747 		ip1dbg(("sctp_ipif_insert: mem failure..\n"));
1009*3510Svi117747 		rw_exit(&sctps->sctps_g_ipifs_lock);
1010*3510Svi117747 		rw_exit(&sctps->sctps_g_ills_lock);
1011*3510Svi117747 		return;
1012*3510Svi117747 	}
1013*3510Svi117747 	sctps->sctps_g_ipifs_count++;
1014*3510Svi117747 	rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
1015*3510Svi117747 	sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
1016*3510Svi117747 	sctp_ipif->sctp_ipif_ill = sctp_ill;
1017*3510Svi117747 	sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
1018*3510Svi117747 	sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
1019*3510Svi117747 	sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
1020*3510Svi117747 	if (ipif->ipif_flags & IPIF_UP)
1021*3510Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
1022*3510Svi117747 	else
1023*3510Svi117747 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
1024*3510Svi117747 	sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1025*3510Svi117747 	/*
1026*3510Svi117747 	 * We add it to the head so that it is quicker to find good/recent
1027*3510Svi117747 	 * additions.
1028*3510Svi117747 	 */
1029*3510Svi117747 	list_insert_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
1030*3510Svi117747 	    (void *)sctp_ipif);
1031*3510Svi117747 	sctps->sctps_g_ipifs[hindex].ipif_count++;
1032*3510Svi117747 	atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
1033*3510Svi117747 	if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_UP)
1034*3510Svi117747 		sctp_chk_and_updt_saddr(hindex, sctp_ipif, sctps);
1035*3510Svi117747 	rw_exit(&sctps->sctps_g_ipifs_lock);
1036*3510Svi117747 	rw_exit(&sctps->sctps_g_ills_lock);
1037*3510Svi117747 }
1038*3510Svi117747 
10390Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
10400Sstevel@tonic-gate void
10410Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
10420Sstevel@tonic-gate {
10430Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
10440Sstevel@tonic-gate 	int		i;
10450Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
10460Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
10470Sstevel@tonic-gate 	uint_t		ill_index;
1048*3510Svi117747 	uint_t		hindex;
10493448Sdh155122 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
10503448Sdh155122 	sctp_stack_t	*sctps = ns->netstack_sctp;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
10530Sstevel@tonic-gate 
10543448Sdh155122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
10553448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
10583448Sdh155122 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
10593448Sdh155122 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
10600Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
10610Sstevel@tonic-gate 			break;
10623448Sdh155122 		sctp_ill = list_next(
10633448Sdh155122 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
10640Sstevel@tonic-gate 	}
10650Sstevel@tonic-gate 	if (sctp_ill == NULL) {
10663448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
10673448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
10680Sstevel@tonic-gate 		return;
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 
1071*3510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr,
1072*3510Svi117747 	    ipif->ipif_ill->ill_isv6);
1073*3510Svi117747 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
1074*3510Svi117747 	for (i = 0; i < sctps->sctps_g_ipifs[hindex].ipif_count; i++) {
1075*3510Svi117747 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid) {
1076*3510Svi117747 			ASSERT(IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1077*3510Svi117747 			    &ipif->ipif_v6lcl_addr));
10780Sstevel@tonic-gate 			break;
1079*3510Svi117747 		}
10803448Sdh155122 		sctp_ipif = list_next(
1081*3510Svi117747 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
10820Sstevel@tonic-gate 		    sctp_ipif);
10830Sstevel@tonic-gate 	}
1084*3510Svi117747 	if (sctp_ipif == NULL) {
10850Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
10863448Sdh155122 		rw_exit(&sctps->sctps_g_ipifs_lock);
10873448Sdh155122 		rw_exit(&sctps->sctps_g_ills_lock);
10880Sstevel@tonic-gate 		return;
10890Sstevel@tonic-gate 	}
1090*3510Svi117747 	ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
10910Sstevel@tonic-gate 	switch (op) {
10920Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
10930Sstevel@tonic-gate 	{
10940Sstevel@tonic-gate 		list_t		*ipif_list;
10950Sstevel@tonic-gate 		list_t		*ill_list;
10960Sstevel@tonic-gate 
10973448Sdh155122 		ill_list = &sctps->sctps_g_ills[ill_index].sctp_ill_list;
1098*3510Svi117747 		ipif_list = &sctps->sctps_g_ipifs[hindex].sctp_ipif_list;
10990Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
11000Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
11013448Sdh155122 			rw_exit(&sctps->sctps_g_ipifs_lock);
11023448Sdh155122 			rw_exit(&sctps->sctps_g_ills_lock);
11030Sstevel@tonic-gate 			return;
11040Sstevel@tonic-gate 		}
11050Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
1106*3510Svi117747 		sctps->sctps_g_ipifs[hindex].ipif_count--;
11073448Sdh155122 		sctps->sctps_g_ipifs_count--;
11080Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
11090Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
11100Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
11113448Sdh155122 		if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
11123448Sdh155122 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
11130Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
11140Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
11150Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
11163448Sdh155122 				sctps->sctps_ills_count--;
11173448Sdh155122 				sctps->sctps_g_ills[ill_index].ill_count--;
11180Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
11190Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
11200Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
11210Sstevel@tonic-gate 			}
11220Sstevel@tonic-gate 		}
11230Sstevel@tonic-gate 		break;
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	case SCTP_IPIF_UP:
11270Sstevel@tonic-gate 
11283448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
11290Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
11300Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
1131*3510Svi117747 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1132432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
11330Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
1134*3510Svi117747 		sctp_chk_and_updt_saddr(hindex, sctp_ipif,
1135*3510Svi117747 		    ipif->ipif_ill->ill_ipst->ips_netstack->netstack_sctp);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 		break;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
11400Sstevel@tonic-gate 
11413448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
11420Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
11430Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
11440Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
1145432Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
11460Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 		break;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
11510Sstevel@tonic-gate 
11523448Sdh155122 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
11530Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
11540Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
1155*3510Svi117747 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1156*3510Svi117747 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
11570Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		break;
11600Sstevel@tonic-gate 	}
11613448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
11623448Sdh155122 	rw_exit(&sctps->sctps_g_ills_lock);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate /*
11660Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
11670Sstevel@tonic-gate  * sctp locking by the caller.
11680Sstevel@tonic-gate  */
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate /* Remove a specific saddr from the list */
11710Sstevel@tonic-gate void
11720Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
11730Sstevel@tonic-gate {
11740Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
11750Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
11780Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
11810Sstevel@tonic-gate 
1182432Svi117747 	if (sctp->sctp_bound_to_all == 1)
11830Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
11860Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
11890Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /*
11930Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
11940Sstevel@tonic-gate  * Called with no locks held.
11950Sstevel@tonic-gate  */
11960Sstevel@tonic-gate void
11970Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
11980Sstevel@tonic-gate     boolean_t fanout_locked)
11990Sstevel@tonic-gate {
12000Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
12010Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
12020Sstevel@tonic-gate 	int			cnt;
12030Sstevel@tonic-gate 	in6_addr_t		addr;
12040Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
1205852Svi117747 	int			ifindex = 0;
12060Sstevel@tonic-gate 
1207852Svi117747 	ASSERT(sctp->sctp_nsaddrs >= addcnt);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	if (!fanout_locked) {
12100Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
12110Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
12120Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
12130Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
12140Sstevel@tonic-gate 	}
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
12170Sstevel@tonic-gate 		switch (sctp->sctp_family) {
12180Sstevel@tonic-gate 		case AF_INET:
12190Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
12200Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
12210Sstevel@tonic-gate 			break;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 		case AF_INET6:
12240Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
12250Sstevel@tonic-gate 			addr = sin6->sin6_addr;
1226852Svi117747 			ifindex = sin6->sin6_scope_id;
12270Sstevel@tonic-gate 			break;
12280Sstevel@tonic-gate 		}
1229*3510Svi117747 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
1230*3510Svi117747 		    sctp->sctp_zoneid, !sctp->sctp_connp->conn_allzones,
1231*3510Svi117747 		    ifindex, 0, B_TRUE, sctp->sctp_sctps);
12320Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
12330Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
12340Sstevel@tonic-gate 	}
1235432Svi117747 	if (sctp->sctp_bound_to_all == 1)
12360Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	if (!fanout_locked) {
12390Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
12400Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
12410Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
12420Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate /*
12470Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
12480Sstevel@tonic-gate  * Called with no locks held.
12490Sstevel@tonic-gate  */
12500Sstevel@tonic-gate sctp_saddr_ipif_t *
1251852Svi117747 sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
12520Sstevel@tonic-gate {
1253*3510Svi117747 	int			cnt;
1254*3510Svi117747 	sctp_saddr_ipif_t	*ipif_obj;
1255*3510Svi117747 	int			hindex;
12560Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
12570Sstevel@tonic-gate 
1258*3510Svi117747 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
1259*3510Svi117747 	if (sctp->sctp_saddrs[hindex].ipif_count == 0)
12600Sstevel@tonic-gate 		return (NULL);
12610Sstevel@tonic-gate 
1262*3510Svi117747 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
1263*3510Svi117747 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
1264*3510Svi117747 		sctp_ipif = ipif_obj->saddr_ipifp;
1265*3510Svi117747 		/*
1266*3510Svi117747 		 * Zone check shouldn't be needed.
1267*3510Svi117747 		 */
1268*3510Svi117747 		if (IN6_ARE_ADDR_EQUAL(addr, &sctp_ipif->sctp_ipif_saddr) &&
1269*3510Svi117747 		    (ifindex == 0 ||
1270*3510Svi117747 		    ifindex == sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
1271*3510Svi117747 		    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state)) {
1272*3510Svi117747 			return (ipif_obj);
1273*3510Svi117747 		}
1274*3510Svi117747 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
1275*3510Svi117747 		    ipif_obj);
1276*3510Svi117747 	}
1277*3510Svi117747 	return (NULL);
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate 
1280432Svi117747 /* Given an address, add it to the source address list */
1281432Svi117747 int
1282852Svi117747 sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1283432Svi117747 {
1284432Svi117747 	sctp_ipif_t		*sctp_ipif;
1285432Svi117747 
1286*3510Svi117747 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp->sctp_zoneid,
1287*3510Svi117747 	    !sctp->sctp_connp->conn_allzones, ifindex, 0, B_TRUE,
1288*3510Svi117747 	    sctp->sctp_sctps);
1289432Svi117747 	if (sctp_ipif == NULL)
1290432Svi117747 		return (EINVAL);
1291432Svi117747 
1292*3510Svi117747 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE,
1293*3510Svi117747 	    B_FALSE) != 0) {
1294432Svi117747 		SCTP_IPIF_REFRELE(sctp_ipif);
1295432Svi117747 		return (EINVAL);
1296432Svi117747 	}
1297432Svi117747 	return (0);
1298432Svi117747 }
1299432Svi117747 
1300432Svi117747 /*
1301432Svi117747  * Remove or mark as dontsrc addresses that are currently not part of the
1302432Svi117747  * association. One would delete addresses when processing an INIT and
1303432Svi117747  * mark as dontsrc when processing an INIT-ACK.
1304432Svi117747  */
1305432Svi117747 void
1306432Svi117747 sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete)
1307432Svi117747 {
1308432Svi117747 	int			i;
1309432Svi117747 	int			l;
1310432Svi117747 	sctp_saddr_ipif_t	*obj;
1311432Svi117747 	int			scanned = 0;
1312432Svi117747 	int			naddr;
1313432Svi117747 	int			nsaddr;
1314432Svi117747 
1315432Svi117747 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1316432Svi117747 
1317432Svi117747 	/*
1318432Svi117747 	 * Irregardless of the supported address in the INIT, v4
1319432Svi117747 	 * must be supported.
1320432Svi117747 	 */
1321432Svi117747 	if (sctp->sctp_family == AF_INET)
1322432Svi117747 		supp_af = PARM_SUPP_V4;
1323432Svi117747 
1324432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
1325432Svi117747 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1326432Svi117747 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1327432Svi117747 			continue;
1328432Svi117747 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1329432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1330432Svi117747 		for (l = 0; l < naddr; l++) {
1331432Svi117747 			sctp_ipif_t	*ipif;
1332432Svi117747 
1333432Svi117747 			ipif = obj->saddr_ipifp;
1334432Svi117747 			scanned++;
1335432Svi117747 
1336432Svi117747 			/*
1337432Svi117747 			 * Delete/mark dontsrc loopback/linklocal addresses and
1338432Svi117747 			 * unsupported address.
1339852Svi117747 			 * On a clustered node, we trust the clustering module
1340852Svi117747 			 * to do the right thing w.r.t loopback addresses, so
1341852Svi117747 			 * we ignore loopback addresses in this check.
1342432Svi117747 			 */
1343852Svi117747 			if ((SCTP_IS_IPIF_LOOPBACK(ipif) &&
1344852Svi117747 			    cl_sctp_check_addrs == NULL) ||
1345852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(ipif) ||
1346432Svi117747 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1347432Svi117747 				if (!delete) {
1348432Svi117747 					obj->saddr_ipif_unconfirmed = 1;
1349432Svi117747 					goto next_obj;
1350432Svi117747 				}
1351432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1352432Svi117747 					sctp->sctp_bound_to_all = 0;
1353432Svi117747 				if (scanned < nsaddr) {
1354432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1355432Svi117747 					    sctp_ipif_list, obj);
1356432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1357432Svi117747 					continue;
1358432Svi117747 				}
1359432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1360432Svi117747 			}
1361432Svi117747 	next_obj:
1362432Svi117747 			if (scanned >= nsaddr)
1363432Svi117747 				return;
1364432Svi117747 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1365432Svi117747 			    obj);
1366432Svi117747 		}
1367432Svi117747 	}
1368432Svi117747 }
1369432Svi117747 
1370432Svi117747 
13710Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
13720Sstevel@tonic-gate in6_addr_t
13730Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6)
13740Sstevel@tonic-gate {
13750Sstevel@tonic-gate 	int			i;
13760Sstevel@tonic-gate 	int			l;
13770Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
13780Sstevel@tonic-gate 	int			scanned = 0;
13790Sstevel@tonic-gate 	in6_addr_t		addr;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
13820Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
13830Sstevel@tonic-gate 			continue;
13840Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
13850Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
13860Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1389432Svi117747 			if (!SCTP_DONT_SRC(obj) &&
13900Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
1391432Svi117747 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
13920Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
13930Sstevel@tonic-gate 			}
13940Sstevel@tonic-gate 			scanned++;
13950Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
13960Sstevel@tonic-gate 				goto got_none;
13970Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
13980Sstevel@tonic-gate 			    obj);
13990Sstevel@tonic-gate 		}
14000Sstevel@tonic-gate 	}
14010Sstevel@tonic-gate got_none:
14020Sstevel@tonic-gate 	/* Need to double check this */
14030Sstevel@tonic-gate 	if (isv6 == B_TRUE)
14040Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
14050Sstevel@tonic-gate 	else
14060Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	return (addr);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate /*
14120Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
14130Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
14140Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
14150Sstevel@tonic-gate  */
14160Sstevel@tonic-gate int
14170Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate 	int			i;
14200Sstevel@tonic-gate 	int			l;
14210Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
14220Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
14230Sstevel@tonic-gate 	int			family = sctp->sctp_family;
14240Sstevel@tonic-gate 	int			max = *addrcnt;
14250Sstevel@tonic-gate 	size_t			added = 0;
14260Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
14270Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
14280Sstevel@tonic-gate 	int			scanned = 0;
14290Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
14320Sstevel@tonic-gate 		return (EINVAL);
14330Sstevel@tonic-gate 
1434852Svi117747 	/*
1435852Svi117747 	 * Skip loopback addresses for non-loopback assoc., ignore
1436852Svi117747 	 * this on a clustered node.
1437852Svi117747 	 */
1438852Svi117747 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback &&
1439852Svi117747 	    (cl_sctp_check_addrs == NULL)) {
14400Sstevel@tonic-gate 		skip_lback = B_TRUE;
1441852Svi117747 	}
1442852Svi117747 
14430Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
14440Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
14450Sstevel@tonic-gate 			continue;
14460Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
14470Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
14480Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
14490Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 			scanned++;
14520Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1453432Svi117747 			    SCTP_DONT_SRC(obj) ||
1454852Svi117747 			    (SCTP_IS_IPIF_LOOPBACK(ipif) && skip_lback)) {
14550Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
14560Sstevel@tonic-gate 					goto done;
14570Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
14580Sstevel@tonic-gate 				    sctp_ipif_list, obj);
14590Sstevel@tonic-gate 				continue;
14600Sstevel@tonic-gate 			}
14610Sstevel@tonic-gate 			switch (family) {
14620Sstevel@tonic-gate 			case AF_INET:
14630Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
14640Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
14650Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
14660Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
14670Sstevel@tonic-gate 				break;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 			case AF_INET6:
14700Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
14710Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
14720Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
14730Sstevel@tonic-gate 				sin6->sin6_addr = addr;
14740Sstevel@tonic-gate 				break;
14750Sstevel@tonic-gate 			}
14760Sstevel@tonic-gate 			added++;
14770Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
14780Sstevel@tonic-gate 				goto done;
14790Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
14800Sstevel@tonic-gate 			    obj);
14810Sstevel@tonic-gate 		}
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate done:
14840Sstevel@tonic-gate 	*addrcnt = added;
14850Sstevel@tonic-gate 	return (0);
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate /*
1489252Svi117747  * Given the supported address family, walk through the source address list
1490252Svi117747  * and return the total length of the available addresses. If 'p' is not
1491252Svi117747  * null, construct the parameter list for the addresses in 'p'.
1492432Svi117747  * 'modify' will only be set when we want the source address list to
1493432Svi117747  * be modified. The source address list will be modified only when
1494432Svi117747  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1495432Svi117747  * be false since the 'sctp' will be that of the listener.
14960Sstevel@tonic-gate  */
14970Sstevel@tonic-gate size_t
1498432Svi117747 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
14990Sstevel@tonic-gate {
15000Sstevel@tonic-gate 	int			i;
15010Sstevel@tonic-gate 	int			l;
15020Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1503252Svi117747 	size_t			paramlen = 0;
15040Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
15050Sstevel@tonic-gate 	int			scanned = 0;
1506432Svi117747 	int			naddr;
1507432Svi117747 	int			nsaddr;
1508852Svi117747 	boolean_t		del_ll = B_FALSE;
1509852Svi117747 	boolean_t		del_lb = B_FALSE;
1510852Svi117747 
15110Sstevel@tonic-gate 
1512852Svi117747 	/*
1513852Svi117747 	 * On a clustered node don't bother changing anything
1514852Svi117747 	 * on the loopback interface.
1515852Svi117747 	 */
1516852Svi117747 	if (modify && !sctp->sctp_loopback && (cl_sctp_check_addrs == NULL))
1517852Svi117747 		del_lb = B_TRUE;
1518852Svi117747 
1519852Svi117747 	if (modify && !sctp->sctp_linklocal)
1520852Svi117747 		del_ll = B_TRUE;
1521432Svi117747 
1522432Svi117747 	nsaddr = sctp->sctp_nsaddrs;
15230Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
15240Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
15250Sstevel@tonic-gate 			continue;
15260Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1527432Svi117747 		naddr = sctp->sctp_saddrs[i].ipif_count;
1528432Svi117747 		for (l = 0; l < naddr; l++) {
15290Sstevel@tonic-gate 			in6_addr_t	addr;
15300Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
1531852Svi117747 			boolean_t	ipif_lb;
1532852Svi117747 			boolean_t	ipif_ll;
1533432Svi117747 			boolean_t	unsupp_af;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
15360Sstevel@tonic-gate 			scanned++;
1537432Svi117747 
1538852Svi117747 			ipif_lb = SCTP_IS_IPIF_LOOPBACK(ipif);
1539852Svi117747 			ipif_ll = SCTP_IS_IPIF_LINKLOCAL(ipif);
1540432Svi117747 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1541432Svi117747 			/*
1542432Svi117747 			 * We need to either delete or skip loopback/linklocal
1543852Svi117747 			 * or unsupported addresses, if required.
1544432Svi117747 			 */
1545852Svi117747 			if ((ipif_ll && del_ll) || (ipif_lb && del_lb) ||
1546852Svi117747 			    (unsupp_af && modify)) {
1547432Svi117747 				if (sctp->sctp_bound_to_all == 1)
1548432Svi117747 					sctp->sctp_bound_to_all = 0;
1549432Svi117747 				if (scanned < nsaddr) {
1550432Svi117747 					obj = list_next(&sctp->sctp_saddrs[i].
1551432Svi117747 					    sctp_ipif_list, obj);
1552432Svi117747 					sctp_ipif_hash_remove(sctp, ipif);
1553432Svi117747 					continue;
1554432Svi117747 				}
1555432Svi117747 				sctp_ipif_hash_remove(sctp, ipif);
1556432Svi117747 				goto next_addr;
1557852Svi117747 			} else if (ipif_ll || unsupp_af ||
1558852Svi117747 			    (ipif_lb && (cl_sctp_check_addrs == NULL))) {
1559252Svi117747 				goto next_addr;
15600Sstevel@tonic-gate 			}
1561432Svi117747 
1562432Svi117747 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1563432Svi117747 				goto next_addr;
1564252Svi117747 			if (p != NULL)
1565252Svi117747 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
15660Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1567432Svi117747 			if (!ipif->sctp_ipif_isv6) {
15680Sstevel@tonic-gate 				struct in_addr	*v4;
15690Sstevel@tonic-gate 
1570252Svi117747 				if (p != NULL) {
1571252Svi117747 					hdr->sph_type = htons(PARM_ADDR4);
1572252Svi117747 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1573252Svi117747 					v4 = (struct in_addr *)(hdr + 1);
1574252Svi117747 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1575252Svi117747 				}
1576252Svi117747 				paramlen += PARM_ADDR4_LEN;
1577432Svi117747 			} else {
1578252Svi117747 				if (p != NULL) {
1579252Svi117747 					hdr->sph_type = htons(PARM_ADDR6);
1580252Svi117747 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1581252Svi117747 					bcopy(&addr, hdr + 1, sizeof (addr));
1582252Svi117747 				}
1583252Svi117747 				paramlen += PARM_ADDR6_LEN;
15840Sstevel@tonic-gate 			}
1585252Svi117747 next_addr:
1586432Svi117747 			if (scanned >= nsaddr)
1587252Svi117747 				return (paramlen);
15880Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
15890Sstevel@tonic-gate 			    obj);
15900Sstevel@tonic-gate 		}
15910Sstevel@tonic-gate 	}
1592252Svi117747 	return (paramlen);
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate 
1595852Svi117747 /*
1596852Svi117747  * This is used on a clustered node to obtain a list of addresses, the list
1597852Svi117747  * consists of sockaddr_in structs for v4 and sockaddr_in6 for v6. The list
1598852Svi117747  * is then passed onto the clustering module which sends back the correct
1599852Svi117747  * list based on the port info. Regardless of the input, i.e INADDR_ANY
1600852Svi117747  * or specific address(es), we create the list since it could be modified by
1601852Svi117747  * the clustering module. When given a list of addresses, we simply
1602852Svi117747  * create the list of sockaddr_in or sockaddr_in6 structs using those
1603852Svi117747  * addresses. If there is an INADDR_ANY in the input list, or if the
1604852Svi117747  * input is INADDR_ANY, we create a list of sockaddr_in or sockaddr_in6
1605852Svi117747  * structs consisting all the addresses in the global interface list
1606852Svi117747  * except those that are hosted on the loopback interface. We create
1607852Svi117747  * a list of sockaddr_in[6] structs just so that it can be directly input
1608852Svi117747  * to sctp_valid_addr_list() once the clustering module has processed it.
1609852Svi117747  */
1610852Svi117747 int
1611852Svi117747 sctp_get_addrlist(sctp_t *sctp, const void *addrs, uint32_t *addrcnt,
1612852Svi117747     uchar_t **addrlist, int *uspec, size_t *size)
1613852Svi117747 {
1614852Svi117747 	int			cnt;
1615852Svi117747 	int			icnt;
1616852Svi117747 	sctp_ipif_t		*sctp_ipif;
1617852Svi117747 	struct sockaddr_in	*s4;
1618852Svi117747 	struct sockaddr_in6	*s6;
1619852Svi117747 	uchar_t			*p;
1620852Svi117747 	int			err = 0;
16213448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
1622852Svi117747 
1623852Svi117747 	*addrlist = NULL;
1624852Svi117747 	*size = 0;
1625852Svi117747 
1626852Svi117747 	/*
1627852Svi117747 	 * Create a list of sockaddr_in[6] structs using the input list.
1628852Svi117747 	 */
1629852Svi117747 	if (sctp->sctp_family == AF_INET) {
1630852Svi117747 		*size = sizeof (struct sockaddr_in) * *addrcnt;
1631852Svi117747 		*addrlist = kmem_zalloc(*size,  KM_SLEEP);
1632852Svi117747 		p = *addrlist;
1633852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1634852Svi117747 			s4 = (struct sockaddr_in *)addrs + cnt;
1635852Svi117747 			/*
1636852Svi117747 			 * We need to create a list of all the available
1637852Svi117747 			 * addresses if there is an INADDR_ANY. However,
1638852Svi117747 			 * if we are beyond LISTEN, then this is invalid
1639852Svi117747 			 * (see sctp_valid_addr_list(). So, we just fail
1640852Svi117747 			 * it here rather than wait till it fails in
1641852Svi117747 			 * sctp_valid_addr_list().
1642852Svi117747 			 */
1643852Svi117747 			if (s4->sin_addr.s_addr == INADDR_ANY) {
1644852Svi117747 				kmem_free(*addrlist, *size);
1645852Svi117747 				*addrlist = NULL;
1646852Svi117747 				*size = 0;
1647852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1648852Svi117747 					*addrcnt = 0;
1649852Svi117747 					return (EINVAL);
1650852Svi117747 				}
1651852Svi117747 				if (uspec != NULL)
1652852Svi117747 					*uspec = 1;
1653852Svi117747 				goto get_all_addrs;
1654852Svi117747 			} else {
1655852Svi117747 				bcopy(s4, p, sizeof (*s4));
1656852Svi117747 				p += sizeof (*s4);
1657852Svi117747 			}
1658852Svi117747 		}
1659852Svi117747 	} else {
1660852Svi117747 		*size = sizeof (struct sockaddr_in6) * *addrcnt;
1661852Svi117747 		*addrlist = kmem_zalloc(*size, KM_SLEEP);
1662852Svi117747 		p = *addrlist;
1663852Svi117747 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1664852Svi117747 			s6 = (struct sockaddr_in6 *)addrs + cnt;
1665852Svi117747 			/*
1666852Svi117747 			 * Comments for INADDR_ANY, above, apply here too.
1667852Svi117747 			 */
1668852Svi117747 			if (IN6_IS_ADDR_UNSPECIFIED(&s6->sin6_addr)) {
1669852Svi117747 				kmem_free(*addrlist, *size);
1670852Svi117747 				*size = 0;
1671852Svi117747 				*addrlist = NULL;
1672852Svi117747 				if (sctp->sctp_state > SCTPS_LISTEN) {
1673852Svi117747 					*addrcnt = 0;
1674852Svi117747 					return (EINVAL);
1675852Svi117747 				}
1676852Svi117747 				if (uspec != NULL)
1677852Svi117747 					*uspec = 1;
1678852Svi117747 				goto get_all_addrs;
1679852Svi117747 			} else {
1680852Svi117747 				bcopy(addrs, p, sizeof (*s6));
1681852Svi117747 				p += sizeof (*s6);
1682852Svi117747 			}
1683852Svi117747 		}
1684852Svi117747 	}
1685852Svi117747 	return (err);
1686852Svi117747 get_all_addrs:
1687852Svi117747 
1688852Svi117747 	/*
1689852Svi117747 	 * Allocate max possible size. We allocate the max. size here because
1690852Svi117747 	 * the clustering module could end up adding addresses to the list.
1691852Svi117747 	 * We allocate upfront so that the clustering module need to bother
1692852Svi117747 	 * re-sizing the list.
1693852Svi117747 	 */
16943448Sdh155122 	if (sctp->sctp_family == AF_INET) {
16953448Sdh155122 		*size = sizeof (struct sockaddr_in) *
16963448Sdh155122 		    sctps->sctps_g_ipifs_count;
16973448Sdh155122 	} else {
16983448Sdh155122 		*size = sizeof (struct sockaddr_in6) *
16993448Sdh155122 		    sctps->sctps_g_ipifs_count;
17003448Sdh155122 	}
1701852Svi117747 	*addrlist = kmem_zalloc(*size, KM_SLEEP);
1702852Svi117747 	*addrcnt = 0;
1703852Svi117747 	p = *addrlist;
17043448Sdh155122 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
1705852Svi117747 
1706852Svi117747 	/*
1707852Svi117747 	 * Walk through the global interface list and add all addresses,
1708852Svi117747 	 * except those that are hosted on loopback interfaces.
1709852Svi117747 	 */
1710852Svi117747 	for (cnt = 0; cnt <  SCTP_IPIF_HASH; cnt++) {
17113448Sdh155122 		if (sctps->sctps_g_ipifs[cnt].ipif_count == 0)
1712852Svi117747 			continue;
17133448Sdh155122 		sctp_ipif = list_head(
17143448Sdh155122 		    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list);
17153448Sdh155122 		for (icnt = 0;
17163448Sdh155122 		    icnt < sctps->sctps_g_ipifs[cnt].ipif_count;
17173448Sdh155122 		    icnt++) {
1718852Svi117747 			in6_addr_t	addr;
1719852Svi117747 
1720852Svi117747 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1721852Svi117747 			addr = sctp_ipif->sctp_ipif_saddr;
1722852Svi117747 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
1723852Svi117747 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
1724852Svi117747 			    SCTP_IS_IPIF_LOOPBACK(sctp_ipif) ||
1725852Svi117747 			    SCTP_IS_IPIF_LINKLOCAL(sctp_ipif) ||
17262263Ssommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
1727852Svi117747 			    (sctp->sctp_ipversion == IPV4_VERSION &&
1728852Svi117747 			    sctp_ipif->sctp_ipif_isv6) ||
1729852Svi117747 			    (sctp->sctp_connp->conn_ipv6_v6only &&
1730852Svi117747 			    !sctp_ipif->sctp_ipif_isv6)) {
1731852Svi117747 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1732852Svi117747 				sctp_ipif = list_next(
17333448Sdh155122 				    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1734852Svi117747 				    sctp_ipif);
1735852Svi117747 				continue;
1736852Svi117747 			}
1737852Svi117747 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1738852Svi117747 			if (sctp->sctp_family == AF_INET) {
1739852Svi117747 				s4 = (struct sockaddr_in *)p;
1740852Svi117747 				IN6_V4MAPPED_TO_INADDR(&addr, &s4->sin_addr);
1741852Svi117747 				s4->sin_family = AF_INET;
1742852Svi117747 				p += sizeof (*s4);
1743852Svi117747 			} else {
1744852Svi117747 				s6 = (struct sockaddr_in6 *)p;
1745852Svi117747 				s6->sin6_addr = addr;
1746852Svi117747 				s6->sin6_family = AF_INET6;
1747852Svi117747 				s6->sin6_scope_id =
1748852Svi117747 				    sctp_ipif->sctp_ipif_ill->sctp_ill_index;
1749852Svi117747 				p += sizeof (*s6);
1750852Svi117747 			}
1751852Svi117747 			(*addrcnt)++;
17523448Sdh155122 			sctp_ipif = list_next(
17533448Sdh155122 			    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1754852Svi117747 			    sctp_ipif);
1755852Svi117747 		}
1756852Svi117747 	}
17573448Sdh155122 	rw_exit(&sctps->sctps_g_ipifs_lock);
1758852Svi117747 	return (err);
1759852Svi117747 }
1760852Svi117747 
1761852Svi117747 /*
1762852Svi117747  * Get a list of addresses from the source address list. The  caller is
1763852Svi117747  * responsible for allocating sufficient buffer for this.
1764852Svi117747  */
1765852Svi117747 void
1766852Svi117747 sctp_get_saddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1767852Svi117747 {
1768852Svi117747 	int			cnt;
1769852Svi117747 	int			icnt;
1770852Svi117747 	sctp_saddr_ipif_t	*obj;
1771852Svi117747 	int			naddr;
1772852Svi117747 	int			scanned = 0;
1773852Svi117747 
1774852Svi117747 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1775852Svi117747 		if (sctp->sctp_saddrs[cnt].ipif_count == 0)
1776852Svi117747 			continue;
1777852Svi117747 		obj = list_head(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
1778852Svi117747 		naddr = sctp->sctp_saddrs[cnt].ipif_count;
1779852Svi117747 		for (icnt = 0; icnt < naddr; icnt++) {
1780852Svi117747 			sctp_ipif_t	*ipif;
1781852Svi117747 
1782852Svi117747 			if (psize < sizeof (ipif->sctp_ipif_saddr))
1783852Svi117747 				return;
1784852Svi117747 
1785852Svi117747 			scanned++;
1786852Svi117747 			ipif = obj->saddr_ipifp;
1787852Svi117747 			bcopy(&ipif->sctp_ipif_saddr, p,
1788852Svi117747 			    sizeof (ipif->sctp_ipif_saddr));
1789852Svi117747 			p += sizeof (ipif->sctp_ipif_saddr);
1790852Svi117747 			psize -= sizeof (ipif->sctp_ipif_saddr);
1791852Svi117747 			if (scanned >= sctp->sctp_nsaddrs)
1792852Svi117747 				return;
17933448Sdh155122 			obj = list_next(
17943448Sdh155122 			    &sctp->sctp_saddrs[icnt].sctp_ipif_list,
1795852Svi117747 			    obj);
1796852Svi117747 		}
1797852Svi117747 	}
1798852Svi117747 }
1799852Svi117747 
1800852Svi117747 /*
1801852Svi117747  * Get a list of addresses from the remote address list. The  caller is
1802852Svi117747  * responsible for allocating sufficient buffer for this.
1803852Svi117747  */
1804852Svi117747 void
1805852Svi117747 sctp_get_faddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1806852Svi117747 {
1807852Svi117747 	sctp_faddr_t	*fp;
1808852Svi117747 
1809852Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1810852Svi117747 		if (psize < sizeof (fp->faddr))
1811852Svi117747 			return;
1812852Svi117747 		bcopy(&fp->faddr, p, sizeof (fp->faddr));
1813852Svi117747 		p += sizeof (fp->faddr);
1814852Svi117747 		psize -= sizeof (fp->faddr);
1815852Svi117747 	}
1816852Svi117747 }
18170Sstevel@tonic-gate 
18183448Sdh155122 static void
18193448Sdh155122 sctp_free_ills(sctp_stack_t *sctps)
18203448Sdh155122 {
18213448Sdh155122 	int			i;
18223448Sdh155122 	int			l;
18233448Sdh155122 	sctp_ill_t	*sctp_ill;
18243448Sdh155122 
18253448Sdh155122 	if (sctps->sctps_ills_count == 0)
18263448Sdh155122 		return;
18273448Sdh155122 
18283448Sdh155122 	for (i = 0; i < SCTP_ILL_HASH; i++) {
18293448Sdh155122 		sctp_ill = list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
18303448Sdh155122 		for (l = 0; l < sctps->sctps_g_ills[i].ill_count; l++) {
18313448Sdh155122 			ASSERT(sctp_ill->sctp_ill_ipifcnt == 0);
18323448Sdh155122 			list_remove(&sctps->sctps_g_ills[i].sctp_ill_list,
18333448Sdh155122 			    sctp_ill);
18343448Sdh155122 			sctps->sctps_ills_count--;
18353448Sdh155122 			kmem_free(sctp_ill->sctp_ill_name,
18363448Sdh155122 			    sctp_ill->sctp_ill_name_length);
18373448Sdh155122 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
18383448Sdh155122 			sctp_ill =
18393448Sdh155122 			    list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
18403448Sdh155122 		}
18413448Sdh155122 		sctps->sctps_g_ills[i].ill_count = 0;
18423448Sdh155122 	}
18433448Sdh155122 	ASSERT(sctps->sctps_ills_count == 0);
18443448Sdh155122 }
18453448Sdh155122 
18463448Sdh155122 static void
18473448Sdh155122 sctp_free_ipifs(sctp_stack_t *sctps)
18483448Sdh155122 {
18493448Sdh155122 	int			i;
18503448Sdh155122 	int			l;
18513448Sdh155122 	sctp_ipif_t	*sctp_ipif;
18523448Sdh155122 	sctp_ill_t	*sctp_ill;
18533448Sdh155122 
18543448Sdh155122 	if (sctps->sctps_g_ipifs_count == 0)
18553448Sdh155122 		return;
18563448Sdh155122 
18573448Sdh155122 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
18583448Sdh155122 		sctp_ipif = list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
18593448Sdh155122 		for (l = 0; l < sctps->sctps_g_ipifs[i].ipif_count; l++) {
18603448Sdh155122 			sctp_ill = sctp_ipif->sctp_ipif_ill;
18613448Sdh155122 
18623448Sdh155122 			list_remove(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
18633448Sdh155122 			    sctp_ipif);
18643448Sdh155122 			sctps->sctps_g_ipifs_count--;
18653448Sdh155122 			(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt,
18663448Sdh155122 			    -1);
18673448Sdh155122 			kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
18683448Sdh155122 			sctp_ipif =
18693448Sdh155122 			    list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
18703448Sdh155122 		}
18713448Sdh155122 		sctps->sctps_g_ipifs[i].ipif_count = 0;
18723448Sdh155122 	}
18733448Sdh155122 	ASSERT(sctps->sctps_g_ipifs_count == 0);
18743448Sdh155122 }
18753448Sdh155122 
18763448Sdh155122 
18770Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
18780Sstevel@tonic-gate void
18793448Sdh155122 sctp_saddr_init(sctp_stack_t *sctps)
18800Sstevel@tonic-gate {
18810Sstevel@tonic-gate 	int	i;
18820Sstevel@tonic-gate 
18833448Sdh155122 	sctps->sctps_g_ills = kmem_zalloc(sizeof (sctp_ill_hash_t) *
18843448Sdh155122 	    SCTP_ILL_HASH, KM_SLEEP);
18853448Sdh155122 	sctps->sctps_g_ipifs = kmem_zalloc(sizeof (sctp_ipif_hash_t) *
18863448Sdh155122 	    SCTP_IPIF_HASH, KM_SLEEP);
18873448Sdh155122 
18883448Sdh155122 	rw_init(&sctps->sctps_g_ills_lock, NULL, RW_DEFAULT, NULL);
18893448Sdh155122 	rw_init(&sctps->sctps_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
18923448Sdh155122 		sctps->sctps_g_ills[i].ill_count = 0;
18933448Sdh155122 		list_create(&sctps->sctps_g_ills[i].sctp_ill_list,
18943448Sdh155122 		    sizeof (sctp_ill_t),
18950Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
18983448Sdh155122 		sctps->sctps_g_ipifs[i].ipif_count = 0;
18993448Sdh155122 		list_create(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
19000Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
19010Sstevel@tonic-gate 	}
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate void
19053448Sdh155122 sctp_saddr_fini(sctp_stack_t *sctps)
19060Sstevel@tonic-gate {
19070Sstevel@tonic-gate 	int	i;
19080Sstevel@tonic-gate 
19093448Sdh155122 	sctp_free_ipifs(sctps);
19103448Sdh155122 	sctp_free_ills(sctps);
19113448Sdh155122 
19120Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
19133448Sdh155122 		list_destroy(&sctps->sctps_g_ills[i].sctp_ill_list);
19140Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
19153448Sdh155122 		list_destroy(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
19163448Sdh155122 
19173448Sdh155122 	ASSERT(sctps->sctps_ills_count == 0 && sctps->sctps_g_ipifs_count == 0);
19183448Sdh155122 	kmem_free(sctps->sctps_g_ills, sizeof (sctp_ill_hash_t) *
19193448Sdh155122 	    SCTP_ILL_HASH);
19203448Sdh155122 	sctps->sctps_g_ills = NULL;
19213448Sdh155122 	kmem_free(sctps->sctps_g_ipifs, sizeof (sctp_ipif_hash_t) *
19223448Sdh155122 	    SCTP_IPIF_HASH);
19233448Sdh155122 	sctps->sctps_g_ipifs = NULL;
19243448Sdh155122 	rw_destroy(&sctps->sctps_g_ills_lock);
19253448Sdh155122 	rw_destroy(&sctps->sctps_g_ipifs_lock);
19260Sstevel@tonic-gate }
1927