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