sctp_peeloff.c revision 163953
1/*-
2 * Copyright (c) 2001-2006, Cisco Systems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * a) Redistributions of source code must retain the above copyright notice,
8 *   this list of conditions and the following disclaimer.
9 *
10 * b) Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *   the documentation and/or other materials provided with the distribution.
13 *
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32/* $KAME: sctp_peeloff.c,v 1.13 2005/03/06 16:04:18 itojun Exp $	 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/netinet/sctp_peeloff.c 163953 2006-11-03 15:23:16Z rrs $");
36
37#include "opt_ipsec.h"
38#include "opt_inet6.h"
39#include "opt_inet.h"
40
41#include "opt_sctp.h"
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
47#include <sys/mbuf.h>
48#include <sys/domain.h>
49#include <sys/proc.h>
50#include <sys/protosw.h>
51#include <sys/socket.h>
52#include <sys/socketvar.h>
53#include <sys/sysctl.h>
54#include <sys/syslog.h>
55#include <net/if.h>
56#include <net/route.h>
57#include <netinet/in.h>
58#include <netinet/in_systm.h>
59#include <netinet/ip.h>
60#ifdef INET6
61#include <netinet/ip6.h>
62#endif
63#include <netinet/in_pcb.h>
64#include <netinet/in_var.h>
65#include <netinet/ip_var.h>
66#ifdef INET6
67#include <netinet6/ip6_var.h>
68#endif
69#include <netinet/ip_icmp.h>
70#include <netinet/icmp_var.h>
71#include <netinet/sctp_os.h>
72#include <netinet/sctp_pcb.h>
73#include <netinet/sctp.h>
74#include <netinet/sctp_uio.h>
75#include <netinet/sctp_var.h>
76#include <netinet/sctp_peeloff.h>
77#include <netinet/sctputil.h>
78#include <netinet/sctp_auth.h>
79
80#ifdef IPSEC
81#include <netinet6/ipsec.h>
82#include <netkey/key.h>
83#endif				/* IPSEC */
84
85
86#ifdef SCTP_DEBUG
87extern uint32_t sctp_debug_on;
88
89#endif				/* SCTP_DEBUG */
90
91
92int
93sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
94{
95	struct sctp_inpcb *inp;
96	struct sctp_tcb *stcb;
97
98	inp = (struct sctp_inpcb *)head->so_pcb;
99	if (inp == NULL) {
100		return (EFAULT);
101	}
102	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
103	if (stcb == NULL) {
104		return (ENOTCONN);
105	}
106	SCTP_TCB_UNLOCK(stcb);
107	/* We are clear to peel this one off */
108	return (0);
109}
110
111int
112sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
113{
114	struct sctp_inpcb *inp, *n_inp;
115	struct sctp_tcb *stcb;
116
117	inp = (struct sctp_inpcb *)head->so_pcb;
118	if (inp == NULL)
119		return (EFAULT);
120	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
121	if (stcb == NULL)
122		return (ENOTCONN);
123
124	n_inp = (struct sctp_inpcb *)so->so_pcb;
125	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
126	    SCTP_PCB_FLAGS_CONNECTED |
127	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
128	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
129	n_inp->sctp_socket = so;
130	n_inp->sctp_features = inp->sctp_features;
131	n_inp->sctp_frag_point = inp->sctp_frag_point;
132	n_inp->partial_delivery_point = inp->partial_delivery_point;
133	n_inp->sctp_context = inp->sctp_context;
134	n_inp->inp_starting_point_for_iterator = NULL;
135
136	/*
137	 * Now we must move it from one hash table to another and get the
138	 * stcb in the right place.
139	 */
140	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
141
142	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb);
143
144	SCTP_TCB_UNLOCK(stcb);
145	return (0);
146}
147
148struct socket *
149sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
150{
151	struct socket *newso;
152	struct sctp_inpcb *inp, *n_inp;
153	struct sctp_tcb *stcb;
154
155#ifdef SCTP_DEBUG
156	if (sctp_debug_on & SCTP_DEBUG_PEEL1) {
157		printf("SCTP peel-off called\n");
158	}
159#endif				/* SCTP_DEBUG */
160
161	inp = (struct sctp_inpcb *)head->so_pcb;
162	if (inp == NULL) {
163		*error = EFAULT;
164		return (NULL);
165	}
166	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
167	if (stcb == NULL) {
168		*error = ENOTCONN;
169		return (NULL);
170	}
171	newso = sonewconn(head, SS_ISCONNECTED
172	    );
173	if (newso == NULL) {
174#ifdef SCTP_DEBUG
175		if (sctp_debug_on & SCTP_DEBUG_PEEL1) {
176			printf("sctp_peeloff:sonewconn failed err\n");
177		}
178#endif				/* SCTP_DEBUG */
179		*error = ENOMEM;
180		SCTP_TCB_UNLOCK(stcb);
181		return (NULL);
182	}
183	n_inp = (struct sctp_inpcb *)newso->so_pcb;
184	SOCK_LOCK(head);
185	SCTP_INP_WLOCK(inp);
186	SCTP_INP_WLOCK(n_inp);
187	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
188	    SCTP_PCB_FLAGS_CONNECTED |
189	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
190	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
191	n_inp->sctp_features = inp->sctp_features;
192	n_inp->sctp_frag_point = inp->sctp_frag_point;
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	}
215	/* Turn off any non-blocking semantic. */
216	newso->so_state &= ~SS_NBIO;
217	newso->so_state |= SS_ISCONNECTED;
218	/* We remove it right away */
219#ifdef SCTP_LOCK_LOGGING
220	sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
221#endif
222	TAILQ_REMOVE(&head->so_comp, newso, so_list);
223	head->so_qlen--;
224	SOCK_UNLOCK(head);
225	/*
226	 * Now we must move it from one hash table to another and get the
227	 * stcb in the right place.
228	 */
229	SCTP_INP_WUNLOCK(n_inp);
230	SCTP_INP_WUNLOCK(inp);
231	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
232	/*
233	 * And now the final hack. We move data in the pending side i.e.
234	 * head to the new socket buffer. Let the GRUBBING begin :-0
235	 */
236	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb);
237
238	SCTP_TCB_UNLOCK(stcb);
239	return (newso);
240}
241