xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_bind.c (revision 12869:fb36eaeb6ee0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211735Skcpoon 
220Sstevel@tonic-gate /*
23*12869SKacheong.Poon@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/systm.h>
280Sstevel@tonic-gate #include <sys/stream.h>
290Sstevel@tonic-gate #include <sys/cmn_err.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
320Sstevel@tonic-gate #include <sys/tihdr.h>
330Sstevel@tonic-gate #include <sys/stropts.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/random.h>
360Sstevel@tonic-gate #include <sys/policy.h>
371676Sjpk #include <sys/tsol/tndb.h>
381676Sjpk #include <sys/tsol/tnet.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <netinet/ip6.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <inet/common.h>
440Sstevel@tonic-gate #include <inet/ip.h>
450Sstevel@tonic-gate #include <inet/ip6.h>
460Sstevel@tonic-gate #include <inet/ipclassifier.h>
470Sstevel@tonic-gate #include "sctp_impl.h"
480Sstevel@tonic-gate #include "sctp_asconf.h"
490Sstevel@tonic-gate #include "sctp_addr.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
52*12869SKacheong.Poon@Sun.COM  * Minimum number of associations which can be created per listener.  Used
53*12869SKacheong.Poon@Sun.COM  * when the listener association count is in effect.
54*12869SKacheong.Poon@Sun.COM  */
55*12869SKacheong.Poon@Sun.COM static uint32_t sctp_min_assoc_listener = 2;
56*12869SKacheong.Poon@Sun.COM 
57*12869SKacheong.Poon@Sun.COM /*
580Sstevel@tonic-gate  * Returns 0 on success, EACCES on permission failure.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate static int
sctp_select_port(sctp_t * sctp,in_port_t * requested_port,int * user_specified)610Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified)
620Sstevel@tonic-gate {
633448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
6411042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
653448Sdh155122 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * Get a valid port (within the anonymous range and should not
680Sstevel@tonic-gate 	 * be a privileged one) to use if the user has not given a port.
690Sstevel@tonic-gate 	 * If multiple threads are here, they may all start with
700Sstevel@tonic-gate 	 * with the same initial port. But, it should be fine as long as
710Sstevel@tonic-gate 	 * sctp_bindi will ensure that no two threads will be assigned
720Sstevel@tonic-gate 	 * the same port.
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 	if (*requested_port == 0) {
753448Sdh155122 		*requested_port = sctp_update_next_port(
763448Sdh155122 		    sctps->sctps_next_port_to_try,
7711042SErik.Nordmark@Sun.COM 		    crgetzone(connp->conn_cred), sctps);
781676Sjpk 		if (*requested_port == 0)
791676Sjpk 			return (EACCES);
800Sstevel@tonic-gate 		*user_specified = 0;
810Sstevel@tonic-gate 	} else {
820Sstevel@tonic-gate 		int i;
830Sstevel@tonic-gate 		boolean_t priv = B_FALSE;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 		/*
860Sstevel@tonic-gate 		 * If the requested_port is in the well-known privileged range,
870Sstevel@tonic-gate 		 * verify that the stream was opened by a privileged user.
880Sstevel@tonic-gate 		 * Note: No locks are held when inspecting sctp_g_*epriv_ports
890Sstevel@tonic-gate 		 * but instead the code relies on:
900Sstevel@tonic-gate 		 * - the fact that the address of the array and its size never
910Sstevel@tonic-gate 		 *   changes
920Sstevel@tonic-gate 		 * - the atomic assignment of the elements of the array
930Sstevel@tonic-gate 		 */
943448Sdh155122 		if (*requested_port < sctps->sctps_smallest_nonpriv_port) {
950Sstevel@tonic-gate 			priv = B_TRUE;
960Sstevel@tonic-gate 		} else {
973448Sdh155122 			for (i = 0; i < sctps->sctps_g_num_epriv_ports; i++) {
983448Sdh155122 				if (*requested_port ==
993448Sdh155122 				    sctps->sctps_g_epriv_ports[i]) {
1000Sstevel@tonic-gate 					priv = B_TRUE;
1010Sstevel@tonic-gate 					break;
1020Sstevel@tonic-gate 				}
1030Sstevel@tonic-gate 			}
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 		if (priv) {
1060Sstevel@tonic-gate 			/*
1070Sstevel@tonic-gate 			 * sctp_bind() should take a cred_t argument so that
1080Sstevel@tonic-gate 			 * we can use it here.
1090Sstevel@tonic-gate 			 */
11011042SErik.Nordmark@Sun.COM 			if (secpolicy_net_privaddr(connp->conn_cred,
1116134Scasper 			    *requested_port, IPPROTO_SCTP) != 0) {
1120Sstevel@tonic-gate 				dprint(1,
1130Sstevel@tonic-gate 				    ("sctp_bind(x): no prive for port %d",
1140Sstevel@tonic-gate 				    *requested_port));
1151676Sjpk 				return (EACCES);
1160Sstevel@tonic-gate 			}
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 		*user_specified = 1;
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	return (0);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
sctp_listen(sctp_t * sctp)1250Sstevel@tonic-gate sctp_listen(sctp_t *sctp)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	sctp_tf_t	*tf;
1283448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12911042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	RUN_SCTP(sctp);
1320Sstevel@tonic-gate 	/*
1330Sstevel@tonic-gate 	 * TCP handles listen() increasing the backlog, need to check
134852Svi117747 	 * if it should be handled here too
1350Sstevel@tonic-gate 	 */
1364505Skcpoon 	if (sctp->sctp_state > SCTPS_BOUND ||
1374505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
1380Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1390Sstevel@tonic-gate 		return (EINVAL);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/* Do an anonymous bind for unbound socket doing listen(). */
1430Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0) {
1440Sstevel@tonic-gate 		struct sockaddr_storage ss;
1450Sstevel@tonic-gate 		int ret;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		bzero(&ss, sizeof (ss));
14811042SErik.Nordmark@Sun.COM 		ss.ss_family = connp->conn_family;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1510Sstevel@tonic-gate 		if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
1524505Skcpoon 		    sizeof (ss))) != 0)
1530Sstevel@tonic-gate 			return (ret);
1540Sstevel@tonic-gate 		RUN_SCTP(sctp)
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
15711042SErik.Nordmark@Sun.COM 	/* Cache things in the ixa without any refhold */
15811849SErik.Nordmark@Sun.COM 	ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
15911042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_cred = connp->conn_cred;
16011042SErik.Nordmark@Sun.COM 	connp->conn_ixa->ixa_cpid = connp->conn_cpid;
16111042SErik.Nordmark@Sun.COM 	if (is_system_labeled())
16211042SErik.Nordmark@Sun.COM 		connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
16311042SErik.Nordmark@Sun.COM 
1640Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_LISTEN;
1650Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
16611066Srafael.vanoni@sun.com 	sctp->sctp_last_secret_update = ddi_get_lbolt64();
1670Sstevel@tonic-gate 	bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
168*12869SKacheong.Poon@Sun.COM 
169*12869SKacheong.Poon@Sun.COM 	/*
170*12869SKacheong.Poon@Sun.COM 	 * If there is an association limit, allocate and initialize
171*12869SKacheong.Poon@Sun.COM 	 * the counter struct.  Note that since listen can be called
172*12869SKacheong.Poon@Sun.COM 	 * multiple times, the struct may have been allready allocated.
173*12869SKacheong.Poon@Sun.COM 	 */
174*12869SKacheong.Poon@Sun.COM 	if (!list_is_empty(&sctps->sctps_listener_conf) &&
175*12869SKacheong.Poon@Sun.COM 	    sctp->sctp_listen_cnt == NULL) {
176*12869SKacheong.Poon@Sun.COM 		sctp_listen_cnt_t *slc;
177*12869SKacheong.Poon@Sun.COM 		uint32_t ratio;
178*12869SKacheong.Poon@Sun.COM 
179*12869SKacheong.Poon@Sun.COM 		ratio = sctp_find_listener_conf(sctps,
180*12869SKacheong.Poon@Sun.COM 		    ntohs(connp->conn_lport));
181*12869SKacheong.Poon@Sun.COM 		if (ratio != 0) {
182*12869SKacheong.Poon@Sun.COM 			uint32_t mem_ratio, tot_buf;
183*12869SKacheong.Poon@Sun.COM 
184*12869SKacheong.Poon@Sun.COM 			slc = kmem_alloc(sizeof (sctp_listen_cnt_t), KM_SLEEP);
185*12869SKacheong.Poon@Sun.COM 			/*
186*12869SKacheong.Poon@Sun.COM 			 * Calculate the connection limit based on
187*12869SKacheong.Poon@Sun.COM 			 * the configured ratio and maxusers.  Maxusers
188*12869SKacheong.Poon@Sun.COM 			 * are calculated based on memory size,
189*12869SKacheong.Poon@Sun.COM 			 * ~ 1 user per MB.  Note that the conn_rcvbuf
190*12869SKacheong.Poon@Sun.COM 			 * and conn_sndbuf may change after a
191*12869SKacheong.Poon@Sun.COM 			 * connection is accepted.  So what we have
192*12869SKacheong.Poon@Sun.COM 			 * is only an approximation.
193*12869SKacheong.Poon@Sun.COM 			 */
194*12869SKacheong.Poon@Sun.COM 			if ((tot_buf = connp->conn_rcvbuf +
195*12869SKacheong.Poon@Sun.COM 			    connp->conn_sndbuf) < MB) {
196*12869SKacheong.Poon@Sun.COM 				mem_ratio = MB / tot_buf;
197*12869SKacheong.Poon@Sun.COM 				slc->slc_max = maxusers / ratio * mem_ratio;
198*12869SKacheong.Poon@Sun.COM 			} else {
199*12869SKacheong.Poon@Sun.COM 				mem_ratio = tot_buf / MB;
200*12869SKacheong.Poon@Sun.COM 				slc->slc_max = maxusers / ratio / mem_ratio;
201*12869SKacheong.Poon@Sun.COM 			}
202*12869SKacheong.Poon@Sun.COM 			/* At least we should allow some associations! */
203*12869SKacheong.Poon@Sun.COM 			if (slc->slc_max < sctp_min_assoc_listener)
204*12869SKacheong.Poon@Sun.COM 				slc->slc_max = sctp_min_assoc_listener;
205*12869SKacheong.Poon@Sun.COM 			slc->slc_cnt = 1;
206*12869SKacheong.Poon@Sun.COM 			slc->slc_drop = 0;
207*12869SKacheong.Poon@Sun.COM 			sctp->sctp_listen_cnt = slc;
208*12869SKacheong.Poon@Sun.COM 		}
209*12869SKacheong.Poon@Sun.COM 	}
210*12869SKacheong.Poon@Sun.COM 
211*12869SKacheong.Poon@Sun.COM 
2123448Sdh155122 	tf = &sctps->sctps_listen_fanout[SCTP_LISTEN_HASH(
21311042SErik.Nordmark@Sun.COM 	    ntohs(connp->conn_lport))];
2140Sstevel@tonic-gate 	sctp_listen_hash_insert(tf, sctp);
215*12869SKacheong.Poon@Sun.COM 
2160Sstevel@tonic-gate 	WAKE_SCTP(sctp);
2170Sstevel@tonic-gate 	return (0);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * Bind the sctp_t to a sockaddr, which includes an address and other
2220Sstevel@tonic-gate  * information, such as port or flowinfo.
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate int
sctp_bind(sctp_t * sctp,struct sockaddr * sa,socklen_t len)2250Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	int		user_specified;
2280Sstevel@tonic-gate 	boolean_t	bind_to_req_port_only;
2290Sstevel@tonic-gate 	in_port_t	requested_port;
2300Sstevel@tonic-gate 	in_port_t	allocated_port;
2310Sstevel@tonic-gate 	int		err = 0;
23211042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
23311042SErik.Nordmark@Sun.COM 	uint_t		scope_id;
23411042SErik.Nordmark@Sun.COM 	sin_t		*sin;
23511042SErik.Nordmark@Sun.COM 	sin6_t		*sin6;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	RUN_SCTP(sctp);
2400Sstevel@tonic-gate 
2418348SEric.Yu@Sun.COM 	if ((sctp->sctp_state >= SCTPS_BOUND) ||
2428348SEric.Yu@Sun.COM 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING) ||
2438348SEric.Yu@Sun.COM 	    (sa == NULL || len == 0)) {
2448348SEric.Yu@Sun.COM 		/*
2458348SEric.Yu@Sun.COM 		 * Multiple binds not allowed for any SCTP socket
2468348SEric.Yu@Sun.COM 		 * Also binding with null address is not supported.
2478348SEric.Yu@Sun.COM 		 */
2480Sstevel@tonic-gate 		err = EINVAL;
2490Sstevel@tonic-gate 		goto done;
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	switch (sa->sa_family) {
2530Sstevel@tonic-gate 	case AF_INET:
25411042SErik.Nordmark@Sun.COM 		sin = (sin_t *)sa;
2550Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in) ||
25611042SErik.Nordmark@Sun.COM 		    connp->conn_family == AF_INET6) {
25711042SErik.Nordmark@Sun.COM 			err = EINVAL;
25811042SErik.Nordmark@Sun.COM 			goto done;
25911042SErik.Nordmark@Sun.COM 		}
26011042SErik.Nordmark@Sun.COM 		requested_port = ntohs(sin->sin_port);
26111042SErik.Nordmark@Sun.COM 		break;
26211042SErik.Nordmark@Sun.COM 	case AF_INET6:
26311042SErik.Nordmark@Sun.COM 		sin6 = (sin6_t *)sa;
26411042SErik.Nordmark@Sun.COM 		if (len < sizeof (struct sockaddr_in6) ||
26511042SErik.Nordmark@Sun.COM 		    connp->conn_family == AF_INET) {
2660Sstevel@tonic-gate 			err = EINVAL;
2670Sstevel@tonic-gate 			goto done;
2680Sstevel@tonic-gate 		}
26911042SErik.Nordmark@Sun.COM 		requested_port = ntohs(sin6->sin6_port);
27011042SErik.Nordmark@Sun.COM 		/* Set the flowinfo. */
27111042SErik.Nordmark@Sun.COM 		connp->conn_flowinfo =
27211042SErik.Nordmark@Sun.COM 		    sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK;
27311042SErik.Nordmark@Sun.COM 
27411042SErik.Nordmark@Sun.COM 		scope_id = sin6->sin6_scope_id;
27511042SErik.Nordmark@Sun.COM 		if (scope_id != 0 && IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
27611042SErik.Nordmark@Sun.COM 			connp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
27711042SErik.Nordmark@Sun.COM 			connp->conn_ixa->ixa_scopeid = scope_id;
27811042SErik.Nordmark@Sun.COM 			connp->conn_incoming_ifindex = scope_id;
27911042SErik.Nordmark@Sun.COM 		} else {
28011042SErik.Nordmark@Sun.COM 			connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
28111042SErik.Nordmark@Sun.COM 			connp->conn_incoming_ifindex = connp->conn_bound_if;
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 		break;
2840Sstevel@tonic-gate 	default:
2850Sstevel@tonic-gate 		err = EAFNOSUPPORT;
2860Sstevel@tonic-gate 		goto done;
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
2890Sstevel@tonic-gate 
2901676Sjpk 	err = sctp_select_port(sctp, &requested_port, &user_specified);
2911676Sjpk 	if (err != 0)
2920Sstevel@tonic-gate 		goto done;
2930Sstevel@tonic-gate 
294852Svi117747 	if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE,
295852Svi117747 	    user_specified == 1 ? htons(requested_port) : 0)) != 0) {
2960Sstevel@tonic-gate 		goto done;
297852Svi117747 	}
2981676Sjpk 	err = sctp_bindi(sctp, requested_port, bind_to_req_port_only,
2991676Sjpk 	    user_specified, &allocated_port);
3001676Sjpk 	if (err != 0) {
3010Sstevel@tonic-gate 		sctp_free_saddrs(sctp);
3021676Sjpk 	} else {
3031676Sjpk 		ASSERT(sctp->sctp_state == SCTPS_BOUND);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate done:
3060Sstevel@tonic-gate 	WAKE_SCTP(sctp);
3070Sstevel@tonic-gate 	return (err);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Perform bind/unbind operation of a list of addresses on a sctp_t
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate int
sctp_bindx(sctp_t * sctp,const void * addrs,int addrcnt,int bindop)3140Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	ASSERT(sctp != NULL);
3170Sstevel@tonic-gate 	ASSERT(addrs != NULL);
3180Sstevel@tonic-gate 	ASSERT(addrcnt > 0);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	switch (bindop) {
3210Sstevel@tonic-gate 	case SCTP_BINDX_ADD_ADDR:
322852Svi117747 		return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE,
32311042SErik.Nordmark@Sun.COM 		    sctp->sctp_connp->conn_lport));
3240Sstevel@tonic-gate 	case SCTP_BINDX_REM_ADDR:
3250Sstevel@tonic-gate 		return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
3260Sstevel@tonic-gate 	default:
3270Sstevel@tonic-gate 		return (EINVAL);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate  * Add a list of addresses to a sctp_t.
3330Sstevel@tonic-gate  */
3340Sstevel@tonic-gate int
sctp_bind_add(sctp_t * sctp,const void * addrs,uint32_t addrcnt,boolean_t caller_hold_lock,in_port_t port)3350Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
336852Svi117747     boolean_t caller_hold_lock, in_port_t port)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	int		err = 0;
3390Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
3403448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
34111042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (!caller_hold_lock)
3440Sstevel@tonic-gate 		RUN_SCTP(sctp);
3450Sstevel@tonic-gate 
3464505Skcpoon 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
3474505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
3480Sstevel@tonic-gate 		if (!caller_hold_lock)
3490Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3500Sstevel@tonic-gate 		return (EINVAL);
3510Sstevel@tonic-gate 	}
352252Svi117747 
353252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
354252Svi117747 		/*
355252Svi117747 		 * Let's do some checking here rather than undoing the
356252Svi117747 		 * add later (for these reasons).
357252Svi117747 		 */
3583448Sdh155122 		if (!sctps->sctps_addip_enabled ||
3593448Sdh155122 		    !sctp->sctp_understands_asconf ||
360252Svi117747 		    !sctp->sctp_understands_addip) {
361252Svi117747 			if (!caller_hold_lock)
362252Svi117747 				WAKE_SCTP(sctp);
363252Svi117747 			return (EINVAL);
364252Svi117747 		}
3650Sstevel@tonic-gate 		do_asconf = B_TRUE;
366252Svi117747 	}
367852Svi117747 	/*
368852Svi117747 	 * On a clustered node, for an inaddr_any bind, we will pass the list
369852Svi117747 	 * of all the addresses in the global list, minus any address on the
370852Svi117747 	 * loopback interface, and expect the clustering susbsystem to give us
371852Svi117747 	 * the correct list for the 'port'. For explicit binds we give the
372852Svi117747 	 * list of addresses  and the clustering module validates it for the
373852Svi117747 	 * 'port'.
374852Svi117747 	 *
375852Svi117747 	 * On a non-clustered node, cl_sctp_check_addrs will be NULL and
376852Svi117747 	 * we proceed as usual.
377852Svi117747 	 */
378852Svi117747 	if (cl_sctp_check_addrs != NULL) {
379852Svi117747 		uchar_t		*addrlist = NULL;
380852Svi117747 		size_t		size = 0;
381852Svi117747 		int		unspec = 0;
382852Svi117747 		boolean_t	do_listen;
383852Svi117747 		uchar_t		*llist = NULL;
384852Svi117747 		size_t		lsize = 0;
385852Svi117747 
386852Svi117747 		/*
387852Svi117747 		 * If we are adding addresses after listening, but before
388852Svi117747 		 * an association is established, we need to update the
389852Svi117747 		 * clustering module with this info.
390852Svi117747 		 */
391852Svi117747 		do_listen = !do_asconf && sctp->sctp_state > SCTPS_BOUND &&
392852Svi117747 		    cl_sctp_listen != NULL;
393852Svi117747 
394852Svi117747 		err = sctp_get_addrlist(sctp, addrs, &addrcnt, &addrlist,
395852Svi117747 		    &unspec, &size);
396852Svi117747 		if (err != 0) {
397852Svi117747 			ASSERT(addrlist == NULL);
398852Svi117747 			ASSERT(addrcnt == 0);
399852Svi117747 			ASSERT(size == 0);
400852Svi117747 			if (!caller_hold_lock)
401852Svi117747 				WAKE_SCTP(sctp);
4023448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_check_addrs);
403852Svi117747 			return (err);
404852Svi117747 		}
405852Svi117747 		ASSERT(addrlist != NULL);
40611042SErik.Nordmark@Sun.COM 		(*cl_sctp_check_addrs)(connp->conn_family, port, &addrlist,
407852Svi117747 		    size, &addrcnt, unspec == 1);
408852Svi117747 		if (addrcnt == 0) {
409852Svi117747 			/* We free the list */
410852Svi117747 			kmem_free(addrlist, size);
411852Svi117747 			if (!caller_hold_lock)
412852Svi117747 				WAKE_SCTP(sctp);
413852Svi117747 			return (EINVAL);
414852Svi117747 		}
415852Svi117747 		if (do_listen) {
416852Svi117747 			lsize = sizeof (in6_addr_t) * addrcnt;
417852Svi117747 			llist = kmem_alloc(lsize, KM_SLEEP);
418852Svi117747 		}
419852Svi117747 		err = sctp_valid_addr_list(sctp, addrlist, addrcnt, llist,
420852Svi117747 		    lsize);
421852Svi117747 		if (err == 0 && do_listen) {
42211042SErik.Nordmark@Sun.COM 			(*cl_sctp_listen)(connp->conn_family, llist,
42311042SErik.Nordmark@Sun.COM 			    addrcnt, connp->conn_lport);
424852Svi117747 			/* list will be freed by the clustering module */
425852Svi117747 		} else if (err != 0 && llist != NULL) {
426852Svi117747 			kmem_free(llist, lsize);
427852Svi117747 		}
428852Svi117747 		/* free the list we allocated */
429852Svi117747 		kmem_free(addrlist, size);
430852Svi117747 	} else {
431852Svi117747 		err = sctp_valid_addr_list(sctp, addrs, addrcnt, NULL, 0);
432852Svi117747 	}
4330Sstevel@tonic-gate 	if (err != 0) {
4340Sstevel@tonic-gate 		if (!caller_hold_lock)
4350Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4360Sstevel@tonic-gate 		return (err);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	/* Need to send  ASCONF messages */
4390Sstevel@tonic-gate 	if (do_asconf) {
4400Sstevel@tonic-gate 		err = sctp_add_ip(sctp, addrs, addrcnt);
4410Sstevel@tonic-gate 		if (err != 0) {
4420Sstevel@tonic-gate 			sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
4430Sstevel@tonic-gate 			if (!caller_hold_lock)
4440Sstevel@tonic-gate 				WAKE_SCTP(sctp);
4450Sstevel@tonic-gate 			return (err);
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 	if (!caller_hold_lock)
4490Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4500Sstevel@tonic-gate 	return (0);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * Remove one or more addresses bound to the sctp_t.
4550Sstevel@tonic-gate  */
4560Sstevel@tonic-gate int
sctp_bind_del(sctp_t * sctp,const void * addrs,uint32_t addrcnt,boolean_t caller_hold_lock)4570Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
4580Sstevel@tonic-gate     boolean_t caller_hold_lock)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	int		error = 0;
4610Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
462852Svi117747 	uchar_t		*ulist = NULL;
463852Svi117747 	size_t		usize = 0;
4643448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
46511042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (!caller_hold_lock)
4680Sstevel@tonic-gate 		RUN_SCTP(sctp);
4690Sstevel@tonic-gate 
4704505Skcpoon 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
4714505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
4720Sstevel@tonic-gate 		if (!caller_hold_lock)
4730Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4740Sstevel@tonic-gate 		return (EINVAL);
4750Sstevel@tonic-gate 	}
476252Svi117747 	/*
477252Svi117747 	 * Fail the remove if we are beyond listen, but can't send this
478252Svi117747 	 * to the peer.
479252Svi117747 	 */
480252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
4813448Sdh155122 		if (!sctps->sctps_addip_enabled ||
4823448Sdh155122 		    !sctp->sctp_understands_asconf ||
483252Svi117747 		    !sctp->sctp_understands_addip) {
484252Svi117747 			if (!caller_hold_lock)
485252Svi117747 				WAKE_SCTP(sctp);
486252Svi117747 			return (EINVAL);
487252Svi117747 		}
4880Sstevel@tonic-gate 		do_asconf = B_TRUE;
489252Svi117747 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/* Can't delete the last address nor all of the addresses */
4920Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
4930Sstevel@tonic-gate 		if (!caller_hold_lock)
4940Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4950Sstevel@tonic-gate 		return (EINVAL);
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
498852Svi117747 	if (cl_sctp_unlisten != NULL && !do_asconf &&
499852Svi117747 	    sctp->sctp_state > SCTPS_BOUND) {
500852Svi117747 		usize = sizeof (in6_addr_t) * addrcnt;
501852Svi117747 		ulist = kmem_alloc(usize, KM_SLEEP);
502852Svi117747 	}
503852Svi117747 
504852Svi117747 	error = sctp_del_ip(sctp, addrs, addrcnt, ulist, usize);
505852Svi117747 	if (error != 0) {
506852Svi117747 		if (ulist != NULL)
507852Svi117747 			kmem_free(ulist, usize);
508852Svi117747 		if (!caller_hold_lock)
509852Svi117747 			WAKE_SCTP(sctp);
510852Svi117747 		return (error);
511852Svi117747 	}
512852Svi117747 	/* ulist will be non-NULL only if cl_sctp_unlisten is non-NULL */
513852Svi117747 	if (ulist != NULL) {
514852Svi117747 		ASSERT(cl_sctp_unlisten != NULL);
51511042SErik.Nordmark@Sun.COM 		(*cl_sctp_unlisten)(connp->conn_family, ulist, addrcnt,
51611042SErik.Nordmark@Sun.COM 		    connp->conn_lport);
517852Svi117747 		/* ulist will be freed by the clustering module */
518852Svi117747 	}
5190Sstevel@tonic-gate 	if (!caller_hold_lock)
5200Sstevel@tonic-gate 		WAKE_SCTP(sctp);
5210Sstevel@tonic-gate 	return (error);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5251676Sjpk  * Returns 0 for success, errno value otherwise.
5261676Sjpk  *
5271676Sjpk  * If the "bind_to_req_port_only" parameter is set and the requested port
5281676Sjpk  * number is available, then set allocated_port to it.  If not available,
5291676Sjpk  * return an error.
5300Sstevel@tonic-gate  *
5311676Sjpk  * If the "bind_to_req_port_only" parameter is not set and the requested port
5321676Sjpk  * number is available, then set allocated_port to it.  If not available,
5331676Sjpk  * find the first anonymous port we can and set allocated_port to that.  If no
5341676Sjpk  * anonymous ports are available, return an error.
5350Sstevel@tonic-gate  *
5361676Sjpk  * In either case, when succeeding, update the sctp_t to record the port number
5370Sstevel@tonic-gate  * and insert it in the bind hash table.
5380Sstevel@tonic-gate  */
5391676Sjpk int
sctp_bindi(sctp_t * sctp,in_port_t port,boolean_t bind_to_req_port_only,int user_specified,in_port_t * allocated_port)5401676Sjpk sctp_bindi(sctp_t *sctp, in_port_t port, boolean_t bind_to_req_port_only,
5411676Sjpk     int user_specified, in_port_t *allocated_port)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	/* number of times we have run around the loop */
5440Sstevel@tonic-gate 	int count = 0;
5450Sstevel@tonic-gate 	/* maximum number of times to run around the loop */
5460Sstevel@tonic-gate 	int loopmax;
5473448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
54811042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
54911042SErik.Nordmark@Sun.COM 	zone_t *zone = crgetzone(connp->conn_cred);
55011042SErik.Nordmark@Sun.COM 	zoneid_t zoneid = connp->conn_zoneid;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * Lookup for free addresses is done in a loop and "loopmax"
5540Sstevel@tonic-gate 	 * influences how long we spin in the loop
5550Sstevel@tonic-gate 	 */
5560Sstevel@tonic-gate 	if (bind_to_req_port_only) {
5570Sstevel@tonic-gate 		/*
5580Sstevel@tonic-gate 		 * If the requested port is busy, don't bother to look
5590Sstevel@tonic-gate 		 * for a new one. Setting loop maximum count to 1 has
5600Sstevel@tonic-gate 		 * that effect.
5610Sstevel@tonic-gate 		 */
5620Sstevel@tonic-gate 		loopmax = 1;
5630Sstevel@tonic-gate 	} else {
5640Sstevel@tonic-gate 		/*
5650Sstevel@tonic-gate 		 * If the requested port is busy, look for a free one
5660Sstevel@tonic-gate 		 * in the anonymous port range.
5670Sstevel@tonic-gate 		 * Set loopmax appropriately so that one does not look
5680Sstevel@tonic-gate 		 * forever in the case all of the anonymous ports are in use.
5690Sstevel@tonic-gate 		 */
5703448Sdh155122 		loopmax = (sctps->sctps_largest_anon_port -
5713448Sdh155122 		    sctps->sctps_smallest_anon_port + 1);
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 	do {
5740Sstevel@tonic-gate 		uint16_t	lport;
5750Sstevel@tonic-gate 		sctp_tf_t	*tbf;
5760Sstevel@tonic-gate 		sctp_t		*lsctp;
5770Sstevel@tonic-gate 		int		addrcmp;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		lport = htons(port);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		/*
5820Sstevel@tonic-gate 		 * Ensure that the sctp_t is not currently in the bind hash.
5830Sstevel@tonic-gate 		 * Hold the lock on the hash bucket to ensure that
5840Sstevel@tonic-gate 		 * the duplicate check plus the insertion is an atomic
5850Sstevel@tonic-gate 		 * operation.
5860Sstevel@tonic-gate 		 *
5870Sstevel@tonic-gate 		 * This function does an inline lookup on the bind hash list
5880Sstevel@tonic-gate 		 * Make sure that we access only members of sctp_t
5890Sstevel@tonic-gate 		 * and that we don't look at sctp_sctp, since we are not
5900Sstevel@tonic-gate 		 * doing a SCTPB_REFHOLD. For more details please see the notes
5910Sstevel@tonic-gate 		 * in sctp_compress()
5920Sstevel@tonic-gate 		 */
5930Sstevel@tonic-gate 		sctp_bind_hash_remove(sctp);
5943448Sdh155122 		tbf = &sctps->sctps_bind_fanout[SCTP_BIND_HASH(port)];
5950Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
5960Sstevel@tonic-gate 		for (lsctp = tbf->tf_sctp; lsctp != NULL;
5970Sstevel@tonic-gate 		    lsctp = lsctp->sctp_bind_hash) {
59811042SErik.Nordmark@Sun.COM 			conn_t *lconnp = lsctp->sctp_connp;
5990Sstevel@tonic-gate 
60011042SErik.Nordmark@Sun.COM 			if (lport != lconnp->conn_lport ||
6010Sstevel@tonic-gate 			    lsctp->sctp_state < SCTPS_BOUND)
6020Sstevel@tonic-gate 				continue;
6030Sstevel@tonic-gate 
6041676Sjpk 			/*
6051676Sjpk 			 * On a labeled system, we must treat bindings to ports
6061676Sjpk 			 * on shared IP addresses by sockets with MAC exemption
6071676Sjpk 			 * privilege as being in all zones, as there's
6081676Sjpk 			 * otherwise no way to identify the right receiver.
6091676Sjpk 			 */
61011042SErik.Nordmark@Sun.COM 			if (lconnp->conn_zoneid != zoneid &&
61111042SErik.Nordmark@Sun.COM 			    lconnp->conn_mac_mode == CONN_MAC_DEFAULT &&
61211042SErik.Nordmark@Sun.COM 			    connp->conn_mac_mode == CONN_MAC_DEFAULT)
6131676Sjpk 				continue;
6141676Sjpk 
6150Sstevel@tonic-gate 			addrcmp = sctp_compare_saddrs(sctp, lsctp);
6160Sstevel@tonic-gate 			if (addrcmp != SCTP_ADDR_DISJOINT) {
61711042SErik.Nordmark@Sun.COM 				if (!connp->conn_reuseaddr) {
6180Sstevel@tonic-gate 					/* in use */
6190Sstevel@tonic-gate 					break;
6200Sstevel@tonic-gate 				} else if (lsctp->sctp_state == SCTPS_BOUND ||
6210Sstevel@tonic-gate 				    lsctp->sctp_state == SCTPS_LISTEN) {
6220Sstevel@tonic-gate 					/*
6230Sstevel@tonic-gate 					 * socket option SO_REUSEADDR is set
6240Sstevel@tonic-gate 					 * on the binding sctp_t.
6250Sstevel@tonic-gate 					 *
6260Sstevel@tonic-gate 					 * We have found a match of IP source
6270Sstevel@tonic-gate 					 * address and source port, which is
6280Sstevel@tonic-gate 					 * refused regardless of the
6290Sstevel@tonic-gate 					 * SO_REUSEADDR setting, so we break.
6300Sstevel@tonic-gate 					 */
6310Sstevel@tonic-gate 					break;
6320Sstevel@tonic-gate 				}
6330Sstevel@tonic-gate 			}
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 		if (lsctp != NULL) {
6360Sstevel@tonic-gate 			/* The port number is busy */
6370Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
6380Sstevel@tonic-gate 		} else {
6391676Sjpk 			if (is_system_labeled()) {
6401676Sjpk 				mlp_type_t addrtype, mlptype;
64111042SErik.Nordmark@Sun.COM 				uint_t ipversion;
6421676Sjpk 
6431676Sjpk 				/*
6441676Sjpk 				 * On a labeled system we must check the type
6451676Sjpk 				 * of the binding requested by the user (either
6461676Sjpk 				 * MLP or SLP on shared and private addresses),
6471676Sjpk 				 * and that the user's requested binding
6481676Sjpk 				 * is permitted.
6491676Sjpk 				 */
65011042SErik.Nordmark@Sun.COM 				if (connp->conn_family == AF_INET)
65111042SErik.Nordmark@Sun.COM 					ipversion = IPV4_VERSION;
65211042SErik.Nordmark@Sun.COM 				else
65311042SErik.Nordmark@Sun.COM 					ipversion = IPV6_VERSION;
65411042SErik.Nordmark@Sun.COM 
65510352Sdanmcd@sun.com 				addrtype = tsol_mlp_addr_type(
65610352Sdanmcd@sun.com 				    connp->conn_allzones ? ALL_ZONES :
65710352Sdanmcd@sun.com 				    zone->zone_id,
65811042SErik.Nordmark@Sun.COM 				    ipversion,
65911042SErik.Nordmark@Sun.COM 				    connp->conn_family == AF_INET ?
6601676Sjpk 				    (void *)&sctp->sctp_ipha->ipha_src :
6613448Sdh155122 				    (void *)&sctp->sctp_ip6h->ip6_src,
6623448Sdh155122 				    sctps->sctps_netstack->netstack_ip);
6631676Sjpk 
6641676Sjpk 				/*
6651676Sjpk 				 * tsol_mlp_addr_type returns the possibilities
6661676Sjpk 				 * for the selected address.  Since all local
6671676Sjpk 				 * addresses are either private or shared, the
6681676Sjpk 				 * return value mlptSingle means "local address
6691676Sjpk 				 * not valid (interface not present)."
6701676Sjpk 				 */
6711676Sjpk 				if (addrtype == mlptSingle) {
6721676Sjpk 					mutex_exit(&tbf->tf_lock);
6731676Sjpk 					return (EADDRNOTAVAIL);
6741676Sjpk 				}
6751676Sjpk 				mlptype = tsol_mlp_port_type(zone, IPPROTO_SCTP,
6761676Sjpk 				    port, addrtype);
6771676Sjpk 				if (mlptype != mlptSingle) {
6781676Sjpk 					if (secpolicy_net_bindmlp(connp->
6791676Sjpk 					    conn_cred) != 0) {
6801676Sjpk 						mutex_exit(&tbf->tf_lock);
6811676Sjpk 						return (EACCES);
6821676Sjpk 					}
6831676Sjpk 					/*
6841676Sjpk 					 * If we're binding a shared MLP, then
6851676Sjpk 					 * make sure that this zone is the one
6861676Sjpk 					 * that owns that MLP.  Shared MLPs can
6871676Sjpk 					 * be owned by at most one zone.
6883448Sdh155122 					 *
6893448Sdh155122 					 * No need to handle exclusive-stack
6903448Sdh155122 					 * zones since ALL_ZONES only applies
6913448Sdh155122 					 * to the shared stack.
6921676Sjpk 					 */
6931676Sjpk 
6941676Sjpk 					if (mlptype == mlptShared &&
6951676Sjpk 					    addrtype == mlptShared &&
6961676Sjpk 					    connp->conn_zoneid !=
6971676Sjpk 					    tsol_mlp_findzone(IPPROTO_SCTP,
6981676Sjpk 					    lport)) {
6991676Sjpk 						mutex_exit(&tbf->tf_lock);
7001676Sjpk 						return (EACCES);
7011676Sjpk 					}
7021676Sjpk 					connp->conn_mlp_type = mlptype;
7031676Sjpk 				}
7041676Sjpk 			}
7050Sstevel@tonic-gate 			/*
7060Sstevel@tonic-gate 			 * This port is ours. Insert in fanout and mark as
7070Sstevel@tonic-gate 			 * bound to prevent others from getting the port
7080Sstevel@tonic-gate 			 * number.
7090Sstevel@tonic-gate 			 */
7100Sstevel@tonic-gate 			sctp->sctp_state = SCTPS_BOUND;
71111042SErik.Nordmark@Sun.COM 			connp->conn_lport = lport;
7120Sstevel@tonic-gate 
7133448Sdh155122 			ASSERT(&sctps->sctps_bind_fanout[
7144505Skcpoon 			    SCTP_BIND_HASH(port)] == tbf);
7150Sstevel@tonic-gate 			sctp_bind_hash_insert(tbf, sctp, 1);
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 			/*
7200Sstevel@tonic-gate 			 * We don't want sctp_next_port_to_try to "inherit"
7210Sstevel@tonic-gate 			 * a port number supplied by the user in a bind.
7221676Sjpk 			 *
7230Sstevel@tonic-gate 			 * This is the only place where sctp_next_port_to_try
7240Sstevel@tonic-gate 			 * is updated. After the update, it may or may not
7250Sstevel@tonic-gate 			 * be in the valid range.
7260Sstevel@tonic-gate 			 */
7271676Sjpk 			if (user_specified == 0)
7283448Sdh155122 				sctps->sctps_next_port_to_try = port + 1;
7291676Sjpk 
7301676Sjpk 			*allocated_port = port;
7311676Sjpk 
7321676Sjpk 			return (0);
7330Sstevel@tonic-gate 		}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		if ((count == 0) && (user_specified)) {
7360Sstevel@tonic-gate 			/*
7370Sstevel@tonic-gate 			 * We may have to return an anonymous port. So
7380Sstevel@tonic-gate 			 * get one to start with.
7390Sstevel@tonic-gate 			 */
7403448Sdh155122 			port = sctp_update_next_port(
7413448Sdh155122 			    sctps->sctps_next_port_to_try,
7423448Sdh155122 			    zone, sctps);
7430Sstevel@tonic-gate 			user_specified = 0;
7440Sstevel@tonic-gate 		} else {
7453448Sdh155122 			port = sctp_update_next_port(port + 1, zone, sctps);
7460Sstevel@tonic-gate 		}
7471676Sjpk 		if (port == 0)
7481676Sjpk 			break;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 		/*
7510Sstevel@tonic-gate 		 * Don't let this loop run forever in the case where
7520Sstevel@tonic-gate 		 * all of the anonymous ports are in use.
7530Sstevel@tonic-gate 		 */
7540Sstevel@tonic-gate 	} while (++count < loopmax);
7551676Sjpk 
7561676Sjpk 	return (bind_to_req_port_only ? EADDRINUSE : EADDRNOTAVAIL);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * Don't let port fall into the privileged range.
7610Sstevel@tonic-gate  * Since the extra privileged ports can be arbitrary we also
7620Sstevel@tonic-gate  * ensure that we exclude those from consideration.
7630Sstevel@tonic-gate  * sctp_g_epriv_ports is not sorted thus we loop over it until
7640Sstevel@tonic-gate  * there are no changes.
7650Sstevel@tonic-gate  *
7660Sstevel@tonic-gate  * Note: No locks are held when inspecting sctp_g_*epriv_ports
7670Sstevel@tonic-gate  * but instead the code relies on:
7680Sstevel@tonic-gate  * - the fact that the address of the array and its size never changes
7690Sstevel@tonic-gate  * - the atomic assignment of the elements of the array
7700Sstevel@tonic-gate  */
7710Sstevel@tonic-gate in_port_t
sctp_update_next_port(in_port_t port,zone_t * zone,sctp_stack_t * sctps)7723448Sdh155122 sctp_update_next_port(in_port_t port, zone_t *zone, sctp_stack_t *sctps)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	int i;
7751676Sjpk 	boolean_t restart = B_FALSE;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate retry:
7783448Sdh155122 	if (port < sctps->sctps_smallest_anon_port)
7793448Sdh155122 		port = sctps->sctps_smallest_anon_port;
7800Sstevel@tonic-gate 
7813448Sdh155122 	if (port > sctps->sctps_largest_anon_port) {
7821676Sjpk 		if (restart)
7831676Sjpk 			return (0);
7841676Sjpk 		restart = B_TRUE;
7853448Sdh155122 		port = sctps->sctps_smallest_anon_port;
7861676Sjpk 	}
7871676Sjpk 
7883448Sdh155122 	if (port < sctps->sctps_smallest_nonpriv_port)
7893448Sdh155122 		port = sctps->sctps_smallest_nonpriv_port;
7900Sstevel@tonic-gate 
7913448Sdh155122 	for (i = 0; i < sctps->sctps_g_num_epriv_ports; i++) {
7923448Sdh155122 		if (port == sctps->sctps_g_epriv_ports[i]) {
7930Sstevel@tonic-gate 			port++;
7940Sstevel@tonic-gate 			/*
7950Sstevel@tonic-gate 			 * Make sure whether the port is in the
7960Sstevel@tonic-gate 			 * valid range.
7970Sstevel@tonic-gate 			 *
7980Sstevel@tonic-gate 			 * XXX Note that if sctp_g_epriv_ports contains
7990Sstevel@tonic-gate 			 * all the anonymous ports this will be an
8000Sstevel@tonic-gate 			 * infinite loop.
8010Sstevel@tonic-gate 			 */
8020Sstevel@tonic-gate 			goto retry;
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate 	}
8051676Sjpk 
8061676Sjpk 	if (is_system_labeled() &&
8071676Sjpk 	    (i = tsol_next_port(zone, port, IPPROTO_SCTP, B_TRUE)) != 0) {
8081676Sjpk 		port = i;
8091676Sjpk 		goto retry;
8101676Sjpk 	}
8111676Sjpk 
8120Sstevel@tonic-gate 	return (port);
8130Sstevel@tonic-gate }
814