sctp_peeloff.c revision 233660
1/*-
2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * a) Redistributions of source code must retain the above copyright notice,
10 *    this list of conditions and the following disclaimer.
11 *
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.
15 *
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.
19 *
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.
31 */
32
33
34/* $KAME: sctp_peeloff.c,v 1.13 2005/03/06 16:04:18 itojun Exp $	 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/netinet/sctp_peeloff.c 233660 2012-03-29 13:36:53Z rrs $");
38#include <netinet/sctp_os.h>
39#include <netinet/sctp_pcb.h>
40#include <netinet/sctputil.h>
41#include <netinet/sctp_var.h>
42#include <netinet/sctp_var.h>
43#include <netinet/sctp_sysctl.h>
44#include <netinet/sctp.h>
45#include <netinet/sctp_uio.h>
46#include <netinet/sctp_peeloff.h>
47#include <netinet/sctputil.h>
48#include <netinet/sctp_auth.h>
49
50
51int
52sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
53{
54	struct sctp_inpcb *inp;
55	struct sctp_tcb *stcb;
56	uint32_t state;
57
58	if (head == NULL) {
59		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF);
60		return (EBADF);
61	}
62	inp = (struct sctp_inpcb *)head->so_pcb;
63	if (inp == NULL) {
64		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
65		return (EFAULT);
66	}
67	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
68	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
69		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP);
70		return (EOPNOTSUPP);
71	}
72	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
73	if (stcb == NULL) {
74		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
75		return (ENOENT);
76	}
77	state = SCTP_GET_STATE((&stcb->asoc));
78	if ((state == SCTP_STATE_EMPTY) ||
79	    (state == SCTP_STATE_INUSE) ||
80	    (state == SCTP_STATE_COOKIE_WAIT) ||
81	    (state == SCTP_STATE_COOKIE_ECHOED)) {
82		SCTP_TCB_UNLOCK(stcb);
83		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
84		return (ENOTCONN);
85	}
86	SCTP_TCB_UNLOCK(stcb);
87	/* We are clear to peel this one off */
88	return (0);
89}
90
91int
92sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
93{
94	struct sctp_inpcb *inp, *n_inp;
95	struct sctp_tcb *stcb;
96	uint32_t state;
97
98	inp = (struct sctp_inpcb *)head->so_pcb;
99	if (inp == NULL) {
100		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
101		return (EFAULT);
102	}
103	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
104	if (stcb == NULL) {
105		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
106		return (ENOTCONN);
107	}
108	state = SCTP_GET_STATE((&stcb->asoc));
109	if ((state == SCTP_STATE_EMPTY) ||
110	    (state == SCTP_STATE_INUSE) ||
111	    (state == SCTP_STATE_COOKIE_WAIT) ||
112	    (state == SCTP_STATE_COOKIE_ECHOED)) {
113		SCTP_TCB_UNLOCK(stcb);
114		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
115		return (ENOTCONN);
116	}
117	n_inp = (struct sctp_inpcb *)so->so_pcb;
118	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
119	    SCTP_PCB_FLAGS_CONNECTED |
120	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
121	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
122	n_inp->sctp_socket = so;
123	n_inp->sctp_features = inp->sctp_features;
124	n_inp->sctp_mobility_features = inp->sctp_mobility_features;
125	n_inp->sctp_frag_point = inp->sctp_frag_point;
126	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
127	n_inp->sctp_ecn_enable = inp->sctp_ecn_enable;
128	n_inp->partial_delivery_point = inp->partial_delivery_point;
129	n_inp->sctp_context = inp->sctp_context;
130	n_inp->local_strreset_support = inp->local_strreset_support;
131	n_inp->inp_starting_point_for_iterator = NULL;
132	/* copy in the authentication parameters from the original endpoint */
133	if (n_inp->sctp_ep.local_hmacs)
134		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
135	n_inp->sctp_ep.local_hmacs =
136	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
137	if (n_inp->sctp_ep.local_auth_chunks)
138		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
139	n_inp->sctp_ep.local_auth_chunks =
140	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
141	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
142	    &n_inp->sctp_ep.shared_keys);
143	/*
144	 * Now we must move it from one hash table to another and get the
145	 * stcb in the right place.
146	 */
147	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
148	atomic_add_int(&stcb->asoc.refcnt, 1);
149	SCTP_TCB_UNLOCK(stcb);
150
151	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
152	atomic_subtract_int(&stcb->asoc.refcnt, 1);
153
154	return (0);
155}
156
157
158struct socket *
159sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
160{
161	struct socket *newso;
162	struct sctp_inpcb *inp, *n_inp;
163	struct sctp_tcb *stcb;
164
165	SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
166	inp = (struct sctp_inpcb *)head->so_pcb;
167	if (inp == NULL) {
168		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
169		*error = EFAULT;
170		return (NULL);
171	}
172	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
173	if (stcb == NULL) {
174		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
175		*error = ENOTCONN;
176		return (NULL);
177	}
178	atomic_add_int(&stcb->asoc.refcnt, 1);
179	SCTP_TCB_UNLOCK(stcb);
180	CURVNET_SET(head->so_vnet);
181	newso = sonewconn(head, SS_ISCONNECTED
182	    );
183	CURVNET_RESTORE();
184	if (newso == NULL) {
185		SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
186		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
187		*error = ENOMEM;
188		atomic_subtract_int(&stcb->asoc.refcnt, 1);
189		return (NULL);
190
191	}
192	SCTP_TCB_LOCK(stcb);
193	atomic_subtract_int(&stcb->asoc.refcnt, 1);
194	n_inp = (struct sctp_inpcb *)newso->so_pcb;
195	SOCK_LOCK(head);
196	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
197	    SCTP_PCB_FLAGS_CONNECTED |
198	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
199	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
200	n_inp->sctp_features = inp->sctp_features;
201	n_inp->sctp_frag_point = inp->sctp_frag_point;
202	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
203	n_inp->sctp_ecn_enable = inp->sctp_ecn_enable;
204	n_inp->partial_delivery_point = inp->partial_delivery_point;
205	n_inp->sctp_context = inp->sctp_context;
206	n_inp->local_strreset_support = inp->local_strreset_support;
207	n_inp->inp_starting_point_for_iterator = NULL;
208
209	/* copy in the authentication parameters from the original endpoint */
210	if (n_inp->sctp_ep.local_hmacs)
211		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
212	n_inp->sctp_ep.local_hmacs =
213	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
214	if (n_inp->sctp_ep.local_auth_chunks)
215		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
216	n_inp->sctp_ep.local_auth_chunks =
217	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
218	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
219	    &n_inp->sctp_ep.shared_keys);
220
221	n_inp->sctp_socket = newso;
222	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
223		sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
224		n_inp->sctp_ep.auto_close_time = 0;
225		sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
226		    SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1);
227	}
228	/* Turn off any non-blocking semantic. */
229	SCTP_CLEAR_SO_NBIO(newso);
230	newso->so_state |= SS_ISCONNECTED;
231	/* We remove it right away */
232
233#ifdef SCTP_LOCK_LOGGING
234	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
235		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
236	}
237#endif
238	TAILQ_REMOVE(&head->so_comp, newso, so_list);
239	head->so_qlen--;
240	SOCK_UNLOCK(head);
241	/*
242	 * Now we must move it from one hash table to another and get the
243	 * stcb in the right place.
244	 */
245	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
246	atomic_add_int(&stcb->asoc.refcnt, 1);
247	SCTP_TCB_UNLOCK(stcb);
248	/*
249	 * And now the final hack. We move data in the pending side i.e.
250	 * head to the new socket buffer. Let the GRUBBING begin :-0
251	 */
252	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
253	atomic_subtract_int(&stcb->asoc.refcnt, 1);
254	return (newso);
255}
256