1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * a) Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 14 * b) Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the distribution. 17 * 18 * c) Neither the name of Cisco Systems, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #include <netinet/sctp_os.h> 37 #include <netinet/sctp_pcb.h> 38 #include <netinet/sctputil.h> 39 #include <netinet/sctp_var.h> 40 #include <netinet/sctp_var.h> 41 #include <netinet/sctp_sysctl.h> 42 #include <netinet/sctp.h> 43 #include <netinet/sctp_uio.h> 44 #include <netinet/sctp_peeloff.h> 45 #include <netinet/sctputil.h> 46 #include <netinet/sctp_auth.h> 47 48 int 49 sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id) 50 { 51 struct sctp_inpcb *inp; 52 struct sctp_tcb *stcb; 53 uint32_t state; 54 55 if (head == NULL) { 56 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF); 57 return (EBADF); 58 } 59 inp = (struct sctp_inpcb *)head->so_pcb; 60 if (inp == NULL) { 61 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT); 62 return (EFAULT); 63 } 64 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 65 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 66 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP); 67 return (EOPNOTSUPP); 68 } 69 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); 70 if (stcb == NULL) { 71 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT); 72 return (ENOENT); 73 } 74 state = SCTP_GET_STATE(stcb); 75 if ((state == SCTP_STATE_EMPTY) || 76 (state == SCTP_STATE_INUSE)) { 77 SCTP_TCB_UNLOCK(stcb); 78 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); 79 return (ENOTCONN); 80 } 81 SCTP_TCB_UNLOCK(stcb); 82 /* We are clear to peel this one off */ 83 return (0); 84 } 85 86 int 87 sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id) 88 { 89 struct sctp_inpcb *inp, *n_inp; 90 struct sctp_tcb *stcb; 91 uint32_t state; 92 93 inp = (struct sctp_inpcb *)head->so_pcb; 94 if (inp == NULL) { 95 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT); 96 return (EFAULT); 97 } 98 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); 99 if (stcb == NULL) { 100 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); 101 return (ENOTCONN); 102 } 103 104 state = SCTP_GET_STATE(stcb); 105 if ((state == SCTP_STATE_EMPTY) || 106 (state == SCTP_STATE_INUSE)) { 107 SCTP_TCB_UNLOCK(stcb); 108 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); 109 return (ENOTCONN); 110 } 111 112 n_inp = (struct sctp_inpcb *)so->so_pcb; 113 n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | 114 SCTP_PCB_FLAGS_CONNECTED | 115 SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */ 116 (SCTP_PCB_COPY_FLAGS & inp->sctp_flags)); 117 n_inp->sctp_socket = so; 118 n_inp->sctp_features = inp->sctp_features; 119 n_inp->sctp_mobility_features = inp->sctp_mobility_features; 120 n_inp->sctp_frag_point = inp->sctp_frag_point; 121 n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off; 122 n_inp->ecn_supported = inp->ecn_supported; 123 n_inp->prsctp_supported = inp->prsctp_supported; 124 n_inp->auth_supported = inp->auth_supported; 125 n_inp->asconf_supported = inp->asconf_supported; 126 n_inp->reconfig_supported = inp->reconfig_supported; 127 n_inp->nrsack_supported = inp->nrsack_supported; 128 n_inp->pktdrop_supported = inp->pktdrop_supported; 129 n_inp->partial_delivery_point = inp->partial_delivery_point; 130 n_inp->sctp_context = inp->sctp_context; 131 n_inp->max_cwnd = inp->max_cwnd; 132 n_inp->local_strreset_support = inp->local_strreset_support; 133 /* copy in the authentication parameters from the original endpoint */ 134 if (n_inp->sctp_ep.local_hmacs) 135 sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs); 136 n_inp->sctp_ep.local_hmacs = 137 sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); 138 if (n_inp->sctp_ep.local_auth_chunks) 139 sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks); 140 n_inp->sctp_ep.local_auth_chunks = 141 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); 142 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, 143 &n_inp->sctp_ep.shared_keys); 144 /* 145 * Now we must move it from one hash table to another and get the 146 * stcb in the right place. 147 */ 148 sctp_move_pcb_and_assoc(inp, n_inp, stcb); 149 atomic_add_int(&stcb->asoc.refcnt, 1); 150 SCTP_TCB_UNLOCK(stcb); 151 152 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT); 153 atomic_subtract_int(&stcb->asoc.refcnt, 1); 154 155 return (0); 156 } 157