michael@0: /*- michael@0: * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are met: michael@0: * michael@0: * a) Redistributions of source code must retain the above copyright notice, michael@0: * this list of conditions and the following disclaimer. michael@0: * michael@0: * b) Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in michael@0: * the documentation and/or other materials provided with the distribution. michael@0: * michael@0: * c) Neither the name of Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef __FreeBSD__ michael@0: #include michael@0: __FBSDID("$FreeBSD: head/sys/netinet/sctp_peeloff.c 243565 2012-11-26 16:44:03Z tuexen $"); michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #if defined(__APPLE__) michael@0: #define APPLE_FILE_NO 5 michael@0: #endif michael@0: michael@0: int michael@0: sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id) michael@0: { michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_tcb *stcb; michael@0: uint32_t state; michael@0: michael@0: if (head == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF); michael@0: return (EBADF); michael@0: } michael@0: inp = (struct sctp_inpcb *)head->so_pcb; michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT); michael@0: return (EFAULT); michael@0: } michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP); michael@0: return (EOPNOTSUPP); michael@0: } michael@0: stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); michael@0: if (stcb == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT); michael@0: return (ENOENT); michael@0: } michael@0: state = SCTP_GET_STATE((&stcb->asoc)); michael@0: if ((state == SCTP_STATE_EMPTY) || michael@0: (state == SCTP_STATE_INUSE)) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); michael@0: return (ENOTCONN); michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: /* We are clear to peel this one off */ michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id) michael@0: { michael@0: struct sctp_inpcb *inp, *n_inp; michael@0: struct sctp_tcb *stcb; michael@0: uint32_t state; michael@0: michael@0: inp = (struct sctp_inpcb *)head->so_pcb; michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT); michael@0: return (EFAULT); michael@0: } michael@0: stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); michael@0: if (stcb == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); michael@0: return (ENOTCONN); michael@0: } michael@0: michael@0: state = SCTP_GET_STATE((&stcb->asoc)); michael@0: if ((state == SCTP_STATE_EMPTY) || michael@0: (state == SCTP_STATE_INUSE)) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); michael@0: return (ENOTCONN); michael@0: } michael@0: michael@0: n_inp = (struct sctp_inpcb *)so->so_pcb; michael@0: n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | michael@0: SCTP_PCB_FLAGS_CONNECTED | michael@0: SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */ michael@0: (SCTP_PCB_COPY_FLAGS & inp->sctp_flags)); michael@0: n_inp->sctp_socket = so; michael@0: n_inp->sctp_features = inp->sctp_features; michael@0: n_inp->sctp_mobility_features = inp->sctp_mobility_features; michael@0: n_inp->sctp_frag_point = inp->sctp_frag_point; michael@0: n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off; michael@0: n_inp->sctp_ecn_enable = inp->sctp_ecn_enable; michael@0: n_inp->partial_delivery_point = inp->partial_delivery_point; michael@0: n_inp->sctp_context = inp->sctp_context; michael@0: n_inp->local_strreset_support = inp->local_strreset_support; michael@0: n_inp->inp_starting_point_for_iterator = NULL; michael@0: /* copy in the authentication parameters from the original endpoint */ michael@0: if (n_inp->sctp_ep.local_hmacs) michael@0: sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs); michael@0: n_inp->sctp_ep.local_hmacs = michael@0: sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); michael@0: if (n_inp->sctp_ep.local_auth_chunks) michael@0: sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks); michael@0: n_inp->sctp_ep.local_auth_chunks = michael@0: sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); michael@0: (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, michael@0: &n_inp->sctp_ep.shared_keys); michael@0: #if defined(__Userspace__) michael@0: n_inp->ulp_info = inp->ulp_info; michael@0: n_inp->recv_callback = inp->recv_callback; michael@0: n_inp->send_callback = inp->send_callback; michael@0: n_inp->send_sb_threshold = inp->send_sb_threshold; michael@0: #endif michael@0: /* michael@0: * Now we must move it from one hash table to another and get the michael@0: * stcb in the right place. michael@0: */ michael@0: sctp_move_pcb_and_assoc(inp, n_inp, stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: michael@0: #if defined(__FreeBSD__) michael@0: sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT); michael@0: #else michael@0: sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK); michael@0: #endif michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: #if defined(HAVE_SCTP_PEELOFF_SOCKOPT) michael@0: struct socket * michael@0: sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error) michael@0: { michael@0: #if defined(__Userspace__) michael@0: /* if __Userspace__ chooses to originally not support peeloff, put it here... */ michael@0: #endif michael@0: #if defined(__Panda__) michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: #else michael@0: struct socket *newso; michael@0: struct sctp_inpcb *inp, *n_inp; michael@0: struct sctp_tcb *stcb; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n"); michael@0: inp = (struct sctp_inpcb *)head->so_pcb; michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT); michael@0: *error = EFAULT; michael@0: return (NULL); michael@0: } michael@0: stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); michael@0: if (stcb == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); michael@0: *error = ENOTCONN; michael@0: return (NULL); michael@0: } michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_SET(head->so_vnet); michael@0: #endif michael@0: newso = sonewconn(head, SS_ISCONNECTED michael@0: #if defined(__APPLE__) michael@0: , NULL michael@0: #elif defined(__Panda__) michael@0: /* place this socket in the assoc's vrf id */ michael@0: , NULL, stcb->asoc.vrf_id michael@0: #endif michael@0: ); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: if (newso == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n"); michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM); michael@0: *error = ENOMEM; michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: return (NULL); michael@0: michael@0: } michael@0: #if defined(__APPLE__) michael@0: else { michael@0: SCTP_SOCKET_LOCK(newso, 1); michael@0: } michael@0: #endif michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: n_inp = (struct sctp_inpcb *)newso->so_pcb; michael@0: SOCK_LOCK(head); michael@0: n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | michael@0: SCTP_PCB_FLAGS_CONNECTED | michael@0: SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */ michael@0: (SCTP_PCB_COPY_FLAGS & inp->sctp_flags)); michael@0: n_inp->sctp_features = inp->sctp_features; michael@0: n_inp->sctp_frag_point = inp->sctp_frag_point; michael@0: n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off; michael@0: n_inp->sctp_ecn_enable = inp->sctp_ecn_enable; michael@0: n_inp->partial_delivery_point = inp->partial_delivery_point; michael@0: n_inp->sctp_context = inp->sctp_context; michael@0: n_inp->local_strreset_support = inp->local_strreset_support; michael@0: n_inp->inp_starting_point_for_iterator = NULL; michael@0: #if defined(__Userspace__) michael@0: n_inp->ulp_info = inp->ulp_info; michael@0: n_inp->recv_callback = inp->recv_callback; michael@0: n_inp->send_callback = inp->send_callback; michael@0: n_inp->send_sb_threshold = inp->send_sb_threshold; michael@0: #endif michael@0: michael@0: /* copy in the authentication parameters from the original endpoint */ michael@0: if (n_inp->sctp_ep.local_hmacs) michael@0: sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs); michael@0: n_inp->sctp_ep.local_hmacs = michael@0: sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); michael@0: if (n_inp->sctp_ep.local_auth_chunks) michael@0: sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks); michael@0: n_inp->sctp_ep.local_auth_chunks = michael@0: sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); michael@0: (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, michael@0: &n_inp->sctp_ep.shared_keys); michael@0: michael@0: n_inp->sctp_socket = newso; michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { michael@0: sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE); michael@0: n_inp->sctp_ep.auto_close_time = 0; michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL, michael@0: SCTP_FROM_SCTP_PEELOFF+SCTP_LOC_1); michael@0: } michael@0: /* Turn off any non-blocking semantic. */ michael@0: SCTP_CLEAR_SO_NBIO(newso); michael@0: newso->so_state |= SS_ISCONNECTED; michael@0: /* We remove it right away */ michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__) michael@0: #ifdef SCTP_LOCK_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) { michael@0: sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK); michael@0: } michael@0: #endif michael@0: TAILQ_REMOVE(&head->so_comp, newso, so_list); michael@0: head->so_qlen--; michael@0: SOCK_UNLOCK(head); michael@0: #else michael@0: newso = TAILQ_FIRST(&head->so_q); michael@0: if (soqremque(newso, 1) == 0) { michael@0: SCTP_PRINTF("soremque failed, peeloff-fails (invarients would panic)\n"); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); michael@0: *error = ENOTCONN; michael@0: return (NULL); michael@0: michael@0: } michael@0: #endif michael@0: /* michael@0: * Now we must move it from one hash table to another and get the michael@0: * stcb in the right place. michael@0: */ michael@0: sctp_move_pcb_and_assoc(inp, n_inp, stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: /* michael@0: * And now the final hack. We move data in the pending side i.e. michael@0: * head to the new socket buffer. Let the GRUBBING begin :-0 michael@0: */ michael@0: #if defined(__FreeBSD__) michael@0: sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT); michael@0: #else michael@0: sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK); michael@0: #endif michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: return (newso); michael@0: #endif michael@0: } michael@0: #endif