xref: /titanic_51/usr/src/uts/common/inet/sctp/sctp_addr.c (revision 5af4ae460e4b20d1119c788db199a9821b2d19b2)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/systm.h>
30 #include <sys/stream.h>
31 #include <sys/cmn_err.h>
32 #include <sys/ddi.h>
33 #include <sys/sunddi.h>
34 #include <sys/kmem.h>
35 #include <sys/socket.h>
36 #include <sys/sysmacros.h>
37 #include <sys/list.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/ip6.h>
41 #include <netinet/sctp.h>
42 
43 #include <inet/common.h>
44 #include <inet/ip.h>
45 #include <inet/ip6.h>
46 #include <inet/ip_if.h>
47 #include <inet/ipclassifier.h>
48 #include <inet/sctp_ip.h>
49 #include "sctp_impl.h"
50 #include "sctp_addr.h"
51 
52 static void		sctp_ipif_inactive(sctp_ipif_t *);
53 static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
54 			    zoneid_t, boolean_t, uint_t, uint_t, boolean_t,
55 			    sctp_stack_t *);
56 static int		sctp_get_all_ipifs(sctp_t *, int);
57 static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int,
58 			    boolean_t, boolean_t);
59 static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
60 static void		sctp_fix_saddr(sctp_t *, in6_addr_t *);
61 static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
62 			    sctp_ipif_hash_t *);
63 static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
64 
65 #define	SCTP_ADDR4_HASH(addr)	\
66 	(((addr) ^ ((addr) >> 8) ^ ((addr) >> 16) ^ ((addr) >> 24)) &	\
67 	(SCTP_IPIF_HASH - 1))
68 
69 #define	SCTP_ADDR6_HASH(addr)	\
70 	(((addr).s6_addr32[3] ^						\
71 	(((addr).s6_addr32[3] ^ (addr).s6_addr32[2]) >> 12)) &		\
72 	(SCTP_IPIF_HASH - 1))
73 
74 #define	SCTP_IPIF_ADDR_HASH(addr, isv6)					\
75 	((isv6) ? SCTP_ADDR6_HASH((addr)) : 				\
76 	SCTP_ADDR4_HASH((addr)._S6_un._S6_u32[3]))
77 
78 #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
79 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
80 	(sctp_ipif_state) ==  SCTP_IPIFS_DOWN)
81 
82 #define	SCTP_IPIF_DISCARD(sctp_ipif_flags)	\
83 	((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED))
84 
85 #define	SCTP_IS_IPIF_LOOPBACK(ipif)		\
86 	((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK)
87 
88 #define	SCTP_IS_IPIF_LINKLOCAL(ipif)		\
89 	((ipif)->sctp_ipif_isv6 && 		\
90 	IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr))
91 
92 #define	SCTP_UNSUPP_AF(ipif, supp_af)	\
93 	((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) ||	\
94 	((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6)))
95 
96 #define	SCTP_IPIF_ZONE_MATCH(sctp, ipif) 				\
97 	IPCL_ZONE_MATCH((sctp)->sctp_connp, (ipif)->sctp_ipif_zoneid)
98 
99 #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
100 #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
101 
102 /*
103  * SCTP Interface list manipulation functions, locking used.
104  */
105 
106 /*
107  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
108  * marked as condemned. Also, check if the ILL needs to go away.
109  */
110 static void
111 sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
112 {
113 	sctp_ill_t	*sctp_ill;
114 	uint_t		hindex;
115 	uint_t		ill_index;
116 	sctp_stack_t	*sctps = sctp_ipif->sctp_ipif_ill->
117 	    sctp_ill_netstack->netstack_sctp;
118 
119 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
120 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
121 
122 	hindex = SCTP_IPIF_ADDR_HASH(sctp_ipif->sctp_ipif_saddr,
123 	    sctp_ipif->sctp_ipif_isv6);
124 
125 	sctp_ill = sctp_ipif->sctp_ipif_ill;
126 	ASSERT(sctp_ill != NULL);
127 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
128 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
129 	    sctp_ipif->sctp_ipif_refcnt != 0) {
130 		rw_exit(&sctps->sctps_g_ipifs_lock);
131 		rw_exit(&sctps->sctps_g_ills_lock);
132 		return;
133 	}
134 	list_remove(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
135 	    sctp_ipif);
136 	sctps->sctps_g_ipifs[hindex].ipif_count--;
137 	sctps->sctps_g_ipifs_count--;
138 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
139 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
140 
141 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
142 	if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
143 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
144 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
145 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
146 			list_remove(&sctps->sctps_g_ills[ill_index].
147 			    sctp_ill_list, (void *)sctp_ill);
148 			sctps->sctps_g_ills[ill_index].ill_count--;
149 			sctps->sctps_ills_count--;
150 			kmem_free(sctp_ill->sctp_ill_name,
151 			    sctp_ill->sctp_ill_name_length);
152 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
153 		}
154 	}
155 	rw_exit(&sctps->sctps_g_ipifs_lock);
156 	rw_exit(&sctps->sctps_g_ills_lock);
157 }
158 
159 /*
160  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
161  * We are either looking for a IPIF with the given address before
162  * inserting it into the global list or looking for an IPIF for an
163  * address given an SCTP. In the former case we always check the zoneid,
164  * but for the latter case, check_zid could be B_FALSE if the connp
165  * for the sctp has conn_all_zones set. When looking for an address we
166  * give preference to one that is up, so even though we may find one that
167  * is not up we keep looking if there is one up, we hold the down addr
168  * in backup_ipif in case we don't find one that is up - i.e. we return
169  * the backup_ipif in that case. Note that if we are looking for. If we
170  * are specifically looking for an up address, then usable will be set
171  * to true.
172  */
173 static sctp_ipif_t *
174 sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid,
175     boolean_t check_zid, uint_t ifindex, uint_t seqid, boolean_t usable,
176     sctp_stack_t *sctps)
177 {
178 	int		j;
179 	sctp_ipif_t	*sctp_ipif;
180 	sctp_ipif_t	*backup_ipif = NULL;
181 	int		hindex;
182 
183 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
184 
185 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
186 	if (sctps->sctps_g_ipifs[hindex].ipif_count == 0) {
187 		rw_exit(&sctps->sctps_g_ipifs_lock);
188 		return (NULL);
189 	}
190 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
191 	for (j = 0; j < sctps->sctps_g_ipifs[hindex].ipif_count; j++) {
192 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
193 		if ((!check_zid ||
194 		    (sctp_ipif->sctp_ipif_zoneid == ALL_ZONES ||
195 		    zoneid == sctp_ipif->sctp_ipif_zoneid)) &&
196 		    (ifindex == 0 || ifindex ==
197 		    sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
198 		    ((seqid != 0 && seqid == sctp_ipif->sctp_ipif_id) ||
199 		    (IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
200 		    addr)))) {
201 			if (!usable || sctp_ipif->sctp_ipif_state ==
202 			    SCTP_IPIFS_UP) {
203 				rw_exit(&sctp_ipif->sctp_ipif_lock);
204 				if (refhold)
205 					SCTP_IPIF_REFHOLD(sctp_ipif);
206 				rw_exit(&sctps->sctps_g_ipifs_lock);
207 				return (sctp_ipif);
208 			} else if (sctp_ipif->sctp_ipif_state ==
209 			    SCTP_IPIFS_DOWN && backup_ipif == NULL) {
210 				backup_ipif = sctp_ipif;
211 			}
212 		}
213 		rw_exit(&sctp_ipif->sctp_ipif_lock);
214 		sctp_ipif = list_next(
215 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list, sctp_ipif);
216 	}
217 	if (backup_ipif != NULL) {
218 		if (refhold)
219 			SCTP_IPIF_REFHOLD(backup_ipif);
220 		rw_exit(&sctps->sctps_g_ipifs_lock);
221 		return (backup_ipif);
222 	}
223 	rw_exit(&sctps->sctps_g_ipifs_lock);
224 	return (NULL);
225 }
226 
227 /*
228  * Populate the list with all the SCTP ipifs for a given ipversion.
229  * Increments sctp_ipif refcnt.
230  * Called with no locks held.
231  */
232 static int
233 sctp_get_all_ipifs(sctp_t *sctp, int sleep)
234 {
235 	sctp_ipif_t		*sctp_ipif;
236 	int			i;
237 	int			j;
238 	int			error = 0;
239 	sctp_stack_t	*sctps = sctp->sctp_sctps;
240 
241 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
242 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
243 		if (sctps->sctps_g_ipifs[i].ipif_count == 0)
244 			continue;
245 		sctp_ipif = list_head(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
246 		for (j = 0; j < sctps->sctps_g_ipifs[i].ipif_count; j++) {
247 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
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->sctp_ipversion == IPV4_VERSION &&
252 			    sctp_ipif->sctp_ipif_isv6) ||
253 			    (sctp->sctp_connp->conn_ipv6_v6only &&
254 			    !sctp_ipif->sctp_ipif_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 			    IN_MULTICAST(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 
985 	sctps = ipif->ipif_ill->ill_ipst->ips_netstack->netstack_sctp;
986 
987 	/* Index for new address */
988 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr, ill->ill_isv6);
989 
990 	/*
991 	 * The address on this IPIF is changing, we need to look for
992 	 * this old address and mark it condemned, before creating
993 	 * one for the new address.
994 	 */
995 	osctp_ipif = sctp_lookup_ipif_addr(&v6addr, B_FALSE,
996 	    ipif->ipif_zoneid, B_TRUE, SCTP_ILL_TO_PHYINDEX(ill),
997 	    ipif->ipif_seqid, B_FALSE, sctps);
998 
999 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
1000 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
1001 
1002 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
1003 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
1004 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
1005 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill) &&
1006 		    sctp_ill->sctp_ill_isv6 == ill->ill_isv6) {
1007 			break;
1008 		}
1009 		sctp_ill = list_next(
1010 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
1011 	}
1012 
1013 	if (sctp_ill == NULL) {
1014 		ip1dbg(("sctp_update_ipif_addr: ill not found ..\n"));
1015 		rw_exit(&sctps->sctps_g_ipifs_lock);
1016 		rw_exit(&sctps->sctps_g_ills_lock);
1017 		return;
1018 	}
1019 
1020 	if (osctp_ipif != NULL) {
1021 
1022 		/* The address is the same? */
1023 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &v6addr)) {
1024 			boolean_t	chk_n_updt = B_FALSE;
1025 
1026 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
1027 			rw_enter(&osctp_ipif->sctp_ipif_lock, RW_WRITER);
1028 			if (ipif->ipif_flags & IPIF_UP &&
1029 			    osctp_ipif->sctp_ipif_state != SCTP_IPIFS_UP) {
1030 				osctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
1031 				chk_n_updt = B_TRUE;
1032 			} else {
1033 				osctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
1034 			}
1035 			osctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1036 			rw_exit(&osctp_ipif->sctp_ipif_lock);
1037 			if (chk_n_updt) {
1038 				sctp_chk_and_updt_saddr(hindex, osctp_ipif,
1039 				    sctps);
1040 			}
1041 			rw_exit(&sctps->sctps_g_ipifs_lock);
1042 			rw_exit(&sctps->sctps_g_ills_lock);
1043 			return;
1044 		}
1045 		/*
1046 		 * We are effectively removing this address from the ILL.
1047 		 */
1048 		if (osctp_ipif->sctp_ipif_refcnt != 0) {
1049 			osctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
1050 		} else {
1051 			list_t		*ipif_list;
1052 			int		ohindex;
1053 
1054 			osctp_ill = osctp_ipif->sctp_ipif_ill;
1055 			/* hash index for the old one */
1056 			ohindex = SCTP_IPIF_ADDR_HASH(
1057 			    osctp_ipif->sctp_ipif_saddr,
1058 			    osctp_ipif->sctp_ipif_isv6);
1059 
1060 			ipif_list =
1061 			    &sctps->sctps_g_ipifs[ohindex].sctp_ipif_list;
1062 
1063 			list_remove(ipif_list, (void *)osctp_ipif);
1064 			sctps->sctps_g_ipifs[ohindex].ipif_count--;
1065 			sctps->sctps_g_ipifs_count--;
1066 			rw_destroy(&osctp_ipif->sctp_ipif_lock);
1067 			kmem_free(osctp_ipif, sizeof (sctp_ipif_t));
1068 			(void) atomic_add_32_nv(&osctp_ill->sctp_ill_ipifcnt,
1069 			    -1);
1070 		}
1071 	}
1072 
1073 	sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
1074 	/* Try again? */
1075 	if (sctp_ipif == NULL) {
1076 		cmn_err(CE_WARN, "sctp_update_ipif_addr: error adding "
1077 		    "IPIF %p to SCTP's IPIF list", (void *)ipif);
1078 		rw_exit(&sctps->sctps_g_ipifs_lock);
1079 		rw_exit(&sctps->sctps_g_ills_lock);
1080 		return;
1081 	}
1082 	sctps->sctps_g_ipifs_count++;
1083 	rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
1084 	sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
1085 	sctp_ipif->sctp_ipif_ill = sctp_ill;
1086 	sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
1087 	sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
1088 	sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
1089 	if (ipif->ipif_flags & IPIF_UP)
1090 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
1091 	else
1092 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
1093 	sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1094 	/*
1095 	 * We add it to the head so that it is quicker to find good/recent
1096 	 * additions.
1097 	 */
1098 	list_insert_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
1099 	    (void *)sctp_ipif);
1100 	sctps->sctps_g_ipifs[hindex].ipif_count++;
1101 	atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
1102 	if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_UP)
1103 		sctp_chk_and_updt_saddr(hindex, sctp_ipif, sctps);
1104 	rw_exit(&sctps->sctps_g_ipifs_lock);
1105 	rw_exit(&sctps->sctps_g_ills_lock);
1106 }
1107 
1108 /* Insert, Remove,  Mark up or Mark down the ipif */
1109 void
1110 sctp_update_ipif(ipif_t *ipif, int op)
1111 {
1112 	ill_t		*ill = ipif->ipif_ill;
1113 	int		i;
1114 	sctp_ill_t	*sctp_ill;
1115 	sctp_ipif_t	*sctp_ipif;
1116 	uint_t		ill_index;
1117 	uint_t		hindex;
1118 	netstack_t	*ns = ipif->ipif_ill->ill_ipst->ips_netstack;
1119 	sctp_stack_t	*sctps = ns->netstack_sctp;
1120 
1121 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
1122 
1123 	rw_enter(&sctps->sctps_g_ills_lock, RW_READER);
1124 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_WRITER);
1125 
1126 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
1127 	sctp_ill = list_head(&sctps->sctps_g_ills[ill_index].sctp_ill_list);
1128 	for (i = 0; i < sctps->sctps_g_ills[ill_index].ill_count; i++) {
1129 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill) &&
1130 		    sctp_ill->sctp_ill_isv6 == ill->ill_isv6) {
1131 			break;
1132 		}
1133 		sctp_ill = list_next(
1134 		    &sctps->sctps_g_ills[ill_index].sctp_ill_list, sctp_ill);
1135 	}
1136 	if (sctp_ill == NULL) {
1137 		rw_exit(&sctps->sctps_g_ipifs_lock);
1138 		rw_exit(&sctps->sctps_g_ills_lock);
1139 		return;
1140 	}
1141 
1142 	hindex = SCTP_IPIF_ADDR_HASH(ipif->ipif_v6lcl_addr,
1143 	    ipif->ipif_ill->ill_isv6);
1144 	sctp_ipif = list_head(&sctps->sctps_g_ipifs[hindex].sctp_ipif_list);
1145 	for (i = 0; i < sctps->sctps_g_ipifs[hindex].ipif_count; i++) {
1146 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid) {
1147 			ASSERT(IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
1148 			    &ipif->ipif_v6lcl_addr));
1149 			break;
1150 		}
1151 		sctp_ipif = list_next(
1152 		    &sctps->sctps_g_ipifs[hindex].sctp_ipif_list,
1153 		    sctp_ipif);
1154 	}
1155 	if (sctp_ipif == NULL) {
1156 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
1157 		rw_exit(&sctps->sctps_g_ipifs_lock);
1158 		rw_exit(&sctps->sctps_g_ills_lock);
1159 		return;
1160 	}
1161 	ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
1162 	switch (op) {
1163 	case SCTP_IPIF_REMOVE:
1164 	{
1165 		list_t		*ipif_list;
1166 		list_t		*ill_list;
1167 
1168 		ill_list = &sctps->sctps_g_ills[ill_index].sctp_ill_list;
1169 		ipif_list = &sctps->sctps_g_ipifs[hindex].sctp_ipif_list;
1170 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
1171 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
1172 			rw_exit(&sctps->sctps_g_ipifs_lock);
1173 			rw_exit(&sctps->sctps_g_ills_lock);
1174 			return;
1175 		}
1176 		list_remove(ipif_list, (void *)sctp_ipif);
1177 		sctps->sctps_g_ipifs[hindex].ipif_count--;
1178 		sctps->sctps_g_ipifs_count--;
1179 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
1180 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1181 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
1182 		if (rw_tryupgrade(&sctps->sctps_g_ills_lock) != 0) {
1183 			rw_downgrade(&sctps->sctps_g_ipifs_lock);
1184 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
1185 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
1186 				list_remove(ill_list, (void *)sctp_ill);
1187 				sctps->sctps_ills_count--;
1188 				sctps->sctps_g_ills[ill_index].ill_count--;
1189 				kmem_free(sctp_ill->sctp_ill_name,
1190 				    sctp_ill->sctp_ill_name_length);
1191 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
1192 			}
1193 		}
1194 		break;
1195 	}
1196 
1197 	case SCTP_IPIF_UP:
1198 
1199 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1200 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
1201 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
1202 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1203 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1204 		rw_exit(&sctp_ipif->sctp_ipif_lock);
1205 		sctp_chk_and_updt_saddr(hindex, sctp_ipif,
1206 		    ipif->ipif_ill->ill_ipst->ips_netstack->netstack_sctp);
1207 
1208 		break;
1209 
1210 	case SCTP_IPIF_UPDATE:
1211 
1212 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1213 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
1214 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1215 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
1216 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1217 		rw_exit(&sctp_ipif->sctp_ipif_lock);
1218 
1219 		break;
1220 
1221 	case SCTP_IPIF_DOWN:
1222 
1223 		rw_downgrade(&sctps->sctps_g_ipifs_lock);
1224 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
1225 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
1226 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
1227 		sctp_ipif->sctp_ipif_flags = ipif->ipif_flags;
1228 		rw_exit(&sctp_ipif->sctp_ipif_lock);
1229 
1230 		break;
1231 	}
1232 	rw_exit(&sctps->sctps_g_ipifs_lock);
1233 	rw_exit(&sctps->sctps_g_ills_lock);
1234 }
1235 
1236 /*
1237  * SCTP source address list manipulaton, locking not used (except for
1238  * sctp locking by the caller.
1239  */
1240 
1241 /* Remove a specific saddr from the list */
1242 void
1243 sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
1244 {
1245 	if (sctp->sctp_conn_tfp != NULL)
1246 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
1247 
1248 	if (sctp->sctp_listen_tfp != NULL)
1249 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
1250 
1251 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
1252 
1253 	if (sctp->sctp_bound_to_all == 1)
1254 		sctp->sctp_bound_to_all = 0;
1255 
1256 	if (sctp->sctp_conn_tfp != NULL)
1257 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
1258 
1259 	if (sctp->sctp_listen_tfp != NULL)
1260 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
1261 }
1262 
1263 /*
1264  * Delete source address from the existing list. No error checking done here
1265  * Called with no locks held.
1266  */
1267 void
1268 sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
1269     boolean_t fanout_locked)
1270 {
1271 	struct sockaddr_in	*sin4;
1272 	struct sockaddr_in6	*sin6;
1273 	int			cnt;
1274 	in6_addr_t		addr;
1275 	sctp_ipif_t		*sctp_ipif;
1276 	int			ifindex = 0;
1277 
1278 	ASSERT(sctp->sctp_nsaddrs >= addcnt);
1279 
1280 	if (!fanout_locked) {
1281 		if (sctp->sctp_conn_tfp != NULL)
1282 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
1283 		if (sctp->sctp_listen_tfp != NULL)
1284 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
1285 	}
1286 
1287 	for (cnt = 0; cnt < addcnt; cnt++) {
1288 		switch (sctp->sctp_family) {
1289 		case AF_INET:
1290 			sin4 = (struct sockaddr_in *)addrs + cnt;
1291 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
1292 			break;
1293 
1294 		case AF_INET6:
1295 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
1296 			addr = sin6->sin6_addr;
1297 			ifindex = sin6->sin6_scope_id;
1298 			break;
1299 		}
1300 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
1301 		    sctp->sctp_zoneid, !sctp->sctp_connp->conn_allzones,
1302 		    ifindex, 0, B_TRUE, sctp->sctp_sctps);
1303 		ASSERT(sctp_ipif != NULL);
1304 		sctp_ipif_hash_remove(sctp, sctp_ipif);
1305 	}
1306 	if (sctp->sctp_bound_to_all == 1)
1307 		sctp->sctp_bound_to_all = 0;
1308 
1309 	if (!fanout_locked) {
1310 		if (sctp->sctp_conn_tfp != NULL)
1311 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
1312 		if (sctp->sctp_listen_tfp != NULL)
1313 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
1314 	}
1315 }
1316 
1317 /*
1318  * Given an address get the corresponding entry from the list
1319  * Called with no locks held.
1320  */
1321 sctp_saddr_ipif_t *
1322 sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1323 {
1324 	int			cnt;
1325 	sctp_saddr_ipif_t	*ipif_obj;
1326 	int			hindex;
1327 	sctp_ipif_t		*sctp_ipif;
1328 
1329 	hindex = SCTP_IPIF_ADDR_HASH(*addr, !IN6_IS_ADDR_V4MAPPED(addr));
1330 	if (sctp->sctp_saddrs[hindex].ipif_count == 0)
1331 		return (NULL);
1332 
1333 	ipif_obj = list_head(&sctp->sctp_saddrs[hindex].sctp_ipif_list);
1334 	for (cnt = 0; cnt < sctp->sctp_saddrs[hindex].ipif_count; cnt++) {
1335 		sctp_ipif = ipif_obj->saddr_ipifp;
1336 		/*
1337 		 * Zone check shouldn't be needed.
1338 		 */
1339 		if (IN6_ARE_ADDR_EQUAL(addr, &sctp_ipif->sctp_ipif_saddr) &&
1340 		    (ifindex == 0 ||
1341 		    ifindex == sctp_ipif->sctp_ipif_ill->sctp_ill_index) &&
1342 		    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state)) {
1343 			return (ipif_obj);
1344 		}
1345 		ipif_obj = list_next(&sctp->sctp_saddrs[hindex].sctp_ipif_list,
1346 		    ipif_obj);
1347 	}
1348 	return (NULL);
1349 }
1350 
1351 /* Given an address, add it to the source address list */
1352 int
1353 sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex)
1354 {
1355 	sctp_ipif_t		*sctp_ipif;
1356 
1357 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp->sctp_zoneid,
1358 	    !sctp->sctp_connp->conn_allzones, ifindex, 0, B_TRUE,
1359 	    sctp->sctp_sctps);
1360 	if (sctp_ipif == NULL)
1361 		return (EINVAL);
1362 
1363 	if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE,
1364 	    B_FALSE) != 0) {
1365 		SCTP_IPIF_REFRELE(sctp_ipif);
1366 		return (EINVAL);
1367 	}
1368 	return (0);
1369 }
1370 
1371 /*
1372  * Remove or mark as dontsrc addresses that are currently not part of the
1373  * association. One would delete addresses when processing an INIT and
1374  * mark as dontsrc when processing an INIT-ACK.
1375  */
1376 void
1377 sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete,
1378     in6_addr_t *no_del_addr)
1379 {
1380 	int			i;
1381 	int			l;
1382 	sctp_saddr_ipif_t	*obj;
1383 	int			scanned = 0;
1384 	int			naddr;
1385 	int			nsaddr;
1386 
1387 	ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0);
1388 
1389 	/*
1390 	 * Irregardless of the supported address in the INIT, v4
1391 	 * must be supported.
1392 	 */
1393 	if (sctp->sctp_family == AF_INET)
1394 		supp_af = PARM_SUPP_V4;
1395 
1396 	nsaddr = sctp->sctp_nsaddrs;
1397 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1398 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1399 			continue;
1400 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1401 		naddr = sctp->sctp_saddrs[i].ipif_count;
1402 		for (l = 0; l < naddr; l++) {
1403 			sctp_ipif_t	*ipif;
1404 
1405 			ipif = obj->saddr_ipifp;
1406 			scanned++;
1407 
1408 			if (IN6_ARE_ADDR_EQUAL(&ipif->sctp_ipif_saddr,
1409 			    no_del_addr)) {
1410 				goto next_obj;
1411 			}
1412 
1413 			/*
1414 			 * Delete/mark dontsrc loopback/linklocal addresses and
1415 			 * unsupported address.
1416 			 * On a clustered node, we trust the clustering module
1417 			 * to do the right thing w.r.t loopback addresses, so
1418 			 * we ignore loopback addresses in this check.
1419 			 */
1420 			if ((SCTP_IS_IPIF_LOOPBACK(ipif) &&
1421 			    cl_sctp_check_addrs == NULL) ||
1422 			    SCTP_IS_IPIF_LINKLOCAL(ipif) ||
1423 			    SCTP_UNSUPP_AF(ipif, supp_af)) {
1424 				if (!delete) {
1425 					obj->saddr_ipif_unconfirmed = 1;
1426 					goto next_obj;
1427 				}
1428 				if (sctp->sctp_bound_to_all == 1)
1429 					sctp->sctp_bound_to_all = 0;
1430 				if (scanned < nsaddr) {
1431 					obj = list_next(&sctp->sctp_saddrs[i].
1432 					    sctp_ipif_list, obj);
1433 					sctp_ipif_hash_remove(sctp, ipif);
1434 					continue;
1435 				}
1436 				sctp_ipif_hash_remove(sctp, ipif);
1437 			}
1438 	next_obj:
1439 			if (scanned >= nsaddr)
1440 				return;
1441 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1442 			    obj);
1443 		}
1444 	}
1445 }
1446 
1447 
1448 /* Get the first valid address from the list. Called with no locks held */
1449 in6_addr_t
1450 sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6, boolean_t *addr_set)
1451 {
1452 	int			i;
1453 	int			l;
1454 	sctp_saddr_ipif_t	*obj;
1455 	int			scanned = 0;
1456 	in6_addr_t		addr;
1457 
1458 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1459 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1460 			continue;
1461 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1462 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
1463 			sctp_ipif_t	*ipif;
1464 
1465 			ipif = obj->saddr_ipifp;
1466 			if (!SCTP_DONT_SRC(obj) &&
1467 			    ipif->sctp_ipif_isv6 == isv6 &&
1468 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP) {
1469 				*addr_set = B_TRUE;
1470 				return (ipif->sctp_ipif_saddr);
1471 			}
1472 			scanned++;
1473 			if (scanned >= sctp->sctp_nsaddrs)
1474 				goto got_none;
1475 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1476 			    obj);
1477 		}
1478 	}
1479 got_none:
1480 	/* Need to double check this */
1481 	if (isv6 == B_TRUE)
1482 		addr =  ipv6_all_zeros;
1483 	else
1484 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
1485 	*addr_set = B_FALSE;
1486 	return (addr);
1487 }
1488 
1489 /*
1490  * Return the list of local addresses of an association.  The parameter
1491  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
1492  * sockaddr_in6 *) depending on the address family.
1493  */
1494 int
1495 sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
1496 {
1497 	int			i;
1498 	int			l;
1499 	sctp_saddr_ipif_t	*obj;
1500 	sctp_t			*sctp = (sctp_t *)conn;
1501 	int			family = sctp->sctp_family;
1502 	int			max = *addrcnt;
1503 	size_t			added = 0;
1504 	struct sockaddr_in6	*sin6;
1505 	struct sockaddr_in	*sin4;
1506 	int			scanned = 0;
1507 	boolean_t		skip_lback = B_FALSE;
1508 
1509 	if (sctp->sctp_nsaddrs == 0)
1510 		return (EINVAL);
1511 
1512 	/*
1513 	 * Skip loopback addresses for non-loopback assoc., ignore
1514 	 * this on a clustered node.
1515 	 */
1516 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback &&
1517 	    (cl_sctp_check_addrs == NULL)) {
1518 		skip_lback = B_TRUE;
1519 	}
1520 
1521 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1522 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1523 			continue;
1524 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1525 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
1526 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
1527 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
1528 
1529 			scanned++;
1530 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1531 			    SCTP_DONT_SRC(obj) ||
1532 			    (SCTP_IS_IPIF_LOOPBACK(ipif) && skip_lback)) {
1533 				if (scanned >= sctp->sctp_nsaddrs)
1534 					goto done;
1535 				obj = list_next(&sctp->sctp_saddrs[i].
1536 				    sctp_ipif_list, obj);
1537 				continue;
1538 			}
1539 			switch (family) {
1540 			case AF_INET:
1541 				sin4 = (struct sockaddr_in *)myaddrs + added;
1542 				sin4->sin_family = AF_INET;
1543 				sin4->sin_port = sctp->sctp_lport;
1544 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1545 				break;
1546 
1547 			case AF_INET6:
1548 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
1549 				sin6->sin6_family = AF_INET6;
1550 				sin6->sin6_port = sctp->sctp_lport;
1551 				sin6->sin6_addr = addr;
1552 				break;
1553 			}
1554 			added++;
1555 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
1556 				goto done;
1557 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1558 			    obj);
1559 		}
1560 	}
1561 done:
1562 	*addrcnt = added;
1563 	return (0);
1564 }
1565 
1566 /*
1567  * Given the supported address family, walk through the source address list
1568  * and return the total length of the available addresses. If 'p' is not
1569  * null, construct the parameter list for the addresses in 'p'.
1570  * 'modify' will only be set when we want the source address list to
1571  * be modified. The source address list will be modified only when
1572  * generating an INIT chunk. For generating an INIT-ACK 'modify' will
1573  * be false since the 'sctp' will be that of the listener.
1574  */
1575 size_t
1576 sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify)
1577 {
1578 	int			i;
1579 	int			l;
1580 	sctp_saddr_ipif_t	*obj;
1581 	size_t			paramlen = 0;
1582 	sctp_parm_hdr_t		*hdr;
1583 	int			scanned = 0;
1584 	int			naddr;
1585 	int			nsaddr;
1586 	boolean_t		del_ll = B_FALSE;
1587 	boolean_t		del_lb = B_FALSE;
1588 
1589 
1590 	/*
1591 	 * On a clustered node don't bother changing anything
1592 	 * on the loopback interface.
1593 	 */
1594 	if (modify && !sctp->sctp_loopback && (cl_sctp_check_addrs == NULL))
1595 		del_lb = B_TRUE;
1596 
1597 	if (modify && !sctp->sctp_linklocal)
1598 		del_ll = B_TRUE;
1599 
1600 	nsaddr = sctp->sctp_nsaddrs;
1601 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1602 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1603 			continue;
1604 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1605 		naddr = sctp->sctp_saddrs[i].ipif_count;
1606 		for (l = 0; l < naddr; l++) {
1607 			in6_addr_t	addr;
1608 			sctp_ipif_t	*ipif;
1609 			boolean_t	ipif_lb;
1610 			boolean_t	ipif_ll;
1611 			boolean_t	unsupp_af;
1612 
1613 			ipif = obj->saddr_ipifp;
1614 			scanned++;
1615 
1616 			ipif_lb = SCTP_IS_IPIF_LOOPBACK(ipif);
1617 			ipif_ll = SCTP_IS_IPIF_LINKLOCAL(ipif);
1618 			unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af);
1619 			/*
1620 			 * We need to either delete or skip loopback/linklocal
1621 			 * or unsupported addresses, if required.
1622 			 */
1623 			if ((ipif_ll && del_ll) || (ipif_lb && del_lb) ||
1624 			    (unsupp_af && modify)) {
1625 				if (sctp->sctp_bound_to_all == 1)
1626 					sctp->sctp_bound_to_all = 0;
1627 				if (scanned < nsaddr) {
1628 					obj = list_next(&sctp->sctp_saddrs[i].
1629 					    sctp_ipif_list, obj);
1630 					sctp_ipif_hash_remove(sctp, ipif);
1631 					continue;
1632 				}
1633 				sctp_ipif_hash_remove(sctp, ipif);
1634 				goto next_addr;
1635 			} else if (ipif_ll || unsupp_af ||
1636 			    (ipif_lb && (cl_sctp_check_addrs == NULL))) {
1637 				goto next_addr;
1638 			}
1639 
1640 			if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state))
1641 				goto next_addr;
1642 			if (p != NULL)
1643 				hdr = (sctp_parm_hdr_t *)(p + paramlen);
1644 			addr = ipif->sctp_ipif_saddr;
1645 			if (!ipif->sctp_ipif_isv6) {
1646 				struct in_addr	*v4;
1647 
1648 				if (p != NULL) {
1649 					hdr->sph_type = htons(PARM_ADDR4);
1650 					hdr->sph_len = htons(PARM_ADDR4_LEN);
1651 					v4 = (struct in_addr *)(hdr + 1);
1652 					IN6_V4MAPPED_TO_INADDR(&addr, v4);
1653 				}
1654 				paramlen += PARM_ADDR4_LEN;
1655 			} else {
1656 				if (p != NULL) {
1657 					hdr->sph_type = htons(PARM_ADDR6);
1658 					hdr->sph_len = htons(PARM_ADDR6_LEN);
1659 					bcopy(&addr, hdr + 1, sizeof (addr));
1660 				}
1661 				paramlen += PARM_ADDR6_LEN;
1662 			}
1663 next_addr:
1664 			if (scanned >= nsaddr)
1665 				return (paramlen);
1666 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1667 			    obj);
1668 		}
1669 	}
1670 	return (paramlen);
1671 }
1672 
1673 /*
1674  * This is used on a clustered node to obtain a list of addresses, the list
1675  * consists of sockaddr_in structs for v4 and sockaddr_in6 for v6. The list
1676  * is then passed onto the clustering module which sends back the correct
1677  * list based on the port info. Regardless of the input, i.e INADDR_ANY
1678  * or specific address(es), we create the list since it could be modified by
1679  * the clustering module. When given a list of addresses, we simply
1680  * create the list of sockaddr_in or sockaddr_in6 structs using those
1681  * addresses. If there is an INADDR_ANY in the input list, or if the
1682  * input is INADDR_ANY, we create a list of sockaddr_in or sockaddr_in6
1683  * structs consisting all the addresses in the global interface list
1684  * except those that are hosted on the loopback interface. We create
1685  * a list of sockaddr_in[6] structs just so that it can be directly input
1686  * to sctp_valid_addr_list() once the clustering module has processed it.
1687  */
1688 int
1689 sctp_get_addrlist(sctp_t *sctp, const void *addrs, uint32_t *addrcnt,
1690     uchar_t **addrlist, int *uspec, size_t *size)
1691 {
1692 	int			cnt;
1693 	int			icnt;
1694 	sctp_ipif_t		*sctp_ipif;
1695 	struct sockaddr_in	*s4;
1696 	struct sockaddr_in6	*s6;
1697 	uchar_t			*p;
1698 	int			err = 0;
1699 	sctp_stack_t		*sctps = sctp->sctp_sctps;
1700 
1701 	*addrlist = NULL;
1702 	*size = 0;
1703 
1704 	/*
1705 	 * Create a list of sockaddr_in[6] structs using the input list.
1706 	 */
1707 	if (sctp->sctp_family == AF_INET) {
1708 		*size = sizeof (struct sockaddr_in) * *addrcnt;
1709 		*addrlist = kmem_zalloc(*size,  KM_SLEEP);
1710 		p = *addrlist;
1711 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1712 			s4 = (struct sockaddr_in *)addrs + cnt;
1713 			/*
1714 			 * We need to create a list of all the available
1715 			 * addresses if there is an INADDR_ANY. However,
1716 			 * if we are beyond LISTEN, then this is invalid
1717 			 * (see sctp_valid_addr_list(). So, we just fail
1718 			 * it here rather than wait till it fails in
1719 			 * sctp_valid_addr_list().
1720 			 */
1721 			if (s4->sin_addr.s_addr == INADDR_ANY) {
1722 				kmem_free(*addrlist, *size);
1723 				*addrlist = NULL;
1724 				*size = 0;
1725 				if (sctp->sctp_state > SCTPS_LISTEN) {
1726 					*addrcnt = 0;
1727 					return (EINVAL);
1728 				}
1729 				if (uspec != NULL)
1730 					*uspec = 1;
1731 				goto get_all_addrs;
1732 			} else {
1733 				bcopy(s4, p, sizeof (*s4));
1734 				p += sizeof (*s4);
1735 			}
1736 		}
1737 	} else {
1738 		*size = sizeof (struct sockaddr_in6) * *addrcnt;
1739 		*addrlist = kmem_zalloc(*size, KM_SLEEP);
1740 		p = *addrlist;
1741 		for (cnt = 0; cnt < *addrcnt; cnt++) {
1742 			s6 = (struct sockaddr_in6 *)addrs + cnt;
1743 			/*
1744 			 * Comments for INADDR_ANY, above, apply here too.
1745 			 */
1746 			if (IN6_IS_ADDR_UNSPECIFIED(&s6->sin6_addr)) {
1747 				kmem_free(*addrlist, *size);
1748 				*size = 0;
1749 				*addrlist = NULL;
1750 				if (sctp->sctp_state > SCTPS_LISTEN) {
1751 					*addrcnt = 0;
1752 					return (EINVAL);
1753 				}
1754 				if (uspec != NULL)
1755 					*uspec = 1;
1756 				goto get_all_addrs;
1757 			} else {
1758 				bcopy(addrs, p, sizeof (*s6));
1759 				p += sizeof (*s6);
1760 			}
1761 		}
1762 	}
1763 	return (err);
1764 get_all_addrs:
1765 
1766 	/*
1767 	 * Allocate max possible size. We allocate the max. size here because
1768 	 * the clustering module could end up adding addresses to the list.
1769 	 * We allocate upfront so that the clustering module need to bother
1770 	 * re-sizing the list.
1771 	 */
1772 	if (sctp->sctp_family == AF_INET) {
1773 		*size = sizeof (struct sockaddr_in) *
1774 		    sctps->sctps_g_ipifs_count;
1775 	} else {
1776 		*size = sizeof (struct sockaddr_in6) *
1777 		    sctps->sctps_g_ipifs_count;
1778 	}
1779 	*addrlist = kmem_zalloc(*size, KM_SLEEP);
1780 	*addrcnt = 0;
1781 	p = *addrlist;
1782 	rw_enter(&sctps->sctps_g_ipifs_lock, RW_READER);
1783 
1784 	/*
1785 	 * Walk through the global interface list and add all addresses,
1786 	 * except those that are hosted on loopback interfaces.
1787 	 */
1788 	for (cnt = 0; cnt <  SCTP_IPIF_HASH; cnt++) {
1789 		if (sctps->sctps_g_ipifs[cnt].ipif_count == 0)
1790 			continue;
1791 		sctp_ipif = list_head(
1792 		    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list);
1793 		for (icnt = 0;
1794 		    icnt < sctps->sctps_g_ipifs[cnt].ipif_count;
1795 		    icnt++) {
1796 			in6_addr_t	addr;
1797 
1798 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
1799 			addr = sctp_ipif->sctp_ipif_saddr;
1800 			if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) ||
1801 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
1802 			    SCTP_IS_IPIF_LOOPBACK(sctp_ipif) ||
1803 			    SCTP_IS_IPIF_LINKLOCAL(sctp_ipif) ||
1804 			    !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) ||
1805 			    (sctp->sctp_ipversion == IPV4_VERSION &&
1806 			    sctp_ipif->sctp_ipif_isv6) ||
1807 			    (sctp->sctp_connp->conn_ipv6_v6only &&
1808 			    !sctp_ipif->sctp_ipif_isv6)) {
1809 				rw_exit(&sctp_ipif->sctp_ipif_lock);
1810 				sctp_ipif = list_next(
1811 				    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1812 				    sctp_ipif);
1813 				continue;
1814 			}
1815 			rw_exit(&sctp_ipif->sctp_ipif_lock);
1816 			if (sctp->sctp_family == AF_INET) {
1817 				s4 = (struct sockaddr_in *)p;
1818 				IN6_V4MAPPED_TO_INADDR(&addr, &s4->sin_addr);
1819 				s4->sin_family = AF_INET;
1820 				p += sizeof (*s4);
1821 			} else {
1822 				s6 = (struct sockaddr_in6 *)p;
1823 				s6->sin6_addr = addr;
1824 				s6->sin6_family = AF_INET6;
1825 				s6->sin6_scope_id =
1826 				    sctp_ipif->sctp_ipif_ill->sctp_ill_index;
1827 				p += sizeof (*s6);
1828 			}
1829 			(*addrcnt)++;
1830 			sctp_ipif = list_next(
1831 			    &sctps->sctps_g_ipifs[cnt].sctp_ipif_list,
1832 			    sctp_ipif);
1833 		}
1834 	}
1835 	rw_exit(&sctps->sctps_g_ipifs_lock);
1836 	return (err);
1837 }
1838 
1839 /*
1840  * Get a list of addresses from the source address list. The  caller is
1841  * responsible for allocating sufficient buffer for this.
1842  */
1843 void
1844 sctp_get_saddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1845 {
1846 	int			cnt;
1847 	int			icnt;
1848 	sctp_saddr_ipif_t	*obj;
1849 	int			naddr;
1850 	int			scanned = 0;
1851 
1852 	for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1853 		if (sctp->sctp_saddrs[cnt].ipif_count == 0)
1854 			continue;
1855 		obj = list_head(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
1856 		naddr = sctp->sctp_saddrs[cnt].ipif_count;
1857 		for (icnt = 0; icnt < naddr; icnt++) {
1858 			sctp_ipif_t	*ipif;
1859 
1860 			if (psize < sizeof (ipif->sctp_ipif_saddr))
1861 				return;
1862 
1863 			scanned++;
1864 			ipif = obj->saddr_ipifp;
1865 			bcopy(&ipif->sctp_ipif_saddr, p,
1866 			    sizeof (ipif->sctp_ipif_saddr));
1867 			p += sizeof (ipif->sctp_ipif_saddr);
1868 			psize -= sizeof (ipif->sctp_ipif_saddr);
1869 			if (scanned >= sctp->sctp_nsaddrs)
1870 				return;
1871 			obj = list_next(
1872 			    &sctp->sctp_saddrs[icnt].sctp_ipif_list,
1873 			    obj);
1874 		}
1875 	}
1876 }
1877 
1878 /*
1879  * Get a list of addresses from the remote address list. The  caller is
1880  * responsible for allocating sufficient buffer for this.
1881  */
1882 void
1883 sctp_get_faddr_list(sctp_t *sctp, uchar_t *p, size_t psize)
1884 {
1885 	sctp_faddr_t	*fp;
1886 
1887 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1888 		if (psize < sizeof (fp->faddr))
1889 			return;
1890 		bcopy(&fp->faddr, p, sizeof (fp->faddr));
1891 		p += sizeof (fp->faddr);
1892 		psize -= sizeof (fp->faddr);
1893 	}
1894 }
1895 
1896 static void
1897 sctp_free_ills(sctp_stack_t *sctps)
1898 {
1899 	int			i;
1900 	int			l;
1901 	sctp_ill_t	*sctp_ill;
1902 
1903 	if (sctps->sctps_ills_count == 0)
1904 		return;
1905 
1906 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1907 		sctp_ill = list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
1908 		for (l = 0; l < sctps->sctps_g_ills[i].ill_count; l++) {
1909 			ASSERT(sctp_ill->sctp_ill_ipifcnt == 0);
1910 			list_remove(&sctps->sctps_g_ills[i].sctp_ill_list,
1911 			    sctp_ill);
1912 			sctps->sctps_ills_count--;
1913 			kmem_free(sctp_ill->sctp_ill_name,
1914 			    sctp_ill->sctp_ill_name_length);
1915 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
1916 			sctp_ill =
1917 			    list_tail(&sctps->sctps_g_ills[i].sctp_ill_list);
1918 		}
1919 		sctps->sctps_g_ills[i].ill_count = 0;
1920 	}
1921 	ASSERT(sctps->sctps_ills_count == 0);
1922 }
1923 
1924 static void
1925 sctp_free_ipifs(sctp_stack_t *sctps)
1926 {
1927 	int			i;
1928 	int			l;
1929 	sctp_ipif_t	*sctp_ipif;
1930 	sctp_ill_t	*sctp_ill;
1931 
1932 	if (sctps->sctps_g_ipifs_count == 0)
1933 		return;
1934 
1935 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1936 		sctp_ipif = list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1937 		for (l = 0; l < sctps->sctps_g_ipifs[i].ipif_count; l++) {
1938 			sctp_ill = sctp_ipif->sctp_ipif_ill;
1939 
1940 			list_remove(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
1941 			    sctp_ipif);
1942 			sctps->sctps_g_ipifs_count--;
1943 			(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt,
1944 			    -1);
1945 			kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
1946 			sctp_ipif =
1947 			    list_tail(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1948 		}
1949 		sctps->sctps_g_ipifs[i].ipif_count = 0;
1950 	}
1951 	ASSERT(sctps->sctps_g_ipifs_count == 0);
1952 }
1953 
1954 
1955 /* Initialize the SCTP ILL list and lock */
1956 void
1957 sctp_saddr_init(sctp_stack_t *sctps)
1958 {
1959 	int	i;
1960 
1961 	sctps->sctps_g_ills = kmem_zalloc(sizeof (sctp_ill_hash_t) *
1962 	    SCTP_ILL_HASH, KM_SLEEP);
1963 	sctps->sctps_g_ipifs = kmem_zalloc(sizeof (sctp_ipif_hash_t) *
1964 	    SCTP_IPIF_HASH, KM_SLEEP);
1965 
1966 	rw_init(&sctps->sctps_g_ills_lock, NULL, RW_DEFAULT, NULL);
1967 	rw_init(&sctps->sctps_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
1968 
1969 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1970 		sctps->sctps_g_ills[i].ill_count = 0;
1971 		list_create(&sctps->sctps_g_ills[i].sctp_ill_list,
1972 		    sizeof (sctp_ill_t),
1973 		    offsetof(sctp_ill_t, sctp_ills));
1974 	}
1975 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1976 		sctps->sctps_g_ipifs[i].ipif_count = 0;
1977 		list_create(&sctps->sctps_g_ipifs[i].sctp_ipif_list,
1978 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
1979 	}
1980 }
1981 
1982 void
1983 sctp_saddr_fini(sctp_stack_t *sctps)
1984 {
1985 	int	i;
1986 
1987 	sctp_free_ipifs(sctps);
1988 	sctp_free_ills(sctps);
1989 
1990 	for (i = 0; i < SCTP_ILL_HASH; i++)
1991 		list_destroy(&sctps->sctps_g_ills[i].sctp_ill_list);
1992 	for (i = 0; i < SCTP_IPIF_HASH; i++)
1993 		list_destroy(&sctps->sctps_g_ipifs[i].sctp_ipif_list);
1994 
1995 	ASSERT(sctps->sctps_ills_count == 0 && sctps->sctps_g_ipifs_count == 0);
1996 	kmem_free(sctps->sctps_g_ills, sizeof (sctp_ill_hash_t) *
1997 	    SCTP_ILL_HASH);
1998 	sctps->sctps_g_ills = NULL;
1999 	kmem_free(sctps->sctps_g_ipifs, sizeof (sctp_ipif_hash_t) *
2000 	    SCTP_IPIF_HASH);
2001 	sctps->sctps_g_ipifs = NULL;
2002 	rw_destroy(&sctps->sctps_g_ills_lock);
2003 	rw_destroy(&sctps->sctps_g_ipifs_lock);
2004 }
2005