xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_bind.c (revision 1735:077045a8a111)
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  */
21*1735Skcpoon 
220Sstevel@tonic-gate /*
231676Sjpk  * Copyright 2006 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>
401676Sjpk #include <sys/tsol/tndb.h>
411676Sjpk #include <sys/tsol/tnet.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <netinet/in.h>
440Sstevel@tonic-gate #include <netinet/ip6.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <inet/common.h>
470Sstevel@tonic-gate #include <inet/ip.h>
480Sstevel@tonic-gate #include <inet/ip6.h>
490Sstevel@tonic-gate #include <inet/ipclassifier.h>
500Sstevel@tonic-gate #include "sctp_impl.h"
510Sstevel@tonic-gate #include "sctp_asconf.h"
520Sstevel@tonic-gate #include "sctp_addr.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate uint_t	sctp_next_port_to_try;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * Returns 0 on success, EACCES on permission failure.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate static int
600Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	/*
630Sstevel@tonic-gate 	 * Get a valid port (within the anonymous range and should not
640Sstevel@tonic-gate 	 * be a privileged one) to use if the user has not given a port.
650Sstevel@tonic-gate 	 * If multiple threads are here, they may all start with
660Sstevel@tonic-gate 	 * with the same initial port. But, it should be fine as long as
670Sstevel@tonic-gate 	 * sctp_bindi will ensure that no two threads will be assigned
680Sstevel@tonic-gate 	 * the same port.
690Sstevel@tonic-gate 	 */
700Sstevel@tonic-gate 	if (*requested_port == 0) {
711676Sjpk 		*requested_port = sctp_update_next_port(sctp_next_port_to_try,
721676Sjpk 		    crgetzone(sctp->sctp_credp));
731676Sjpk 		if (*requested_port == 0)
741676Sjpk 			return (EACCES);
750Sstevel@tonic-gate 		*user_specified = 0;
760Sstevel@tonic-gate 	} else {
770Sstevel@tonic-gate 		int i;
780Sstevel@tonic-gate 		boolean_t priv = B_FALSE;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 		/*
810Sstevel@tonic-gate 		 * If the requested_port is in the well-known privileged range,
820Sstevel@tonic-gate 		 * verify that the stream was opened by a privileged user.
830Sstevel@tonic-gate 		 * Note: No locks are held when inspecting sctp_g_*epriv_ports
840Sstevel@tonic-gate 		 * but instead the code relies on:
850Sstevel@tonic-gate 		 * - the fact that the address of the array and its size never
860Sstevel@tonic-gate 		 *   changes
870Sstevel@tonic-gate 		 * - the atomic assignment of the elements of the array
880Sstevel@tonic-gate 		 */
890Sstevel@tonic-gate 		if (*requested_port < sctp_smallest_nonpriv_port) {
900Sstevel@tonic-gate 			priv = B_TRUE;
910Sstevel@tonic-gate 		} else {
920Sstevel@tonic-gate 			for (i = 0; i < sctp_g_num_epriv_ports; i++) {
930Sstevel@tonic-gate 				if (*requested_port == sctp_g_epriv_ports[i]) {
940Sstevel@tonic-gate 					priv = B_TRUE;
950Sstevel@tonic-gate 					break;
960Sstevel@tonic-gate 				}
970Sstevel@tonic-gate 			}
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 		if (priv) {
1000Sstevel@tonic-gate 			/*
1010Sstevel@tonic-gate 			 * sctp_bind() should take a cred_t argument so that
1020Sstevel@tonic-gate 			 * we can use it here.
1030Sstevel@tonic-gate 			 */
1040Sstevel@tonic-gate 			if (secpolicy_net_privaddr(sctp->sctp_credp,
1050Sstevel@tonic-gate 			    *requested_port) != 0) {
1060Sstevel@tonic-gate 				dprint(1,
1070Sstevel@tonic-gate 				    ("sctp_bind(x): no prive for port %d",
1080Sstevel@tonic-gate 				    *requested_port));
1091676Sjpk 				return (EACCES);
1100Sstevel@tonic-gate 			}
1110Sstevel@tonic-gate 		}
1120Sstevel@tonic-gate 		*user_specified = 1;
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	return (0);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate int
1190Sstevel@tonic-gate sctp_listen(sctp_t *sctp)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	sctp_tf_t	*tf;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	RUN_SCTP(sctp);
1240Sstevel@tonic-gate 	/*
1250Sstevel@tonic-gate 	 * TCP handles listen() increasing the backlog, need to check
126852Svi117747 	 * if it should be handled here too
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_BOUND) {
1290Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1300Sstevel@tonic-gate 		return (EINVAL);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	/* Do an anonymous bind for unbound socket doing listen(). */
1340Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0) {
1350Sstevel@tonic-gate 		struct sockaddr_storage ss;
1360Sstevel@tonic-gate 		int ret;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 		bzero(&ss, sizeof (ss));
1390Sstevel@tonic-gate 		ss.ss_family = sctp->sctp_family;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1420Sstevel@tonic-gate 		if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
1430Sstevel@tonic-gate 			sizeof (ss))) != 0)
1440Sstevel@tonic-gate 			return (ret);
1450Sstevel@tonic-gate 		RUN_SCTP(sctp)
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_LISTEN;
1490Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
1500Sstevel@tonic-gate 	sctp->sctp_last_secret_update = lbolt64;
1510Sstevel@tonic-gate 	bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
1520Sstevel@tonic-gate 	tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))];
1530Sstevel@tonic-gate 	sctp_listen_hash_insert(tf, sctp);
1540Sstevel@tonic-gate 	WAKE_SCTP(sctp);
1550Sstevel@tonic-gate 	return (0);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * Bind the sctp_t to a sockaddr, which includes an address and other
1600Sstevel@tonic-gate  * information, such as port or flowinfo.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate int
1630Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	int		user_specified;
1660Sstevel@tonic-gate 	boolean_t	bind_to_req_port_only;
1670Sstevel@tonic-gate 	in_port_t	requested_port;
1680Sstevel@tonic-gate 	in_port_t	allocated_port;
1690Sstevel@tonic-gate 	int		err = 0;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	ASSERT(sctp != NULL);
1720Sstevel@tonic-gate 	ASSERT(sa);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	RUN_SCTP(sctp);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_BOUND) {
1770Sstevel@tonic-gate 		err = EINVAL;
1780Sstevel@tonic-gate 		goto done;
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	switch (sa->sa_family) {
1820Sstevel@tonic-gate 	case AF_INET:
1830Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in) ||
1840Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
1850Sstevel@tonic-gate 			err = EINVAL;
1860Sstevel@tonic-gate 			goto done;
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port);
1890Sstevel@tonic-gate 		break;
1900Sstevel@tonic-gate 	case AF_INET6:
1910Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in6) ||
1920Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET) {
1930Sstevel@tonic-gate 			err = EINVAL;
1940Sstevel@tonic-gate 			goto done;
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
1970Sstevel@tonic-gate 		/* Set the flowinfo. */
1980Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_vcf =
1990Sstevel@tonic-gate 		    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
2000Sstevel@tonic-gate 		    (((struct sockaddr_in6 *)sa)->sin6_flowinfo &
2010Sstevel@tonic-gate 		    ~IPV6_VERS_AND_FLOW_MASK);
2020Sstevel@tonic-gate 		break;
2030Sstevel@tonic-gate 	default:
2040Sstevel@tonic-gate 		err = EAFNOSUPPORT;
2050Sstevel@tonic-gate 		goto done;
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
2080Sstevel@tonic-gate 
2091676Sjpk 	err = sctp_select_port(sctp, &requested_port, &user_specified);
2101676Sjpk 	if (err != 0)
2110Sstevel@tonic-gate 		goto done;
2120Sstevel@tonic-gate 
213852Svi117747 	if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE,
214852Svi117747 	    user_specified == 1 ? htons(requested_port) : 0)) != 0) {
2150Sstevel@tonic-gate 		goto done;
216852Svi117747 	}
2171676Sjpk 	err = sctp_bindi(sctp, requested_port, bind_to_req_port_only,
2181676Sjpk 	    user_specified, &allocated_port);
2191676Sjpk 	if (err != 0) {
2200Sstevel@tonic-gate 		sctp_free_saddrs(sctp);
2211676Sjpk 	} else {
2221676Sjpk 		ASSERT(sctp->sctp_state == SCTPS_BOUND);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate done:
2250Sstevel@tonic-gate 	WAKE_SCTP(sctp);
2260Sstevel@tonic-gate 	return (err);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * Perform bind/unbind operation of a list of addresses on a sctp_t
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate int
2330Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2360Sstevel@tonic-gate 	ASSERT(addrs != NULL);
2370Sstevel@tonic-gate 	ASSERT(addrcnt > 0);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	switch (bindop) {
2400Sstevel@tonic-gate 	case SCTP_BINDX_ADD_ADDR:
241852Svi117747 		return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE,
242852Svi117747 		    sctp->sctp_lport));
2430Sstevel@tonic-gate 	case SCTP_BINDX_REM_ADDR:
2440Sstevel@tonic-gate 		return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
2450Sstevel@tonic-gate 	default:
2460Sstevel@tonic-gate 		return (EINVAL);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * Add a list of addresses to a sctp_t.
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate int
2540Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
255852Svi117747     boolean_t caller_hold_lock, in_port_t port)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	int		err = 0;
2580Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (!caller_hold_lock)
2610Sstevel@tonic-gate 		RUN_SCTP(sctp);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
2640Sstevel@tonic-gate 		if (!caller_hold_lock)
2650Sstevel@tonic-gate 			WAKE_SCTP(sctp);
2660Sstevel@tonic-gate 		return (EINVAL);
2670Sstevel@tonic-gate 	}
268252Svi117747 
269252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
270252Svi117747 		/*
271252Svi117747 		 * Let's do some checking here rather than undoing the
272252Svi117747 		 * add later (for these reasons).
273252Svi117747 		 */
274252Svi117747 		if (!sctp_addip_enabled || !sctp->sctp_understands_asconf ||
275252Svi117747 		    !sctp->sctp_understands_addip) {
276252Svi117747 			if (!caller_hold_lock)
277252Svi117747 				WAKE_SCTP(sctp);
278252Svi117747 			return (EINVAL);
279252Svi117747 		}
2800Sstevel@tonic-gate 		do_asconf = B_TRUE;
281252Svi117747 	}
282852Svi117747 	/*
283852Svi117747 	 * On a clustered node, for an inaddr_any bind, we will pass the list
284852Svi117747 	 * of all the addresses in the global list, minus any address on the
285852Svi117747 	 * loopback interface, and expect the clustering susbsystem to give us
286852Svi117747 	 * the correct list for the 'port'. For explicit binds we give the
287852Svi117747 	 * list of addresses  and the clustering module validates it for the
288852Svi117747 	 * 'port'.
289852Svi117747 	 *
290852Svi117747 	 * On a non-clustered node, cl_sctp_check_addrs will be NULL and
291852Svi117747 	 * we proceed as usual.
292852Svi117747 	 */
293852Svi117747 	if (cl_sctp_check_addrs != NULL) {
294852Svi117747 		uchar_t		*addrlist = NULL;
295852Svi117747 		size_t		size = 0;
296852Svi117747 		int		unspec = 0;
297852Svi117747 		boolean_t	do_listen;
298852Svi117747 		uchar_t		*llist = NULL;
299852Svi117747 		size_t		lsize = 0;
300852Svi117747 
301852Svi117747 		/*
302852Svi117747 		 * If we are adding addresses after listening, but before
303852Svi117747 		 * an association is established, we need to update the
304852Svi117747 		 * clustering module with this info.
305852Svi117747 		 */
306852Svi117747 		do_listen = !do_asconf && sctp->sctp_state > SCTPS_BOUND &&
307852Svi117747 		    cl_sctp_listen != NULL;
308852Svi117747 
309852Svi117747 		err = sctp_get_addrlist(sctp, addrs, &addrcnt, &addrlist,
310852Svi117747 		    &unspec, &size);
311852Svi117747 		if (err != 0) {
312852Svi117747 			ASSERT(addrlist == NULL);
313852Svi117747 			ASSERT(addrcnt == 0);
314852Svi117747 			ASSERT(size == 0);
315852Svi117747 			if (!caller_hold_lock)
316852Svi117747 				WAKE_SCTP(sctp);
317*1735Skcpoon 			SCTP_KSTAT(sctp_cl_check_addrs);
318852Svi117747 			return (err);
319852Svi117747 		}
320852Svi117747 		ASSERT(addrlist != NULL);
321852Svi117747 		(*cl_sctp_check_addrs)(sctp->sctp_family, port, &addrlist,
322852Svi117747 		    size, &addrcnt, unspec == 1);
323852Svi117747 		if (addrcnt == 0) {
324852Svi117747 			/* We free the list */
325852Svi117747 			kmem_free(addrlist, size);
326852Svi117747 			if (!caller_hold_lock)
327852Svi117747 				WAKE_SCTP(sctp);
328852Svi117747 			return (EINVAL);
329852Svi117747 		}
330852Svi117747 		if (do_listen) {
331852Svi117747 			lsize = sizeof (in6_addr_t) * addrcnt;
332852Svi117747 			llist = kmem_alloc(lsize, KM_SLEEP);
333852Svi117747 		}
334852Svi117747 		err = sctp_valid_addr_list(sctp, addrlist, addrcnt, llist,
335852Svi117747 		    lsize);
336852Svi117747 		if (err == 0 && do_listen) {
337852Svi117747 			(*cl_sctp_listen)(sctp->sctp_family, llist,
338852Svi117747 			    addrcnt, sctp->sctp_lport);
339852Svi117747 			/* list will be freed by the clustering module */
340852Svi117747 		} else if (err != 0 && llist != NULL) {
341852Svi117747 			kmem_free(llist, lsize);
342852Svi117747 		}
343852Svi117747 		/* free the list we allocated */
344852Svi117747 		kmem_free(addrlist, size);
345852Svi117747 	} else {
346852Svi117747 		err = sctp_valid_addr_list(sctp, addrs, addrcnt, NULL, 0);
347852Svi117747 	}
3480Sstevel@tonic-gate 	if (err != 0) {
3490Sstevel@tonic-gate 		if (!caller_hold_lock)
3500Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3510Sstevel@tonic-gate 		return (err);
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 	/* Need to send  ASCONF messages */
3540Sstevel@tonic-gate 	if (do_asconf) {
3550Sstevel@tonic-gate 		err = sctp_add_ip(sctp, addrs, addrcnt);
3560Sstevel@tonic-gate 		if (err != 0) {
3570Sstevel@tonic-gate 			sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
3580Sstevel@tonic-gate 			if (!caller_hold_lock)
3590Sstevel@tonic-gate 				WAKE_SCTP(sctp);
3600Sstevel@tonic-gate 			return (err);
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 	if (!caller_hold_lock)
3640Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3650Sstevel@tonic-gate 	if (do_asconf)
3660Sstevel@tonic-gate 		sctp_process_sendq(sctp);
3670Sstevel@tonic-gate 	return (0);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * Remove one or more addresses bound to the sctp_t.
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate int
3740Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
3750Sstevel@tonic-gate     boolean_t caller_hold_lock)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	int		error = 0;
3780Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
379852Svi117747 	uchar_t		*ulist = NULL;
380852Svi117747 	size_t		usize = 0;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (!caller_hold_lock)
3830Sstevel@tonic-gate 		RUN_SCTP(sctp);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
3860Sstevel@tonic-gate 		if (!caller_hold_lock)
3870Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3880Sstevel@tonic-gate 		return (EINVAL);
3890Sstevel@tonic-gate 	}
390252Svi117747 	/*
391252Svi117747 	 * Fail the remove if we are beyond listen, but can't send this
392252Svi117747 	 * to the peer.
393252Svi117747 	 */
394252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
395252Svi117747 		if (!sctp_addip_enabled || !sctp->sctp_understands_asconf ||
396252Svi117747 		    !sctp->sctp_understands_addip) {
397252Svi117747 			if (!caller_hold_lock)
398252Svi117747 				WAKE_SCTP(sctp);
399252Svi117747 			return (EINVAL);
400252Svi117747 		}
4010Sstevel@tonic-gate 		do_asconf = B_TRUE;
402252Svi117747 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	/* Can't delete the last address nor all of the addresses */
4050Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
4060Sstevel@tonic-gate 		if (!caller_hold_lock)
4070Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4080Sstevel@tonic-gate 		return (EINVAL);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
411852Svi117747 	if (cl_sctp_unlisten != NULL && !do_asconf &&
412852Svi117747 	    sctp->sctp_state > SCTPS_BOUND) {
413852Svi117747 		usize = sizeof (in6_addr_t) * addrcnt;
414852Svi117747 		ulist = kmem_alloc(usize, KM_SLEEP);
415852Svi117747 	}
416852Svi117747 
417852Svi117747 	error = sctp_del_ip(sctp, addrs, addrcnt, ulist, usize);
418852Svi117747 	if (error != 0) {
419852Svi117747 		if (ulist != NULL)
420852Svi117747 			kmem_free(ulist, usize);
421852Svi117747 		if (!caller_hold_lock)
422852Svi117747 			WAKE_SCTP(sctp);
423852Svi117747 		return (error);
424852Svi117747 	}
425852Svi117747 	/* ulist will be non-NULL only if cl_sctp_unlisten is non-NULL */
426852Svi117747 	if (ulist != NULL) {
427852Svi117747 		ASSERT(cl_sctp_unlisten != NULL);
428852Svi117747 		(*cl_sctp_unlisten)(sctp->sctp_family, ulist, addrcnt,
429852Svi117747 		    sctp->sctp_lport);
430852Svi117747 		/* ulist will be freed by the clustering module */
431852Svi117747 	}
4320Sstevel@tonic-gate 	if (!caller_hold_lock)
4330Sstevel@tonic-gate 		WAKE_SCTP(sctp);
434852Svi117747 	if (do_asconf)
4350Sstevel@tonic-gate 		sctp_process_sendq(sctp);
4360Sstevel@tonic-gate 	return (error);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /*
4401676Sjpk  * Returns 0 for success, errno value otherwise.
4411676Sjpk  *
4421676Sjpk  * If the "bind_to_req_port_only" parameter is set and the requested port
4431676Sjpk  * number is available, then set allocated_port to it.  If not available,
4441676Sjpk  * return an error.
4450Sstevel@tonic-gate  *
4461676Sjpk  * If the "bind_to_req_port_only" parameter is not set and the requested port
4471676Sjpk  * number is available, then set allocated_port to it.  If not available,
4481676Sjpk  * find the first anonymous port we can and set allocated_port to that.  If no
4491676Sjpk  * anonymous ports are available, return an error.
4500Sstevel@tonic-gate  *
4511676Sjpk  * In either case, when succeeding, update the sctp_t to record the port number
4520Sstevel@tonic-gate  * and insert it in the bind hash table.
4530Sstevel@tonic-gate  */
4541676Sjpk int
4551676Sjpk sctp_bindi(sctp_t *sctp, in_port_t port, boolean_t bind_to_req_port_only,
4561676Sjpk     int user_specified, in_port_t *allocated_port)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	/* number of times we have run around the loop */
4590Sstevel@tonic-gate 	int count = 0;
4600Sstevel@tonic-gate 	/* maximum number of times to run around the loop */
4610Sstevel@tonic-gate 	int loopmax;
4620Sstevel@tonic-gate 	zoneid_t zoneid = sctp->sctp_zoneid;
4631676Sjpk 	zone_t *zone = crgetzone(sctp->sctp_credp);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * Lookup for free addresses is done in a loop and "loopmax"
4670Sstevel@tonic-gate 	 * influences how long we spin in the loop
4680Sstevel@tonic-gate 	 */
4690Sstevel@tonic-gate 	if (bind_to_req_port_only) {
4700Sstevel@tonic-gate 		/*
4710Sstevel@tonic-gate 		 * If the requested port is busy, don't bother to look
4720Sstevel@tonic-gate 		 * for a new one. Setting loop maximum count to 1 has
4730Sstevel@tonic-gate 		 * that effect.
4740Sstevel@tonic-gate 		 */
4750Sstevel@tonic-gate 		loopmax = 1;
4760Sstevel@tonic-gate 	} else {
4770Sstevel@tonic-gate 		/*
4780Sstevel@tonic-gate 		 * If the requested port is busy, look for a free one
4790Sstevel@tonic-gate 		 * in the anonymous port range.
4800Sstevel@tonic-gate 		 * Set loopmax appropriately so that one does not look
4810Sstevel@tonic-gate 		 * forever in the case all of the anonymous ports are in use.
4820Sstevel@tonic-gate 		 */
4830Sstevel@tonic-gate 		loopmax = (sctp_largest_anon_port -
4840Sstevel@tonic-gate 		    sctp_smallest_anon_port + 1);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 	do {
4870Sstevel@tonic-gate 		uint16_t	lport;
4880Sstevel@tonic-gate 		sctp_tf_t	*tbf;
4890Sstevel@tonic-gate 		sctp_t		*lsctp;
4900Sstevel@tonic-gate 		int		addrcmp;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		lport = htons(port);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		/*
4950Sstevel@tonic-gate 		 * Ensure that the sctp_t is not currently in the bind hash.
4960Sstevel@tonic-gate 		 * Hold the lock on the hash bucket to ensure that
4970Sstevel@tonic-gate 		 * the duplicate check plus the insertion is an atomic
4980Sstevel@tonic-gate 		 * operation.
4990Sstevel@tonic-gate 		 *
5000Sstevel@tonic-gate 		 * This function does an inline lookup on the bind hash list
5010Sstevel@tonic-gate 		 * Make sure that we access only members of sctp_t
5020Sstevel@tonic-gate 		 * and that we don't look at sctp_sctp, since we are not
5030Sstevel@tonic-gate 		 * doing a SCTPB_REFHOLD. For more details please see the notes
5040Sstevel@tonic-gate 		 * in sctp_compress()
5050Sstevel@tonic-gate 		 */
5060Sstevel@tonic-gate 		sctp_bind_hash_remove(sctp);
5070Sstevel@tonic-gate 		tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)];
5080Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
5090Sstevel@tonic-gate 		for (lsctp = tbf->tf_sctp; lsctp != NULL;
5100Sstevel@tonic-gate 		    lsctp = lsctp->sctp_bind_hash) {
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 			if (lport != lsctp->sctp_lport ||
5130Sstevel@tonic-gate 			    lsctp->sctp_state < SCTPS_BOUND)
5140Sstevel@tonic-gate 				continue;
5150Sstevel@tonic-gate 
5161676Sjpk 			/*
5171676Sjpk 			 * On a labeled system, we must treat bindings to ports
5181676Sjpk 			 * on shared IP addresses by sockets with MAC exemption
5191676Sjpk 			 * privilege as being in all zones, as there's
5201676Sjpk 			 * otherwise no way to identify the right receiver.
5211676Sjpk 			 */
5221676Sjpk 			if (lsctp->sctp_zoneid != zoneid &&
5231676Sjpk 			    !lsctp->sctp_mac_exempt && !sctp->sctp_mac_exempt)
5241676Sjpk 				continue;
5251676Sjpk 
5260Sstevel@tonic-gate 			addrcmp = sctp_compare_saddrs(sctp, lsctp);
5270Sstevel@tonic-gate 			if (addrcmp != SCTP_ADDR_DISJOINT) {
5280Sstevel@tonic-gate 				if (!sctp->sctp_reuseaddr) {
5290Sstevel@tonic-gate 					/* in use */
5300Sstevel@tonic-gate 					break;
5310Sstevel@tonic-gate 				} else if (lsctp->sctp_state == SCTPS_BOUND ||
5320Sstevel@tonic-gate 				    lsctp->sctp_state == SCTPS_LISTEN) {
5330Sstevel@tonic-gate 					/*
5340Sstevel@tonic-gate 					 * socket option SO_REUSEADDR is set
5350Sstevel@tonic-gate 					 * on the binding sctp_t.
5360Sstevel@tonic-gate 					 *
5370Sstevel@tonic-gate 					 * We have found a match of IP source
5380Sstevel@tonic-gate 					 * address and source port, which is
5390Sstevel@tonic-gate 					 * refused regardless of the
5400Sstevel@tonic-gate 					 * SO_REUSEADDR setting, so we break.
5410Sstevel@tonic-gate 					 */
5420Sstevel@tonic-gate 					break;
5430Sstevel@tonic-gate 				}
5440Sstevel@tonic-gate 			}
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 		if (lsctp != NULL) {
5470Sstevel@tonic-gate 			/* The port number is busy */
5480Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
5490Sstevel@tonic-gate 		} else {
5501676Sjpk 			conn_t *connp = sctp->sctp_connp;
5511676Sjpk 
5521676Sjpk 			if (is_system_labeled()) {
5531676Sjpk 				mlp_type_t addrtype, mlptype;
5541676Sjpk 
5551676Sjpk 				/*
5561676Sjpk 				 * On a labeled system we must check the type
5571676Sjpk 				 * of the binding requested by the user (either
5581676Sjpk 				 * MLP or SLP on shared and private addresses),
5591676Sjpk 				 * and that the user's requested binding
5601676Sjpk 				 * is permitted.
5611676Sjpk 				 */
5621676Sjpk 				addrtype = tsol_mlp_addr_type(zone->zone_id,
5631676Sjpk 				    sctp->sctp_ipversion,
5641676Sjpk 				    sctp->sctp_ipversion == IPV4_VERSION ?
5651676Sjpk 				    (void *)&sctp->sctp_ipha->ipha_src :
5661676Sjpk 				    (void *)&sctp->sctp_ip6h->ip6_src);
5671676Sjpk 
5681676Sjpk 				/*
5691676Sjpk 				 * tsol_mlp_addr_type returns the possibilities
5701676Sjpk 				 * for the selected address.  Since all local
5711676Sjpk 				 * addresses are either private or shared, the
5721676Sjpk 				 * return value mlptSingle means "local address
5731676Sjpk 				 * not valid (interface not present)."
5741676Sjpk 				 */
5751676Sjpk 				if (addrtype == mlptSingle) {
5761676Sjpk 					mutex_exit(&tbf->tf_lock);
5771676Sjpk 					return (EADDRNOTAVAIL);
5781676Sjpk 				}
5791676Sjpk 				mlptype = tsol_mlp_port_type(zone, IPPROTO_SCTP,
5801676Sjpk 				    port, addrtype);
5811676Sjpk 				if (mlptype != mlptSingle) {
5821676Sjpk 					if (secpolicy_net_bindmlp(connp->
5831676Sjpk 					    conn_cred) != 0) {
5841676Sjpk 						mutex_exit(&tbf->tf_lock);
5851676Sjpk 						return (EACCES);
5861676Sjpk 					}
5871676Sjpk 					/*
5881676Sjpk 					 * If we're binding a shared MLP, then
5891676Sjpk 					 * make sure that this zone is the one
5901676Sjpk 					 * that owns that MLP.  Shared MLPs can
5911676Sjpk 					 * be owned by at most one zone.
5921676Sjpk 					 */
5931676Sjpk 
5941676Sjpk 					if (mlptype == mlptShared &&
5951676Sjpk 					    addrtype == mlptShared &&
5961676Sjpk 					    connp->conn_zoneid !=
5971676Sjpk 					    tsol_mlp_findzone(IPPROTO_SCTP,
5981676Sjpk 					    lport)) {
5991676Sjpk 						mutex_exit(&tbf->tf_lock);
6001676Sjpk 						return (EACCES);
6011676Sjpk 					}
6021676Sjpk 					connp->conn_mlp_type = mlptype;
6031676Sjpk 				}
6041676Sjpk 			}
6050Sstevel@tonic-gate 			/*
6060Sstevel@tonic-gate 			 * This port is ours. Insert in fanout and mark as
6070Sstevel@tonic-gate 			 * bound to prevent others from getting the port
6080Sstevel@tonic-gate 			 * number.
6090Sstevel@tonic-gate 			 */
6100Sstevel@tonic-gate 			sctp->sctp_state = SCTPS_BOUND;
6110Sstevel@tonic-gate 			sctp->sctp_lport = lport;
6121676Sjpk 			sctp->sctp_sctph->sh_sport = lport;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 			ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf);
6150Sstevel@tonic-gate 			sctp_bind_hash_insert(tbf, sctp, 1);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 			/*
6200Sstevel@tonic-gate 			 * We don't want sctp_next_port_to_try to "inherit"
6210Sstevel@tonic-gate 			 * a port number supplied by the user in a bind.
6221676Sjpk 			 *
6230Sstevel@tonic-gate 			 * This is the only place where sctp_next_port_to_try
6240Sstevel@tonic-gate 			 * is updated. After the update, it may or may not
6250Sstevel@tonic-gate 			 * be in the valid range.
6260Sstevel@tonic-gate 			 */
6271676Sjpk 			if (user_specified == 0)
6281676Sjpk 				sctp_next_port_to_try = port + 1;
6291676Sjpk 
6301676Sjpk 			*allocated_port = port;
6311676Sjpk 
6321676Sjpk 			return (0);
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		if ((count == 0) && (user_specified)) {
6360Sstevel@tonic-gate 			/*
6370Sstevel@tonic-gate 			 * We may have to return an anonymous port. So
6380Sstevel@tonic-gate 			 * get one to start with.
6390Sstevel@tonic-gate 			 */
6401676Sjpk 			port = sctp_update_next_port(sctp_next_port_to_try,
6411676Sjpk 			    zone);
6420Sstevel@tonic-gate 			user_specified = 0;
6430Sstevel@tonic-gate 		} else {
6441676Sjpk 			port = sctp_update_next_port(port + 1, zone);
6450Sstevel@tonic-gate 		}
6461676Sjpk 		if (port == 0)
6471676Sjpk 			break;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		/*
6500Sstevel@tonic-gate 		 * Don't let this loop run forever in the case where
6510Sstevel@tonic-gate 		 * all of the anonymous ports are in use.
6520Sstevel@tonic-gate 		 */
6530Sstevel@tonic-gate 	} while (++count < loopmax);
6541676Sjpk 
6551676Sjpk 	return (bind_to_req_port_only ? EADDRINUSE : EADDRNOTAVAIL);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate  * Don't let port fall into the privileged range.
6600Sstevel@tonic-gate  * Since the extra privileged ports can be arbitrary we also
6610Sstevel@tonic-gate  * ensure that we exclude those from consideration.
6620Sstevel@tonic-gate  * sctp_g_epriv_ports is not sorted thus we loop over it until
6630Sstevel@tonic-gate  * there are no changes.
6640Sstevel@tonic-gate  *
6650Sstevel@tonic-gate  * Note: No locks are held when inspecting sctp_g_*epriv_ports
6660Sstevel@tonic-gate  * but instead the code relies on:
6670Sstevel@tonic-gate  * - the fact that the address of the array and its size never changes
6680Sstevel@tonic-gate  * - the atomic assignment of the elements of the array
6690Sstevel@tonic-gate  */
6700Sstevel@tonic-gate in_port_t
6711676Sjpk sctp_update_next_port(in_port_t port, zone_t *zone)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	int i;
6741676Sjpk 	boolean_t restart = B_FALSE;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate retry:
6771676Sjpk 	if (port < sctp_smallest_anon_port)
6780Sstevel@tonic-gate 		port = sctp_smallest_anon_port;
6790Sstevel@tonic-gate 
6801676Sjpk 	if (port > sctp_largest_anon_port) {
6811676Sjpk 		if (restart)
6821676Sjpk 			return (0);
6831676Sjpk 		restart = B_TRUE;
6841676Sjpk 		port = sctp_smallest_anon_port;
6851676Sjpk 	}
6861676Sjpk 
6870Sstevel@tonic-gate 	if (port < sctp_smallest_nonpriv_port)
6880Sstevel@tonic-gate 		port = sctp_smallest_nonpriv_port;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	for (i = 0; i < sctp_g_num_epriv_ports; i++) {
6910Sstevel@tonic-gate 		if (port == sctp_g_epriv_ports[i]) {
6920Sstevel@tonic-gate 			port++;
6930Sstevel@tonic-gate 			/*
6940Sstevel@tonic-gate 			 * Make sure whether the port is in the
6950Sstevel@tonic-gate 			 * valid range.
6960Sstevel@tonic-gate 			 *
6970Sstevel@tonic-gate 			 * XXX Note that if sctp_g_epriv_ports contains
6980Sstevel@tonic-gate 			 * all the anonymous ports this will be an
6990Sstevel@tonic-gate 			 * infinite loop.
7000Sstevel@tonic-gate 			 */
7010Sstevel@tonic-gate 			goto retry;
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 	}
7041676Sjpk 
7051676Sjpk 	if (is_system_labeled() &&
7061676Sjpk 	    (i = tsol_next_port(zone, port, IPPROTO_SCTP, B_TRUE)) != 0) {
7071676Sjpk 		port = i;
7081676Sjpk 		goto retry;
7091676Sjpk 	}
7101676Sjpk 
7110Sstevel@tonic-gate 	return (port);
7120Sstevel@tonic-gate }
713