xref: /freebsd/sys/netinet/sctp_peeloff.c (revision 4c8945a06b01a5c8122cdeb402af36bb46a06acc)
1 /*-
2  * Copyright (c) 2001-2007, by 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$");
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>
47 
48 
49 int
50 sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
51 {
52 	struct sctp_inpcb *inp;
53 	struct sctp_tcb *stcb;
54 	uint32_t state;
55 
56 	inp = (struct sctp_inpcb *)head->so_pcb;
57 	if (inp == NULL) {
58 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
59 		return (EFAULT);
60 	}
61 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
62 	if (stcb == NULL) {
63 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
64 		return (ENOENT);
65 	}
66 	state = SCTP_GET_STATE((&stcb->asoc));
67 	if ((state == SCTP_STATE_EMPTY) ||
68 	    (state == SCTP_STATE_INUSE) ||
69 	    (state == SCTP_STATE_COOKIE_WAIT) ||
70 	    (state == SCTP_STATE_COOKIE_ECHOED)) {
71 		SCTP_TCB_UNLOCK(stcb);
72 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
73 		return (ENOTCONN);
74 	}
75 	SCTP_TCB_UNLOCK(stcb);
76 	/* We are clear to peel this one off */
77 	return (0);
78 }
79 
80 int
81 sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
82 {
83 	struct sctp_inpcb *inp, *n_inp;
84 	struct sctp_tcb *stcb;
85 	uint32_t state;
86 
87 	inp = (struct sctp_inpcb *)head->so_pcb;
88 	if (inp == NULL) {
89 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
90 		return (EFAULT);
91 	}
92 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
93 	if (stcb == NULL) {
94 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
95 		return (ENOTCONN);
96 	}
97 	state = SCTP_GET_STATE((&stcb->asoc));
98 	if ((state == SCTP_STATE_EMPTY) ||
99 	    (state == SCTP_STATE_INUSE) ||
100 	    (state == SCTP_STATE_COOKIE_WAIT) ||
101 	    (state == SCTP_STATE_COOKIE_ECHOED)) {
102 		SCTP_TCB_UNLOCK(stcb);
103 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
104 		return (ENOTCONN);
105 	}
106 	n_inp = (struct sctp_inpcb *)so->so_pcb;
107 	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
108 	    SCTP_PCB_FLAGS_CONNECTED |
109 	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
110 	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
111 	n_inp->sctp_socket = so;
112 	n_inp->sctp_features = inp->sctp_features;
113 	n_inp->sctp_mobility_features = inp->sctp_mobility_features;
114 	n_inp->sctp_frag_point = inp->sctp_frag_point;
115 	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
116 	n_inp->partial_delivery_point = inp->partial_delivery_point;
117 	n_inp->sctp_context = inp->sctp_context;
118 	n_inp->inp_starting_point_for_iterator = NULL;
119 	/* copy in the authentication parameters from the original endpoint */
120 	if (n_inp->sctp_ep.local_hmacs)
121 		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
122 	n_inp->sctp_ep.local_hmacs =
123 	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
124 	if (n_inp->sctp_ep.local_auth_chunks)
125 		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
126 	n_inp->sctp_ep.local_auth_chunks =
127 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
128 	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
129 	    &n_inp->sctp_ep.shared_keys);
130 	/*
131 	 * Now we must move it from one hash table to another and get the
132 	 * stcb in the right place.
133 	 */
134 	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
135 	atomic_add_int(&stcb->asoc.refcnt, 1);
136 	SCTP_TCB_UNLOCK(stcb);
137 
138 	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
139 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
140 
141 	return (0);
142 }
143 
144 
145 struct socket *
146 sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
147 {
148 	struct socket *newso;
149 	struct sctp_inpcb *inp, *n_inp;
150 	struct sctp_tcb *stcb;
151 
152 	SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
153 	inp = (struct sctp_inpcb *)head->so_pcb;
154 	if (inp == NULL) {
155 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
156 		*error = EFAULT;
157 		return (NULL);
158 	}
159 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
160 	if (stcb == NULL) {
161 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
162 		*error = ENOTCONN;
163 		return (NULL);
164 	}
165 	atomic_add_int(&stcb->asoc.refcnt, 1);
166 	SCTP_TCB_UNLOCK(stcb);
167 	newso = sonewconn(head, SS_ISCONNECTED
168 	    );
169 	if (newso == NULL) {
170 		SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
171 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
172 		*error = ENOMEM;
173 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
174 		return (NULL);
175 
176 	}
177 	SCTP_TCB_LOCK(stcb);
178 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
179 	n_inp = (struct sctp_inpcb *)newso->so_pcb;
180 	SOCK_LOCK(head);
181 	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
182 	    SCTP_PCB_FLAGS_CONNECTED |
183 	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
184 	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
185 	n_inp->sctp_features = inp->sctp_features;
186 	n_inp->sctp_frag_point = inp->sctp_frag_point;
187 	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
188 	n_inp->partial_delivery_point = inp->partial_delivery_point;
189 	n_inp->sctp_context = inp->sctp_context;
190 	n_inp->inp_starting_point_for_iterator = NULL;
191 
192 	/* copy in the authentication parameters from the original endpoint */
193 	if (n_inp->sctp_ep.local_hmacs)
194 		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
195 	n_inp->sctp_ep.local_hmacs =
196 	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
197 	if (n_inp->sctp_ep.local_auth_chunks)
198 		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
199 	n_inp->sctp_ep.local_auth_chunks =
200 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
201 	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
202 	    &n_inp->sctp_ep.shared_keys);
203 
204 	n_inp->sctp_socket = newso;
205 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
206 		sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
207 		n_inp->sctp_ep.auto_close_time = 0;
208 		sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
209 		    SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1);
210 	}
211 	/* Turn off any non-blocking semantic. */
212 	SCTP_CLEAR_SO_NBIO(newso);
213 	newso->so_state |= SS_ISCONNECTED;
214 	/* We remove it right away */
215 
216 #ifdef SCTP_LOCK_LOGGING
217 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
218 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
219 	}
220 #endif
221 	TAILQ_REMOVE(&head->so_comp, newso, so_list);
222 	head->so_qlen--;
223 	SOCK_UNLOCK(head);
224 	/*
225 	 * Now we must move it from one hash table to another and get the
226 	 * stcb in the right place.
227 	 */
228 	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
229 	atomic_add_int(&stcb->asoc.refcnt, 1);
230 	SCTP_TCB_UNLOCK(stcb);
231 	/*
232 	 * And now the final hack. We move data in the pending side i.e.
233 	 * head to the new socket buffer. Let the GRUBBING begin :-0
234 	 */
235 	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
236 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
237 	return (newso);
238 }
239