1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/list.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
40*7c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
41*7c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include <inet/common.h>
44*7c478bd9Sstevel@tonic-gate #include <inet/ip.h>
45*7c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
46*7c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
47*7c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
48*7c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
49*7c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
50*7c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static void		sctp_ipif_inactive(sctp_ipif_t *);
53*7c478bd9Sstevel@tonic-gate static sctp_ipif_t	*sctp_lookup_ipif_addr(in6_addr_t *, boolean_t,
54*7c478bd9Sstevel@tonic-gate 			    zoneid_t zoneid);
55*7c478bd9Sstevel@tonic-gate static int		sctp_get_all_ipifs(sctp_t *, int);
56*7c478bd9Sstevel@tonic-gate int			sctp_valid_addr_list(sctp_t *, const void *, uint32_t);
57*7c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_ipif_lookup(sctp_t *, uint_t);
58*7c478bd9Sstevel@tonic-gate static int		sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int);
59*7c478bd9Sstevel@tonic-gate static void		sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *);
60*7c478bd9Sstevel@tonic-gate static int		sctp_compare_ipif_list(sctp_ipif_hash_t *,
61*7c478bd9Sstevel@tonic-gate 			    sctp_ipif_hash_t *);
62*7c478bd9Sstevel@tonic-gate int			sctp_compare_saddrs(sctp_t *, sctp_t *);
63*7c478bd9Sstevel@tonic-gate static int		sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int);
64*7c478bd9Sstevel@tonic-gate int			sctp_dup_saddrs(sctp_t *, sctp_t *, int);
65*7c478bd9Sstevel@tonic-gate void			sctp_free_saddrs(sctp_t *);
66*7c478bd9Sstevel@tonic-gate void			sctp_update_ill(ill_t *, int);
67*7c478bd9Sstevel@tonic-gate void			sctp_update_ipif(ipif_t *, int);
68*7c478bd9Sstevel@tonic-gate void			sctp_move_ipif(ipif_t *, ill_t *, ill_t *);
69*7c478bd9Sstevel@tonic-gate void			sctp_del_saddr(sctp_t *, sctp_saddr_ipif_t *);
70*7c478bd9Sstevel@tonic-gate void			sctp_del_saddr_list(sctp_t *, const void *, int,
71*7c478bd9Sstevel@tonic-gate 			    boolean_t);
72*7c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t	*sctp_saddr_lookup(sctp_t *, in6_addr_t *);
73*7c478bd9Sstevel@tonic-gate in6_addr_t		sctp_get_valid_addr(sctp_t *, boolean_t);
74*7c478bd9Sstevel@tonic-gate int			sctp_getmyaddrs(void *, void *, int *);
75*7c478bd9Sstevel@tonic-gate void			sctp_saddr_init();
76*7c478bd9Sstevel@tonic-gate void			sctp_saddr_fini();
77*7c478bd9Sstevel@tonic-gate #define	SCTP_IPIF_USABLE(sctp_ipif_state)	\
78*7c478bd9Sstevel@tonic-gate 	((sctp_ipif_state) == SCTP_IPIFS_UP ||	\
79*7c478bd9Sstevel@tonic-gate 	((sctp_ipif_state) ==  SCTP_IPIFS_DOWN))
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate #define	SCTP_ILL_HASH_FN(index)		((index) % SCTP_ILL_HASH)
82*7c478bd9Sstevel@tonic-gate #define	SCTP_IPIF_HASH_FN(seqid)	((seqid) % SCTP_IPIF_HASH)
83*7c478bd9Sstevel@tonic-gate #define	SCTP_ILL_TO_PHYINDEX(ill)	((ill)->ill_phyint->phyint_ifindex)
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /* Global list of SCTP ILLs */
86*7c478bd9Sstevel@tonic-gate sctp_ill_hash_t	sctp_g_ills[SCTP_ILL_HASH];
87*7c478bd9Sstevel@tonic-gate uint32_t	sctp_ills_count = 0;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /* Global list of SCTP IPIFs */
90*7c478bd9Sstevel@tonic-gate sctp_ipif_hash_t	sctp_g_ipifs[SCTP_IPIF_HASH];
91*7c478bd9Sstevel@tonic-gate uint32_t		sctp_g_ipifs_count;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate  *
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  * SCTP Interface list manipulation functions, locking used.
97*7c478bd9Sstevel@tonic-gate  *
98*7c478bd9Sstevel@tonic-gate  *
99*7c478bd9Sstevel@tonic-gate  */
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is
103*7c478bd9Sstevel@tonic-gate  * marked as condemned. Also, check if the ILL needs to go away.
104*7c478bd9Sstevel@tonic-gate  * Called with no locks held.
105*7c478bd9Sstevel@tonic-gate  */
106*7c478bd9Sstevel@tonic-gate static void
107*7c478bd9Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif)
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
110*7c478bd9Sstevel@tonic-gate 	uint_t		ipif_index;
111*7c478bd9Sstevel@tonic-gate 	uint_t		ill_index;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
114*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(sctp_ipif->sctp_ipif_id);
117*7c478bd9Sstevel@tonic-gate 	sctp_ill = sctp_ipif->sctp_ipif_ill;
118*7c478bd9Sstevel@tonic-gate 	ASSERT(sctp_ill != NULL);
119*7c478bd9Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index);
120*7c478bd9Sstevel@tonic-gate 	if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED ||
121*7c478bd9Sstevel@tonic-gate 	    sctp_ipif->sctp_ipif_refcnt != 0) {
122*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
123*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
124*7c478bd9Sstevel@tonic-gate 		return;
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 	list_remove(&sctp_g_ipifs[ipif_index].sctp_ipif_list, sctp_ipif);
127*7c478bd9Sstevel@tonic-gate 	sctp_g_ipifs[ipif_index].ipif_count--;
128*7c478bd9Sstevel@tonic-gate 	sctp_g_ipifs_count--;
129*7c478bd9Sstevel@tonic-gate 	rw_destroy(&sctp_ipif->sctp_ipif_lock);
130*7c478bd9Sstevel@tonic-gate 	kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
133*7c478bd9Sstevel@tonic-gate 	if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
134*7c478bd9Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
135*7c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0 &&
136*7c478bd9Sstevel@tonic-gate 		    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
137*7c478bd9Sstevel@tonic-gate 			list_remove(&sctp_g_ills[ill_index].sctp_ill_list,
138*7c478bd9Sstevel@tonic-gate 			    (void *)sctp_ill);
139*7c478bd9Sstevel@tonic-gate 			sctp_g_ills[ill_index].ill_count--;
140*7c478bd9Sstevel@tonic-gate 			sctp_ills_count--;
141*7c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
142*7c478bd9Sstevel@tonic-gate 			    sctp_ill->sctp_ill_name_length);
143*7c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
147*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate /*
151*7c478bd9Sstevel@tonic-gate  * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt.
152*7c478bd9Sstevel@tonic-gate  * Called with no locks held.
153*7c478bd9Sstevel@tonic-gate  */
154*7c478bd9Sstevel@tonic-gate static sctp_ipif_t *
155*7c478bd9Sstevel@tonic-gate sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, zoneid_t zoneid)
156*7c478bd9Sstevel@tonic-gate {
157*7c478bd9Sstevel@tonic-gate 	int		i;
158*7c478bd9Sstevel@tonic-gate 	int		j;
159*7c478bd9Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
162*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
163*7c478bd9Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
164*7c478bd9Sstevel@tonic-gate 			continue;
165*7c478bd9Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
166*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
167*7c478bd9Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
168*7c478bd9Sstevel@tonic-gate 			if (zoneid == sctp_ipif->sctp_ipif_zoneid &&
169*7c478bd9Sstevel@tonic-gate 			    SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) &&
170*7c478bd9Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr,
171*7c478bd9Sstevel@tonic-gate 			    addr)) {
172*7c478bd9Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
173*7c478bd9Sstevel@tonic-gate 				if (refhold)
174*7c478bd9Sstevel@tonic-gate 					SCTP_IPIF_REFHOLD(sctp_ipif);
175*7c478bd9Sstevel@tonic-gate 				rw_exit(&sctp_g_ipifs_lock);
176*7c478bd9Sstevel@tonic-gate 				return (sctp_ipif);
177*7c478bd9Sstevel@tonic-gate 			}
178*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
179*7c478bd9Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
180*7c478bd9Sstevel@tonic-gate 			    sctp_ipif);
181*7c478bd9Sstevel@tonic-gate 		}
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
184*7c478bd9Sstevel@tonic-gate 	return (NULL);
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate /*
188*7c478bd9Sstevel@tonic-gate  * Populate the list with all the SCTP ipifs for a given ipversion.
189*7c478bd9Sstevel@tonic-gate  * Increments sctp_ipif refcnt.
190*7c478bd9Sstevel@tonic-gate  * Called with no locks held.
191*7c478bd9Sstevel@tonic-gate  */
192*7c478bd9Sstevel@tonic-gate static int
193*7c478bd9Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep)
194*7c478bd9Sstevel@tonic-gate {
195*7c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
196*7c478bd9Sstevel@tonic-gate 	int			i;
197*7c478bd9Sstevel@tonic-gate 	int			j;
198*7c478bd9Sstevel@tonic-gate 	int			error = 0;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
201*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
202*7c478bd9Sstevel@tonic-gate 		if (sctp_g_ipifs[i].ipif_count == 0)
203*7c478bd9Sstevel@tonic-gate 			continue;
204*7c478bd9Sstevel@tonic-gate 		sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list);
205*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) {
206*7c478bd9Sstevel@tonic-gate 			rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER);
207*7c478bd9Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_zoneid != sctp->sctp_zoneid ||
208*7c478bd9Sstevel@tonic-gate 			    !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) ||
209*7c478bd9Sstevel@tonic-gate 			    (sctp->sctp_ipversion == IPV4_VERSION &&
210*7c478bd9Sstevel@tonic-gate 			    !IN6_IS_ADDR_V4MAPPED(
211*7c478bd9Sstevel@tonic-gate 			    &sctp_ipif->sctp_ipif_saddr)) ||
212*7c478bd9Sstevel@tonic-gate 			    (sctp->sctp_connp->conn_ipv6_v6only &&
213*7c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(
214*7c478bd9Sstevel@tonic-gate 			    &sctp_ipif->sctp_ipif_saddr))) {
215*7c478bd9Sstevel@tonic-gate 				rw_exit(&sctp_ipif->sctp_ipif_lock);
216*7c478bd9Sstevel@tonic-gate 				sctp_ipif = list_next(
217*7c478bd9Sstevel@tonic-gate 				    &sctp_g_ipifs[i].sctp_ipif_list, sctp_ipif);
218*7c478bd9Sstevel@tonic-gate 				continue;
219*7c478bd9Sstevel@tonic-gate 			}
220*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_ipif->sctp_ipif_lock);
221*7c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFHOLD(sctp_ipif);
222*7c478bd9Sstevel@tonic-gate 			error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep);
223*7c478bd9Sstevel@tonic-gate 			if (error != 0)
224*7c478bd9Sstevel@tonic-gate 				goto free_stuff;
225*7c478bd9Sstevel@tonic-gate 			sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list,
226*7c478bd9Sstevel@tonic-gate 			    sctp_ipif);
227*7c478bd9Sstevel@tonic-gate 		}
228*7c478bd9Sstevel@tonic-gate 	}
229*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
230*7c478bd9Sstevel@tonic-gate 	return (0);
231*7c478bd9Sstevel@tonic-gate free_stuff:
232*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
233*7c478bd9Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
234*7c478bd9Sstevel@tonic-gate 	return (ENOMEM);
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate /*
238*7c478bd9Sstevel@tonic-gate  * Given a list of address, fills in the list of SCTP ipifs if all the addresses
239*7c478bd9Sstevel@tonic-gate  * are present in the SCTP interface list, return number of addresses filled
240*7c478bd9Sstevel@tonic-gate  * or error.
241*7c478bd9Sstevel@tonic-gate  * Called with no locks held.
242*7c478bd9Sstevel@tonic-gate  */
243*7c478bd9Sstevel@tonic-gate int
244*7c478bd9Sstevel@tonic-gate sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
247*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
248*7c478bd9Sstevel@tonic-gate 	struct in_addr		*addr4;
249*7c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
250*7c478bd9Sstevel@tonic-gate 	int			cnt;
251*7c478bd9Sstevel@tonic-gate 	int			err = 0;
252*7c478bd9Sstevel@tonic-gate 	int			saddr_cnt = 0;
253*7c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*ipif;
254*7c478bd9Sstevel@tonic-gate 	boolean_t		bind_to_all = B_FALSE;
255*7c478bd9Sstevel@tonic-gate 	boolean_t		check_addrs = B_FALSE;
256*7c478bd9Sstevel@tonic-gate 	boolean_t		check_lport = B_FALSE;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	/*
259*7c478bd9Sstevel@tonic-gate 	 * Need to check for port and address depending on the state.
260*7c478bd9Sstevel@tonic-gate 	 * After a socket is bound, we need to make sure that subsequent
261*7c478bd9Sstevel@tonic-gate 	 * bindx() has correct port.  After an association is established,
262*7c478bd9Sstevel@tonic-gate 	 * we need to check for changing the bound address to invalid
263*7c478bd9Sstevel@tonic-gate 	 * addresses.
264*7c478bd9Sstevel@tonic-gate 	 */
265*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_BOUND) {
266*7c478bd9Sstevel@tonic-gate 		check_lport = B_TRUE;
267*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_LISTEN)
268*7c478bd9Sstevel@tonic-gate 			check_addrs = B_TRUE;
269*7c478bd9Sstevel@tonic-gate 	}
270*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
271*7c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
272*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
273*7c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
274*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < addrcnt; cnt++) {
275*7c478bd9Sstevel@tonic-gate 		boolean_t	lookup_saddr = B_TRUE;
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 		switch (sctp->sctp_family) {
278*7c478bd9Sstevel@tonic-gate 		case AF_INET:
279*7c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
280*7c478bd9Sstevel@tonic-gate 			if (sin4->sin_family != AF_INET || (check_lport &&
281*7c478bd9Sstevel@tonic-gate 			    sin4->sin_port != sctp->sctp_lport)) {
282*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
283*7c478bd9Sstevel@tonic-gate 				goto free_ret;
284*7c478bd9Sstevel@tonic-gate 			}
285*7c478bd9Sstevel@tonic-gate 			addr4 = &sin4->sin_addr;
286*7c478bd9Sstevel@tonic-gate 			if (check_addrs &&
287*7c478bd9Sstevel@tonic-gate 			    (addr4->s_addr == INADDR_ANY ||
288*7c478bd9Sstevel@tonic-gate 			    addr4->s_addr == INADDR_BROADCAST ||
289*7c478bd9Sstevel@tonic-gate 			    IN_MULTICAST(addr4->s_addr))) {
290*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
291*7c478bd9Sstevel@tonic-gate 				goto free_ret;
292*7c478bd9Sstevel@tonic-gate 			}
293*7c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(addr4, &addr);
294*7c478bd9Sstevel@tonic-gate 			if (!check_addrs && addr4->s_addr == INADDR_ANY) {
295*7c478bd9Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
296*7c478bd9Sstevel@tonic-gate 				bind_to_all = B_TRUE;
297*7c478bd9Sstevel@tonic-gate 			}
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 			break;
300*7c478bd9Sstevel@tonic-gate 		case AF_INET6:
301*7c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
302*7c478bd9Sstevel@tonic-gate 			if (sin6->sin6_family != AF_INET6 || (check_lport &&
303*7c478bd9Sstevel@tonic-gate 			    sin6->sin6_port != sctp->sctp_lport)) {
304*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
305*7c478bd9Sstevel@tonic-gate 				goto free_ret;
306*7c478bd9Sstevel@tonic-gate 			}
307*7c478bd9Sstevel@tonic-gate 			addr = sin6->sin6_addr;
308*7c478bd9Sstevel@tonic-gate 			if (sctp->sctp_connp->conn_ipv6_v6only &&
309*7c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&addr)) {
310*7c478bd9Sstevel@tonic-gate 				err = EAFNOSUPPORT;
311*7c478bd9Sstevel@tonic-gate 				goto free_ret;
312*7c478bd9Sstevel@tonic-gate 			}
313*7c478bd9Sstevel@tonic-gate 			if (check_addrs &&
314*7c478bd9Sstevel@tonic-gate 			    (IN6_IS_ADDR_LINKLOCAL(&addr) ||
315*7c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_MULTICAST(&addr) ||
316*7c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_UNSPECIFIED(&addr))) {
317*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
318*7c478bd9Sstevel@tonic-gate 				goto free_ret;
319*7c478bd9Sstevel@tonic-gate 			}
320*7c478bd9Sstevel@tonic-gate 			if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) {
321*7c478bd9Sstevel@tonic-gate 				lookup_saddr = B_FALSE;
322*7c478bd9Sstevel@tonic-gate 				bind_to_all = B_TRUE;
323*7c478bd9Sstevel@tonic-gate 			}
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 			break;
326*7c478bd9Sstevel@tonic-gate 		default:
327*7c478bd9Sstevel@tonic-gate 			err = EAFNOSUPPORT;
328*7c478bd9Sstevel@tonic-gate 			goto free_ret;
329*7c478bd9Sstevel@tonic-gate 		}
330*7c478bd9Sstevel@tonic-gate 		if (lookup_saddr) {
331*7c478bd9Sstevel@tonic-gate 			ipif = sctp_lookup_ipif_addr(&addr, B_TRUE,
332*7c478bd9Sstevel@tonic-gate 			    sctp->sctp_zoneid);
333*7c478bd9Sstevel@tonic-gate 			if (ipif == NULL) {
334*7c478bd9Sstevel@tonic-gate 				/* Address not in the list */
335*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
336*7c478bd9Sstevel@tonic-gate 				goto free_ret;
337*7c478bd9Sstevel@tonic-gate 			} else if (check_addrs &&
338*7c478bd9Sstevel@tonic-gate 			    (ipif->sctp_ipif_ill->sctp_ill_flags &
339*7c478bd9Sstevel@tonic-gate 			    PHYI_LOOPBACK)) {
340*7c478bd9Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
341*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
342*7c478bd9Sstevel@tonic-gate 				goto free_ret;
343*7c478bd9Sstevel@tonic-gate 			}
344*7c478bd9Sstevel@tonic-gate 		}
345*7c478bd9Sstevel@tonic-gate 		if (!bind_to_all) {
346*7c478bd9Sstevel@tonic-gate 			err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP);
347*7c478bd9Sstevel@tonic-gate 			if (err != 0) {
348*7c478bd9Sstevel@tonic-gate 				SCTP_IPIF_REFRELE(ipif);
349*7c478bd9Sstevel@tonic-gate 				if (check_addrs && err == EALREADY)
350*7c478bd9Sstevel@tonic-gate 					err = EADDRINUSE;
351*7c478bd9Sstevel@tonic-gate 				goto free_ret;
352*7c478bd9Sstevel@tonic-gate 			}
353*7c478bd9Sstevel@tonic-gate 			saddr_cnt++;
354*7c478bd9Sstevel@tonic-gate 		}
355*7c478bd9Sstevel@tonic-gate 	}
356*7c478bd9Sstevel@tonic-gate 	if (bind_to_all) {
357*7c478bd9Sstevel@tonic-gate 		/*
358*7c478bd9Sstevel@tonic-gate 		 * Free whatever we might have added before encountering
359*7c478bd9Sstevel@tonic-gate 		 * inaddr_any.
360*7c478bd9Sstevel@tonic-gate 		 */
361*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_nsaddrs > 0) {
362*7c478bd9Sstevel@tonic-gate 			sctp_free_saddrs(sctp);
363*7c478bd9Sstevel@tonic-gate 			ASSERT(sctp->sctp_nsaddrs == 0);
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 		err = sctp_get_all_ipifs(sctp, KM_SLEEP);
366*7c478bd9Sstevel@tonic-gate 		if (err != 0)
367*7c478bd9Sstevel@tonic-gate 			return (err);
368*7c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
369*7c478bd9Sstevel@tonic-gate 	}
370*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
371*7c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
372*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
373*7c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
374*7c478bd9Sstevel@tonic-gate 	return (0);
375*7c478bd9Sstevel@tonic-gate free_ret:
376*7c478bd9Sstevel@tonic-gate 	if (saddr_cnt != 0)
377*7c478bd9Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE);
378*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
379*7c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
380*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
381*7c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
382*7c478bd9Sstevel@tonic-gate 	return (err);
383*7c478bd9Sstevel@tonic-gate }
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *
386*7c478bd9Sstevel@tonic-gate sctp_ipif_lookup(sctp_t *sctp, uint_t ipif_index)
387*7c478bd9Sstevel@tonic-gate {
388*7c478bd9Sstevel@tonic-gate 	int			cnt;
389*7c478bd9Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif_index);
390*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_saddrs[seqid].ipif_count == 0)
393*7c478bd9Sstevel@tonic-gate 		return (NULL);
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
396*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
397*7c478bd9Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif_index)
398*7c478bd9Sstevel@tonic-gate 			return (ipif_obj);
399*7c478bd9Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
400*7c478bd9Sstevel@tonic-gate 		    ipif_obj);
401*7c478bd9Sstevel@tonic-gate 	}
402*7c478bd9Sstevel@tonic-gate 	return (NULL);
403*7c478bd9Sstevel@tonic-gate }
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate static int
406*7c478bd9Sstevel@tonic-gate sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep)
407*7c478bd9Sstevel@tonic-gate {
408*7c478bd9Sstevel@tonic-gate 	int			cnt;
409*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
410*7c478bd9Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
413*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
414*7c478bd9Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id)
415*7c478bd9Sstevel@tonic-gate 			return (EALREADY);
416*7c478bd9Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
417*7c478bd9Sstevel@tonic-gate 		    ipif_obj);
418*7c478bd9Sstevel@tonic-gate 	}
419*7c478bd9Sstevel@tonic-gate 	ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep);
420*7c478bd9Sstevel@tonic-gate 	if (ipif_obj == NULL) {
421*7c478bd9Sstevel@tonic-gate 		/* Need to do something */
422*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
423*7c478bd9Sstevel@tonic-gate 	}
424*7c478bd9Sstevel@tonic-gate 	ipif_obj->saddr_ipifp = ipif;
425*7c478bd9Sstevel@tonic-gate 	list_insert_tail(&sctp->sctp_saddrs[seqid].sctp_ipif_list, ipif_obj);
426*7c478bd9Sstevel@tonic-gate 	sctp->sctp_saddrs[seqid].ipif_count++;
427*7c478bd9Sstevel@tonic-gate 	sctp->sctp_nsaddrs++;
428*7c478bd9Sstevel@tonic-gate 	return (0);
429*7c478bd9Sstevel@tonic-gate }
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate static void
432*7c478bd9Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif)
433*7c478bd9Sstevel@tonic-gate {
434*7c478bd9Sstevel@tonic-gate 	int			cnt;
435*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*ipif_obj;
436*7c478bd9Sstevel@tonic-gate 	int			seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id);
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 	ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list);
439*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) {
440*7c478bd9Sstevel@tonic-gate 		if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) {
441*7c478bd9Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
442*7c478bd9Sstevel@tonic-gate 			    ipif_obj);
443*7c478bd9Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
444*7c478bd9Sstevel@tonic-gate 			sctp->sctp_saddrs[seqid].ipif_count--;
445*7c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp);
446*7c478bd9Sstevel@tonic-gate 			kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t));
447*7c478bd9Sstevel@tonic-gate 			break;
448*7c478bd9Sstevel@tonic-gate 		}
449*7c478bd9Sstevel@tonic-gate 		ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list,
450*7c478bd9Sstevel@tonic-gate 		    ipif_obj);
451*7c478bd9Sstevel@tonic-gate 	}
452*7c478bd9Sstevel@tonic-gate }
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate static int
455*7c478bd9Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2)
456*7c478bd9Sstevel@tonic-gate {
457*7c478bd9Sstevel@tonic-gate 	int			i;
458*7c478bd9Sstevel@tonic-gate 	int			j;
459*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj1;
460*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj2;
461*7c478bd9Sstevel@tonic-gate 	int			overlap = 0;
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	obj1 = list_head(&list1->sctp_ipif_list);
464*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
465*7c478bd9Sstevel@tonic-gate 		obj2 = list_head(&list2->sctp_ipif_list);
466*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < list2->ipif_count; j++) {
467*7c478bd9Sstevel@tonic-gate 			if (obj1->saddr_ipifp->sctp_ipif_id ==
468*7c478bd9Sstevel@tonic-gate 			    obj2->saddr_ipifp->sctp_ipif_id) {
469*7c478bd9Sstevel@tonic-gate 				overlap++;
470*7c478bd9Sstevel@tonic-gate 				break;
471*7c478bd9Sstevel@tonic-gate 			}
472*7c478bd9Sstevel@tonic-gate 			obj2 = list_next(&list2->sctp_ipif_list,
473*7c478bd9Sstevel@tonic-gate 			    obj2);
474*7c478bd9Sstevel@tonic-gate 		}
475*7c478bd9Sstevel@tonic-gate 		obj1 = list_next(&list1->sctp_ipif_list, obj1);
476*7c478bd9Sstevel@tonic-gate 	}
477*7c478bd9Sstevel@tonic-gate 	return (overlap);
478*7c478bd9Sstevel@tonic-gate }
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate int
481*7c478bd9Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2)
482*7c478bd9Sstevel@tonic-gate {
483*7c478bd9Sstevel@tonic-gate 	int		i;
484*7c478bd9Sstevel@tonic-gate 	int		overlap = 0;
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
487*7c478bd9Sstevel@tonic-gate 		overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i],
488*7c478bd9Sstevel@tonic-gate 		    &sctp2->sctp_saddrs[i]);
489*7c478bd9Sstevel@tonic-gate 	}
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs &&
492*7c478bd9Sstevel@tonic-gate 	    overlap == sctp1->sctp_nsaddrs) {
493*7c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
494*7c478bd9Sstevel@tonic-gate 	}
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	if (overlap == sctp1->sctp_nsaddrs)
497*7c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 	if (overlap > 0)
500*7c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
503*7c478bd9Sstevel@tonic-gate }
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate static int
506*7c478bd9Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep)
507*7c478bd9Sstevel@tonic-gate {
508*7c478bd9Sstevel@tonic-gate 	int			i;
509*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
510*7c478bd9Sstevel@tonic-gate 	int			error = 0;
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 	obj = list_head(&list1->sctp_ipif_list);
513*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < list1->ipif_count; i++) {
514*7c478bd9Sstevel@tonic-gate 		SCTP_IPIF_REFHOLD(obj->saddr_ipifp);
515*7c478bd9Sstevel@tonic-gate 		error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep);
516*7c478bd9Sstevel@tonic-gate 		if (error != 0)
517*7c478bd9Sstevel@tonic-gate 			return (error);
518*7c478bd9Sstevel@tonic-gate 		obj = list_next(&list1->sctp_ipif_list, obj);
519*7c478bd9Sstevel@tonic-gate 	}
520*7c478bd9Sstevel@tonic-gate 	return (error);
521*7c478bd9Sstevel@tonic-gate }
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate int
524*7c478bd9Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep)
525*7c478bd9Sstevel@tonic-gate {
526*7c478bd9Sstevel@tonic-gate 	int	error = 0;
527*7c478bd9Sstevel@tonic-gate 	int	i;
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 	if (sctp1 == NULL || sctp1->sctp_bound_to_all)
530*7c478bd9Sstevel@tonic-gate 		return (sctp_get_all_ipifs(sctp2, sleep));
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
533*7c478bd9Sstevel@tonic-gate 		if (sctp1->sctp_saddrs[i].ipif_count == 0)
534*7c478bd9Sstevel@tonic-gate 			continue;
535*7c478bd9Sstevel@tonic-gate 		error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep);
536*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
537*7c478bd9Sstevel@tonic-gate 			sctp_free_saddrs(sctp2);
538*7c478bd9Sstevel@tonic-gate 			return (error);
539*7c478bd9Sstevel@tonic-gate 		}
540*7c478bd9Sstevel@tonic-gate 	}
541*7c478bd9Sstevel@tonic-gate 	return (0);
542*7c478bd9Sstevel@tonic-gate }
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate void
545*7c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp)
546*7c478bd9Sstevel@tonic-gate {
547*7c478bd9Sstevel@tonic-gate 	int			i;
548*7c478bd9Sstevel@tonic-gate 	int			l;
549*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
552*7c478bd9Sstevel@tonic-gate 		return;
553*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
554*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
555*7c478bd9Sstevel@tonic-gate 			continue;
556*7c478bd9Sstevel@tonic-gate 		obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
557*7c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
558*7c478bd9Sstevel@tonic-gate 			list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj);
559*7c478bd9Sstevel@tonic-gate 			SCTP_IPIF_REFRELE(obj->saddr_ipifp);
560*7c478bd9Sstevel@tonic-gate 			sctp->sctp_nsaddrs--;
561*7c478bd9Sstevel@tonic-gate 			kmem_free(obj, sizeof (sctp_saddr_ipif_t));
562*7c478bd9Sstevel@tonic-gate 			obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list);
563*7c478bd9Sstevel@tonic-gate 		}
564*7c478bd9Sstevel@tonic-gate 		sctp->sctp_saddrs[i].ipif_count = 0;
565*7c478bd9Sstevel@tonic-gate 	}
566*7c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs == 0);
567*7c478bd9Sstevel@tonic-gate }
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate /*
570*7c478bd9Sstevel@tonic-gate  * Add/Delete the given ILL from the SCTP ILL list. Called with no locks
571*7c478bd9Sstevel@tonic-gate  * held.
572*7c478bd9Sstevel@tonic-gate  */
573*7c478bd9Sstevel@tonic-gate void
574*7c478bd9Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op)
575*7c478bd9Sstevel@tonic-gate {
576*7c478bd9Sstevel@tonic-gate 	int		i;
577*7c478bd9Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill = NULL;
578*7c478bd9Sstevel@tonic-gate 	uint_t		index;
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 	ip2dbg(("sctp_update_ill: %s\n", ill->ill_name));
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_WRITER);
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
585*7c478bd9Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
586*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
587*7c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
588*7c478bd9Sstevel@tonic-gate 			break;
589*7c478bd9Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
590*7c478bd9Sstevel@tonic-gate 		    sctp_ill);
591*7c478bd9Sstevel@tonic-gate 	}
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	switch (op) {
594*7c478bd9Sstevel@tonic-gate 	case SCTP_ILL_INSERT:
595*7c478bd9Sstevel@tonic-gate 		if (sctp_ill != NULL) {
596*7c478bd9Sstevel@tonic-gate 			/* Unmark it if it is condemned */
597*7c478bd9Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED)
598*7c478bd9Sstevel@tonic-gate 				sctp_ill->sctp_ill_state = 0;
599*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
600*7c478bd9Sstevel@tonic-gate 			return;
601*7c478bd9Sstevel@tonic-gate 		}
602*7c478bd9Sstevel@tonic-gate 		sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP);
603*7c478bd9Sstevel@tonic-gate 		/* Need to re-try? */
604*7c478bd9Sstevel@tonic-gate 		if (sctp_ill == NULL) {
605*7c478bd9Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
606*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
607*7c478bd9Sstevel@tonic-gate 			return;
608*7c478bd9Sstevel@tonic-gate 		}
609*7c478bd9Sstevel@tonic-gate 		sctp_ill->sctp_ill_name =
610*7c478bd9Sstevel@tonic-gate 		    kmem_zalloc(ill->ill_name_length, KM_NOSLEEP);
611*7c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_name == NULL) {
612*7c478bd9Sstevel@tonic-gate 			ip1dbg(("sctp_ill_insert: mem error..\n"));
613*7c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
614*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
615*7c478bd9Sstevel@tonic-gate 			return;
616*7c478bd9Sstevel@tonic-gate 		}
617*7c478bd9Sstevel@tonic-gate 		bcopy(ill->ill_name, sctp_ill->sctp_ill_name,
618*7c478bd9Sstevel@tonic-gate 		    ill->ill_name_length);
619*7c478bd9Sstevel@tonic-gate 		sctp_ill->sctp_ill_name_length = ill->ill_name_length;
620*7c478bd9Sstevel@tonic-gate 		sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill);
621*7c478bd9Sstevel@tonic-gate 		sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags;
622*7c478bd9Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ills[index].sctp_ill_list,
623*7c478bd9Sstevel@tonic-gate 		    (void *)sctp_ill);
624*7c478bd9Sstevel@tonic-gate 		sctp_g_ills[index].ill_count++;
625*7c478bd9Sstevel@tonic-gate 		sctp_ills_count++;
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate 		break;
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 	case SCTP_ILL_REMOVE:
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 		if (sctp_ill == NULL) {
632*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
633*7c478bd9Sstevel@tonic-gate 			return;
634*7c478bd9Sstevel@tonic-gate 		}
635*7c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_ipifcnt == 0) {
636*7c478bd9Sstevel@tonic-gate 			list_remove(&sctp_g_ills[index].sctp_ill_list,
637*7c478bd9Sstevel@tonic-gate 			    (void *)sctp_ill);
638*7c478bd9Sstevel@tonic-gate 			sctp_g_ills[index].ill_count--;
639*7c478bd9Sstevel@tonic-gate 			sctp_ills_count--;
640*7c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill->sctp_ill_name,
641*7c478bd9Sstevel@tonic-gate 			    ill->ill_name_length);
642*7c478bd9Sstevel@tonic-gate 			kmem_free(sctp_ill, sizeof (sctp_ill_t));
643*7c478bd9Sstevel@tonic-gate 		} else {
644*7c478bd9Sstevel@tonic-gate 			sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED;
645*7c478bd9Sstevel@tonic-gate 		}
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 		break;
648*7c478bd9Sstevel@tonic-gate 	}
649*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
650*7c478bd9Sstevel@tonic-gate }
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate /* move ipif from f_ill to t_ill */
653*7c478bd9Sstevel@tonic-gate void
654*7c478bd9Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill)
655*7c478bd9Sstevel@tonic-gate {
656*7c478bd9Sstevel@tonic-gate 	sctp_ill_t	*fsctp_ill = NULL;
657*7c478bd9Sstevel@tonic-gate 	sctp_ill_t	*tsctp_ill = NULL;
658*7c478bd9Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
659*7c478bd9Sstevel@tonic-gate 	uint_t		index;
660*7c478bd9Sstevel@tonic-gate 	int		i;
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
663*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_READER);
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill));
666*7c478bd9Sstevel@tonic-gate 	fsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
667*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
668*7c478bd9Sstevel@tonic-gate 		if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill))
669*7c478bd9Sstevel@tonic-gate 			break;
670*7c478bd9Sstevel@tonic-gate 		fsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
671*7c478bd9Sstevel@tonic-gate 		    fsctp_ill);
672*7c478bd9Sstevel@tonic-gate 	}
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 	index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill));
675*7c478bd9Sstevel@tonic-gate 	tsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list);
676*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[index].ill_count; i++) {
677*7c478bd9Sstevel@tonic-gate 		if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill))
678*7c478bd9Sstevel@tonic-gate 			break;
679*7c478bd9Sstevel@tonic-gate 		tsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list,
680*7c478bd9Sstevel@tonic-gate 		    tsctp_ill);
681*7c478bd9Sstevel@tonic-gate 	}
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 	index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
684*7c478bd9Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[index].sctp_ipif_list);
685*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[index].ipif_count; i++) {
686*7c478bd9Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
687*7c478bd9Sstevel@tonic-gate 			break;
688*7c478bd9Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[index].sctp_ipif_list,
689*7c478bd9Sstevel@tonic-gate 		    sctp_ipif);
690*7c478bd9Sstevel@tonic-gate 	}
691*7c478bd9Sstevel@tonic-gate 	/* Should be an ASSERT? */
692*7c478bd9Sstevel@tonic-gate 	if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) {
693*7c478bd9Sstevel@tonic-gate 		ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n",
694*7c478bd9Sstevel@tonic-gate 		    (void *)ipif, (void *)f_ill, (void *)t_ill));
695*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
696*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
697*7c478bd9Sstevel@tonic-gate 		return;
698*7c478bd9Sstevel@tonic-gate 	}
699*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
700*7c478bd9Sstevel@tonic-gate 	ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill);
701*7c478bd9Sstevel@tonic-gate 	sctp_ipif->sctp_ipif_ill = tsctp_ill;
702*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_ipif->sctp_ipif_lock);
703*7c478bd9Sstevel@tonic-gate 	(void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1);
704*7c478bd9Sstevel@tonic-gate 	atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1);
705*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
706*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
707*7c478bd9Sstevel@tonic-gate }
708*7c478bd9Sstevel@tonic-gate 
709*7c478bd9Sstevel@tonic-gate /* Insert, Remove,  Mark up or Mark down the ipif */
710*7c478bd9Sstevel@tonic-gate void
711*7c478bd9Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op)
712*7c478bd9Sstevel@tonic-gate {
713*7c478bd9Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
714*7c478bd9Sstevel@tonic-gate 	int		i;
715*7c478bd9Sstevel@tonic-gate 	sctp_ill_t	*sctp_ill;
716*7c478bd9Sstevel@tonic-gate 	sctp_ipif_t	*sctp_ipif;
717*7c478bd9Sstevel@tonic-gate 	uint_t		ill_index;
718*7c478bd9Sstevel@tonic-gate 	uint_t		ipif_index;
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 	ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid));
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ills_lock, RW_READER);
723*7c478bd9Sstevel@tonic-gate 	rw_enter(&sctp_g_ipifs_lock, RW_WRITER);
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate 	ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill));
726*7c478bd9Sstevel@tonic-gate 	sctp_ill = list_head(&sctp_g_ills[ill_index].sctp_ill_list);
727*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ills[ill_index].ill_count; i++) {
728*7c478bd9Sstevel@tonic-gate 		if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill))
729*7c478bd9Sstevel@tonic-gate 			break;
730*7c478bd9Sstevel@tonic-gate 		sctp_ill = list_next(&sctp_g_ills[ill_index].sctp_ill_list,
731*7c478bd9Sstevel@tonic-gate 		    sctp_ill);
732*7c478bd9Sstevel@tonic-gate 	}
733*7c478bd9Sstevel@tonic-gate 	if (sctp_ill == NULL) {
734*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
735*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
736*7c478bd9Sstevel@tonic-gate 		return;
737*7c478bd9Sstevel@tonic-gate 	}
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate 	ipif_index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid);
740*7c478bd9Sstevel@tonic-gate 	sctp_ipif = list_head(&sctp_g_ipifs[ipif_index].sctp_ipif_list);
741*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sctp_g_ipifs[ipif_index].ipif_count; i++) {
742*7c478bd9Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid)
743*7c478bd9Sstevel@tonic-gate 			break;
744*7c478bd9Sstevel@tonic-gate 		sctp_ipif = list_next(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
745*7c478bd9Sstevel@tonic-gate 		    sctp_ipif);
746*7c478bd9Sstevel@tonic-gate 	}
747*7c478bd9Sstevel@tonic-gate 	if (op != SCTP_IPIF_INSERT && sctp_ipif == NULL) {
748*7c478bd9Sstevel@tonic-gate 		ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op));
749*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ipifs_lock);
750*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_g_ills_lock);
751*7c478bd9Sstevel@tonic-gate 		return;
752*7c478bd9Sstevel@tonic-gate 	}
753*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
754*7c478bd9Sstevel@tonic-gate 	if (sctp_ipif != NULL)
755*7c478bd9Sstevel@tonic-gate 		ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill);
756*7c478bd9Sstevel@tonic-gate #endif
757*7c478bd9Sstevel@tonic-gate 	switch (op) {
758*7c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_INSERT:
759*7c478bd9Sstevel@tonic-gate 		if (sctp_ipif != NULL) {
760*7c478bd9Sstevel@tonic-gate 			if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED)
761*7c478bd9Sstevel@tonic-gate 				sctp_ipif->sctp_ipif_state = 0;
762*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
763*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
764*7c478bd9Sstevel@tonic-gate 			return;
765*7c478bd9Sstevel@tonic-gate 		}
766*7c478bd9Sstevel@tonic-gate 		sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP);
767*7c478bd9Sstevel@tonic-gate 		/* Try again? */
768*7c478bd9Sstevel@tonic-gate 		if (sctp_ipif == NULL) {
769*7c478bd9Sstevel@tonic-gate 			ip1dbg(("sctp_ipif_insert: mem failure..\n"));
770*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
771*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
772*7c478bd9Sstevel@tonic-gate 			return;
773*7c478bd9Sstevel@tonic-gate 		}
774*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_id = ipif->ipif_seqid;
775*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_ill = sctp_ill;
776*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
777*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
778*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
779*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
780*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6;
781*7c478bd9Sstevel@tonic-gate 		rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL);
782*7c478bd9Sstevel@tonic-gate 		list_insert_tail(&sctp_g_ipifs[ipif_index].sctp_ipif_list,
783*7c478bd9Sstevel@tonic-gate 		    (void *)sctp_ipif);
784*7c478bd9Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count++;
785*7c478bd9Sstevel@tonic-gate 		sctp_g_ipifs_count++;
786*7c478bd9Sstevel@tonic-gate 		atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1);
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 		break;
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_REMOVE:
791*7c478bd9Sstevel@tonic-gate 	{
792*7c478bd9Sstevel@tonic-gate 		list_t		*ipif_list;
793*7c478bd9Sstevel@tonic-gate 		list_t		*ill_list;
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate 		ill_list = &sctp_g_ills[ill_index].sctp_ill_list;
796*7c478bd9Sstevel@tonic-gate 		ipif_list = &sctp_g_ipifs[ipif_index].sctp_ipif_list;
797*7c478bd9Sstevel@tonic-gate 		if (sctp_ipif->sctp_ipif_refcnt != 0) {
798*7c478bd9Sstevel@tonic-gate 			sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED;
799*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ipifs_lock);
800*7c478bd9Sstevel@tonic-gate 			rw_exit(&sctp_g_ills_lock);
801*7c478bd9Sstevel@tonic-gate 			return;
802*7c478bd9Sstevel@tonic-gate 		}
803*7c478bd9Sstevel@tonic-gate 		list_remove(ipif_list, (void *)sctp_ipif);
804*7c478bd9Sstevel@tonic-gate 		sctp_g_ipifs[ipif_index].ipif_count--;
805*7c478bd9Sstevel@tonic-gate 		sctp_g_ipifs_count--;
806*7c478bd9Sstevel@tonic-gate 		rw_destroy(&sctp_ipif->sctp_ipif_lock);
807*7c478bd9Sstevel@tonic-gate 		kmem_free(sctp_ipif, sizeof (sctp_ipif_t));
808*7c478bd9Sstevel@tonic-gate 		(void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1);
809*7c478bd9Sstevel@tonic-gate 		if (rw_tryupgrade(&sctp_g_ills_lock) != 0) {
810*7c478bd9Sstevel@tonic-gate 			rw_downgrade(&sctp_g_ipifs_lock);
811*7c478bd9Sstevel@tonic-gate 			if (sctp_ill->sctp_ill_ipifcnt == 0 &&
812*7c478bd9Sstevel@tonic-gate 			    sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) {
813*7c478bd9Sstevel@tonic-gate 				list_remove(ill_list, (void *)sctp_ill);
814*7c478bd9Sstevel@tonic-gate 				sctp_ills_count--;
815*7c478bd9Sstevel@tonic-gate 				sctp_g_ills[ill_index].ill_count--;
816*7c478bd9Sstevel@tonic-gate 				kmem_free(sctp_ill->sctp_ill_name,
817*7c478bd9Sstevel@tonic-gate 				    sctp_ill->sctp_ill_name_length);
818*7c478bd9Sstevel@tonic-gate 				kmem_free(sctp_ill, sizeof (sctp_ill_t));
819*7c478bd9Sstevel@tonic-gate 			}
820*7c478bd9Sstevel@tonic-gate 		}
821*7c478bd9Sstevel@tonic-gate 		break;
822*7c478bd9Sstevel@tonic-gate 	}
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_UP:
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
827*7c478bd9Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
828*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP;
829*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
830*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
831*7c478bd9Sstevel@tonic-gate 
832*7c478bd9Sstevel@tonic-gate 		break;
833*7c478bd9Sstevel@tonic-gate 
834*7c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_UPDATE:
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
837*7c478bd9Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
838*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu;
839*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr;
840*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid;
841*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
842*7c478bd9Sstevel@tonic-gate 
843*7c478bd9Sstevel@tonic-gate 		break;
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 	case SCTP_IPIF_DOWN:
846*7c478bd9Sstevel@tonic-gate 
847*7c478bd9Sstevel@tonic-gate 		rw_downgrade(&sctp_g_ipifs_lock);
848*7c478bd9Sstevel@tonic-gate 		rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER);
849*7c478bd9Sstevel@tonic-gate 		sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN;
850*7c478bd9Sstevel@tonic-gate 		rw_exit(&sctp_ipif->sctp_ipif_lock);
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate 		break;
853*7c478bd9Sstevel@tonic-gate 	}
854*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ipifs_lock);
855*7c478bd9Sstevel@tonic-gate 	rw_exit(&sctp_g_ills_lock);
856*7c478bd9Sstevel@tonic-gate }
857*7c478bd9Sstevel@tonic-gate 
858*7c478bd9Sstevel@tonic-gate /*
859*7c478bd9Sstevel@tonic-gate  *
860*7c478bd9Sstevel@tonic-gate  *
861*7c478bd9Sstevel@tonic-gate  * SCTP source address list manipulaton, locking not used (except for
862*7c478bd9Sstevel@tonic-gate  * sctp locking by the caller.
863*7c478bd9Sstevel@tonic-gate  *
864*7c478bd9Sstevel@tonic-gate  *
865*7c478bd9Sstevel@tonic-gate  */
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate /* Remove a specific saddr from the list */
868*7c478bd9Sstevel@tonic-gate void
869*7c478bd9Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp)
870*7c478bd9Sstevel@tonic-gate {
871*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
872*7c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
875*7c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 	sctp_ipif_hash_remove(sctp, sp->saddr_ipifp);
878*7c478bd9Sstevel@tonic-gate 
879*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_bound_to_all)
880*7c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
881*7c478bd9Sstevel@tonic-gate 
882*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL)
883*7c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
884*7c478bd9Sstevel@tonic-gate 
885*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_listen_tfp != NULL)
886*7c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
887*7c478bd9Sstevel@tonic-gate }
888*7c478bd9Sstevel@tonic-gate 
889*7c478bd9Sstevel@tonic-gate /*
890*7c478bd9Sstevel@tonic-gate  * Delete source address from the existing list. No error checking done here
891*7c478bd9Sstevel@tonic-gate  * Called with no locks held.
892*7c478bd9Sstevel@tonic-gate  */
893*7c478bd9Sstevel@tonic-gate void
894*7c478bd9Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt,
895*7c478bd9Sstevel@tonic-gate     boolean_t fanout_locked)
896*7c478bd9Sstevel@tonic-gate {
897*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
898*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
899*7c478bd9Sstevel@tonic-gate 	int			cnt;
900*7c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
901*7c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > addcnt);
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate 	if (!fanout_locked) {
906*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
907*7c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
908*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
909*7c478bd9Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_listen_tfp->tf_lock);
910*7c478bd9Sstevel@tonic-gate 	}
911*7c478bd9Sstevel@tonic-gate 
912*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < addcnt; cnt++) {
913*7c478bd9Sstevel@tonic-gate 		switch (sctp->sctp_family) {
914*7c478bd9Sstevel@tonic-gate 		case AF_INET:
915*7c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + cnt;
916*7c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr);
917*7c478bd9Sstevel@tonic-gate 			break;
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 		case AF_INET6:
920*7c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + cnt;
921*7c478bd9Sstevel@tonic-gate 			addr = sin6->sin6_addr;
922*7c478bd9Sstevel@tonic-gate 			break;
923*7c478bd9Sstevel@tonic-gate 		}
924*7c478bd9Sstevel@tonic-gate 		sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE,
925*7c478bd9Sstevel@tonic-gate 		    sctp->sctp_zoneid);
926*7c478bd9Sstevel@tonic-gate 		ASSERT(sctp_ipif != NULL);
927*7c478bd9Sstevel@tonic-gate 		sctp_ipif_hash_remove(sctp, sctp_ipif);
928*7c478bd9Sstevel@tonic-gate 	}
929*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_bound_to_all)
930*7c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 0;
931*7c478bd9Sstevel@tonic-gate 
932*7c478bd9Sstevel@tonic-gate 	if (!fanout_locked) {
933*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_conn_tfp != NULL)
934*7c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
935*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_listen_tfp != NULL)
936*7c478bd9Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_listen_tfp->tf_lock);
937*7c478bd9Sstevel@tonic-gate 	}
938*7c478bd9Sstevel@tonic-gate }
939*7c478bd9Sstevel@tonic-gate 
940*7c478bd9Sstevel@tonic-gate /*
941*7c478bd9Sstevel@tonic-gate  * Given an address get the corresponding entry from the list
942*7c478bd9Sstevel@tonic-gate  * Called with no locks held.
943*7c478bd9Sstevel@tonic-gate  */
944*7c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *
945*7c478bd9Sstevel@tonic-gate sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr)
946*7c478bd9Sstevel@tonic-gate {
947*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*saddr_ipifs;
948*7c478bd9Sstevel@tonic-gate 	sctp_ipif_t		*sctp_ipif;
949*7c478bd9Sstevel@tonic-gate 
950*7c478bd9Sstevel@tonic-gate 	sctp_ipif = sctp_lookup_ipif_addr(addr, B_FALSE, sctp->sctp_zoneid);
951*7c478bd9Sstevel@tonic-gate 	if (sctp_ipif == NULL)
952*7c478bd9Sstevel@tonic-gate 		return (NULL);
953*7c478bd9Sstevel@tonic-gate 
954*7c478bd9Sstevel@tonic-gate 	saddr_ipifs = sctp_ipif_lookup(sctp, sctp_ipif->sctp_ipif_id);
955*7c478bd9Sstevel@tonic-gate 	return (saddr_ipifs);
956*7c478bd9Sstevel@tonic-gate }
957*7c478bd9Sstevel@tonic-gate 
958*7c478bd9Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */
959*7c478bd9Sstevel@tonic-gate in6_addr_t
960*7c478bd9Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6)
961*7c478bd9Sstevel@tonic-gate {
962*7c478bd9Sstevel@tonic-gate 	int			i;
963*7c478bd9Sstevel@tonic-gate 	int			l;
964*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
965*7c478bd9Sstevel@tonic-gate 	int			scanned = 0;
966*7c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
969*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
970*7c478bd9Sstevel@tonic-gate 			continue;
971*7c478bd9Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
972*7c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
973*7c478bd9Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
974*7c478bd9Sstevel@tonic-gate 			sctp_ill_t	*ill;
975*7c478bd9Sstevel@tonic-gate 
976*7c478bd9Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
977*7c478bd9Sstevel@tonic-gate 			ill = ipif->sctp_ipif_ill;
978*7c478bd9Sstevel@tonic-gate 			if (!obj->saddr_ipif_dontsrc &&
979*7c478bd9Sstevel@tonic-gate 			    ipif->sctp_ipif_isv6 == isv6 &&
980*7c478bd9Sstevel@tonic-gate 			    ipif->sctp_ipif_state == SCTP_IPIFS_UP &&
981*7c478bd9Sstevel@tonic-gate 			    !(ill->sctp_ill_flags & PHYI_LOOPBACK)) {
982*7c478bd9Sstevel@tonic-gate 				return (ipif->sctp_ipif_saddr);
983*7c478bd9Sstevel@tonic-gate 			}
984*7c478bd9Sstevel@tonic-gate 			scanned++;
985*7c478bd9Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
986*7c478bd9Sstevel@tonic-gate 				goto got_none;
987*7c478bd9Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
988*7c478bd9Sstevel@tonic-gate 			    obj);
989*7c478bd9Sstevel@tonic-gate 		}
990*7c478bd9Sstevel@tonic-gate 	}
991*7c478bd9Sstevel@tonic-gate got_none:
992*7c478bd9Sstevel@tonic-gate 	/* Need to double check this */
993*7c478bd9Sstevel@tonic-gate 	if (isv6 == B_TRUE)
994*7c478bd9Sstevel@tonic-gate 		addr =  ipv6_all_zeros;
995*7c478bd9Sstevel@tonic-gate 	else
996*7c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(0, &addr);
997*7c478bd9Sstevel@tonic-gate 
998*7c478bd9Sstevel@tonic-gate 	return (addr);
999*7c478bd9Sstevel@tonic-gate }
1000*7c478bd9Sstevel@tonic-gate 
1001*7c478bd9Sstevel@tonic-gate /* Given a list, get the combined lengths. Called with no locks held. */
1002*7c478bd9Sstevel@tonic-gate size_t
1003*7c478bd9Sstevel@tonic-gate sctp_addr_len(sctp_t *sctp, int af)
1004*7c478bd9Sstevel@tonic-gate {
1005*7c478bd9Sstevel@tonic-gate 	int			i;
1006*7c478bd9Sstevel@tonic-gate 	int			l;
1007*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1008*7c478bd9Sstevel@tonic-gate 	size_t			paramlen = 0;
1009*7c478bd9Sstevel@tonic-gate 	int			scanned = 0;
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1012*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1013*7c478bd9Sstevel@tonic-gate 			continue;
1014*7c478bd9Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1015*7c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
1016*7c478bd9Sstevel@tonic-gate 			in6_addr_t	addr;
1017*7c478bd9Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
1018*7c478bd9Sstevel@tonic-gate 			sctp_ill_t	*ill;
1019*7c478bd9Sstevel@tonic-gate 
1020*7c478bd9Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1021*7c478bd9Sstevel@tonic-gate 			ill = ipif->sctp_ipif_ill;
1022*7c478bd9Sstevel@tonic-gate 			scanned++;
1023*7c478bd9Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state != SCTP_IPIFS_UP) ||
1024*7c478bd9Sstevel@tonic-gate 			    (ill->sctp_ill_flags & PHYI_LOOPBACK)) {
1025*7c478bd9Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
1026*7c478bd9Sstevel@tonic-gate 					return (paramlen);
1027*7c478bd9Sstevel@tonic-gate 
1028*7c478bd9Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
1029*7c478bd9Sstevel@tonic-gate 				    sctp_ipif_list, obj);
1030*7c478bd9Sstevel@tonic-gate 				continue;
1031*7c478bd9Sstevel@tonic-gate 			}
1032*7c478bd9Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1033*7c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&addr)) {
1034*7c478bd9Sstevel@tonic-gate 				/*
1035*7c478bd9Sstevel@tonic-gate 				 * Only send v4 address if the other side
1036*7c478bd9Sstevel@tonic-gate 				 * supports it.
1037*7c478bd9Sstevel@tonic-gate 				 */
1038*7c478bd9Sstevel@tonic-gate 				if (af & PARM_SUPP_V4)
1039*7c478bd9Sstevel@tonic-gate 					paramlen += PARM_ADDR4_LEN;
1040*7c478bd9Sstevel@tonic-gate 			} else if (!IN6_IS_ADDR_LINKLOCAL(&addr) &&
1041*7c478bd9Sstevel@tonic-gate 			    (af & PARM_SUPP_V6)) {
1042*7c478bd9Sstevel@tonic-gate 				paramlen += PARM_ADDR6_LEN;
1043*7c478bd9Sstevel@tonic-gate 			}
1044*7c478bd9Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
1045*7c478bd9Sstevel@tonic-gate 				return (paramlen);
1046*7c478bd9Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1047*7c478bd9Sstevel@tonic-gate 			    obj);
1048*7c478bd9Sstevel@tonic-gate 		}
1049*7c478bd9Sstevel@tonic-gate 	}
1050*7c478bd9Sstevel@tonic-gate 	return (paramlen);
1051*7c478bd9Sstevel@tonic-gate }
1052*7c478bd9Sstevel@tonic-gate 
1053*7c478bd9Sstevel@tonic-gate /*
1054*7c478bd9Sstevel@tonic-gate  * Return the list of local addresses of an association.  The parameter
1055*7c478bd9Sstevel@tonic-gate  * myaddrs is supposed to be either (struct sockaddr_in *) or (struct
1056*7c478bd9Sstevel@tonic-gate  * sockaddr_in6 *) depending on the address family.
1057*7c478bd9Sstevel@tonic-gate  */
1058*7c478bd9Sstevel@tonic-gate int
1059*7c478bd9Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt)
1060*7c478bd9Sstevel@tonic-gate {
1061*7c478bd9Sstevel@tonic-gate 	int			i;
1062*7c478bd9Sstevel@tonic-gate 	int			l;
1063*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1064*7c478bd9Sstevel@tonic-gate 	sctp_t			*sctp = (sctp_t *)conn;
1065*7c478bd9Sstevel@tonic-gate 	int			family = sctp->sctp_family;
1066*7c478bd9Sstevel@tonic-gate 	int			max = *addrcnt;
1067*7c478bd9Sstevel@tonic-gate 	size_t			added = 0;
1068*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
1069*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
1070*7c478bd9Sstevel@tonic-gate 	int			scanned = 0;
1071*7c478bd9Sstevel@tonic-gate 	boolean_t		skip_lback = B_FALSE;
1072*7c478bd9Sstevel@tonic-gate 
1073*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0)
1074*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1075*7c478bd9Sstevel@tonic-gate 
1076*7c478bd9Sstevel@tonic-gate 	/* Skip loopback addresses for non-loopback assoc. */
1077*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback)
1078*7c478bd9Sstevel@tonic-gate 		skip_lback = B_TRUE;
1079*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1080*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1081*7c478bd9Sstevel@tonic-gate 			continue;
1082*7c478bd9Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1083*7c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
1084*7c478bd9Sstevel@tonic-gate 			sctp_ipif_t	*ipif = obj->saddr_ipifp;
1085*7c478bd9Sstevel@tonic-gate 			sctp_ill_t	*ill = ipif->sctp_ipif_ill;
1086*7c478bd9Sstevel@tonic-gate 			in6_addr_t	addr = ipif->sctp_ipif_saddr;
1087*7c478bd9Sstevel@tonic-gate 
1088*7c478bd9Sstevel@tonic-gate 			scanned++;
1089*7c478bd9Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) ||
1090*7c478bd9Sstevel@tonic-gate 			    obj->saddr_ipif_dontsrc ||
1091*7c478bd9Sstevel@tonic-gate 			    ((ill->sctp_ill_flags & PHYI_LOOPBACK) &&
1092*7c478bd9Sstevel@tonic-gate 			    skip_lback)) {
1093*7c478bd9Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
1094*7c478bd9Sstevel@tonic-gate 					goto done;
1095*7c478bd9Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
1096*7c478bd9Sstevel@tonic-gate 				    sctp_ipif_list, obj);
1097*7c478bd9Sstevel@tonic-gate 				continue;
1098*7c478bd9Sstevel@tonic-gate 			}
1099*7c478bd9Sstevel@tonic-gate 			switch (family) {
1100*7c478bd9Sstevel@tonic-gate 			case AF_INET:
1101*7c478bd9Sstevel@tonic-gate 				sin4 = (struct sockaddr_in *)myaddrs + added;
1102*7c478bd9Sstevel@tonic-gate 				sin4->sin_family = AF_INET;
1103*7c478bd9Sstevel@tonic-gate 				sin4->sin_port = sctp->sctp_lport;
1104*7c478bd9Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1105*7c478bd9Sstevel@tonic-gate 				break;
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate 			case AF_INET6:
1108*7c478bd9Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)myaddrs + added;
1109*7c478bd9Sstevel@tonic-gate 				sin6->sin6_family = AF_INET6;
1110*7c478bd9Sstevel@tonic-gate 				sin6->sin6_port = sctp->sctp_lport;
1111*7c478bd9Sstevel@tonic-gate 				sin6->sin6_addr = addr;
1112*7c478bd9Sstevel@tonic-gate 				break;
1113*7c478bd9Sstevel@tonic-gate 			}
1114*7c478bd9Sstevel@tonic-gate 			added++;
1115*7c478bd9Sstevel@tonic-gate 			if (added >= max || scanned >= sctp->sctp_nsaddrs)
1116*7c478bd9Sstevel@tonic-gate 				goto done;
1117*7c478bd9Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1118*7c478bd9Sstevel@tonic-gate 			    obj);
1119*7c478bd9Sstevel@tonic-gate 		}
1120*7c478bd9Sstevel@tonic-gate 	}
1121*7c478bd9Sstevel@tonic-gate done:
1122*7c478bd9Sstevel@tonic-gate 	*addrcnt = added;
1123*7c478bd9Sstevel@tonic-gate 	return (0);
1124*7c478bd9Sstevel@tonic-gate }
1125*7c478bd9Sstevel@tonic-gate 
1126*7c478bd9Sstevel@tonic-gate /*
1127*7c478bd9Sstevel@tonic-gate  * Given a list construct the list of addresses in p. Called with no locks
1128*7c478bd9Sstevel@tonic-gate  * held
1129*7c478bd9Sstevel@tonic-gate  */
1130*7c478bd9Sstevel@tonic-gate size_t
1131*7c478bd9Sstevel@tonic-gate sctp_addr_val(sctp_t *sctp, int supp_af, uchar_t *p)
1132*7c478bd9Sstevel@tonic-gate {
1133*7c478bd9Sstevel@tonic-gate 	int			i;
1134*7c478bd9Sstevel@tonic-gate 	int			l;
1135*7c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*obj;
1136*7c478bd9Sstevel@tonic-gate 	size_t			added = 0;
1137*7c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*hdr;
1138*7c478bd9Sstevel@tonic-gate 	int			scanned = 0;
1139*7c478bd9Sstevel@tonic-gate 
1140*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1141*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_saddrs[i].ipif_count == 0)
1142*7c478bd9Sstevel@tonic-gate 			continue;
1143*7c478bd9Sstevel@tonic-gate 		obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list);
1144*7c478bd9Sstevel@tonic-gate 		for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) {
1145*7c478bd9Sstevel@tonic-gate 			in6_addr_t	addr;
1146*7c478bd9Sstevel@tonic-gate 			sctp_ipif_t	*ipif;
1147*7c478bd9Sstevel@tonic-gate 			sctp_ill_t	*ill;
1148*7c478bd9Sstevel@tonic-gate 
1149*7c478bd9Sstevel@tonic-gate 			ipif = obj->saddr_ipifp;
1150*7c478bd9Sstevel@tonic-gate 			ill = ipif->sctp_ipif_ill;
1151*7c478bd9Sstevel@tonic-gate 			scanned++;
1152*7c478bd9Sstevel@tonic-gate 			if ((ipif->sctp_ipif_state != SCTP_IPIFS_UP) ||
1153*7c478bd9Sstevel@tonic-gate 			    (ill->sctp_ill_flags & PHYI_LOOPBACK)) {
1154*7c478bd9Sstevel@tonic-gate 				if (scanned >= sctp->sctp_nsaddrs)
1155*7c478bd9Sstevel@tonic-gate 					return (added);
1156*7c478bd9Sstevel@tonic-gate 				obj = list_next(&sctp->sctp_saddrs[i].
1157*7c478bd9Sstevel@tonic-gate 				    sctp_ipif_list, obj);
1158*7c478bd9Sstevel@tonic-gate 				continue;
1159*7c478bd9Sstevel@tonic-gate 			}
1160*7c478bd9Sstevel@tonic-gate 			hdr = (sctp_parm_hdr_t *)(p + added);
1161*7c478bd9Sstevel@tonic-gate 			addr = ipif->sctp_ipif_saddr;
1162*7c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&addr)) {
1163*7c478bd9Sstevel@tonic-gate 				struct in_addr	*v4;
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate 				/* The other side does not support v4 */
1166*7c478bd9Sstevel@tonic-gate 				if (!(supp_af & PARM_SUPP_V4))
1167*7c478bd9Sstevel@tonic-gate 					continue;
1168*7c478bd9Sstevel@tonic-gate 
1169*7c478bd9Sstevel@tonic-gate 				hdr->sph_type = htons(PARM_ADDR4);
1170*7c478bd9Sstevel@tonic-gate 				hdr->sph_len = htons(PARM_ADDR4_LEN);
1171*7c478bd9Sstevel@tonic-gate 				v4 = (struct in_addr *)(hdr + 1);
1172*7c478bd9Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&addr, v4);
1173*7c478bd9Sstevel@tonic-gate 				added += PARM_ADDR4_LEN;
1174*7c478bd9Sstevel@tonic-gate 			} else if (!IN6_IS_ADDR_LINKLOCAL(&addr) &&
1175*7c478bd9Sstevel@tonic-gate 			    (supp_af & PARM_SUPP_V6)) {
1176*7c478bd9Sstevel@tonic-gate 				hdr->sph_type = htons(PARM_ADDR6);
1177*7c478bd9Sstevel@tonic-gate 				hdr->sph_len = htons(PARM_ADDR6_LEN);
1178*7c478bd9Sstevel@tonic-gate 				bcopy(&addr, hdr + 1, sizeof (addr));
1179*7c478bd9Sstevel@tonic-gate 				added += PARM_ADDR6_LEN;
1180*7c478bd9Sstevel@tonic-gate 			}
1181*7c478bd9Sstevel@tonic-gate 			if (scanned >= sctp->sctp_nsaddrs)
1182*7c478bd9Sstevel@tonic-gate 				return (added);
1183*7c478bd9Sstevel@tonic-gate 			obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list,
1184*7c478bd9Sstevel@tonic-gate 			    obj);
1185*7c478bd9Sstevel@tonic-gate 		}
1186*7c478bd9Sstevel@tonic-gate 	}
1187*7c478bd9Sstevel@tonic-gate 	return (added);
1188*7c478bd9Sstevel@tonic-gate }
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate 
1191*7c478bd9Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */
1192*7c478bd9Sstevel@tonic-gate void
1193*7c478bd9Sstevel@tonic-gate sctp_saddr_init()
1194*7c478bd9Sstevel@tonic-gate {
1195*7c478bd9Sstevel@tonic-gate 	int	i;
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate 	rw_init(&sctp_g_ills_lock, NULL, RW_DEFAULT, NULL);
1198*7c478bd9Sstevel@tonic-gate 	rw_init(&sctp_g_ipifs_lock, NULL, RW_DEFAULT, NULL);
1199*7c478bd9Sstevel@tonic-gate 
1200*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1201*7c478bd9Sstevel@tonic-gate 		sctp_g_ills[i].ill_count = 0;
1202*7c478bd9Sstevel@tonic-gate 		list_create(&sctp_g_ills[i].sctp_ill_list, sizeof (sctp_ill_t),
1203*7c478bd9Sstevel@tonic-gate 		    offsetof(sctp_ill_t, sctp_ills));
1204*7c478bd9Sstevel@tonic-gate 	}
1205*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1206*7c478bd9Sstevel@tonic-gate 		sctp_g_ipifs[i].ipif_count = 0;
1207*7c478bd9Sstevel@tonic-gate 		list_create(&sctp_g_ipifs[i].sctp_ipif_list,
1208*7c478bd9Sstevel@tonic-gate 		    sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs));
1209*7c478bd9Sstevel@tonic-gate 	}
1210*7c478bd9Sstevel@tonic-gate }
1211*7c478bd9Sstevel@tonic-gate 
1212*7c478bd9Sstevel@tonic-gate void
1213*7c478bd9Sstevel@tonic-gate sctp_saddr_fini()
1214*7c478bd9Sstevel@tonic-gate {
1215*7c478bd9Sstevel@tonic-gate 	int	i;
1216*7c478bd9Sstevel@tonic-gate 
1217*7c478bd9Sstevel@tonic-gate 	rw_destroy(&sctp_g_ills_lock);
1218*7c478bd9Sstevel@tonic-gate 	rw_destroy(&sctp_g_ipifs_lock);
1219*7c478bd9Sstevel@tonic-gate 	ASSERT(sctp_ills_count == 0 && sctp_g_ipifs_count == 0);
1220*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_ILL_HASH; i++)
1221*7c478bd9Sstevel@tonic-gate 		list_destroy(&sctp_g_ills[i].sctp_ill_list);
1222*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCTP_IPIF_HASH; i++)
1223*7c478bd9Sstevel@tonic-gate 		list_destroy(&sctp_g_ipifs[i].sctp_ipif_list);
1224*7c478bd9Sstevel@tonic-gate }
1225