Resync patch with contrib.
[dragonfly.git] / sys / netinet / sctp_peeloff.c
blob77395c9f3bf54579edfec5f157ef87b9627ccfdb
1 /* $KAME: sctp_peeloff.c,v 1.12 2004/08/17 04:06:19 itojun Exp $ */
2 /* $DragonFly: src/sys/netinet/sctp_peeloff.c,v 1.5 2006/12/22 23:57:52 swildner Exp $ */
4 /*
5 * Copyright (C) 2002, 2003 Cisco Systems Inc,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 #if !(defined(__OpenBSD__) || defined(__APPLE__))
33 #include "opt_ipsec.h"
34 #endif
35 #if defined(__FreeBSD__) || defined(__DragonFly__)
36 #include "opt_inet6.h"
37 #include "opt_inet.h"
38 #endif
39 #if defined(__NetBSD__)
40 #include "opt_inet.h"
41 #endif
43 #ifdef __APPLE__
44 #include <sctp.h>
45 #elif !defined(__OpenBSD__)
46 #include "opt_sctp.h"
47 #endif
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/domain.h>
55 #include <sys/proc.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/syslog.h>
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #endif
69 #include <netinet/in_pcb.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip_var.h>
72 #ifdef INET6
73 #include <netinet6/ip6_var.h>
74 #endif
75 #include <netinet/ip_icmp.h>
76 #include <netinet/icmp_var.h>
77 #include <netinet/sctp_pcb.h>
78 #include <netinet/sctp.h>
79 #include <netinet/sctp_uio.h>
80 #include <netinet/sctp_var.h>
81 #include <netinet/sctp_peeloff.h>
82 #include <netinet/sctputil.h>
84 #ifdef IPSEC
85 #ifndef __OpenBSD__
86 #include <netinet6/ipsec.h>
87 #include <netproto/key/key.h>
88 #else
89 #undef IPSEC
90 #endif
91 #endif /*IPSEC*/
93 #ifdef SCTP_DEBUG
94 extern u_int32_t sctp_debug_on;
95 #endif /* SCTP_DEBUG */
98 int
99 sctp_can_peel_off(struct socket *head, caddr_t assoc_id)
101 struct sctp_inpcb *inp;
102 struct sctp_tcb *stcb;
103 inp = (struct sctp_inpcb *)head->so_pcb;
104 if (inp == NULL) {
105 return (EFAULT);
107 stcb = sctp_findassociation_ep_asocid(inp, assoc_id);
108 if (stcb == NULL) {
109 return (ENOTCONN);
111 /* We are clear to peel this one off */
112 return (0);
116 sctp_do_peeloff(struct socket *head, struct socket *so, caddr_t assoc_id)
118 struct sctp_inpcb *inp, *n_inp;
119 struct sctp_tcb *stcb;
121 inp = (struct sctp_inpcb *)head->so_pcb;
122 if (inp == NULL)
123 return (EFAULT);
124 stcb = sctp_findassociation_ep_asocid(inp, assoc_id);
125 if (stcb == NULL)
126 return (ENOTCONN);
128 n_inp = (struct sctp_inpcb *)so->so_pcb;
129 n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
130 SCTP_PCB_FLAGS_CONNECTED |
131 SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */
132 (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
133 n_inp->sctp_socket = so;
136 * Now we must move it from one hash table to another and get
137 * the stcb in the right place.
139 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
141 * And now the final hack. We move data in the
142 * pending side i.e. head to the new socket
143 * buffer. Let the GRUBBING begin :-0
145 sctp_grub_through_socket_buffer(inp, head, so, stcb);
146 return (0);
149 struct socket *
150 sctp_get_peeloff(struct socket *head, caddr_t assoc_id, int *error)
152 struct socket *newso;
153 struct sctp_inpcb *inp, *n_inp;
154 struct sctp_tcb *stcb;
156 #ifdef SCTP_DEBUG
157 if (sctp_debug_on & SCTP_DEBUG_PEEL1) {
158 kprintf("SCTP peel-off called\n");
160 #endif /* SCTP_DEBUG */
162 inp = (struct sctp_inpcb *)head->so_pcb;
163 if (inp == NULL) {
164 *error = EFAULT;
165 return (NULL);
167 stcb = sctp_findassociation_ep_asocid(inp, assoc_id);
168 if (stcb == NULL) {
169 *error = ENOTCONN;
170 return (NULL);
172 newso = sonewconn(head, SS_ISCONNECTED);
173 if (newso == NULL) {
174 #ifdef SCTP_DEBUG
175 if (sctp_debug_on & SCTP_DEBUG_PEEL1) {
176 kprintf("sctp_peeloff:sonewconn failed err\n");
178 #endif /* SCTP_DEBUG */
179 *error = ENOMEM;
180 SCTP_TCB_UNLOCK(stcb);
181 return (NULL);
183 n_inp = (struct sctp_inpcb *)newso->so_pcb;
184 SCTP_INP_WLOCK(n_inp);
185 n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
186 SCTP_PCB_FLAGS_CONNECTED |
187 SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */
188 (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
189 n_inp->sctp_socket = newso;
190 newso->so_state |= SS_ISCONNECTED;
191 /* We remove it right away */
192 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
193 SOCK_LOCK(head);
194 TAILQ_REMOVE(&head->so_comp, newso, so_list);
195 head->so_qlen--;
196 SOCK_UNLOCK(head);
197 #else
199 #if defined(__NetBSD__) || defined(__OpenBSD__)
200 newso = TAILQ_FIRST(&head->so_q);
201 #else
202 newso = head->so_q;
203 #endif
204 if (soqremque(newso, 1) == 0)
205 panic("sctp_peeloff");
206 #endif /* __FreeBSD__ */
208 * Now we must move it from one hash table to another and get
209 * the stcb in the right place.
211 SCTP_INP_WUNLOCK(n_inp);
212 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
214 * And now the final hack. We move data in the
215 * pending side i.e. head to the new socket
216 * buffer. Let the GRUBBING begin :-0
218 sctp_grub_through_socket_buffer(inp, head, newso, stcb);
219 SCTP_TCB_UNLOCK(stcb);
220 return (newso);