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
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * 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  */
210Sstevel@tonic-gate /*
22*1676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
340Sstevel@tonic-gate #include <sys/tihdr.h>
350Sstevel@tonic-gate #include <sys/stropts.h>
360Sstevel@tonic-gate #include <sys/socket.h>
370Sstevel@tonic-gate #include <sys/random.h>
380Sstevel@tonic-gate #include <sys/policy.h>
39*1676Sjpk #include <sys/tsol/tndb.h>
40*1676Sjpk #include <sys/tsol/tnet.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <netinet/in.h>
430Sstevel@tonic-gate #include <netinet/ip6.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <inet/common.h>
460Sstevel@tonic-gate #include <inet/ip.h>
470Sstevel@tonic-gate #include <inet/ip6.h>
480Sstevel@tonic-gate #include <inet/ipclassifier.h>
490Sstevel@tonic-gate #include "sctp_impl.h"
500Sstevel@tonic-gate #include "sctp_asconf.h"
510Sstevel@tonic-gate #include "sctp_addr.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate uint_t	sctp_next_port_to_try;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Returns 0 on success, EACCES on permission failure.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static int
590Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	/*
620Sstevel@tonic-gate 	 * Get a valid port (within the anonymous range and should not
630Sstevel@tonic-gate 	 * be a privileged one) to use if the user has not given a port.
640Sstevel@tonic-gate 	 * If multiple threads are here, they may all start with
650Sstevel@tonic-gate 	 * with the same initial port. But, it should be fine as long as
660Sstevel@tonic-gate 	 * sctp_bindi will ensure that no two threads will be assigned
670Sstevel@tonic-gate 	 * the same port.
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	if (*requested_port == 0) {
70*1676Sjpk 		*requested_port = sctp_update_next_port(sctp_next_port_to_try,
71*1676Sjpk 		    crgetzone(sctp->sctp_credp));
72*1676Sjpk 		if (*requested_port == 0)
73*1676Sjpk 			return (EACCES);
740Sstevel@tonic-gate 		*user_specified = 0;
750Sstevel@tonic-gate 	} else {
760Sstevel@tonic-gate 		int i;
770Sstevel@tonic-gate 		boolean_t priv = B_FALSE;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 		/*
800Sstevel@tonic-gate 		 * If the requested_port is in the well-known privileged range,
810Sstevel@tonic-gate 		 * verify that the stream was opened by a privileged user.
820Sstevel@tonic-gate 		 * Note: No locks are held when inspecting sctp_g_*epriv_ports
830Sstevel@tonic-gate 		 * but instead the code relies on:
840Sstevel@tonic-gate 		 * - the fact that the address of the array and its size never
850Sstevel@tonic-gate 		 *   changes
860Sstevel@tonic-gate 		 * - the atomic assignment of the elements of the array
870Sstevel@tonic-gate 		 */
880Sstevel@tonic-gate 		if (*requested_port < sctp_smallest_nonpriv_port) {
890Sstevel@tonic-gate 			priv = B_TRUE;
900Sstevel@tonic-gate 		} else {
910Sstevel@tonic-gate 			for (i = 0; i < sctp_g_num_epriv_ports; i++) {
920Sstevel@tonic-gate 				if (*requested_port == sctp_g_epriv_ports[i]) {
930Sstevel@tonic-gate 					priv = B_TRUE;
940Sstevel@tonic-gate 					break;
950Sstevel@tonic-gate 				}
960Sstevel@tonic-gate 			}
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 		if (priv) {
990Sstevel@tonic-gate 			/*
1000Sstevel@tonic-gate 			 * sctp_bind() should take a cred_t argument so that
1010Sstevel@tonic-gate 			 * we can use it here.
1020Sstevel@tonic-gate 			 */
1030Sstevel@tonic-gate 			if (secpolicy_net_privaddr(sctp->sctp_credp,
1040Sstevel@tonic-gate 			    *requested_port) != 0) {
1050Sstevel@tonic-gate 				dprint(1,
1060Sstevel@tonic-gate 				    ("sctp_bind(x): no prive for port %d",
1070Sstevel@tonic-gate 				    *requested_port));
108*1676Sjpk 				return (EACCES);
1090Sstevel@tonic-gate 			}
1100Sstevel@tonic-gate 		}
1110Sstevel@tonic-gate 		*user_specified = 1;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	return (0);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate int
1180Sstevel@tonic-gate sctp_listen(sctp_t *sctp)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	sctp_tf_t	*tf;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	RUN_SCTP(sctp);
1230Sstevel@tonic-gate 	/*
1240Sstevel@tonic-gate 	 * TCP handles listen() increasing the backlog, need to check
125852Svi117747 	 * if it should be handled here too
1260Sstevel@tonic-gate 	 */
1270Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_BOUND) {
1280Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1290Sstevel@tonic-gate 		return (EINVAL);
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/* Do an anonymous bind for unbound socket doing listen(). */
1330Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 0) {
1340Sstevel@tonic-gate 		struct sockaddr_storage ss;
1350Sstevel@tonic-gate 		int ret;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		bzero(&ss, sizeof (ss));
1380Sstevel@tonic-gate 		ss.ss_family = sctp->sctp_family;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		WAKE_SCTP(sctp);
1410Sstevel@tonic-gate 		if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
1420Sstevel@tonic-gate 			sizeof (ss))) != 0)
1430Sstevel@tonic-gate 			return (ret);
1440Sstevel@tonic-gate 		RUN_SCTP(sctp)
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_LISTEN;
1480Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
1490Sstevel@tonic-gate 	sctp->sctp_last_secret_update = lbolt64;
1500Sstevel@tonic-gate 	bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
1510Sstevel@tonic-gate 	tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))];
1520Sstevel@tonic-gate 	sctp_listen_hash_insert(tf, sctp);
1530Sstevel@tonic-gate 	WAKE_SCTP(sctp);
1540Sstevel@tonic-gate 	return (0);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Bind the sctp_t to a sockaddr, which includes an address and other
1590Sstevel@tonic-gate  * information, such as port or flowinfo.
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate int
1620Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	int		user_specified;
1650Sstevel@tonic-gate 	boolean_t	bind_to_req_port_only;
1660Sstevel@tonic-gate 	in_port_t	requested_port;
1670Sstevel@tonic-gate 	in_port_t	allocated_port;
1680Sstevel@tonic-gate 	int		err = 0;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	ASSERT(sctp != NULL);
1710Sstevel@tonic-gate 	ASSERT(sa);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	RUN_SCTP(sctp);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_BOUND) {
1760Sstevel@tonic-gate 		err = EINVAL;
1770Sstevel@tonic-gate 		goto done;
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	switch (sa->sa_family) {
1810Sstevel@tonic-gate 	case AF_INET:
1820Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in) ||
1830Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
1840Sstevel@tonic-gate 			err = EINVAL;
1850Sstevel@tonic-gate 			goto done;
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port);
1880Sstevel@tonic-gate 		break;
1890Sstevel@tonic-gate 	case AF_INET6:
1900Sstevel@tonic-gate 		if (len < sizeof (struct sockaddr_in6) ||
1910Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET) {
1920Sstevel@tonic-gate 			err = EINVAL;
1930Sstevel@tonic-gate 			goto done;
1940Sstevel@tonic-gate 		}
1950Sstevel@tonic-gate 		requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
1960Sstevel@tonic-gate 		/* Set the flowinfo. */
1970Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_vcf =
1980Sstevel@tonic-gate 		    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
1990Sstevel@tonic-gate 		    (((struct sockaddr_in6 *)sa)->sin6_flowinfo &
2000Sstevel@tonic-gate 		    ~IPV6_VERS_AND_FLOW_MASK);
2010Sstevel@tonic-gate 		break;
2020Sstevel@tonic-gate 	default:
2030Sstevel@tonic-gate 		err = EAFNOSUPPORT;
2040Sstevel@tonic-gate 		goto done;
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
2070Sstevel@tonic-gate 
208*1676Sjpk 	err = sctp_select_port(sctp, &requested_port, &user_specified);
209*1676Sjpk 	if (err != 0)
2100Sstevel@tonic-gate 		goto done;
2110Sstevel@tonic-gate 
212852Svi117747 	if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE,
213852Svi117747 	    user_specified == 1 ? htons(requested_port) : 0)) != 0) {
2140Sstevel@tonic-gate 		goto done;
215852Svi117747 	}
216*1676Sjpk 	err = sctp_bindi(sctp, requested_port, bind_to_req_port_only,
217*1676Sjpk 	    user_specified, &allocated_port);
218*1676Sjpk 	if (err != 0) {
2190Sstevel@tonic-gate 		sctp_free_saddrs(sctp);
220*1676Sjpk 	} else {
221*1676Sjpk 		ASSERT(sctp->sctp_state == SCTPS_BOUND);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate done:
2240Sstevel@tonic-gate 	WAKE_SCTP(sctp);
2250Sstevel@tonic-gate 	return (err);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * Perform bind/unbind operation of a list of addresses on a sctp_t
2300Sstevel@tonic-gate  */
2310Sstevel@tonic-gate int
2320Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	ASSERT(sctp != NULL);
2350Sstevel@tonic-gate 	ASSERT(addrs != NULL);
2360Sstevel@tonic-gate 	ASSERT(addrcnt > 0);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	switch (bindop) {
2390Sstevel@tonic-gate 	case SCTP_BINDX_ADD_ADDR:
240852Svi117747 		return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE,
241852Svi117747 		    sctp->sctp_lport));
2420Sstevel@tonic-gate 	case SCTP_BINDX_REM_ADDR:
2430Sstevel@tonic-gate 		return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
2440Sstevel@tonic-gate 	default:
2450Sstevel@tonic-gate 		return (EINVAL);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * Add a list of addresses to a sctp_t.
2510Sstevel@tonic-gate  */
2520Sstevel@tonic-gate int
2530Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
254852Svi117747     boolean_t caller_hold_lock, in_port_t port)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	int		err = 0;
2570Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if (!caller_hold_lock)
2600Sstevel@tonic-gate 		RUN_SCTP(sctp);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
2630Sstevel@tonic-gate 		if (!caller_hold_lock)
2640Sstevel@tonic-gate 			WAKE_SCTP(sctp);
2650Sstevel@tonic-gate 		return (EINVAL);
2660Sstevel@tonic-gate 	}
267252Svi117747 
268252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
269252Svi117747 		/*
270252Svi117747 		 * Let's do some checking here rather than undoing the
271252Svi117747 		 * add later (for these reasons).
272252Svi117747 		 */
273252Svi117747 		if (!sctp_addip_enabled || !sctp->sctp_understands_asconf ||
274252Svi117747 		    !sctp->sctp_understands_addip) {
275252Svi117747 			if (!caller_hold_lock)
276252Svi117747 				WAKE_SCTP(sctp);
277252Svi117747 			return (EINVAL);
278252Svi117747 		}
2790Sstevel@tonic-gate 		do_asconf = B_TRUE;
280252Svi117747 	}
281852Svi117747 	/*
282852Svi117747 	 * On a clustered node, for an inaddr_any bind, we will pass the list
283852Svi117747 	 * of all the addresses in the global list, minus any address on the
284852Svi117747 	 * loopback interface, and expect the clustering susbsystem to give us
285852Svi117747 	 * the correct list for the 'port'. For explicit binds we give the
286852Svi117747 	 * list of addresses  and the clustering module validates it for the
287852Svi117747 	 * 'port'.
288852Svi117747 	 *
289852Svi117747 	 * On a non-clustered node, cl_sctp_check_addrs will be NULL and
290852Svi117747 	 * we proceed as usual.
291852Svi117747 	 */
292852Svi117747 	if (cl_sctp_check_addrs != NULL) {
293852Svi117747 		uchar_t		*addrlist = NULL;
294852Svi117747 		size_t		size = 0;
295852Svi117747 		int		unspec = 0;
296852Svi117747 		boolean_t	do_listen;
297852Svi117747 		uchar_t		*llist = NULL;
298852Svi117747 		size_t		lsize = 0;
299852Svi117747 
300852Svi117747 		/*
301852Svi117747 		 * If we are adding addresses after listening, but before
302852Svi117747 		 * an association is established, we need to update the
303852Svi117747 		 * clustering module with this info.
304852Svi117747 		 */
305852Svi117747 		do_listen = !do_asconf && sctp->sctp_state > SCTPS_BOUND &&
306852Svi117747 		    cl_sctp_listen != NULL;
307852Svi117747 
308852Svi117747 		err = sctp_get_addrlist(sctp, addrs, &addrcnt, &addrlist,
309852Svi117747 		    &unspec, &size);
310852Svi117747 		if (err != 0) {
311852Svi117747 			ASSERT(addrlist == NULL);
312852Svi117747 			ASSERT(addrcnt == 0);
313852Svi117747 			ASSERT(size == 0);
314852Svi117747 			if (!caller_hold_lock)
315852Svi117747 				WAKE_SCTP(sctp);
316852Svi117747 			return (err);
317852Svi117747 		}
318852Svi117747 		ASSERT(addrlist != NULL);
319852Svi117747 		(*cl_sctp_check_addrs)(sctp->sctp_family, port, &addrlist,
320852Svi117747 		    size, &addrcnt, unspec == 1);
321852Svi117747 		if (addrcnt == 0) {
322852Svi117747 			/* We free the list */
323852Svi117747 			kmem_free(addrlist, size);
324852Svi117747 			if (!caller_hold_lock)
325852Svi117747 				WAKE_SCTP(sctp);
326852Svi117747 			return (EINVAL);
327852Svi117747 		}
328852Svi117747 		if (do_listen) {
329852Svi117747 			lsize = sizeof (in6_addr_t) * addrcnt;
330852Svi117747 			llist = kmem_alloc(lsize, KM_SLEEP);
331852Svi117747 		}
332852Svi117747 		err = sctp_valid_addr_list(sctp, addrlist, addrcnt, llist,
333852Svi117747 		    lsize);
334852Svi117747 		if (err == 0 && do_listen) {
335852Svi117747 			(*cl_sctp_listen)(sctp->sctp_family, llist,
336852Svi117747 			    addrcnt, sctp->sctp_lport);
337852Svi117747 			/* list will be freed by the clustering module */
338852Svi117747 		} else if (err != 0 && llist != NULL) {
339852Svi117747 			kmem_free(llist, lsize);
340852Svi117747 		}
341852Svi117747 		/* free the list we allocated */
342852Svi117747 		kmem_free(addrlist, size);
343852Svi117747 	} else {
344852Svi117747 		err = sctp_valid_addr_list(sctp, addrs, addrcnt, NULL, 0);
345852Svi117747 	}
3460Sstevel@tonic-gate 	if (err != 0) {
3470Sstevel@tonic-gate 		if (!caller_hold_lock)
3480Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3490Sstevel@tonic-gate 		return (err);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 	/* Need to send  ASCONF messages */
3520Sstevel@tonic-gate 	if (do_asconf) {
3530Sstevel@tonic-gate 		err = sctp_add_ip(sctp, addrs, addrcnt);
3540Sstevel@tonic-gate 		if (err != 0) {
3550Sstevel@tonic-gate 			sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
3560Sstevel@tonic-gate 			if (!caller_hold_lock)
3570Sstevel@tonic-gate 				WAKE_SCTP(sctp);
3580Sstevel@tonic-gate 			return (err);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 	if (!caller_hold_lock)
3620Sstevel@tonic-gate 		WAKE_SCTP(sctp);
3630Sstevel@tonic-gate 	if (do_asconf)
3640Sstevel@tonic-gate 		sctp_process_sendq(sctp);
3650Sstevel@tonic-gate 	return (0);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate  * Remove one or more addresses bound to the sctp_t.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate int
3720Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
3730Sstevel@tonic-gate     boolean_t caller_hold_lock)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	int		error = 0;
3760Sstevel@tonic-gate 	boolean_t	do_asconf = B_FALSE;
377852Svi117747 	uchar_t		*ulist = NULL;
378852Svi117747 	size_t		usize = 0;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	if (!caller_hold_lock)
3810Sstevel@tonic-gate 		RUN_SCTP(sctp);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
3840Sstevel@tonic-gate 		if (!caller_hold_lock)
3850Sstevel@tonic-gate 			WAKE_SCTP(sctp);
3860Sstevel@tonic-gate 		return (EINVAL);
3870Sstevel@tonic-gate 	}
388252Svi117747 	/*
389252Svi117747 	 * Fail the remove if we are beyond listen, but can't send this
390252Svi117747 	 * to the peer.
391252Svi117747 	 */
392252Svi117747 	if (sctp->sctp_state > SCTPS_LISTEN) {
393252Svi117747 		if (!sctp_addip_enabled || !sctp->sctp_understands_asconf ||
394252Svi117747 		    !sctp->sctp_understands_addip) {
395252Svi117747 			if (!caller_hold_lock)
396252Svi117747 				WAKE_SCTP(sctp);
397252Svi117747 			return (EINVAL);
398252Svi117747 		}
3990Sstevel@tonic-gate 		do_asconf = B_TRUE;
400252Svi117747 	}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/* Can't delete the last address nor all of the addresses */
4030Sstevel@tonic-gate 	if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
4040Sstevel@tonic-gate 		if (!caller_hold_lock)
4050Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4060Sstevel@tonic-gate 		return (EINVAL);
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
409852Svi117747 	if (cl_sctp_unlisten != NULL && !do_asconf &&
410852Svi117747 	    sctp->sctp_state > SCTPS_BOUND) {
411852Svi117747 		usize = sizeof (in6_addr_t) * addrcnt;
412852Svi117747 		ulist = kmem_alloc(usize, KM_SLEEP);
413852Svi117747 	}
414852Svi117747 
415852Svi117747 	error = sctp_del_ip(sctp, addrs, addrcnt, ulist, usize);
416852Svi117747 	if (error != 0) {
417852Svi117747 		if (ulist != NULL)
418852Svi117747 			kmem_free(ulist, usize);
419852Svi117747 		if (!caller_hold_lock)
420852Svi117747 			WAKE_SCTP(sctp);
421852Svi117747 		return (error);
422852Svi117747 	}
423852Svi117747 	/* ulist will be non-NULL only if cl_sctp_unlisten is non-NULL */
424852Svi117747 	if (ulist != NULL) {
425852Svi117747 		ASSERT(cl_sctp_unlisten != NULL);
426852Svi117747 		(*cl_sctp_unlisten)(sctp->sctp_family, ulist, addrcnt,
427852Svi117747 		    sctp->sctp_lport);
428852Svi117747 		/* ulist will be freed by the clustering module */
429852Svi117747 	}
4300Sstevel@tonic-gate 	if (!caller_hold_lock)
4310Sstevel@tonic-gate 		WAKE_SCTP(sctp);
432852Svi117747 	if (do_asconf)
4330Sstevel@tonic-gate 		sctp_process_sendq(sctp);
4340Sstevel@tonic-gate 	return (error);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /*
438*1676Sjpk  * Returns 0 for success, errno value otherwise.
439*1676Sjpk  *
440*1676Sjpk  * If the "bind_to_req_port_only" parameter is set and the requested port
441*1676Sjpk  * number is available, then set allocated_port to it.  If not available,
442*1676Sjpk  * return an error.
4430Sstevel@tonic-gate  *
444*1676Sjpk  * If the "bind_to_req_port_only" parameter is not set and the requested port
445*1676Sjpk  * number is available, then set allocated_port to it.  If not available,
446*1676Sjpk  * find the first anonymous port we can and set allocated_port to that.  If no
447*1676Sjpk  * anonymous ports are available, return an error.
4480Sstevel@tonic-gate  *
449*1676Sjpk  * In either case, when succeeding, update the sctp_t to record the port number
4500Sstevel@tonic-gate  * and insert it in the bind hash table.
4510Sstevel@tonic-gate  */
452*1676Sjpk int
453*1676Sjpk sctp_bindi(sctp_t *sctp, in_port_t port, boolean_t bind_to_req_port_only,
454*1676Sjpk     int user_specified, in_port_t *allocated_port)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	/* number of times we have run around the loop */
4570Sstevel@tonic-gate 	int count = 0;
4580Sstevel@tonic-gate 	/* maximum number of times to run around the loop */
4590Sstevel@tonic-gate 	int loopmax;
4600Sstevel@tonic-gate 	zoneid_t zoneid = sctp->sctp_zoneid;
461*1676Sjpk 	zone_t *zone = crgetzone(sctp->sctp_credp);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/*
4640Sstevel@tonic-gate 	 * Lookup for free addresses is done in a loop and "loopmax"
4650Sstevel@tonic-gate 	 * influences how long we spin in the loop
4660Sstevel@tonic-gate 	 */
4670Sstevel@tonic-gate 	if (bind_to_req_port_only) {
4680Sstevel@tonic-gate 		/*
4690Sstevel@tonic-gate 		 * If the requested port is busy, don't bother to look
4700Sstevel@tonic-gate 		 * for a new one. Setting loop maximum count to 1 has
4710Sstevel@tonic-gate 		 * that effect.
4720Sstevel@tonic-gate 		 */
4730Sstevel@tonic-gate 		loopmax = 1;
4740Sstevel@tonic-gate 	} else {
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * If the requested port is busy, look for a free one
4770Sstevel@tonic-gate 		 * in the anonymous port range.
4780Sstevel@tonic-gate 		 * Set loopmax appropriately so that one does not look
4790Sstevel@tonic-gate 		 * forever in the case all of the anonymous ports are in use.
4800Sstevel@tonic-gate 		 */
4810Sstevel@tonic-gate 		loopmax = (sctp_largest_anon_port -
4820Sstevel@tonic-gate 		    sctp_smallest_anon_port + 1);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 	do {
4850Sstevel@tonic-gate 		uint16_t	lport;
4860Sstevel@tonic-gate 		sctp_tf_t	*tbf;
4870Sstevel@tonic-gate 		sctp_t		*lsctp;
4880Sstevel@tonic-gate 		int		addrcmp;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 		lport = htons(port);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		/*
4930Sstevel@tonic-gate 		 * Ensure that the sctp_t is not currently in the bind hash.
4940Sstevel@tonic-gate 		 * Hold the lock on the hash bucket to ensure that
4950Sstevel@tonic-gate 		 * the duplicate check plus the insertion is an atomic
4960Sstevel@tonic-gate 		 * operation.
4970Sstevel@tonic-gate 		 *
4980Sstevel@tonic-gate 		 * This function does an inline lookup on the bind hash list
4990Sstevel@tonic-gate 		 * Make sure that we access only members of sctp_t
5000Sstevel@tonic-gate 		 * and that we don't look at sctp_sctp, since we are not
5010Sstevel@tonic-gate 		 * doing a SCTPB_REFHOLD. For more details please see the notes
5020Sstevel@tonic-gate 		 * in sctp_compress()
5030Sstevel@tonic-gate 		 */
5040Sstevel@tonic-gate 		sctp_bind_hash_remove(sctp);
5050Sstevel@tonic-gate 		tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)];
5060Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
5070Sstevel@tonic-gate 		for (lsctp = tbf->tf_sctp; lsctp != NULL;
5080Sstevel@tonic-gate 		    lsctp = lsctp->sctp_bind_hash) {
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 			if (lport != lsctp->sctp_lport ||
5110Sstevel@tonic-gate 			    lsctp->sctp_state < SCTPS_BOUND)
5120Sstevel@tonic-gate 				continue;
5130Sstevel@tonic-gate 
514*1676Sjpk 			/*
515*1676Sjpk 			 * On a labeled system, we must treat bindings to ports
516*1676Sjpk 			 * on shared IP addresses by sockets with MAC exemption
517*1676Sjpk 			 * privilege as being in all zones, as there's
518*1676Sjpk 			 * otherwise no way to identify the right receiver.
519*1676Sjpk 			 */
520*1676Sjpk 			if (lsctp->sctp_zoneid != zoneid &&
521*1676Sjpk 			    !lsctp->sctp_mac_exempt && !sctp->sctp_mac_exempt)
522*1676Sjpk 				continue;
523*1676Sjpk 
5240Sstevel@tonic-gate 			addrcmp = sctp_compare_saddrs(sctp, lsctp);
5250Sstevel@tonic-gate 			if (addrcmp != SCTP_ADDR_DISJOINT) {
5260Sstevel@tonic-gate 				if (!sctp->sctp_reuseaddr) {
5270Sstevel@tonic-gate 					/* in use */
5280Sstevel@tonic-gate 					break;
5290Sstevel@tonic-gate 				} else if (lsctp->sctp_state == SCTPS_BOUND ||
5300Sstevel@tonic-gate 				    lsctp->sctp_state == SCTPS_LISTEN) {
5310Sstevel@tonic-gate 					/*
5320Sstevel@tonic-gate 					 * socket option SO_REUSEADDR is set
5330Sstevel@tonic-gate 					 * on the binding sctp_t.
5340Sstevel@tonic-gate 					 *
5350Sstevel@tonic-gate 					 * We have found a match of IP source
5360Sstevel@tonic-gate 					 * address and source port, which is
5370Sstevel@tonic-gate 					 * refused regardless of the
5380Sstevel@tonic-gate 					 * SO_REUSEADDR setting, so we break.
5390Sstevel@tonic-gate 					 */
5400Sstevel@tonic-gate 					break;
5410Sstevel@tonic-gate 				}
5420Sstevel@tonic-gate 			}
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 		if (lsctp != NULL) {
5450Sstevel@tonic-gate 			/* The port number is busy */
5460Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
5470Sstevel@tonic-gate 		} else {
548*1676Sjpk 			conn_t *connp = sctp->sctp_connp;
549*1676Sjpk 
550*1676Sjpk 			if (is_system_labeled()) {
551*1676Sjpk 				mlp_type_t addrtype, mlptype;
552*1676Sjpk 
553*1676Sjpk 				/*
554*1676Sjpk 				 * On a labeled system we must check the type
555*1676Sjpk 				 * of the binding requested by the user (either
556*1676Sjpk 				 * MLP or SLP on shared and private addresses),
557*1676Sjpk 				 * and that the user's requested binding
558*1676Sjpk 				 * is permitted.
559*1676Sjpk 				 */
560*1676Sjpk 				addrtype = tsol_mlp_addr_type(zone->zone_id,
561*1676Sjpk 				    sctp->sctp_ipversion,
562*1676Sjpk 				    sctp->sctp_ipversion == IPV4_VERSION ?
563*1676Sjpk 				    (void *)&sctp->sctp_ipha->ipha_src :
564*1676Sjpk 				    (void *)&sctp->sctp_ip6h->ip6_src);
565*1676Sjpk 
566*1676Sjpk 				/*
567*1676Sjpk 				 * tsol_mlp_addr_type returns the possibilities
568*1676Sjpk 				 * for the selected address.  Since all local
569*1676Sjpk 				 * addresses are either private or shared, the
570*1676Sjpk 				 * return value mlptSingle means "local address
571*1676Sjpk 				 * not valid (interface not present)."
572*1676Sjpk 				 */
573*1676Sjpk 				if (addrtype == mlptSingle) {
574*1676Sjpk 					mutex_exit(&tbf->tf_lock);
575*1676Sjpk 					return (EADDRNOTAVAIL);
576*1676Sjpk 				}
577*1676Sjpk 				mlptype = tsol_mlp_port_type(zone, IPPROTO_SCTP,
578*1676Sjpk 				    port, addrtype);
579*1676Sjpk 				if (mlptype != mlptSingle) {
580*1676Sjpk 					if (secpolicy_net_bindmlp(connp->
581*1676Sjpk 					    conn_cred) != 0) {
582*1676Sjpk 						mutex_exit(&tbf->tf_lock);
583*1676Sjpk 						return (EACCES);
584*1676Sjpk 					}
585*1676Sjpk 					/*
586*1676Sjpk 					 * If we're binding a shared MLP, then
587*1676Sjpk 					 * make sure that this zone is the one
588*1676Sjpk 					 * that owns that MLP.  Shared MLPs can
589*1676Sjpk 					 * be owned by at most one zone.
590*1676Sjpk 					 */
591*1676Sjpk 
592*1676Sjpk 					if (mlptype == mlptShared &&
593*1676Sjpk 					    addrtype == mlptShared &&
594*1676Sjpk 					    connp->conn_zoneid !=
595*1676Sjpk 					    tsol_mlp_findzone(IPPROTO_SCTP,
596*1676Sjpk 					    lport)) {
597*1676Sjpk 						mutex_exit(&tbf->tf_lock);
598*1676Sjpk 						return (EACCES);
599*1676Sjpk 					}
600*1676Sjpk 					connp->conn_mlp_type = mlptype;
601*1676Sjpk 				}
602*1676Sjpk 			}
6030Sstevel@tonic-gate 			/*
6040Sstevel@tonic-gate 			 * This port is ours. Insert in fanout and mark as
6050Sstevel@tonic-gate 			 * bound to prevent others from getting the port
6060Sstevel@tonic-gate 			 * number.
6070Sstevel@tonic-gate 			 */
6080Sstevel@tonic-gate 			sctp->sctp_state = SCTPS_BOUND;
6090Sstevel@tonic-gate 			sctp->sctp_lport = lport;
610*1676Sjpk 			sctp->sctp_sctph->sh_sport = lport;
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 			ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf);
6130Sstevel@tonic-gate 			sctp_bind_hash_insert(tbf, sctp, 1);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 			/*
6180Sstevel@tonic-gate 			 * We don't want sctp_next_port_to_try to "inherit"
6190Sstevel@tonic-gate 			 * a port number supplied by the user in a bind.
620*1676Sjpk 			 *
6210Sstevel@tonic-gate 			 * This is the only place where sctp_next_port_to_try
6220Sstevel@tonic-gate 			 * is updated. After the update, it may or may not
6230Sstevel@tonic-gate 			 * be in the valid range.
6240Sstevel@tonic-gate 			 */
625*1676Sjpk 			if (user_specified == 0)
626*1676Sjpk 				sctp_next_port_to_try = port + 1;
627*1676Sjpk 
628*1676Sjpk 			*allocated_port = port;
629*1676Sjpk 
630*1676Sjpk 			return (0);
6310Sstevel@tonic-gate 		}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		if ((count == 0) && (user_specified)) {
6340Sstevel@tonic-gate 			/*
6350Sstevel@tonic-gate 			 * We may have to return an anonymous port. So
6360Sstevel@tonic-gate 			 * get one to start with.
6370Sstevel@tonic-gate 			 */
638*1676Sjpk 			port = sctp_update_next_port(sctp_next_port_to_try,
639*1676Sjpk 			    zone);
6400Sstevel@tonic-gate 			user_specified = 0;
6410Sstevel@tonic-gate 		} else {
642*1676Sjpk 			port = sctp_update_next_port(port + 1, zone);
6430Sstevel@tonic-gate 		}
644*1676Sjpk 		if (port == 0)
645*1676Sjpk 			break;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		/*
6480Sstevel@tonic-gate 		 * Don't let this loop run forever in the case where
6490Sstevel@tonic-gate 		 * all of the anonymous ports are in use.
6500Sstevel@tonic-gate 		 */
6510Sstevel@tonic-gate 	} while (++count < loopmax);
652*1676Sjpk 
653*1676Sjpk 	return (bind_to_req_port_only ? EADDRINUSE : EADDRNOTAVAIL);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate  * Don't let port fall into the privileged range.
6580Sstevel@tonic-gate  * Since the extra privileged ports can be arbitrary we also
6590Sstevel@tonic-gate  * ensure that we exclude those from consideration.
6600Sstevel@tonic-gate  * sctp_g_epriv_ports is not sorted thus we loop over it until
6610Sstevel@tonic-gate  * there are no changes.
6620Sstevel@tonic-gate  *
6630Sstevel@tonic-gate  * Note: No locks are held when inspecting sctp_g_*epriv_ports
6640Sstevel@tonic-gate  * but instead the code relies on:
6650Sstevel@tonic-gate  * - the fact that the address of the array and its size never changes
6660Sstevel@tonic-gate  * - the atomic assignment of the elements of the array
6670Sstevel@tonic-gate  */
6680Sstevel@tonic-gate in_port_t
669*1676Sjpk sctp_update_next_port(in_port_t port, zone_t *zone)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	int i;
672*1676Sjpk 	boolean_t restart = B_FALSE;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate retry:
675*1676Sjpk 	if (port < sctp_smallest_anon_port)
6760Sstevel@tonic-gate 		port = sctp_smallest_anon_port;
6770Sstevel@tonic-gate 
678*1676Sjpk 	if (port > sctp_largest_anon_port) {
679*1676Sjpk 		if (restart)
680*1676Sjpk 			return (0);
681*1676Sjpk 		restart = B_TRUE;
682*1676Sjpk 		port = sctp_smallest_anon_port;
683*1676Sjpk 	}
684*1676Sjpk 
6850Sstevel@tonic-gate 	if (port < sctp_smallest_nonpriv_port)
6860Sstevel@tonic-gate 		port = sctp_smallest_nonpriv_port;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	for (i = 0; i < sctp_g_num_epriv_ports; i++) {
6890Sstevel@tonic-gate 		if (port == sctp_g_epriv_ports[i]) {
6900Sstevel@tonic-gate 			port++;
6910Sstevel@tonic-gate 			/*
6920Sstevel@tonic-gate 			 * Make sure whether the port is in the
6930Sstevel@tonic-gate 			 * valid range.
6940Sstevel@tonic-gate 			 *
6950Sstevel@tonic-gate 			 * XXX Note that if sctp_g_epriv_ports contains
6960Sstevel@tonic-gate 			 * all the anonymous ports this will be an
6970Sstevel@tonic-gate 			 * infinite loop.
6980Sstevel@tonic-gate 			 */
6990Sstevel@tonic-gate 			goto retry;
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 	}
702*1676Sjpk 
703*1676Sjpk 	if (is_system_labeled() &&
704*1676Sjpk 	    (i = tsol_next_port(zone, port, IPPROTO_SCTP, B_TRUE)) != 0) {
705*1676Sjpk 		port = i;
706*1676Sjpk 		goto retry;
707*1676Sjpk 	}
708*1676Sjpk 
7090Sstevel@tonic-gate 	return (port);
7100Sstevel@tonic-gate }
711