xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_bind.c (revision 252:8fc692a5cbf7)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*252Svi117747  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/cmn_err.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
350Sstevel@tonic-gate #include <sys/tihdr.h>
360Sstevel@tonic-gate #include <sys/stropts.h>
370Sstevel@tonic-gate #include <sys/socket.h>
380Sstevel@tonic-gate #include <sys/random.h>
390Sstevel@tonic-gate #include <sys/policy.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <netinet/ip6.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <inet/common.h>
450Sstevel@tonic-gate #include <inet/ip.h>
460Sstevel@tonic-gate #include <inet/ip6.h>
470Sstevel@tonic-gate #include <inet/ipclassifier.h>
480Sstevel@tonic-gate #include "sctp_impl.h"
490Sstevel@tonic-gate #include "sctp_asconf.h"
500Sstevel@tonic-gate #include "sctp_addr.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate uint_t	sctp_next_port_to_try;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Returns 0 on success, EACCES on permission failure.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static int
580Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	/*
610Sstevel@tonic-gate 	 * Get a valid port (within the anonymous range and should not
620Sstevel@tonic-gate 	 * be a privileged one) to use if the user has not given a port.
630Sstevel@tonic-gate 	 * If multiple threads are here, they may all start with
640Sstevel@tonic-gate 	 * with the same initial port. But, it should be fine as long as
650Sstevel@tonic-gate 	 * sctp_bindi will ensure that no two threads will be assigned
660Sstevel@tonic-gate 	 * the same port.
670Sstevel@tonic-gate 	 */
680Sstevel@tonic-gate 	if (*requested_port == 0) {
690Sstevel@tonic-gate 		*requested_port = sctp_update_next_port(sctp_next_port_to_try);
700Sstevel@tonic-gate 		*user_specified = 0;
710Sstevel@tonic-gate 	} else {
720Sstevel@tonic-gate 		int i;
730Sstevel@tonic-gate 		boolean_t priv = B_FALSE;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 		/*
760Sstevel@tonic-gate 		 * If the requested_port is in the well-known privileged range,
770Sstevel@tonic-gate 		 * verify that the stream was opened by a privileged user.
780Sstevel@tonic-gate 		 * Note: No locks are held when inspecting sctp_g_*epriv_ports
790Sstevel@tonic-gate 		 * but instead the code relies on:
800Sstevel@tonic-gate 		 * - the fact that the address of the array and its size never
810Sstevel@tonic-gate 		 *   changes
820Sstevel@tonic-gate 		 * - the atomic assignment of the elements of the array
830Sstevel@tonic-gate 		 */
840Sstevel@tonic-gate 		if (*requested_port < sctp_smallest_nonpriv_port) {
850Sstevel@tonic-gate 			priv = B_TRUE;
860Sstevel@tonic-gate 		} else {
870Sstevel@tonic-gate 			for (i = 0; i < sctp_g_num_epriv_ports; i++) {
880Sstevel@tonic-gate 				if (*requested_port == sctp_g_epriv_ports[i]) {
890Sstevel@tonic-gate 					priv = B_TRUE;
900Sstevel@tonic-gate 					break;
910Sstevel@tonic-gate 				}
920Sstevel@tonic-gate 			}
930Sstevel@tonic-gate 		}
940Sstevel@tonic-gate 		if (priv) {
950Sstevel@tonic-gate 			/*
960Sstevel@tonic-gate 			 * sctp_bind() should take a cred_t argument so that
970Sstevel@tonic-gate 			 * we can use it here.
980Sstevel@tonic-gate 			 */
990Sstevel@tonic-gate 			if (secpolicy_net_privaddr(sctp->sctp_credp,
1000Sstevel@tonic-gate 			    *requested_port) != 0) {
1010Sstevel@tonic-gate 				dprint(1,
1020Sstevel@tonic-gate 				    ("sctp_bind(x): no prive for port %d",
1030Sstevel@tonic-gate 				    *requested_port));
1040Sstevel@tonic-gate 				return (TACCES);
1050Sstevel@tonic-gate 			}
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 		*user_specified = 1;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	return (0);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate int
1140Sstevel@tonic-gate sctp_listen(sctp_t *sctp)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	sctp_tf_t	*tf;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	RUN_SCTP(sctp);
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 * TCP handles listen() increasing the backlog, need to check
1210Sstevel@tonic-gate 	 * if it should be handled here too - VENU.
1220Sstevel@tonic-gate 	 */
1230Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_BOUND) {
1240Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1250Sstevel@tonic-gate 		return (EINVAL);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/* Do an anonymous bind for unbound socket doing listen(). */
1290Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0) {
1300Sstevel@tonic-gate 		struct sockaddr_storage ss;
1310Sstevel@tonic-gate 		int ret;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		bzero(&ss, sizeof (ss));
1340Sstevel@tonic-gate 		ss.ss_family = sctp->sctp_family;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1370Sstevel@tonic-gate 		if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
1380Sstevel@tonic-gate 			sizeof (ss))) != 0)
1390Sstevel@tonic-gate 			return (ret);
1400Sstevel@tonic-gate 		RUN_SCTP(sctp)
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_LISTEN;
1440Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
1450Sstevel@tonic-gate 	sctp->sctp_last_secret_update = lbolt64;
1460Sstevel@tonic-gate 	bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
1470Sstevel@tonic-gate 	tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))];
1480Sstevel@tonic-gate 	sctp_listen_hash_insert(tf, sctp);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	WAKE_SCTP(sctp);
1510Sstevel@tonic-gate 	return (0);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * Bind the sctp_t to a sockaddr, which includes an address and other
1560Sstevel@tonic-gate  * information, such as port or flowinfo.
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate int
1590Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	int		user_specified;
1620Sstevel@tonic-gate 	boolean_t	bind_to_req_port_only;
1630Sstevel@tonic-gate 	in_port_t	requested_port;
1640Sstevel@tonic-gate 	in_port_t	allocated_port;
1650Sstevel@tonic-gate 	int		err = 0;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	ASSERT(sctp != NULL);
1680Sstevel@tonic-gate 	ASSERT(sa);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	RUN_SCTP(sctp);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_BOUND) {
1730Sstevel@tonic-gate 		err = EINVAL;
1740Sstevel@tonic-gate 		goto done;
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	switch (sa->sa_family) {
1780Sstevel@tonic-gate 	case AF_INET:
1790Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in) ||
1800Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
1810Sstevel@tonic-gate 			err = EINVAL;
1820Sstevel@tonic-gate 			goto done;
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port);
1850Sstevel@tonic-gate 		break;
1860Sstevel@tonic-gate 	case AF_INET6:
1870Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in6) ||
1880Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET) {
1890Sstevel@tonic-gate 			err = EINVAL;
1900Sstevel@tonic-gate 			goto done;
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
1930Sstevel@tonic-gate 		/* Set the flowinfo. */
1940Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_vcf =
1950Sstevel@tonic-gate 		    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
1960Sstevel@tonic-gate 		    (((struct sockaddr_in6 *)sa)->sin6_flowinfo &
1970Sstevel@tonic-gate 		    ~IPV6_VERS_AND_FLOW_MASK);
1980Sstevel@tonic-gate 		break;
1990Sstevel@tonic-gate 	default:
2000Sstevel@tonic-gate 		err = EAFNOSUPPORT;
2010Sstevel@tonic-gate 		goto done;
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 	bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (sctp_select_port(sctp, &requested_port, &user_specified) != 0) {
2060Sstevel@tonic-gate 		err = EPERM;
2070Sstevel@tonic-gate 		goto done;
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE)) != 0)
2110Sstevel@tonic-gate 		goto done;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	allocated_port = sctp_bindi(sctp, requested_port,
2140Sstevel@tonic-gate 	    bind_to_req_port_only, user_specified);
2150Sstevel@tonic-gate 	if (allocated_port == 0) {
2160Sstevel@tonic-gate 		sctp_free_saddrs(sctp);
2170Sstevel@tonic-gate 		if (bind_to_req_port_only) {
2180Sstevel@tonic-gate 			err = EADDRINUSE;
2190Sstevel@tonic-gate 			goto done;
2200Sstevel@tonic-gate 		} else {
2210Sstevel@tonic-gate 			err = EADDRNOTAVAIL;
2220Sstevel@tonic-gate 			goto done;
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	ASSERT(sctp->sctp_state == SCTPS_BOUND);
2260Sstevel@tonic-gate done:
2270Sstevel@tonic-gate 	WAKE_SCTP(sctp);
2280Sstevel@tonic-gate 	return (err);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * Perform bind/unbind operation of a list of addresses on a sctp_t
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate int
2350Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2380Sstevel@tonic-gate 	ASSERT(addrs != NULL);
2390Sstevel@tonic-gate 	ASSERT(addrcnt > 0);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	switch (bindop) {
2420Sstevel@tonic-gate 	case SCTP_BINDX_ADD_ADDR:
2430Sstevel@tonic-gate 		return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE));
2440Sstevel@tonic-gate 	case SCTP_BINDX_REM_ADDR:
2450Sstevel@tonic-gate 		return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
2460Sstevel@tonic-gate 	default:
2470Sstevel@tonic-gate 		return (EINVAL);
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * Add a list of addresses to a sctp_t.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate int
2550Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
2560Sstevel@tonic-gate     boolean_t caller_hold_lock)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	int		err = 0;
2590Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (!caller_hold_lock)
2620Sstevel@tonic-gate 		RUN_SCTP(sctp);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
2650Sstevel@tonic-gate 		if (!caller_hold_lock)
2660Sstevel@tonic-gate 			WAKE_SCTP(sctp);
2670Sstevel@tonic-gate 		return (EINVAL);
2680Sstevel@tonic-gate 	}
269*252Svi117747 
270*252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
271*252Svi117747 		/*
272*252Svi117747 		 * Let's do some checking here rather than undoing the
273*252Svi117747 		 * add later (for these reasons).
274*252Svi117747 		 */
275*252Svi117747 		if (!sctp_addip_enabled || !sctp->sctp_understands_asconf ||
276*252Svi117747 		    !sctp->sctp_understands_addip) {
277*252Svi117747 			if (!caller_hold_lock)
278*252Svi117747 				WAKE_SCTP(sctp);
279*252Svi117747 			return (EINVAL);
280*252Svi117747 		}
2810Sstevel@tonic-gate 		do_asconf = B_TRUE;
282*252Svi117747 	}
2830Sstevel@tonic-gate 	err = sctp_valid_addr_list(sctp, addrs, addrcnt);
2840Sstevel@tonic-gate 	if (err != 0) {
2850Sstevel@tonic-gate 		if (!caller_hold_lock)
2860Sstevel@tonic-gate 			WAKE_SCTP(sctp);
2870Sstevel@tonic-gate 		return (err);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/* Need to send  ASCONF messages */
2910Sstevel@tonic-gate 	if (do_asconf) {
2920Sstevel@tonic-gate 		err = sctp_add_ip(sctp, addrs, addrcnt);
2930Sstevel@tonic-gate 		if (err != 0) {
2940Sstevel@tonic-gate 			sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
2950Sstevel@tonic-gate 			if (!caller_hold_lock)
2960Sstevel@tonic-gate 				WAKE_SCTP(sctp);
2970Sstevel@tonic-gate 			return (err);
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 	if (!caller_hold_lock)
3010Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3020Sstevel@tonic-gate 	if (do_asconf)
3030Sstevel@tonic-gate 		sctp_process_sendq(sctp);
3040Sstevel@tonic-gate 	return (0);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * Remove one or more addresses bound to the sctp_t.
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate int
3110Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
3120Sstevel@tonic-gate     boolean_t caller_hold_lock)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	int		error = 0;
3150Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (!caller_hold_lock)
3180Sstevel@tonic-gate 		RUN_SCTP(sctp);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
3210Sstevel@tonic-gate 		if (!caller_hold_lock)
3220Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3230Sstevel@tonic-gate 		return (EINVAL);
3240Sstevel@tonic-gate 	}
325*252Svi117747 	/*
326*252Svi117747 	 * Fail the remove if we are beyond listen, but can't send this
327*252Svi117747 	 * to the peer.
328*252Svi117747 	 */
329*252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
330*252Svi117747 		if (!sctp_addip_enabled || !sctp->sctp_understands_asconf ||
331*252Svi117747 		    !sctp->sctp_understands_addip) {
332*252Svi117747 			if (!caller_hold_lock)
333*252Svi117747 				WAKE_SCTP(sctp);
334*252Svi117747 			return (EINVAL);
335*252Svi117747 		}
3360Sstevel@tonic-gate 		do_asconf = B_TRUE;
337*252Svi117747 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/* Can't delete the last address nor all of the addresses */
3400Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
3410Sstevel@tonic-gate 		if (!caller_hold_lock)
3420Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3430Sstevel@tonic-gate 		return (EINVAL);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	error = sctp_del_ip(sctp, addrs, addrcnt);
3470Sstevel@tonic-gate 	if (!caller_hold_lock)
3480Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3490Sstevel@tonic-gate 	if (error == 0 && do_asconf)
3500Sstevel@tonic-gate 		sctp_process_sendq(sctp);
3510Sstevel@tonic-gate 	return (error);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * If the "bind_to_req_port_only" parameter is set, if the requested port
3560Sstevel@tonic-gate  * number is available, return it, If not return 0
3570Sstevel@tonic-gate  *
3580Sstevel@tonic-gate  * If "bind_to_req_port_only" parameter is not set and
3590Sstevel@tonic-gate  * If the requested port number is available, return it.  If not, return
3600Sstevel@tonic-gate  * the first anonymous port we happen across.  If no anonymous ports are
3610Sstevel@tonic-gate  * available, return 0. addr is the requested local address, if any.
3620Sstevel@tonic-gate  *
3630Sstevel@tonic-gate  * In either case, when succeeding update the sctp_t to record the port number
3640Sstevel@tonic-gate  * and insert it in the bind hash table.
3650Sstevel@tonic-gate  */
3660Sstevel@tonic-gate in_port_t
3670Sstevel@tonic-gate sctp_bindi(sctp_t *sctp, in_port_t port, int bind_to_req_port_only,
3680Sstevel@tonic-gate     int user_specified)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	/* number of times we have run around the loop */
3710Sstevel@tonic-gate 	int count = 0;
3720Sstevel@tonic-gate 	/* maximum number of times to run around the loop */
3730Sstevel@tonic-gate 	int loopmax;
3740Sstevel@tonic-gate 	zoneid_t zoneid = sctp->sctp_zoneid;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	/*
3770Sstevel@tonic-gate 	 * Lookup for free addresses is done in a loop and "loopmax"
3780Sstevel@tonic-gate 	 * influences how long we spin in the loop
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	if (bind_to_req_port_only) {
3810Sstevel@tonic-gate 		/*
3820Sstevel@tonic-gate 		 * If the requested port is busy, don't bother to look
3830Sstevel@tonic-gate 		 * for a new one. Setting loop maximum count to 1 has
3840Sstevel@tonic-gate 		 * that effect.
3850Sstevel@tonic-gate 		 */
3860Sstevel@tonic-gate 		loopmax = 1;
3870Sstevel@tonic-gate 	} else {
3880Sstevel@tonic-gate 		/*
3890Sstevel@tonic-gate 		 * If the requested port is busy, look for a free one
3900Sstevel@tonic-gate 		 * in the anonymous port range.
3910Sstevel@tonic-gate 		 * Set loopmax appropriately so that one does not look
3920Sstevel@tonic-gate 		 * forever in the case all of the anonymous ports are in use.
3930Sstevel@tonic-gate 		 */
3940Sstevel@tonic-gate 		loopmax = (sctp_largest_anon_port -
3950Sstevel@tonic-gate 		    sctp_smallest_anon_port + 1);
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	do {
3980Sstevel@tonic-gate 		uint16_t	lport;
3990Sstevel@tonic-gate 		sctp_tf_t	*tbf;
4000Sstevel@tonic-gate 		sctp_t		*lsctp;
4010Sstevel@tonic-gate 		int		addrcmp;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		lport = htons(port);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		/*
4060Sstevel@tonic-gate 		 * Ensure that the sctp_t is not currently in the bind hash.
4070Sstevel@tonic-gate 		 * Hold the lock on the hash bucket to ensure that
4080Sstevel@tonic-gate 		 * the duplicate check plus the insertion is an atomic
4090Sstevel@tonic-gate 		 * operation.
4100Sstevel@tonic-gate 		 *
4110Sstevel@tonic-gate 		 * This function does an inline lookup on the bind hash list
4120Sstevel@tonic-gate 		 * Make sure that we access only members of sctp_t
4130Sstevel@tonic-gate 		 * and that we don't look at sctp_sctp, since we are not
4140Sstevel@tonic-gate 		 * doing a SCTPB_REFHOLD. For more details please see the notes
4150Sstevel@tonic-gate 		 * in sctp_compress()
4160Sstevel@tonic-gate 		 */
4170Sstevel@tonic-gate 		sctp_bind_hash_remove(sctp);
4180Sstevel@tonic-gate 		tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)];
4190Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
4200Sstevel@tonic-gate 		for (lsctp = tbf->tf_sctp; lsctp != NULL;
4210Sstevel@tonic-gate 		    lsctp = lsctp->sctp_bind_hash) {
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 			if (lport != lsctp->sctp_lport ||
4240Sstevel@tonic-gate 			    lsctp->sctp_zoneid != zoneid ||
4250Sstevel@tonic-gate 			    lsctp->sctp_state < SCTPS_BOUND)
4260Sstevel@tonic-gate 				continue;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 			addrcmp = sctp_compare_saddrs(sctp, lsctp);
4290Sstevel@tonic-gate 			if (addrcmp != SCTP_ADDR_DISJOINT) {
4300Sstevel@tonic-gate 				if (!sctp->sctp_reuseaddr) {
4310Sstevel@tonic-gate 					/* in use */
4320Sstevel@tonic-gate 					break;
4330Sstevel@tonic-gate 				} else if (lsctp->sctp_state == SCTPS_BOUND ||
4340Sstevel@tonic-gate 				    lsctp->sctp_state == SCTPS_LISTEN) {
4350Sstevel@tonic-gate 					/*
4360Sstevel@tonic-gate 					 * socket option SO_REUSEADDR is set
4370Sstevel@tonic-gate 					 * on the binding sctp_t.
4380Sstevel@tonic-gate 					 *
4390Sstevel@tonic-gate 					 * We have found a match of IP source
4400Sstevel@tonic-gate 					 * address and source port, which is
4410Sstevel@tonic-gate 					 * refused regardless of the
4420Sstevel@tonic-gate 					 * SO_REUSEADDR setting, so we break.
4430Sstevel@tonic-gate 					 */
4440Sstevel@tonic-gate 					break;
4450Sstevel@tonic-gate 				}
4460Sstevel@tonic-gate 			}
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 		if (lsctp != NULL) {
4490Sstevel@tonic-gate 			/* The port number is busy */
4500Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4510Sstevel@tonic-gate 		} else {
4520Sstevel@tonic-gate 			/*
4530Sstevel@tonic-gate 			 * This port is ours. Insert in fanout and mark as
4540Sstevel@tonic-gate 			 * bound to prevent others from getting the port
4550Sstevel@tonic-gate 			 * number.
4560Sstevel@tonic-gate 			 */
4570Sstevel@tonic-gate 			sctp->sctp_state = SCTPS_BOUND;
4580Sstevel@tonic-gate 			sctp->sctp_lport = lport;
4590Sstevel@tonic-gate 			sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 			ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf);
4620Sstevel@tonic-gate 			sctp_bind_hash_insert(tbf, sctp, 1);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 			/*
4670Sstevel@tonic-gate 			 * We don't want sctp_next_port_to_try to "inherit"
4680Sstevel@tonic-gate 			 * a port number supplied by the user in a bind.
4690Sstevel@tonic-gate 			 */
4700Sstevel@tonic-gate 			if (user_specified != 0)
4710Sstevel@tonic-gate 				return (port);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 			/*
4740Sstevel@tonic-gate 			 * This is the only place where sctp_next_port_to_try
4750Sstevel@tonic-gate 			 * is updated. After the update, it may or may not
4760Sstevel@tonic-gate 			 * be in the valid range.
4770Sstevel@tonic-gate 			 */
4780Sstevel@tonic-gate 			sctp_next_port_to_try = port + 1;
4790Sstevel@tonic-gate 			return (port);
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 		if ((count == 0) && (user_specified)) {
4830Sstevel@tonic-gate 			/*
4840Sstevel@tonic-gate 			 * We may have to return an anonymous port. So
4850Sstevel@tonic-gate 			 * get one to start with.
4860Sstevel@tonic-gate 			 */
4870Sstevel@tonic-gate 			port = sctp_update_next_port(sctp_next_port_to_try);
4880Sstevel@tonic-gate 			user_specified = 0;
4890Sstevel@tonic-gate 		} else {
4900Sstevel@tonic-gate 			port = sctp_update_next_port(port + 1);
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		/*
4940Sstevel@tonic-gate 		 * Don't let this loop run forever in the case where
4950Sstevel@tonic-gate 		 * all of the anonymous ports are in use.
4960Sstevel@tonic-gate 		 */
4970Sstevel@tonic-gate 	} while (++count < loopmax);
4980Sstevel@tonic-gate 	return (0);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate  * Don't let port fall into the privileged range.
5030Sstevel@tonic-gate  * Since the extra privileged ports can be arbitrary we also
5040Sstevel@tonic-gate  * ensure that we exclude those from consideration.
5050Sstevel@tonic-gate  * sctp_g_epriv_ports is not sorted thus we loop over it until
5060Sstevel@tonic-gate  * there are no changes.
5070Sstevel@tonic-gate  *
5080Sstevel@tonic-gate  * Note: No locks are held when inspecting sctp_g_*epriv_ports
5090Sstevel@tonic-gate  * but instead the code relies on:
5100Sstevel@tonic-gate  * - the fact that the address of the array and its size never changes
5110Sstevel@tonic-gate  * - the atomic assignment of the elements of the array
5120Sstevel@tonic-gate  */
5130Sstevel@tonic-gate in_port_t
5140Sstevel@tonic-gate sctp_update_next_port(in_port_t port)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	int i;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate retry:
5190Sstevel@tonic-gate 	if (port < sctp_smallest_anon_port || port > sctp_largest_anon_port)
5200Sstevel@tonic-gate 		port = sctp_smallest_anon_port;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (port < sctp_smallest_nonpriv_port)
5230Sstevel@tonic-gate 		port = sctp_smallest_nonpriv_port;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	for (i = 0; i < sctp_g_num_epriv_ports; i++) {
5260Sstevel@tonic-gate 		if (port == sctp_g_epriv_ports[i]) {
5270Sstevel@tonic-gate 			port++;
5280Sstevel@tonic-gate 			/*
5290Sstevel@tonic-gate 			 * Make sure whether the port is in the
5300Sstevel@tonic-gate 			 * valid range.
5310Sstevel@tonic-gate 			 *
5320Sstevel@tonic-gate 			 * XXX Note that if sctp_g_epriv_ports contains
5330Sstevel@tonic-gate 			 * all the anonymous ports this will be an
5340Sstevel@tonic-gate 			 * infinite loop.
5350Sstevel@tonic-gate 			 */
5360Sstevel@tonic-gate 			goto retry;
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	return (port);
5400Sstevel@tonic-gate }
541