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.2 2005/07/15 15:02:02 eirikn Exp $ */
5 * Copyright (C) 2002, 2003 Cisco Systems Inc,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
32 #if !(defined(__OpenBSD__) || defined(__APPLE__))
33 #include "opt_ipsec.h"
35 #if defined(__FreeBSD__) || defined(__DragonFly__)
36 #include "opt_inet6.h"
39 #if defined(__NetBSD__)
45 #elif !defined(__OpenBSD__)
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
54 #include <sys/domain.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>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
67 #include <netinet/ip6.h>
69 #include <netinet/in_pcb.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip_var.h>
73 #include <netinet6/ip6_var.h>
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>
86 #include <netinet6/ipsec.h>
87 #include <netkey/key.h>
94 extern u_int32_t sctp_debug_on;
95 #endif /* SCTP_DEBUG */
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;
107 stcb = sctp_findassociation_ep_asocid(inp, assoc_id);
111 /* We are clear to peel this one off */
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;
124 stcb = sctp_findassociation_ep_asocid(inp, assoc_id);
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);
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;
157 if (sctp_debug_on & SCTP_DEBUG_PEEL1) {
158 printf("SCTP peel-off called\n");
160 #endif /* SCTP_DEBUG */
162 inp = (struct sctp_inpcb *)head->so_pcb;
167 stcb = sctp_findassociation_ep_asocid(inp, assoc_id);
172 newso = sonewconn(head, SS_ISCONNECTED);
175 if (sctp_debug_on & SCTP_DEBUG_PEEL1) {
176 printf("sctp_peeloff:sonewconn failed err\n");
178 #endif /* SCTP_DEBUG */
180 SCTP_TCB_UNLOCK(stcb);
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 /* Turn off any non-blocking symantic. */
191 newso->so_state &= ~SS_NBIO;
192 newso->so_state |= SS_ISCONNECTED;
193 /* We remove it right away */
194 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
196 TAILQ_REMOVE(&head->so_comp, newso, so_list);
201 #if defined(__NetBSD__) || defined(__OpenBSD__)
202 newso = TAILQ_FIRST(&head->so_q);
206 if (soqremque(newso, 1) == 0)
207 panic("sctp_peeloff");
208 #endif /* __FreeBSD__ */
210 * Now we must move it from one hash table to another and get
211 * the stcb in the right place.
213 SCTP_INP_WUNLOCK(n_inp);
214 sctp_move_pcb_and_assoc(inp, n_inp, stcb);
216 * And now the final hack. We move data in the
217 * pending side i.e. head to the new socket
218 * buffer. Let the GRUBBING begin :-0
220 sctp_grub_through_socket_buffer(inp, head, newso, stcb);
221 SCTP_TCB_UNLOCK(stcb);