10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211735Skcpoon 
220Sstevel@tonic-gate /*
233448Sdh155122  * Copyright 2007 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 /*
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 {
603448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
613448Sdh155122 
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) {
713448Sdh155122 		*requested_port = sctp_update_next_port(
723448Sdh155122 		    sctps->sctps_next_port_to_try,
733448Sdh155122 		    crgetzone(sctp->sctp_credp), sctps);
741676Sjpk 		if (*requested_port == 0)
751676Sjpk 			return (EACCES);
760Sstevel@tonic-gate 		*user_specified = 0;
770Sstevel@tonic-gate 	} else {
780Sstevel@tonic-gate 		int i;
790Sstevel@tonic-gate 		boolean_t priv = B_FALSE;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 		/*
820Sstevel@tonic-gate 		 * If the requested_port is in the well-known privileged range,
830Sstevel@tonic-gate 		 * verify that the stream was opened by a privileged user.
840Sstevel@tonic-gate 		 * Note: No locks are held when inspecting sctp_g_*epriv_ports
850Sstevel@tonic-gate 		 * but instead the code relies on:
860Sstevel@tonic-gate 		 * - the fact that the address of the array and its size never
870Sstevel@tonic-gate 		 *   changes
880Sstevel@tonic-gate 		 * - the atomic assignment of the elements of the array
890Sstevel@tonic-gate 		 */
903448Sdh155122 		if (*requested_port < sctps->sctps_smallest_nonpriv_port) {
910Sstevel@tonic-gate 			priv = B_TRUE;
920Sstevel@tonic-gate 		} else {
933448Sdh155122 			for (i = 0; i < sctps->sctps_g_num_epriv_ports; i++) {
943448Sdh155122 				if (*requested_port ==
953448Sdh155122 				    sctps->sctps_g_epriv_ports[i]) {
960Sstevel@tonic-gate 					priv = B_TRUE;
970Sstevel@tonic-gate 					break;
980Sstevel@tonic-gate 				}
990Sstevel@tonic-gate 			}
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 		if (priv) {
1020Sstevel@tonic-gate 			/*
1030Sstevel@tonic-gate 			 * sctp_bind() should take a cred_t argument so that
1040Sstevel@tonic-gate 			 * we can use it here.
1050Sstevel@tonic-gate 			 */
1060Sstevel@tonic-gate 			if (secpolicy_net_privaddr(sctp->sctp_credp,
1070Sstevel@tonic-gate 			    *requested_port) != 0) {
1080Sstevel@tonic-gate 				dprint(1,
1090Sstevel@tonic-gate 				    ("sctp_bind(x): no prive for port %d",
1100Sstevel@tonic-gate 				    *requested_port));
1111676Sjpk 				return (EACCES);
1120Sstevel@tonic-gate 			}
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 		*user_specified = 1;
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	return (0);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate int
1210Sstevel@tonic-gate sctp_listen(sctp_t *sctp)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	sctp_tf_t	*tf;
1243448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	RUN_SCTP(sctp);
1270Sstevel@tonic-gate 	/*
1280Sstevel@tonic-gate 	 * TCP handles listen() increasing the backlog, need to check
129852Svi117747 	 * if it should be handled here too
1300Sstevel@tonic-gate 	 */
131*4505Skcpoon 	if (sctp->sctp_state > SCTPS_BOUND ||
132*4505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
1330Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1340Sstevel@tonic-gate 		return (EINVAL);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/* Do an anonymous bind for unbound socket doing listen(). */
1380Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0) {
1390Sstevel@tonic-gate 		struct sockaddr_storage ss;
1400Sstevel@tonic-gate 		int ret;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		bzero(&ss, sizeof (ss));
1430Sstevel@tonic-gate 		ss.ss_family = sctp->sctp_family;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1460Sstevel@tonic-gate 		if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
147*4505Skcpoon 		    sizeof (ss))) != 0)
1480Sstevel@tonic-gate 			return (ret);
1490Sstevel@tonic-gate 		RUN_SCTP(sctp)
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_LISTEN;
1530Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
1540Sstevel@tonic-gate 	sctp->sctp_last_secret_update = lbolt64;
1550Sstevel@tonic-gate 	bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
1563448Sdh155122 	tf = &sctps->sctps_listen_fanout[SCTP_LISTEN_HASH(
157*4505Skcpoon 	    ntohs(sctp->sctp_lport))];
1580Sstevel@tonic-gate 	sctp_listen_hash_insert(tf, sctp);
1590Sstevel@tonic-gate 	WAKE_SCTP(sctp);
1600Sstevel@tonic-gate 	return (0);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * Bind the sctp_t to a sockaddr, which includes an address and other
1650Sstevel@tonic-gate  * information, such as port or flowinfo.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate int
1680Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	int		user_specified;
1710Sstevel@tonic-gate 	boolean_t	bind_to_req_port_only;
1720Sstevel@tonic-gate 	in_port_t	requested_port;
1730Sstevel@tonic-gate 	in_port_t	allocated_port;
1740Sstevel@tonic-gate 	int		err = 0;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	ASSERT(sctp != NULL);
1770Sstevel@tonic-gate 	ASSERT(sa);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	RUN_SCTP(sctp);
1800Sstevel@tonic-gate 
181*4505Skcpoon 	if (sctp->sctp_state > SCTPS_BOUND ||
182*4505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
1830Sstevel@tonic-gate 		err = EINVAL;
1840Sstevel@tonic-gate 		goto done;
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	switch (sa->sa_family) {
1880Sstevel@tonic-gate 	case AF_INET:
1890Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in) ||
1900Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
1910Sstevel@tonic-gate 			err = EINVAL;
1920Sstevel@tonic-gate 			goto done;
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port);
1950Sstevel@tonic-gate 		break;
1960Sstevel@tonic-gate 	case AF_INET6:
1970Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in6) ||
1980Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET) {
1990Sstevel@tonic-gate 			err = EINVAL;
2000Sstevel@tonic-gate 			goto done;
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
2030Sstevel@tonic-gate 		/* Set the flowinfo. */
2040Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_vcf =
2050Sstevel@tonic-gate 		    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
2060Sstevel@tonic-gate 		    (((struct sockaddr_in6 *)sa)->sin6_flowinfo &
2070Sstevel@tonic-gate 		    ~IPV6_VERS_AND_FLOW_MASK);
2080Sstevel@tonic-gate 		break;
2090Sstevel@tonic-gate 	default:
2100Sstevel@tonic-gate 		err = EAFNOSUPPORT;
2110Sstevel@tonic-gate 		goto done;
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
2140Sstevel@tonic-gate 
2151676Sjpk 	err = sctp_select_port(sctp, &requested_port, &user_specified);
2161676Sjpk 	if (err != 0)
2170Sstevel@tonic-gate 		goto done;
2180Sstevel@tonic-gate 
219852Svi117747 	if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE,
220852Svi117747 	    user_specified == 1 ? htons(requested_port) : 0)) != 0) {
2210Sstevel@tonic-gate 		goto done;
222852Svi117747 	}
2231676Sjpk 	err = sctp_bindi(sctp, requested_port, bind_to_req_port_only,
2241676Sjpk 	    user_specified, &allocated_port);
2251676Sjpk 	if (err != 0) {
2260Sstevel@tonic-gate 		sctp_free_saddrs(sctp);
2271676Sjpk 	} else {
2281676Sjpk 		ASSERT(sctp->sctp_state == SCTPS_BOUND);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate done:
2310Sstevel@tonic-gate 	WAKE_SCTP(sctp);
2320Sstevel@tonic-gate 	return (err);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate  * Perform bind/unbind operation of a list of addresses on a sctp_t
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate int
2390Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2420Sstevel@tonic-gate 	ASSERT(addrs != NULL);
2430Sstevel@tonic-gate 	ASSERT(addrcnt > 0);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	switch (bindop) {
2460Sstevel@tonic-gate 	case SCTP_BINDX_ADD_ADDR:
247852Svi117747 		return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE,
248852Svi117747 		    sctp->sctp_lport));
2490Sstevel@tonic-gate 	case SCTP_BINDX_REM_ADDR:
2500Sstevel@tonic-gate 		return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
2510Sstevel@tonic-gate 	default:
2520Sstevel@tonic-gate 		return (EINVAL);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * Add a list of addresses to a sctp_t.
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate int
2600Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
261852Svi117747     boolean_t caller_hold_lock, in_port_t port)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	int		err = 0;
2640Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
2653448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (!caller_hold_lock)
2680Sstevel@tonic-gate 		RUN_SCTP(sctp);
2690Sstevel@tonic-gate 
270*4505Skcpoon 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
271*4505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
2720Sstevel@tonic-gate 		if (!caller_hold_lock)
2730Sstevel@tonic-gate 			WAKE_SCTP(sctp);
2740Sstevel@tonic-gate 		return (EINVAL);
2750Sstevel@tonic-gate 	}
276252Svi117747 
277252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
278252Svi117747 		/*
279252Svi117747 		 * Let's do some checking here rather than undoing the
280252Svi117747 		 * add later (for these reasons).
281252Svi117747 		 */
2823448Sdh155122 		if (!sctps->sctps_addip_enabled ||
2833448Sdh155122 		    !sctp->sctp_understands_asconf ||
284252Svi117747 		    !sctp->sctp_understands_addip) {
285252Svi117747 			if (!caller_hold_lock)
286252Svi117747 				WAKE_SCTP(sctp);
287252Svi117747 			return (EINVAL);
288252Svi117747 		}
2890Sstevel@tonic-gate 		do_asconf = B_TRUE;
290252Svi117747 	}
291852Svi117747 	/*
292852Svi117747 	 * On a clustered node, for an inaddr_any bind, we will pass the list
293852Svi117747 	 * of all the addresses in the global list, minus any address on the
294852Svi117747 	 * loopback interface, and expect the clustering susbsystem to give us
295852Svi117747 	 * the correct list for the 'port'. For explicit binds we give the
296852Svi117747 	 * list of addresses  and the clustering module validates it for the
297852Svi117747 	 * 'port'.
298852Svi117747 	 *
299852Svi117747 	 * On a non-clustered node, cl_sctp_check_addrs will be NULL and
300852Svi117747 	 * we proceed as usual.
301852Svi117747 	 */
302852Svi117747 	if (cl_sctp_check_addrs != NULL) {
303852Svi117747 		uchar_t		*addrlist = NULL;
304852Svi117747 		size_t		size = 0;
305852Svi117747 		int		unspec = 0;
306852Svi117747 		boolean_t	do_listen;
307852Svi117747 		uchar_t		*llist = NULL;
308852Svi117747 		size_t		lsize = 0;
309852Svi117747 
310852Svi117747 		/*
311852Svi117747 		 * If we are adding addresses after listening, but before
312852Svi117747 		 * an association is established, we need to update the
313852Svi117747 		 * clustering module with this info.
314852Svi117747 		 */
315852Svi117747 		do_listen = !do_asconf && sctp->sctp_state > SCTPS_BOUND &&
316852Svi117747 		    cl_sctp_listen != NULL;
317852Svi117747 
318852Svi117747 		err = sctp_get_addrlist(sctp, addrs, &addrcnt, &addrlist,
319852Svi117747 		    &unspec, &size);
320852Svi117747 		if (err != 0) {
321852Svi117747 			ASSERT(addrlist == NULL);
322852Svi117747 			ASSERT(addrcnt == 0);
323852Svi117747 			ASSERT(size == 0);
324852Svi117747 			if (!caller_hold_lock)
325852Svi117747 				WAKE_SCTP(sctp);
3263448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_check_addrs);
327852Svi117747 			return (err);
328852Svi117747 		}
329852Svi117747 		ASSERT(addrlist != NULL);
330852Svi117747 		(*cl_sctp_check_addrs)(sctp->sctp_family, port, &addrlist,
331852Svi117747 		    size, &addrcnt, unspec == 1);
332852Svi117747 		if (addrcnt == 0) {
333852Svi117747 			/* We free the list */
334852Svi117747 			kmem_free(addrlist, size);
335852Svi117747 			if (!caller_hold_lock)
336852Svi117747 				WAKE_SCTP(sctp);
337852Svi117747 			return (EINVAL);
338852Svi117747 		}
339852Svi117747 		if (do_listen) {
340852Svi117747 			lsize = sizeof (in6_addr_t) * addrcnt;
341852Svi117747 			llist = kmem_alloc(lsize, KM_SLEEP);
342852Svi117747 		}
343852Svi117747 		err = sctp_valid_addr_list(sctp, addrlist, addrcnt, llist,
344852Svi117747 		    lsize);
345852Svi117747 		if (err == 0 && do_listen) {
346852Svi117747 			(*cl_sctp_listen)(sctp->sctp_family, llist,
347852Svi117747 			    addrcnt, sctp->sctp_lport);
348852Svi117747 			/* list will be freed by the clustering module */
349852Svi117747 		} else if (err != 0 && llist != NULL) {
350852Svi117747 			kmem_free(llist, lsize);
351852Svi117747 		}
352852Svi117747 		/* free the list we allocated */
353852Svi117747 		kmem_free(addrlist, size);
354852Svi117747 	} else {
355852Svi117747 		err = sctp_valid_addr_list(sctp, addrs, addrcnt, NULL, 0);
356852Svi117747 	}
3570Sstevel@tonic-gate 	if (err != 0) {
3580Sstevel@tonic-gate 		if (!caller_hold_lock)
3590Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3600Sstevel@tonic-gate 		return (err);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 	/* Need to send  ASCONF messages */
3630Sstevel@tonic-gate 	if (do_asconf) {
3640Sstevel@tonic-gate 		err = sctp_add_ip(sctp, addrs, addrcnt);
3650Sstevel@tonic-gate 		if (err != 0) {
3660Sstevel@tonic-gate 			sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
3670Sstevel@tonic-gate 			if (!caller_hold_lock)
3680Sstevel@tonic-gate 				WAKE_SCTP(sctp);
3690Sstevel@tonic-gate 			return (err);
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 	if (!caller_hold_lock)
3730Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3740Sstevel@tonic-gate 	if (do_asconf)
3750Sstevel@tonic-gate 		sctp_process_sendq(sctp);
3760Sstevel@tonic-gate 	return (0);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * Remove one or more addresses bound to the sctp_t.
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate int
3830Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
3840Sstevel@tonic-gate     boolean_t caller_hold_lock)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	int		error = 0;
3870Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
388852Svi117747 	uchar_t		*ulist = NULL;
389852Svi117747 	size_t		usize = 0;
3903448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (!caller_hold_lock)
3930Sstevel@tonic-gate 		RUN_SCTP(sctp);
3940Sstevel@tonic-gate 
395*4505Skcpoon 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
396*4505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
3970Sstevel@tonic-gate 		if (!caller_hold_lock)
3980Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3990Sstevel@tonic-gate 		return (EINVAL);
4000Sstevel@tonic-gate 	}
401252Svi117747 	/*
402252Svi117747 	 * Fail the remove if we are beyond listen, but can't send this
403252Svi117747 	 * to the peer.
404252Svi117747 	 */
405252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
4063448Sdh155122 		if (!sctps->sctps_addip_enabled ||
4073448Sdh155122 		    !sctp->sctp_understands_asconf ||
408252Svi117747 		    !sctp->sctp_understands_addip) {
409252Svi117747 			if (!caller_hold_lock)
410252Svi117747 				WAKE_SCTP(sctp);
411252Svi117747 			return (EINVAL);
412252Svi117747 		}
4130Sstevel@tonic-gate 		do_asconf = B_TRUE;
414252Svi117747 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/* Can't delete the last address nor all of the addresses */
4170Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
4180Sstevel@tonic-gate 		if (!caller_hold_lock)
4190Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4200Sstevel@tonic-gate 		return (EINVAL);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
423852Svi117747 	if (cl_sctp_unlisten != NULL && !do_asconf &&
424852Svi117747 	    sctp->sctp_state > SCTPS_BOUND) {
425852Svi117747 		usize = sizeof (in6_addr_t) * addrcnt;
426852Svi117747 		ulist = kmem_alloc(usize, KM_SLEEP);
427852Svi117747 	}
428852Svi117747 
429852Svi117747 	error = sctp_del_ip(sctp, addrs, addrcnt, ulist, usize);
430852Svi117747 	if (error != 0) {
431852Svi117747 		if (ulist != NULL)
432852Svi117747 			kmem_free(ulist, usize);
433852Svi117747 		if (!caller_hold_lock)
434852Svi117747 			WAKE_SCTP(sctp);
435852Svi117747 		return (error);
436852Svi117747 	}
437852Svi117747 	/* ulist will be non-NULL only if cl_sctp_unlisten is non-NULL */
438852Svi117747 	if (ulist != NULL) {
439852Svi117747 		ASSERT(cl_sctp_unlisten != NULL);
440852Svi117747 		(*cl_sctp_unlisten)(sctp->sctp_family, ulist, addrcnt,
441852Svi117747 		    sctp->sctp_lport);
442852Svi117747 		/* ulist will be freed by the clustering module */
443852Svi117747 	}
4440Sstevel@tonic-gate 	if (!caller_hold_lock)
4450Sstevel@tonic-gate 		WAKE_SCTP(sctp);
446852Svi117747 	if (do_asconf)
4470Sstevel@tonic-gate 		sctp_process_sendq(sctp);
4480Sstevel@tonic-gate 	return (error);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4521676Sjpk  * Returns 0 for success, errno value otherwise.
4531676Sjpk  *
4541676Sjpk  * If the "bind_to_req_port_only" parameter is set and the requested port
4551676Sjpk  * number is available, then set allocated_port to it.  If not available,
4561676Sjpk  * return an error.
4570Sstevel@tonic-gate  *
4581676Sjpk  * If the "bind_to_req_port_only" parameter is not set and the requested port
4591676Sjpk  * number is available, then set allocated_port to it.  If not available,
4601676Sjpk  * find the first anonymous port we can and set allocated_port to that.  If no
4611676Sjpk  * anonymous ports are available, return an error.
4620Sstevel@tonic-gate  *
4631676Sjpk  * In either case, when succeeding, update the sctp_t to record the port number
4640Sstevel@tonic-gate  * and insert it in the bind hash table.
4650Sstevel@tonic-gate  */
4661676Sjpk int
4671676Sjpk sctp_bindi(sctp_t *sctp, in_port_t port, boolean_t bind_to_req_port_only,
4681676Sjpk     int user_specified, in_port_t *allocated_port)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	/* number of times we have run around the loop */
4710Sstevel@tonic-gate 	int count = 0;
4720Sstevel@tonic-gate 	/* maximum number of times to run around the loop */
4730Sstevel@tonic-gate 	int loopmax;
4740Sstevel@tonic-gate 	zoneid_t zoneid = sctp->sctp_zoneid;
4751676Sjpk 	zone_t *zone = crgetzone(sctp->sctp_credp);
4763448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * Lookup for free addresses is done in a loop and "loopmax"
4800Sstevel@tonic-gate 	 * influences how long we spin in the loop
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	if (bind_to_req_port_only) {
4830Sstevel@tonic-gate 		/*
4840Sstevel@tonic-gate 		 * If the requested port is busy, don't bother to look
4850Sstevel@tonic-gate 		 * for a new one. Setting loop maximum count to 1 has
4860Sstevel@tonic-gate 		 * that effect.
4870Sstevel@tonic-gate 		 */
4880Sstevel@tonic-gate 		loopmax = 1;
4890Sstevel@tonic-gate 	} else {
4900Sstevel@tonic-gate 		/*
4910Sstevel@tonic-gate 		 * If the requested port is busy, look for a free one
4920Sstevel@tonic-gate 		 * in the anonymous port range.
4930Sstevel@tonic-gate 		 * Set loopmax appropriately so that one does not look
4940Sstevel@tonic-gate 		 * forever in the case all of the anonymous ports are in use.
4950Sstevel@tonic-gate 		 */
4963448Sdh155122 		loopmax = (sctps->sctps_largest_anon_port -
4973448Sdh155122 		    sctps->sctps_smallest_anon_port + 1);
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 	do {
5000Sstevel@tonic-gate 		uint16_t	lport;
5010Sstevel@tonic-gate 		sctp_tf_t	*tbf;
5020Sstevel@tonic-gate 		sctp_t		*lsctp;
5030Sstevel@tonic-gate 		int		addrcmp;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		lport = htons(port);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		/*
5080Sstevel@tonic-gate 		 * Ensure that the sctp_t is not currently in the bind hash.
5090Sstevel@tonic-gate 		 * Hold the lock on the hash bucket to ensure that
5100Sstevel@tonic-gate 		 * the duplicate check plus the insertion is an atomic
5110Sstevel@tonic-gate 		 * operation.
5120Sstevel@tonic-gate 		 *
5130Sstevel@tonic-gate 		 * This function does an inline lookup on the bind hash list
5140Sstevel@tonic-gate 		 * Make sure that we access only members of sctp_t
5150Sstevel@tonic-gate 		 * and that we don't look at sctp_sctp, since we are not
5160Sstevel@tonic-gate 		 * doing a SCTPB_REFHOLD. For more details please see the notes
5170Sstevel@tonic-gate 		 * in sctp_compress()
5180Sstevel@tonic-gate 		 */
5190Sstevel@tonic-gate 		sctp_bind_hash_remove(sctp);
5203448Sdh155122 		tbf = &sctps->sctps_bind_fanout[SCTP_BIND_HASH(port)];
5210Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
5220Sstevel@tonic-gate 		for (lsctp = tbf->tf_sctp; lsctp != NULL;
5230Sstevel@tonic-gate 		    lsctp = lsctp->sctp_bind_hash) {
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 			if (lport != lsctp->sctp_lport ||
5260Sstevel@tonic-gate 			    lsctp->sctp_state < SCTPS_BOUND)
5270Sstevel@tonic-gate 				continue;
5280Sstevel@tonic-gate 
5291676Sjpk 			/*
5301676Sjpk 			 * On a labeled system, we must treat bindings to ports
5311676Sjpk 			 * on shared IP addresses by sockets with MAC exemption
5321676Sjpk 			 * privilege as being in all zones, as there's
5331676Sjpk 			 * otherwise no way to identify the right receiver.
5341676Sjpk 			 */
5351676Sjpk 			if (lsctp->sctp_zoneid != zoneid &&
5361676Sjpk 			    !lsctp->sctp_mac_exempt && !sctp->sctp_mac_exempt)
5371676Sjpk 				continue;
5381676Sjpk 
5390Sstevel@tonic-gate 			addrcmp = sctp_compare_saddrs(sctp, lsctp);
5400Sstevel@tonic-gate 			if (addrcmp != SCTP_ADDR_DISJOINT) {
5410Sstevel@tonic-gate 				if (!sctp->sctp_reuseaddr) {
5420Sstevel@tonic-gate 					/* in use */
5430Sstevel@tonic-gate 					break;
5440Sstevel@tonic-gate 				} else if (lsctp->sctp_state == SCTPS_BOUND ||
5450Sstevel@tonic-gate 				    lsctp->sctp_state == SCTPS_LISTEN) {
5460Sstevel@tonic-gate 					/*
5470Sstevel@tonic-gate 					 * socket option SO_REUSEADDR is set
5480Sstevel@tonic-gate 					 * on the binding sctp_t.
5490Sstevel@tonic-gate 					 *
5500Sstevel@tonic-gate 					 * We have found a match of IP source
5510Sstevel@tonic-gate 					 * address and source port, which is
5520Sstevel@tonic-gate 					 * refused regardless of the
5530Sstevel@tonic-gate 					 * SO_REUSEADDR setting, so we break.
5540Sstevel@tonic-gate 					 */
5550Sstevel@tonic-gate 					break;
5560Sstevel@tonic-gate 				}
5570Sstevel@tonic-gate 			}
5580Sstevel@tonic-gate 		}
5590Sstevel@tonic-gate 		if (lsctp != NULL) {
5600Sstevel@tonic-gate 			/* The port number is busy */
5610Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
5620Sstevel@tonic-gate 		} else {
5631676Sjpk 			conn_t *connp = sctp->sctp_connp;
5641676Sjpk 
5651676Sjpk 			if (is_system_labeled()) {
5661676Sjpk 				mlp_type_t addrtype, mlptype;
5671676Sjpk 
5681676Sjpk 				/*
5691676Sjpk 				 * On a labeled system we must check the type
5701676Sjpk 				 * of the binding requested by the user (either
5711676Sjpk 				 * MLP or SLP on shared and private addresses),
5721676Sjpk 				 * and that the user's requested binding
5731676Sjpk 				 * is permitted.
5741676Sjpk 				 */
5751676Sjpk 				addrtype = tsol_mlp_addr_type(zone->zone_id,
5761676Sjpk 				    sctp->sctp_ipversion,
5771676Sjpk 				    sctp->sctp_ipversion == IPV4_VERSION ?
5781676Sjpk 				    (void *)&sctp->sctp_ipha->ipha_src :
5793448Sdh155122 				    (void *)&sctp->sctp_ip6h->ip6_src,
5803448Sdh155122 				    sctps->sctps_netstack->netstack_ip);
5811676Sjpk 
5821676Sjpk 				/*
5831676Sjpk 				 * tsol_mlp_addr_type returns the possibilities
5841676Sjpk 				 * for the selected address.  Since all local
5851676Sjpk 				 * addresses are either private or shared, the
5861676Sjpk 				 * return value mlptSingle means "local address
5871676Sjpk 				 * not valid (interface not present)."
5881676Sjpk 				 */
5891676Sjpk 				if (addrtype == mlptSingle) {
5901676Sjpk 					mutex_exit(&tbf->tf_lock);
5911676Sjpk 					return (EADDRNOTAVAIL);
5921676Sjpk 				}
5931676Sjpk 				mlptype = tsol_mlp_port_type(zone, IPPROTO_SCTP,
5941676Sjpk 				    port, addrtype);
5951676Sjpk 				if (mlptype != mlptSingle) {
5961676Sjpk 					if (secpolicy_net_bindmlp(connp->
5971676Sjpk 					    conn_cred) != 0) {
5981676Sjpk 						mutex_exit(&tbf->tf_lock);
5991676Sjpk 						return (EACCES);
6001676Sjpk 					}
6011676Sjpk 					/*
6021676Sjpk 					 * If we're binding a shared MLP, then
6031676Sjpk 					 * make sure that this zone is the one
6041676Sjpk 					 * that owns that MLP.  Shared MLPs can
6051676Sjpk 					 * be owned by at most one zone.
6063448Sdh155122 					 *
6073448Sdh155122 					 * No need to handle exclusive-stack
6083448Sdh155122 					 * zones since ALL_ZONES only applies
6093448Sdh155122 					 * to the shared stack.
6101676Sjpk 					 */
6111676Sjpk 
6121676Sjpk 					if (mlptype == mlptShared &&
6131676Sjpk 					    addrtype == mlptShared &&
6141676Sjpk 					    connp->conn_zoneid !=
6151676Sjpk 					    tsol_mlp_findzone(IPPROTO_SCTP,
6161676Sjpk 					    lport)) {
6171676Sjpk 						mutex_exit(&tbf->tf_lock);
6181676Sjpk 						return (EACCES);
6191676Sjpk 					}
6201676Sjpk 					connp->conn_mlp_type = mlptype;
6211676Sjpk 				}
6221676Sjpk 			}
6230Sstevel@tonic-gate 			/*
6240Sstevel@tonic-gate 			 * This port is ours. Insert in fanout and mark as
6250Sstevel@tonic-gate 			 * bound to prevent others from getting the port
6260Sstevel@tonic-gate 			 * number.
6270Sstevel@tonic-gate 			 */
6280Sstevel@tonic-gate 			sctp->sctp_state = SCTPS_BOUND;
6290Sstevel@tonic-gate 			sctp->sctp_lport = lport;
6301676Sjpk 			sctp->sctp_sctph->sh_sport = lport;
6310Sstevel@tonic-gate 
6323448Sdh155122 			ASSERT(&sctps->sctps_bind_fanout[
633*4505Skcpoon 			    SCTP_BIND_HASH(port)] == tbf);
6340Sstevel@tonic-gate 			sctp_bind_hash_insert(tbf, sctp, 1);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 			/*
6390Sstevel@tonic-gate 			 * We don't want sctp_next_port_to_try to "inherit"
6400Sstevel@tonic-gate 			 * a port number supplied by the user in a bind.
6411676Sjpk 			 *
6420Sstevel@tonic-gate 			 * This is the only place where sctp_next_port_to_try
6430Sstevel@tonic-gate 			 * is updated. After the update, it may or may not
6440Sstevel@tonic-gate 			 * be in the valid range.
6450Sstevel@tonic-gate 			 */
6461676Sjpk 			if (user_specified == 0)
6473448Sdh155122 				sctps->sctps_next_port_to_try = port + 1;
6481676Sjpk 
6491676Sjpk 			*allocated_port = port;
6501676Sjpk 
6511676Sjpk 			return (0);
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		if ((count == 0) && (user_specified)) {
6550Sstevel@tonic-gate 			/*
6560Sstevel@tonic-gate 			 * We may have to return an anonymous port. So
6570Sstevel@tonic-gate 			 * get one to start with.
6580Sstevel@tonic-gate 			 */
6593448Sdh155122 			port = sctp_update_next_port(
6603448Sdh155122 			    sctps->sctps_next_port_to_try,
6613448Sdh155122 			    zone, sctps);
6620Sstevel@tonic-gate 			user_specified = 0;
6630Sstevel@tonic-gate 		} else {
6643448Sdh155122 			port = sctp_update_next_port(port + 1, zone, sctps);
6650Sstevel@tonic-gate 		}
6661676Sjpk 		if (port == 0)
6671676Sjpk 			break;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		/*
6700Sstevel@tonic-gate 		 * Don't let this loop run forever in the case where
6710Sstevel@tonic-gate 		 * all of the anonymous ports are in use.
6720Sstevel@tonic-gate 		 */
6730Sstevel@tonic-gate 	} while (++count < loopmax);
6741676Sjpk 
6751676Sjpk 	return (bind_to_req_port_only ? EADDRINUSE : EADDRNOTAVAIL);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate /*
6790Sstevel@tonic-gate  * Don't let port fall into the privileged range.
6800Sstevel@tonic-gate  * Since the extra privileged ports can be arbitrary we also
6810Sstevel@tonic-gate  * ensure that we exclude those from consideration.
6820Sstevel@tonic-gate  * sctp_g_epriv_ports is not sorted thus we loop over it until
6830Sstevel@tonic-gate  * there are no changes.
6840Sstevel@tonic-gate  *
6850Sstevel@tonic-gate  * Note: No locks are held when inspecting sctp_g_*epriv_ports
6860Sstevel@tonic-gate  * but instead the code relies on:
6870Sstevel@tonic-gate  * - the fact that the address of the array and its size never changes
6880Sstevel@tonic-gate  * - the atomic assignment of the elements of the array
6890Sstevel@tonic-gate  */
6900Sstevel@tonic-gate in_port_t
6913448Sdh155122 sctp_update_next_port(in_port_t port, zone_t *zone, sctp_stack_t *sctps)
6920Sstevel@tonic-gate {
6930Sstevel@tonic-gate 	int i;
6941676Sjpk 	boolean_t restart = B_FALSE;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate retry:
6973448Sdh155122 	if (port < sctps->sctps_smallest_anon_port)
6983448Sdh155122 		port = sctps->sctps_smallest_anon_port;
6990Sstevel@tonic-gate 
7003448Sdh155122 	if (port > sctps->sctps_largest_anon_port) {
7011676Sjpk 		if (restart)
7021676Sjpk 			return (0);
7031676Sjpk 		restart = B_TRUE;
7043448Sdh155122 		port = sctps->sctps_smallest_anon_port;
7051676Sjpk 	}
7061676Sjpk 
7073448Sdh155122 	if (port < sctps->sctps_smallest_nonpriv_port)
7083448Sdh155122 		port = sctps->sctps_smallest_nonpriv_port;
7090Sstevel@tonic-gate 
7103448Sdh155122 	for (i = 0; i < sctps->sctps_g_num_epriv_ports; i++) {
7113448Sdh155122 		if (port == sctps->sctps_g_epriv_ports[i]) {
7120Sstevel@tonic-gate 			port++;
7130Sstevel@tonic-gate 			/*
7140Sstevel@tonic-gate 			 * Make sure whether the port is in the
7150Sstevel@tonic-gate 			 * valid range.
7160Sstevel@tonic-gate 			 *
7170Sstevel@tonic-gate 			 * XXX Note that if sctp_g_epriv_ports contains
7180Sstevel@tonic-gate 			 * all the anonymous ports this will be an
7190Sstevel@tonic-gate 			 * infinite loop.
7200Sstevel@tonic-gate 			 */
7210Sstevel@tonic-gate 			goto retry;
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 	}
7241676Sjpk 
7251676Sjpk 	if (is_system_labeled() &&
7261676Sjpk 	    (i = tsol_next_port(zone, port, IPPROTO_SCTP, B_TRUE)) != 0) {
7271676Sjpk 		port = i;
7281676Sjpk 		goto retry;
7291676Sjpk 	}
7301676Sjpk 
7310Sstevel@tonic-gate 	return (port);
7320Sstevel@tonic-gate }
733