sctp_peeloff.c revision 175845
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs
31163953Srrs
32163953Srrs/* $KAME: sctp_peeloff.c,v 1.13 2005/03/06 16:04:18 itojun Exp $	 */
33163953Srrs
34163953Srrs#include <sys/cdefs.h>
35163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_peeloff.c 175845 2008-01-31 08:22:24Z rwatson $");
36163953Srrs#include <netinet/sctp_os.h>
37163953Srrs#include <netinet/sctp_pcb.h>
38170744Srrs#include <netinet/sctputil.h>
39170744Srrs#include <netinet/sctp_var.h>
40170744Srrs#include <netinet/sctp_var.h>
41170744Srrs#include <netinet/sctp_sysctl.h>
42163953Srrs#include <netinet/sctp.h>
43163953Srrs#include <netinet/sctp_uio.h>
44163953Srrs#include <netinet/sctp_peeloff.h>
45163953Srrs#include <netinet/sctputil.h>
46163953Srrs#include <netinet/sctp_auth.h>
47163953Srrs
48163953Srrs
49163953Srrsint
50163953Srrssctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
51163953Srrs{
52163953Srrs	struct sctp_inpcb *inp;
53163953Srrs	struct sctp_tcb *stcb;
54171477Srrs	uint32_t state;
55163953Srrs
56163953Srrs	inp = (struct sctp_inpcb *)head->so_pcb;
57163953Srrs	if (inp == NULL) {
58171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
59163953Srrs		return (EFAULT);
60163953Srrs	}
61163953Srrs	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
62163953Srrs	if (stcb == NULL) {
63172090Srrs		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
64172090Srrs		return (ENOENT);
65163953Srrs	}
66171477Srrs	state = SCTP_GET_STATE((&stcb->asoc));
67171477Srrs	if ((state == SCTP_STATE_EMPTY) ||
68171477Srrs	    (state == SCTP_STATE_INUSE) ||
69171477Srrs	    (state == SCTP_STATE_COOKIE_WAIT) ||
70171477Srrs	    (state == SCTP_STATE_COOKIE_ECHOED)) {
71171477Srrs		SCTP_TCB_UNLOCK(stcb);
72171943Srrs		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
73171477Srrs		return (ENOTCONN);
74171477Srrs	}
75163953Srrs	SCTP_TCB_UNLOCK(stcb);
76163953Srrs	/* We are clear to peel this one off */
77163953Srrs	return (0);
78163953Srrs}
79163953Srrs
80163953Srrsint
81163953Srrssctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
82163953Srrs{
83163953Srrs	struct sctp_inpcb *inp, *n_inp;
84163953Srrs	struct sctp_tcb *stcb;
85171477Srrs	uint32_t state;
86163953Srrs
87163953Srrs	inp = (struct sctp_inpcb *)head->so_pcb;
88171943Srrs	if (inp == NULL) {
89171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
90163953Srrs		return (EFAULT);
91171943Srrs	}
92163953Srrs	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
93171943Srrs	if (stcb == NULL) {
94171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
95163953Srrs		return (ENOTCONN);
96171943Srrs	}
97171477Srrs	state = SCTP_GET_STATE((&stcb->asoc));
98171477Srrs	if ((state == SCTP_STATE_EMPTY) ||
99171477Srrs	    (state == SCTP_STATE_INUSE) ||
100171477Srrs	    (state == SCTP_STATE_COOKIE_WAIT) ||
101171477Srrs	    (state == SCTP_STATE_COOKIE_ECHOED)) {
102171477Srrs		SCTP_TCB_UNLOCK(stcb);
103171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
104171477Srrs		return (ENOTCONN);
105171477Srrs	}
106163953Srrs	n_inp = (struct sctp_inpcb *)so->so_pcb;
107163953Srrs	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
108163953Srrs	    SCTP_PCB_FLAGS_CONNECTED |
109163953Srrs	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
110163953Srrs	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
111163953Srrs	n_inp->sctp_socket = so;
112163953Srrs	n_inp->sctp_features = inp->sctp_features;
113171990Srrs	n_inp->sctp_mobility_features = inp->sctp_mobility_features;
114163953Srrs	n_inp->sctp_frag_point = inp->sctp_frag_point;
115163953Srrs	n_inp->partial_delivery_point = inp->partial_delivery_point;
116163953Srrs	n_inp->sctp_context = inp->sctp_context;
117163953Srrs	n_inp->inp_starting_point_for_iterator = NULL;
118171990Srrs	/* copy in the authentication parameters from the original endpoint */
119171990Srrs	if (n_inp->sctp_ep.local_hmacs)
120171990Srrs		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
121171990Srrs	n_inp->sctp_ep.local_hmacs =
122171990Srrs	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
123171990Srrs	if (n_inp->sctp_ep.local_auth_chunks)
124171990Srrs		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
125171990Srrs	n_inp->sctp_ep.local_auth_chunks =
126171990Srrs	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
127171990Srrs	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
128171990Srrs	    &n_inp->sctp_ep.shared_keys);
129163953Srrs	/*
130163953Srrs	 * Now we must move it from one hash table to another and get the
131163953Srrs	 * stcb in the right place.
132163953Srrs	 */
133163953Srrs	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
134170056Srrs	atomic_add_int(&stcb->asoc.refcnt, 1);
135170056Srrs	SCTP_TCB_UNLOCK(stcb);
136163953Srrs
137175845Srwatson	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
138170056Srrs	atomic_subtract_int(&stcb->asoc.refcnt, 1);
139163953Srrs
140163953Srrs	return (0);
141163953Srrs}
142163953Srrs
143168709Srrs
144163953Srrsstruct socket *
145163953Srrssctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
146163953Srrs{
147163953Srrs	struct socket *newso;
148163953Srrs	struct sctp_inpcb *inp, *n_inp;
149163953Srrs	struct sctp_tcb *stcb;
150163953Srrs
151169420Srrs	SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
152163953Srrs	inp = (struct sctp_inpcb *)head->so_pcb;
153163953Srrs	if (inp == NULL) {
154171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
155163953Srrs		*error = EFAULT;
156163953Srrs		return (NULL);
157163953Srrs	}
158163953Srrs	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
159163953Srrs	if (stcb == NULL) {
160171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
161163953Srrs		*error = ENOTCONN;
162163953Srrs		return (NULL);
163163953Srrs	}
164172090Srrs	atomic_add_int(&stcb->asoc.refcnt, 1);
165172090Srrs	SCTP_TCB_UNLOCK(stcb);
166163953Srrs	newso = sonewconn(head, SS_ISCONNECTED
167163953Srrs	    );
168163953Srrs	if (newso == NULL) {
169169420Srrs		SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
170171943Srrs		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
171163953Srrs		*error = ENOMEM;
172172090Srrs		atomic_subtract_int(&stcb->asoc.refcnt, 1);
173163953Srrs		return (NULL);
174170056Srrs
175163953Srrs	}
176172090Srrs	SCTP_TCB_LOCK(stcb);
177172090Srrs	atomic_subtract_int(&stcb->asoc.refcnt, 1);
178163953Srrs	n_inp = (struct sctp_inpcb *)newso->so_pcb;
179163953Srrs	SOCK_LOCK(head);
180163953Srrs	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
181163953Srrs	    SCTP_PCB_FLAGS_CONNECTED |
182163953Srrs	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
183163953Srrs	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
184163953Srrs	n_inp->sctp_features = inp->sctp_features;
185163953Srrs	n_inp->sctp_frag_point = inp->sctp_frag_point;
186163953Srrs	n_inp->partial_delivery_point = inp->partial_delivery_point;
187163953Srrs	n_inp->sctp_context = inp->sctp_context;
188163953Srrs	n_inp->inp_starting_point_for_iterator = NULL;
189163953Srrs
190163953Srrs	/* copy in the authentication parameters from the original endpoint */
191163953Srrs	if (n_inp->sctp_ep.local_hmacs)
192163953Srrs		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
193163953Srrs	n_inp->sctp_ep.local_hmacs =
194163953Srrs	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
195163953Srrs	if (n_inp->sctp_ep.local_auth_chunks)
196163953Srrs		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
197163953Srrs	n_inp->sctp_ep.local_auth_chunks =
198163953Srrs	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
199163953Srrs	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
200163953Srrs	    &n_inp->sctp_ep.shared_keys);
201163953Srrs
202163953Srrs	n_inp->sctp_socket = newso;
203163953Srrs	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
204163953Srrs		sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
205163953Srrs		n_inp->sctp_ep.auto_close_time = 0;
206165220Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
207165220Srrs		    SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1);
208163953Srrs	}
209163953Srrs	/* Turn off any non-blocking semantic. */
210167598Srrs	SCTP_CLEAR_SO_NBIO(newso);
211163953Srrs	newso->so_state |= SS_ISCONNECTED;
212163953Srrs	/* We remove it right away */
213168709Srrs
214163953Srrs#ifdef SCTP_LOCK_LOGGING
215170744Srrs	if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
216170744Srrs		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
217170744Srrs	}
218163953Srrs#endif
219163953Srrs	TAILQ_REMOVE(&head->so_comp, newso, so_list);
220163953Srrs	head->so_qlen--;
221163953Srrs	SOCK_UNLOCK(head);
222163953Srrs	/*
223163953Srrs	 * Now we must move it from one hash table to another and get the
224163953Srrs	 * stcb in the right place.
225163953Srrs	 */
226163953Srrs	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
227170056Srrs	atomic_add_int(&stcb->asoc.refcnt, 1);
228170056Srrs	SCTP_TCB_UNLOCK(stcb);
229163953Srrs	/*
230163953Srrs	 * And now the final hack. We move data in the pending side i.e.
231163953Srrs	 * head to the new socket buffer. Let the GRUBBING begin :-0
232163953Srrs	 */
233175845Srwatson	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
234170056Srrs	atomic_subtract_int(&stcb->asoc.refcnt, 1);
235163953Srrs	return (newso);
236163953Srrs}
237