xref: /freebsd-src/sys/netinet/sctp_peeloff.c (revision c3179e6660e1365111b89cb6c05c3a4c47375e73)
1f8829a4aSRandall Stewart /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7f8829a4aSRandall Stewart  *
8f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
9f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
10f8829a4aSRandall Stewart  *
11f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
12f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
15f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
16f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
20f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
21f8829a4aSRandall Stewart  *
22f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
33f8829a4aSRandall Stewart  */
34f8829a4aSRandall Stewart 
35f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
36f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
3780fefe0aSRandall Stewart #include <netinet/sctputil.h>
3880fefe0aSRandall Stewart #include <netinet/sctp_var.h>
3980fefe0aSRandall Stewart #include <netinet/sctp_var.h>
4080fefe0aSRandall Stewart #include <netinet/sctp_sysctl.h>
41f8829a4aSRandall Stewart #include <netinet/sctp.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_peeloff.h>
44f8829a4aSRandall Stewart #include <netinet/sctputil.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_auth.h>
46f8829a4aSRandall Stewart 
47f8829a4aSRandall Stewart int
sctp_can_peel_off(struct socket * head,sctp_assoc_t assoc_id)48f8829a4aSRandall Stewart sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
49f8829a4aSRandall Stewart {
50f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
51f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
5218e198d3SRandall Stewart 	uint32_t state;
53f8829a4aSRandall Stewart 
54425d06a1SMichael Tuexen 	if (head == NULL) {
55bd79f2deSMichael Tuexen 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF);
56425d06a1SMichael Tuexen 		return (EBADF);
57425d06a1SMichael Tuexen 	}
58f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)head->so_pcb;
59f8829a4aSRandall Stewart 	if (inp == NULL) {
60bd79f2deSMichael Tuexen 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
61f8829a4aSRandall Stewart 		return (EFAULT);
62f8829a4aSRandall Stewart 	}
6399f293a2SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6499f293a2SMichael Tuexen 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
6599f293a2SMichael Tuexen 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP);
6699f293a2SMichael Tuexen 		return (EOPNOTSUPP);
6799f293a2SMichael Tuexen 	}
68f8829a4aSRandall Stewart 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
69f8829a4aSRandall Stewart 	if (stcb == NULL) {
70ceaad40aSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
71ceaad40aSRandall Stewart 		return (ENOENT);
72f8829a4aSRandall Stewart 	}
73*839d21d6SMichael Tuexen 	state = SCTP_GET_STATE(stcb);
7418e198d3SRandall Stewart 	if ((state == SCTP_STATE_EMPTY) ||
75a50f0e31SMichael Tuexen 	    (state == SCTP_STATE_INUSE)) {
7618e198d3SRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
77c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
7818e198d3SRandall Stewart 		return (ENOTCONN);
7918e198d3SRandall Stewart 	}
80f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
81f8829a4aSRandall Stewart 	/* We are clear to peel this one off */
82f8829a4aSRandall Stewart 	return (0);
83f8829a4aSRandall Stewart }
84f8829a4aSRandall Stewart 
85f8829a4aSRandall Stewart int
sctp_do_peeloff(struct socket * head,struct socket * so,sctp_assoc_t assoc_id)86f8829a4aSRandall Stewart sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
87f8829a4aSRandall Stewart {
88f8829a4aSRandall Stewart 	struct sctp_inpcb *inp, *n_inp;
89f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
9018e198d3SRandall Stewart 	uint32_t state;
91f8829a4aSRandall Stewart 
92f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)head->so_pcb;
93c4739e2fSRandall Stewart 	if (inp == NULL) {
94c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
95f8829a4aSRandall Stewart 		return (EFAULT);
96c4739e2fSRandall Stewart 	}
97f8829a4aSRandall Stewart 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
98c4739e2fSRandall Stewart 	if (stcb == NULL) {
99c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
100f8829a4aSRandall Stewart 		return (ENOTCONN);
101c4739e2fSRandall Stewart 	}
1020053ed28SMichael Tuexen 
103*839d21d6SMichael Tuexen 	state = SCTP_GET_STATE(stcb);
10418e198d3SRandall Stewart 	if ((state == SCTP_STATE_EMPTY) ||
105a50f0e31SMichael Tuexen 	    (state == SCTP_STATE_INUSE)) {
10618e198d3SRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
107c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
10818e198d3SRandall Stewart 		return (ENOTCONN);
10918e198d3SRandall Stewart 	}
1100053ed28SMichael Tuexen 
111f8829a4aSRandall Stewart 	n_inp = (struct sctp_inpcb *)so->so_pcb;
112f8829a4aSRandall Stewart 	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
113f8829a4aSRandall Stewart 	    SCTP_PCB_FLAGS_CONNECTED |
114f8829a4aSRandall Stewart 	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
115f8829a4aSRandall Stewart 	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
116f8829a4aSRandall Stewart 	n_inp->sctp_socket = so;
117f8829a4aSRandall Stewart 	n_inp->sctp_features = inp->sctp_features;
1182afb3e84SRandall Stewart 	n_inp->sctp_mobility_features = inp->sctp_mobility_features;
119f8829a4aSRandall Stewart 	n_inp->sctp_frag_point = inp->sctp_frag_point;
12020083c2eSMichael Tuexen 	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
121f342355aSMichael Tuexen 	n_inp->ecn_supported = inp->ecn_supported;
122dd973b0eSMichael Tuexen 	n_inp->prsctp_supported = inp->prsctp_supported;
123c79bec9cSMichael Tuexen 	n_inp->auth_supported = inp->auth_supported;
124c79bec9cSMichael Tuexen 	n_inp->asconf_supported = inp->asconf_supported;
125317e00efSMichael Tuexen 	n_inp->reconfig_supported = inp->reconfig_supported;
126caea9879SMichael Tuexen 	n_inp->nrsack_supported = inp->nrsack_supported;
127cb9b8e6fSMichael Tuexen 	n_inp->pktdrop_supported = inp->pktdrop_supported;
128f8829a4aSRandall Stewart 	n_inp->partial_delivery_point = inp->partial_delivery_point;
129f8829a4aSRandall Stewart 	n_inp->sctp_context = inp->sctp_context;
13059b6d5beSMichael Tuexen 	n_inp->max_cwnd = inp->max_cwnd;
131c4e848b7SRandall Stewart 	n_inp->local_strreset_support = inp->local_strreset_support;
1322afb3e84SRandall Stewart 	/* copy in the authentication parameters from the original endpoint */
1332afb3e84SRandall Stewart 	if (n_inp->sctp_ep.local_hmacs)
1342afb3e84SRandall Stewart 		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
1352afb3e84SRandall Stewart 	n_inp->sctp_ep.local_hmacs =
1362afb3e84SRandall Stewart 	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1372afb3e84SRandall Stewart 	if (n_inp->sctp_ep.local_auth_chunks)
1382afb3e84SRandall Stewart 		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
1392afb3e84SRandall Stewart 	n_inp->sctp_ep.local_auth_chunks =
1402afb3e84SRandall Stewart 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1412afb3e84SRandall Stewart 	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
1422afb3e84SRandall Stewart 	    &n_inp->sctp_ep.shared_keys);
143f8829a4aSRandall Stewart 	/*
144f8829a4aSRandall Stewart 	 * Now we must move it from one hash table to another and get the
145f8829a4aSRandall Stewart 	 * stcb in the right place.
146f8829a4aSRandall Stewart 	 */
147f8829a4aSRandall Stewart 	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
148d61a0ae0SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
149d61a0ae0SRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
150f8829a4aSRandall Stewart 
151265de5bbSRobert Watson 	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
152d61a0ae0SRandall Stewart 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
153f8829a4aSRandall Stewart 
154f8829a4aSRandall Stewart 	return (0);
155f8829a4aSRandall Stewart }
156