sctp_bind.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * CDDL HEADER START
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms of the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Common Development and Distribution License, Version 1.0 only
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the "License"). You may not use this file except in compliance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * or http://www.opensolaris.org/os/licensing.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing permissions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL HEADER in each
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below this CDDL HEADER, with the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * fields enclosed by brackets "[]" replaced with your own identifying
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * information: Portions Copyright [yyyy] [name of copyright owner]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * CDDL HEADER END
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Use is subject to license terms.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#pragma ident "%Z%%M% %I% %E% SMI"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/types.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/systm.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/stream.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/cmn_err.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/kmem.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#define _SUN_TPI_VERSION 2
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/tihdr.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/stropts.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/socket.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/random.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <sys/policy.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <netinet/in.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <netinet/ip6.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <inet/common.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <inet/ip.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <inet/ip6.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include <inet/ipclassifier.h>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include "sctp_impl.h"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include "sctp_asconf.h"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster#include "sctp_addr.h"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosteruint_t sctp_next_port_to_try;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns 0 on success, EACCES on permission failure.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterstatic int
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fostersctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Get a valid port (within the anonymous range and should not
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * be a privileged one) to use if the user has not given a port.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If multiple threads are here, they may all start with
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the same initial port. But, it should be fine as long as
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * sctp_bindi will ensure that no two threads will be assigned
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the same port.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (*requested_port == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *requested_port = sctp_update_next_port(sctp_next_port_to_try);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *user_specified = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int i;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean_t priv = B_FALSE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If the requested_port is in the well-known privileged range,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * verify that the stream was opened by a privileged user.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Note: No locks are held when inspecting sctp_g_*epriv_ports
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * but instead the code relies on:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * - the fact that the address of the array and its size never
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * changes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * - the atomic assignment of the elements of the array
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (*requested_port < sctp_smallest_nonpriv_port) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster priv = B_TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (i = 0; i < sctp_g_num_epriv_ports; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (*requested_port == sctp_g_epriv_ports[i]) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster priv = B_TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (priv) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * sctp_bind() should take a cred_t argument so that
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * we can use it here.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (secpolicy_net_privaddr(sctp->sctp_credp,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *requested_port) != 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster dprint(1,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ("sctp_bind(x): no prive for port %d",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *requested_port));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (TACCES);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *user_specified = 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterint
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fostersctp_listen(sctp_t *sctp)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_tf_t *tf;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RUN_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * TCP handles listen() increasing the backlog, need to check
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * if it should be handled here too - VENU.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp->sctp_state > SCTPS_BOUND) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (EINVAL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Do an anonymous bind for unbound socket doing listen(). */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp->sctp_nsaddrs == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster struct sockaddr_storage ss;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int ret;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bzero(&ss, sizeof (ss));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ss.ss_family = sctp->sctp_family;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sizeof (ss))) != 0)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (ret);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RUN_SCTP(sctp)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp->sctp_state = SCTPS_LISTEN;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp->sctp_last_secret_update = lbolt64;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_listen_hash_insert(tf, sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Bind the sctp_t to a sockaddr, which includes an address and other
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * information, such as port or flowinfo.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterint
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fostersctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int user_specified;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean_t bind_to_req_port_only;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster in_port_t requested_port;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster in_port_t allocated_port;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int err = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ASSERT(sctp != NULL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ASSERT(sa);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RUN_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp->sctp_state > SCTPS_BOUND) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = EINVAL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster goto done;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster switch (sa->sa_family) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case AF_INET:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (len < sizeof (struct sockaddr_in) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp->sctp_family == AF_INET6) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = EINVAL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster goto done;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case AF_INET6:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (len < sizeof (struct sockaddr_in6) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp->sctp_family == AF_INET) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = EINVAL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster goto done;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Set the flowinfo. */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp->sctp_ip6h->ip6_vcf =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (((struct sockaddr_in6 *)sa)->sin6_flowinfo &
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ~IPV6_VERS_AND_FLOW_MASK);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster default:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = EAFNOSUPPORT;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster goto done;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp_select_port(sctp, &requested_port, &user_specified) != 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = EPERM;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster goto done;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE)) != 0)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster goto done;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster allocated_port = sctp_bindi(sctp, requested_port,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bind_to_req_port_only, user_specified);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (allocated_port == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_free_saddrs(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (bind_to_req_port_only) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = EADDRINUSE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster goto done;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = EADDRNOTAVAIL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster goto done;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ASSERT(sctp->sctp_state == SCTPS_BOUND);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterdone:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (err);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Perform bind/unbind operation of a list of addresses on a sctp_t
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterint
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fostersctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ASSERT(sctp != NULL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ASSERT(addrs != NULL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ASSERT(addrcnt > 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster switch (bindop) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case SCTP_BINDX_ADD_ADDR:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case SCTP_BINDX_REM_ADDR:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster default:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (EINVAL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Add a list of addresses to a sctp_t.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterint
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fostersctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean_t caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int err = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean_t do_asconf = B_FALSE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RUN_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp->sctp_state > SCTPS_ESTABLISHED) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (EINVAL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp->sctp_state > SCTPS_LISTEN && sctp_addip_enabled)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster do_asconf = B_TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = sctp_valid_addr_list(sctp, addrs, addrcnt);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (err != 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (err);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Need to send ASCONF messages */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (do_asconf) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = sctp_add_ip(sctp, addrs, addrcnt);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (err != 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (err);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (do_asconf)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_process_sendq(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Remove one or more addresses bound to the sctp_t.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterint
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fostersctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean_t caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int error = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean_t do_asconf = B_FALSE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RUN_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp->sctp_state > SCTPS_ESTABLISHED) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (EINVAL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp->sctp_state > SCTPS_LISTEN && sctp_addip_enabled)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster do_asconf = B_TRUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Can't delete the last address nor all of the addresses */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (EINVAL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster error = sctp_del_ip(sctp, addrs, addrcnt);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!caller_hold_lock)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster WAKE_SCTP(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (error == 0 && do_asconf)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_process_sendq(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (error);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If the "bind_to_req_port_only" parameter is set, if the requested port
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * number is available, return it, If not return 0
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If "bind_to_req_port_only" parameter is not set and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If the requested port number is available, return it. If not, return
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the first anonymous port we happen across. If no anonymous ports are
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * available, return 0. addr is the requested local address, if any.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * In either case, when succeeding update the sctp_t to record the port number
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and insert it in the bind hash table.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterin_port_t
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fostersctp_bindi(sctp_t *sctp, in_port_t port, int bind_to_req_port_only,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int user_specified)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* number of times we have run around the loop */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int count = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* maximum number of times to run around the loop */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int loopmax;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster zoneid_t zoneid = sctp->sctp_zoneid;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Lookup for free addresses is done in a loop and "loopmax"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * influences how long we spin in the loop
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (bind_to_req_port_only) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If the requested port is busy, don't bother to look
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for a new one. Setting loop maximum count to 1 has
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * that effect.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster loopmax = 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If the requested port is busy, look for a free one
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * in the anonymous port range.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Set loopmax appropriately so that one does not look
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * forever in the case all of the anonymous ports are in use.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster loopmax = (sctp_largest_anon_port -
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_smallest_anon_port + 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster do {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster uint16_t lport;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_tf_t *tbf;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_t *lsctp;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int addrcmp;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lport = htons(port);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Ensure that the sctp_t is not currently in the bind hash.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Hold the lock on the hash bucket to ensure that
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the duplicate check plus the insertion is an atomic
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * operation.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This function does an inline lookup on the bind hash list
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Make sure that we access only members of sctp_t
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and that we don't look at sctp_sctp, since we are not
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * doing a SCTPB_REFHOLD. For more details please see the notes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * in sctp_compress()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp_bind_hash_remove(sctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mutex_enter(&tbf->tf_lock);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (lsctp = tbf->tf_sctp; lsctp != NULL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lsctp = lsctp->sctp_bind_hash) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (lport != lsctp->sctp_lport ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lsctp->sctp_zoneid != zoneid ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lsctp->sctp_state < SCTPS_BOUND)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster continue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster addrcmp = sctp_compare_saddrs(sctp, lsctp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (addrcmp != SCTP_ADDR_DISJOINT) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!sctp->sctp_reuseaddr) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* in use */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (lsctp->sctp_state == SCTPS_BOUND ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lsctp->sctp_state == SCTPS_LISTEN) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * socket option SO_REUSEADDR is set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * on the binding sctp_t.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * We have found a match of IP source
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * address and source port, which is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * refused regardless of the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * SO_REUSEADDR setting, so we break.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (lsctp != NULL) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* The port number is busy */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mutex_exit(&tbf->tf_lock);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This port is ours. Insert in fanout and mark as
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * bound to prevent others from getting the port
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * number.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp->sctp_state = SCTPS_BOUND;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp->sctp_lport = lport;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf);
sctp_bind_hash_insert(tbf, sctp, 1);
mutex_exit(&tbf->tf_lock);
/*
* We don't want sctp_next_port_to_try to "inherit"
* a port number supplied by the user in a bind.
*/
if (user_specified != 0)
return (port);
/*
* This is the only place where sctp_next_port_to_try
* is updated. After the update, it may or may not
* be in the valid range.
*/
sctp_next_port_to_try = port + 1;
return (port);
}
if ((count == 0) && (user_specified)) {
/*
* We may have to return an anonymous port. So
* get one to start with.
*/
port = sctp_update_next_port(sctp_next_port_to_try);
user_specified = 0;
} else {
port = sctp_update_next_port(port + 1);
}
/*
* Don't let this loop run forever in the case where
* all of the anonymous ports are in use.
*/
} while (++count < loopmax);
return (0);
}
/*
* Don't let port fall into the privileged range.
* Since the extra privileged ports can be arbitrary we also
* ensure that we exclude those from consideration.
* sctp_g_epriv_ports is not sorted thus we loop over it until
* there are no changes.
*
* Note: No locks are held when inspecting sctp_g_*epriv_ports
* but instead the code relies on:
* - the fact that the address of the array and its size never changes
* - the atomic assignment of the elements of the array
*/
in_port_t
sctp_update_next_port(in_port_t port)
{
int i;
retry:
if (port < sctp_smallest_anon_port || port > sctp_largest_anon_port)
port = sctp_smallest_anon_port;
if (port < sctp_smallest_nonpriv_port)
port = sctp_smallest_nonpriv_port;
for (i = 0; i < sctp_g_num_epriv_ports; i++) {
if (port == sctp_g_epriv_ports[i]) {
port++;
/*
* Make sure whether the port is in the
* valid range.
*
* XXX Note that if sctp_g_epriv_ports contains
* all the anonymous ports this will be an
* infinite loop.
*/
goto retry;
}
}
return (port);
}