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/cmn_err.h>
33*0Sstevel@tonic-gate #include <sys/kmem.h>
34*0Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
35*0Sstevel@tonic-gate #include <sys/tihdr.h>
36*0Sstevel@tonic-gate #include <sys/stropts.h>
37*0Sstevel@tonic-gate #include <sys/socket.h>
38*0Sstevel@tonic-gate #include <sys/random.h>
39*0Sstevel@tonic-gate #include <sys/policy.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <netinet/in.h>
42*0Sstevel@tonic-gate #include <netinet/ip6.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include <inet/common.h>
45*0Sstevel@tonic-gate #include <inet/ip.h>
46*0Sstevel@tonic-gate #include <inet/ip6.h>
47*0Sstevel@tonic-gate #include <inet/ipclassifier.h>
48*0Sstevel@tonic-gate #include "sctp_impl.h"
49*0Sstevel@tonic-gate #include "sctp_asconf.h"
50*0Sstevel@tonic-gate #include "sctp_addr.h"
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate uint_t	sctp_next_port_to_try;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate  * Returns 0 on success, EACCES on permission failure.
56*0Sstevel@tonic-gate  */
57*0Sstevel@tonic-gate static int
58*0Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified)
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	/*
61*0Sstevel@tonic-gate 	 * Get a valid port (within the anonymous range and should not
62*0Sstevel@tonic-gate 	 * be a privileged one) to use if the user has not given a port.
63*0Sstevel@tonic-gate 	 * If multiple threads are here, they may all start with
64*0Sstevel@tonic-gate 	 * with the same initial port. But, it should be fine as long as
65*0Sstevel@tonic-gate 	 * sctp_bindi will ensure that no two threads will be assigned
66*0Sstevel@tonic-gate 	 * the same port.
67*0Sstevel@tonic-gate 	 */
68*0Sstevel@tonic-gate 	if (*requested_port == 0) {
69*0Sstevel@tonic-gate 		*requested_port = sctp_update_next_port(sctp_next_port_to_try);
70*0Sstevel@tonic-gate 		*user_specified = 0;
71*0Sstevel@tonic-gate 	} else {
72*0Sstevel@tonic-gate 		int i;
73*0Sstevel@tonic-gate 		boolean_t priv = B_FALSE;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 		/*
76*0Sstevel@tonic-gate 		 * If the requested_port is in the well-known privileged range,
77*0Sstevel@tonic-gate 		 * verify that the stream was opened by a privileged user.
78*0Sstevel@tonic-gate 		 * Note: No locks are held when inspecting sctp_g_*epriv_ports
79*0Sstevel@tonic-gate 		 * but instead the code relies on:
80*0Sstevel@tonic-gate 		 * - the fact that the address of the array and its size never
81*0Sstevel@tonic-gate 		 *   changes
82*0Sstevel@tonic-gate 		 * - the atomic assignment of the elements of the array
83*0Sstevel@tonic-gate 		 */
84*0Sstevel@tonic-gate 		if (*requested_port < sctp_smallest_nonpriv_port) {
85*0Sstevel@tonic-gate 			priv = B_TRUE;
86*0Sstevel@tonic-gate 		} else {
87*0Sstevel@tonic-gate 			for (i = 0; i < sctp_g_num_epriv_ports; i++) {
88*0Sstevel@tonic-gate 				if (*requested_port == sctp_g_epriv_ports[i]) {
89*0Sstevel@tonic-gate 					priv = B_TRUE;
90*0Sstevel@tonic-gate 					break;
91*0Sstevel@tonic-gate 				}
92*0Sstevel@tonic-gate 			}
93*0Sstevel@tonic-gate 		}
94*0Sstevel@tonic-gate 		if (priv) {
95*0Sstevel@tonic-gate 			/*
96*0Sstevel@tonic-gate 			 * sctp_bind() should take a cred_t argument so that
97*0Sstevel@tonic-gate 			 * we can use it here.
98*0Sstevel@tonic-gate 			 */
99*0Sstevel@tonic-gate 			if (secpolicy_net_privaddr(sctp->sctp_credp,
100*0Sstevel@tonic-gate 			    *requested_port) != 0) {
101*0Sstevel@tonic-gate 				dprint(1,
102*0Sstevel@tonic-gate 				    ("sctp_bind(x): no prive for port %d",
103*0Sstevel@tonic-gate 				    *requested_port));
104*0Sstevel@tonic-gate 				return (TACCES);
105*0Sstevel@tonic-gate 			}
106*0Sstevel@tonic-gate 		}
107*0Sstevel@tonic-gate 		*user_specified = 1;
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	return (0);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate int
114*0Sstevel@tonic-gate sctp_listen(sctp_t *sctp)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	sctp_tf_t	*tf;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	RUN_SCTP(sctp);
119*0Sstevel@tonic-gate 	/*
120*0Sstevel@tonic-gate 	 * TCP handles listen() increasing the backlog, need to check
121*0Sstevel@tonic-gate 	 * if it should be handled here too - VENU.
122*0Sstevel@tonic-gate 	 */
123*0Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_BOUND) {
124*0Sstevel@tonic-gate 		WAKE_SCTP(sctp);
125*0Sstevel@tonic-gate 		return (EINVAL);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	/* Do an anonymous bind for unbound socket doing listen(). */
129*0Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0) {
130*0Sstevel@tonic-gate 		struct sockaddr_storage ss;
131*0Sstevel@tonic-gate 		int ret;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 		bzero(&ss, sizeof (ss));
134*0Sstevel@tonic-gate 		ss.ss_family = sctp->sctp_family;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 		WAKE_SCTP(sctp);
137*0Sstevel@tonic-gate 		if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
138*0Sstevel@tonic-gate 			sizeof (ss))) != 0)
139*0Sstevel@tonic-gate 			return (ret);
140*0Sstevel@tonic-gate 		RUN_SCTP(sctp)
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_LISTEN;
144*0Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
145*0Sstevel@tonic-gate 	sctp->sctp_last_secret_update = lbolt64;
146*0Sstevel@tonic-gate 	bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
147*0Sstevel@tonic-gate 	tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))];
148*0Sstevel@tonic-gate 	sctp_listen_hash_insert(tf, sctp);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	WAKE_SCTP(sctp);
151*0Sstevel@tonic-gate 	return (0);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate  * Bind the sctp_t to a sockaddr, which includes an address and other
156*0Sstevel@tonic-gate  * information, such as port or flowinfo.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate int
159*0Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
160*0Sstevel@tonic-gate {
161*0Sstevel@tonic-gate 	int		user_specified;
162*0Sstevel@tonic-gate 	boolean_t	bind_to_req_port_only;
163*0Sstevel@tonic-gate 	in_port_t	requested_port;
164*0Sstevel@tonic-gate 	in_port_t	allocated_port;
165*0Sstevel@tonic-gate 	int		err = 0;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	ASSERT(sctp != NULL);
168*0Sstevel@tonic-gate 	ASSERT(sa);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	RUN_SCTP(sctp);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_BOUND) {
173*0Sstevel@tonic-gate 		err = EINVAL;
174*0Sstevel@tonic-gate 		goto done;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	switch (sa->sa_family) {
178*0Sstevel@tonic-gate 	case AF_INET:
179*0Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in) ||
180*0Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
181*0Sstevel@tonic-gate 			err = EINVAL;
182*0Sstevel@tonic-gate 			goto done;
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port);
185*0Sstevel@tonic-gate 		break;
186*0Sstevel@tonic-gate 	case AF_INET6:
187*0Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in6) ||
188*0Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET) {
189*0Sstevel@tonic-gate 			err = EINVAL;
190*0Sstevel@tonic-gate 			goto done;
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
193*0Sstevel@tonic-gate 		/* Set the flowinfo. */
194*0Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_vcf =
195*0Sstevel@tonic-gate 		    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
196*0Sstevel@tonic-gate 		    (((struct sockaddr_in6 *)sa)->sin6_flowinfo &
197*0Sstevel@tonic-gate 		    ~IPV6_VERS_AND_FLOW_MASK);
198*0Sstevel@tonic-gate 		break;
199*0Sstevel@tonic-gate 	default:
200*0Sstevel@tonic-gate 		err = EAFNOSUPPORT;
201*0Sstevel@tonic-gate 		goto done;
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 	bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if (sctp_select_port(sctp, &requested_port, &user_specified) != 0) {
206*0Sstevel@tonic-gate 		err = EPERM;
207*0Sstevel@tonic-gate 		goto done;
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE)) != 0)
211*0Sstevel@tonic-gate 		goto done;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	allocated_port = sctp_bindi(sctp, requested_port,
214*0Sstevel@tonic-gate 	    bind_to_req_port_only, user_specified);
215*0Sstevel@tonic-gate 	if (allocated_port == 0) {
216*0Sstevel@tonic-gate 		sctp_free_saddrs(sctp);
217*0Sstevel@tonic-gate 		if (bind_to_req_port_only) {
218*0Sstevel@tonic-gate 			err = EADDRINUSE;
219*0Sstevel@tonic-gate 			goto done;
220*0Sstevel@tonic-gate 		} else {
221*0Sstevel@tonic-gate 			err = EADDRNOTAVAIL;
222*0Sstevel@tonic-gate 			goto done;
223*0Sstevel@tonic-gate 		}
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 	ASSERT(sctp->sctp_state == SCTPS_BOUND);
226*0Sstevel@tonic-gate done:
227*0Sstevel@tonic-gate 	WAKE_SCTP(sctp);
228*0Sstevel@tonic-gate 	return (err);
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate /*
232*0Sstevel@tonic-gate  * Perform bind/unbind operation of a list of addresses on a sctp_t
233*0Sstevel@tonic-gate  */
234*0Sstevel@tonic-gate int
235*0Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
236*0Sstevel@tonic-gate {
237*0Sstevel@tonic-gate 	ASSERT(sctp != NULL);
238*0Sstevel@tonic-gate 	ASSERT(addrs != NULL);
239*0Sstevel@tonic-gate 	ASSERT(addrcnt > 0);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	switch (bindop) {
242*0Sstevel@tonic-gate 	case SCTP_BINDX_ADD_ADDR:
243*0Sstevel@tonic-gate 		return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE));
244*0Sstevel@tonic-gate 	case SCTP_BINDX_REM_ADDR:
245*0Sstevel@tonic-gate 		return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
246*0Sstevel@tonic-gate 	default:
247*0Sstevel@tonic-gate 		return (EINVAL);
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate /*
252*0Sstevel@tonic-gate  * Add a list of addresses to a sctp_t.
253*0Sstevel@tonic-gate  */
254*0Sstevel@tonic-gate int
255*0Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
256*0Sstevel@tonic-gate     boolean_t caller_hold_lock)
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate 	int		err = 0;
259*0Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	if (!caller_hold_lock)
262*0Sstevel@tonic-gate 		RUN_SCTP(sctp);
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
265*0Sstevel@tonic-gate 		if (!caller_hold_lock)
266*0Sstevel@tonic-gate 			WAKE_SCTP(sctp);
267*0Sstevel@tonic-gate 		return (EINVAL);
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_LISTEN && sctp_addip_enabled)
270*0Sstevel@tonic-gate 		do_asconf = B_TRUE;
271*0Sstevel@tonic-gate 	err = sctp_valid_addr_list(sctp, addrs, addrcnt);
272*0Sstevel@tonic-gate 	if (err != 0) {
273*0Sstevel@tonic-gate 		if (!caller_hold_lock)
274*0Sstevel@tonic-gate 			WAKE_SCTP(sctp);
275*0Sstevel@tonic-gate 		return (err);
276*0Sstevel@tonic-gate 	}
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	/* Need to send  ASCONF messages */
279*0Sstevel@tonic-gate 	if (do_asconf) {
280*0Sstevel@tonic-gate 		err = sctp_add_ip(sctp, addrs, addrcnt);
281*0Sstevel@tonic-gate 		if (err != 0) {
282*0Sstevel@tonic-gate 			sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
283*0Sstevel@tonic-gate 			if (!caller_hold_lock)
284*0Sstevel@tonic-gate 				WAKE_SCTP(sctp);
285*0Sstevel@tonic-gate 			return (err);
286*0Sstevel@tonic-gate 		}
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 	if (!caller_hold_lock)
289*0Sstevel@tonic-gate 		WAKE_SCTP(sctp);
290*0Sstevel@tonic-gate 	if (do_asconf)
291*0Sstevel@tonic-gate 		sctp_process_sendq(sctp);
292*0Sstevel@tonic-gate 	return (0);
293*0Sstevel@tonic-gate }
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate /*
296*0Sstevel@tonic-gate  * Remove one or more addresses bound to the sctp_t.
297*0Sstevel@tonic-gate  */
298*0Sstevel@tonic-gate int
299*0Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
300*0Sstevel@tonic-gate     boolean_t caller_hold_lock)
301*0Sstevel@tonic-gate {
302*0Sstevel@tonic-gate 	int		error = 0;
303*0Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	if (!caller_hold_lock)
306*0Sstevel@tonic-gate 		RUN_SCTP(sctp);
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
309*0Sstevel@tonic-gate 		if (!caller_hold_lock)
310*0Sstevel@tonic-gate 			WAKE_SCTP(sctp);
311*0Sstevel@tonic-gate 		return (EINVAL);
312*0Sstevel@tonic-gate 	}
313*0Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_LISTEN && sctp_addip_enabled)
314*0Sstevel@tonic-gate 		do_asconf = B_TRUE;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	/* Can't delete the last address nor all of the addresses */
317*0Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
318*0Sstevel@tonic-gate 		if (!caller_hold_lock)
319*0Sstevel@tonic-gate 			WAKE_SCTP(sctp);
320*0Sstevel@tonic-gate 		return (EINVAL);
321*0Sstevel@tonic-gate 	}
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	error = sctp_del_ip(sctp, addrs, addrcnt);
324*0Sstevel@tonic-gate 	if (!caller_hold_lock)
325*0Sstevel@tonic-gate 		WAKE_SCTP(sctp);
326*0Sstevel@tonic-gate 	if (error == 0 && do_asconf)
327*0Sstevel@tonic-gate 		sctp_process_sendq(sctp);
328*0Sstevel@tonic-gate 	return (error);
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate /*
332*0Sstevel@tonic-gate  * If the "bind_to_req_port_only" parameter is set, if the requested port
333*0Sstevel@tonic-gate  * number is available, return it, If not return 0
334*0Sstevel@tonic-gate  *
335*0Sstevel@tonic-gate  * If "bind_to_req_port_only" parameter is not set and
336*0Sstevel@tonic-gate  * If the requested port number is available, return it.  If not, return
337*0Sstevel@tonic-gate  * the first anonymous port we happen across.  If no anonymous ports are
338*0Sstevel@tonic-gate  * available, return 0. addr is the requested local address, if any.
339*0Sstevel@tonic-gate  *
340*0Sstevel@tonic-gate  * In either case, when succeeding update the sctp_t to record the port number
341*0Sstevel@tonic-gate  * and insert it in the bind hash table.
342*0Sstevel@tonic-gate  */
343*0Sstevel@tonic-gate in_port_t
344*0Sstevel@tonic-gate sctp_bindi(sctp_t *sctp, in_port_t port, int bind_to_req_port_only,
345*0Sstevel@tonic-gate     int user_specified)
346*0Sstevel@tonic-gate {
347*0Sstevel@tonic-gate 	/* number of times we have run around the loop */
348*0Sstevel@tonic-gate 	int count = 0;
349*0Sstevel@tonic-gate 	/* maximum number of times to run around the loop */
350*0Sstevel@tonic-gate 	int loopmax;
351*0Sstevel@tonic-gate 	zoneid_t zoneid = sctp->sctp_zoneid;
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	/*
354*0Sstevel@tonic-gate 	 * Lookup for free addresses is done in a loop and "loopmax"
355*0Sstevel@tonic-gate 	 * influences how long we spin in the loop
356*0Sstevel@tonic-gate 	 */
357*0Sstevel@tonic-gate 	if (bind_to_req_port_only) {
358*0Sstevel@tonic-gate 		/*
359*0Sstevel@tonic-gate 		 * If the requested port is busy, don't bother to look
360*0Sstevel@tonic-gate 		 * for a new one. Setting loop maximum count to 1 has
361*0Sstevel@tonic-gate 		 * that effect.
362*0Sstevel@tonic-gate 		 */
363*0Sstevel@tonic-gate 		loopmax = 1;
364*0Sstevel@tonic-gate 	} else {
365*0Sstevel@tonic-gate 		/*
366*0Sstevel@tonic-gate 		 * If the requested port is busy, look for a free one
367*0Sstevel@tonic-gate 		 * in the anonymous port range.
368*0Sstevel@tonic-gate 		 * Set loopmax appropriately so that one does not look
369*0Sstevel@tonic-gate 		 * forever in the case all of the anonymous ports are in use.
370*0Sstevel@tonic-gate 		 */
371*0Sstevel@tonic-gate 		loopmax = (sctp_largest_anon_port -
372*0Sstevel@tonic-gate 		    sctp_smallest_anon_port + 1);
373*0Sstevel@tonic-gate 	}
374*0Sstevel@tonic-gate 	do {
375*0Sstevel@tonic-gate 		uint16_t	lport;
376*0Sstevel@tonic-gate 		sctp_tf_t	*tbf;
377*0Sstevel@tonic-gate 		sctp_t		*lsctp;
378*0Sstevel@tonic-gate 		int		addrcmp;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 		lport = htons(port);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 		/*
383*0Sstevel@tonic-gate 		 * Ensure that the sctp_t is not currently in the bind hash.
384*0Sstevel@tonic-gate 		 * Hold the lock on the hash bucket to ensure that
385*0Sstevel@tonic-gate 		 * the duplicate check plus the insertion is an atomic
386*0Sstevel@tonic-gate 		 * operation.
387*0Sstevel@tonic-gate 		 *
388*0Sstevel@tonic-gate 		 * This function does an inline lookup on the bind hash list
389*0Sstevel@tonic-gate 		 * Make sure that we access only members of sctp_t
390*0Sstevel@tonic-gate 		 * and that we don't look at sctp_sctp, since we are not
391*0Sstevel@tonic-gate 		 * doing a SCTPB_REFHOLD. For more details please see the notes
392*0Sstevel@tonic-gate 		 * in sctp_compress()
393*0Sstevel@tonic-gate 		 */
394*0Sstevel@tonic-gate 		sctp_bind_hash_remove(sctp);
395*0Sstevel@tonic-gate 		tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)];
396*0Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
397*0Sstevel@tonic-gate 		for (lsctp = tbf->tf_sctp; lsctp != NULL;
398*0Sstevel@tonic-gate 		    lsctp = lsctp->sctp_bind_hash) {
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 			if (lport != lsctp->sctp_lport ||
401*0Sstevel@tonic-gate 			    lsctp->sctp_zoneid != zoneid ||
402*0Sstevel@tonic-gate 			    lsctp->sctp_state < SCTPS_BOUND)
403*0Sstevel@tonic-gate 				continue;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 			addrcmp = sctp_compare_saddrs(sctp, lsctp);
406*0Sstevel@tonic-gate 			if (addrcmp != SCTP_ADDR_DISJOINT) {
407*0Sstevel@tonic-gate 				if (!sctp->sctp_reuseaddr) {
408*0Sstevel@tonic-gate 					/* in use */
409*0Sstevel@tonic-gate 					break;
410*0Sstevel@tonic-gate 				} else if (lsctp->sctp_state == SCTPS_BOUND ||
411*0Sstevel@tonic-gate 				    lsctp->sctp_state == SCTPS_LISTEN) {
412*0Sstevel@tonic-gate 					/*
413*0Sstevel@tonic-gate 					 * socket option SO_REUSEADDR is set
414*0Sstevel@tonic-gate 					 * on the binding sctp_t.
415*0Sstevel@tonic-gate 					 *
416*0Sstevel@tonic-gate 					 * We have found a match of IP source
417*0Sstevel@tonic-gate 					 * address and source port, which is
418*0Sstevel@tonic-gate 					 * refused regardless of the
419*0Sstevel@tonic-gate 					 * SO_REUSEADDR setting, so we break.
420*0Sstevel@tonic-gate 					 */
421*0Sstevel@tonic-gate 					break;
422*0Sstevel@tonic-gate 				}
423*0Sstevel@tonic-gate 			}
424*0Sstevel@tonic-gate 		}
425*0Sstevel@tonic-gate 		if (lsctp != NULL) {
426*0Sstevel@tonic-gate 			/* The port number is busy */
427*0Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
428*0Sstevel@tonic-gate 		} else {
429*0Sstevel@tonic-gate 			/*
430*0Sstevel@tonic-gate 			 * This port is ours. Insert in fanout and mark as
431*0Sstevel@tonic-gate 			 * bound to prevent others from getting the port
432*0Sstevel@tonic-gate 			 * number.
433*0Sstevel@tonic-gate 			 */
434*0Sstevel@tonic-gate 			sctp->sctp_state = SCTPS_BOUND;
435*0Sstevel@tonic-gate 			sctp->sctp_lport = lport;
436*0Sstevel@tonic-gate 			sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 			ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf);
439*0Sstevel@tonic-gate 			sctp_bind_hash_insert(tbf, sctp, 1);
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 			/*
444*0Sstevel@tonic-gate 			 * We don't want sctp_next_port_to_try to "inherit"
445*0Sstevel@tonic-gate 			 * a port number supplied by the user in a bind.
446*0Sstevel@tonic-gate 			 */
447*0Sstevel@tonic-gate 			if (user_specified != 0)
448*0Sstevel@tonic-gate 				return (port);
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 			/*
451*0Sstevel@tonic-gate 			 * This is the only place where sctp_next_port_to_try
452*0Sstevel@tonic-gate 			 * is updated. After the update, it may or may not
453*0Sstevel@tonic-gate 			 * be in the valid range.
454*0Sstevel@tonic-gate 			 */
455*0Sstevel@tonic-gate 			sctp_next_port_to_try = port + 1;
456*0Sstevel@tonic-gate 			return (port);
457*0Sstevel@tonic-gate 		}
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 		if ((count == 0) && (user_specified)) {
460*0Sstevel@tonic-gate 			/*
461*0Sstevel@tonic-gate 			 * We may have to return an anonymous port. So
462*0Sstevel@tonic-gate 			 * get one to start with.
463*0Sstevel@tonic-gate 			 */
464*0Sstevel@tonic-gate 			port = sctp_update_next_port(sctp_next_port_to_try);
465*0Sstevel@tonic-gate 			user_specified = 0;
466*0Sstevel@tonic-gate 		} else {
467*0Sstevel@tonic-gate 			port = sctp_update_next_port(port + 1);
468*0Sstevel@tonic-gate 		}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 		/*
471*0Sstevel@tonic-gate 		 * Don't let this loop run forever in the case where
472*0Sstevel@tonic-gate 		 * all of the anonymous ports are in use.
473*0Sstevel@tonic-gate 		 */
474*0Sstevel@tonic-gate 	} while (++count < loopmax);
475*0Sstevel@tonic-gate 	return (0);
476*0Sstevel@tonic-gate }
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate /*
479*0Sstevel@tonic-gate  * Don't let port fall into the privileged range.
480*0Sstevel@tonic-gate  * Since the extra privileged ports can be arbitrary we also
481*0Sstevel@tonic-gate  * ensure that we exclude those from consideration.
482*0Sstevel@tonic-gate  * sctp_g_epriv_ports is not sorted thus we loop over it until
483*0Sstevel@tonic-gate  * there are no changes.
484*0Sstevel@tonic-gate  *
485*0Sstevel@tonic-gate  * Note: No locks are held when inspecting sctp_g_*epriv_ports
486*0Sstevel@tonic-gate  * but instead the code relies on:
487*0Sstevel@tonic-gate  * - the fact that the address of the array and its size never changes
488*0Sstevel@tonic-gate  * - the atomic assignment of the elements of the array
489*0Sstevel@tonic-gate  */
490*0Sstevel@tonic-gate in_port_t
491*0Sstevel@tonic-gate sctp_update_next_port(in_port_t port)
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate 	int i;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate retry:
496*0Sstevel@tonic-gate 	if (port < sctp_smallest_anon_port || port > sctp_largest_anon_port)
497*0Sstevel@tonic-gate 		port = sctp_smallest_anon_port;
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	if (port < sctp_smallest_nonpriv_port)
500*0Sstevel@tonic-gate 		port = sctp_smallest_nonpriv_port;
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	for (i = 0; i < sctp_g_num_epriv_ports; i++) {
503*0Sstevel@tonic-gate 		if (port == sctp_g_epriv_ports[i]) {
504*0Sstevel@tonic-gate 			port++;
505*0Sstevel@tonic-gate 			/*
506*0Sstevel@tonic-gate 			 * Make sure whether the port is in the
507*0Sstevel@tonic-gate 			 * valid range.
508*0Sstevel@tonic-gate 			 *
509*0Sstevel@tonic-gate 			 * XXX Note that if sctp_g_epriv_ports contains
510*0Sstevel@tonic-gate 			 * all the anonymous ports this will be an
511*0Sstevel@tonic-gate 			 * infinite loop.
512*0Sstevel@tonic-gate 			 */
513*0Sstevel@tonic-gate 			goto retry;
514*0Sstevel@tonic-gate 		}
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate 	return (port);
517*0Sstevel@tonic-gate }
518