xref: /titanic_41/usr/src/uts/common/inet/sctp/sctp_addr.c (revision 56e2cc86321ec889bf83a888d902c60d6fb2ef8d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/systm.h>
28 #include <sys/stream.h>
29 #include <sys/cmn_err.h>
30 #include <sys/ddi.h>
31 #include <sys/sunddi.h>
32 #include <sys/kmem.h>
33 #include <sys/socket.h>
34 #include <sys/sysmacros.h>
35 #include <sys/list.h>
36 
37 #include <netinet/in.h>
38 #include <netinet/ip6.h>
39 #include <netinet/sctp.h>
40 
41 #include <inet/common.h>
42 #include <inet/ip.h>
43 #include <inet/ip6.h>
44 #include <inet/ip_if.h>
45 #include <inet/ipclassifier.h>
46 #include <inet/sctp_ip.h>
47 #include "sctp_impl.h"
48 #include "sctp_addr.h"
49 
50 static void		sctp_ipif_inactive(sctp_ipif_t *);
51 static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
52 			    zoneid_t, boolean_t, uint_t, uint_t, boolean_t,
53 			    sctp_stack_t *);
54 static int		sctp_get_all_ipifs(sctp_t *, int);
55 static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
56 			    boolean_t, boolean_t);
57 static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
58 static void		sctp_fix_saddr(sctp_t *, in6_addr_t *);
59 static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
60 			    sctp_ipif_hash_t *);
61 static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
62 
63 #define	SCTP_ADDR4_HASH(addr)	\
64 	(((addr) ^ ((addr) >> 8) ^ ((addr) >> 16) ^ ((addr) >> 24)) &	\
65 	(SCTP_IPIF_HASH - 1))
66 
67 #define	SCTP_ADDR6_HASH(addr)	\
68 	(((addr).s6_addr32[3] ^						\
69 	(((addr).s6_addr32[3] ^ (addr).s6_addr32[2]) >> 12)) &		\
70 	(SCTP_IPIF_HASH - 1))
71 
72 #define	SCTP_IPIF_ADDR_HASH(addr, isv6)					\
73 	((isv6) ? SCTP_ADDR6_HASH((addr)) : 				\
74 	SCTP_ADDR4_HASH((addr)._S6_un._S6_u32[3]))
75 
76 #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
77 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
78 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
79 
80 #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
81 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
82 
83 #define	SCTP_IS_IPIF_LOOPBACK(ipif)		\
84 	((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK)
85 
86 #define	SCTP_IS_IPIF_LINKLOCAL(ipif)		\
87 	((ipif)->sctp_ipif_isv6 && 		\
88 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr))
89 
90 #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
91 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
92 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
93 
94 #define	SCTP_IPIF_ZONE_MATCH(sctp, ipif) 				\
95 	IPCL_ZONE_MATCH((sctp)->sctp_connp, (ipif)->sctp_ipif_zoneid)
96 
97 #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
98 #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
99 
100 /*
101  * SCTP Interface list manipulation functions, locking used.
102  */
103 
104 /*
105  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
106  * marked as condemned. Also, check if the ILL needs to go away.
107  */
108 static void
109 sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
110 {
111 	sctp_ill_t	*sctp_ill;
112 	uint_t		hindex;
113 	uint_t		ill_index;
114 	sctp_stack_t	*sctps = sctp_ipif->sctp_ipif_ill->
115 	    sctp_ill_netstack->netstack_sctp;
116 
117 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
118 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
119 
120 	hindex = SCTP_IPIF_ADDR_HASH(sctp_ipif->sctp_ipif_saddr,
121 	    sctp_ipif->sctp_ipif_isv6);
122 
123 	sctp_ill = sctp_ipif->sctp_ipif_ill;
124 	ASSERT(sctp_ill != NULL);
125 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
126 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
127 	    sctp_ipif->sctp_ipif_refcnt != 0) {
128 		rw_exit(&sctps->sctps_g_ipifs_lock);
129 		rw_exit(&sctps->sctps_g_ills_lock);
130 		return;
131 	}
132 	list_remove(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
133 	    sctp_ipif);
134 	sctps->sctps_g_ipifs[hindex].ipif_count--;
135 	sctps->sctps_g_ipifs_count--;
136 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
137 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
138 
139 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
140 	if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
141 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
142 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
143 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
144 			list_remove(&sctps->sctps_g_ills[ill_index].
145 			    sctp_ill_list, (void *)sctp_ill);
146 			sctps->sctps_g_ills[ill_index].ill_count--;
147 			sctps->sctps_ills_count--;
148 			kmem_free(sctp_ill->sctp_ill_name,
149 			    sctp_ill->sctp_ill_name_length);
150 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
151 		}
152 	}
153 	rw_exit(&sctps->sctps_g_ipifs_lock);
154 	rw_exit(&sctps->sctps_g_ills_lock);
155 }
156 
157 /*
158  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
159  * We are either looking for a IPIF with the given address before
160  * inserting it into the global list or looking for an IPIF for an
161  * address given an SCTP. In the former case we always check the zoneid,
162  * but for the latter case, check_zid could be B_FALSE if the connp
163  * for the sctp has conn_all_zones set. When looking for an address we
164  * give preference to one that is up, so even though we may find one that
165  * is not up we keep looking if there is one up, we hold the down addr
166  * in backup_ipif in case we don't find one that is up - i.e. we return
167  * the backup_ipif in that case. Note that if we are looking for. If we
168  * are specifically looking for an up address, then usable will be set
169  * to true.
170  */
171 static sctp_ipif_t *
172 sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid,
173     boolean_t check_zid, uint_t ifindex, uint_t seqid, boolean_t usable,
174     sctp_stack_t *sctps)
175 {
176 	int		j;
177 	sctp_ipif_t	*sctp_ipif;
178 	sctp_ipif_t	*backup_ipif = NULL;
179 	int		hindex;
180 
181 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
182 
183 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
184 	if (sctps->sctps_g_ipifs[hindex].ipif_count == 0) {
185 		rw_exit(&sctps->sctps_g_ipifs_lock);
186 		return (NULL);
187 	}
188 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
189 	for (j = 0; j < sctps->sctps_g_ipifs[hindex].ipif_count; j++) {
190 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
191 		if ((!check_zid ||
192 		    (sctp_ipif->sctp_ipif_zoneid == ALL_ZONES ||
193 		    zoneid == sctp_ipif->sctp_ipif_zoneid)) &&
194 		    (ifindex == 0 || ifindex ==
195 		    sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
196 		    ((seqid != 0 && seqid == sctp_ipif->sctp_ipif_id) ||
197 		    (IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
198 		    addr)))) {
199 			if (!usable || sctp_ipif->sctp_ipif_state ==
200 			    SCTP_IPIFS_UP) {
201 				rw_exit(&sctp_ipif->sctp_ipif_lock);
202 				if (refhold)
203 					SCTP_IPIF_REFHOLD(sctp_ipif);
204 				rw_exit(&sctps->sctps_g_ipifs_lock);
205 				return (sctp_ipif);
206 			} else if (sctp_ipif->sctp_ipif_state ==
207 			    SCTP_IPIFS_DOWN && backup_ipif == NULL) {
208 				backup_ipif = sctp_ipif;
209 			}
210 		}
211 		rw_exit(&sctp_ipif->sctp_ipif_lock);
212 		sctp_ipif = list_next(
213 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list, sctp_ipif);
214 	}
215 	if (backup_ipif != NULL) {
216 		if (refhold)
217 			SCTP_IPIF_REFHOLD(backup_ipif);
218 		rw_exit(&sctps->sctps_g_ipifs_lock);
219 		return (backup_ipif);
220 	}
221 	rw_exit(&sctps->sctps_g_ipifs_lock);
222 	return (NULL);
223 }
224 
225 /*
226  * Populate the list with all the SCTP ipifs for a given ipversion.
227  * Increments sctp_ipif refcnt.
228  * Called with no locks held.
229  */
230 static int
231 sctp_get_all_ipifs(sctp_t *sctp, int sleep)
232 {
233 	sctp_ipif_t		*sctp_ipif;
234 	int			i;
235 	int			j;
236 	int			error = 0;
237 	sctp_stack_t		*sctps = sctp->sctp_sctps;
238 	boolean_t		isv6;
239 
240 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
241 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
242 		if (sctps->sctps_g_ipifs[i].ipif_count == 0)
243 			continue;
244 		sctp_ipif = list_head(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
245 		for (j = 0; j < sctps->sctps_g_ipifs[i].ipif_count; j++) {
246 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
247 			isv6 = sctp_ipif->sctp_ipif_isv6;
248 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
249 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
250 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
251 			    SCTP_IS_ADDR_UNSPEC(!isv6,
252 			    sctp_ipif->sctp_ipif_saddr) ||
253 			    (sctp->sctp_ipversion == IPV4_VERSION && isv6) ||
254 			    (sctp->sctp_connp->conn_ipv6_v6only && !isv6)) {
255 				rw_exit(&sctp_ipif->sctp_ipif_lock);
256 				sctp_ipif = list_next(
257 				    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
258 				    sctp_ipif);
259 				continue;
260 			}
261 			rw_exit(&sctp_ipif->sctp_ipif_lock);
262 			SCTP_IPIF_REFHOLD(sctp_ipif);
263 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep,
264 			    B_FALSE, B_FALSE);
265 			if (error != 0 && error != EALREADY)
266 				goto free_stuff;
267 			sctp_ipif = list_next(
268 			    &sctps->sctps_g_ipifs[i].sctp_ipif_list,
269 			    sctp_ipif);
270 		}
271 	}
272 	rw_exit(&sctps->sctps_g_ipifs_lock);
273 	return (0);
274 free_stuff:
275 	rw_exit(&sctps->sctps_g_ipifs_lock);
276 	sctp_free_saddrs(sctp);
277 	return (ENOMEM);
278 }
279 
280 /*
281  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
282  * are present in the SCTP interface list, return number of addresses filled
283  * or error. If the caller wants the list of addresses, it sends a pre-allocated
284  * buffer - list. Currently, this list is only used on a clustered node when
285  * the SCTP is in the listen state (from sctp_bind_add()). When called on a
286  * clustered node, the input is always a list of addresses (even if the
287  * original bind() was to INADDR_ANY).
288  * Called with no locks held.
289  */
290 int
291 sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
292     uchar_t *list, size_t lsize)
293 {
294 	struct sockaddr_in	*sin4;
295 	struct sockaddr_in6	*sin6;
296 	struct in_addr		*addr4;
297 	in6_addr_t		addr;
298 	int			cnt;
299 	int			err = 0;
300 	int			saddr_cnt = 0;
301 	sctp_ipif_t		*ipif;
302 	boolean_t		bind_to_all = B_FALSE;
303 	boolean_t		check_addrs = B_FALSE;
304 	boolean_t		check_lport = B_FALSE;
305 	uchar_t			*p = list;
306 
307 	/*
308 	 * Need to check for port and address depending on the state.
309 	 * After a socket is bound, we need to make sure that subsequent
310 	 * bindx() has correct port.  After an association is established,
311 	 * we need to check for changing the bound address to invalid
312 	 * addresses.
313 	 */
314 	if (sctp->sctp_state >= SCTPS_BOUND) {
315 		check_lport = B_TRUE;
316 		if (sctp->sctp_state > SCTPS_LISTEN)
317 			check_addrs = B_TRUE;
318 	}
319 
320 	if (sctp->sctp_conn_tfp != NULL)
321 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
322 	if (sctp->sctp_listen_tfp != NULL)
323 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
324 	for (cnt = 0; cnt < addrcnt; cnt++) {
325 		boolean_t	lookup_saddr = B_TRUE;
326 		uint_t		ifindex = 0;
327 
328 		switch (sctp->sctp_family) {
329 		case AF_INET:
330 			sin4 = (struct sockaddr_in *)addrs + cnt;
331 			if (sin4->sin_family != AF_INET || (check_lport &&
332 			    sin4->sin_port != sctp->sctp_lport)) {
333 				err = EINVAL;
334 				goto free_ret;
335 			}
336 			addr4 = &sin4->sin_addr;
337 			if (check_addrs &&
338 			    (addr4->s_addr == INADDR_ANY ||
339 			    addr4->s_addr == INADDR_BROADCAST ||
340 			    CLASSD(addr4->s_addr))) {
341 				err = EINVAL;
342 				goto free_ret;
343 			}
344 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
345 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
346 				lookup_saddr = B_FALSE;
347 				bind_to_all = B_TRUE;
348 			}
349 
350 			break;
351 		case AF_INET6:
352 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
353 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
354 			    sin6->sin6_port != sctp->sctp_lport)) {
355 				err = EINVAL;
356 				goto free_ret;
357 			}
358 			addr = sin6->sin6_addr;
359 			/* Contains the interface index */
360 			ifindex = sin6->sin6_scope_id;
361 			if (sctp->sctp_connp->conn_ipv6_v6only &&
362 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
363 				err = EAFNOSUPPORT;
364 				goto free_ret;
365 			}
366 			if (check_addrs &&
367 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
368 			    IN6_IS_ADDR_MULTICAST(&addr) ||
369 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
370 				err = EINVAL;
371 				goto free_ret;
372 			}
373 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
374 				lookup_saddr = B_FALSE;
375 				bind_to_all = B_TRUE;
376 			}
377 
378 			break;
379 		default:
380 			err = EAFNOSUPPORT;
381 			goto free_ret;
382 		}
383 		if (lookup_saddr) {
384 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
385 			    sctp->sctp_zoneid, !sctp->sctp_connp->conn_allzones,
386 			    ifindex, 0, B_TRUE, sctp->sctp_sctps);
387 			if (ipif == NULL) {
388 				/* Address not in the list */
389 				err = EINVAL;
390 				goto free_ret;
391 			} else if (check_addrs && SCTP_IS_IPIF_LOOPBACK(ipif) &&
392 			    cl_sctp_check_addrs == NULL) {
393 				SCTP_IPIF_REFRELE(ipif);
394 				err = EINVAL;
395 				goto free_ret;
396 			}
397 		}
398 		if (!bind_to_all) {
399 			/*
400 			 * If an address is added after association setup,
401 			 * we need to wait for the peer to send us an ASCONF
402 			 * ACK before we can start using it.
403 			 * saddr_ipif_dontsrc will be reset (to 0) when we
404 			 * get the ASCONF ACK for this address.
405 			 */
406 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP,
407 			    check_addrs ? B_TRUE : B_FALSE, B_FALSE);
408 			if (err != 0) {
409 				SCTP_IPIF_REFRELE(ipif);
410 				if (check_addrs && err == EALREADY)
411 					err = EADDRINUSE;
412 				goto free_ret;
413 			}
414 			saddr_cnt++;
415 			if (lsize >= sizeof (addr)) {
416 				bcopy(&addr, p, sizeof (addr));
417 				p += sizeof (addr);
418 				lsize -= sizeof (addr);
419 			}
420 		}
421 	}
422 	if (bind_to_all) {
423 		/*
424 		 * Free whatever we might have added before encountering
425 		 * inaddr_any.
426 		 */
427 		if (sctp->sctp_nsaddrs > 0) {
428 			sctp_free_saddrs(sctp);
429 			ASSERT(sctp->sctp_nsaddrs == 0);
430 		}
431 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
432 		if (err != 0)
433 			return (err);
434 		sctp->sctp_bound_to_all = 1;
435 	}
436 	if (sctp->sctp_listen_tfp != NULL)
437 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
438 	if (sctp->sctp_conn_tfp != NULL)
439 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
440 	return (0);
441 free_ret:
442 	if (saddr_cnt != 0)
443 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
444 	if (sctp->sctp_listen_tfp != NULL)
445 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
446 	if (sctp->sctp_conn_tfp != NULL)
447 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
448 	return (err);
449 }
450 
451 static int
452 sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep,
453     boolean_t dontsrc, boolean_t allow_dup)
454 {
455 	int			cnt;
456 	sctp_saddr_ipif_t	*ipif_obj;
457 	int			hindex;
458 
459 	hindex = SCTP_IPIF_ADDR_HASH(ipif->sctp_ipif_saddr,
460 	    ipif->sctp_ipif_isv6);
461 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
462 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
463 		if (IN6_ARE_ADDR_EQUAL(&ipif_obj->saddr_ipifp->sctp_ipif_saddr,
464 		    &ipif->sctp_ipif_saddr)) {
465 			if (ipif->sctp_ipif_id !=
466 			    ipif_obj->saddr_ipifp->sctp_ipif_id &&
467 			    ipif_obj->saddr_ipifp->sctp_ipif_state ==
468 			    SCTP_IPIFS_DOWN && ipif->sctp_ipif_state ==
469 			    SCTP_IPIFS_UP) {
470 				SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
471 				ipif_obj->saddr_ipifp = ipif;
472 				ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
473 				return (0);
474 			} else if (!allow_dup || ipif->sctp_ipif_id ==
475 			    ipif_obj->saddr_ipifp->sctp_ipif_id) {
476 				return (EALREADY);
477 			}
478 		}
479 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
480 		    ipif_obj);
481 	}
482 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
483 	if (ipif_obj == NULL) {
484 		/* Need to do something */
485 		return (ENOMEM);
486 	}
487 	ipif_obj->saddr_ipifp = ipif;
488 	ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0;
489 	list_insert_tail(&sctp->sctp_saddrs[hindex].sctp_ipif_list, ipif_obj);
490 	sctp->sctp_saddrs[hindex].ipif_count++;
491 	sctp->sctp_nsaddrs++;
492 	return (0);
493 }
494 
495 /*
496  * Given a source address, walk through the peer address list to see
497  * if the source address is being used.  If it is, reset that.
498  */
499 static void
500 sctp_fix_saddr(sctp_t *sctp, in6_addr_t *saddr)
501 {
502 	sctp_faddr_t	*fp;
503 
504 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
505 		if (!IN6_ARE_ADDR_EQUAL(&fp->saddr, saddr))
506 			continue;
507 		if (fp->ire != NULL) {
508 			IRE_REFRELE_NOTR(fp->ire);
509 			fp->ire = NULL;
510 		}
511 		V6_SET_ZERO(fp->saddr);
512 	}
513 }
514 
515 static void
516 sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
517 {
518 	int			cnt;
519 	sctp_saddr_ipif_t	*ipif_obj;
520 	int			hindex;
521 
522 	hindex = SCTP_IPIF_ADDR_HASH(ipif->sctp_ipif_saddr,
523 	    ipif->sctp_ipif_isv6);
524 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
525 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
526 		if (IN6_ARE_ADDR_EQUAL(&ipif_obj->saddr_ipifp->sctp_ipif_saddr,
527 		    &ipif->sctp_ipif_saddr)) {
528 			list_remove(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
529 			    ipif_obj);
530 			sctp->sctp_saddrs[hindex].ipif_count--;
531 			sctp->sctp_nsaddrs--;
532 			sctp_fix_saddr(sctp, &ipif->sctp_ipif_saddr);
533 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
534 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
535 			break;
536 		}
537 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
538 		    ipif_obj);
539 	}
540 }
541 
542 static int
543 sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
544 {
545 	int			i;
546 	int			j;
547 	sctp_saddr_ipif_t	*obj1;
548 	sctp_saddr_ipif_t	*obj2;
549 	int			overlap = 0;
550 
551 	obj1 = list_head(&list1->sctp_ipif_list);
552 	for (i = 0; i < list1->ipif_count; i++) {
553 		obj2 = list_head(&list2->sctp_ipif_list);
554 		for (j = 0; j < list2->ipif_count; j++) {
555 			if (IN6_ARE_ADDR_EQUAL(
556 			    &obj1->saddr_ipifp->sctp_ipif_saddr,
557 			    &obj2->saddr_ipifp->sctp_ipif_saddr)) {
558 				overlap++;
559 				break;
560 			}
561 			obj2 = list_next(&list2->sctp_ipif_list,
562 			    obj2);
563 		}
564 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
565 	}
566 	return (overlap);
567 }
568 
569 int
570 sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
571 {
572 	int		i;
573 	int		overlap = 0;
574 
575 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
576 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
577 		    &sctp2->sctp_saddrs[i]);
578 	}
579 
580 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
581 	    overlap == sctp1->sctp_nsaddrs) {
582 		return (SCTP_ADDR_EQUAL);
583 	}
584 
585 	if (overlap == sctp1->sctp_nsaddrs)
586 		return (SCTP_ADDR_SUBSET);
587 
588 	if (overlap > 0)
589 		return (SCTP_ADDR_OVERLAP);
590 
591 	return (SCTP_ADDR_DISJOINT);
592 }
593 
594 static int
595 sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
596 {
597 	int			i;
598 	sctp_saddr_ipif_t	*obj;
599 	int			error = 0;
600 
601 	obj = list_head(&list1->sctp_ipif_list);
602 	for (i = 0; i < list1->ipif_count; i++) {
603 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
604 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep,
605 		    B_FALSE, B_FALSE);
606 		ASSERT(error != EALREADY);
607 		if (error != 0)
608 			return (error);
609 		obj = list_next(&list1->sctp_ipif_list, obj);
610 	}
611 	return (error);
612 }
613 
614 int
615 sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
616 {
617 	int	error = 0;
618 	int	i;
619 
620 	if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1)
621 		return (sctp_get_all_ipifs(sctp2, sleep));
622 
623 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
624 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
625 			continue;
626 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
627 		if (error != 0) {
628 			sctp_free_saddrs(sctp2);
629 			return (error);
630 		}
631 	}
632 	return (0);
633 }
634 
635 void
636 sctp_free_saddrs(sctp_t *sctp)
637 {
638 	int			i;
639 	int			l;
640 	sctp_saddr_ipif_t	*obj;
641 
642 	if (sctp->sctp_nsaddrs == 0)
643 		return;
644 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
645 		if (sctp->sctp_saddrs[i].ipif_count == 0)
646 			continue;
647 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
648 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
649 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
650 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
651 			sctp->sctp_nsaddrs--;
652 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
653 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
654 		}
655 		sctp->sctp_saddrs[i].ipif_count = 0;
656 	}
657 	if (sctp->sctp_bound_to_all == 1)
658 		sctp->sctp_bound_to_all = 0;
659 	ASSERT(sctp->sctp_nsaddrs == 0);
660 }
661 
662 /*
663  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
664  * held.
665  */
666 void
667 sctp_update_ill(ill_t *ill, int op)
668 {
669 	int		i;
670 	sctp_ill_t	*sctp_ill = NULL;
671 	uint_t		index;
672 	netstack_t	*ns = ill->ill_ipst->ips_netstack;
673 	sctp_stack_t	*sctps = ns->netstack_sctp;
674 
675 	rw_enter(&sctps->sctps_g_ills_lock, RW_WRITER);
676 
677 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
678 	sctp_ill = list_head(&sctps->sctps_g_ills[index].sctp_ill_list);
679 	for (i = 0; i < sctps->sctps_g_ills[index].ill_count; i++) {
680 		if ((sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill)) &&
681 		    (sctp_ill->sctp_ill_isv6 == ill->ill_isv6)) {
682 			break;
683 		}
684 		sctp_ill = list_next(&sctps->sctps_g_ills[index].sctp_ill_list,
685 		    sctp_ill);
686 	}
687 
688 	switch (op) {
689 	case SCTP_ILL_INSERT:
690 		if (sctp_ill != NULL) {
691 			/* Unmark it if it is condemned */
692 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
693 				sctp_ill->sctp_ill_state = 0;
694 			rw_exit(&sctps->sctps_g_ills_lock);
695 			return;
696 		}
697 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
698 		/* Need to re-try? */
699 		if (sctp_ill == NULL) {
700 			cmn_err(CE_WARN, "sctp_update_ill: error adding "
701 			    "ILL %p to SCTP's ILL list", (void *)ill);
702 			rw_exit(&sctps->sctps_g_ills_lock);
703 			return;
704 		}
705 		sctp_ill->sctp_ill_name = kmem_zalloc(ill->ill_name_length,
706 		    KM_NOSLEEP);
707 		if (sctp_ill->sctp_ill_name == NULL) {
708 			cmn_err(CE_WARN, "sctp_update_ill: error adding "
709 			    "ILL %p to SCTP's ILL list", (void *)ill);
710 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
711 			rw_exit(&sctps->sctps_g_ills_lock);
712 			return;
713 		}
714 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
715 		    ill->ill_name_length);
716 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
717 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
718 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
719 		sctp_ill->sctp_ill_netstack = ns;	/* No netstack_hold */
720 		sctp_ill->sctp_ill_isv6 = ill->ill_isv6;
721 		list_insert_tail(&sctps->sctps_g_ills[index].sctp_ill_list,
722 		    (void *)sctp_ill);
723 		sctps->sctps_g_ills[index].ill_count++;
724 		sctps->sctps_ills_count++;
725 
726 		break;
727 
728 	case SCTP_ILL_REMOVE:
729 
730 		if (sctp_ill == NULL) {
731 			rw_exit(&sctps->sctps_g_ills_lock);
732 			return;
733 		}
734 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
735 			list_remove(&sctps->sctps_g_ills[index].sctp_ill_list,
736 			    (void *)sctp_ill);
737 			sctps->sctps_g_ills[index].ill_count--;
738 			sctps->sctps_ills_count--;
739 			kmem_free(sctp_ill->sctp_ill_name,
740 			    ill->ill_name_length);
741 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
742 		} else {
743 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
744 		}
745 
746 		break;
747 	}
748 	rw_exit(&sctps->sctps_g_ills_lock);
749 }
750 
751 /*
752  * The ILL's index is being changed, just remove it from the old list,
753  * change the SCTP ILL's index and re-insert using the new index.
754  */
755 void
756 sctp_ill_reindex(ill_t *ill, uint_t orig_ill_index)
757 {
758 	sctp_ill_t	*sctp_ill = NULL;
759 	sctp_ill_t	*nxt_sill;
760 	uint_t		indx;
761 	uint_t		nindx;
762 	boolean_t	once = B_FALSE;
763 	netstack_t	*ns = ill->ill_ipst->ips_netstack;
764 	sctp_stack_t	*sctps = ns->netstack_sctp;
765 
766 	rw_enter(&sctps->sctps_g_ills_lock, RW_WRITER);
767 
768 	indx = SCTP_ILL_HASH_FN(orig_ill_index);
769 	nindx = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
770 	sctp_ill = list_head(&sctps->sctps_g_ills[indx].sctp_ill_list);
771 	while (sctp_ill != NULL) {
772 		nxt_sill = list_next(&sctps->sctps_g_ills[indx].sctp_ill_list,
773 		    sctp_ill);
774 		if (sctp_ill->sctp_ill_index == orig_ill_index) {
775 			sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
776 			/*
777 			 * if the new index hashes to the same value, all's
778 			 * done.
779 			 */
780 			if (nindx != indx) {
781 				list_remove(
782 				    &sctps->sctps_g_ills[indx].sctp_ill_list,
783 				    (void *)sctp_ill);
784 				sctps->sctps_g_ills[indx].ill_count--;
785 				list_insert_tail(
786 				    &sctps->sctps_g_ills[nindx].sctp_ill_list,
787 				    (void *)sctp_ill);
788 				sctps->sctps_g_ills[nindx].ill_count++;
789 			}
790 			if (once)
791 				break;
792 			/* We might have one for v4 and for v6 */
793 			once = B_TRUE;
794 		}
795 		sctp_ill = nxt_sill;
796 	}
797 	rw_exit(&sctps->sctps_g_ills_lock);
798 }
799 
800 /* move ipif from f_ill to t_ill */
801 void
802 sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
803 {
804 	sctp_ill_t	*fsctp_ill = NULL;
805 	sctp_ill_t	*tsctp_ill = NULL;
806 	sctp_ipif_t	*sctp_ipif;
807 	uint_t		hindex;
808 	int		i;
809 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
810 	sctp_stack_t	*sctps = ns->netstack_sctp;
811 
812 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
813 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
814 
815 	hindex = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
816 	fsctp_ill = list_head(&sctps->sctps_g_ills[hindex].sctp_ill_list);
817 	for (i = 0; i < sctps->sctps_g_ills[hindex].ill_count; i++) {
818 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill) &&
819 		    fsctp_ill->sctp_ill_isv6 == f_ill->ill_isv6) {
820 			break;
821 		}
822 		fsctp_ill = list_next(
823 		    &sctps->sctps_g_ills[hindex].sctp_ill_list, fsctp_ill);
824 	}
825 
826 	hindex = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
827 	tsctp_ill = list_head(&sctps->sctps_g_ills[hindex].sctp_ill_list);
828 	for (i = 0; i < sctps->sctps_g_ills[hindex].ill_count; i++) {
829 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill) &&
830 		    tsctp_ill->sctp_ill_isv6 == t_ill->ill_isv6) {
831 			break;
832 		}
833 		tsctp_ill = list_next(
834 		    &sctps->sctps_g_ills[hindex].sctp_ill_list, tsctp_ill);
835 	}
836 
837 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr,
838 	    ipif->ipif_ill->ill_isv6);
839 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
840 	for (i = 0; i < sctps->sctps_g_ipifs[hindex].ipif_count; i++) {
841 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
842 			break;
843 		sctp_ipif = list_next(
844 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list, sctp_ipif);
845 	}
846 	/* Should be an ASSERT? */
847 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
848 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
849 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
850 		rw_exit(&sctps->sctps_g_ipifs_lock);
851 		rw_exit(&sctps->sctps_g_ills_lock);
852 		return;
853 	}
854 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
855 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
856 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
857 	rw_exit(&sctp_ipif->sctp_ipif_lock);
858 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
859 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
860 	rw_exit(&sctps->sctps_g_ipifs_lock);
861 	rw_exit(&sctps->sctps_g_ills_lock);
862 }
863 
864 /*
865  * Walk the list of SCTPs and find each that has oipif in it's saddr list, and
866  * if so replace it with nipif.
867  */
868 void
869 sctp_update_saddrs(sctp_ipif_t *oipif, sctp_ipif_t *nipif, int idx,
870     sctp_stack_t *sctps)
871 {
872 	sctp_t			*sctp;
873 	sctp_t			*sctp_prev = NULL;
874 	sctp_saddr_ipif_t	*sobj;
875 	int			count;
876 
877 	sctp = sctps->sctps_gsctp;
878 	mutex_enter(&sctps->sctps_g_lock);
879 	while (sctp != NULL && oipif->sctp_ipif_refcnt > 0) {
880 		mutex_enter(&sctp->sctp_reflock);
881 		if (sctp->sctp_condemned ||
882 		    sctp->sctp_saddrs[idx].ipif_count <= 0) {
883 			mutex_exit(&sctp->sctp_reflock);
884 			sctp = list_next(&sctps->sctps_g_list, sctp);
885 			continue;
886 		}
887 		sctp->sctp_refcnt++;
888 		mutex_exit(&sctp->sctp_reflock);
889 		mutex_exit(&sctps->sctps_g_lock);
890 		if (sctp_prev != NULL)
891 			SCTP_REFRELE(sctp_prev);
892 
893 		RUN_SCTP(sctp);
894 		sobj = list_head(&sctp->sctp_saddrs[idx].sctp_ipif_list);
895 		for (count = 0; count <
896 		    sctp->sctp_saddrs[idx].ipif_count; count++) {
897 			if (sobj->saddr_ipifp == oipif) {
898 				SCTP_IPIF_REFHOLD(nipif);
899 				sobj->saddr_ipifp = nipif;
900 				ASSERT(oipif->sctp_ipif_refcnt > 0);
901 				/* We have the writer lock */
902 				oipif->sctp_ipif_refcnt--;
903 				/*
904 				 * Can't have more than one referring
905 				 * to the same sctp_ipif.
906 				 */
907 				break;
908 			}
909 			sobj = list_next(&sctp->sctp_saddrs[idx].sctp_ipif_list,
910 			    sobj);
911 		}
912 		WAKE_SCTP(sctp);
913 		sctp_prev = sctp;
914 		mutex_enter(&sctps->sctps_g_lock);
915 		sctp = list_next(&sctps->sctps_g_list, sctp);
916 	}
917 	mutex_exit(&sctps->sctps_g_lock);
918 	if (sctp_prev != NULL)
919 		SCTP_REFRELE(sctp_prev);
920 }
921 
922 /*
923  * Given an ipif, walk the hash list in the global ipif table and for
924  * any other SCTP ipif with the same address and non-zero reference, walk
925  * the SCTP list and update the saddr list, if required, to point to the
926  * new SCTP ipif.
927  */
928 void
929 sctp_chk_and_updt_saddr(int hindex, sctp_ipif_t *ipif, sctp_stack_t *sctps)
930 {
931 	int		cnt;
932 	sctp_ipif_t	*sipif;
933 
934 	ASSERT(sctps->sctps_g_ipifs[hindex].ipif_count > 0);
935 	ASSERT(ipif->sctp_ipif_state == SCTP_IPIFS_UP);
936 
937 	sipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
938 	for (cnt = 0; cnt < sctps->sctps_g_ipifs[hindex].ipif_count; cnt++) {
939 		rw_enter(&sipif->sctp_ipif_lock, RW_WRITER);
940 		if (sipif->sctp_ipif_id != ipif->sctp_ipif_id &&
941 		    IN6_ARE_ADDR_EQUAL(&sipif->sctp_ipif_saddr,
942 		    &ipif->sctp_ipif_saddr) && sipif->sctp_ipif_refcnt > 0) {
943 			/*
944 			 * There can only be one address up at any time
945 			 * and we are here because ipif has been brought
946 			 * up.
947 			 */
948 			ASSERT(sipif->sctp_ipif_state != SCTP_IPIFS_UP);
949 			/*
950 			 * Someone has a reference to this we need to update to
951 			 * point to the new sipif.
952 			 */
953 			sctp_update_saddrs(sipif, ipif, hindex, sctps);
954 		}
955 		rw_exit(&sipif->sctp_ipif_lock);
956 		sipif = list_next(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
957 		    sipif);
958 	}
959 }
960 
961 /*
962  * Insert a new SCTP ipif using 'ipif'. v6addr is the address that existed
963  * prior to the current address in 'ipif'. Only when an existing address
964  * is changed on an IPIF, will v6addr be specified. If the IPIF already
965  * exists in the global SCTP ipif table, then we either removed it, if
966  * it doesn't have any existing reference, or mark it condemned otherwise.
967  * If an address is being brought up (IPIF_UP), then we need to scan
968  * the SCTP list to check if there is any SCTP that points to the *same*
969  * address on a different SCTP ipif and update in that case.
970  */
971 void
972 sctp_update_ipif_addr(ipif_t *ipif, in6_addr_t v6addr)
973 {
974 	ill_t		*ill = ipif->ipif_ill;
975 	int		i;
976 	sctp_ill_t	*sctp_ill;
977 	sctp_ill_t	*osctp_ill;
978 	sctp_ipif_t	*sctp_ipif = NULL;
979 	sctp_ipif_t	*osctp_ipif = NULL;
980 	uint_t		ill_index;
981 	int		hindex;
982 	sctp_stack_t	*sctps;
983 
984 	sctps = ipif->ipif_ill->ill_ipst->ips_netstack->netstack_sctp;
985 
986 	/* Index for new address */
987 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr, ill->ill_isv6);
988 
989 	/*
990 	 * The address on this IPIF is changing, we need to look for
991 	 * this old address and mark it condemned, before creating
992 	 * one for the new address.
993 	 */
994 	osctp_ipif = sctp_lookup_ipif_addr(&v6addr, B_FALSE,
995 	    ipif->ipif_zoneid, B_TRUE, SCTP_ILL_TO_PHYINDEX(ill),
996 	    ipif->ipif_seqid, B_FALSE, sctps);
997 
998 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
999 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
1000 
1001 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
1002 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
1003 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
1004 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill) &&
1005 		    sctp_ill->sctp_ill_isv6 == ill->ill_isv6) {
1006 			break;
1007 		}
1008 		sctp_ill = list_next(
1009 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
1010 	}
1011 
1012 	if (sctp_ill == NULL) {
1013 		ip1dbg(("sctp_update_ipif_addr: ill not found ..\n"));
1014 		rw_exit(&sctps->sctps_g_ipifs_lock);
1015 		rw_exit(&sctps->sctps_g_ills_lock);
1016 		return;
1017 	}
1018 
1019 	if (osctp_ipif != NULL) {
1020 
1021 		/* The address is the same? */
1022 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &v6addr)) {
1023 			boolean_t	chk_n_updt = B_FALSE;
1024 
1025 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
1026 			rw_enter(&osctp_ipif->sctp_ipif_lock, RW_WRITER);
1027 			if (ipif->ipif_flags & IPIF_UP &&
1028 			    osctp_ipif->sctp_ipif_state != SCTP_IPIFS_UP) {
1029 				osctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
1030 				chk_n_updt = B_TRUE;
1031 			} else {
1032 				osctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
1033 			}
1034 			osctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1035 			rw_exit(&osctp_ipif->sctp_ipif_lock);
1036 			if (chk_n_updt) {
1037 				sctp_chk_and_updt_saddr(hindex, osctp_ipif,
1038 				    sctps);
1039 			}
1040 			rw_exit(&sctps->sctps_g_ipifs_lock);
1041 			rw_exit(&sctps->sctps_g_ills_lock);
1042 			return;
1043 		}
1044 		/*
1045 		 * We are effectively removing this address from the ILL.
1046 		 */
1047 		if (osctp_ipif->sctp_ipif_refcnt != 0) {
1048 			osctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
1049 		} else {
1050 			list_t		*ipif_list;
1051 			int		ohindex;
1052 
1053 			osctp_ill = osctp_ipif->sctp_ipif_ill;
1054 			/* hash index for the old one */
1055 			ohindex = SCTP_IPIF_ADDR_HASH(
1056 			    osctp_ipif->sctp_ipif_saddr,
1057 			    osctp_ipif->sctp_ipif_isv6);
1058 
1059 			ipif_list =
1060 			    &sctps->sctps_g_ipifs[ohindex].sctp_ipif_list;
1061 
1062 			list_remove(ipif_list, (void *)osctp_ipif);
1063 			sctps->sctps_g_ipifs[ohindex].ipif_count--;
1064 			sctps->sctps_g_ipifs_count--;
1065 			rw_destroy(&osctp_ipif->sctp_ipif_lock);
1066 			kmem_free(osctp_ipif, sizeof (sctp_ipif_t));
1067 			(void) atomic_add_32_nv(&osctp_ill->sctp_ill_ipifcnt,
1068 			    -1);
1069 		}
1070 	}
1071 
1072 	sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
1073 	/* Try again? */
1074 	if (sctp_ipif == NULL) {
1075 		cmn_err(CE_WARN, "sctp_update_ipif_addr: error adding "
1076 		    "IPIF %p to SCTP's IPIF list", (void *)ipif);
1077 		rw_exit(&sctps->sctps_g_ipifs_lock);
1078 		rw_exit(&sctps->sctps_g_ills_lock);
1079 		return;
1080 	}
1081 	sctps->sctps_g_ipifs_count++;
1082 	rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
1083 	sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
1084 	sctp_ipif->sctp_ipif_ill = sctp_ill;
1085 	sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
1086 	sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
1087 	sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
1088 	if (ipif->ipif_flags & IPIF_UP)
1089 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
1090 	else
1091 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
1092 	sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1093 	/*
1094 	 * We add it to the head so that it is quicker to find good/recent
1095 	 * additions.
1096 	 */
1097 	list_insert_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
1098 	    (void *)sctp_ipif);
1099 	sctps->sctps_g_ipifs[hindex].ipif_count++;
1100 	atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
1101 	if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_UP)
1102 		sctp_chk_and_updt_saddr(hindex, sctp_ipif, sctps);
1103 	rw_exit(&sctps->sctps_g_ipifs_lock);
1104 	rw_exit(&sctps->sctps_g_ills_lock);
1105 }
1106 
1107 /* Insert, Remove,  Mark up or Mark down the ipif */
1108 void
1109 sctp_update_ipif(ipif_t *ipif, int op)
1110 {
1111 	ill_t		*ill = ipif->ipif_ill;
1112 	int		i;
1113 	sctp_ill_t	*sctp_ill;
1114 	sctp_ipif_t	*sctp_ipif;
1115 	uint_t		ill_index;
1116 	uint_t		hindex;
1117 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
1118 	sctp_stack_t	*sctps = ns->netstack_sctp;
1119 
1120 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
1121 
1122 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
1123 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
1124 
1125 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
1126 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
1127 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
1128 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill) &&
1129 		    sctp_ill->sctp_ill_isv6 == ill->ill_isv6) {
1130 			break;
1131 		}
1132 		sctp_ill = list_next(
1133 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
1134 	}
1135 	if (sctp_ill == NULL) {
1136 		rw_exit(&sctps->sctps_g_ipifs_lock);
1137 		rw_exit(&sctps->sctps_g_ills_lock);
1138 		return;
1139 	}
1140 
1141 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr,
1142 	    ipif->ipif_ill->ill_isv6);
1143 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
1144 	for (i = 0; i < sctps->sctps_g_ipifs[hindex].ipif_count; i++) {
1145 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid) {
1146 			ASSERT(IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1147 			    &ipif->ipif_v6lcl_addr));
1148 			break;
1149 		}
1150 		sctp_ipif = list_next(
1151 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
1152 		    sctp_ipif);
1153 	}
1154 	if (sctp_ipif == NULL) {
1155 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
1156 		rw_exit(&sctps->sctps_g_ipifs_lock);
1157 		rw_exit(&sctps->sctps_g_ills_lock);
1158 		return;
1159 	}
1160 	ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
1161 	switch (op) {
1162 	case SCTP_IPIF_REMOVE:
1163 	{
1164 		list_t		*ipif_list;
1165 		list_t		*ill_list;
1166 
1167 		ill_list = &sctps->sctps_g_ills[ill_index].sctp_ill_list;
1168 		ipif_list = &sctps->sctps_g_ipifs[hindex].sctp_ipif_list;
1169 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
1170 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
1171 			rw_exit(&sctps->sctps_g_ipifs_lock);
1172 			rw_exit(&sctps->sctps_g_ills_lock);
1173 			return;
1174 		}
1175 		list_remove(ipif_list, (void *)sctp_ipif);
1176 		sctps->sctps_g_ipifs[hindex].ipif_count--;
1177 		sctps->sctps_g_ipifs_count--;
1178 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
1179 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1180 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1181 		if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
1182 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
1183 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1184 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1185 				list_remove(ill_list, (void *)sctp_ill);
1186 				sctps->sctps_ills_count--;
1187 				sctps->sctps_g_ills[ill_index].ill_count--;
1188 				kmem_free(sctp_ill->sctp_ill_name,
1189 				    sctp_ill->sctp_ill_name_length);
1190 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
1191 			}
1192 		}
1193 		break;
1194 	}
1195 
1196 	case SCTP_IPIF_UP:
1197 
1198 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1199 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
1200 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
1201 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1202 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1203 		rw_exit(&sctp_ipif->sctp_ipif_lock);
1204 		sctp_chk_and_updt_saddr(hindex, sctp_ipif,
1205 		    ipif->ipif_ill->ill_ipst->ips_netstack->netstack_sctp);
1206 
1207 		break;
1208 
1209 	case SCTP_IPIF_UPDATE:
1210 
1211 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1212 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
1213 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1214 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
1215 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1216 		rw_exit(&sctp_ipif->sctp_ipif_lock);
1217 
1218 		break;
1219 
1220 	case SCTP_IPIF_DOWN:
1221 
1222 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1223 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
1224 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
1225 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1226 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1227 		rw_exit(&sctp_ipif->sctp_ipif_lock);
1228 
1229 		break;
1230 	}
1231 	rw_exit(&sctps->sctps_g_ipifs_lock);
1232 	rw_exit(&sctps->sctps_g_ills_lock);
1233 }
1234 
1235 /*
1236  * SCTP source address list manipulaton, locking not used (except for
1237  * sctp locking by the caller.
1238  */
1239 
1240 /* Remove a specific saddr from the list */
1241 void
1242 sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
1243 {
1244 	if (sctp->sctp_conn_tfp != NULL)
1245 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
1246 
1247 	if (sctp->sctp_listen_tfp != NULL)
1248 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
1249 
1250 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
1251 
1252 	if (sctp->sctp_bound_to_all == 1)
1253 		sctp->sctp_bound_to_all = 0;
1254 
1255 	if (sctp->sctp_conn_tfp != NULL)
1256 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
1257 
1258 	if (sctp->sctp_listen_tfp != NULL)
1259 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
1260 }
1261 
1262 /*
1263  * Delete source address from the existing list. No error checking done here
1264  * Called with no locks held.
1265  */
1266 void
1267 sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
1268     boolean_t fanout_locked)
1269 {
1270 	struct sockaddr_in	*sin4;
1271 	struct sockaddr_in6	*sin6;
1272 	int			cnt;
1273 	in6_addr_t		addr;
1274 	sctp_ipif_t		*sctp_ipif;
1275 	int			ifindex = 0;
1276 
1277 	ASSERT(sctp->sctp_nsaddrs >= addcnt);
1278 
1279 	if (!fanout_locked) {
1280 		if (sctp->sctp_conn_tfp != NULL)
1281 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
1282 		if (sctp->sctp_listen_tfp != NULL)
1283 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
1284 	}
1285 
1286 	for (cnt = 0; cnt < addcnt; cnt++) {
1287 		switch (sctp->sctp_family) {
1288 		case AF_INET:
1289 			sin4 = (struct sockaddr_in *)addrs + cnt;
1290 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
1291 			break;
1292 
1293 		case AF_INET6:
1294 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
1295 			addr = sin6->sin6_addr;
1296 			ifindex = sin6->sin6_scope_id;
1297 			break;
1298 		}
1299 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
1300 		    sctp->sctp_zoneid, !sctp->sctp_connp->conn_allzones,
1301 		    ifindex, 0, B_TRUE, sctp->sctp_sctps);
1302 		ASSERT(sctp_ipif != NULL);
1303 		sctp_ipif_hash_remove(sctp, sctp_ipif);
1304 	}
1305 	if (sctp->sctp_bound_to_all == 1)
1306 		sctp->sctp_bound_to_all = 0;
1307 
1308 	if (!fanout_locked) {
1309 		if (sctp->sctp_conn_tfp != NULL)
1310 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
1311 		if (sctp->sctp_listen_tfp != NULL)
1312 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
1313 	}
1314 }
1315 
1316 /*
1317  * Given an address get the corresponding entry from the list
1318  * Called with no locks held.
1319  */
1320 sctp_saddr_ipif_t *
1321 sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1322 {
1323 	int			cnt;
1324 	sctp_saddr_ipif_t	*ipif_obj;
1325 	int			hindex;
1326 	sctp_ipif_t		*sctp_ipif;
1327 
1328 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
1329 	if (sctp->sctp_saddrs[hindex].ipif_count == 0)
1330 		return (NULL);
1331 
1332 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
1333 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
1334 		sctp_ipif = ipif_obj->saddr_ipifp;
1335 		/*
1336 		 * Zone check shouldn't be needed.
1337 		 */
1338 		if (IN6_ARE_ADDR_EQUAL(addr, &sctp_ipif->sctp_ipif_saddr) &&
1339 		    (ifindex == 0 ||
1340 		    ifindex == sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
1341 		    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state)) {
1342 			return (ipif_obj);
1343 		}
1344 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
1345 		    ipif_obj);
1346 	}
1347 	return (NULL);
1348 }
1349 
1350 /* Given an address, add it to the source address list */
1351 int
1352 sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1353 {
1354 	sctp_ipif_t		*sctp_ipif;
1355 
1356 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp->sctp_zoneid,
1357 	    !sctp->sctp_connp->conn_allzones, ifindex, 0, B_TRUE,
1358 	    sctp->sctp_sctps);
1359 	if (sctp_ipif == NULL)
1360 		return (EINVAL);
1361 
1362 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE,
1363 	    B_FALSE) != 0) {
1364 		SCTP_IPIF_REFRELE(sctp_ipif);
1365 		return (EINVAL);
1366 	}
1367 	return (0);
1368 }
1369 
1370 /*
1371  * Remove or mark as dontsrc addresses that are currently not part of the
1372  * association. One would delete addresses when processing an INIT and
1373  * mark as dontsrc when processing an INIT-ACK.
1374  */
1375 void
1376 sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete,
1377     in6_addr_t *no_del_addr)
1378 {
1379 	int			i;
1380 	int			l;
1381 	sctp_saddr_ipif_t	*obj;
1382 	int			scanned = 0;
1383 	int			naddr;
1384 	int			nsaddr;
1385 
1386 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1387 
1388 	/*
1389 	 * Irregardless of the supported address in the INIT, v4
1390 	 * must be supported.
1391 	 */
1392 	if (sctp->sctp_family == AF_INET)
1393 		supp_af = PARM_SUPP_V4;
1394 
1395 	nsaddr = sctp->sctp_nsaddrs;
1396 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1397 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1398 			continue;
1399 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1400 		naddr = sctp->sctp_saddrs[i].ipif_count;
1401 		for (l = 0; l < naddr; l++) {
1402 			sctp_ipif_t	*ipif;
1403 
1404 			ipif = obj->saddr_ipifp;
1405 			scanned++;
1406 
1407 			if (IN6_ARE_ADDR_EQUAL(&ipif->sctp_ipif_saddr,
1408 			    no_del_addr)) {
1409 				goto next_obj;
1410 			}
1411 
1412 			/*
1413 			 * Delete/mark dontsrc loopback/linklocal addresses and
1414 			 * unsupported address.
1415 			 * On a clustered node, we trust the clustering module
1416 			 * to do the right thing w.r.t loopback addresses, so
1417 			 * we ignore loopback addresses in this check.
1418 			 */
1419 			if ((SCTP_IS_IPIF_LOOPBACK(ipif) &&
1420 			    cl_sctp_check_addrs == NULL) ||
1421 			    SCTP_IS_IPIF_LINKLOCAL(ipif) ||
1422 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1423 				if (!delete) {
1424 					obj->saddr_ipif_unconfirmed = 1;
1425 					goto next_obj;
1426 				}
1427 				if (sctp->sctp_bound_to_all == 1)
1428 					sctp->sctp_bound_to_all = 0;
1429 				if (scanned < nsaddr) {
1430 					obj = list_next(&sctp->sctp_saddrs[i].
1431 					    sctp_ipif_list, obj);
1432 					sctp_ipif_hash_remove(sctp, ipif);
1433 					continue;
1434 				}
1435 				sctp_ipif_hash_remove(sctp, ipif);
1436 			}
1437 	next_obj:
1438 			if (scanned >= nsaddr)
1439 				return;
1440 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1441 			    obj);
1442 		}
1443 	}
1444 }
1445 
1446 
1447 /* Get the first valid address from the list. Called with no locks held */
1448 in6_addr_t
1449 sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6, boolean_t *addr_set)
1450 {
1451 	int			i;
1452 	int			l;
1453 	sctp_saddr_ipif_t	*obj;
1454 	int			scanned = 0;
1455 	in6_addr_t		addr;
1456 
1457 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1458 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1459 			continue;
1460 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1461 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
1462 			sctp_ipif_t	*ipif;
1463 
1464 			ipif = obj->saddr_ipifp;
1465 			if (!SCTP_DONT_SRC(obj) &&
1466 			    ipif->sctp_ipif_isv6 == isv6 &&
1467 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
1468 				*addr_set = B_TRUE;
1469 				return (ipif->sctp_ipif_saddr);
1470 			}
1471 			scanned++;
1472 			if (scanned >= sctp->sctp_nsaddrs)
1473 				goto got_none;
1474 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1475 			    obj);
1476 		}
1477 	}
1478 got_none:
1479 	/* Need to double check this */
1480 	if (isv6 == B_TRUE)
1481 		addr =  ipv6_all_zeros;
1482 	else
1483 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
1484 	*addr_set = B_FALSE;
1485 	return (addr);
1486 }
1487 
1488 /*
1489  * Return the list of local addresses of an association.  The parameter
1490  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
1491  * sockaddr_in6 *) depending on the address family.
1492  */
1493 int
1494 sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
1495 {
1496 	int			i;
1497 	int			l;
1498 	sctp_saddr_ipif_t	*obj;
1499 	sctp_t			*sctp = (sctp_t *)conn;
1500 	int			family = sctp->sctp_family;
1501 	int			max = *addrcnt;
1502 	size_t			added = 0;
1503 	struct sockaddr_in6	*sin6;
1504 	struct sockaddr_in	*sin4;
1505 	int			scanned = 0;
1506 	boolean_t		skip_lback = B_FALSE;
1507 
1508 	if (sctp->sctp_nsaddrs == 0)
1509 		return (EINVAL);
1510 
1511 	/*
1512 	 * Skip loopback addresses for non-loopback assoc., ignore
1513 	 * this on a clustered node.
1514 	 */
1515 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback &&
1516 	    (cl_sctp_check_addrs == NULL)) {
1517 		skip_lback = B_TRUE;
1518 	}
1519 
1520 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1521 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1522 			continue;
1523 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1524 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
1525 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
1526 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
1527 
1528 			scanned++;
1529 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1530 			    SCTP_DONT_SRC(obj) ||
1531 			    (SCTP_IS_IPIF_LOOPBACK(ipif) && skip_lback)) {
1532 				if (scanned >= sctp->sctp_nsaddrs)
1533 					goto done;
1534 				obj = list_next(&sctp->sctp_saddrs[i].
1535 				    sctp_ipif_list, obj);
1536 				continue;
1537 			}
1538 			switch (family) {
1539 			case AF_INET:
1540 				sin4 = (struct sockaddr_in *)myaddrs + added;
1541 				sin4->sin_family = AF_INET;
1542 				sin4->sin_port = sctp->sctp_lport;
1543 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1544 				break;
1545 
1546 			case AF_INET6:
1547 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
1548 				sin6->sin6_family = AF_INET6;
1549 				sin6->sin6_port = sctp->sctp_lport;
1550 				sin6->sin6_addr = addr;
1551 				break;
1552 			}
1553 			added++;
1554 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
1555 				goto done;
1556 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1557 			    obj);
1558 		}
1559 	}
1560 done:
1561 	*addrcnt = added;
1562 	return (0);
1563 }
1564 
1565 /*
1566  * Given the supported address family, walk through the source address list
1567  * and return the total length of the available addresses. If 'p' is not
1568  * null, construct the parameter list for the addresses in 'p'.
1569  * 'modify' will only be set when we want the source address list to
1570  * be modified. The source address list will be modified only when
1571  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1572  * be false since the 'sctp' will be that of the listener.
1573  */
1574 size_t
1575 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
1576 {
1577 	int			i;
1578 	int			l;
1579 	sctp_saddr_ipif_t	*obj;
1580 	size_t			paramlen = 0;
1581 	sctp_parm_hdr_t		*hdr;
1582 	int			scanned = 0;
1583 	int			naddr;
1584 	int			nsaddr;
1585 	boolean_t		del_ll = B_FALSE;
1586 	boolean_t		del_lb = B_FALSE;
1587 
1588 
1589 	/*
1590 	 * On a clustered node don't bother changing anything
1591 	 * on the loopback interface.
1592 	 */
1593 	if (modify && !sctp->sctp_loopback && (cl_sctp_check_addrs == NULL))
1594 		del_lb = B_TRUE;
1595 
1596 	if (modify && !sctp->sctp_linklocal)
1597 		del_ll = B_TRUE;
1598 
1599 	nsaddr = sctp->sctp_nsaddrs;
1600 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1601 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1602 			continue;
1603 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1604 		naddr = sctp->sctp_saddrs[i].ipif_count;
1605 		for (l = 0; l < naddr; l++) {
1606 			in6_addr_t	addr;
1607 			sctp_ipif_t	*ipif;
1608 			boolean_t	ipif_lb;
1609 			boolean_t	ipif_ll;
1610 			boolean_t	unsupp_af;
1611 
1612 			ipif = obj->saddr_ipifp;
1613 			scanned++;
1614 
1615 			ipif_lb = SCTP_IS_IPIF_LOOPBACK(ipif);
1616 			ipif_ll = SCTP_IS_IPIF_LINKLOCAL(ipif);
1617 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1618 			/*
1619 			 * We need to either delete or skip loopback/linklocal
1620 			 * or unsupported addresses, if required.
1621 			 */
1622 			if ((ipif_ll && del_ll) || (ipif_lb && del_lb) ||
1623 			    (unsupp_af && modify)) {
1624 				if (sctp->sctp_bound_to_all == 1)
1625 					sctp->sctp_bound_to_all = 0;
1626 				if (scanned < nsaddr) {
1627 					obj = list_next(&sctp->sctp_saddrs[i].
1628 					    sctp_ipif_list, obj);
1629 					sctp_ipif_hash_remove(sctp, ipif);
1630 					continue;
1631 				}
1632 				sctp_ipif_hash_remove(sctp, ipif);
1633 				goto next_addr;
1634 			} else if (ipif_ll || unsupp_af ||
1635 			    (ipif_lb && (cl_sctp_check_addrs == NULL))) {
1636 				goto next_addr;
1637 			}
1638 
1639 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1640 				goto next_addr;
1641 			if (p != NULL)
1642 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
1643 			addr = ipif->sctp_ipif_saddr;
1644 			if (!ipif->sctp_ipif_isv6) {
1645 				struct in_addr	*v4;
1646 
1647 				if (p != NULL) {
1648 					hdr->sph_type = htons(PARM_ADDR4);
1649 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1650 					v4 = (struct in_addr *)(hdr + 1);
1651 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1652 				}
1653 				paramlen += PARM_ADDR4_LEN;
1654 			} else {
1655 				if (p != NULL) {
1656 					hdr->sph_type = htons(PARM_ADDR6);
1657 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1658 					bcopy(&addr, hdr + 1, sizeof (addr));
1659 				}
1660 				paramlen += PARM_ADDR6_LEN;
1661 			}
1662 next_addr:
1663 			if (scanned >= nsaddr)
1664 				return (paramlen);
1665 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1666 			    obj);
1667 		}
1668 	}
1669 	return (paramlen);
1670 }
1671 
1672 /*
1673  * This is used on a clustered node to obtain a list of addresses, the list
1674  * consists of sockaddr_in structs for v4 and sockaddr_in6 for v6. The list
1675  * is then passed onto the clustering module which sends back the correct
1676  * list based on the port info. Regardless of the input, i.e INADDR_ANY
1677  * or specific address(es), we create the list since it could be modified by
1678  * the clustering module. When given a list of addresses, we simply
1679  * create the list of sockaddr_in or sockaddr_in6 structs using those
1680  * addresses. If there is an INADDR_ANY in the input list, or if the
1681  * input is INADDR_ANY, we create a list of sockaddr_in or sockaddr_in6
1682  * structs consisting all the addresses in the global interface list
1683  * except those that are hosted on the loopback interface. We create
1684  * a list of sockaddr_in[6] structs just so that it can be directly input
1685  * to sctp_valid_addr_list() once the clustering module has processed it.
1686  */
1687 int
1688 sctp_get_addrlist(sctp_t *sctp, const void *addrs, uint32_t *addrcnt,
1689     uchar_t **addrlist, int *uspec, size_t *size)
1690 {
1691 	int			cnt;
1692 	int			icnt;
1693 	sctp_ipif_t		*sctp_ipif;
1694 	struct sockaddr_in	*s4;
1695 	struct sockaddr_in6	*s6;
1696 	uchar_t			*p;
1697 	int			err = 0;
1698 	sctp_stack_t		*sctps = sctp->sctp_sctps;
1699 
1700 	*addrlist = NULL;
1701 	*size = 0;
1702 
1703 	/*
1704 	 * Create a list of sockaddr_in[6] structs using the input list.
1705 	 */
1706 	if (sctp->sctp_family == AF_INET) {
1707 		*size = sizeof (struct sockaddr_in) * *addrcnt;
1708 		*addrlist = kmem_zalloc(*size,  KM_SLEEP);
1709 		p = *addrlist;
1710 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1711 			s4 = (struct sockaddr_in *)addrs + cnt;
1712 			/*
1713 			 * We need to create a list of all the available
1714 			 * addresses if there is an INADDR_ANY. However,
1715 			 * if we are beyond LISTEN, then this is invalid
1716 			 * (see sctp_valid_addr_list(). So, we just fail
1717 			 * it here rather than wait till it fails in
1718 			 * sctp_valid_addr_list().
1719 			 */
1720 			if (s4->sin_addr.s_addr == INADDR_ANY) {
1721 				kmem_free(*addrlist, *size);
1722 				*addrlist = NULL;
1723 				*size = 0;
1724 				if (sctp->sctp_state > SCTPS_LISTEN) {
1725 					*addrcnt = 0;
1726 					return (EINVAL);
1727 				}
1728 				if (uspec != NULL)
1729 					*uspec = 1;
1730 				goto get_all_addrs;
1731 			} else {
1732 				bcopy(s4, p, sizeof (*s4));
1733 				p += sizeof (*s4);
1734 			}
1735 		}
1736 	} else {
1737 		*size = sizeof (struct sockaddr_in6) * *addrcnt;
1738 		*addrlist = kmem_zalloc(*size, KM_SLEEP);
1739 		p = *addrlist;
1740 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1741 			s6 = (struct sockaddr_in6 *)addrs + cnt;
1742 			/*
1743 			 * Comments for INADDR_ANY, above, apply here too.
1744 			 */
1745 			if (IN6_IS_ADDR_UNSPECIFIED(&s6->sin6_addr)) {
1746 				kmem_free(*addrlist, *size);
1747 				*size = 0;
1748 				*addrlist = NULL;
1749 				if (sctp->sctp_state > SCTPS_LISTEN) {
1750 					*addrcnt = 0;
1751 					return (EINVAL);
1752 				}
1753 				if (uspec != NULL)
1754 					*uspec = 1;
1755 				goto get_all_addrs;
1756 			} else {
1757 				bcopy(addrs, p, sizeof (*s6));
1758 				p += sizeof (*s6);
1759 			}
1760 		}
1761 	}
1762 	return (err);
1763 get_all_addrs:
1764 
1765 	/*
1766 	 * Allocate max possible size. We allocate the max. size here because
1767 	 * the clustering module could end up adding addresses to the list.
1768 	 * We allocate upfront so that the clustering module need to bother
1769 	 * re-sizing the list.
1770 	 */
1771 	if (sctp->sctp_family == AF_INET) {
1772 		*size = sizeof (struct sockaddr_in) *
1773 		    sctps->sctps_g_ipifs_count;
1774 	} else {
1775 		*size = sizeof (struct sockaddr_in6) *
1776 		    sctps->sctps_g_ipifs_count;
1777 	}
1778 	*addrlist = kmem_zalloc(*size, KM_SLEEP);
1779 	*addrcnt = 0;
1780 	p = *addrlist;
1781 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
1782 
1783 	/*
1784 	 * Walk through the global interface list and add all addresses,
1785 	 * except those that are hosted on loopback interfaces.
1786 	 */
1787 	for (cnt = 0; cnt <  SCTP_IPIF_HASH; cnt++) {
1788 		if (sctps->sctps_g_ipifs[cnt].ipif_count == 0)
1789 			continue;
1790 		sctp_ipif = list_head(
1791 		    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list);
1792 		for (icnt = 0;
1793 		    icnt < sctps->sctps_g_ipifs[cnt].ipif_count;
1794 		    icnt++) {
1795 			in6_addr_t	addr;
1796 
1797 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1798 			addr = sctp_ipif->sctp_ipif_saddr;
1799 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
1800 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
1801 			    SCTP_IS_IPIF_LOOPBACK(sctp_ipif) ||
1802 			    SCTP_IS_IPIF_LINKLOCAL(sctp_ipif) ||
1803 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
1804 			    (sctp->sctp_ipversion == IPV4_VERSION &&
1805 			    sctp_ipif->sctp_ipif_isv6) ||
1806 			    (sctp->sctp_connp->conn_ipv6_v6only &&
1807 			    !sctp_ipif->sctp_ipif_isv6)) {
1808 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1809 				sctp_ipif = list_next(
1810 				    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1811 				    sctp_ipif);
1812 				continue;
1813 			}
1814 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1815 			if (sctp->sctp_family == AF_INET) {
1816 				s4 = (struct sockaddr_in *)p;
1817 				IN6_V4MAPPED_TO_INADDR(&addr, &s4->sin_addr);
1818 				s4->sin_family = AF_INET;
1819 				p += sizeof (*s4);
1820 			} else {
1821 				s6 = (struct sockaddr_in6 *)p;
1822 				s6->sin6_addr = addr;
1823 				s6->sin6_family = AF_INET6;
1824 				s6->sin6_scope_id =
1825 				    sctp_ipif->sctp_ipif_ill->sctp_ill_index;
1826 				p += sizeof (*s6);
1827 			}
1828 			(*addrcnt)++;
1829 			sctp_ipif = list_next(
1830 			    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1831 			    sctp_ipif);
1832 		}
1833 	}
1834 	rw_exit(&sctps->sctps_g_ipifs_lock);
1835 	return (err);
1836 }
1837 
1838 /*
1839  * Get a list of addresses from the source address list. The  caller is
1840  * responsible for allocating sufficient buffer for this.
1841  */
1842 void
1843 sctp_get_saddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1844 {
1845 	int			cnt;
1846 	int			icnt;
1847 	sctp_saddr_ipif_t	*obj;
1848 	int			naddr;
1849 	int			scanned = 0;
1850 
1851 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1852 		if (sctp->sctp_saddrs[cnt].ipif_count == 0)
1853 			continue;
1854 		obj = list_head(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
1855 		naddr = sctp->sctp_saddrs[cnt].ipif_count;
1856 		for (icnt = 0; icnt < naddr; icnt++) {
1857 			sctp_ipif_t	*ipif;
1858 
1859 			if (psize < sizeof (ipif->sctp_ipif_saddr))
1860 				return;
1861 
1862 			scanned++;
1863 			ipif = obj->saddr_ipifp;
1864 			bcopy(&ipif->sctp_ipif_saddr, p,
1865 			    sizeof (ipif->sctp_ipif_saddr));
1866 			p += sizeof (ipif->sctp_ipif_saddr);
1867 			psize -= sizeof (ipif->sctp_ipif_saddr);
1868 			if (scanned >= sctp->sctp_nsaddrs)
1869 				return;
1870 			obj = list_next(
1871 			    &sctp->sctp_saddrs[icnt].sctp_ipif_list,
1872 			    obj);
1873 		}
1874 	}
1875 }
1876 
1877 /*
1878  * Get a list of addresses from the remote address list. The  caller is
1879  * responsible for allocating sufficient buffer for this.
1880  */
1881 void
1882 sctp_get_faddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1883 {
1884 	sctp_faddr_t	*fp;
1885 
1886 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1887 		if (psize < sizeof (fp->faddr))
1888 			return;
1889 		bcopy(&fp->faddr, p, sizeof (fp->faddr));
1890 		p += sizeof (fp->faddr);
1891 		psize -= sizeof (fp->faddr);
1892 	}
1893 }
1894 
1895 static void
1896 sctp_free_ills(sctp_stack_t *sctps)
1897 {
1898 	int			i;
1899 	int			l;
1900 	sctp_ill_t	*sctp_ill;
1901 
1902 	if (sctps->sctps_ills_count == 0)
1903 		return;
1904 
1905 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1906 		sctp_ill = list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
1907 		for (l = 0; l < sctps->sctps_g_ills[i].ill_count; l++) {
1908 			ASSERT(sctp_ill->sctp_ill_ipifcnt == 0);
1909 			list_remove(&sctps->sctps_g_ills[i].sctp_ill_list,
1910 			    sctp_ill);
1911 			sctps->sctps_ills_count--;
1912 			kmem_free(sctp_ill->sctp_ill_name,
1913 			    sctp_ill->sctp_ill_name_length);
1914 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1915 			sctp_ill =
1916 			    list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
1917 		}
1918 		sctps->sctps_g_ills[i].ill_count = 0;
1919 	}
1920 	ASSERT(sctps->sctps_ills_count == 0);
1921 }
1922 
1923 static void
1924 sctp_free_ipifs(sctp_stack_t *sctps)
1925 {
1926 	int			i;
1927 	int			l;
1928 	sctp_ipif_t	*sctp_ipif;
1929 	sctp_ill_t	*sctp_ill;
1930 
1931 	if (sctps->sctps_g_ipifs_count == 0)
1932 		return;
1933 
1934 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1935 		sctp_ipif = list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1936 		for (l = 0; l < sctps->sctps_g_ipifs[i].ipif_count; l++) {
1937 			sctp_ill = sctp_ipif->sctp_ipif_ill;
1938 
1939 			list_remove(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
1940 			    sctp_ipif);
1941 			sctps->sctps_g_ipifs_count--;
1942 			(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt,
1943 			    -1);
1944 			kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1945 			sctp_ipif =
1946 			    list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1947 		}
1948 		sctps->sctps_g_ipifs[i].ipif_count = 0;
1949 	}
1950 	ASSERT(sctps->sctps_g_ipifs_count == 0);
1951 }
1952 
1953 
1954 /* Initialize the SCTP ILL list and lock */
1955 void
1956 sctp_saddr_init(sctp_stack_t *sctps)
1957 {
1958 	int	i;
1959 
1960 	sctps->sctps_g_ills = kmem_zalloc(sizeof (sctp_ill_hash_t) *
1961 	    SCTP_ILL_HASH, KM_SLEEP);
1962 	sctps->sctps_g_ipifs = kmem_zalloc(sizeof (sctp_ipif_hash_t) *
1963 	    SCTP_IPIF_HASH, KM_SLEEP);
1964 
1965 	rw_init(&sctps->sctps_g_ills_lock, NULL, RW_DEFAULT, NULL);
1966 	rw_init(&sctps->sctps_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
1967 
1968 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1969 		sctps->sctps_g_ills[i].ill_count = 0;
1970 		list_create(&sctps->sctps_g_ills[i].sctp_ill_list,
1971 		    sizeof (sctp_ill_t),
1972 		    offsetof(sctp_ill_t, sctp_ills));
1973 	}
1974 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1975 		sctps->sctps_g_ipifs[i].ipif_count = 0;
1976 		list_create(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
1977 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
1978 	}
1979 }
1980 
1981 void
1982 sctp_saddr_fini(sctp_stack_t *sctps)
1983 {
1984 	int	i;
1985 
1986 	sctp_free_ipifs(sctps);
1987 	sctp_free_ills(sctps);
1988 
1989 	for (i = 0; i < SCTP_ILL_HASH; i++)
1990 		list_destroy(&sctps->sctps_g_ills[i].sctp_ill_list);
1991 	for (i = 0; i < SCTP_IPIF_HASH; i++)
1992 		list_destroy(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1993 
1994 	ASSERT(sctps->sctps_ills_count == 0 && sctps->sctps_g_ipifs_count == 0);
1995 	kmem_free(sctps->sctps_g_ills, sizeof (sctp_ill_hash_t) *
1996 	    SCTP_ILL_HASH);
1997 	sctps->sctps_g_ills = NULL;
1998 	kmem_free(sctps->sctps_g_ipifs, sizeof (sctp_ipif_hash_t) *
1999 	    SCTP_IPIF_HASH);
2000 	sctps->sctps_g_ipifs = NULL;
2001 	rw_destroy(&sctps->sctps_g_ills_lock);
2002 	rw_destroy(&sctps->sctps_g_ipifs_lock);
2003 }
2004