xref: /freebsd/sys/netinet/sctp_peeloff.c (revision 190cef3d52236565eb22e18b33e9e865ec634aa3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctputil.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_peeloff.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_auth.h>
49 
50 
51 int
52 sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
53 {
54 	struct sctp_inpcb *inp;
55 	struct sctp_tcb *stcb;
56 	uint32_t state;
57 
58 	if (head == NULL) {
59 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF);
60 		return (EBADF);
61 	}
62 	inp = (struct sctp_inpcb *)head->so_pcb;
63 	if (inp == NULL) {
64 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
65 		return (EFAULT);
66 	}
67 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
68 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
69 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP);
70 		return (EOPNOTSUPP);
71 	}
72 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
73 	if (stcb == NULL) {
74 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
75 		return (ENOENT);
76 	}
77 	state = SCTP_GET_STATE(stcb);
78 	if ((state == SCTP_STATE_EMPTY) ||
79 	    (state == SCTP_STATE_INUSE)) {
80 		SCTP_TCB_UNLOCK(stcb);
81 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
82 		return (ENOTCONN);
83 	}
84 	SCTP_TCB_UNLOCK(stcb);
85 	/* We are clear to peel this one off */
86 	return (0);
87 }
88 
89 int
90 sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
91 {
92 	struct sctp_inpcb *inp, *n_inp;
93 	struct sctp_tcb *stcb;
94 	uint32_t state;
95 
96 	inp = (struct sctp_inpcb *)head->so_pcb;
97 	if (inp == NULL) {
98 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
99 		return (EFAULT);
100 	}
101 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
102 	if (stcb == NULL) {
103 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
104 		return (ENOTCONN);
105 	}
106 
107 	state = SCTP_GET_STATE(stcb);
108 	if ((state == SCTP_STATE_EMPTY) ||
109 	    (state == SCTP_STATE_INUSE)) {
110 		SCTP_TCB_UNLOCK(stcb);
111 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
112 		return (ENOTCONN);
113 	}
114 
115 	n_inp = (struct sctp_inpcb *)so->so_pcb;
116 	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
117 	    SCTP_PCB_FLAGS_CONNECTED |
118 	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
119 	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
120 	n_inp->sctp_socket = so;
121 	n_inp->sctp_features = inp->sctp_features;
122 	n_inp->sctp_mobility_features = inp->sctp_mobility_features;
123 	n_inp->sctp_frag_point = inp->sctp_frag_point;
124 	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
125 	n_inp->ecn_supported = inp->ecn_supported;
126 	n_inp->prsctp_supported = inp->prsctp_supported;
127 	n_inp->auth_supported = inp->auth_supported;
128 	n_inp->asconf_supported = inp->asconf_supported;
129 	n_inp->reconfig_supported = inp->reconfig_supported;
130 	n_inp->nrsack_supported = inp->nrsack_supported;
131 	n_inp->pktdrop_supported = inp->pktdrop_supported;
132 	n_inp->partial_delivery_point = inp->partial_delivery_point;
133 	n_inp->sctp_context = inp->sctp_context;
134 	n_inp->max_cwnd = inp->max_cwnd;
135 	n_inp->local_strreset_support = inp->local_strreset_support;
136 	n_inp->inp_starting_point_for_iterator = NULL;
137 	/* copy in the authentication parameters from the original endpoint */
138 	if (n_inp->sctp_ep.local_hmacs)
139 		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
140 	n_inp->sctp_ep.local_hmacs =
141 	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
142 	if (n_inp->sctp_ep.local_auth_chunks)
143 		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
144 	n_inp->sctp_ep.local_auth_chunks =
145 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
146 	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
147 	    &n_inp->sctp_ep.shared_keys);
148 	/*
149 	 * Now we must move it from one hash table to another and get the
150 	 * stcb in the right place.
151 	 */
152 	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
153 	atomic_add_int(&stcb->asoc.refcnt, 1);
154 	SCTP_TCB_UNLOCK(stcb);
155 
156 	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
157 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
158 
159 	return (0);
160 }
161