| 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 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 36 | #include <sys/cdefs.h> |
| 37 | __FBSDID("$FreeBSD$"); |
| 38 | #endif |
| 39 |
| 40 | #include <netinet/sctp_os.h> |
| 41 | #include <netinet/sctp_pcb.h> |
| 42 | #include <netinet/sctputil.h> |
| 43 | #include <netinet/sctp_var.h> |
| 44 | #include <netinet/sctp_var.h> |
| 45 | #include <netinet/sctp_sysctl.h> |
| 46 | #include <netinet/sctp.h> |
| 47 | #include <netinet/sctp_uio.h> |
| 48 | #include <netinet/sctp_peeloff.h> |
| 49 | #include <netinet/sctputil.h> |
| 50 | #include <netinet/sctp_auth.h> |
| 51 |
| 52 | int |
| 53 | sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id) |
| 54 | { |
| 55 | struct sctp_inpcb *inp; |
| 56 | struct sctp_tcb *stcb; |
| 57 | uint32_t state; |
| 58 |
| 59 | if (head == NULL) { |
| 60 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF); |
| 61 | return (EBADF); |
| 62 | } |
| 63 | inp = (struct sctp_inpcb *)head->so_pcb; |
| 64 | if (inp == NULL) { |
| 65 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT); |
| 66 | return (EFAULT); |
| 67 | } |
| 68 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 69 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
| 70 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP); |
| 71 | return (EOPNOTSUPP); |
| 72 | } |
| 73 | stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); |
| 74 | if (stcb == NULL) { |
| 75 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT); |
| 76 | return (ENOENT); |
| 77 | } |
| 78 | state = SCTP_GET_STATE(stcb); |
| 79 | if ((state == SCTP_STATE_EMPTY) || |
| 80 | (state == SCTP_STATE_INUSE)) { |
| 81 | SCTP_TCB_UNLOCK(stcb); |
| 82 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); |
| 83 | return (ENOTCONN); |
| 84 | } |
| 85 | SCTP_TCB_UNLOCK(stcb); |
| 86 | /* We are clear to peel this one off */ |
| 87 | return (0); |
| 88 | } |
| 89 |
| 90 | int |
| 91 | sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id) |
| 92 | { |
| 93 | struct sctp_inpcb *inp, *n_inp; |
| 94 | struct sctp_tcb *stcb; |
| 95 | uint32_t state; |
| 96 |
| 97 | inp = (struct sctp_inpcb *)head->so_pcb; |
| 98 | if (inp == NULL) { |
| 99 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT); |
| 100 | return (EFAULT); |
| 101 | } |
| 102 | stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); |
| 103 | if (stcb == NULL) { |
| 104 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); |
| 105 | return (ENOTCONN); |
| 106 | } |
| 107 |
| 108 | state = SCTP_GET_STATE(stcb); |
| 109 | if ((state == SCTP_STATE_EMPTY) || |
| 110 | (state == SCTP_STATE_INUSE)) { |
| 111 | SCTP_TCB_UNLOCK(stcb); |
| 112 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); |
| 113 | return (ENOTCONN); |
| 114 | } |
| 115 |
| 116 | n_inp = (struct sctp_inpcb *)so->so_pcb; |
| 117 | n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | |
| 118 | SCTP_PCB_FLAGS_CONNECTED | |
| 119 | SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */ |
| 120 | (SCTP_PCB_COPY_FLAGS & inp->sctp_flags)); |
| 121 | n_inp->sctp_socket = so; |
| 122 | n_inp->sctp_features = inp->sctp_features; |
| 123 | n_inp->sctp_mobility_features = inp->sctp_mobility_features; |
| 124 | n_inp->sctp_frag_point = inp->sctp_frag_point; |
| 125 | n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off; |
| 126 | n_inp->ecn_supported = inp->ecn_supported; |
| 127 | n_inp->prsctp_supported = inp->prsctp_supported; |
| 128 | n_inp->auth_supported = inp->auth_supported; |
| 129 | n_inp->asconf_supported = inp->asconf_supported; |
| 130 | n_inp->reconfig_supported = inp->reconfig_supported; |
| 131 | n_inp->nrsack_supported = inp->nrsack_supported; |
| 132 | n_inp->pktdrop_supported = inp->pktdrop_supported; |
| 133 | n_inp->partial_delivery_point = inp->partial_delivery_point; |
| 134 | n_inp->sctp_context = inp->sctp_context; |
| 135 | n_inp->max_cwnd = inp->max_cwnd; |
| 136 | n_inp->local_strreset_support = inp->local_strreset_support; |
| 137 | /* copy in the authentication parameters from the original endpoint */ |
| 138 | if (n_inp->sctp_ep.local_hmacs) |
| 139 | sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs); |
| 140 | n_inp->sctp_ep.local_hmacs = |
| 141 | sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); |
| 142 | if (n_inp->sctp_ep.local_auth_chunks) |
| 143 | sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks); |
| 144 | n_inp->sctp_ep.local_auth_chunks = |
| 145 | sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); |
| 146 | (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, |
| 147 | &n_inp->sctp_ep.shared_keys); |
| 148 | #if defined(__Userspace__) |
| 149 | n_inp->ulp_info = inp->ulp_info; |
| 150 | n_inp->recv_callback = inp->recv_callback; |
| 151 | n_inp->send_callback = inp->send_callback; |
| 152 | n_inp->send_sb_threshold = inp->send_sb_threshold; |
| 153 | #endif |
| 154 | /* |
| 155 | * Now we must move it from one hash table to another and get the |
| 156 | * stcb in the right place. |
| 157 | */ |
| 158 | sctp_move_pcb_and_assoc(inp, n_inp, stcb); |
| 159 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 160 | SCTP_TCB_UNLOCK(stcb); |
| 161 |
| 162 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 163 | sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT); |
| 164 | #else |
| 165 | sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK); |
| 166 | #endif |
| 167 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 168 |
| 169 | return (0); |
| 170 | } |
| 171 |
| 172 | #if defined(HAVE_SCTP_PEELOFF_SOCKOPT) |
| 173 | struct socket * |
| 174 | sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error) |
| 175 | { |
| 176 | struct socket *newso; |
| 177 | struct sctp_inpcb *inp, *n_inp; |
| 178 | struct sctp_tcb *stcb; |
| 179 |
| 180 | SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n"); |
| 181 | inp = (struct sctp_inpcb *)head->so_pcb; |
| 182 | if (inp == NULL) { |
| 183 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT); |
| 184 | *error = EFAULT; |
| 185 | return (NULL); |
| 186 | } |
| 187 | stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); |
| 188 | if (stcb == NULL) { |
| 189 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN); |
| 190 | *error = ENOTCONN; |
| 191 | return (NULL); |
| 192 | } |
| 193 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 194 | SCTP_TCB_UNLOCK(stcb); |
| 195 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 196 | CURVNET_SET(head->so_vnet); |
| 197 | #endif |
| 198 | newso = sonewconn(head, SS_ISCONNECTED |
| 199 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 200 | , NULL |
| 201 | #endif |
| 202 | ); |
| 203 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 204 | CURVNET_RESTORE(); |
| 205 | #endif |
| 206 | if (newso == NULL) { |
| 207 | SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n"); |
| 208 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM); |
| 209 | *error = ENOMEM; |
| 210 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 211 | return (NULL); |
| 212 |
| 213 | } |
| 214 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 215 | else { |
| 216 | SCTP_SOCKET_LOCK(newso, 1); |
| 217 | } |
| 218 | #endif |
| 219 | SCTP_TCB_LOCK(stcb); |
| 220 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 221 | n_inp = (struct sctp_inpcb *)newso->so_pcb; |
| 222 | SOCK_LOCK(head); |
| 223 | n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | |
| 224 | SCTP_PCB_FLAGS_CONNECTED | |
| 225 | SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */ |
| 226 | (SCTP_PCB_COPY_FLAGS & inp->sctp_flags)); |
| 227 | n_inp->sctp_features = inp->sctp_features; |
| 228 | n_inp->sctp_frag_point = inp->sctp_frag_point; |
| 229 | n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off; |
| 230 | n_inp->ecn_supported = inp->ecn_supported; |
| 231 | n_inp->prsctp_supported = inp->prsctp_supported; |
| 232 | n_inp->auth_supported = inp->auth_supported; |
| 233 | n_inp->asconf_supported = inp->asconf_supported; |
| 234 | n_inp->reconfig_supported = inp->reconfig_supported; |
| 235 | n_inp->nrsack_supported = inp->nrsack_supported; |
| 236 | n_inp->pktdrop_supported = inp->pktdrop_supported; |
| 237 | n_inp->partial_delivery_point = inp->partial_delivery_point; |
| 238 | n_inp->sctp_context = inp->sctp_context; |
| 239 | n_inp->max_cwnd = inp->max_cwnd; |
| 240 | n_inp->local_strreset_support = inp->local_strreset_support; |
| 241 | n_inp->inp_starting_point_for_iterator = NULL; |
| 242 | #if defined(__Userspace__) |
| 243 | n_inp->ulp_info = inp->ulp_info; |
| 244 | n_inp->recv_callback = inp->recv_callback; |
| 245 | n_inp->send_callback = inp->send_callback; |
| 246 | n_inp->send_sb_threshold = inp->send_sb_threshold; |
| 247 | #endif |
| 248 |
| 249 | /* copy in the authentication parameters from the original endpoint */ |
| 250 | if (n_inp->sctp_ep.local_hmacs) |
| 251 | sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs); |
| 252 | n_inp->sctp_ep.local_hmacs = |
| 253 | sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); |
| 254 | if (n_inp->sctp_ep.local_auth_chunks) |
| 255 | sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks); |
| 256 | n_inp->sctp_ep.local_auth_chunks = |
| 257 | sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); |
| 258 | (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, |
| 259 | &n_inp->sctp_ep.shared_keys); |
| 260 |
| 261 | n_inp->sctp_socket = newso; |
| 262 | if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { |
| 263 | sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE); |
| 264 | n_inp->sctp_ep.auto_close_time = 0; |
| 265 | sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL, |
| 266 | SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1); |
| 267 | } |
| 268 | /* Turn off any non-blocking semantic. */ |
| 269 | SOCK_LOCK(newso); |
| 270 | SCTP_CLEAR_SO_NBIO(newso); |
| 271 | newso->so_state |= SS_ISCONNECTED; |
| 272 | SOCK_UNLOCK(newso); |
| 273 | /* We remove it right away */ |
| 274 |
| 275 | #ifdef SCTP_LOCK_LOGGING |
| 276 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) { |
| 277 | sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK); |
| 278 | } |
| 279 | #endif |
| 280 | TAILQ_REMOVE(&head->so_comp, newso, so_list); |
| 281 | head->so_qlen--; |
| 282 | SOCK_UNLOCK(head); |
| 283 | /* |
| 284 | * Now we must move it from one hash table to another and get the |
| 285 | * stcb in the right place. |
| 286 | */ |
| 287 | sctp_move_pcb_and_assoc(inp, n_inp, stcb); |
| 288 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 289 | SCTP_TCB_UNLOCK(stcb); |
| 290 | /* |
| 291 | * And now the final hack. We move data in the pending side i.e. |
| 292 | * head to the new socket buffer. Let the GRUBBING begin :-0 |
| 293 | */ |
| 294 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 295 | sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT); |
| 296 | #else |
| 297 | sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK); |
| 298 | #endif |
| 299 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 300 | return (newso); |
| 301 | } |
| 302 | #endif |
| 303 |