2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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>
50 sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
52 struct sctp_inpcb *inp;
53 struct sctp_tcb *stcb;
57 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF);
60 inp = (struct sctp_inpcb *)head->so_pcb;
62 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
65 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
66 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
67 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP);
70 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
72 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
75 state = SCTP_GET_STATE((&stcb->asoc));
76 if ((state == SCTP_STATE_EMPTY) ||
77 (state == SCTP_STATE_INUSE) ||
78 (state == SCTP_STATE_COOKIE_WAIT) ||
79 (state == SCTP_STATE_COOKIE_ECHOED)) {
80 SCTP_TCB_UNLOCK(stcb);
81 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
84 SCTP_TCB_UNLOCK(stcb);
85 /* We are clear to peel this one off */
90 sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
92 struct sctp_inpcb *inp, *n_inp;
93 struct sctp_tcb *stcb;
96 inp = (struct sctp_inpcb *)head->so_pcb;
98 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
101 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
103 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
106 state = SCTP_GET_STATE((&stcb->asoc));
107 if ((state == SCTP_STATE_EMPTY) ||
108 (state == SCTP_STATE_INUSE) ||
109 (state == SCTP_STATE_COOKIE_WAIT) ||
110 (state == SCTP_STATE_COOKIE_ECHOED)) {
111 SCTP_TCB_UNLOCK(stcb);
112 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
115 n_inp = (struct sctp_inpcb *)so->so_pcb;
116 n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
117 SCTP_PCB_FLAGS_CONNECTED |
118 SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */
119 (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
120 n_inp->sctp_socket = so;
121 n_inp->sctp_features = inp->sctp_features;
122 n_inp->sctp_mobility_features = inp->sctp_mobility_features;
123 n_inp->sctp_frag_point = inp->sctp_frag_point;
124 n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
125 n_inp->sctp_ecn_enable = inp->sctp_ecn_enable;
126 n_inp->partial_delivery_point = inp->partial_delivery_point;
127 n_inp->sctp_context = inp->sctp_context;
128 n_inp->local_strreset_support = inp->local_strreset_support;
129 n_inp->inp_starting_point_for_iterator = NULL;
130 /* copy in the authentication parameters from the original endpoint */
131 if (n_inp->sctp_ep.local_hmacs)
132 sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
133 n_inp->sctp_ep.local_hmacs =
134 sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
135 if (n_inp->sctp_ep.local_auth_chunks)
136 sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
137 n_inp->sctp_ep.local_auth_chunks =
138 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
139 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
140 &n_inp->sctp_ep.shared_keys);
142 * Now we must move it from one hash table to another and get the
143 * stcb in the right place.
145 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
146 atomic_add_int(&stcb->asoc.refcnt, 1);
147 SCTP_TCB_UNLOCK(stcb);
149 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
150 atomic_subtract_int(&stcb->asoc.refcnt, 1);
157 sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
159 struct socket *newso;
160 struct sctp_inpcb *inp, *n_inp;
161 struct sctp_tcb *stcb;
163 SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
164 inp = (struct sctp_inpcb *)head->so_pcb;
166 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
170 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
172 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
176 atomic_add_int(&stcb->asoc.refcnt, 1);
177 SCTP_TCB_UNLOCK(stcb);
178 CURVNET_SET(head->so_vnet);
179 newso = sonewconn(head, SS_ISCONNECTED
183 SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
184 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
186 atomic_subtract_int(&stcb->asoc.refcnt, 1);
191 atomic_subtract_int(&stcb->asoc.refcnt, 1);
192 n_inp = (struct sctp_inpcb *)newso->so_pcb;
194 n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
195 SCTP_PCB_FLAGS_CONNECTED |
196 SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */
197 (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
198 n_inp->sctp_features = inp->sctp_features;
199 n_inp->sctp_frag_point = inp->sctp_frag_point;
200 n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
201 n_inp->sctp_ecn_enable = inp->sctp_ecn_enable;
202 n_inp->partial_delivery_point = inp->partial_delivery_point;
203 n_inp->sctp_context = inp->sctp_context;
204 n_inp->local_strreset_support = inp->local_strreset_support;
205 n_inp->inp_starting_point_for_iterator = NULL;
207 /* copy in the authentication parameters from the original endpoint */
208 if (n_inp->sctp_ep.local_hmacs)
209 sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
210 n_inp->sctp_ep.local_hmacs =
211 sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
212 if (n_inp->sctp_ep.local_auth_chunks)
213 sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
214 n_inp->sctp_ep.local_auth_chunks =
215 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
216 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
217 &n_inp->sctp_ep.shared_keys);
219 n_inp->sctp_socket = newso;
220 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
221 sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
222 n_inp->sctp_ep.auto_close_time = 0;
223 sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
224 SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1);
226 /* Turn off any non-blocking semantic. */
227 SCTP_CLEAR_SO_NBIO(newso);
228 newso->so_state |= SS_ISCONNECTED;
229 /* We remove it right away */
231 #ifdef SCTP_LOCK_LOGGING
232 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
233 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
236 TAILQ_REMOVE(&head->so_comp, newso, so_list);
240 * Now we must move it from one hash table to another and get the
241 * stcb in the right place.
243 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
244 atomic_add_int(&stcb->asoc.refcnt, 1);
245 SCTP_TCB_UNLOCK(stcb);
247 * And now the final hack. We move data in the pending side i.e.
248 * head to the new socket buffer. Let the GRUBBING begin :-0
250 sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
251 atomic_subtract_int(&stcb->asoc.refcnt, 1);