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