sctp_peeloff.c revision 218235
1/*-
2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart, rrs@lakerest.net and
4 *                          Michael Tuexen, tuexen@fh-muenster.de
5 *                          All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * a) Redistributions of source code must retain the above copyright notice,
11 *   this list of conditions and the following disclaimer.
12 *
13 * b) Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in
15 *   the documentation and/or other materials provided with the distribution.
16 *
17 * c) Neither the name of Cisco Systems, Inc. nor the names of its
18 *    contributors may be used to endorse or promote products derived
19 *    from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35/* $KAME: sctp_peeloff.c,v 1.13 2005/03/06 16:04:18 itojun Exp $	 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/netinet/sctp_peeloff.c 218235 2011-02-03 19:59:00Z tuexen $");
39#include <netinet/sctp_os.h>
40#include <netinet/sctp_pcb.h>
41#include <netinet/sctputil.h>
42#include <netinet/sctp_var.h>
43#include <netinet/sctp_var.h>
44#include <netinet/sctp_sysctl.h>
45#include <netinet/sctp.h>
46#include <netinet/sctp_uio.h>
47#include <netinet/sctp_peeloff.h>
48#include <netinet/sctputil.h>
49#include <netinet/sctp_auth.h>
50
51
52int
53sctp_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	inp = (struct sctp_inpcb *)head->so_pcb;
60	if (inp == NULL) {
61		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
62		return (EFAULT);
63	}
64	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
65	if (stcb == NULL) {
66		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
67		return (ENOENT);
68	}
69	state = SCTP_GET_STATE((&stcb->asoc));
70	if ((state == SCTP_STATE_EMPTY) ||
71	    (state == SCTP_STATE_INUSE) ||
72	    (state == SCTP_STATE_COOKIE_WAIT) ||
73	    (state == SCTP_STATE_COOKIE_ECHOED)) {
74		SCTP_TCB_UNLOCK(stcb);
75		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
76		return (ENOTCONN);
77	}
78	SCTP_TCB_UNLOCK(stcb);
79	/* We are clear to peel this one off */
80	return (0);
81}
82
83int
84sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
85{
86	struct sctp_inpcb *inp, *n_inp;
87	struct sctp_tcb *stcb;
88	uint32_t state;
89
90	inp = (struct sctp_inpcb *)head->so_pcb;
91	if (inp == NULL) {
92		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
93		return (EFAULT);
94	}
95	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
96	if (stcb == NULL) {
97		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
98		return (ENOTCONN);
99	}
100	state = SCTP_GET_STATE((&stcb->asoc));
101	if ((state == SCTP_STATE_EMPTY) ||
102	    (state == SCTP_STATE_INUSE) ||
103	    (state == SCTP_STATE_COOKIE_WAIT) ||
104	    (state == SCTP_STATE_COOKIE_ECHOED)) {
105		SCTP_TCB_UNLOCK(stcb);
106		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
107		return (ENOTCONN);
108	}
109	n_inp = (struct sctp_inpcb *)so->so_pcb;
110	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
111	    SCTP_PCB_FLAGS_CONNECTED |
112	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
113	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
114	n_inp->sctp_socket = so;
115	n_inp->sctp_features = inp->sctp_features;
116	n_inp->sctp_mobility_features = inp->sctp_mobility_features;
117	n_inp->sctp_frag_point = inp->sctp_frag_point;
118	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
119	n_inp->sctp_ecn_enable = inp->sctp_ecn_enable;
120	n_inp->partial_delivery_point = inp->partial_delivery_point;
121	n_inp->sctp_context = inp->sctp_context;
122	n_inp->inp_starting_point_for_iterator = NULL;
123	/* copy in the authentication parameters from the original endpoint */
124	if (n_inp->sctp_ep.local_hmacs)
125		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
126	n_inp->sctp_ep.local_hmacs =
127	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
128	if (n_inp->sctp_ep.local_auth_chunks)
129		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
130	n_inp->sctp_ep.local_auth_chunks =
131	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
132	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
133	    &n_inp->sctp_ep.shared_keys);
134	/*
135	 * Now we must move it from one hash table to another and get the
136	 * stcb in the right place.
137	 */
138	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
139	atomic_add_int(&stcb->asoc.refcnt, 1);
140	SCTP_TCB_UNLOCK(stcb);
141
142	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
143	atomic_subtract_int(&stcb->asoc.refcnt, 1);
144
145	return (0);
146}
147
148
149struct socket *
150sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
151{
152	struct socket *newso;
153	struct sctp_inpcb *inp, *n_inp;
154	struct sctp_tcb *stcb;
155
156	SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
157	inp = (struct sctp_inpcb *)head->so_pcb;
158	if (inp == NULL) {
159		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
160		*error = EFAULT;
161		return (NULL);
162	}
163	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
164	if (stcb == NULL) {
165		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
166		*error = ENOTCONN;
167		return (NULL);
168	}
169	atomic_add_int(&stcb->asoc.refcnt, 1);
170	SCTP_TCB_UNLOCK(stcb);
171	newso = sonewconn(head, SS_ISCONNECTED
172	    );
173	if (newso == NULL) {
174		SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
175		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
176		*error = ENOMEM;
177		atomic_subtract_int(&stcb->asoc.refcnt, 1);
178		return (NULL);
179
180	}
181	SCTP_TCB_LOCK(stcb);
182	atomic_subtract_int(&stcb->asoc.refcnt, 1);
183	n_inp = (struct sctp_inpcb *)newso->so_pcb;
184	SOCK_LOCK(head);
185	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
186	    SCTP_PCB_FLAGS_CONNECTED |
187	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
188	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
189	n_inp->sctp_features = inp->sctp_features;
190	n_inp->sctp_frag_point = inp->sctp_frag_point;
191	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
192	n_inp->sctp_ecn_enable = inp->sctp_ecn_enable;
193	n_inp->partial_delivery_point = inp->partial_delivery_point;
194	n_inp->sctp_context = inp->sctp_context;
195	n_inp->inp_starting_point_for_iterator = NULL;
196
197	/* copy in the authentication parameters from the original endpoint */
198	if (n_inp->sctp_ep.local_hmacs)
199		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
200	n_inp->sctp_ep.local_hmacs =
201	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
202	if (n_inp->sctp_ep.local_auth_chunks)
203		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
204	n_inp->sctp_ep.local_auth_chunks =
205	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
206	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
207	    &n_inp->sctp_ep.shared_keys);
208
209	n_inp->sctp_socket = newso;
210	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
211		sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
212		n_inp->sctp_ep.auto_close_time = 0;
213		sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
214		    SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1);
215	}
216	/* Turn off any non-blocking semantic. */
217	SCTP_CLEAR_SO_NBIO(newso);
218	newso->so_state |= SS_ISCONNECTED;
219	/* We remove it right away */
220
221#ifdef SCTP_LOCK_LOGGING
222	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
223		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
224	}
225#endif
226	TAILQ_REMOVE(&head->so_comp, newso, so_list);
227	head->so_qlen--;
228	SOCK_UNLOCK(head);
229	/*
230	 * Now we must move it from one hash table to another and get the
231	 * stcb in the right place.
232	 */
233	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
234	atomic_add_int(&stcb->asoc.refcnt, 1);
235	SCTP_TCB_UNLOCK(stcb);
236	/*
237	 * And now the final hack. We move data in the pending side i.e.
238	 * head to the new socket buffer. Let the GRUBBING begin :-0
239	 */
240	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
241	atomic_subtract_int(&stcb->asoc.refcnt, 1);
242	return (newso);
243}
244