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