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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23252Svi117747 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/systm.h> 310Sstevel@tonic-gate #include <sys/stream.h> 320Sstevel@tonic-gate #include <sys/cmn_err.h> 330Sstevel@tonic-gate #include <sys/kmem.h> 340Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 350Sstevel@tonic-gate #include <sys/tihdr.h> 360Sstevel@tonic-gate #include <sys/stropts.h> 370Sstevel@tonic-gate #include <sys/socket.h> 380Sstevel@tonic-gate #include <sys/random.h> 390Sstevel@tonic-gate #include <sys/policy.h> 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 uint_t sctp_next_port_to_try; 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * Returns 0 on success, EACCES on permission failure. 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate static int 580Sstevel@tonic-gate sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified) 590Sstevel@tonic-gate { 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) { 690Sstevel@tonic-gate *requested_port = sctp_update_next_port(sctp_next_port_to_try); 700Sstevel@tonic-gate *user_specified = 0; 710Sstevel@tonic-gate } else { 720Sstevel@tonic-gate int i; 730Sstevel@tonic-gate boolean_t priv = B_FALSE; 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * If the requested_port is in the well-known privileged range, 770Sstevel@tonic-gate * verify that the stream was opened by a privileged user. 780Sstevel@tonic-gate * Note: No locks are held when inspecting sctp_g_*epriv_ports 790Sstevel@tonic-gate * but instead the code relies on: 800Sstevel@tonic-gate * - the fact that the address of the array and its size never 810Sstevel@tonic-gate * changes 820Sstevel@tonic-gate * - the atomic assignment of the elements of the array 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate if (*requested_port < sctp_smallest_nonpriv_port) { 850Sstevel@tonic-gate priv = B_TRUE; 860Sstevel@tonic-gate } else { 870Sstevel@tonic-gate for (i = 0; i < sctp_g_num_epriv_ports; i++) { 880Sstevel@tonic-gate if (*requested_port == sctp_g_epriv_ports[i]) { 890Sstevel@tonic-gate priv = B_TRUE; 900Sstevel@tonic-gate break; 910Sstevel@tonic-gate } 920Sstevel@tonic-gate } 930Sstevel@tonic-gate } 940Sstevel@tonic-gate if (priv) { 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * sctp_bind() should take a cred_t argument so that 970Sstevel@tonic-gate * we can use it here. 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate if (secpolicy_net_privaddr(sctp->sctp_credp, 1000Sstevel@tonic-gate *requested_port) != 0) { 1010Sstevel@tonic-gate dprint(1, 1020Sstevel@tonic-gate ("sctp_bind(x): no prive for port %d", 1030Sstevel@tonic-gate *requested_port)); 1040Sstevel@tonic-gate return (TACCES); 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate *user_specified = 1; 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate return (0); 1110Sstevel@tonic-gate } 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate int 1140Sstevel@tonic-gate sctp_listen(sctp_t *sctp) 1150Sstevel@tonic-gate { 1160Sstevel@tonic-gate sctp_tf_t *tf; 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate RUN_SCTP(sctp); 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * TCP handles listen() increasing the backlog, need to check 121*852Svi117747 * if it should be handled here too 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_BOUND) { 1240Sstevel@tonic-gate WAKE_SCTP(sctp); 1250Sstevel@tonic-gate return (EINVAL); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* Do an anonymous bind for unbound socket doing listen(). */ 1290Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 0) { 1300Sstevel@tonic-gate struct sockaddr_storage ss; 1310Sstevel@tonic-gate int ret; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate bzero(&ss, sizeof (ss)); 1340Sstevel@tonic-gate ss.ss_family = sctp->sctp_family; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate WAKE_SCTP(sctp); 1370Sstevel@tonic-gate if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss, 1380Sstevel@tonic-gate sizeof (ss))) != 0) 1390Sstevel@tonic-gate return (ret); 1400Sstevel@tonic-gate RUN_SCTP(sctp) 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate sctp->sctp_state = SCTPS_LISTEN; 1440Sstevel@tonic-gate (void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN); 1450Sstevel@tonic-gate sctp->sctp_last_secret_update = lbolt64; 1460Sstevel@tonic-gate bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN); 1470Sstevel@tonic-gate tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))]; 1480Sstevel@tonic-gate sctp_listen_hash_insert(tf, sctp); 1490Sstevel@tonic-gate WAKE_SCTP(sctp); 1500Sstevel@tonic-gate return (0); 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * Bind the sctp_t to a sockaddr, which includes an address and other 1550Sstevel@tonic-gate * information, such as port or flowinfo. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate int 1580Sstevel@tonic-gate sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len) 1590Sstevel@tonic-gate { 1600Sstevel@tonic-gate int user_specified; 1610Sstevel@tonic-gate boolean_t bind_to_req_port_only; 1620Sstevel@tonic-gate in_port_t requested_port; 1630Sstevel@tonic-gate in_port_t allocated_port; 1640Sstevel@tonic-gate int err = 0; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate ASSERT(sctp != NULL); 1670Sstevel@tonic-gate ASSERT(sa); 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate RUN_SCTP(sctp); 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_BOUND) { 1720Sstevel@tonic-gate err = EINVAL; 1730Sstevel@tonic-gate goto done; 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate switch (sa->sa_family) { 1770Sstevel@tonic-gate case AF_INET: 1780Sstevel@tonic-gate if (len < sizeof (struct sockaddr_in) || 1790Sstevel@tonic-gate sctp->sctp_family == AF_INET6) { 1800Sstevel@tonic-gate err = EINVAL; 1810Sstevel@tonic-gate goto done; 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port); 1840Sstevel@tonic-gate break; 1850Sstevel@tonic-gate case AF_INET6: 1860Sstevel@tonic-gate if (len < sizeof (struct sockaddr_in6) || 1870Sstevel@tonic-gate sctp->sctp_family == AF_INET) { 1880Sstevel@tonic-gate err = EINVAL; 1890Sstevel@tonic-gate goto done; 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port); 1920Sstevel@tonic-gate /* Set the flowinfo. */ 1930Sstevel@tonic-gate sctp->sctp_ip6h->ip6_vcf = 1940Sstevel@tonic-gate (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 1950Sstevel@tonic-gate (((struct sockaddr_in6 *)sa)->sin6_flowinfo & 1960Sstevel@tonic-gate ~IPV6_VERS_AND_FLOW_MASK); 1970Sstevel@tonic-gate break; 1980Sstevel@tonic-gate default: 1990Sstevel@tonic-gate err = EAFNOSUPPORT; 2000Sstevel@tonic-gate goto done; 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE; 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate if (sctp_select_port(sctp, &requested_port, &user_specified) != 0) { 2050Sstevel@tonic-gate err = EPERM; 2060Sstevel@tonic-gate goto done; 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 209*852Svi117747 if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE, 210*852Svi117747 user_specified == 1 ? htons(requested_port) : 0)) != 0) { 2110Sstevel@tonic-gate goto done; 212*852Svi117747 } 2130Sstevel@tonic-gate allocated_port = sctp_bindi(sctp, requested_port, 2140Sstevel@tonic-gate bind_to_req_port_only, user_specified); 2150Sstevel@tonic-gate if (allocated_port == 0) { 2160Sstevel@tonic-gate sctp_free_saddrs(sctp); 2170Sstevel@tonic-gate if (bind_to_req_port_only) { 2180Sstevel@tonic-gate err = EADDRINUSE; 2190Sstevel@tonic-gate goto done; 2200Sstevel@tonic-gate } else { 2210Sstevel@tonic-gate err = EADDRNOTAVAIL; 2220Sstevel@tonic-gate goto done; 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate ASSERT(sctp->sctp_state == SCTPS_BOUND); 2260Sstevel@tonic-gate done: 2270Sstevel@tonic-gate WAKE_SCTP(sctp); 2280Sstevel@tonic-gate return (err); 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2320Sstevel@tonic-gate * Perform bind/unbind operation of a list of addresses on a sctp_t 2330Sstevel@tonic-gate */ 2340Sstevel@tonic-gate int 2350Sstevel@tonic-gate sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop) 2360Sstevel@tonic-gate { 2370Sstevel@tonic-gate ASSERT(sctp != NULL); 2380Sstevel@tonic-gate ASSERT(addrs != NULL); 2390Sstevel@tonic-gate ASSERT(addrcnt > 0); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate switch (bindop) { 2420Sstevel@tonic-gate case SCTP_BINDX_ADD_ADDR: 243*852Svi117747 return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE, 244*852Svi117747 sctp->sctp_lport)); 2450Sstevel@tonic-gate case SCTP_BINDX_REM_ADDR: 2460Sstevel@tonic-gate return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE)); 2470Sstevel@tonic-gate default: 2480Sstevel@tonic-gate return (EINVAL); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Add a list of addresses to a sctp_t. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate int 2560Sstevel@tonic-gate sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 257*852Svi117747 boolean_t caller_hold_lock, in_port_t port) 2580Sstevel@tonic-gate { 2590Sstevel@tonic-gate int err = 0; 2600Sstevel@tonic-gate boolean_t do_asconf = B_FALSE; 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate if (!caller_hold_lock) 2630Sstevel@tonic-gate RUN_SCTP(sctp); 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_ESTABLISHED) { 2660Sstevel@tonic-gate if (!caller_hold_lock) 2670Sstevel@tonic-gate WAKE_SCTP(sctp); 2680Sstevel@tonic-gate return (EINVAL); 2690Sstevel@tonic-gate } 270252Svi117747 271252Svi117747 if (sctp->sctp_state > SCTPS_LISTEN) { 272252Svi117747 /* 273252Svi117747 * Let's do some checking here rather than undoing the 274252Svi117747 * add later (for these reasons). 275252Svi117747 */ 276252Svi117747 if (!sctp_addip_enabled || !sctp->sctp_understands_asconf || 277252Svi117747 !sctp->sctp_understands_addip) { 278252Svi117747 if (!caller_hold_lock) 279252Svi117747 WAKE_SCTP(sctp); 280252Svi117747 return (EINVAL); 281252Svi117747 } 2820Sstevel@tonic-gate do_asconf = B_TRUE; 283252Svi117747 } 284*852Svi117747 /* 285*852Svi117747 * On a clustered node, for an inaddr_any bind, we will pass the list 286*852Svi117747 * of all the addresses in the global list, minus any address on the 287*852Svi117747 * loopback interface, and expect the clustering susbsystem to give us 288*852Svi117747 * the correct list for the 'port'. For explicit binds we give the 289*852Svi117747 * list of addresses and the clustering module validates it for the 290*852Svi117747 * 'port'. 291*852Svi117747 * 292*852Svi117747 * On a non-clustered node, cl_sctp_check_addrs will be NULL and 293*852Svi117747 * we proceed as usual. 294*852Svi117747 */ 295*852Svi117747 if (cl_sctp_check_addrs != NULL) { 296*852Svi117747 uchar_t *addrlist = NULL; 297*852Svi117747 size_t size = 0; 298*852Svi117747 int unspec = 0; 299*852Svi117747 boolean_t do_listen; 300*852Svi117747 uchar_t *llist = NULL; 301*852Svi117747 size_t lsize = 0; 302*852Svi117747 303*852Svi117747 /* 304*852Svi117747 * If we are adding addresses after listening, but before 305*852Svi117747 * an association is established, we need to update the 306*852Svi117747 * clustering module with this info. 307*852Svi117747 */ 308*852Svi117747 do_listen = !do_asconf && sctp->sctp_state > SCTPS_BOUND && 309*852Svi117747 cl_sctp_listen != NULL; 310*852Svi117747 311*852Svi117747 err = sctp_get_addrlist(sctp, addrs, &addrcnt, &addrlist, 312*852Svi117747 &unspec, &size); 313*852Svi117747 if (err != 0) { 314*852Svi117747 ASSERT(addrlist == NULL); 315*852Svi117747 ASSERT(addrcnt == 0); 316*852Svi117747 ASSERT(size == 0); 317*852Svi117747 if (!caller_hold_lock) 318*852Svi117747 WAKE_SCTP(sctp); 319*852Svi117747 return (err); 320*852Svi117747 } 321*852Svi117747 ASSERT(addrlist != NULL); 322*852Svi117747 (*cl_sctp_check_addrs)(sctp->sctp_family, port, &addrlist, 323*852Svi117747 size, &addrcnt, unspec == 1); 324*852Svi117747 if (addrcnt == 0) { 325*852Svi117747 /* We free the list */ 326*852Svi117747 kmem_free(addrlist, size); 327*852Svi117747 if (!caller_hold_lock) 328*852Svi117747 WAKE_SCTP(sctp); 329*852Svi117747 return (EINVAL); 330*852Svi117747 } 331*852Svi117747 if (do_listen) { 332*852Svi117747 lsize = sizeof (in6_addr_t) * addrcnt; 333*852Svi117747 llist = kmem_alloc(lsize, KM_SLEEP); 334*852Svi117747 } 335*852Svi117747 err = sctp_valid_addr_list(sctp, addrlist, addrcnt, llist, 336*852Svi117747 lsize); 337*852Svi117747 if (err == 0 && do_listen) { 338*852Svi117747 (*cl_sctp_listen)(sctp->sctp_family, llist, 339*852Svi117747 addrcnt, sctp->sctp_lport); 340*852Svi117747 /* list will be freed by the clustering module */ 341*852Svi117747 } else if (err != 0 && llist != NULL) { 342*852Svi117747 kmem_free(llist, lsize); 343*852Svi117747 } 344*852Svi117747 /* free the list we allocated */ 345*852Svi117747 kmem_free(addrlist, size); 346*852Svi117747 } else { 347*852Svi117747 err = sctp_valid_addr_list(sctp, addrs, addrcnt, NULL, 0); 348*852Svi117747 } 3490Sstevel@tonic-gate if (err != 0) { 3500Sstevel@tonic-gate if (!caller_hold_lock) 3510Sstevel@tonic-gate WAKE_SCTP(sctp); 3520Sstevel@tonic-gate return (err); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate /* Need to send ASCONF messages */ 3550Sstevel@tonic-gate if (do_asconf) { 3560Sstevel@tonic-gate err = sctp_add_ip(sctp, addrs, addrcnt); 3570Sstevel@tonic-gate if (err != 0) { 3580Sstevel@tonic-gate sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE); 3590Sstevel@tonic-gate if (!caller_hold_lock) 3600Sstevel@tonic-gate WAKE_SCTP(sctp); 3610Sstevel@tonic-gate return (err); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate if (!caller_hold_lock) 3650Sstevel@tonic-gate WAKE_SCTP(sctp); 3660Sstevel@tonic-gate if (do_asconf) 3670Sstevel@tonic-gate sctp_process_sendq(sctp); 3680Sstevel@tonic-gate return (0); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Remove one or more addresses bound to the sctp_t. 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate int 3750Sstevel@tonic-gate sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 3760Sstevel@tonic-gate boolean_t caller_hold_lock) 3770Sstevel@tonic-gate { 3780Sstevel@tonic-gate int error = 0; 3790Sstevel@tonic-gate boolean_t do_asconf = B_FALSE; 380*852Svi117747 uchar_t *ulist = NULL; 381*852Svi117747 size_t usize = 0; 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate if (!caller_hold_lock) 3840Sstevel@tonic-gate RUN_SCTP(sctp); 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_ESTABLISHED) { 3870Sstevel@tonic-gate if (!caller_hold_lock) 3880Sstevel@tonic-gate WAKE_SCTP(sctp); 3890Sstevel@tonic-gate return (EINVAL); 3900Sstevel@tonic-gate } 391252Svi117747 /* 392252Svi117747 * Fail the remove if we are beyond listen, but can't send this 393252Svi117747 * to the peer. 394252Svi117747 */ 395252Svi117747 if (sctp->sctp_state > SCTPS_LISTEN) { 396252Svi117747 if (!sctp_addip_enabled || !sctp->sctp_understands_asconf || 397252Svi117747 !sctp->sctp_understands_addip) { 398252Svi117747 if (!caller_hold_lock) 399252Svi117747 WAKE_SCTP(sctp); 400252Svi117747 return (EINVAL); 401252Svi117747 } 4020Sstevel@tonic-gate do_asconf = B_TRUE; 403252Svi117747 } 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate /* Can't delete the last address nor all of the addresses */ 4060Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) { 4070Sstevel@tonic-gate if (!caller_hold_lock) 4080Sstevel@tonic-gate WAKE_SCTP(sctp); 4090Sstevel@tonic-gate return (EINVAL); 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate 412*852Svi117747 if (cl_sctp_unlisten != NULL && !do_asconf && 413*852Svi117747 sctp->sctp_state > SCTPS_BOUND) { 414*852Svi117747 usize = sizeof (in6_addr_t) * addrcnt; 415*852Svi117747 ulist = kmem_alloc(usize, KM_SLEEP); 416*852Svi117747 } 417*852Svi117747 418*852Svi117747 error = sctp_del_ip(sctp, addrs, addrcnt, ulist, usize); 419*852Svi117747 if (error != 0) { 420*852Svi117747 if (ulist != NULL) 421*852Svi117747 kmem_free(ulist, usize); 422*852Svi117747 if (!caller_hold_lock) 423*852Svi117747 WAKE_SCTP(sctp); 424*852Svi117747 return (error); 425*852Svi117747 } 426*852Svi117747 /* ulist will be non-NULL only if cl_sctp_unlisten is non-NULL */ 427*852Svi117747 if (ulist != NULL) { 428*852Svi117747 ASSERT(cl_sctp_unlisten != NULL); 429*852Svi117747 (*cl_sctp_unlisten)(sctp->sctp_family, ulist, addrcnt, 430*852Svi117747 sctp->sctp_lport); 431*852Svi117747 /* ulist will be freed by the clustering module */ 432*852Svi117747 } 4330Sstevel@tonic-gate if (!caller_hold_lock) 4340Sstevel@tonic-gate WAKE_SCTP(sctp); 435*852Svi117747 if (do_asconf) 4360Sstevel@tonic-gate sctp_process_sendq(sctp); 4370Sstevel@tonic-gate return (error); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * If the "bind_to_req_port_only" parameter is set, if the requested port 4420Sstevel@tonic-gate * number is available, return it, If not return 0 4430Sstevel@tonic-gate * 4440Sstevel@tonic-gate * If "bind_to_req_port_only" parameter is not set and 4450Sstevel@tonic-gate * If the requested port number is available, return it. If not, return 4460Sstevel@tonic-gate * the first anonymous port we happen across. If no anonymous ports are 4470Sstevel@tonic-gate * available, return 0. addr is the requested local address, if any. 4480Sstevel@tonic-gate * 4490Sstevel@tonic-gate * In either case, when succeeding update the sctp_t to record the port number 4500Sstevel@tonic-gate * and insert it in the bind hash table. 4510Sstevel@tonic-gate */ 4520Sstevel@tonic-gate in_port_t 4530Sstevel@tonic-gate sctp_bindi(sctp_t *sctp, in_port_t port, int bind_to_req_port_only, 4540Sstevel@tonic-gate int user_specified) 4550Sstevel@tonic-gate { 4560Sstevel@tonic-gate /* number of times we have run around the loop */ 4570Sstevel@tonic-gate int count = 0; 4580Sstevel@tonic-gate /* maximum number of times to run around the loop */ 4590Sstevel@tonic-gate int loopmax; 4600Sstevel@tonic-gate zoneid_t zoneid = sctp->sctp_zoneid; 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate /* 4630Sstevel@tonic-gate * Lookup for free addresses is done in a loop and "loopmax" 4640Sstevel@tonic-gate * influences how long we spin in the loop 4650Sstevel@tonic-gate */ 4660Sstevel@tonic-gate if (bind_to_req_port_only) { 4670Sstevel@tonic-gate /* 4680Sstevel@tonic-gate * If the requested port is busy, don't bother to look 4690Sstevel@tonic-gate * for a new one. Setting loop maximum count to 1 has 4700Sstevel@tonic-gate * that effect. 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate loopmax = 1; 4730Sstevel@tonic-gate } else { 4740Sstevel@tonic-gate /* 4750Sstevel@tonic-gate * If the requested port is busy, look for a free one 4760Sstevel@tonic-gate * in the anonymous port range. 4770Sstevel@tonic-gate * Set loopmax appropriately so that one does not look 4780Sstevel@tonic-gate * forever in the case all of the anonymous ports are in use. 4790Sstevel@tonic-gate */ 4800Sstevel@tonic-gate loopmax = (sctp_largest_anon_port - 4810Sstevel@tonic-gate sctp_smallest_anon_port + 1); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate do { 4840Sstevel@tonic-gate uint16_t lport; 4850Sstevel@tonic-gate sctp_tf_t *tbf; 4860Sstevel@tonic-gate sctp_t *lsctp; 4870Sstevel@tonic-gate int addrcmp; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate lport = htons(port); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate /* 4920Sstevel@tonic-gate * Ensure that the sctp_t is not currently in the bind hash. 4930Sstevel@tonic-gate * Hold the lock on the hash bucket to ensure that 4940Sstevel@tonic-gate * the duplicate check plus the insertion is an atomic 4950Sstevel@tonic-gate * operation. 4960Sstevel@tonic-gate * 4970Sstevel@tonic-gate * This function does an inline lookup on the bind hash list 4980Sstevel@tonic-gate * Make sure that we access only members of sctp_t 4990Sstevel@tonic-gate * and that we don't look at sctp_sctp, since we are not 5000Sstevel@tonic-gate * doing a SCTPB_REFHOLD. For more details please see the notes 5010Sstevel@tonic-gate * in sctp_compress() 5020Sstevel@tonic-gate */ 5030Sstevel@tonic-gate sctp_bind_hash_remove(sctp); 5040Sstevel@tonic-gate tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)]; 5050Sstevel@tonic-gate mutex_enter(&tbf->tf_lock); 5060Sstevel@tonic-gate for (lsctp = tbf->tf_sctp; lsctp != NULL; 5070Sstevel@tonic-gate lsctp = lsctp->sctp_bind_hash) { 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate if (lport != lsctp->sctp_lport || 5100Sstevel@tonic-gate lsctp->sctp_zoneid != zoneid || 5110Sstevel@tonic-gate lsctp->sctp_state < SCTPS_BOUND) 5120Sstevel@tonic-gate continue; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate addrcmp = sctp_compare_saddrs(sctp, lsctp); 5150Sstevel@tonic-gate if (addrcmp != SCTP_ADDR_DISJOINT) { 5160Sstevel@tonic-gate if (!sctp->sctp_reuseaddr) { 5170Sstevel@tonic-gate /* in use */ 5180Sstevel@tonic-gate break; 5190Sstevel@tonic-gate } else if (lsctp->sctp_state == SCTPS_BOUND || 5200Sstevel@tonic-gate lsctp->sctp_state == SCTPS_LISTEN) { 5210Sstevel@tonic-gate /* 5220Sstevel@tonic-gate * socket option SO_REUSEADDR is set 5230Sstevel@tonic-gate * on the binding sctp_t. 5240Sstevel@tonic-gate * 5250Sstevel@tonic-gate * We have found a match of IP source 5260Sstevel@tonic-gate * address and source port, which is 5270Sstevel@tonic-gate * refused regardless of the 5280Sstevel@tonic-gate * SO_REUSEADDR setting, so we break. 5290Sstevel@tonic-gate */ 5300Sstevel@tonic-gate break; 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate if (lsctp != NULL) { 5350Sstevel@tonic-gate /* The port number is busy */ 5360Sstevel@tonic-gate mutex_exit(&tbf->tf_lock); 5370Sstevel@tonic-gate } else { 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * This port is ours. Insert in fanout and mark as 5400Sstevel@tonic-gate * bound to prevent others from getting the port 5410Sstevel@tonic-gate * number. 5420Sstevel@tonic-gate */ 5430Sstevel@tonic-gate sctp->sctp_state = SCTPS_BOUND; 5440Sstevel@tonic-gate sctp->sctp_lport = lport; 5450Sstevel@tonic-gate sctp->sctp_sctph->sh_sport = sctp->sctp_lport; 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf); 5480Sstevel@tonic-gate sctp_bind_hash_insert(tbf, sctp, 1); 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate mutex_exit(&tbf->tf_lock); 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate /* 5530Sstevel@tonic-gate * We don't want sctp_next_port_to_try to "inherit" 5540Sstevel@tonic-gate * a port number supplied by the user in a bind. 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate if (user_specified != 0) 5570Sstevel@tonic-gate return (port); 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * This is the only place where sctp_next_port_to_try 5610Sstevel@tonic-gate * is updated. After the update, it may or may not 5620Sstevel@tonic-gate * be in the valid range. 5630Sstevel@tonic-gate */ 5640Sstevel@tonic-gate sctp_next_port_to_try = port + 1; 5650Sstevel@tonic-gate return (port); 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate if ((count == 0) && (user_specified)) { 5690Sstevel@tonic-gate /* 5700Sstevel@tonic-gate * We may have to return an anonymous port. So 5710Sstevel@tonic-gate * get one to start with. 5720Sstevel@tonic-gate */ 5730Sstevel@tonic-gate port = sctp_update_next_port(sctp_next_port_to_try); 5740Sstevel@tonic-gate user_specified = 0; 5750Sstevel@tonic-gate } else { 5760Sstevel@tonic-gate port = sctp_update_next_port(port + 1); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate /* 5800Sstevel@tonic-gate * Don't let this loop run forever in the case where 5810Sstevel@tonic-gate * all of the anonymous ports are in use. 5820Sstevel@tonic-gate */ 5830Sstevel@tonic-gate } while (++count < loopmax); 5840Sstevel@tonic-gate return (0); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate /* 5880Sstevel@tonic-gate * Don't let port fall into the privileged range. 5890Sstevel@tonic-gate * Since the extra privileged ports can be arbitrary we also 5900Sstevel@tonic-gate * ensure that we exclude those from consideration. 5910Sstevel@tonic-gate * sctp_g_epriv_ports is not sorted thus we loop over it until 5920Sstevel@tonic-gate * there are no changes. 5930Sstevel@tonic-gate * 5940Sstevel@tonic-gate * Note: No locks are held when inspecting sctp_g_*epriv_ports 5950Sstevel@tonic-gate * but instead the code relies on: 5960Sstevel@tonic-gate * - the fact that the address of the array and its size never changes 5970Sstevel@tonic-gate * - the atomic assignment of the elements of the array 5980Sstevel@tonic-gate */ 5990Sstevel@tonic-gate in_port_t 6000Sstevel@tonic-gate sctp_update_next_port(in_port_t port) 6010Sstevel@tonic-gate { 6020Sstevel@tonic-gate int i; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate retry: 6050Sstevel@tonic-gate if (port < sctp_smallest_anon_port || port > sctp_largest_anon_port) 6060Sstevel@tonic-gate port = sctp_smallest_anon_port; 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate if (port < sctp_smallest_nonpriv_port) 6090Sstevel@tonic-gate port = sctp_smallest_nonpriv_port; 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate for (i = 0; i < sctp_g_num_epriv_ports; i++) { 6120Sstevel@tonic-gate if (port == sctp_g_epriv_ports[i]) { 6130Sstevel@tonic-gate port++; 6140Sstevel@tonic-gate /* 6150Sstevel@tonic-gate * Make sure whether the port is in the 6160Sstevel@tonic-gate * valid range. 6170Sstevel@tonic-gate * 6180Sstevel@tonic-gate * XXX Note that if sctp_g_epriv_ports contains 6190Sstevel@tonic-gate * all the anonymous ports this will be an 6200Sstevel@tonic-gate * infinite loop. 6210Sstevel@tonic-gate */ 6220Sstevel@tonic-gate goto retry; 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate return (port); 6260Sstevel@tonic-gate } 627