xref: /freebsd/sys/netinet/sctp_peeloff.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 <netinet/sctp_os.h>
36 #include <netinet/sctp_pcb.h>
37 #include <netinet/sctputil.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp.h>
42 #include <netinet/sctp_uio.h>
43 #include <netinet/sctp_peeloff.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_auth.h>
46 
47 int
48 sctp_can_peel_off(struct socket *head, sctp_assoc_t assoc_id)
49 {
50 	struct sctp_inpcb *inp;
51 	struct sctp_tcb *stcb;
52 	uint32_t state;
53 
54 	if (head == NULL) {
55 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF);
56 		return (EBADF);
57 	}
58 	inp = (struct sctp_inpcb *)head->so_pcb;
59 	if (inp == NULL) {
60 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
61 		return (EFAULT);
62 	}
63 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
64 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
65 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP);
66 		return (EOPNOTSUPP);
67 	}
68 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
69 	if (stcb == NULL) {
70 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
71 		return (ENOENT);
72 	}
73 	state = SCTP_GET_STATE(stcb);
74 	if ((state == SCTP_STATE_EMPTY) ||
75 	    (state == SCTP_STATE_INUSE)) {
76 		SCTP_TCB_UNLOCK(stcb);
77 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
78 		return (ENOTCONN);
79 	}
80 	SCTP_TCB_UNLOCK(stcb);
81 	/* We are clear to peel this one off */
82 	return (0);
83 }
84 
85 int
86 sctp_do_peeloff(struct socket *head, struct socket *so, sctp_assoc_t assoc_id)
87 {
88 	struct sctp_inpcb *inp, *n_inp;
89 	struct sctp_tcb *stcb;
90 	uint32_t state;
91 
92 	inp = (struct sctp_inpcb *)head->so_pcb;
93 	if (inp == NULL) {
94 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
95 		return (EFAULT);
96 	}
97 	stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
98 	if (stcb == NULL) {
99 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
100 		return (ENOTCONN);
101 	}
102 
103 	state = SCTP_GET_STATE(stcb);
104 	if ((state == SCTP_STATE_EMPTY) ||
105 	    (state == SCTP_STATE_INUSE)) {
106 		SCTP_TCB_UNLOCK(stcb);
107 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
108 		return (ENOTCONN);
109 	}
110 
111 	n_inp = (struct sctp_inpcb *)so->so_pcb;
112 	n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
113 	    SCTP_PCB_FLAGS_CONNECTED |
114 	    SCTP_PCB_FLAGS_IN_TCPPOOL |	/* Turn on Blocking IO */
115 	    (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
116 	n_inp->sctp_socket = so;
117 	n_inp->sctp_features = inp->sctp_features;
118 	n_inp->sctp_mobility_features = inp->sctp_mobility_features;
119 	n_inp->sctp_frag_point = inp->sctp_frag_point;
120 	n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
121 	n_inp->ecn_supported = inp->ecn_supported;
122 	n_inp->prsctp_supported = inp->prsctp_supported;
123 	n_inp->auth_supported = inp->auth_supported;
124 	n_inp->asconf_supported = inp->asconf_supported;
125 	n_inp->reconfig_supported = inp->reconfig_supported;
126 	n_inp->nrsack_supported = inp->nrsack_supported;
127 	n_inp->pktdrop_supported = inp->pktdrop_supported;
128 	n_inp->partial_delivery_point = inp->partial_delivery_point;
129 	n_inp->sctp_context = inp->sctp_context;
130 	n_inp->max_cwnd = inp->max_cwnd;
131 	n_inp->local_strreset_support = inp->local_strreset_support;
132 	/* copy in the authentication parameters from the original endpoint */
133 	if (n_inp->sctp_ep.local_hmacs)
134 		sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
135 	n_inp->sctp_ep.local_hmacs =
136 	    sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
137 	if (n_inp->sctp_ep.local_auth_chunks)
138 		sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
139 	n_inp->sctp_ep.local_auth_chunks =
140 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
141 	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
142 	    &n_inp->sctp_ep.shared_keys);
143 	/*
144 	 * Now we must move it from one hash table to another and get the
145 	 * stcb in the right place.
146 	 */
147 	sctp_move_pcb_and_assoc(inp, n_inp, stcb);
148 	atomic_add_int(&stcb->asoc.refcnt, 1);
149 	SCTP_TCB_UNLOCK(stcb);
150 
151 	sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
152 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
153 
154 	return (0);
155 }
156