netwerk/sctp/src/netinet/sctp_peeloff.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/sctp/src/netinet/sctp_peeloff.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,312 @@
     1.4 +/*-
     1.5 + * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
     1.6 + * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
     1.7 + * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + * a) Redistributions of source code must retain the above copyright notice,
    1.13 + *    this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + * b) Redistributions in binary form must reproduce the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer in
    1.17 + *    the documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * c) Neither the name of Cisco Systems, Inc. nor the names of its
    1.20 + *    contributors may be used to endorse or promote products derived
    1.21 + *    from this software without specific prior written permission.
    1.22 + *
    1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    1.25 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.26 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.27 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.28 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.29 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.30 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.31 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.32 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    1.33 + * THE POSSIBILITY OF SUCH DAMAGE.
    1.34 + */
    1.35 +
    1.36 +#ifdef __FreeBSD__
    1.37 +#include <sys/cdefs.h>
    1.38 +__FBSDID("$FreeBSD: head/sys/netinet/sctp_peeloff.c 243565 2012-11-26 16:44:03Z tuexen $");
    1.39 +#endif
    1.40 +
    1.41 +#include <netinet/sctp_os.h>
    1.42 +#include <netinet/sctp_pcb.h>
    1.43 +#include <netinet/sctputil.h>
    1.44 +#include <netinet/sctp_var.h>
    1.45 +#include <netinet/sctp_var.h>
    1.46 +#include <netinet/sctp_sysctl.h>
    1.47 +#include <netinet/sctp.h>
    1.48 +#include <netinet/sctp_uio.h>
    1.49 +#include <netinet/sctp_peeloff.h>
    1.50 +#include <netinet/sctputil.h>
    1.51 +#include <netinet/sctp_auth.h>
    1.52 +
    1.53 +#if defined(__APPLE__)
    1.54 +#define APPLE_FILE_NO 5
    1.55 +#endif
    1.56 +
    1.57 +int
    1.58 +sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
    1.59 +{
    1.60 +	struct sctp_inpcb *inp;
    1.61 +	struct sctp_tcb *stcb;
    1.62 +	uint32_t state;
    1.63 +
    1.64 +	if (head == NULL) {
    1.65 +		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF);
    1.66 +		return (EBADF);
    1.67 +	}
    1.68 +	inp = (struct sctp_inpcb *)head->so_pcb;
    1.69 +	if (inp == NULL) {
    1.70 +		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
    1.71 +		return (EFAULT);
    1.72 +	}
    1.73 +	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
    1.74 +	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
    1.75 +		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP);
    1.76 +		return (EOPNOTSUPP);
    1.77 +	}
    1.78 +	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
    1.79 +	if (stcb == NULL) {
    1.80 +		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
    1.81 +		return (ENOENT);
    1.82 +	}
    1.83 +	state = SCTP_GET_STATE((&stcb->asoc));
    1.84 +	if ((state == SCTP_STATE_EMPTY) ||
    1.85 +	    (state == SCTP_STATE_INUSE)) {
    1.86 +		SCTP_TCB_UNLOCK(stcb);
    1.87 +		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
    1.88 +		return (ENOTCONN);
    1.89 +	}
    1.90 +	SCTP_TCB_UNLOCK(stcb);
    1.91 +	/* We are clear to peel this one off */
    1.92 +	return (0);
    1.93 +}
    1.94 +
    1.95 +int
    1.96 +sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
    1.97 +{
    1.98 +	struct sctp_inpcb *inp, *n_inp;
    1.99 +	struct sctp_tcb *stcb;
   1.100 +	uint32_t state;
   1.101 +
   1.102 +	inp = (struct sctp_inpcb *)head->so_pcb;
   1.103 +	if (inp == NULL) {
   1.104 +		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
   1.105 +		return (EFAULT);
   1.106 +	}
   1.107 +	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
   1.108 +	if (stcb == NULL) {
   1.109 +		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
   1.110 +		return (ENOTCONN);
   1.111 +	}
   1.112 +
   1.113 +	state = SCTP_GET_STATE((&stcb->asoc));
   1.114 +	if ((state == SCTP_STATE_EMPTY) ||
   1.115 +	    (state == SCTP_STATE_INUSE)) {
   1.116 +		SCTP_TCB_UNLOCK(stcb);
   1.117 +		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
   1.118 +		return (ENOTCONN);
   1.119 +	}
   1.120 +
   1.121 +	n_inp = (struct sctp_inpcb *)so->so_pcb;
   1.122 +	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
   1.123 +	    SCTP_PCB_FLAGS_CONNECTED |
   1.124 +	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
   1.125 +	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
   1.126 +	n_inp->sctp_socket = so;
   1.127 +	n_inp->sctp_features = inp->sctp_features;
   1.128 +	n_inp->sctp_mobility_features = inp->sctp_mobility_features;
   1.129 +	n_inp->sctp_frag_point = inp->sctp_frag_point;
   1.130 +	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
   1.131 +	n_inp->sctp_ecn_enable = inp->sctp_ecn_enable;
   1.132 +	n_inp->partial_delivery_point = inp->partial_delivery_point;
   1.133 +	n_inp->sctp_context = inp->sctp_context;
   1.134 +	n_inp->local_strreset_support = inp->local_strreset_support;
   1.135 +	n_inp->inp_starting_point_for_iterator = NULL;
   1.136 +	/* copy in the authentication parameters from the original endpoint */
   1.137 +	if (n_inp->sctp_ep.local_hmacs)
   1.138 +		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
   1.139 +	n_inp->sctp_ep.local_hmacs =
   1.140 +	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
   1.141 +	if (n_inp->sctp_ep.local_auth_chunks)
   1.142 +		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
   1.143 +	n_inp->sctp_ep.local_auth_chunks =
   1.144 +	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
   1.145 +	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
   1.146 +	    &n_inp->sctp_ep.shared_keys);
   1.147 +#if defined(__Userspace__)
   1.148 +	n_inp->ulp_info = inp->ulp_info;
   1.149 +	n_inp->recv_callback = inp->recv_callback;
   1.150 +	n_inp->send_callback = inp->send_callback;
   1.151 +	n_inp->send_sb_threshold = inp->send_sb_threshold;
   1.152 +#endif
   1.153 +	/*
   1.154 +	 * Now we must move it from one hash table to another and get the
   1.155 +	 * stcb in the right place.
   1.156 +	 */
   1.157 +	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
   1.158 +	atomic_add_int(&stcb->asoc.refcnt, 1);
   1.159 +	SCTP_TCB_UNLOCK(stcb);
   1.160 +
   1.161 +#if defined(__FreeBSD__)
   1.162 +	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
   1.163 +#else
   1.164 +	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK);
   1.165 +#endif
   1.166 +	atomic_subtract_int(&stcb->asoc.refcnt, 1);
   1.167 +
   1.168 +	return (0);
   1.169 +}
   1.170 +
   1.171 +#if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
   1.172 +struct socket *
   1.173 +sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
   1.174 +{
   1.175 +#if defined(__Userspace__)
   1.176 +    /* if __Userspace__ chooses to originally not support peeloff, put it here... */
   1.177 +#endif
   1.178 +#if defined(__Panda__)
   1.179 +	SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EINVAL);
   1.180 +	*error = EINVAL;
   1.181 +	return (NULL);
   1.182 +#else
   1.183 +	struct socket *newso;
   1.184 +	struct sctp_inpcb *inp, *n_inp;
   1.185 +	struct sctp_tcb *stcb;
   1.186 +
   1.187 +	SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
   1.188 +	inp = (struct sctp_inpcb *)head->so_pcb;
   1.189 +	if (inp == NULL) {
   1.190 +		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
   1.191 +		*error = EFAULT;
   1.192 +		return (NULL);
   1.193 +	}
   1.194 +	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
   1.195 +	if (stcb == NULL) {
   1.196 +		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
   1.197 +		*error = ENOTCONN;
   1.198 +		return (NULL);
   1.199 +	}
   1.200 +	atomic_add_int(&stcb->asoc.refcnt, 1);
   1.201 +	SCTP_TCB_UNLOCK(stcb);
   1.202 +#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
   1.203 +	CURVNET_SET(head->so_vnet);
   1.204 +#endif
   1.205 +	newso = sonewconn(head, SS_ISCONNECTED
   1.206 +#if defined(__APPLE__)
   1.207 +	    , NULL
   1.208 +#elif defined(__Panda__)
   1.209 +	    /* place this socket in the assoc's vrf id */
   1.210 +	    , NULL, stcb->asoc.vrf_id
   1.211 +#endif
   1.212 +		);
   1.213 +#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
   1.214 +	CURVNET_RESTORE();
   1.215 +#endif
   1.216 +	if (newso == NULL) {
   1.217 +		SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
   1.218 +		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
   1.219 +		*error = ENOMEM;
   1.220 +		atomic_subtract_int(&stcb->asoc.refcnt, 1);
   1.221 +		return (NULL);
   1.222 +
   1.223 +	}
   1.224 +#if defined(__APPLE__)
   1.225 +	  else {
   1.226 +		SCTP_SOCKET_LOCK(newso, 1);
   1.227 +	}
   1.228 +#endif
   1.229 +	SCTP_TCB_LOCK(stcb);
   1.230 +	atomic_subtract_int(&stcb->asoc.refcnt, 1);
   1.231 +	n_inp = (struct sctp_inpcb *)newso->so_pcb;
   1.232 +	SOCK_LOCK(head);
   1.233 +	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
   1.234 +	    SCTP_PCB_FLAGS_CONNECTED |
   1.235 +	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
   1.236 +	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
   1.237 +	n_inp->sctp_features = inp->sctp_features;
   1.238 +	n_inp->sctp_frag_point = inp->sctp_frag_point;
   1.239 +	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
   1.240 +	n_inp->sctp_ecn_enable = inp->sctp_ecn_enable;
   1.241 +	n_inp->partial_delivery_point = inp->partial_delivery_point;
   1.242 +	n_inp->sctp_context = inp->sctp_context;
   1.243 +	n_inp->local_strreset_support = inp->local_strreset_support;
   1.244 +	n_inp->inp_starting_point_for_iterator = NULL;
   1.245 +#if defined(__Userspace__)
   1.246 +	n_inp->ulp_info = inp->ulp_info;
   1.247 +	n_inp->recv_callback = inp->recv_callback;
   1.248 +	n_inp->send_callback = inp->send_callback;
   1.249 +	n_inp->send_sb_threshold = inp->send_sb_threshold;
   1.250 +#endif
   1.251 +
   1.252 +	/* copy in the authentication parameters from the original endpoint */
   1.253 +	if (n_inp->sctp_ep.local_hmacs)
   1.254 +		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
   1.255 +	n_inp->sctp_ep.local_hmacs =
   1.256 +	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
   1.257 +	if (n_inp->sctp_ep.local_auth_chunks)
   1.258 +		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
   1.259 +	n_inp->sctp_ep.local_auth_chunks =
   1.260 +	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
   1.261 +	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
   1.262 +	    &n_inp->sctp_ep.shared_keys);
   1.263 +
   1.264 +	n_inp->sctp_socket = newso;
   1.265 +	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
   1.266 +		sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
   1.267 +		n_inp->sctp_ep.auto_close_time = 0;
   1.268 +		sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
   1.269 +				SCTP_FROM_SCTP_PEELOFF+SCTP_LOC_1);
   1.270 +	}
   1.271 +	/* Turn off any non-blocking semantic. */
   1.272 +	SCTP_CLEAR_SO_NBIO(newso);
   1.273 +        newso->so_state |= SS_ISCONNECTED;
   1.274 +	/* We remove it right away */
   1.275 +
   1.276 +#if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
   1.277 +#ifdef SCTP_LOCK_LOGGING
   1.278 +	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
   1.279 +		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
   1.280 +	}
   1.281 +#endif
   1.282 +	TAILQ_REMOVE(&head->so_comp, newso, so_list);
   1.283 +	head->so_qlen--;
   1.284 +	SOCK_UNLOCK(head);
   1.285 +#else
   1.286 +        newso = TAILQ_FIRST(&head->so_q);
   1.287 +	if (soqremque(newso, 1) == 0) {
   1.288 +		SCTP_PRINTF("soremque failed, peeloff-fails (invarients would panic)\n");
   1.289 +		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
   1.290 +		*error = ENOTCONN;
   1.291 +		return (NULL);
   1.292 +
   1.293 +	}
   1.294 +#endif
   1.295 +	/*
   1.296 +	 * Now we must move it from one hash table to another and get the
   1.297 +	 * stcb in the right place.
   1.298 +	 */
   1.299 +        sctp_move_pcb_and_assoc(inp, n_inp, stcb);
   1.300 +	atomic_add_int(&stcb->asoc.refcnt, 1);
   1.301 +	SCTP_TCB_UNLOCK(stcb);
   1.302 +	/*
   1.303 +	 * And now the final hack. We move data in the pending side i.e.
   1.304 +	 * head to the new socket buffer. Let the GRUBBING begin :-0
   1.305 +	 */
   1.306 +#if defined(__FreeBSD__)
   1.307 +	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
   1.308 +#else
   1.309 +	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK);
   1.310 +#endif
   1.311 +	atomic_subtract_int(&stcb->asoc.refcnt, 1);
   1.312 +	return (newso);
   1.313 +#endif
   1.314 +}
   1.315 +#endif

mercurial