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 #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 $");
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
sctp_can_peel_off(struct socket * head,sctp_assoc_t assoc_id)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
sctp_do_peeloff(struct socket * head,struct socket * so,sctp_assoc_t assoc_id)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 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;
154 #endif
155 /*
156 * Now we must move it from one hash table to another and get the
157 * stcb in the right place.
158 */
159 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
160 atomic_add_int(&stcb->asoc.refcnt, 1);
161 SCTP_TCB_UNLOCK(stcb);
162
163 #if defined(__FreeBSD__) && !defined(__Userspace__)
164 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
165 #else
166 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK);
167 #endif
168 atomic_subtract_int(&stcb->asoc.refcnt, 1);
169
170 return (0);
171 }
172
173 #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
174 struct socket *
sctp_get_peeloff(struct socket * head,sctp_assoc_t assoc_id,int * error)175 sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
176 {
177 struct socket *newso;
178 struct sctp_inpcb *inp, *n_inp;
179 struct sctp_tcb *stcb;
180
181 SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
182 inp = (struct sctp_inpcb *)head->so_pcb;
183 if (inp == NULL) {
184 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
185 *error = EFAULT;
186 return (NULL);
187 }
188 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
189 if (stcb == NULL) {
190 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
191 *error = ENOTCONN;
192 return (NULL);
193 }
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);
198 #endif
199 newso = sonewconn(head, SS_ISCONNECTED
200 #if defined(__APPLE__) && !defined(__Userspace__)
201 , NULL
202 #endif
203 );
204 #if defined(__FreeBSD__) && !defined(__Userspace__)
205 CURVNET_RESTORE();
206 #endif
207 if (newso == NULL) {
208 SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
209 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
210 *error = ENOMEM;
211 atomic_subtract_int(&stcb->asoc.refcnt, 1);
212 return (NULL);
213
214 }
215 #if defined(__APPLE__) && !defined(__Userspace__)
216 else {
217 SCTP_SOCKET_LOCK(newso, 1);
218 }
219 #endif
220 SCTP_TCB_LOCK(stcb);
221 atomic_subtract_int(&stcb->asoc.refcnt, 1);
222 n_inp = (struct sctp_inpcb *)newso->so_pcb;
223 SOCK_LOCK(head);
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;
248 #endif
249
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);
261
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);
268 }
269 /* Turn off any non-blocking semantic. */
270 SOCK_LOCK(newso);
271 SCTP_CLEAR_SO_NBIO(newso);
272 newso->so_state |= SS_ISCONNECTED;
273 SOCK_UNLOCK(newso);
274 /* We remove it right away */
275
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);
279 }
280 #endif
281 TAILQ_REMOVE(&head->so_comp, newso, so_list);
282 head->so_qlen--;
283 SOCK_UNLOCK(head);
284 /*
285 * Now we must move it from one hash table to another and get the
286 * stcb in the right place.
287 */
288 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
289 atomic_add_int(&stcb->asoc.refcnt, 1);
290 SCTP_TCB_UNLOCK(stcb);
291 /*
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
294 */
295 #if defined(__FreeBSD__) && !defined(__Userspace__)
296 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
297 #else
298 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK);
299 #endif
300 atomic_subtract_int(&stcb->asoc.refcnt, 1);
301 return (newso);
302 }
303 #endif
304