17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*77c67f2fSkcpoon 227c478bd9Sstevel@tonic-gate /* 2345916cd2Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/systm.h> 317c478bd9Sstevel@tonic-gate #include <sys/stream.h> 327c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 337c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 347c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 357c478bd9Sstevel@tonic-gate #include <sys/tihdr.h> 367c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 377c478bd9Sstevel@tonic-gate #include <sys/socket.h> 387c478bd9Sstevel@tonic-gate #include <sys/random.h> 397c478bd9Sstevel@tonic-gate #include <sys/policy.h> 4045916cd2Sjpk #include <sys/tsol/tndb.h> 4145916cd2Sjpk #include <sys/tsol/tnet.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <netinet/in.h> 447c478bd9Sstevel@tonic-gate #include <netinet/ip6.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include <inet/common.h> 477c478bd9Sstevel@tonic-gate #include <inet/ip.h> 487c478bd9Sstevel@tonic-gate #include <inet/ip6.h> 497c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h> 507c478bd9Sstevel@tonic-gate #include "sctp_impl.h" 517c478bd9Sstevel@tonic-gate #include "sctp_asconf.h" 527c478bd9Sstevel@tonic-gate #include "sctp_addr.h" 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate uint_t sctp_next_port_to_try; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * Returns 0 on success, EACCES on permission failure. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate static int 607c478bd9Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified) 617c478bd9Sstevel@tonic-gate { 627c478bd9Sstevel@tonic-gate /* 637c478bd9Sstevel@tonic-gate * Get a valid port (within the anonymous range and should not 647c478bd9Sstevel@tonic-gate * be a privileged one) to use if the user has not given a port. 657c478bd9Sstevel@tonic-gate * If multiple threads are here, they may all start with 667c478bd9Sstevel@tonic-gate * with the same initial port. But, it should be fine as long as 677c478bd9Sstevel@tonic-gate * sctp_bindi will ensure that no two threads will be assigned 687c478bd9Sstevel@tonic-gate * the same port. 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate if (*requested_port == 0) { 7145916cd2Sjpk *requested_port = sctp_update_next_port(sctp_next_port_to_try, 7245916cd2Sjpk crgetzone(sctp->sctp_credp)); 7345916cd2Sjpk if (*requested_port == 0) 7445916cd2Sjpk return (EACCES); 757c478bd9Sstevel@tonic-gate *user_specified = 0; 767c478bd9Sstevel@tonic-gate } else { 777c478bd9Sstevel@tonic-gate int i; 787c478bd9Sstevel@tonic-gate boolean_t priv = B_FALSE; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * If the requested_port is in the well-known privileged range, 827c478bd9Sstevel@tonic-gate * verify that the stream was opened by a privileged user. 837c478bd9Sstevel@tonic-gate * Note: No locks are held when inspecting sctp_g_*epriv_ports 847c478bd9Sstevel@tonic-gate * but instead the code relies on: 857c478bd9Sstevel@tonic-gate * - the fact that the address of the array and its size never 867c478bd9Sstevel@tonic-gate * changes 877c478bd9Sstevel@tonic-gate * - the atomic assignment of the elements of the array 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate if (*requested_port < sctp_smallest_nonpriv_port) { 907c478bd9Sstevel@tonic-gate priv = B_TRUE; 917c478bd9Sstevel@tonic-gate } else { 927c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_num_epriv_ports; i++) { 937c478bd9Sstevel@tonic-gate if (*requested_port == sctp_g_epriv_ports[i]) { 947c478bd9Sstevel@tonic-gate priv = B_TRUE; 957c478bd9Sstevel@tonic-gate break; 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate if (priv) { 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * sctp_bind() should take a cred_t argument so that 1027c478bd9Sstevel@tonic-gate * we can use it here. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate if (secpolicy_net_privaddr(sctp->sctp_credp, 1057c478bd9Sstevel@tonic-gate *requested_port) != 0) { 1067c478bd9Sstevel@tonic-gate dprint(1, 1077c478bd9Sstevel@tonic-gate ("sctp_bind(x): no prive for port %d", 1087c478bd9Sstevel@tonic-gate *requested_port)); 10945916cd2Sjpk return (EACCES); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate *user_specified = 1; 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate return (0); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate int 1197c478bd9Sstevel@tonic-gate sctp_listen(sctp_t *sctp) 1207c478bd9Sstevel@tonic-gate { 1217c478bd9Sstevel@tonic-gate sctp_tf_t *tf; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate RUN_SCTP(sctp); 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * TCP handles listen() increasing the backlog, need to check 1261d8c4025Svi * if it should be handled here too 1277c478bd9Sstevel@tonic-gate */ 1287c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_BOUND) { 1297c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 1307c478bd9Sstevel@tonic-gate return (EINVAL); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate /* Do an anonymous bind for unbound socket doing listen(). */ 1347c478bd9Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 0) { 1357c478bd9Sstevel@tonic-gate struct sockaddr_storage ss; 1367c478bd9Sstevel@tonic-gate int ret; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate bzero(&ss, sizeof (ss)); 1397c478bd9Sstevel@tonic-gate ss.ss_family = sctp->sctp_family; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 1427c478bd9Sstevel@tonic-gate if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss, 1437c478bd9Sstevel@tonic-gate sizeof (ss))) != 0) 1447c478bd9Sstevel@tonic-gate return (ret); 1457c478bd9Sstevel@tonic-gate RUN_SCTP(sctp) 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate sctp->sctp_state = SCTPS_LISTEN; 1497c478bd9Sstevel@tonic-gate (void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN); 1507c478bd9Sstevel@tonic-gate sctp->sctp_last_secret_update = lbolt64; 1517c478bd9Sstevel@tonic-gate bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN); 1527c478bd9Sstevel@tonic-gate tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))]; 1537c478bd9Sstevel@tonic-gate sctp_listen_hash_insert(tf, sctp); 1547c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 1557c478bd9Sstevel@tonic-gate return (0); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* 1597c478bd9Sstevel@tonic-gate * Bind the sctp_t to a sockaddr, which includes an address and other 1607c478bd9Sstevel@tonic-gate * information, such as port or flowinfo. 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate int 1637c478bd9Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len) 1647c478bd9Sstevel@tonic-gate { 1657c478bd9Sstevel@tonic-gate int user_specified; 1667c478bd9Sstevel@tonic-gate boolean_t bind_to_req_port_only; 1677c478bd9Sstevel@tonic-gate in_port_t requested_port; 1687c478bd9Sstevel@tonic-gate in_port_t allocated_port; 1697c478bd9Sstevel@tonic-gate int err = 0; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate ASSERT(sctp != NULL); 1727c478bd9Sstevel@tonic-gate ASSERT(sa); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate RUN_SCTP(sctp); 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_BOUND) { 1777c478bd9Sstevel@tonic-gate err = EINVAL; 1787c478bd9Sstevel@tonic-gate goto done; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate switch (sa->sa_family) { 1827c478bd9Sstevel@tonic-gate case AF_INET: 1837c478bd9Sstevel@tonic-gate if (len < sizeof (struct sockaddr_in) || 1847c478bd9Sstevel@tonic-gate sctp->sctp_family == AF_INET6) { 1857c478bd9Sstevel@tonic-gate err = EINVAL; 1867c478bd9Sstevel@tonic-gate goto done; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port); 1897c478bd9Sstevel@tonic-gate break; 1907c478bd9Sstevel@tonic-gate case AF_INET6: 1917c478bd9Sstevel@tonic-gate if (len < sizeof (struct sockaddr_in6) || 1927c478bd9Sstevel@tonic-gate sctp->sctp_family == AF_INET) { 1937c478bd9Sstevel@tonic-gate err = EINVAL; 1947c478bd9Sstevel@tonic-gate goto done; 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port); 1977c478bd9Sstevel@tonic-gate /* Set the flowinfo. */ 1987c478bd9Sstevel@tonic-gate sctp->sctp_ip6h->ip6_vcf = 1997c478bd9Sstevel@tonic-gate (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 2007c478bd9Sstevel@tonic-gate (((struct sockaddr_in6 *)sa)->sin6_flowinfo & 2017c478bd9Sstevel@tonic-gate ~IPV6_VERS_AND_FLOW_MASK); 2027c478bd9Sstevel@tonic-gate break; 2037c478bd9Sstevel@tonic-gate default: 2047c478bd9Sstevel@tonic-gate err = EAFNOSUPPORT; 2057c478bd9Sstevel@tonic-gate goto done; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE; 2087c478bd9Sstevel@tonic-gate 20945916cd2Sjpk err = sctp_select_port(sctp, &requested_port, &user_specified); 21045916cd2Sjpk if (err != 0) 2117c478bd9Sstevel@tonic-gate goto done; 2127c478bd9Sstevel@tonic-gate 2131d8c4025Svi if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE, 2141d8c4025Svi user_specified == 1 ? htons(requested_port) : 0)) != 0) { 2157c478bd9Sstevel@tonic-gate goto done; 2161d8c4025Svi } 21745916cd2Sjpk err = sctp_bindi(sctp, requested_port, bind_to_req_port_only, 21845916cd2Sjpk user_specified, &allocated_port); 21945916cd2Sjpk if (err != 0) { 2207c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp); 22145916cd2Sjpk } else { 22245916cd2Sjpk ASSERT(sctp->sctp_state == SCTPS_BOUND); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate done: 2257c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 2267c478bd9Sstevel@tonic-gate return (err); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * Perform bind/unbind operation of a list of addresses on a sctp_t 2317c478bd9Sstevel@tonic-gate */ 2327c478bd9Sstevel@tonic-gate int 2337c478bd9Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop) 2347c478bd9Sstevel@tonic-gate { 2357c478bd9Sstevel@tonic-gate ASSERT(sctp != NULL); 2367c478bd9Sstevel@tonic-gate ASSERT(addrs != NULL); 2377c478bd9Sstevel@tonic-gate ASSERT(addrcnt > 0); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate switch (bindop) { 2407c478bd9Sstevel@tonic-gate case SCTP_BINDX_ADD_ADDR: 2411d8c4025Svi return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE, 2421d8c4025Svi sctp->sctp_lport)); 2437c478bd9Sstevel@tonic-gate case SCTP_BINDX_REM_ADDR: 2447c478bd9Sstevel@tonic-gate return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE)); 2457c478bd9Sstevel@tonic-gate default: 2467c478bd9Sstevel@tonic-gate return (EINVAL); 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate /* 2517c478bd9Sstevel@tonic-gate * Add a list of addresses to a sctp_t. 2527c478bd9Sstevel@tonic-gate */ 2537c478bd9Sstevel@tonic-gate int 2547c478bd9Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 2551d8c4025Svi boolean_t caller_hold_lock, in_port_t port) 2567c478bd9Sstevel@tonic-gate { 2577c478bd9Sstevel@tonic-gate int err = 0; 2587c478bd9Sstevel@tonic-gate boolean_t do_asconf = B_FALSE; 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 2617c478bd9Sstevel@tonic-gate RUN_SCTP(sctp); 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_ESTABLISHED) { 2647c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 2657c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 2667c478bd9Sstevel@tonic-gate return (EINVAL); 2677c478bd9Sstevel@tonic-gate } 268df19b344Svi 269df19b344Svi if (sctp->sctp_state > SCTPS_LISTEN) { 270df19b344Svi /* 271df19b344Svi * Let's do some checking here rather than undoing the 272df19b344Svi * add later (for these reasons). 273df19b344Svi */ 274df19b344Svi if (!sctp_addip_enabled || !sctp->sctp_understands_asconf || 275df19b344Svi !sctp->sctp_understands_addip) { 276df19b344Svi if (!caller_hold_lock) 277df19b344Svi WAKE_SCTP(sctp); 278df19b344Svi return (EINVAL); 279df19b344Svi } 2807c478bd9Sstevel@tonic-gate do_asconf = B_TRUE; 281df19b344Svi } 2821d8c4025Svi /* 2831d8c4025Svi * On a clustered node, for an inaddr_any bind, we will pass the list 2841d8c4025Svi * of all the addresses in the global list, minus any address on the 2851d8c4025Svi * loopback interface, and expect the clustering susbsystem to give us 2861d8c4025Svi * the correct list for the 'port'. For explicit binds we give the 2871d8c4025Svi * list of addresses and the clustering module validates it for the 2881d8c4025Svi * 'port'. 2891d8c4025Svi * 2901d8c4025Svi * On a non-clustered node, cl_sctp_check_addrs will be NULL and 2911d8c4025Svi * we proceed as usual. 2921d8c4025Svi */ 2931d8c4025Svi if (cl_sctp_check_addrs != NULL) { 2941d8c4025Svi uchar_t *addrlist = NULL; 2951d8c4025Svi size_t size = 0; 2961d8c4025Svi int unspec = 0; 2971d8c4025Svi boolean_t do_listen; 2981d8c4025Svi uchar_t *llist = NULL; 2991d8c4025Svi size_t lsize = 0; 3001d8c4025Svi 3011d8c4025Svi /* 3021d8c4025Svi * If we are adding addresses after listening, but before 3031d8c4025Svi * an association is established, we need to update the 3041d8c4025Svi * clustering module with this info. 3051d8c4025Svi */ 3061d8c4025Svi do_listen = !do_asconf && sctp->sctp_state > SCTPS_BOUND && 3071d8c4025Svi cl_sctp_listen != NULL; 3081d8c4025Svi 3091d8c4025Svi err = sctp_get_addrlist(sctp, addrs, &addrcnt, &addrlist, 3101d8c4025Svi &unspec, &size); 3111d8c4025Svi if (err != 0) { 3121d8c4025Svi ASSERT(addrlist == NULL); 3131d8c4025Svi ASSERT(addrcnt == 0); 3141d8c4025Svi ASSERT(size == 0); 3151d8c4025Svi if (!caller_hold_lock) 3161d8c4025Svi WAKE_SCTP(sctp); 317*77c67f2fSkcpoon SCTP_KSTAT(sctp_cl_check_addrs); 3181d8c4025Svi return (err); 3191d8c4025Svi } 3201d8c4025Svi ASSERT(addrlist != NULL); 3211d8c4025Svi (*cl_sctp_check_addrs)(sctp->sctp_family, port, &addrlist, 3221d8c4025Svi size, &addrcnt, unspec == 1); 3231d8c4025Svi if (addrcnt == 0) { 3241d8c4025Svi /* We free the list */ 3251d8c4025Svi kmem_free(addrlist, size); 3261d8c4025Svi if (!caller_hold_lock) 3271d8c4025Svi WAKE_SCTP(sctp); 3281d8c4025Svi return (EINVAL); 3291d8c4025Svi } 3301d8c4025Svi if (do_listen) { 3311d8c4025Svi lsize = sizeof (in6_addr_t) * addrcnt; 3321d8c4025Svi llist = kmem_alloc(lsize, KM_SLEEP); 3331d8c4025Svi } 3341d8c4025Svi err = sctp_valid_addr_list(sctp, addrlist, addrcnt, llist, 3351d8c4025Svi lsize); 3361d8c4025Svi if (err == 0 && do_listen) { 3371d8c4025Svi (*cl_sctp_listen)(sctp->sctp_family, llist, 3381d8c4025Svi addrcnt, sctp->sctp_lport); 3391d8c4025Svi /* list will be freed by the clustering module */ 3401d8c4025Svi } else if (err != 0 && llist != NULL) { 3411d8c4025Svi kmem_free(llist, lsize); 3421d8c4025Svi } 3431d8c4025Svi /* free the list we allocated */ 3441d8c4025Svi kmem_free(addrlist, size); 3451d8c4025Svi } else { 3461d8c4025Svi err = sctp_valid_addr_list(sctp, addrs, addrcnt, NULL, 0); 3471d8c4025Svi } 3487c478bd9Sstevel@tonic-gate if (err != 0) { 3497c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3507c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 3517c478bd9Sstevel@tonic-gate return (err); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate /* Need to send ASCONF messages */ 3547c478bd9Sstevel@tonic-gate if (do_asconf) { 3557c478bd9Sstevel@tonic-gate err = sctp_add_ip(sctp, addrs, addrcnt); 3567c478bd9Sstevel@tonic-gate if (err != 0) { 3577c478bd9Sstevel@tonic-gate sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE); 3587c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3597c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 3607c478bd9Sstevel@tonic-gate return (err); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3647c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 3657c478bd9Sstevel@tonic-gate if (do_asconf) 3667c478bd9Sstevel@tonic-gate sctp_process_sendq(sctp); 3677c478bd9Sstevel@tonic-gate return (0); 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate /* 3717c478bd9Sstevel@tonic-gate * Remove one or more addresses bound to the sctp_t. 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate int 3747c478bd9Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 3757c478bd9Sstevel@tonic-gate boolean_t caller_hold_lock) 3767c478bd9Sstevel@tonic-gate { 3777c478bd9Sstevel@tonic-gate int error = 0; 3787c478bd9Sstevel@tonic-gate boolean_t do_asconf = B_FALSE; 3791d8c4025Svi uchar_t *ulist = NULL; 3801d8c4025Svi size_t usize = 0; 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3837c478bd9Sstevel@tonic-gate RUN_SCTP(sctp); 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_ESTABLISHED) { 3867c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3877c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 3887c478bd9Sstevel@tonic-gate return (EINVAL); 3897c478bd9Sstevel@tonic-gate } 390df19b344Svi /* 391df19b344Svi * Fail the remove if we are beyond listen, but can't send this 392df19b344Svi * to the peer. 393df19b344Svi */ 394df19b344Svi if (sctp->sctp_state > SCTPS_LISTEN) { 395df19b344Svi if (!sctp_addip_enabled || !sctp->sctp_understands_asconf || 396df19b344Svi !sctp->sctp_understands_addip) { 397df19b344Svi if (!caller_hold_lock) 398df19b344Svi WAKE_SCTP(sctp); 399df19b344Svi return (EINVAL); 400df19b344Svi } 4017c478bd9Sstevel@tonic-gate do_asconf = B_TRUE; 402df19b344Svi } 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate /* Can't delete the last address nor all of the addresses */ 4057c478bd9Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) { 4067c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 4077c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 4087c478bd9Sstevel@tonic-gate return (EINVAL); 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate 4111d8c4025Svi if (cl_sctp_unlisten != NULL && !do_asconf && 4121d8c4025Svi sctp->sctp_state > SCTPS_BOUND) { 4131d8c4025Svi usize = sizeof (in6_addr_t) * addrcnt; 4141d8c4025Svi ulist = kmem_alloc(usize, KM_SLEEP); 4151d8c4025Svi } 4161d8c4025Svi 4171d8c4025Svi error = sctp_del_ip(sctp, addrs, addrcnt, ulist, usize); 4181d8c4025Svi if (error != 0) { 4191d8c4025Svi if (ulist != NULL) 4201d8c4025Svi kmem_free(ulist, usize); 4211d8c4025Svi if (!caller_hold_lock) 4221d8c4025Svi WAKE_SCTP(sctp); 4231d8c4025Svi return (error); 4241d8c4025Svi } 4251d8c4025Svi /* ulist will be non-NULL only if cl_sctp_unlisten is non-NULL */ 4261d8c4025Svi if (ulist != NULL) { 4271d8c4025Svi ASSERT(cl_sctp_unlisten != NULL); 4281d8c4025Svi (*cl_sctp_unlisten)(sctp->sctp_family, ulist, addrcnt, 4291d8c4025Svi sctp->sctp_lport); 4301d8c4025Svi /* ulist will be freed by the clustering module */ 4311d8c4025Svi } 4327c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 4337c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 4341d8c4025Svi if (do_asconf) 4357c478bd9Sstevel@tonic-gate sctp_process_sendq(sctp); 4367c478bd9Sstevel@tonic-gate return (error); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* 44045916cd2Sjpk * Returns 0 for success, errno value otherwise. 4417c478bd9Sstevel@tonic-gate * 44245916cd2Sjpk * If the "bind_to_req_port_only" parameter is set and the requested port 44345916cd2Sjpk * number is available, then set allocated_port to it. If not available, 44445916cd2Sjpk * return an error. 4457c478bd9Sstevel@tonic-gate * 44645916cd2Sjpk * If the "bind_to_req_port_only" parameter is not set and the requested port 44745916cd2Sjpk * number is available, then set allocated_port to it. If not available, 44845916cd2Sjpk * find the first anonymous port we can and set allocated_port to that. If no 44945916cd2Sjpk * anonymous ports are available, return an error. 45045916cd2Sjpk * 45145916cd2Sjpk * In either case, when succeeding, update the sctp_t to record the port number 4527c478bd9Sstevel@tonic-gate * and insert it in the bind hash table. 4537c478bd9Sstevel@tonic-gate */ 45445916cd2Sjpk int 45545916cd2Sjpk sctp_bindi(sctp_t *sctp, in_port_t port, boolean_t bind_to_req_port_only, 45645916cd2Sjpk int user_specified, in_port_t *allocated_port) 4577c478bd9Sstevel@tonic-gate { 4587c478bd9Sstevel@tonic-gate /* number of times we have run around the loop */ 4597c478bd9Sstevel@tonic-gate int count = 0; 4607c478bd9Sstevel@tonic-gate /* maximum number of times to run around the loop */ 4617c478bd9Sstevel@tonic-gate int loopmax; 4627c478bd9Sstevel@tonic-gate zoneid_t zoneid = sctp->sctp_zoneid; 46345916cd2Sjpk zone_t *zone = crgetzone(sctp->sctp_credp); 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate /* 4667c478bd9Sstevel@tonic-gate * Lookup for free addresses is done in a loop and "loopmax" 4677c478bd9Sstevel@tonic-gate * influences how long we spin in the loop 4687c478bd9Sstevel@tonic-gate */ 4697c478bd9Sstevel@tonic-gate if (bind_to_req_port_only) { 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * If the requested port is busy, don't bother to look 4727c478bd9Sstevel@tonic-gate * for a new one. Setting loop maximum count to 1 has 4737c478bd9Sstevel@tonic-gate * that effect. 4747c478bd9Sstevel@tonic-gate */ 4757c478bd9Sstevel@tonic-gate loopmax = 1; 4767c478bd9Sstevel@tonic-gate } else { 4777c478bd9Sstevel@tonic-gate /* 4787c478bd9Sstevel@tonic-gate * If the requested port is busy, look for a free one 4797c478bd9Sstevel@tonic-gate * in the anonymous port range. 4807c478bd9Sstevel@tonic-gate * Set loopmax appropriately so that one does not look 4817c478bd9Sstevel@tonic-gate * forever in the case all of the anonymous ports are in use. 4827c478bd9Sstevel@tonic-gate */ 4837c478bd9Sstevel@tonic-gate loopmax = (sctp_largest_anon_port - 4847c478bd9Sstevel@tonic-gate sctp_smallest_anon_port + 1); 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate do { 4877c478bd9Sstevel@tonic-gate uint16_t lport; 4887c478bd9Sstevel@tonic-gate sctp_tf_t *tbf; 4897c478bd9Sstevel@tonic-gate sctp_t *lsctp; 4907c478bd9Sstevel@tonic-gate int addrcmp; 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate lport = htons(port); 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate /* 4957c478bd9Sstevel@tonic-gate * Ensure that the sctp_t is not currently in the bind hash. 4967c478bd9Sstevel@tonic-gate * Hold the lock on the hash bucket to ensure that 4977c478bd9Sstevel@tonic-gate * the duplicate check plus the insertion is an atomic 4987c478bd9Sstevel@tonic-gate * operation. 4997c478bd9Sstevel@tonic-gate * 5007c478bd9Sstevel@tonic-gate * This function does an inline lookup on the bind hash list 5017c478bd9Sstevel@tonic-gate * Make sure that we access only members of sctp_t 5027c478bd9Sstevel@tonic-gate * and that we don't look at sctp_sctp, since we are not 5037c478bd9Sstevel@tonic-gate * doing a SCTPB_REFHOLD. For more details please see the notes 5047c478bd9Sstevel@tonic-gate * in sctp_compress() 5057c478bd9Sstevel@tonic-gate */ 5067c478bd9Sstevel@tonic-gate sctp_bind_hash_remove(sctp); 5077c478bd9Sstevel@tonic-gate tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)]; 5087c478bd9Sstevel@tonic-gate mutex_enter(&tbf->tf_lock); 5097c478bd9Sstevel@tonic-gate for (lsctp = tbf->tf_sctp; lsctp != NULL; 5107c478bd9Sstevel@tonic-gate lsctp = lsctp->sctp_bind_hash) { 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate if (lport != lsctp->sctp_lport || 5137c478bd9Sstevel@tonic-gate lsctp->sctp_state < SCTPS_BOUND) 5147c478bd9Sstevel@tonic-gate continue; 5157c478bd9Sstevel@tonic-gate 51645916cd2Sjpk /* 51745916cd2Sjpk * On a labeled system, we must treat bindings to ports 51845916cd2Sjpk * on shared IP addresses by sockets with MAC exemption 51945916cd2Sjpk * privilege as being in all zones, as there's 52045916cd2Sjpk * otherwise no way to identify the right receiver. 52145916cd2Sjpk */ 52245916cd2Sjpk if (lsctp->sctp_zoneid != zoneid && 52345916cd2Sjpk !lsctp->sctp_mac_exempt && !sctp->sctp_mac_exempt) 52445916cd2Sjpk continue; 52545916cd2Sjpk 5267c478bd9Sstevel@tonic-gate addrcmp = sctp_compare_saddrs(sctp, lsctp); 5277c478bd9Sstevel@tonic-gate if (addrcmp != SCTP_ADDR_DISJOINT) { 5287c478bd9Sstevel@tonic-gate if (!sctp->sctp_reuseaddr) { 5297c478bd9Sstevel@tonic-gate /* in use */ 5307c478bd9Sstevel@tonic-gate break; 5317c478bd9Sstevel@tonic-gate } else if (lsctp->sctp_state == SCTPS_BOUND || 5327c478bd9Sstevel@tonic-gate lsctp->sctp_state == SCTPS_LISTEN) { 5337c478bd9Sstevel@tonic-gate /* 5347c478bd9Sstevel@tonic-gate * socket option SO_REUSEADDR is set 5357c478bd9Sstevel@tonic-gate * on the binding sctp_t. 5367c478bd9Sstevel@tonic-gate * 5377c478bd9Sstevel@tonic-gate * We have found a match of IP source 5387c478bd9Sstevel@tonic-gate * address and source port, which is 5397c478bd9Sstevel@tonic-gate * refused regardless of the 5407c478bd9Sstevel@tonic-gate * SO_REUSEADDR setting, so we break. 5417c478bd9Sstevel@tonic-gate */ 5427c478bd9Sstevel@tonic-gate break; 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate if (lsctp != NULL) { 5477c478bd9Sstevel@tonic-gate /* The port number is busy */ 5487c478bd9Sstevel@tonic-gate mutex_exit(&tbf->tf_lock); 5497c478bd9Sstevel@tonic-gate } else { 55045916cd2Sjpk conn_t *connp = sctp->sctp_connp; 55145916cd2Sjpk 55245916cd2Sjpk if (is_system_labeled()) { 55345916cd2Sjpk mlp_type_t addrtype, mlptype; 55445916cd2Sjpk 55545916cd2Sjpk /* 55645916cd2Sjpk * On a labeled system we must check the type 55745916cd2Sjpk * of the binding requested by the user (either 55845916cd2Sjpk * MLP or SLP on shared and private addresses), 55945916cd2Sjpk * and that the user's requested binding 56045916cd2Sjpk * is permitted. 56145916cd2Sjpk */ 56245916cd2Sjpk addrtype = tsol_mlp_addr_type(zone->zone_id, 56345916cd2Sjpk sctp->sctp_ipversion, 56445916cd2Sjpk sctp->sctp_ipversion == IPV4_VERSION ? 56545916cd2Sjpk (void *)&sctp->sctp_ipha->ipha_src : 56645916cd2Sjpk (void *)&sctp->sctp_ip6h->ip6_src); 56745916cd2Sjpk 56845916cd2Sjpk /* 56945916cd2Sjpk * tsol_mlp_addr_type returns the possibilities 57045916cd2Sjpk * for the selected address. Since all local 57145916cd2Sjpk * addresses are either private or shared, the 57245916cd2Sjpk * return value mlptSingle means "local address 57345916cd2Sjpk * not valid (interface not present)." 57445916cd2Sjpk */ 57545916cd2Sjpk if (addrtype == mlptSingle) { 57645916cd2Sjpk mutex_exit(&tbf->tf_lock); 57745916cd2Sjpk return (EADDRNOTAVAIL); 57845916cd2Sjpk } 57945916cd2Sjpk mlptype = tsol_mlp_port_type(zone, IPPROTO_SCTP, 58045916cd2Sjpk port, addrtype); 58145916cd2Sjpk if (mlptype != mlptSingle) { 58245916cd2Sjpk if (secpolicy_net_bindmlp(connp-> 58345916cd2Sjpk conn_cred) != 0) { 58445916cd2Sjpk mutex_exit(&tbf->tf_lock); 58545916cd2Sjpk return (EACCES); 58645916cd2Sjpk } 58745916cd2Sjpk /* 58845916cd2Sjpk * If we're binding a shared MLP, then 58945916cd2Sjpk * make sure that this zone is the one 59045916cd2Sjpk * that owns that MLP. Shared MLPs can 59145916cd2Sjpk * be owned by at most one zone. 59245916cd2Sjpk */ 59345916cd2Sjpk 59445916cd2Sjpk if (mlptype == mlptShared && 59545916cd2Sjpk addrtype == mlptShared && 59645916cd2Sjpk connp->conn_zoneid != 59745916cd2Sjpk tsol_mlp_findzone(IPPROTO_SCTP, 59845916cd2Sjpk lport)) { 59945916cd2Sjpk mutex_exit(&tbf->tf_lock); 60045916cd2Sjpk return (EACCES); 60145916cd2Sjpk } 60245916cd2Sjpk connp->conn_mlp_type = mlptype; 60345916cd2Sjpk } 60445916cd2Sjpk } 6057c478bd9Sstevel@tonic-gate /* 6067c478bd9Sstevel@tonic-gate * This port is ours. Insert in fanout and mark as 6077c478bd9Sstevel@tonic-gate * bound to prevent others from getting the port 6087c478bd9Sstevel@tonic-gate * number. 6097c478bd9Sstevel@tonic-gate */ 6107c478bd9Sstevel@tonic-gate sctp->sctp_state = SCTPS_BOUND; 6117c478bd9Sstevel@tonic-gate sctp->sctp_lport = lport; 61245916cd2Sjpk sctp->sctp_sctph->sh_sport = lport; 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf); 6157c478bd9Sstevel@tonic-gate sctp_bind_hash_insert(tbf, sctp, 1); 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate mutex_exit(&tbf->tf_lock); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate /* 6207c478bd9Sstevel@tonic-gate * We don't want sctp_next_port_to_try to "inherit" 6217c478bd9Sstevel@tonic-gate * a port number supplied by the user in a bind. 62245916cd2Sjpk * 6237c478bd9Sstevel@tonic-gate * This is the only place where sctp_next_port_to_try 6247c478bd9Sstevel@tonic-gate * is updated. After the update, it may or may not 6257c478bd9Sstevel@tonic-gate * be in the valid range. 6267c478bd9Sstevel@tonic-gate */ 62745916cd2Sjpk if (user_specified == 0) 62845916cd2Sjpk sctp_next_port_to_try = port + 1; 62945916cd2Sjpk 63045916cd2Sjpk *allocated_port = port; 63145916cd2Sjpk 63245916cd2Sjpk return (0); 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate if ((count == 0) && (user_specified)) { 6367c478bd9Sstevel@tonic-gate /* 6377c478bd9Sstevel@tonic-gate * We may have to return an anonymous port. So 6387c478bd9Sstevel@tonic-gate * get one to start with. 6397c478bd9Sstevel@tonic-gate */ 64045916cd2Sjpk port = sctp_update_next_port(sctp_next_port_to_try, 64145916cd2Sjpk zone); 6427c478bd9Sstevel@tonic-gate user_specified = 0; 6437c478bd9Sstevel@tonic-gate } else { 64445916cd2Sjpk port = sctp_update_next_port(port + 1, zone); 6457c478bd9Sstevel@tonic-gate } 64645916cd2Sjpk if (port == 0) 64745916cd2Sjpk break; 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate /* 6507c478bd9Sstevel@tonic-gate * Don't let this loop run forever in the case where 6517c478bd9Sstevel@tonic-gate * all of the anonymous ports are in use. 6527c478bd9Sstevel@tonic-gate */ 6537c478bd9Sstevel@tonic-gate } while (++count < loopmax); 65445916cd2Sjpk 65545916cd2Sjpk return (bind_to_req_port_only ? EADDRINUSE : EADDRNOTAVAIL); 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate /* 6597c478bd9Sstevel@tonic-gate * Don't let port fall into the privileged range. 6607c478bd9Sstevel@tonic-gate * Since the extra privileged ports can be arbitrary we also 6617c478bd9Sstevel@tonic-gate * ensure that we exclude those from consideration. 6627c478bd9Sstevel@tonic-gate * sctp_g_epriv_ports is not sorted thus we loop over it until 6637c478bd9Sstevel@tonic-gate * there are no changes. 6647c478bd9Sstevel@tonic-gate * 6657c478bd9Sstevel@tonic-gate * Note: No locks are held when inspecting sctp_g_*epriv_ports 6667c478bd9Sstevel@tonic-gate * but instead the code relies on: 6677c478bd9Sstevel@tonic-gate * - the fact that the address of the array and its size never changes 6687c478bd9Sstevel@tonic-gate * - the atomic assignment of the elements of the array 6697c478bd9Sstevel@tonic-gate */ 6707c478bd9Sstevel@tonic-gate in_port_t 67145916cd2Sjpk sctp_update_next_port(in_port_t port, zone_t *zone) 6727c478bd9Sstevel@tonic-gate { 6737c478bd9Sstevel@tonic-gate int i; 67445916cd2Sjpk boolean_t restart = B_FALSE; 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate retry: 67745916cd2Sjpk if (port < sctp_smallest_anon_port) 67845916cd2Sjpk port = sctp_smallest_anon_port; 67945916cd2Sjpk 68045916cd2Sjpk if (port > sctp_largest_anon_port) { 68145916cd2Sjpk if (restart) 68245916cd2Sjpk return (0); 68345916cd2Sjpk restart = B_TRUE; 6847c478bd9Sstevel@tonic-gate port = sctp_smallest_anon_port; 68545916cd2Sjpk } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate if (port < sctp_smallest_nonpriv_port) 6887c478bd9Sstevel@tonic-gate port = sctp_smallest_nonpriv_port; 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_num_epriv_ports; i++) { 6917c478bd9Sstevel@tonic-gate if (port == sctp_g_epriv_ports[i]) { 6927c478bd9Sstevel@tonic-gate port++; 6937c478bd9Sstevel@tonic-gate /* 6947c478bd9Sstevel@tonic-gate * Make sure whether the port is in the 6957c478bd9Sstevel@tonic-gate * valid range. 6967c478bd9Sstevel@tonic-gate * 6977c478bd9Sstevel@tonic-gate * XXX Note that if sctp_g_epriv_ports contains 6987c478bd9Sstevel@tonic-gate * all the anonymous ports this will be an 6997c478bd9Sstevel@tonic-gate * infinite loop. 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate goto retry; 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate } 70445916cd2Sjpk 70545916cd2Sjpk if (is_system_labeled() && 70645916cd2Sjpk (i = tsol_next_port(zone, port, IPPROTO_SCTP, B_TRUE)) != 0) { 70745916cd2Sjpk port = i; 70845916cd2Sjpk goto retry; 70945916cd2Sjpk } 71045916cd2Sjpk 7117c478bd9Sstevel@tonic-gate return (port); 7127c478bd9Sstevel@tonic-gate } 713