17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
226be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <sys/systm.h>
277c478bd9Sstevel@tonic-gate #include <sys/stream.h>
281d19ca10Svi #include <sys/cmn_err.h>
297c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
307c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/list.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <netinet/in.h>
377c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
387c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <inet/common.h>
417c478bd9Sstevel@tonic-gate #include <inet/ip.h>
427c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
43bd670b35SErik Nordmark #include <inet/ip_ire.h>
447c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
457c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
467c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
477c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
487c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
517c478bd9Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
52e35d2278Svi 			    zoneid_t, boolean_t, uint_t, uint_t, boolean_t,
53e35d2278Svi 			    sctp_stack_t *);
547c478bd9Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
55f551bb10Svi static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
56e35d2278Svi 			    boolean_t, boolean_t);
5792baa190SGeorge Shepherd static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *,
5892baa190SGeorge Shepherd 			    boolean_t);
59c31292eeSkcpoon static void		sctp_fix_saddr(sctp_t *, in6_addr_t *);
607c478bd9Sstevel@tonic-gate static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
617c478bd9Sstevel@tonic-gate 			    sctp_ipif_hash_t *);
627c478bd9Sstevel@tonic-gate static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
63f551bb10Svi 
64e35d2278Svi #define	SCTP_ADDR4_HASH(addr)	\
65e35d2278Svi 	(((addr) ^ ((addr) >> 8) ^ ((addr) >> 16) ^ ((addr) >> 24)) &	\
66e35d2278Svi 	(SCTP_IPIF_HASH - 1))
67e35d2278Svi 
68e35d2278Svi #define	SCTP_ADDR6_HASH(addr)	\
69e35d2278Svi 	(((addr).s6_addr32[3] ^						\
70e35d2278Svi 	(((addr).s6_addr32[3] ^ (addr).s6_addr32[2]) >> 12)) &		\
71e35d2278Svi 	(SCTP_IPIF_HASH - 1))
72e35d2278Svi 
73e35d2278Svi #define	SCTP_IPIF_ADDR_HASH(addr, isv6)					\
74e35d2278Svi 	((isv6) ? SCTP_ADDR6_HASH((addr)) : 				\
75e35d2278Svi 	SCTP_ADDR4_HASH((addr)._S6_un._S6_u32[3]))
76e35d2278Svi 
777c478bd9Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
787c478bd9Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
79f551bb10Svi 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
80f551bb10Svi 
81f551bb10Svi #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
82f551bb10Svi 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
83f551bb10Svi 
841d8c4025Svi #define	SCTP_IS_IPIF_LOOPBACK(ipif)		\
851d8c4025Svi 	((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK)
861d8c4025Svi 
871d8c4025Svi #define	SCTP_IS_IPIF_LINKLOCAL(ipif)		\
881d8c4025Svi 	((ipif)->sctp_ipif_isv6 && 		\
891d8c4025Svi 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr))
90f551bb10Svi 
91f551bb10Svi #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
92f551bb10Svi 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
93f551bb10Svi 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
947c478bd9Sstevel@tonic-gate 
955d0bc3edSsommerfe #define	SCTP_IPIF_ZONE_MATCH(sctp, ipif) 				\
965d0bc3edSsommerfe 	IPCL_ZONE_MATCH((sctp)->sctp_connp, (ipif)->sctp_ipif_zoneid)
975d0bc3edSsommerfe 
987c478bd9Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
997c478bd9Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
1077c478bd9Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static void
sctp_ipif_inactive(sctp_ipif_t * sctp_ipif)1107c478bd9Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
113e35d2278Svi 	uint_t		hindex;
1147c478bd9Sstevel@tonic-gate 	uint_t		ill_index;
115f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp_ipif->sctp_ipif_ill->
116f4b3ec61Sdh 	    sctp_ill_netstack->netstack_sctp;
1177c478bd9Sstevel@tonic-gate 
118f4b3ec61Sdh 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
119f4b3ec61Sdh 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
1207c478bd9Sstevel@tonic-gate 
121e35d2278Svi 	hindex = SCTP_IPIF_ADDR_HASH(sctp_ipif->sctp_ipif_saddr,
122e35d2278Svi 	    sctp_ipif->sctp_ipif_isv6);
123e35d2278Svi 
1247c478bd9Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
1257c478bd9Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
1267c478bd9Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
1277c478bd9Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
1287c478bd9Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
129f4b3ec61Sdh 		rw_exit(&sctps->sctps_g_ipifs_lock);
130f4b3ec61Sdh 		rw_exit(&sctps->sctps_g_ills_lock);
1317c478bd9Sstevel@tonic-gate 		return;
1327c478bd9Sstevel@tonic-gate 	}
133e35d2278Svi 	list_remove(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
134f4b3ec61Sdh 	    sctp_ipif);
135e35d2278Svi 	sctps->sctps_g_ipifs[hindex].ipif_count--;
136f4b3ec61Sdh 	sctps->sctps_g_ipifs_count--;
1377c478bd9Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
1387c478bd9Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1397c478bd9Sstevel@tonic-gate 
140*640c1670SJosef 'Jeff' Sipek 	atomic_dec_32(&sctp_ill->sctp_ill_ipifcnt);
141f4b3ec61Sdh 	if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
142f4b3ec61Sdh 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1437c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1447c478bd9Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
145f4b3ec61Sdh 			list_remove(&sctps->sctps_g_ills[ill_index].
146f4b3ec61Sdh 			    sctp_ill_list, (void *)sctp_ill);
147f4b3ec61Sdh 			sctps->sctps_g_ills[ill_index].ill_count--;
148f4b3ec61Sdh 			sctps->sctps_ills_count--;
1497c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
1507c478bd9Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
1517c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 	}
154f4b3ec61Sdh 	rw_exit(&sctps->sctps_g_ipifs_lock);
155f4b3ec61Sdh 	rw_exit(&sctps->sctps_g_ills_lock);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
160e35d2278Svi  * We are either looking for a IPIF with the given address before
161e35d2278Svi  * inserting it into the global list or looking for an IPIF for an
162e35d2278Svi  * address given an SCTP. In the former case we always check the zoneid,
163e35d2278Svi  * but for the latter case, check_zid could be B_FALSE if the connp
164e35d2278Svi  * for the sctp has conn_all_zones set. When looking for an address we
165e35d2278Svi  * give preference to one that is up, so even though we may find one that
166e35d2278Svi  * is not up we keep looking if there is one up, we hold the down addr
167e35d2278Svi  * in backup_ipif in case we don't find one that is up - i.e. we return
168e35d2278Svi  * the backup_ipif in that case. Note that if we are looking for. If we
169e35d2278Svi  * are specifically looking for an up address, then usable will be set
170e35d2278Svi  * to true.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate static sctp_ipif_t *
sctp_lookup_ipif_addr(in6_addr_t * addr,boolean_t refhold,zoneid_t zoneid,boolean_t check_zid,uint_t ifindex,uint_t seqid,boolean_t usable,sctp_stack_t * sctps)173e35d2278Svi sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid,
174e35d2278Svi     boolean_t check_zid, uint_t ifindex, uint_t seqid, boolean_t usable,
175e35d2278Svi     sctp_stack_t *sctps)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	int		j;
1787c478bd9Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
179e35d2278Svi 	sctp_ipif_t	*backup_ipif = NULL;
180e35d2278Svi 	int		hindex;
181e35d2278Svi 
182e35d2278Svi 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
1837c478bd9Sstevel@tonic-gate 
184f4b3ec61Sdh 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
185e35d2278Svi 	if (sctps->sctps_g_ipifs[hindex].ipif_count == 0) {
186e35d2278Svi 		rw_exit(&sctps->sctps_g_ipifs_lock);
187e35d2278Svi 		return (NULL);
188e35d2278Svi 	}
189e35d2278Svi 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
190e35d2278Svi 	for (j = 0; j < sctps->sctps_g_ipifs[hindex].ipif_count; j++) {
191e35d2278Svi 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
192e35d2278Svi 		if ((!check_zid ||
193e35d2278Svi 		    (sctp_ipif->sctp_ipif_zoneid == ALL_ZONES ||
194e35d2278Svi 		    zoneid == sctp_ipif->sctp_ipif_zoneid)) &&
195e35d2278Svi 		    (ifindex == 0 || ifindex ==
196e35d2278Svi 		    sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
197e35d2278Svi 		    ((seqid != 0 && seqid == sctp_ipif->sctp_ipif_id) ||
198e35d2278Svi 		    (IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
199e35d2278Svi 		    addr)))) {
200e35d2278Svi 			if (!usable || sctp_ipif->sctp_ipif_state ==
201e35d2278Svi 			    SCTP_IPIFS_UP) {
2027c478bd9Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2037c478bd9Sstevel@tonic-gate 				if (refhold)
2047c478bd9Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
205f4b3ec61Sdh 				rw_exit(&sctps->sctps_g_ipifs_lock);
2067c478bd9Sstevel@tonic-gate 				return (sctp_ipif);
207e35d2278Svi 			} else if (sctp_ipif->sctp_ipif_state ==
208e35d2278Svi 			    SCTP_IPIFS_DOWN && backup_ipif == NULL) {
209e35d2278Svi 				backup_ipif = sctp_ipif;
2107c478bd9Sstevel@tonic-gate 			}
2117c478bd9Sstevel@tonic-gate 		}
212e35d2278Svi 		rw_exit(&sctp_ipif->sctp_ipif_lock);
213e35d2278Svi 		sctp_ipif = list_next(
214e35d2278Svi 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list, sctp_ipif);
215e35d2278Svi 	}
216e35d2278Svi 	if (backup_ipif != NULL) {
217e35d2278Svi 		if (refhold)
218e35d2278Svi 			SCTP_IPIF_REFHOLD(backup_ipif);
219e35d2278Svi 		rw_exit(&sctps->sctps_g_ipifs_lock);
220e35d2278Svi 		return (backup_ipif);
2217c478bd9Sstevel@tonic-gate 	}
222f4b3ec61Sdh 	rw_exit(&sctps->sctps_g_ipifs_lock);
2237c478bd9Sstevel@tonic-gate 	return (NULL);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
2287c478bd9Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
2297c478bd9Sstevel@tonic-gate  * Called with no locks held.
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate static int
sctp_get_all_ipifs(sctp_t * sctp,int sleep)2327c478bd9Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
2357c478bd9Sstevel@tonic-gate 	int			i;
2367c478bd9Sstevel@tonic-gate 	int			j;
2377c478bd9Sstevel@tonic-gate 	int			error = 0;
238a22dfb13SVenugopal Iyer 	sctp_stack_t		*sctps = sctp->sctp_sctps;
239a22dfb13SVenugopal Iyer 	boolean_t		isv6;
240bd670b35SErik Nordmark 	conn_t			*connp = sctp->sctp_connp;
2417c478bd9Sstevel@tonic-gate 
242f4b3ec61Sdh 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
2437c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
244f4b3ec61Sdh 		if (sctps->sctps_g_ipifs[i].ipif_count == 0)
2457c478bd9Sstevel@tonic-gate 			continue;
246f4b3ec61Sdh 		sctp_ipif = list_head(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
247f4b3ec61Sdh 		for (j = 0; j < sctps->sctps_g_ipifs[i].ipif_count; j++) {
2487c478bd9Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
249a22dfb13SVenugopal Iyer 			isv6 = sctp_ipif->sctp_ipif_isv6;
250f551bb10Svi 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
2517c478bd9Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
2525d0bc3edSsommerfe 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
253a22dfb13SVenugopal Iyer 			    SCTP_IS_ADDR_UNSPEC(!isv6,
254a22dfb13SVenugopal Iyer 			    sctp_ipif->sctp_ipif_saddr) ||
255bd670b35SErik Nordmark 			    (connp->conn_family == AF_INET && isv6) ||
256bd670b35SErik Nordmark 			    (connp->conn_ipv6_v6only && !isv6)) {
2577c478bd9Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
2587c478bd9Sstevel@tonic-gate 				sctp_ipif = list_next(
259f4b3ec61Sdh 				    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
260f4b3ec61Sdh 				    sctp_ipif);
2617c478bd9Sstevel@tonic-gate 				continue;
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
2647c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
265f551bb10Svi 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
266e35d2278Svi 			    B_FALSE, B_FALSE);
267e35d2278Svi 			if (error != 0 && error != EALREADY)
2687c478bd9Sstevel@tonic-gate 				goto free_stuff;
269f4b3ec61Sdh 			sctp_ipif = list_next(
270f4b3ec61Sdh 			    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
2717c478bd9Sstevel@tonic-gate 			    sctp_ipif);
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 	}
274f4b3ec61Sdh 	rw_exit(&sctps->sctps_g_ipifs_lock);
2757c478bd9Sstevel@tonic-gate 	return (0);
2767c478bd9Sstevel@tonic-gate free_stuff:
277f4b3ec61Sdh 	rw_exit(&sctps->sctps_g_ipifs_lock);
2787c478bd9Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
2797c478bd9Sstevel@tonic-gate 	return (ENOMEM);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
2847c478bd9Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
2851d8c4025Svi  * or error. If the caller wants the list of addresses, it sends a pre-allocated
2861d8c4025Svi  * buffer - list. Currently, this list is only used on a clustered node when
2871d8c4025Svi  * the SCTP is in the listen state (from sctp_bind_add()). When called on a
2881d8c4025Svi  * clustered node, the input is always a list of addresses (even if the
2891d8c4025Svi  * original bind() was to INADDR_ANY).
2907c478bd9Sstevel@tonic-gate  * Called with no locks held.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate int
sctp_valid_addr_list(sctp_t * sctp,const void * addrs,uint32_t addrcnt,uchar_t * list,size_t lsize)2931d8c4025Svi sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
2941d8c4025Svi     uchar_t *list, size_t lsize)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2977c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2987c478bd9Sstevel@tonic-gate 	struct in_addr		*addr4;
2997c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
3007c478bd9Sstevel@tonic-gate 	int			cnt;
3017c478bd9Sstevel@tonic-gate 	int			err = 0;
3027c478bd9Sstevel@tonic-gate 	int			saddr_cnt = 0;
3037c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
3047c478bd9Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
3057c478bd9Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
3067c478bd9Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
3071d8c4025Svi 	uchar_t			*p = list;
308bd670b35SErik Nordmark 	conn_t			*connp = sctp->sctp_connp;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
3127c478bd9Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
3137c478bd9Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
3147c478bd9Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
3157c478bd9Sstevel@tonic-gate 	 * addresses.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
3187c478bd9Sstevel@tonic-gate 		check_lport = B_TRUE;
3197c478bd9Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
3207c478bd9Sstevel@tonic-gate 			check_addrs = B_TRUE;
3217c478bd9Sstevel@tonic-gate 	}
3221d8c4025Svi 
3237c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
3247c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
3257c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
3267c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
3277c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
3287c478bd9Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
3291d8c4025Svi 		uint_t		ifindex = 0;
3307c478bd9Sstevel@tonic-gate 
331bd670b35SErik Nordmark 		switch (connp->conn_family) {
3327c478bd9Sstevel@tonic-gate 		case AF_INET:
3337c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
3347c478bd9Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
335bd670b35SErik Nordmark 			    sin4->sin_port != connp->conn_lport)) {
3367c478bd9Sstevel@tonic-gate 				err = EINVAL;
3377c478bd9Sstevel@tonic-gate 				goto free_ret;
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
3407c478bd9Sstevel@tonic-gate 			if (check_addrs &&
3417c478bd9Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
3427c478bd9Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
343c4f4b3c8Skcpoon 			    CLASSD(addr4->s_addr))) {
3447c478bd9Sstevel@tonic-gate 				err = EINVAL;
3457c478bd9Sstevel@tonic-gate 				goto free_ret;
3467c478bd9Sstevel@tonic-gate 			}
3477c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
3487c478bd9Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
3497c478bd9Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3507c478bd9Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3517c478bd9Sstevel@tonic-gate 			}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 			break;
3547c478bd9Sstevel@tonic-gate 		case AF_INET6:
3557c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
3567c478bd9Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
357bd670b35SErik Nordmark 			    sin6->sin6_port != connp->conn_lport)) {
3587c478bd9Sstevel@tonic-gate 				err = EINVAL;
3597c478bd9Sstevel@tonic-gate 				goto free_ret;
3607c478bd9Sstevel@tonic-gate 			}
3617c478bd9Sstevel@tonic-gate 			addr = sin6->sin6_addr;
3621d8c4025Svi 			/* Contains the interface index */
3631d8c4025Svi 			ifindex = sin6->sin6_scope_id;
364bd670b35SErik Nordmark 			if (connp->conn_ipv6_v6only &&
3657c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
3667c478bd9Sstevel@tonic-gate 				err = EAFNOSUPPORT;
3677c478bd9Sstevel@tonic-gate 				goto free_ret;
3687c478bd9Sstevel@tonic-gate 			}
3697c478bd9Sstevel@tonic-gate 			if (check_addrs &&
3707c478bd9Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
3717c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
3727c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
3737c478bd9Sstevel@tonic-gate 				err = EINVAL;
3747c478bd9Sstevel@tonic-gate 				goto free_ret;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
3777c478bd9Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
3787c478bd9Sstevel@tonic-gate 				bind_to_all = B_TRUE;
3797c478bd9Sstevel@tonic-gate 			}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 			break;
3827c478bd9Sstevel@tonic-gate 		default:
3837c478bd9Sstevel@tonic-gate 			err = EAFNOSUPPORT;
3847c478bd9Sstevel@tonic-gate 			goto free_ret;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		if (lookup_saddr) {
387e35d2278Svi 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
388bd670b35SErik Nordmark 			    IPCL_ZONEID(connp), !connp->conn_allzones,
389e35d2278Svi 			    ifindex, 0, B_TRUE, sctp->sctp_sctps);
3907c478bd9Sstevel@tonic-gate 			if (ipif == NULL) {
3917c478bd9Sstevel@tonic-gate 				/* Address not in the list */
3927c478bd9Sstevel@tonic-gate 				err = EINVAL;
3937c478bd9Sstevel@tonic-gate 				goto free_ret;
3941d8c4025Svi 			} else if (check_addrs && SCTP_IS_IPIF_LOOPBACK(ipif) &&
3951d8c4025Svi 			    cl_sctp_check_addrs == NULL) {
3967c478bd9Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
3977c478bd9Sstevel@tonic-gate 				err = EINVAL;
3987c478bd9Sstevel@tonic-gate 				goto free_ret;
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 		if (!bind_to_all) {
402f551bb10Svi 			/*
403f551bb10Svi 			 * If an address is added after association setup,
404f551bb10Svi 			 * we need to wait for the peer to send us an ASCONF
405f551bb10Svi 			 * ACK before we can start using it.
406f551bb10Svi 			 * saddr_ipif_dontsrc will be reset (to 0) when we
407f551bb10Svi 			 * get the ASCONF ACK for this address.
408f551bb10Svi 			 */
409f551bb10Svi 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
410e35d2278Svi 			    check_addrs ? B_TRUE : B_FALSE, B_FALSE);
4117c478bd9Sstevel@tonic-gate 			if (err != 0) {
4127c478bd9Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
4137c478bd9Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
4147c478bd9Sstevel@tonic-gate 					err = EADDRINUSE;
4157c478bd9Sstevel@tonic-gate 				goto free_ret;
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 			saddr_cnt++;
4181d8c4025Svi 			if (lsize >= sizeof (addr)) {
4191d8c4025Svi 				bcopy(&addr, p, sizeof (addr));
4201d8c4025Svi 				p += sizeof (addr);
4211d8c4025Svi 				lsize -= sizeof (addr);
4221d8c4025Svi 			}
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	if (bind_to_all) {
4267c478bd9Sstevel@tonic-gate 		/*
4277c478bd9Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
4287c478bd9Sstevel@tonic-gate 		 * inaddr_any.
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
4317c478bd9Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
4327c478bd9Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
4357c478bd9Sstevel@tonic-gate 		if (err != 0)
4367c478bd9Sstevel@tonic-gate 			return (err);
4377c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4407c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4417c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4427c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4437c478bd9Sstevel@tonic-gate 	return (0);
4447c478bd9Sstevel@tonic-gate free_ret:
4457c478bd9Sstevel@tonic-gate 	if (saddr_cnt != 0)
4467c478bd9Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
4477c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
4487c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
4497c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
4507c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
4517c478bd9Sstevel@tonic-gate 	return (err);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static int
sctp_ipif_hash_insert(sctp_t * sctp,sctp_ipif_t * ipif,int sleep,boolean_t dontsrc,boolean_t allow_dup)455f551bb10Svi sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
456e35d2278Svi     boolean_t dontsrc, boolean_t allow_dup)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	int			cnt;
4597c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
460e35d2278Svi 	int			hindex;
461e35d2278Svi 
462e35d2278Svi 	hindex = SCTP_IPIF_ADDR_HASH(ipif->sctp_ipif_saddr,
463e35d2278Svi 	    ipif->sctp_ipif_isv6);
46492baa190SGeorge Shepherd 	rw_enter(&sctp->sctp_saddrs[hindex].ipif_hash_lock, RW_WRITER);
465e35d2278Svi 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
466e35d2278Svi 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
467e35d2278Svi 		if (IN6_ARE_ADDR_EQUAL(&ipif_obj->saddr_ipifp->sctp_ipif_saddr,
468e35d2278Svi 		    &ipif->sctp_ipif_saddr)) {
469e35d2278Svi 			if (ipif->sctp_ipif_id !=
470e35d2278Svi 			    ipif_obj->saddr_ipifp->sctp_ipif_id &&
471e35d2278Svi 			    ipif_obj->saddr_ipifp->sctp_ipif_state ==
472e35d2278Svi 			    SCTP_IPIFS_DOWN && ipif->sctp_ipif_state ==
473e35d2278Svi 			    SCTP_IPIFS_UP) {
474e35d2278Svi 				SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
475e35d2278Svi 				ipif_obj->saddr_ipifp = ipif;
476e35d2278Svi 				ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
47792baa190SGeorge Shepherd 				rw_exit(
47892baa190SGeorge Shepherd 				    &sctp->sctp_saddrs[hindex].ipif_hash_lock);
479e35d2278Svi 				return (0);
480e35d2278Svi 			} else if (!allow_dup || ipif->sctp_ipif_id ==
481e35d2278Svi 			    ipif_obj->saddr_ipifp->sctp_ipif_id) {
48292baa190SGeorge Shepherd 				rw_exit(
48392baa190SGeorge Shepherd 				    &sctp->sctp_saddrs[hindex].ipif_hash_lock);
484e35d2278Svi 				return (EALREADY);
485e35d2278Svi 			}
486e35d2278Svi 		}
487e35d2278Svi 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
4887c478bd9Sstevel@tonic-gate 		    ipif_obj);
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
4917c478bd9Sstevel@tonic-gate 	if (ipif_obj == NULL) {
49292baa190SGeorge Shepherd 		rw_exit(&sctp->sctp_saddrs[hindex].ipif_hash_lock);
4937c478bd9Sstevel@tonic-gate 		/* Need to do something */
4947c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
497f551bb10Svi 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
498e35d2278Svi 	list_insert_tail(&sctp->sctp_saddrs[hindex].sctp_ipif_list, ipif_obj);
499e35d2278Svi 	sctp->sctp_saddrs[hindex].ipif_count++;
5007c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
50192baa190SGeorge Shepherd 	rw_exit(&sctp->sctp_saddrs[hindex].ipif_hash_lock);
5027c478bd9Sstevel@tonic-gate 	return (0);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
505c31292eeSkcpoon /*
506c31292eeSkcpoon  * Given a source address, walk through the peer address list to see
507c31292eeSkcpoon  * if the source address is being used.  If it is, reset that.
508bd670b35SErik Nordmark  * A cleared saddr will then make sctp_make_mp lookup the destination again
509bd670b35SErik Nordmark  * and as part of that look for a new source.
510c31292eeSkcpoon  */
511c31292eeSkcpoon static void
sctp_fix_saddr(sctp_t * sctp,in6_addr_t * saddr)512c31292eeSkcpoon sctp_fix_saddr(sctp_t *sctp, in6_addr_t *saddr)
513c31292eeSkcpoon {
514c31292eeSkcpoon 	sctp_faddr_t	*fp;
515c31292eeSkcpoon 
5166be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->sf_next) {
5176be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (!IN6_ARE_ADDR_EQUAL(&fp->sf_saddr, saddr))
518c31292eeSkcpoon 			continue;
5196be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		V6_SET_ZERO(fp->sf_saddr);
520c31292eeSkcpoon 	}
521c31292eeSkcpoon }
522c31292eeSkcpoon 
5237c478bd9Sstevel@tonic-gate static void
sctp_ipif_hash_remove(sctp_t * sctp,sctp_ipif_t * ipif,boolean_t locked)52492baa190SGeorge Shepherd sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif, boolean_t locked)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	int			cnt;
5277c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
528e35d2278Svi 	int			hindex;
529e35d2278Svi 
530e35d2278Svi 	hindex = SCTP_IPIF_ADDR_HASH(ipif->sctp_ipif_saddr,
531e35d2278Svi 	    ipif->sctp_ipif_isv6);
53292baa190SGeorge Shepherd 	if (!locked)
53392baa190SGeorge Shepherd 		rw_enter(&sctp->sctp_saddrs[hindex].ipif_hash_lock, RW_WRITER);
534e35d2278Svi 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
535e35d2278Svi 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
536e35d2278Svi 		if (IN6_ARE_ADDR_EQUAL(&ipif_obj->saddr_ipifp->sctp_ipif_saddr,
537e35d2278Svi 		    &ipif->sctp_ipif_saddr)) {
538e35d2278Svi 			list_remove(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
5397c478bd9Sstevel@tonic-gate 			    ipif_obj);
540e35d2278Svi 			sctp->sctp_saddrs[hindex].ipif_count--;
5417c478bd9Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
542c31292eeSkcpoon 			sctp_fix_saddr(sctp, &ipif->sctp_ipif_saddr);
5437c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
5447c478bd9Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
5457c478bd9Sstevel@tonic-gate 			break;
5467c478bd9Sstevel@tonic-gate 		}
547e35d2278Svi 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
5487c478bd9Sstevel@tonic-gate 		    ipif_obj);
5497c478bd9Sstevel@tonic-gate 	}
55092baa190SGeorge Shepherd 	if (!locked)
55192baa190SGeorge Shepherd 		rw_exit(&sctp->sctp_saddrs[hindex].ipif_hash_lock);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate static int
sctp_compare_ipif_list(sctp_ipif_hash_t * list1,sctp_ipif_hash_t * list2)5557c478bd9Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	int			i;
5587c478bd9Sstevel@tonic-gate 	int			j;
5597c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
5607c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
5617c478bd9Sstevel@tonic-gate 	int			overlap = 0;
5627c478bd9Sstevel@tonic-gate 
56392baa190SGeorge Shepherd 	rw_enter(&list1->ipif_hash_lock, RW_READER);
56492baa190SGeorge Shepherd 	rw_enter(&list2->ipif_hash_lock, RW_READER);
5657c478bd9Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
5667c478bd9Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
5677c478bd9Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
5687c478bd9Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
569e35d2278Svi 			if (IN6_ARE_ADDR_EQUAL(
570e35d2278Svi 			    &obj1->saddr_ipifp->sctp_ipif_saddr,
571e35d2278Svi 			    &obj2->saddr_ipifp->sctp_ipif_saddr)) {
5727c478bd9Sstevel@tonic-gate 				overlap++;
5737c478bd9Sstevel@tonic-gate 				break;
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
5767c478bd9Sstevel@tonic-gate 			    obj2);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
5797c478bd9Sstevel@tonic-gate 	}
58092baa190SGeorge Shepherd 	rw_exit(&list1->ipif_hash_lock);
58192baa190SGeorge Shepherd 	rw_exit(&list2->ipif_hash_lock);
5827c478bd9Sstevel@tonic-gate 	return (overlap);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate int
sctp_compare_saddrs(sctp_t * sctp1,sctp_t * sctp2)5867c478bd9Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	int		i;
5897c478bd9Sstevel@tonic-gate 	int		overlap = 0;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
5927c478bd9Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
5937c478bd9Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
5977c478bd9Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
5987c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
6027c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if (overlap > 0)
6057c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate static int
sctp_copy_ipifs(sctp_ipif_hash_t * list1,sctp_t * sctp2,int sleep)6117c478bd9Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	int			i;
6147c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
6157c478bd9Sstevel@tonic-gate 	int			error = 0;
6167c478bd9Sstevel@tonic-gate 
61792baa190SGeorge Shepherd 	rw_enter(&list1->ipif_hash_lock, RW_READER);
6187c478bd9Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
6197c478bd9Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
6207c478bd9Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
621f551bb10Svi 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
622e35d2278Svi 		    B_FALSE, B_FALSE);
623e35d2278Svi 		ASSERT(error != EALREADY);
62492baa190SGeorge Shepherd 		if (error != 0) {
62592baa190SGeorge Shepherd 			rw_exit(&list1->ipif_hash_lock);
6267c478bd9Sstevel@tonic-gate 			return (error);
62792baa190SGeorge Shepherd 		}
6287c478bd9Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
6297c478bd9Sstevel@tonic-gate 	}
63092baa190SGeorge Shepherd 	rw_exit(&list1->ipif_hash_lock);
6317c478bd9Sstevel@tonic-gate 	return (error);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate int
sctp_dup_saddrs(sctp_t * sctp1,sctp_t * sctp2,int sleep)6357c478bd9Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	int	error = 0;
6387c478bd9Sstevel@tonic-gate 	int	i;
6397c478bd9Sstevel@tonic-gate 
640f551bb10Svi 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
6417c478bd9Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
64492baa190SGeorge Shepherd 		rw_enter(&sctp1->sctp_saddrs[i].ipif_hash_lock, RW_READER);
64592baa190SGeorge Shepherd 		if (sctp1->sctp_saddrs[i].ipif_count == 0) {
64692baa190SGeorge Shepherd 			rw_exit(&sctp1->sctp_saddrs[i].ipif_hash_lock);
6477c478bd9Sstevel@tonic-gate 			continue;
64892baa190SGeorge Shepherd 		}
6497c478bd9Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
6507c478bd9Sstevel@tonic-gate 		if (error != 0) {
65192baa190SGeorge Shepherd 			rw_exit(&sctp1->sctp_saddrs[i].ipif_hash_lock);
6527c478bd9Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
6537c478bd9Sstevel@tonic-gate 			return (error);
6547c478bd9Sstevel@tonic-gate 		}
65592baa190SGeorge Shepherd 		rw_exit(&sctp1->sctp_saddrs[i].ipif_hash_lock);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	return (0);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate void
sctp_free_saddrs(sctp_t * sctp)6617c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	int			i;
6647c478bd9Sstevel@tonic-gate 	int			l;
6657c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
6687c478bd9Sstevel@tonic-gate 		return;
6697c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
67092baa190SGeorge Shepherd 		rw_enter(&sctp->sctp_saddrs[i].ipif_hash_lock, RW_WRITER);
67192baa190SGeorge Shepherd 		if (sctp->sctp_saddrs[i].ipif_count == 0) {
67292baa190SGeorge Shepherd 			rw_exit(&sctp->sctp_saddrs[i].ipif_hash_lock);
6737c478bd9Sstevel@tonic-gate 			continue;
67492baa190SGeorge Shepherd 		}
6757c478bd9Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6767c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
6777c478bd9Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
6787c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
6797c478bd9Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
6807c478bd9Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
6817c478bd9Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
68492baa190SGeorge Shepherd 		rw_exit(&sctp->sctp_saddrs[i].ipif_hash_lock);
6857c478bd9Sstevel@tonic-gate 	}
686f551bb10Svi 	if (sctp->sctp_bound_to_all == 1)
687f551bb10Svi 		sctp->sctp_bound_to_all = 0;
6887c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
6937c478bd9Sstevel@tonic-gate  * held.
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate void
sctp_update_ill(ill_t * ill,int op)6967c478bd9Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	int		i;
6997c478bd9Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
7007c478bd9Sstevel@tonic-gate 	uint_t		index;
701