17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2177c67f2fSkcpoon 
227c478bd9Sstevel@tonic-gate /*
23ddf7fe95Scasper  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/stream.h>
307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
337c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
347c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <sys/random.h>
377c478bd9Sstevel@tonic-gate #include <sys/policy.h>
3845916cd2Sjpk #include <sys/tsol/tndb.h>
3945916cd2Sjpk #include <sys/tsol/tnet.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <inet/common.h>
457c478bd9Sstevel@tonic-gate #include <inet/ip.h>
467c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
477c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
487c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
497c478bd9Sstevel@tonic-gate #include "sctp_asconf.h"
507c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Returns 0 on success, EACCES on permission failure.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate static int
567c478bd9Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified)
577c478bd9Sstevel@tonic-gate {
58f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
59f4b3ec61Sdh 
607c478bd9Sstevel@tonic-gate 	/*
617c478bd9Sstevel@tonic-gate 	 * Get a valid port (within the anonymous range and should not
627c478bd9Sstevel@tonic-gate 	 * be a privileged one) to use if the user has not given a port.
637c478bd9Sstevel@tonic-gate 	 * If multiple threads are here, they may all start with
647c478bd9Sstevel@tonic-gate 	 * with the same initial port. But, it should be fine as long as
657c478bd9Sstevel@tonic-gate 	 * sctp_bindi will ensure that no two threads will be assigned
667c478bd9Sstevel@tonic-gate 	 * the same port.
677c478bd9Sstevel@tonic-gate 	 */
687c478bd9Sstevel@tonic-gate 	if (*requested_port == 0) {
69f4b3ec61Sdh 		*requested_port = sctp_update_next_port(
70f4b3ec61Sdh 		    sctps->sctps_next_port_to_try,
71f4b3ec61Sdh 		    crgetzone(sctp->sctp_credp), sctps);
7245916cd2Sjpk 		if (*requested_port == 0)
7345916cd2Sjpk 			return (EACCES);
747c478bd9Sstevel@tonic-gate 		*user_specified = 0;
757c478bd9Sstevel@tonic-gate 	} else {
767c478bd9Sstevel@tonic-gate 		int i;
777c478bd9Sstevel@tonic-gate 		boolean_t priv = B_FALSE;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		/*
807c478bd9Sstevel@tonic-gate 		 * If the requested_port is in the well-known privileged range,
817c478bd9Sstevel@tonic-gate 		 * verify that the stream was opened by a privileged user.
827c478bd9Sstevel@tonic-gate 		 * Note: No locks are held when inspecting sctp_g_*epriv_ports
837c478bd9Sstevel@tonic-gate 		 * but instead the code relies on:
847c478bd9Sstevel@tonic-gate 		 * - the fact that the address of the array and its size never
857c478bd9Sstevel@tonic-gate 		 *   changes
867c478bd9Sstevel@tonic-gate 		 * - the atomic assignment of the elements of the array
877c478bd9Sstevel@tonic-gate 		 */
88f4b3ec61Sdh 		if (*requested_port < sctps->sctps_smallest_nonpriv_port) {
897c478bd9Sstevel@tonic-gate 			priv = B_TRUE;
907c478bd9Sstevel@tonic-gate 		} else {
91f4b3ec61Sdh 			for (i = 0; i < sctps->sctps_g_num_epriv_ports; i++) {
92f4b3ec61Sdh 				if (*requested_port ==
93f4b3ec61Sdh 				    sctps->sctps_g_epriv_ports[i]) {
947c478bd9Sstevel@tonic-gate 					priv = B_TRUE;
957c478bd9Sstevel@tonic-gate 					break;
967c478bd9Sstevel@tonic-gate 				}
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 		if (priv) {
1007c478bd9Sstevel@tonic-gate 			/*
1017c478bd9Sstevel@tonic-gate 			 * sctp_bind() should take a cred_t argument so that
1027c478bd9Sstevel@tonic-gate 			 * we can use it here.
1037c478bd9Sstevel@tonic-gate 			 */
1047c478bd9Sstevel@tonic-gate 			if (secpolicy_net_privaddr(sctp->sctp_credp,
105ddf7fe95Scasper 			    *requested_port, IPPROTO_SCTP) != 0) {
1067c478bd9Sstevel@tonic-gate 				dprint(1,
1077c478bd9Sstevel@tonic-gate 				    ("sctp_bind(x): no prive for port %d",
1087c478bd9Sstevel@tonic-gate 				    *requested_port));
10945916cd2Sjpk 				return (EACCES);
1107c478bd9Sstevel@tonic-gate 			}
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 		*user_specified = 1;
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	return (0);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int
1197c478bd9Sstevel@tonic-gate sctp_listen(sctp_t *sctp)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	sctp_tf_t	*tf;
122f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
1257c478bd9Sstevel@tonic-gate 	/*
1267c478bd9Sstevel@tonic-gate 	 * TCP handles listen() increasing the backlog, need to check
1271d8c4025Svi 	 * if it should be handled here too
1287c478bd9Sstevel@tonic-gate 	 */
129b34b8d1aSkcpoon 	if (sctp->sctp_state > SCTPS_BOUND ||
130b34b8d1aSkcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
1317c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1327c478bd9Sstevel@tonic-gate 		return (EINVAL);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/* Do an anonymous bind for unbound socket doing listen(). */
1367c478bd9Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0) {
1377c478bd9Sstevel@tonic-gate 		struct sockaddr_storage ss;
1387c478bd9Sstevel@tonic-gate 		int ret;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		bzero(&ss, sizeof (ss));
1417c478bd9Sstevel@tonic-gate 		ss.ss_family = sctp->sctp_family;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1447c478bd9Sstevel@tonic-gate 		if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
145b34b8d1aSkcpoon 		    sizeof (ss))) != 0)
1467c478bd9Sstevel@tonic-gate 			return (ret);
1477c478bd9Sstevel@tonic-gate 		RUN_SCTP(sctp)
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_LISTEN;
1517c478bd9Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
1527c478bd9Sstevel@tonic-gate 	sctp->sctp_last_secret_update = lbolt64;
1537c478bd9Sstevel@tonic-gate 	bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
154f4b3ec61Sdh 	tf = &sctps->sctps_listen_fanout[SCTP_LISTEN_HASH(
155b34b8d1aSkcpoon 	    ntohs(sctp->sctp_lport))];
1567c478bd9Sstevel@tonic-gate 	sctp_listen_hash_insert(tf, sctp);
1577c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
1587c478bd9Sstevel@tonic-gate 	return (0);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Bind the sctp_t to a sockaddr, which includes an address and other
1637c478bd9Sstevel@tonic-gate  * information, such as port or flowinfo.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate int
1667c478bd9Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	int		user_specified;
1697c478bd9Sstevel@tonic-gate 	boolean_t	bind_to_req_port_only;
1707c478bd9Sstevel@tonic-gate 	in_port_t	requested_port;
1717c478bd9Sstevel@tonic-gate 	in_port_t	allocated_port;
1727c478bd9Sstevel@tonic-gate 	int		err = 0;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
1777c478bd9Sstevel@tonic-gate 
178*0f1702c5SYu Xiangning 	if ((sctp->sctp_state >= SCTPS_BOUND) ||
179*0f1702c5SYu Xiangning 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING) ||
180*0f1702c5SYu Xiangning 	    (sa == NULL || len == 0)) {
181*0f1702c5SYu Xiangning 		/*
182*0f1702c5SYu Xiangning 		 * Multiple binds not allowed for any SCTP socket
183*0f1702c5SYu Xiangning 		 * Also binding with null address is not supported.
184*0f1702c5SYu Xiangning 		 */
1857c478bd9Sstevel@tonic-gate 		err = EINVAL;
1867c478bd9Sstevel@tonic-gate 		goto done;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
1907c478bd9Sstevel@tonic-gate 	case AF_INET:
1917c478bd9Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in) ||
1927c478bd9Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
1937c478bd9Sstevel@tonic-gate 			err = EINVAL;
1947c478bd9Sstevel@tonic-gate 			goto done;
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port);
1977c478bd9Sstevel@tonic-gate 		break;
1987c478bd9Sstevel@tonic-gate 	case AF_INET6:
1997c478bd9Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in6) ||
2007c478bd9Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET) {
2017c478bd9Sstevel@tonic-gate 			err = EINVAL;
2027c478bd9Sstevel@tonic-gate 			goto done;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
2057c478bd9Sstevel@tonic-gate 		/* Set the flowinfo. */
2067c478bd9Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_vcf =
2077c478bd9Sstevel@tonic-gate 		    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
2087c478bd9Sstevel@tonic-gate 		    (((struct sockaddr_in6 *)sa)->sin6_flowinfo &
2097c478bd9Sstevel@tonic-gate 		    ~IPV6_VERS_AND_FLOW_MASK);
2107c478bd9Sstevel@tonic-gate 		break;
2117c478bd9Sstevel@tonic-gate 	default:
2127c478bd9Sstevel@tonic-gate 		err = EAFNOSUPPORT;
2137c478bd9Sstevel@tonic-gate 		goto done;
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 	bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
2167c478bd9Sstevel@tonic-gate 
21745916cd2Sjpk 	err = sctp_select_port(sctp, &requested_port, &user_specified);
21845916cd2Sjpk 	if (err != 0)
2197c478bd9Sstevel@tonic-gate 		goto done;
2207c478bd9Sstevel@tonic-gate 
2211d8c4025Svi 	if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE,
2221d8c4025Svi 	    user_specified == 1 ? htons(requested_port) : 0)) != 0) {
2237c478bd9Sstevel@tonic-gate 		goto done;
2241d8c4025Svi 	}
22545916cd2Sjpk 	err = sctp_bindi(sctp, requested_port, bind_to_req_port_only,
22645916cd2Sjpk 	    user_specified, &allocated_port);
22745916cd2Sjpk 	if (err != 0) {
2287c478bd9Sstevel@tonic-gate 		sctp_free_saddrs(sctp);
22945916cd2Sjpk 	} else {
23045916cd2Sjpk 		ASSERT(sctp->sctp_state == SCTPS_BOUND);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate done:
2337c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
2347c478bd9Sstevel@tonic-gate 	return (err);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * Perform bind/unbind operation of a list of addresses on a sctp_t
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate int
2417c478bd9Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2447c478bd9Sstevel@tonic-gate 	ASSERT(addrs != NULL);
2457c478bd9Sstevel@tonic-gate 	ASSERT(addrcnt > 0);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	switch (bindop) {
2487c478bd9Sstevel@tonic-gate 	case SCTP_BINDX_ADD_ADDR:
2491d8c4025Svi 		return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE,
2501d8c4025Svi 		    sctp->sctp_lport));
2517c478bd9Sstevel@tonic-gate 	case SCTP_BINDX_REM_ADDR:
2527c478bd9Sstevel@tonic-gate 		return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
2537c478bd9Sstevel@tonic-gate 	default:
2547c478bd9Sstevel@tonic-gate 		return (EINVAL);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Add a list of addresses to a sctp_t.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate int
2627c478bd9Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
2631d8c4025Svi     boolean_t caller_hold_lock, in_port_t port)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	int		err = 0;
2667c478bd9Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
267f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
2707c478bd9Sstevel@tonic-gate 		RUN_SCTP(sctp);
2717c478bd9Sstevel@tonic-gate 
272b34b8d1aSkcpoon 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
273b34b8d1aSkcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
2747c478bd9Sstevel@tonic-gate 		if (!caller_hold_lock)
2757c478bd9Sstevel@tonic-gate 			WAKE_SCTP(sctp);
2767c478bd9Sstevel@tonic-gate 		return (EINVAL);
2777c478bd9Sstevel@tonic-gate 	}
278df19b344Svi 
279df19b344Svi 	if (sctp->sctp_state > SCTPS_LISTEN) {
280df19b344Svi 		/*
281df19b344Svi 		 * Let's do some checking here rather than undoing the
282df19b344Svi 		 * add later (for these reasons).
283df19b344Svi 		 */
284f4b3ec61Sdh 		if (!sctps->sctps_addip_enabled ||
285f4b3ec61Sdh 		    !sctp->sctp_understands_asconf ||
286df19b344Svi 		    !sctp->sctp_understands_addip) {
287df19b344Svi 			if (!caller_hold_lock)
288df19b344Svi 				WAKE_SCTP(sctp);
289df19b344Svi 			return (EINVAL);
290df19b344Svi 		}
2917c478bd9Sstevel@tonic-gate 		do_asconf = B_TRUE;
292df19b344Svi 	}
2931d8c4025Svi 	/*
2941d8c4025Svi 	 * On a clustered node, for an inaddr_any bind, we will pass the list
2951d8c4025Svi 	 * of all the addresses in the global list, minus any address on the
2961d8c4025Svi 	 * loopback interface, and expect the clustering susbsystem to give us
2971d8c4025Svi 	 * the correct list for the 'port'. For explicit binds we give the
2981d8c4025Svi 	 * list of addresses  and the clustering module validates it for the
2991d8c4025Svi 	 * 'port'.
3001d8c4025Svi 	 *
3011d8c4025Svi 	 * On a non-clustered node, cl_sctp_check_addrs will be NULL and
3021d8c4025Svi 	 * we proceed as usual.
3031d8c4025Svi 	 */
3041d8c4025Svi 	if (cl_sctp_check_addrs != NULL) {
3051d8c4025Svi 		uchar_t		*addrlist = NULL;
3061d8c4025Svi 		size_t		size = 0;
3071d8c4025Svi 		int		unspec = 0;
3081d8c4025Svi 		boolean_t	do_listen;
3091d8c4025Svi 		uchar_t		*llist = NULL;
3101d8c4025Svi 		size_t		lsize = 0;
3111d8c4025Svi 
3121d8c4025Svi 		/*
3131d8c4025Svi 		 * If we are adding addresses after listening, but before
3141d8c4025Svi 		 * an association is established, we need to update the
3151d8c4025Svi 		 * clustering module with this info.
3161d8c4025Svi 		 */
3171d8c4025Svi 		do_listen = !do_asconf && sctp->sctp_state > SCTPS_BOUND &&
3181d8c4025Svi 		    cl_sctp_listen != NULL;
3191d8c4025Svi 
3201d8c4025Svi 		err = sctp_get_addrlist(sctp, addrs, &addrcnt, &addrlist,
3211d8c4025Svi 		    &unspec, &size);
3221d8c4025Svi 		if (err != 0) {
3231d8c4025Svi 			ASSERT(addrlist == NULL);
3241d8c4025Svi 			ASSERT(addrcnt == 0);
3251d8c4025Svi 			ASSERT(size == 0);
3261d8c4025Svi 			if (!caller_hold_lock)
3271d8c4025Svi 				WAKE_SCTP(sctp);
328f4b3ec61Sdh 			SCTP_KSTAT(sctps, sctp_cl_check_addrs);
3291d8c4025Svi 			return (err);
3301d8c4025Svi 		}
3311d8c4025Svi 		ASSERT(addrlist != NULL);
3321d8c4025Svi 		(*cl_sctp_check_addrs)(sctp->sctp_family, port, &addrlist,
3331d8c4025Svi 		    size, &addrcnt, unspec == 1);
3341d8c4025Svi 		if (addrcnt == 0) {
3351d8c4025Svi 			/* We free the list */
3361d8c4025Svi 			kmem_free(addrlist, size);
3371d8c4025Svi 			if (!caller_hold_lock)
3381d8c4025Svi 				WAKE_SCTP(sctp);
3391d8c4025Svi 			return (EINVAL);
3401d8c4025Svi 		}
3411d8c4025Svi 		if (do_listen) {
3421d8c4025Svi 			lsize = sizeof (in6_addr_t) * addrcnt;
3431d8c4025Svi 			llist = kmem_alloc(lsize, KM_SLEEP);
3441d8c4025Svi 		}
3451d8c4025Svi 		err = sctp_valid_addr_list(sctp, addrlist, addrcnt, llist,
3461d8c4025Svi 		    lsize);
3471d8c4025Svi 		if (err == 0 && do_listen) {
3481d8c4025Svi 			(*cl_sctp_listen)(sctp->sctp_family, llist,
3491d8c4025Svi 			    addrcnt, sctp->sctp_lport);
3501d8c4025Svi 			/* list will be freed by the clustering module */
3511d8c4025Svi 		} else if (err != 0 && llist != NULL) {
3521d8c4025Svi 			kmem_free(llist, lsize);
3531d8c4025Svi 		}
3541d8c4025Svi 		/* free the list we allocated */
3551d8c4025Svi 		kmem_free(addrlist, size);
3561d8c4025Svi 	} else {
3571d8c4025Svi 		err = sctp_valid_addr_list(sctp, addrs, addrcnt, NULL, 0);
3581d8c4025Svi 	}
3597c478bd9Sstevel@tonic-gate 	if (err != 0) {
3607c478bd9Sstevel@tonic-gate 		if (!caller_hold_lock)
3617c478bd9Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3627c478bd9Sstevel@tonic-gate 		return (err);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 	/* Need to send  ASCONF messages */
3657c478bd9Sstevel@tonic-gate 	if (do_asconf) {
3667c478bd9Sstevel@tonic-gate 		err = sctp_add_ip(sctp, addrs, addrcnt);
3677c478bd9Sstevel@tonic-gate 		if (err != 0) {
3687c478bd9Sstevel@tonic-gate 			sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
3697c478bd9Sstevel@tonic-gate 			if (!caller_hold_lock)
3707c478bd9Sstevel@tonic-gate 				WAKE_SCTP(sctp);
3717c478bd9Sstevel@tonic-gate 			return (err);
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
3757c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3767c478bd9Sstevel@tonic-gate 	if (do_asconf)
3777c478bd9Sstevel@tonic-gate 		sctp_process_sendq(sctp);
3787c478bd9Sstevel@tonic-gate 	return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Remove one or more addresses bound to the sctp_t.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate int
3857c478bd9Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
3867c478bd9Sstevel@tonic-gate     boolean_t caller_hold_lock)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	int		error = 0;
3897c478bd9Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
3901d8c4025Svi 	uchar_t		*ulist = NULL;
3911d8c4025Svi 	size_t		usize = 0;
392f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
3957c478bd9Sstevel@tonic-gate 		RUN_SCTP(sctp);
3967c478bd9Sstevel@tonic-gate 
397b34b8d1aSkcpoon 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
398b34b8d1aSkcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
3997c478bd9Sstevel@tonic-gate 		if (!caller_hold_lock)
4007c478bd9Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4017c478bd9Sstevel@tonic-gate 		return (EINVAL);
4027c478bd9Sstevel@tonic-gate 	}
403df19b344Svi 	/*
404df19b344Svi 	 * Fail the remove if we are beyond listen, but can't send this
405df19b344Svi 	 * to the peer.
406df19b344Svi 	 */
407df19b344Svi 	if (sctp->sctp_state > SCTPS_LISTEN) {
408f4b3ec61Sdh 		if (!sctps->sctps_addip_enabled ||
409f4b3ec61Sdh 		    !sctp->sctp_understands_asconf ||
410df19b344Svi 		    !sctp->sctp_understands_addip) {
411df19b344Svi 			if (!caller_hold_lock)
412df19b344Svi 				WAKE_SCTP(sctp);
413df19b344Svi 			return (EINVAL);
414df19b344Svi 		}
4157c478bd9Sstevel@tonic-gate 		do_asconf = B_TRUE;
416df19b344Svi 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/* Can't delete the last address nor all of the addresses */
4197c478bd9Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
4207c478bd9Sstevel@tonic-gate 		if (!caller_hold_lock)
4217c478bd9Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4227c478bd9Sstevel@tonic-gate 		return (EINVAL);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4251d8c4025Svi 	if (cl_sctp_unlisten != NULL && !do_asconf &&
4261d8c4025Svi 	    sctp->sctp_state > SCTPS_BOUND) {
4271d8c4025Svi 		usize = sizeof (in6_addr_t) * addrcnt;
4281d8c4025Svi 		ulist = kmem_alloc(usize, KM_SLEEP);
4291d8c4025Svi 	}
4301d8c4025Svi 
4311d8c4025Svi 	error = sctp_del_ip(sctp, addrs, addrcnt, ulist, usize);
4321d8c4025Svi 	if (error != 0) {
4331d8c4025Svi 		if (ulist != NULL)
4341d8c4025Svi 			kmem_free(ulist, usize);
4351d8c4025Svi 		if (!caller_hold_lock)
4361d8c4025Svi 			WAKE_SCTP(sctp);
4371d8c4025Svi 		return (error);
4381d8c4025Svi 	}
4391d8c4025Svi 	/* ulist will be non-NULL only if cl_sctp_unlisten is non-NULL */
4401d8c4025Svi 	if (ulist != NULL) {
4411d8c4025Svi 		ASSERT(cl_sctp_unlisten != NULL);
4421d8c4025Svi 		(*cl_sctp_unlisten)(sctp->sctp_family, ulist, addrcnt,
4431d8c4025Svi 		    sctp->sctp_lport);
4441d8c4025Svi 		/* ulist will be freed by the clustering module */
4451d8c4025Svi 	}
4467c478bd9Sstevel@tonic-gate 	if (!caller_hold_lock)
4477c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4481d8c4025Svi 	if (do_asconf)
4497c478bd9Sstevel@tonic-gate 		sctp_process_sendq(sctp);
4507c478bd9Sstevel@tonic-gate 	return (error);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
45445916cd2Sjpk  * Returns 0 for success, errno value otherwise.
4557c478bd9Sstevel@tonic-gate  *
45645916cd2Sjpk  * If the "bind_to_req_port_only" parameter is set and the requested port
45745916cd2Sjpk  * number is available, then set allocated_port to it.  If not available,
45845916cd2Sjpk  * return an error.
4597c478bd9Sstevel@tonic-gate  *
46045916cd2Sjpk  * If the "bind_to_req_port_only" parameter is not set and the requested port
46145916cd2Sjpk  * number is available, then set allocated_port to it.  If not available,
46245916cd2Sjpk  * find the first anonymous port we can and set allocated_port to that.  If no
46345916cd2Sjpk  * anonymous ports are available, return an error.
46445916cd2Sjpk  *
46545916cd2Sjpk  * In either case, when succeeding, update the sctp_t to record the port number
4667c478bd9Sstevel@tonic-gate  * and insert it in the bind hash table.
4677c478bd9Sstevel@tonic-gate  */
46845916cd2Sjpk int
46945916cd2Sjpk sctp_bindi(sctp_t *sctp, in_port_t port, boolean_t bind_to_req_port_only,
47045916cd2Sjpk     int user_specified, in_port_t *allocated_port)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	/* number of times we have run around the loop */
4737c478bd9Sstevel@tonic-gate 	int count = 0;
4747c478bd9Sstevel@tonic-gate 	/* maximum number of times to run around the loop */
4757c478bd9Sstevel@tonic-gate 	int loopmax;
4767c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = sctp->sctp_zoneid;
47745916cd2Sjpk 	zone_t *zone = crgetzone(sctp->sctp_credp);
478f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Lookup for free addresses is done in a loop and "loopmax"
4827c478bd9Sstevel@tonic-gate 	 * influences how long we spin in the loop
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	if (bind_to_req_port_only) {
4857c478bd9Sstevel@tonic-gate 		/*
4867c478bd9Sstevel@tonic-gate 		 * If the requested port is busy, don't bother to look
4877c478bd9Sstevel@tonic-gate 		 * for a new one. Setting loop maximum count to 1 has
4887c478bd9Sstevel@tonic-gate 		 * that effect.
4897c478bd9Sstevel@tonic-gate 		 */
4907c478bd9Sstevel@tonic-gate 		loopmax = 1;
4917c478bd9Sstevel@tonic-gate 	} else {
4927c478bd9Sstevel@tonic-gate 		/*
4937c478bd9Sstevel@tonic-gate 		 * If the requested port is busy, look for a free one
4947c478bd9Sstevel@tonic-gate 		 * in the anonymous port range.
4957c478bd9Sstevel@tonic-gate 		 * Set loopmax appropriately so that one does not look
4967c478bd9Sstevel@tonic-gate 		 * forever in the case all of the anonymous ports are in use.
4977c478bd9Sstevel@tonic-gate 		 */
498f4b3ec61Sdh 		loopmax = (sctps->sctps_largest_anon_port -
499f4b3ec61Sdh 		    sctps->sctps_smallest_anon_port + 1);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	do {
5027c478bd9Sstevel@tonic-gate 		uint16_t	lport;
5037c478bd9Sstevel@tonic-gate 		sctp_tf_t	*tbf;
5047c478bd9Sstevel@tonic-gate 		sctp_t		*lsctp;
5057c478bd9Sstevel@tonic-gate 		int		addrcmp;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		lport = htons(port);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		/*
5107c478bd9Sstevel@tonic-gate 		 * Ensure that the sctp_t is not currently in the bind hash.
5117c478bd9Sstevel@tonic-gate 		 * Hold the lock on the hash bucket to ensure that
5127c478bd9Sstevel@tonic-gate 		 * the duplicate check plus the insertion is an atomic
5137c478bd9Sstevel@tonic-gate 		 * operation.
5147c478bd9Sstevel@tonic-gate 		 *
5157c478bd9Sstevel@tonic-gate 		 * This function does an inline lookup on the bind hash list
5167c478bd9Sstevel@tonic-gate 		 * Make sure that we access only members of sctp_t
5177c478bd9Sstevel@tonic-gate 		 * and that we don't look at sctp_sctp, since we are not
5187c478bd9Sstevel@tonic-gate 		 * doing a SCTPB_REFHOLD. For more details please see the notes
5197c478bd9Sstevel@tonic-gate 		 * in sctp_compress()
5207c478bd9Sstevel@tonic-gate 		 */
5217c478bd9Sstevel@tonic-gate 		sctp_bind_hash_remove(sctp);
522f4b3ec61Sdh 		tbf = &sctps->sctps_bind_fanout[SCTP_BIND_HASH(port)];
5237c478bd9Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
5247c478bd9Sstevel@tonic-gate 		for (lsctp = tbf->tf_sctp; lsctp != NULL;
5257c478bd9Sstevel@tonic-gate 		    lsctp = lsctp->sctp_bind_hash) {
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			if (lport != lsctp->sctp_lport ||
5287c478bd9Sstevel@tonic-gate 			    lsctp->sctp_state < SCTPS_BOUND)
5297c478bd9Sstevel@tonic-gate 				continue;
5307c478bd9Sstevel@tonic-gate 
53145916cd2Sjpk 			/*
53245916cd2Sjpk 			 * On a labeled system, we must treat bindings to ports
53345916cd2Sjpk 			 * on shared IP addresses by sockets with MAC exemption
53445916cd2Sjpk 			 * privilege as being in all zones, as there's
53545916cd2Sjpk 			 * otherwise no way to identify the right receiver.
53645916cd2Sjpk 			 */
53745916cd2Sjpk 			if (lsctp->sctp_zoneid != zoneid &&
53845916cd2Sjpk 			    !lsctp->sctp_mac_exempt && !sctp->sctp_mac_exempt)
53945916cd2Sjpk 				continue;
54045916cd2Sjpk 
5417c478bd9Sstevel@tonic-gate 			addrcmp = sctp_compare_saddrs(sctp, lsctp);
5427c478bd9Sstevel@tonic-gate 			if (addrcmp != SCTP_ADDR_DISJOINT) {
5437c478bd9Sstevel@tonic-gate 				if (!sctp->sctp_reuseaddr) {
5447c478bd9Sstevel@tonic-gate 					/* in use */
5457c478bd9Sstevel@tonic-gate 					break;
5467c478bd9Sstevel@tonic-gate 				} else if (lsctp->sctp_state == SCTPS_BOUND ||
5477c478bd9Sstevel@tonic-gate 				    lsctp->sctp_state == SCTPS_LISTEN) {
5487c478bd9Sstevel@tonic-gate 					/*
5497c478bd9Sstevel@tonic-gate 					 * socket option SO_REUSEADDR is set
5507c478bd9Sstevel@tonic-gate 					 * on the binding sctp_t.
5517c478bd9Sstevel@tonic-gate 					 *
5527c478bd9Sstevel@tonic-gate 					 * We have found a match of IP source
5537c478bd9Sstevel@tonic-gate 					 * address and source port, which is
5547c478bd9Sstevel@tonic-gate 					 * refused regardless of the
5557c478bd9Sstevel@tonic-gate 					 * SO_REUSEADDR setting, so we break.
5567c478bd9Sstevel@tonic-gate 					 */
5577c478bd9Sstevel@tonic-gate 					break;
5587c478bd9Sstevel@tonic-gate 				}
5597c478bd9Sstevel@tonic-gate 			}
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		if (lsctp != NULL) {
5627c478bd9Sstevel@tonic-gate 			/* The port number is busy */
5637c478bd9Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
5647c478bd9Sstevel@tonic-gate 		} else {
56545916cd2Sjpk 			conn_t *connp = sctp->sctp_connp;
56645916cd2Sjpk 
56745916cd2Sjpk 			if (is_system_labeled()) {
56845916cd2Sjpk 				mlp_type_t addrtype, mlptype;
56945916cd2Sjpk 
57045916cd2Sjpk 				/*
57145916cd2Sjpk 				 * On a labeled system we must check the type
57245916cd2Sjpk 				 * of the binding requested by the user (either
57345916cd2Sjpk 				 * MLP or SLP on shared and private addresses),
57445916cd2Sjpk 				 * and that the user's requested binding
57545916cd2Sjpk 				 * is permitted.
57645916cd2Sjpk 				 */
57745916cd2Sjpk 				addrtype = tsol_mlp_addr_type(zone->zone_id,
57845916cd2Sjpk 				    sctp->sctp_ipversion,
57945916cd2Sjpk 				    sctp->sctp_ipversion == IPV4_VERSION ?
58045916cd2Sjpk 				    (void *)&sctp->sctp_ipha->ipha_src :
581f4b3ec61Sdh 				    (void *)&sctp->sctp_ip6h->ip6_src,
582f4b3ec61Sdh 				    sctps->sctps_netstack->netstack_ip);
58345916cd2Sjpk 
58445916cd2Sjpk 				/*
58545916cd2Sjpk 				 * tsol_mlp_addr_type returns the possibilities
58645916cd2Sjpk 				 * for the selected address.  Since all local
58745916cd2Sjpk 				 * addresses are either private or shared, the
58845916cd2Sjpk 				 * return value mlptSingle means "local address
58945916cd2Sjpk 				 * not valid (interface not present)."
59045916cd2Sjpk 				 */
59145916cd2Sjpk 				if (addrtype == mlptSingle) {
59245916cd2Sjpk 					mutex_exit(&tbf->tf_lock);
59345916cd2Sjpk 					return (EADDRNOTAVAIL);
59445916cd2Sjpk 				}
59545916cd2Sjpk 				mlptype = tsol_mlp_port_type(zone, IPPROTO_SCTP,
59645916cd2Sjpk 				    port, addrtype);
59745916cd2Sjpk 				if (mlptype != mlptSingle) {
59845916cd2Sjpk 					if (secpolicy_net_bindmlp(connp->
59945916cd2Sjpk 					    conn_cred) != 0) {
60045916cd2Sjpk 						mutex_exit(&tbf->tf_lock);
60145916cd2Sjpk 						return (EACCES);
60245916cd2Sjpk 					}
60345916cd2Sjpk 					/*
60445916cd2Sjpk 					 * If we're binding a shared MLP, then
60545916cd2Sjpk 					 * make sure that this zone is the one
60645916cd2Sjpk 					 * that owns that MLP.  Shared MLPs can
60745916cd2Sjpk 					 * be owned by at most one zone.
608f4b3ec61Sdh 					 *
609f4b3ec61Sdh 					 * No need to handle exclusive-stack
610f4b3ec61Sdh 					 * zones since ALL_ZONES only applies
611f4b3ec61Sdh 					 * to the shared stack.
61245916cd2Sjpk 					 */
61345916cd2Sjpk 
61445916cd2Sjpk 					if (mlptype == mlptShared &&
61545916cd2Sjpk 					    addrtype == mlptShared &&
61645916cd2Sjpk 					    connp->conn_zoneid !=
61745916cd2Sjpk 					    tsol_mlp_findzone(IPPROTO_SCTP,
61845916cd2Sjpk 					    lport)) {
61945916cd2Sjpk 						mutex_exit(&tbf->tf_lock);
62045916cd2Sjpk 						return (EACCES);
62145916cd2Sjpk 					}
62245916cd2Sjpk 					connp->conn_mlp_type = mlptype;
62345916cd2Sjpk 				}
62445916cd2Sjpk 			}
6257c478bd9Sstevel@tonic-gate 			/*
6267c478bd9Sstevel@tonic-gate 			 * This port is ours. Insert in fanout and mark as
6277c478bd9Sstevel@tonic-gate 			 * bound to prevent others from getting the port
6287c478bd9Sstevel@tonic-gate 			 * number.
6297c478bd9Sstevel@tonic-gate 			 */
6307c478bd9Sstevel@tonic-gate 			sctp->sctp_state = SCTPS_BOUND;
6317c478bd9Sstevel@tonic-gate 			sctp->sctp_lport = lport;
63245916cd2Sjpk 			sctp->sctp_sctph->sh_sport = lport;
6337c478bd9Sstevel@tonic-gate 
634f4b3ec61Sdh 			ASSERT(&sctps->sctps_bind_fanout[
635b34b8d1aSkcpoon 			    SCTP_BIND_HASH(port)] == tbf);
6367c478bd9Sstevel@tonic-gate 			sctp_bind_hash_insert(tbf, sctp, 1);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 			/*
6417c478bd9Sstevel@tonic-gate 			 * We don't want sctp_next_port_to_try to "inherit"
6427c478bd9Sstevel@tonic-gate 			 * a port number supplied by the user in a bind.
64345916cd2Sjpk 			 *
6447c478bd9Sstevel@tonic-gate 			 * This is the only place where sctp_next_port_to_try
6457c478bd9Sstevel@tonic-gate 			 * is updated. After the update, it may or may not
6467c478bd9Sstevel@tonic-gate 			 * be in the valid range.
6477c478bd9Sstevel@tonic-gate 			 */
64845916cd2Sjpk 			if (user_specified == 0)
649f4b3ec61Sdh 				sctps->sctps_next_port_to_try = port + 1;
65045916cd2Sjpk 
65145916cd2Sjpk 			*allocated_port = port;
65245916cd2Sjpk 
65345916cd2Sjpk 			return (0);
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		if ((count == 0) && (user_specified)) {
6577c478bd9Sstevel@tonic-gate 			/*
6587c478bd9Sstevel@tonic-gate 			 * We may have to return an anonymous port. So
6597c478bd9Sstevel@tonic-gate 			 * get one to start with.
6607c478bd9Sstevel@tonic-gate 			 */
661f4b3ec61Sdh 			port = sctp_update_next_port(
662f4b3ec61Sdh 			    sctps->sctps_next_port_to_try,
663f4b3ec61Sdh 			    zone, sctps);
6647c478bd9Sstevel@tonic-gate 			user_specified = 0;
6657c478bd9Sstevel@tonic-gate 		} else {
666f4b3ec61Sdh 			port = sctp_update_next_port(port + 1, zone, sctps);
6677c478bd9Sstevel@tonic-gate 		}
66845916cd2Sjpk 		if (port == 0)
66945916cd2Sjpk 			break;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 		/*
6727c478bd9Sstevel@tonic-gate 		 * Don't let this loop run forever in the case where
6737c478bd9Sstevel@tonic-gate 		 * all of the anonymous ports are in use.
6747c478bd9Sstevel@tonic-gate 		 */
6757c478bd9Sstevel@tonic-gate 	} while (++count < loopmax);
67645916cd2Sjpk 
67745916cd2Sjpk 	return (bind_to_req_port_only ? EADDRINUSE : EADDRNOTAVAIL);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate  * Don't let port fall into the privileged range.
6827c478bd9Sstevel@tonic-gate  * Since the extra privileged ports can be arbitrary we also
6837c478bd9Sstevel@tonic-gate  * ensure that we exclude those from consideration.
6847c478bd9Sstevel@tonic-gate  * sctp_g_epriv_ports is not sorted thus we loop over it until
6857c478bd9Sstevel@tonic-gate  * there are no changes.
6867c478bd9Sstevel@tonic-gate  *
6877c478bd9Sstevel@tonic-gate  * Note: No locks are held when inspecting sctp_g_*epriv_ports
6887c478bd9Sstevel@tonic-gate  * but instead the code relies on:
6897c478bd9Sstevel@tonic-gate  * - the fact that the address of the array and its size never changes
6907c478bd9Sstevel@tonic-gate  * - the atomic assignment of the elements of the array
6917c478bd9Sstevel@tonic-gate  */
6927c478bd9Sstevel@tonic-gate in_port_t
693f4b3ec61Sdh sctp_update_next_port(in_port_t port, zone_t *zone, sctp_stack_t *sctps)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	int i;
69645916cd2Sjpk 	boolean_t restart = B_FALSE;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate retry:
699f4b3ec61Sdh 	if (port < sctps->sctps_smallest_anon_port)
700f4b3ec61Sdh 		port = sctps->sctps_smallest_anon_port;
70145916cd2Sjpk 
702f4b3ec61Sdh 	if (port > sctps->sctps_largest_anon_port) {
70345916cd2Sjpk 		if (restart)
70445916cd2Sjpk 			return (0);
70545916cd2Sjpk 		restart = B_TRUE;
706f4b3ec61Sdh 		port = sctps->sctps_smallest_anon_port;
70745916cd2Sjpk 	}
7087c478bd9Sstevel@tonic-gate 
709f4b3ec61Sdh 	if (port < sctps->sctps_smallest_nonpriv_port)
710f4b3ec61Sdh 		port = sctps->sctps_smallest_nonpriv_port;
7117c478bd9Sstevel@tonic-gate 
712f4b3ec61Sdh 	for (i = 0; i < sctps->sctps_g_num_epriv_ports; i++) {
713f4b3ec61Sdh 		if (port == sctps->sctps_g_epriv_ports[i]) {
7147c478bd9Sstevel@tonic-gate 			port++;
7157c478bd9Sstevel@tonic-gate 			/*
7167c478bd9Sstevel@tonic-gate 			 * Make sure whether the port is in the
7177c478bd9Sstevel@tonic-gate 			 * valid range.
7187c478bd9Sstevel@tonic-gate 			 *
7197c478bd9Sstevel@tonic-gate 			 * XXX Note that if sctp_g_epriv_ports contains
7207c478bd9Sstevel@tonic-gate 			 * all the anonymous ports this will be an
7217c478bd9Sstevel@tonic-gate 			 * infinite loop.
7227c478bd9Sstevel@tonic-gate 			 */
7237c478bd9Sstevel@tonic-gate 			goto retry;
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
72645916cd2Sjpk 
72745916cd2Sjpk 	if (is_system_labeled() &&
72845916cd2Sjpk 	    (i = tsol_next_port(zone, port, IPPROTO_SCTP, B_TRUE)) != 0) {
72945916cd2Sjpk 		port = i;
73045916cd2Sjpk 		goto retry;
73145916cd2Sjpk 	}
73245916cd2Sjpk 
7337c478bd9Sstevel@tonic-gate 	return (port);
7347c478bd9Sstevel@tonic-gate }
735