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 /* 23*10352Sdanmcd@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <sys/types.h> 280Sstevel@tonic-gate #include <sys/systm.h> 290Sstevel@tonic-gate #include <sys/stream.h> 300Sstevel@tonic-gate #include <sys/cmn_err.h> 310Sstevel@tonic-gate #include <sys/kmem.h> 320Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 330Sstevel@tonic-gate #include <sys/tihdr.h> 340Sstevel@tonic-gate #include <sys/stropts.h> 350Sstevel@tonic-gate #include <sys/socket.h> 360Sstevel@tonic-gate #include <sys/random.h> 370Sstevel@tonic-gate #include <sys/policy.h> 381676Sjpk #include <sys/tsol/tndb.h> 391676Sjpk #include <sys/tsol/tnet.h> 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include <netinet/in.h> 420Sstevel@tonic-gate #include <netinet/ip6.h> 430Sstevel@tonic-gate 440Sstevel@tonic-gate #include <inet/common.h> 450Sstevel@tonic-gate #include <inet/ip.h> 460Sstevel@tonic-gate #include <inet/ip6.h> 470Sstevel@tonic-gate #include <inet/ipclassifier.h> 480Sstevel@tonic-gate #include "sctp_impl.h" 490Sstevel@tonic-gate #include "sctp_asconf.h" 500Sstevel@tonic-gate #include "sctp_addr.h" 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * Returns 0 on success, EACCES on permission failure. 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate static int 560Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified) 570Sstevel@tonic-gate { 583448Sdh155122 sctp_stack_t *sctps = sctp->sctp_sctps; 593448Sdh155122 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * Get a valid port (within the anonymous range and should not 620Sstevel@tonic-gate * be a privileged one) to use if the user has not given a port. 630Sstevel@tonic-gate * If multiple threads are here, they may all start with 640Sstevel@tonic-gate * with the same initial port. But, it should be fine as long as 650Sstevel@tonic-gate * sctp_bindi will ensure that no two threads will be assigned 660Sstevel@tonic-gate * the same port. 670Sstevel@tonic-gate */ 680Sstevel@tonic-gate if (*requested_port == 0) { 693448Sdh155122 *requested_port = sctp_update_next_port( 703448Sdh155122 sctps->sctps_next_port_to_try, 713448Sdh155122 crgetzone(sctp->sctp_credp), sctps); 721676Sjpk if (*requested_port == 0) 731676Sjpk 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 */ 883448Sdh155122 if (*requested_port < sctps->sctps_smallest_nonpriv_port) { 890Sstevel@tonic-gate priv = B_TRUE; 900Sstevel@tonic-gate } else { 913448Sdh155122 for (i = 0; i < sctps->sctps_g_num_epriv_ports; i++) { 923448Sdh155122 if (*requested_port == 933448Sdh155122 sctps->sctps_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, 1056134Scasper *requested_port, IPPROTO_SCTP) != 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; 1223448Sdh155122 sctp_stack_t *sctps = sctp->sctp_sctps; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate RUN_SCTP(sctp); 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * TCP handles listen() increasing the backlog, need to check 127852Svi117747 * if it should be handled here too 1280Sstevel@tonic-gate */ 1294505Skcpoon if (sctp->sctp_state > SCTPS_BOUND || 1304505Skcpoon (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) { 1310Sstevel@tonic-gate WAKE_SCTP(sctp); 1320Sstevel@tonic-gate return (EINVAL); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate /* Do an anonymous bind for unbound socket doing listen(). */ 1360Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 0) { 1370Sstevel@tonic-gate struct sockaddr_storage ss; 1380Sstevel@tonic-gate int ret; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate bzero(&ss, sizeof (ss)); 1410Sstevel@tonic-gate ss.ss_family = sctp->sctp_family; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate WAKE_SCTP(sctp); 1440Sstevel@tonic-gate if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss, 1454505Skcpoon sizeof (ss))) != 0) 1460Sstevel@tonic-gate return (ret); 1470Sstevel@tonic-gate RUN_SCTP(sctp) 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate sctp->sctp_state = SCTPS_LISTEN; 1510Sstevel@tonic-gate (void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN); 1520Sstevel@tonic-gate sctp->sctp_last_secret_update = lbolt64; 1530Sstevel@tonic-gate bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN); 1543448Sdh155122 tf = &sctps->sctps_listen_fanout[SCTP_LISTEN_HASH( 1554505Skcpoon ntohs(sctp->sctp_lport))]; 1560Sstevel@tonic-gate sctp_listen_hash_insert(tf, sctp); 1570Sstevel@tonic-gate WAKE_SCTP(sctp); 1580Sstevel@tonic-gate return (0); 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* 1620Sstevel@tonic-gate * Bind the sctp_t to a sockaddr, which includes an address and other 1630Sstevel@tonic-gate * information, such as port or flowinfo. 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate int 1660Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate int user_specified; 1690Sstevel@tonic-gate boolean_t bind_to_req_port_only; 1700Sstevel@tonic-gate in_port_t requested_port; 1710Sstevel@tonic-gate in_port_t allocated_port; 1720Sstevel@tonic-gate int err = 0; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate ASSERT(sctp != NULL); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate RUN_SCTP(sctp); 1770Sstevel@tonic-gate 1788348SEric.Yu@Sun.COM if ((sctp->sctp_state >= SCTPS_BOUND) || 1798348SEric.Yu@Sun.COM (sctp->sctp_connp->conn_state_flags & CONN_CLOSING) || 1808348SEric.Yu@Sun.COM (sa == NULL || len == 0)) { 1818348SEric.Yu@Sun.COM /* 1828348SEric.Yu@Sun.COM * Multiple binds not allowed for any SCTP socket 1838348SEric.Yu@Sun.COM * Also binding with null address is not supported. 1848348SEric.Yu@Sun.COM */ 1850Sstevel@tonic-gate err = EINVAL; 1860Sstevel@tonic-gate goto done; 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate switch (sa->sa_family) { 1900Sstevel@tonic-gate case AF_INET: 1910Sstevel@tonic-gate if (len < sizeof (struct sockaddr_in) || 1920Sstevel@tonic-gate sctp->sctp_family == AF_INET6) { 1930Sstevel@tonic-gate err = EINVAL; 1940Sstevel@tonic-gate goto done; 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port); 1970Sstevel@tonic-gate break; 1980Sstevel@tonic-gate case AF_INET6: 1990Sstevel@tonic-gate if (len < sizeof (struct sockaddr_in6) || 2000Sstevel@tonic-gate sctp->sctp_family == AF_INET) { 2010Sstevel@tonic-gate err = EINVAL; 2020Sstevel@tonic-gate goto done; 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port); 2050Sstevel@tonic-gate /* Set the flowinfo. */ 2060Sstevel@tonic-gate sctp->sctp_ip6h->ip6_vcf = 2070Sstevel@tonic-gate (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 2080Sstevel@tonic-gate (((struct sockaddr_in6 *)sa)->sin6_flowinfo & 2090Sstevel@tonic-gate ~IPV6_VERS_AND_FLOW_MASK); 2100Sstevel@tonic-gate break; 2110Sstevel@tonic-gate default: 2120Sstevel@tonic-gate err = EAFNOSUPPORT; 2130Sstevel@tonic-gate goto done; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE; 2160Sstevel@tonic-gate 2171676Sjpk err = sctp_select_port(sctp, &requested_port, &user_specified); 2181676Sjpk if (err != 0) 2190Sstevel@tonic-gate goto done; 2200Sstevel@tonic-gate 221852Svi117747 if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE, 222852Svi117747 user_specified == 1 ? htons(requested_port) : 0)) != 0) { 2230Sstevel@tonic-gate goto done; 224852Svi117747 } 2251676Sjpk err = sctp_bindi(sctp, requested_port, bind_to_req_port_only, 2261676Sjpk user_specified, &allocated_port); 2271676Sjpk if (err != 0) { 2280Sstevel@tonic-gate sctp_free_saddrs(sctp); 2291676Sjpk } else { 2301676Sjpk ASSERT(sctp->sctp_state == SCTPS_BOUND); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate done: 2330Sstevel@tonic-gate WAKE_SCTP(sctp); 2340Sstevel@tonic-gate return (err); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * Perform bind/unbind operation of a list of addresses on a sctp_t 2390Sstevel@tonic-gate */ 2400Sstevel@tonic-gate int 2410Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop) 2420Sstevel@tonic-gate { 2430Sstevel@tonic-gate ASSERT(sctp != NULL); 2440Sstevel@tonic-gate ASSERT(addrs != NULL); 2450Sstevel@tonic-gate ASSERT(addrcnt > 0); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate switch (bindop) { 2480Sstevel@tonic-gate case SCTP_BINDX_ADD_ADDR: 249852Svi117747 return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE, 250852Svi117747 sctp->sctp_lport)); 2510Sstevel@tonic-gate case SCTP_BINDX_REM_ADDR: 2520Sstevel@tonic-gate return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE)); 2530Sstevel@tonic-gate default: 2540Sstevel@tonic-gate return (EINVAL); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Add a list of addresses to a sctp_t. 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate int 2620Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 263852Svi117747 boolean_t caller_hold_lock, in_port_t port) 2640Sstevel@tonic-gate { 2650Sstevel@tonic-gate int err = 0; 2660Sstevel@tonic-gate boolean_t do_asconf = B_FALSE; 2673448Sdh155122 sctp_stack_t *sctps = sctp->sctp_sctps; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate if (!caller_hold_lock) 2700Sstevel@tonic-gate RUN_SCTP(sctp); 2710Sstevel@tonic-gate 2724505Skcpoon if (sctp->sctp_state > SCTPS_ESTABLISHED || 2734505Skcpoon (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) { 2740Sstevel@tonic-gate if (!caller_hold_lock) 2750Sstevel@tonic-gate WAKE_SCTP(sctp); 2760Sstevel@tonic-gate return (EINVAL); 2770Sstevel@tonic-gate } 278252Svi117747 279252Svi117747 if (sctp->sctp_state > SCTPS_LISTEN) { 280252Svi117747 /* 281252Svi117747 * Let's do some checking here rather than undoing the 282252Svi117747 * add later (for these reasons). 283252Svi117747 */ 2843448Sdh155122 if (!sctps->sctps_addip_enabled || 2853448Sdh155122 !sctp->sctp_understands_asconf || 286252Svi117747 !sctp->sctp_understands_addip) { 287252Svi117747 if (!caller_hold_lock) 288252Svi117747 WAKE_SCTP(sctp); 289252Svi117747 return (EINVAL); 290252Svi117747 } 2910Sstevel@tonic-gate do_asconf = B_TRUE; 292252Svi117747 } 293852Svi117747 /* 294852Svi117747 * On a clustered node, for an inaddr_any bind, we will pass the list 295852Svi117747 * of all the addresses in the global list, minus any address on the 296852Svi117747 * loopback interface, and expect the clustering susbsystem to give us 297852Svi117747 * the correct list for the 'port'. For explicit binds we give the 298852Svi117747 * list of addresses and the clustering module validates it for the 299852Svi117747 * 'port'. 300852Svi117747 * 301852Svi117747 * On a non-clustered node, cl_sctp_check_addrs will be NULL and 302852Svi117747 * we proceed as usual. 303852Svi117747 */ 304852Svi117747 if (cl_sctp_check_addrs != NULL) { 305852Svi117747 uchar_t *addrlist = NULL; 306852Svi117747 size_t size = 0; 307852Svi117747 int unspec = 0; 308852Svi117747 boolean_t do_listen; 309852Svi117747 uchar_t *llist = NULL; 310852Svi117747 size_t lsize = 0; 311852Svi117747 312852Svi117747 /* 313852Svi117747 * If we are adding addresses after listening, but before 314852Svi117747 * an association is established, we need to update the 315852Svi117747 * clustering module with this info. 316852Svi117747 */ 317852Svi117747 do_listen = !do_asconf && sctp->sctp_state > SCTPS_BOUND && 318852Svi117747 cl_sctp_listen != NULL; 319852Svi117747 320852Svi117747 err = sctp_get_addrlist(sctp, addrs, &addrcnt, &addrlist, 321852Svi117747 &unspec, &size); 322852Svi117747 if (err != 0) { 323852Svi117747 ASSERT(addrlist == NULL); 324852Svi117747 ASSERT(addrcnt == 0); 325852Svi117747 ASSERT(size == 0); 326852Svi117747 if (!caller_hold_lock) 327852Svi117747 WAKE_SCTP(sctp); 3283448Sdh155122 SCTP_KSTAT(sctps, sctp_cl_check_addrs); 329852Svi117747 return (err); 330852Svi117747 } 331852Svi117747 ASSERT(addrlist != NULL); 332852Svi117747 (*cl_sctp_check_addrs)(sctp->sctp_family, port, &addrlist, 333852Svi117747 size, &addrcnt, unspec == 1); 334852Svi117747 if (addrcnt == 0) { 335852Svi117747 /* We free the list */ 336852Svi117747 kmem_free(addrlist, size); 337852Svi117747 if (!caller_hold_lock) 338852Svi117747 WAKE_SCTP(sctp); 339852Svi117747 return (EINVAL); 340852Svi117747 } 341852Svi117747 if (do_listen) { 342852Svi117747 lsize = sizeof (in6_addr_t) * addrcnt; 343852Svi117747 llist = kmem_alloc(lsize, KM_SLEEP); 344852Svi117747 } 345852Svi117747 err = sctp_valid_addr_list(sctp, addrlist, addrcnt, llist, 346852Svi117747 lsize); 347852Svi117747 if (err == 0 && do_listen) { 348852Svi117747 (*cl_sctp_listen)(sctp->sctp_family, llist, 349852Svi117747 addrcnt, sctp->sctp_lport); 350852Svi117747 /* list will be freed by the clustering module */ 351852Svi117747 } else if (err != 0 && llist != NULL) { 352852Svi117747 kmem_free(llist, lsize); 353852Svi117747 } 354852Svi117747 /* free the list we allocated */ 355852Svi117747 kmem_free(addrlist, size); 356852Svi117747 } else { 357852Svi117747 err = sctp_valid_addr_list(sctp, addrs, addrcnt, NULL, 0); 358852Svi117747 } 3590Sstevel@tonic-gate if (err != 0) { 3600Sstevel@tonic-gate if (!caller_hold_lock) 3610Sstevel@tonic-gate WAKE_SCTP(sctp); 3620Sstevel@tonic-gate return (err); 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate /* Need to send ASCONF messages */ 3650Sstevel@tonic-gate if (do_asconf) { 3660Sstevel@tonic-gate err = sctp_add_ip(sctp, addrs, addrcnt); 3670Sstevel@tonic-gate if (err != 0) { 3680Sstevel@tonic-gate sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE); 3690Sstevel@tonic-gate if (!caller_hold_lock) 3700Sstevel@tonic-gate WAKE_SCTP(sctp); 3710Sstevel@tonic-gate return (err); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate if (!caller_hold_lock) 3750Sstevel@tonic-gate WAKE_SCTP(sctp); 3760Sstevel@tonic-gate if (do_asconf) 3770Sstevel@tonic-gate sctp_process_sendq(sctp); 3780Sstevel@tonic-gate return (0); 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * Remove one or more addresses bound to the sctp_t. 3830Sstevel@tonic-gate */ 3840Sstevel@tonic-gate int 3850Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 3860Sstevel@tonic-gate boolean_t caller_hold_lock) 3870Sstevel@tonic-gate { 3880Sstevel@tonic-gate int error = 0; 3890Sstevel@tonic-gate boolean_t do_asconf = B_FALSE; 390852Svi117747 uchar_t *ulist = NULL; 391852Svi117747 size_t usize = 0; 3923448Sdh155122 sctp_stack_t *sctps = sctp->sctp_sctps; 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate if (!caller_hold_lock) 3950Sstevel@tonic-gate RUN_SCTP(sctp); 3960Sstevel@tonic-gate 3974505Skcpoon if (sctp->sctp_state > SCTPS_ESTABLISHED || 3984505Skcpoon (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) { 3990Sstevel@tonic-gate if (!caller_hold_lock) 4000Sstevel@tonic-gate WAKE_SCTP(sctp); 4010Sstevel@tonic-gate return (EINVAL); 4020Sstevel@tonic-gate } 403252Svi117747 /* 404252Svi117747 * Fail the remove if we are beyond listen, but can't send this 405252Svi117747 * to the peer. 406252Svi117747 */ 407252Svi117747 if (sctp->sctp_state > SCTPS_LISTEN) { 4083448Sdh155122 if (!sctps->sctps_addip_enabled || 4093448Sdh155122 !sctp->sctp_understands_asconf || 410252Svi117747 !sctp->sctp_understands_addip) { 411252Svi117747 if (!caller_hold_lock) 412252Svi117747 WAKE_SCTP(sctp); 413252Svi117747 return (EINVAL); 414252Svi117747 } 4150Sstevel@tonic-gate do_asconf = B_TRUE; 416252Svi117747 } 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate /* Can't delete the last address nor all of the addresses */ 4190Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) { 4200Sstevel@tonic-gate if (!caller_hold_lock) 4210Sstevel@tonic-gate WAKE_SCTP(sctp); 4220Sstevel@tonic-gate return (EINVAL); 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 425852Svi117747 if (cl_sctp_unlisten != NULL && !do_asconf && 426852Svi117747 sctp->sctp_state > SCTPS_BOUND) { 427852Svi117747 usize = sizeof (in6_addr_t) * addrcnt; 428852Svi117747 ulist = kmem_alloc(usize, KM_SLEEP); 429852Svi117747 } 430852Svi117747 431852Svi117747 error = sctp_del_ip(sctp, addrs, addrcnt, ulist, usize); 432852Svi117747 if (error != 0) { 433852Svi117747 if (ulist != NULL) 434852Svi117747 kmem_free(ulist, usize); 435852Svi117747 if (!caller_hold_lock) 436852Svi117747 WAKE_SCTP(sctp); 437852Svi117747 return (error); 438852Svi117747 } 439852Svi117747 /* ulist will be non-NULL only if cl_sctp_unlisten is non-NULL */ 440852Svi117747 if (ulist != NULL) { 441852Svi117747 ASSERT(cl_sctp_unlisten != NULL); 442852Svi117747 (*cl_sctp_unlisten)(sctp->sctp_family, ulist, addrcnt, 443852Svi117747 sctp->sctp_lport); 444852Svi117747 /* ulist will be freed by the clustering module */ 445852Svi117747 } 4460Sstevel@tonic-gate if (!caller_hold_lock) 4470Sstevel@tonic-gate WAKE_SCTP(sctp); 448852Svi117747 if (do_asconf) 4490Sstevel@tonic-gate sctp_process_sendq(sctp); 4500Sstevel@tonic-gate return (error); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate /* 4541676Sjpk * Returns 0 for success, errno value otherwise. 4551676Sjpk * 4561676Sjpk * If the "bind_to_req_port_only" parameter is set and the requested port 4571676Sjpk * number is available, then set allocated_port to it. If not available, 4581676Sjpk * return an error. 4590Sstevel@tonic-gate * 4601676Sjpk * If the "bind_to_req_port_only" parameter is not set and the requested port 4611676Sjpk * number is available, then set allocated_port to it. If not available, 4621676Sjpk * find the first anonymous port we can and set allocated_port to that. If no 4631676Sjpk * anonymous ports are available, return an error. 4640Sstevel@tonic-gate * 4651676Sjpk * In either case, when succeeding, update the sctp_t to record the port number 4660Sstevel@tonic-gate * and insert it in the bind hash table. 4670Sstevel@tonic-gate */ 4681676Sjpk int 4691676Sjpk sctp_bindi(sctp_t *sctp, in_port_t port, boolean_t bind_to_req_port_only, 4701676Sjpk int user_specified, in_port_t *allocated_port) 4710Sstevel@tonic-gate { 4720Sstevel@tonic-gate /* number of times we have run around the loop */ 4730Sstevel@tonic-gate int count = 0; 4740Sstevel@tonic-gate /* maximum number of times to run around the loop */ 4750Sstevel@tonic-gate int loopmax; 4760Sstevel@tonic-gate zoneid_t zoneid = sctp->sctp_zoneid; 4771676Sjpk zone_t *zone = crgetzone(sctp->sctp_credp); 4783448Sdh155122 sctp_stack_t *sctps = sctp->sctp_sctps; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * Lookup for free addresses is done in a loop and "loopmax" 4820Sstevel@tonic-gate * influences how long we spin in the loop 4830Sstevel@tonic-gate */ 4840Sstevel@tonic-gate if (bind_to_req_port_only) { 4850Sstevel@tonic-gate /* 4860Sstevel@tonic-gate * If the requested port is busy, don't bother to look 4870Sstevel@tonic-gate * for a new one. Setting loop maximum count to 1 has 4880Sstevel@tonic-gate * that effect. 4890Sstevel@tonic-gate */ 4900Sstevel@tonic-gate loopmax = 1; 4910Sstevel@tonic-gate } else { 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * If the requested port is busy, look for a free one 4940Sstevel@tonic-gate * in the anonymous port range. 4950Sstevel@tonic-gate * Set loopmax appropriately so that one does not look 4960Sstevel@tonic-gate * forever in the case all of the anonymous ports are in use. 4970Sstevel@tonic-gate */ 4983448Sdh155122 loopmax = (sctps->sctps_largest_anon_port - 4993448Sdh155122 sctps->sctps_smallest_anon_port + 1); 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate do { 5020Sstevel@tonic-gate uint16_t lport; 5030Sstevel@tonic-gate sctp_tf_t *tbf; 5040Sstevel@tonic-gate sctp_t *lsctp; 5050Sstevel@tonic-gate int addrcmp; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate lport = htons(port); 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate /* 5100Sstevel@tonic-gate * Ensure that the sctp_t is not currently in the bind hash. 5110Sstevel@tonic-gate * Hold the lock on the hash bucket to ensure that 5120Sstevel@tonic-gate * the duplicate check plus the insertion is an atomic 5130Sstevel@tonic-gate * operation. 5140Sstevel@tonic-gate * 5150Sstevel@tonic-gate * This function does an inline lookup on the bind hash list 5160Sstevel@tonic-gate * Make sure that we access only members of sctp_t 5170Sstevel@tonic-gate * and that we don't look at sctp_sctp, since we are not 5180Sstevel@tonic-gate * doing a SCTPB_REFHOLD. For more details please see the notes 5190Sstevel@tonic-gate * in sctp_compress() 5200Sstevel@tonic-gate */ 5210Sstevel@tonic-gate sctp_bind_hash_remove(sctp); 5223448Sdh155122 tbf = &sctps->sctps_bind_fanout[SCTP_BIND_HASH(port)]; 5230Sstevel@tonic-gate mutex_enter(&tbf->tf_lock); 5240Sstevel@tonic-gate for (lsctp = tbf->tf_sctp; lsctp != NULL; 5250Sstevel@tonic-gate lsctp = lsctp->sctp_bind_hash) { 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate if (lport != lsctp->sctp_lport || 5280Sstevel@tonic-gate lsctp->sctp_state < SCTPS_BOUND) 5290Sstevel@tonic-gate continue; 5300Sstevel@tonic-gate 5311676Sjpk /* 5321676Sjpk * On a labeled system, we must treat bindings to ports 5331676Sjpk * on shared IP addresses by sockets with MAC exemption 5341676Sjpk * privilege as being in all zones, as there's 5351676Sjpk * otherwise no way to identify the right receiver. 5361676Sjpk */ 5371676Sjpk if (lsctp->sctp_zoneid != zoneid && 5381676Sjpk !lsctp->sctp_mac_exempt && !sctp->sctp_mac_exempt) 5391676Sjpk continue; 5401676Sjpk 5410Sstevel@tonic-gate addrcmp = sctp_compare_saddrs(sctp, lsctp); 5420Sstevel@tonic-gate if (addrcmp != SCTP_ADDR_DISJOINT) { 5430Sstevel@tonic-gate if (!sctp->sctp_reuseaddr) { 5440Sstevel@tonic-gate /* in use */ 5450Sstevel@tonic-gate break; 5460Sstevel@tonic-gate } else if (lsctp->sctp_state == SCTPS_BOUND || 5470Sstevel@tonic-gate lsctp->sctp_state == SCTPS_LISTEN) { 5480Sstevel@tonic-gate /* 5490Sstevel@tonic-gate * socket option SO_REUSEADDR is set 5500Sstevel@tonic-gate * on the binding sctp_t. 5510Sstevel@tonic-gate * 5520Sstevel@tonic-gate * We have found a match of IP source 5530Sstevel@tonic-gate * address and source port, which is 5540Sstevel@tonic-gate * refused regardless of the 5550Sstevel@tonic-gate * SO_REUSEADDR setting, so we break. 5560Sstevel@tonic-gate */ 5570Sstevel@tonic-gate break; 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate if (lsctp != NULL) { 5620Sstevel@tonic-gate /* The port number is busy */ 5630Sstevel@tonic-gate mutex_exit(&tbf->tf_lock); 5640Sstevel@tonic-gate } else { 5651676Sjpk conn_t *connp = sctp->sctp_connp; 5661676Sjpk 5671676Sjpk if (is_system_labeled()) { 5681676Sjpk mlp_type_t addrtype, mlptype; 5691676Sjpk 5701676Sjpk /* 5711676Sjpk * On a labeled system we must check the type 5721676Sjpk * of the binding requested by the user (either 5731676Sjpk * MLP or SLP on shared and private addresses), 5741676Sjpk * and that the user's requested binding 5751676Sjpk * is permitted. 5761676Sjpk */ 577*10352Sdanmcd@sun.com addrtype = tsol_mlp_addr_type( 578*10352Sdanmcd@sun.com connp->conn_allzones ? ALL_ZONES : 579*10352Sdanmcd@sun.com zone->zone_id, 5801676Sjpk sctp->sctp_ipversion, 5811676Sjpk sctp->sctp_ipversion == IPV4_VERSION ? 5821676Sjpk (void *)&sctp->sctp_ipha->ipha_src : 5833448Sdh155122 (void *)&sctp->sctp_ip6h->ip6_src, 5843448Sdh155122 sctps->sctps_netstack->netstack_ip); 5851676Sjpk 5861676Sjpk /* 5871676Sjpk * tsol_mlp_addr_type returns the possibilities 5881676Sjpk * for the selected address. Since all local 5891676Sjpk * addresses are either private or shared, the 5901676Sjpk * return value mlptSingle means "local address 5911676Sjpk * not valid (interface not present)." 5921676Sjpk */ 5931676Sjpk if (addrtype == mlptSingle) { 5941676Sjpk mutex_exit(&tbf->tf_lock); 5951676Sjpk return (EADDRNOTAVAIL); 5961676Sjpk } 5971676Sjpk mlptype = tsol_mlp_port_type(zone, IPPROTO_SCTP, 5981676Sjpk port, addrtype); 5991676Sjpk if (mlptype != mlptSingle) { 6001676Sjpk if (secpolicy_net_bindmlp(connp-> 6011676Sjpk conn_cred) != 0) { 6021676Sjpk mutex_exit(&tbf->tf_lock); 6031676Sjpk return (EACCES); 6041676Sjpk } 6051676Sjpk /* 6061676Sjpk * If we're binding a shared MLP, then 6071676Sjpk * make sure that this zone is the one 6081676Sjpk * that owns that MLP. Shared MLPs can 6091676Sjpk * be owned by at most one zone. 6103448Sdh155122 * 6113448Sdh155122 * No need to handle exclusive-stack 6123448Sdh155122 * zones since ALL_ZONES only applies 6133448Sdh155122 * to the shared stack. 6141676Sjpk */ 6151676Sjpk 6161676Sjpk if (mlptype == mlptShared && 6171676Sjpk addrtype == mlptShared && 6181676Sjpk connp->conn_zoneid != 6191676Sjpk tsol_mlp_findzone(IPPROTO_SCTP, 6201676Sjpk lport)) { 6211676Sjpk mutex_exit(&tbf->tf_lock); 6221676Sjpk return (EACCES); 6231676Sjpk } 6241676Sjpk connp->conn_mlp_type = mlptype; 6251676Sjpk } 6261676Sjpk } 6270Sstevel@tonic-gate /* 6280Sstevel@tonic-gate * This port is ours. Insert in fanout and mark as 6290Sstevel@tonic-gate * bound to prevent others from getting the port 6300Sstevel@tonic-gate * number. 6310Sstevel@tonic-gate */ 6320Sstevel@tonic-gate sctp->sctp_state = SCTPS_BOUND; 6330Sstevel@tonic-gate sctp->sctp_lport = lport; 6341676Sjpk sctp->sctp_sctph->sh_sport = lport; 6350Sstevel@tonic-gate 6363448Sdh155122 ASSERT(&sctps->sctps_bind_fanout[ 6374505Skcpoon SCTP_BIND_HASH(port)] == tbf); 6380Sstevel@tonic-gate sctp_bind_hash_insert(tbf, sctp, 1); 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate mutex_exit(&tbf->tf_lock); 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate /* 6430Sstevel@tonic-gate * We don't want sctp_next_port_to_try to "inherit" 6440Sstevel@tonic-gate * a port number supplied by the user in a bind. 6451676Sjpk * 6460Sstevel@tonic-gate * This is the only place where sctp_next_port_to_try 6470Sstevel@tonic-gate * is updated. After the update, it may or may not 6480Sstevel@tonic-gate * be in the valid range. 6490Sstevel@tonic-gate */ 6501676Sjpk if (user_specified == 0) 6513448Sdh155122 sctps->sctps_next_port_to_try = port + 1; 6521676Sjpk 6531676Sjpk *allocated_port = port; 6541676Sjpk 6551676Sjpk return (0); 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate if ((count == 0) && (user_specified)) { 6590Sstevel@tonic-gate /* 6600Sstevel@tonic-gate * We may have to return an anonymous port. So 6610Sstevel@tonic-gate * get one to start with. 6620Sstevel@tonic-gate */ 6633448Sdh155122 port = sctp_update_next_port( 6643448Sdh155122 sctps->sctps_next_port_to_try, 6653448Sdh155122 zone, sctps); 6660Sstevel@tonic-gate user_specified = 0; 6670Sstevel@tonic-gate } else { 6683448Sdh155122 port = sctp_update_next_port(port + 1, zone, sctps); 6690Sstevel@tonic-gate } 6701676Sjpk if (port == 0) 6711676Sjpk break; 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * Don't let this loop run forever in the case where 6750Sstevel@tonic-gate * all of the anonymous ports are in use. 6760Sstevel@tonic-gate */ 6770Sstevel@tonic-gate } while (++count < loopmax); 6781676Sjpk 6791676Sjpk return (bind_to_req_port_only ? EADDRINUSE : EADDRNOTAVAIL); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * Don't let port fall into the privileged range. 6840Sstevel@tonic-gate * Since the extra privileged ports can be arbitrary we also 6850Sstevel@tonic-gate * ensure that we exclude those from consideration. 6860Sstevel@tonic-gate * sctp_g_epriv_ports is not sorted thus we loop over it until 6870Sstevel@tonic-gate * there are no changes. 6880Sstevel@tonic-gate * 6890Sstevel@tonic-gate * Note: No locks are held when inspecting sctp_g_*epriv_ports 6900Sstevel@tonic-gate * but instead the code relies on: 6910Sstevel@tonic-gate * - the fact that the address of the array and its size never changes 6920Sstevel@tonic-gate * - the atomic assignment of the elements of the array 6930Sstevel@tonic-gate */ 6940Sstevel@tonic-gate in_port_t 6953448Sdh155122 sctp_update_next_port(in_port_t port, zone_t *zone, sctp_stack_t *sctps) 6960Sstevel@tonic-gate { 6970Sstevel@tonic-gate int i; 6981676Sjpk boolean_t restart = B_FALSE; 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate retry: 7013448Sdh155122 if (port < sctps->sctps_smallest_anon_port) 7023448Sdh155122 port = sctps->sctps_smallest_anon_port; 7030Sstevel@tonic-gate 7043448Sdh155122 if (port > sctps->sctps_largest_anon_port) { 7051676Sjpk if (restart) 7061676Sjpk return (0); 7071676Sjpk restart = B_TRUE; 7083448Sdh155122 port = sctps->sctps_smallest_anon_port; 7091676Sjpk } 7101676Sjpk 7113448Sdh155122 if (port < sctps->sctps_smallest_nonpriv_port) 7123448Sdh155122 port = sctps->sctps_smallest_nonpriv_port; 7130Sstevel@tonic-gate 7143448Sdh155122 for (i = 0; i < sctps->sctps_g_num_epriv_ports; i++) { 7153448Sdh155122 if (port == sctps->sctps_g_epriv_ports[i]) { 7160Sstevel@tonic-gate port++; 7170Sstevel@tonic-gate /* 7180Sstevel@tonic-gate * Make sure whether the port is in the 7190Sstevel@tonic-gate * valid range. 7200Sstevel@tonic-gate * 7210Sstevel@tonic-gate * XXX Note that if sctp_g_epriv_ports contains 7220Sstevel@tonic-gate * all the anonymous ports this will be an 7230Sstevel@tonic-gate * infinite loop. 7240Sstevel@tonic-gate */ 7250Sstevel@tonic-gate goto retry; 7260Sstevel@tonic-gate } 7270Sstevel@tonic-gate } 7281676Sjpk 7291676Sjpk if (is_system_labeled() && 7301676Sjpk (i = tsol_next_port(zone, port, IPPROTO_SCTP, B_TRUE)) != 0) { 7311676Sjpk port = i; 7321676Sjpk goto retry; 7331676Sjpk } 7341676Sjpk 7350Sstevel@tonic-gate return (port); 7360Sstevel@tonic-gate } 737