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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*df19b344Svi * Copyright 2005 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> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <netinet/in.h> 427c478bd9Sstevel@tonic-gate #include <netinet/ip6.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <inet/common.h> 457c478bd9Sstevel@tonic-gate #include <inet/ip.h> 467c478bd9Sstevel@tonic-gate #include <inet/ip6.h> 477c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h> 487c478bd9Sstevel@tonic-gate #include "sctp_impl.h" 497c478bd9Sstevel@tonic-gate #include "sctp_asconf.h" 507c478bd9Sstevel@tonic-gate #include "sctp_addr.h" 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate uint_t sctp_next_port_to_try; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * Returns 0 on success, EACCES on permission failure. 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate static int 587c478bd9Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * Get a valid port (within the anonymous range and should not 627c478bd9Sstevel@tonic-gate * be a privileged one) to use if the user has not given a port. 637c478bd9Sstevel@tonic-gate * If multiple threads are here, they may all start with 647c478bd9Sstevel@tonic-gate * with the same initial port. But, it should be fine as long as 657c478bd9Sstevel@tonic-gate * sctp_bindi will ensure that no two threads will be assigned 667c478bd9Sstevel@tonic-gate * the same port. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate if (*requested_port == 0) { 697c478bd9Sstevel@tonic-gate *requested_port = sctp_update_next_port(sctp_next_port_to_try); 707c478bd9Sstevel@tonic-gate *user_specified = 0; 717c478bd9Sstevel@tonic-gate } else { 727c478bd9Sstevel@tonic-gate int i; 737c478bd9Sstevel@tonic-gate boolean_t priv = B_FALSE; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* 767c478bd9Sstevel@tonic-gate * If the requested_port is in the well-known privileged range, 777c478bd9Sstevel@tonic-gate * verify that the stream was opened by a privileged user. 787c478bd9Sstevel@tonic-gate * Note: No locks are held when inspecting sctp_g_*epriv_ports 797c478bd9Sstevel@tonic-gate * but instead the code relies on: 807c478bd9Sstevel@tonic-gate * - the fact that the address of the array and its size never 817c478bd9Sstevel@tonic-gate * changes 827c478bd9Sstevel@tonic-gate * - the atomic assignment of the elements of the array 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate if (*requested_port < sctp_smallest_nonpriv_port) { 857c478bd9Sstevel@tonic-gate priv = B_TRUE; 867c478bd9Sstevel@tonic-gate } else { 877c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_num_epriv_ports; i++) { 887c478bd9Sstevel@tonic-gate if (*requested_port == sctp_g_epriv_ports[i]) { 897c478bd9Sstevel@tonic-gate priv = B_TRUE; 907c478bd9Sstevel@tonic-gate break; 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate if (priv) { 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * sctp_bind() should take a cred_t argument so that 977c478bd9Sstevel@tonic-gate * we can use it here. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate if (secpolicy_net_privaddr(sctp->sctp_credp, 1007c478bd9Sstevel@tonic-gate *requested_port) != 0) { 1017c478bd9Sstevel@tonic-gate dprint(1, 1027c478bd9Sstevel@tonic-gate ("sctp_bind(x): no prive for port %d", 1037c478bd9Sstevel@tonic-gate *requested_port)); 1047c478bd9Sstevel@tonic-gate return (TACCES); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate *user_specified = 1; 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate return (0); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate int 1147c478bd9Sstevel@tonic-gate sctp_listen(sctp_t *sctp) 1157c478bd9Sstevel@tonic-gate { 1167c478bd9Sstevel@tonic-gate sctp_tf_t *tf; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate RUN_SCTP(sctp); 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * TCP handles listen() increasing the backlog, need to check 1217c478bd9Sstevel@tonic-gate * if it should be handled here too - VENU. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_BOUND) { 1247c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 1257c478bd9Sstevel@tonic-gate return (EINVAL); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate /* Do an anonymous bind for unbound socket doing listen(). */ 1297c478bd9Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 0) { 1307c478bd9Sstevel@tonic-gate struct sockaddr_storage ss; 1317c478bd9Sstevel@tonic-gate int ret; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate bzero(&ss, sizeof (ss)); 1347c478bd9Sstevel@tonic-gate ss.ss_family = sctp->sctp_family; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 1377c478bd9Sstevel@tonic-gate if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss, 1387c478bd9Sstevel@tonic-gate sizeof (ss))) != 0) 1397c478bd9Sstevel@tonic-gate return (ret); 1407c478bd9Sstevel@tonic-gate RUN_SCTP(sctp) 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate sctp->sctp_state = SCTPS_LISTEN; 1447c478bd9Sstevel@tonic-gate (void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN); 1457c478bd9Sstevel@tonic-gate sctp->sctp_last_secret_update = lbolt64; 1467c478bd9Sstevel@tonic-gate bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN); 1477c478bd9Sstevel@tonic-gate tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))]; 1487c478bd9Sstevel@tonic-gate sctp_listen_hash_insert(tf, sctp); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 1517c478bd9Sstevel@tonic-gate return (0); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * Bind the sctp_t to a sockaddr, which includes an address and other 1567c478bd9Sstevel@tonic-gate * information, such as port or flowinfo. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate int 1597c478bd9Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len) 1607c478bd9Sstevel@tonic-gate { 1617c478bd9Sstevel@tonic-gate int user_specified; 1627c478bd9Sstevel@tonic-gate boolean_t bind_to_req_port_only; 1637c478bd9Sstevel@tonic-gate in_port_t requested_port; 1647c478bd9Sstevel@tonic-gate in_port_t allocated_port; 1657c478bd9Sstevel@tonic-gate int err = 0; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate ASSERT(sctp != NULL); 1687c478bd9Sstevel@tonic-gate ASSERT(sa); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate RUN_SCTP(sctp); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_BOUND) { 1737c478bd9Sstevel@tonic-gate err = EINVAL; 1747c478bd9Sstevel@tonic-gate goto done; 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate switch (sa->sa_family) { 1787c478bd9Sstevel@tonic-gate case AF_INET: 1797c478bd9Sstevel@tonic-gate if (len < sizeof (struct sockaddr_in) || 1807c478bd9Sstevel@tonic-gate sctp->sctp_family == AF_INET6) { 1817c478bd9Sstevel@tonic-gate err = EINVAL; 1827c478bd9Sstevel@tonic-gate goto done; 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port); 1857c478bd9Sstevel@tonic-gate break; 1867c478bd9Sstevel@tonic-gate case AF_INET6: 1877c478bd9Sstevel@tonic-gate if (len < sizeof (struct sockaddr_in6) || 1887c478bd9Sstevel@tonic-gate sctp->sctp_family == AF_INET) { 1897c478bd9Sstevel@tonic-gate err = EINVAL; 1907c478bd9Sstevel@tonic-gate goto done; 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port); 1937c478bd9Sstevel@tonic-gate /* Set the flowinfo. */ 1947c478bd9Sstevel@tonic-gate sctp->sctp_ip6h->ip6_vcf = 1957c478bd9Sstevel@tonic-gate (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 1967c478bd9Sstevel@tonic-gate (((struct sockaddr_in6 *)sa)->sin6_flowinfo & 1977c478bd9Sstevel@tonic-gate ~IPV6_VERS_AND_FLOW_MASK); 1987c478bd9Sstevel@tonic-gate break; 1997c478bd9Sstevel@tonic-gate default: 2007c478bd9Sstevel@tonic-gate err = EAFNOSUPPORT; 2017c478bd9Sstevel@tonic-gate goto done; 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate if (sctp_select_port(sctp, &requested_port, &user_specified) != 0) { 2067c478bd9Sstevel@tonic-gate err = EPERM; 2077c478bd9Sstevel@tonic-gate goto done; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE)) != 0) 2117c478bd9Sstevel@tonic-gate goto done; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate allocated_port = sctp_bindi(sctp, requested_port, 2147c478bd9Sstevel@tonic-gate bind_to_req_port_only, user_specified); 2157c478bd9Sstevel@tonic-gate if (allocated_port == 0) { 2167c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp); 2177c478bd9Sstevel@tonic-gate if (bind_to_req_port_only) { 2187c478bd9Sstevel@tonic-gate err = EADDRINUSE; 2197c478bd9Sstevel@tonic-gate goto done; 2207c478bd9Sstevel@tonic-gate } else { 2217c478bd9Sstevel@tonic-gate err = EADDRNOTAVAIL; 2227c478bd9Sstevel@tonic-gate goto done; 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate ASSERT(sctp->sctp_state == SCTPS_BOUND); 2267c478bd9Sstevel@tonic-gate done: 2277c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 2287c478bd9Sstevel@tonic-gate return (err); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * Perform bind/unbind operation of a list of addresses on a sctp_t 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate int 2357c478bd9Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop) 2367c478bd9Sstevel@tonic-gate { 2377c478bd9Sstevel@tonic-gate ASSERT(sctp != NULL); 2387c478bd9Sstevel@tonic-gate ASSERT(addrs != NULL); 2397c478bd9Sstevel@tonic-gate ASSERT(addrcnt > 0); 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate switch (bindop) { 2427c478bd9Sstevel@tonic-gate case SCTP_BINDX_ADD_ADDR: 2437c478bd9Sstevel@tonic-gate return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE)); 2447c478bd9Sstevel@tonic-gate case SCTP_BINDX_REM_ADDR: 2457c478bd9Sstevel@tonic-gate return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE)); 2467c478bd9Sstevel@tonic-gate default: 2477c478bd9Sstevel@tonic-gate return (EINVAL); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * Add a list of addresses to a sctp_t. 2537c478bd9Sstevel@tonic-gate */ 2547c478bd9Sstevel@tonic-gate int 2557c478bd9Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 2567c478bd9Sstevel@tonic-gate boolean_t caller_hold_lock) 2577c478bd9Sstevel@tonic-gate { 2587c478bd9Sstevel@tonic-gate int err = 0; 2597c478bd9Sstevel@tonic-gate boolean_t do_asconf = B_FALSE; 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 2627c478bd9Sstevel@tonic-gate RUN_SCTP(sctp); 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_ESTABLISHED) { 2657c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 2667c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 2677c478bd9Sstevel@tonic-gate return (EINVAL); 2687c478bd9Sstevel@tonic-gate } 269*df19b344Svi 270*df19b344Svi if (sctp->sctp_state > SCTPS_LISTEN) { 271*df19b344Svi /* 272*df19b344Svi * Let's do some checking here rather than undoing the 273*df19b344Svi * add later (for these reasons). 274*df19b344Svi */ 275*df19b344Svi if (!sctp_addip_enabled || !sctp->sctp_understands_asconf || 276*df19b344Svi !sctp->sctp_understands_addip) { 277*df19b344Svi if (!caller_hold_lock) 278*df19b344Svi WAKE_SCTP(sctp); 279*df19b344Svi return (EINVAL); 280*df19b344Svi } 2817c478bd9Sstevel@tonic-gate do_asconf = B_TRUE; 282*df19b344Svi } 2837c478bd9Sstevel@tonic-gate err = sctp_valid_addr_list(sctp, addrs, addrcnt); 2847c478bd9Sstevel@tonic-gate if (err != 0) { 2857c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 2867c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 2877c478bd9Sstevel@tonic-gate return (err); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate /* Need to send ASCONF messages */ 2917c478bd9Sstevel@tonic-gate if (do_asconf) { 2927c478bd9Sstevel@tonic-gate err = sctp_add_ip(sctp, addrs, addrcnt); 2937c478bd9Sstevel@tonic-gate if (err != 0) { 2947c478bd9Sstevel@tonic-gate sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE); 2957c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 2967c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 2977c478bd9Sstevel@tonic-gate return (err); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3017c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 3027c478bd9Sstevel@tonic-gate if (do_asconf) 3037c478bd9Sstevel@tonic-gate sctp_process_sendq(sctp); 3047c478bd9Sstevel@tonic-gate return (0); 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate /* 3087c478bd9Sstevel@tonic-gate * Remove one or more addresses bound to the sctp_t. 3097c478bd9Sstevel@tonic-gate */ 3107c478bd9Sstevel@tonic-gate int 3117c478bd9Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 3127c478bd9Sstevel@tonic-gate boolean_t caller_hold_lock) 3137c478bd9Sstevel@tonic-gate { 3147c478bd9Sstevel@tonic-gate int error = 0; 3157c478bd9Sstevel@tonic-gate boolean_t do_asconf = B_FALSE; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3187c478bd9Sstevel@tonic-gate RUN_SCTP(sctp); 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_ESTABLISHED) { 3217c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3227c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 3237c478bd9Sstevel@tonic-gate return (EINVAL); 3247c478bd9Sstevel@tonic-gate } 325*df19b344Svi /* 326*df19b344Svi * Fail the remove if we are beyond listen, but can't send this 327*df19b344Svi * to the peer. 328*df19b344Svi */ 329*df19b344Svi if (sctp->sctp_state > SCTPS_LISTEN) { 330*df19b344Svi if (!sctp_addip_enabled || !sctp->sctp_understands_asconf || 331*df19b344Svi !sctp->sctp_understands_addip) { 332*df19b344Svi if (!caller_hold_lock) 333*df19b344Svi WAKE_SCTP(sctp); 334*df19b344Svi return (EINVAL); 335*df19b344Svi } 3367c478bd9Sstevel@tonic-gate do_asconf = B_TRUE; 337*df19b344Svi } 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate /* Can't delete the last address nor all of the addresses */ 3407c478bd9Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) { 3417c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3427c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 3437c478bd9Sstevel@tonic-gate return (EINVAL); 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate error = sctp_del_ip(sctp, addrs, addrcnt); 3477c478bd9Sstevel@tonic-gate if (!caller_hold_lock) 3487c478bd9Sstevel@tonic-gate WAKE_SCTP(sctp); 3497c478bd9Sstevel@tonic-gate if (error == 0 && do_asconf) 3507c478bd9Sstevel@tonic-gate sctp_process_sendq(sctp); 3517c478bd9Sstevel@tonic-gate return (error); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate /* 3557c478bd9Sstevel@tonic-gate * If the "bind_to_req_port_only" parameter is set, if the requested port 3567c478bd9Sstevel@tonic-gate * number is available, return it, If not return 0 3577c478bd9Sstevel@tonic-gate * 3587c478bd9Sstevel@tonic-gate * If "bind_to_req_port_only" parameter is not set and 3597c478bd9Sstevel@tonic-gate * If the requested port number is available, return it. If not, return 3607c478bd9Sstevel@tonic-gate * the first anonymous port we happen across. If no anonymous ports are 3617c478bd9Sstevel@tonic-gate * available, return 0. addr is the requested local address, if any. 3627c478bd9Sstevel@tonic-gate * 3637c478bd9Sstevel@tonic-gate * In either case, when succeeding update the sctp_t to record the port number 3647c478bd9Sstevel@tonic-gate * and insert it in the bind hash table. 3657c478bd9Sstevel@tonic-gate */ 3667c478bd9Sstevel@tonic-gate in_port_t 3677c478bd9Sstevel@tonic-gate sctp_bindi(sctp_t *sctp, in_port_t port, int bind_to_req_port_only, 3687c478bd9Sstevel@tonic-gate int user_specified) 3697c478bd9Sstevel@tonic-gate { 3707c478bd9Sstevel@tonic-gate /* number of times we have run around the loop */ 3717c478bd9Sstevel@tonic-gate int count = 0; 3727c478bd9Sstevel@tonic-gate /* maximum number of times to run around the loop */ 3737c478bd9Sstevel@tonic-gate int loopmax; 3747c478bd9Sstevel@tonic-gate zoneid_t zoneid = sctp->sctp_zoneid; 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate /* 3777c478bd9Sstevel@tonic-gate * Lookup for free addresses is done in a loop and "loopmax" 3787c478bd9Sstevel@tonic-gate * influences how long we spin in the loop 3797c478bd9Sstevel@tonic-gate */ 3807c478bd9Sstevel@tonic-gate if (bind_to_req_port_only) { 3817c478bd9Sstevel@tonic-gate /* 3827c478bd9Sstevel@tonic-gate * If the requested port is busy, don't bother to look 3837c478bd9Sstevel@tonic-gate * for a new one. Setting loop maximum count to 1 has 3847c478bd9Sstevel@tonic-gate * that effect. 3857c478bd9Sstevel@tonic-gate */ 3867c478bd9Sstevel@tonic-gate loopmax = 1; 3877c478bd9Sstevel@tonic-gate } else { 3887c478bd9Sstevel@tonic-gate /* 3897c478bd9Sstevel@tonic-gate * If the requested port is busy, look for a free one 3907c478bd9Sstevel@tonic-gate * in the anonymous port range. 3917c478bd9Sstevel@tonic-gate * Set loopmax appropriately so that one does not look 3927c478bd9Sstevel@tonic-gate * forever in the case all of the anonymous ports are in use. 3937c478bd9Sstevel@tonic-gate */ 3947c478bd9Sstevel@tonic-gate loopmax = (sctp_largest_anon_port - 3957c478bd9Sstevel@tonic-gate sctp_smallest_anon_port + 1); 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate do { 3987c478bd9Sstevel@tonic-gate uint16_t lport; 3997c478bd9Sstevel@tonic-gate sctp_tf_t *tbf; 4007c478bd9Sstevel@tonic-gate sctp_t *lsctp; 4017c478bd9Sstevel@tonic-gate int addrcmp; 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate lport = htons(port); 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate /* 4067c478bd9Sstevel@tonic-gate * Ensure that the sctp_t is not currently in the bind hash. 4077c478bd9Sstevel@tonic-gate * Hold the lock on the hash bucket to ensure that 4087c478bd9Sstevel@tonic-gate * the duplicate check plus the insertion is an atomic 4097c478bd9Sstevel@tonic-gate * operation. 4107c478bd9Sstevel@tonic-gate * 4117c478bd9Sstevel@tonic-gate * This function does an inline lookup on the bind hash list 4127c478bd9Sstevel@tonic-gate * Make sure that we access only members of sctp_t 4137c478bd9Sstevel@tonic-gate * and that we don't look at sctp_sctp, since we are not 4147c478bd9Sstevel@tonic-gate * doing a SCTPB_REFHOLD. For more details please see the notes 4157c478bd9Sstevel@tonic-gate * in sctp_compress() 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate sctp_bind_hash_remove(sctp); 4187c478bd9Sstevel@tonic-gate tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)]; 4197c478bd9Sstevel@tonic-gate mutex_enter(&tbf->tf_lock); 4207c478bd9Sstevel@tonic-gate for (lsctp = tbf->tf_sctp; lsctp != NULL; 4217c478bd9Sstevel@tonic-gate lsctp = lsctp->sctp_bind_hash) { 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate if (lport != lsctp->sctp_lport || 4247c478bd9Sstevel@tonic-gate lsctp->sctp_zoneid != zoneid || 4257c478bd9Sstevel@tonic-gate lsctp->sctp_state < SCTPS_BOUND) 4267c478bd9Sstevel@tonic-gate continue; 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate addrcmp = sctp_compare_saddrs(sctp, lsctp); 4297c478bd9Sstevel@tonic-gate if (addrcmp != SCTP_ADDR_DISJOINT) { 4307c478bd9Sstevel@tonic-gate if (!sctp->sctp_reuseaddr) { 4317c478bd9Sstevel@tonic-gate /* in use */ 4327c478bd9Sstevel@tonic-gate break; 4337c478bd9Sstevel@tonic-gate } else if (lsctp->sctp_state == SCTPS_BOUND || 4347c478bd9Sstevel@tonic-gate lsctp->sctp_state == SCTPS_LISTEN) { 4357c478bd9Sstevel@tonic-gate /* 4367c478bd9Sstevel@tonic-gate * socket option SO_REUSEADDR is set 4377c478bd9Sstevel@tonic-gate * on the binding sctp_t. 4387c478bd9Sstevel@tonic-gate * 4397c478bd9Sstevel@tonic-gate * We have found a match of IP source 4407c478bd9Sstevel@tonic-gate * address and source port, which is 4417c478bd9Sstevel@tonic-gate * refused regardless of the 4427c478bd9Sstevel@tonic-gate * SO_REUSEADDR setting, so we break. 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate break; 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate if (lsctp != NULL) { 4497c478bd9Sstevel@tonic-gate /* The port number is busy */ 4507c478bd9Sstevel@tonic-gate mutex_exit(&tbf->tf_lock); 4517c478bd9Sstevel@tonic-gate } else { 4527c478bd9Sstevel@tonic-gate /* 4537c478bd9Sstevel@tonic-gate * This port is ours. Insert in fanout and mark as 4547c478bd9Sstevel@tonic-gate * bound to prevent others from getting the port 4557c478bd9Sstevel@tonic-gate * number. 4567c478bd9Sstevel@tonic-gate */ 4577c478bd9Sstevel@tonic-gate sctp->sctp_state = SCTPS_BOUND; 4587c478bd9Sstevel@tonic-gate sctp->sctp_lport = lport; 4597c478bd9Sstevel@tonic-gate sctp->sctp_sctph->sh_sport = sctp->sctp_lport; 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf); 4627c478bd9Sstevel@tonic-gate sctp_bind_hash_insert(tbf, sctp, 1); 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate mutex_exit(&tbf->tf_lock); 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate /* 4677c478bd9Sstevel@tonic-gate * We don't want sctp_next_port_to_try to "inherit" 4687c478bd9Sstevel@tonic-gate * a port number supplied by the user in a bind. 4697c478bd9Sstevel@tonic-gate */ 4707c478bd9Sstevel@tonic-gate if (user_specified != 0) 4717c478bd9Sstevel@tonic-gate return (port); 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate /* 4747c478bd9Sstevel@tonic-gate * This is the only place where sctp_next_port_to_try 4757c478bd9Sstevel@tonic-gate * is updated. After the update, it may or may not 4767c478bd9Sstevel@tonic-gate * be in the valid range. 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate sctp_next_port_to_try = port + 1; 4797c478bd9Sstevel@tonic-gate return (port); 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate if ((count == 0) && (user_specified)) { 4837c478bd9Sstevel@tonic-gate /* 4847c478bd9Sstevel@tonic-gate * We may have to return an anonymous port. So 4857c478bd9Sstevel@tonic-gate * get one to start with. 4867c478bd9Sstevel@tonic-gate */ 4877c478bd9Sstevel@tonic-gate port = sctp_update_next_port(sctp_next_port_to_try); 4887c478bd9Sstevel@tonic-gate user_specified = 0; 4897c478bd9Sstevel@tonic-gate } else { 4907c478bd9Sstevel@tonic-gate port = sctp_update_next_port(port + 1); 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate /* 4947c478bd9Sstevel@tonic-gate * Don't let this loop run forever in the case where 4957c478bd9Sstevel@tonic-gate * all of the anonymous ports are in use. 4967c478bd9Sstevel@tonic-gate */ 4977c478bd9Sstevel@tonic-gate } while (++count < loopmax); 4987c478bd9Sstevel@tonic-gate return (0); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate /* 5027c478bd9Sstevel@tonic-gate * Don't let port fall into the privileged range. 5037c478bd9Sstevel@tonic-gate * Since the extra privileged ports can be arbitrary we also 5047c478bd9Sstevel@tonic-gate * ensure that we exclude those from consideration. 5057c478bd9Sstevel@tonic-gate * sctp_g_epriv_ports is not sorted thus we loop over it until 5067c478bd9Sstevel@tonic-gate * there are no changes. 5077c478bd9Sstevel@tonic-gate * 5087c478bd9Sstevel@tonic-gate * Note: No locks are held when inspecting sctp_g_*epriv_ports 5097c478bd9Sstevel@tonic-gate * but instead the code relies on: 5107c478bd9Sstevel@tonic-gate * - the fact that the address of the array and its size never changes 5117c478bd9Sstevel@tonic-gate * - the atomic assignment of the elements of the array 5127c478bd9Sstevel@tonic-gate */ 5137c478bd9Sstevel@tonic-gate in_port_t 5147c478bd9Sstevel@tonic-gate sctp_update_next_port(in_port_t port) 5157c478bd9Sstevel@tonic-gate { 5167c478bd9Sstevel@tonic-gate int i; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate retry: 5197c478bd9Sstevel@tonic-gate if (port < sctp_smallest_anon_port || port > sctp_largest_anon_port) 5207c478bd9Sstevel@tonic-gate port = sctp_smallest_anon_port; 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate if (port < sctp_smallest_nonpriv_port) 5237c478bd9Sstevel@tonic-gate port = sctp_smallest_nonpriv_port; 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_num_epriv_ports; i++) { 5267c478bd9Sstevel@tonic-gate if (port == sctp_g_epriv_ports[i]) { 5277c478bd9Sstevel@tonic-gate port++; 5287c478bd9Sstevel@tonic-gate /* 5297c478bd9Sstevel@tonic-gate * Make sure whether the port is in the 5307c478bd9Sstevel@tonic-gate * valid range. 5317c478bd9Sstevel@tonic-gate * 5327c478bd9Sstevel@tonic-gate * XXX Note that if sctp_g_epriv_ports contains 5337c478bd9Sstevel@tonic-gate * all the anonymous ports this will be an 5347c478bd9Sstevel@tonic-gate * infinite loop. 5357c478bd9Sstevel@tonic-gate */ 5367c478bd9Sstevel@tonic-gate goto retry; 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate return (port); 5407c478bd9Sstevel@tonic-gate } 541