xref: /freebsd/sys/netinet/sctp_peeloff.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
1 /*-
2  * Copyright (c) 2001-2007, 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/sctp.h>
39 #include <netinet/sctp_uio.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_peeloff.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_auth.h>
44 
45 
46 #ifdef SCTP_DEBUG
47 extern uint32_t sctp_debug_on;
48 
49 #endif				/* SCTP_DEBUG */
50 
51 
52 int
53 sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
54 {
55 	struct sctp_inpcb *inp;
56 	struct sctp_tcb *stcb;
57 
58 	inp = (struct sctp_inpcb *)head->so_pcb;
59 	if (inp == NULL) {
60 		return (EFAULT);
61 	}
62 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
63 	if (stcb == NULL) {
64 		return (ENOTCONN);
65 	}
66 	SCTP_TCB_UNLOCK(stcb);
67 	/* We are clear to peel this one off */
68 	return (0);
69 }
70 
71 int
72 sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
73 {
74 	struct sctp_inpcb *inp, *n_inp;
75 	struct sctp_tcb *stcb;
76 
77 	inp = (struct sctp_inpcb *)head->so_pcb;
78 	if (inp == NULL)
79 		return (EFAULT);
80 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
81 	if (stcb == NULL)
82 		return (ENOTCONN);
83 
84 	n_inp = (struct sctp_inpcb *)so->so_pcb;
85 	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
86 	    SCTP_PCB_FLAGS_CONNECTED |
87 	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
88 	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
89 	n_inp->sctp_socket = so;
90 	n_inp->sctp_features = inp->sctp_features;
91 	n_inp->sctp_frag_point = inp->sctp_frag_point;
92 	n_inp->partial_delivery_point = inp->partial_delivery_point;
93 	n_inp->sctp_context = inp->sctp_context;
94 	n_inp->inp_starting_point_for_iterator = NULL;
95 
96 	/*
97 	 * Now we must move it from one hash table to another and get the
98 	 * stcb in the right place.
99 	 */
100 	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
101 
102 	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb);
103 
104 	SCTP_TCB_UNLOCK(stcb);
105 	return (0);
106 }
107 
108 struct socket *
109 sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
110 {
111 	struct socket *newso;
112 	struct sctp_inpcb *inp, *n_inp;
113 	struct sctp_tcb *stcb;
114 
115 #ifdef SCTP_DEBUG
116 	if (sctp_debug_on & SCTP_DEBUG_PEEL1) {
117 		printf("SCTP peel-off called\n");
118 	}
119 #endif				/* SCTP_DEBUG */
120 
121 	inp = (struct sctp_inpcb *)head->so_pcb;
122 	if (inp == NULL) {
123 		*error = EFAULT;
124 		return (NULL);
125 	}
126 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
127 	if (stcb == NULL) {
128 		*error = ENOTCONN;
129 		return (NULL);
130 	}
131 	newso = sonewconn(head, SS_ISCONNECTED
132 	    );
133 	if (newso == NULL) {
134 #ifdef SCTP_DEBUG
135 		if (sctp_debug_on & SCTP_DEBUG_PEEL1) {
136 			printf("sctp_peeloff:sonewconn failed err\n");
137 		}
138 #endif				/* SCTP_DEBUG */
139 		*error = ENOMEM;
140 		SCTP_TCB_UNLOCK(stcb);
141 		return (NULL);
142 	}
143 	n_inp = (struct sctp_inpcb *)newso->so_pcb;
144 	SOCK_LOCK(head);
145 	SCTP_INP_WLOCK(inp);
146 	SCTP_INP_WLOCK(n_inp);
147 	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
148 	    SCTP_PCB_FLAGS_CONNECTED |
149 	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
150 	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
151 	n_inp->sctp_features = inp->sctp_features;
152 	n_inp->sctp_frag_point = inp->sctp_frag_point;
153 	n_inp->partial_delivery_point = inp->partial_delivery_point;
154 	n_inp->sctp_context = inp->sctp_context;
155 	n_inp->inp_starting_point_for_iterator = NULL;
156 
157 	/* copy in the authentication parameters from the original endpoint */
158 	if (n_inp->sctp_ep.local_hmacs)
159 		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
160 	n_inp->sctp_ep.local_hmacs =
161 	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
162 	if (n_inp->sctp_ep.local_auth_chunks)
163 		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
164 	n_inp->sctp_ep.local_auth_chunks =
165 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
166 	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
167 	    &n_inp->sctp_ep.shared_keys);
168 
169 	n_inp->sctp_socket = newso;
170 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
171 		sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
172 		n_inp->sctp_ep.auto_close_time = 0;
173 		sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
174 		    SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1);
175 	}
176 	/* Turn off any non-blocking semantic. */
177 	SCTP_CLEAR_SO_NBIO(newso);
178 	newso->so_state |= SS_ISCONNECTED;
179 	/* We remove it right away */
180 #ifdef SCTP_LOCK_LOGGING
181 	sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
182 #endif
183 	TAILQ_REMOVE(&head->so_comp, newso, so_list);
184 	head->so_qlen--;
185 	SOCK_UNLOCK(head);
186 	/*
187 	 * Now we must move it from one hash table to another and get the
188 	 * stcb in the right place.
189 	 */
190 	SCTP_INP_WUNLOCK(n_inp);
191 	SCTP_INP_WUNLOCK(inp);
192 	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
193 	/*
194 	 * And now the final hack. We move data in the pending side i.e.
195 	 * head to the new socket buffer. Let the GRUBBING begin :-0
196 	 */
197 	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb);
198 
199 	SCTP_TCB_UNLOCK(stcb);
200 	return (newso);
201 }
202