2 * SPDX-License-Identifier: BSD-3-Clause
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
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.
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.
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.
35 #if defined(__FreeBSD__) && !defined(__Userspace__)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_peeloff.c 362054 2020-06-11 13:34:09Z tuexen $");
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>
53 sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
55 struct sctp_inpcb *inp;
56 struct sctp_tcb *stcb;
60 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF);
63 inp = (struct sctp_inpcb *)head->so_pcb;
65 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
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);
73 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
75 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
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);
85 SCTP_TCB_UNLOCK(stcb);
86 /* We are clear to peel this one off */
91 sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
93 struct sctp_inpcb *inp, *n_inp;
94 struct sctp_tcb *stcb;
97 inp = (struct sctp_inpcb *)head->so_pcb;
99 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
102 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
104 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
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);
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 n_inp->inp_starting_point_for_iterator = NULL;
138 /* copy in the authentication parameters from the original endpoint */
139 if (n_inp->sctp_ep.local_hmacs)
140 sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
141 n_inp->sctp_ep.local_hmacs =
142 sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
143 if (n_inp->sctp_ep.local_auth_chunks)
144 sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
145 n_inp->sctp_ep.local_auth_chunks =
146 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
147 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
148 &n_inp->sctp_ep.shared_keys);
149 #if defined(__Userspace__)
150 n_inp->ulp_info = inp->ulp_info;
151 n_inp->recv_callback = inp->recv_callback;
152 n_inp->send_callback = inp->send_callback;
153 n_inp->send_sb_threshold = inp->send_sb_threshold;
156 * Now we must move it from one hash table to another and get the
157 * stcb in the right place.
159 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
160 atomic_add_int(&stcb->asoc.refcnt, 1);
161 SCTP_TCB_UNLOCK(stcb);
163 #if defined(__FreeBSD__) && !defined(__Userspace__)
164 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
166 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK);
168 atomic_subtract_int(&stcb->asoc.refcnt, 1);
173 #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
175 sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
177 struct socket *newso;
178 struct sctp_inpcb *inp, *n_inp;
179 struct sctp_tcb *stcb;
181 SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
182 inp = (struct sctp_inpcb *)head->so_pcb;
184 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
188 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
190 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
194 atomic_add_int(&stcb->asoc.refcnt, 1);
195 SCTP_TCB_UNLOCK(stcb);
196 #if defined(__FreeBSD__) && !defined(__Userspace__)
197 CURVNET_SET(head->so_vnet);
199 newso = sonewconn(head, SS_ISCONNECTED
200 #if defined(__APPLE__) && !defined(__Userspace__)
204 #if defined(__FreeBSD__) && !defined(__Userspace__)
208 SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
209 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
211 atomic_subtract_int(&stcb->asoc.refcnt, 1);
215 #if defined(__APPLE__) && !defined(__Userspace__)
217 SCTP_SOCKET_LOCK(newso, 1);
221 atomic_subtract_int(&stcb->asoc.refcnt, 1);
222 n_inp = (struct sctp_inpcb *)newso->so_pcb;
224 n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
225 SCTP_PCB_FLAGS_CONNECTED |
226 SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */
227 (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
228 n_inp->sctp_features = inp->sctp_features;
229 n_inp->sctp_frag_point = inp->sctp_frag_point;
230 n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
231 n_inp->ecn_supported = inp->ecn_supported;
232 n_inp->prsctp_supported = inp->prsctp_supported;
233 n_inp->auth_supported = inp->auth_supported;
234 n_inp->asconf_supported = inp->asconf_supported;
235 n_inp->reconfig_supported = inp->reconfig_supported;
236 n_inp->nrsack_supported = inp->nrsack_supported;
237 n_inp->pktdrop_supported = inp->pktdrop_supported;
238 n_inp->partial_delivery_point = inp->partial_delivery_point;
239 n_inp->sctp_context = inp->sctp_context;
240 n_inp->max_cwnd = inp->max_cwnd;
241 n_inp->local_strreset_support = inp->local_strreset_support;
242 n_inp->inp_starting_point_for_iterator = NULL;
243 #if defined(__Userspace__)
244 n_inp->ulp_info = inp->ulp_info;
245 n_inp->recv_callback = inp->recv_callback;
246 n_inp->send_callback = inp->send_callback;
247 n_inp->send_sb_threshold = inp->send_sb_threshold;
250 /* copy in the authentication parameters from the original endpoint */
251 if (n_inp->sctp_ep.local_hmacs)
252 sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
253 n_inp->sctp_ep.local_hmacs =
254 sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
255 if (n_inp->sctp_ep.local_auth_chunks)
256 sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
257 n_inp->sctp_ep.local_auth_chunks =
258 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
259 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
260 &n_inp->sctp_ep.shared_keys);
262 n_inp->sctp_socket = newso;
263 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
264 sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
265 n_inp->sctp_ep.auto_close_time = 0;
266 sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
267 SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1);
269 /* Turn off any non-blocking semantic. */
271 SCTP_CLEAR_SO_NBIO(newso);
272 newso->so_state |= SS_ISCONNECTED;
274 /* We remove it right away */
276 #ifdef SCTP_LOCK_LOGGING
277 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
278 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
281 TAILQ_REMOVE(&head->so_comp, newso, so_list);
285 * Now we must move it from one hash table to another and get the
286 * stcb in the right place.
288 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
289 atomic_add_int(&stcb->asoc.refcnt, 1);
290 SCTP_TCB_UNLOCK(stcb);
292 * And now the final hack. We move data in the pending side i.e.
293 * head to the new socket buffer. Let the GRUBBING begin :-0
295 #if defined(__FreeBSD__) && !defined(__Userspace__)
296 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
298 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK);
300 atomic_subtract_int(&stcb->asoc.refcnt, 1);