sctp_peeloff.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #if defined(__FreeBSD__) && !defined(__Userspace__)
  35. #include <sys/cdefs.h>
  36. __FBSDID("$FreeBSD$");
  37. #endif
  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. 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. if (head == NULL) {
  56. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EBADF);
  57. return (EBADF);
  58. }
  59. inp = (struct sctp_inpcb *)head->so_pcb;
  60. if (inp == NULL) {
  61. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
  62. return (EFAULT);
  63. }
  64. if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  65. (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
  66. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EOPNOTSUPP);
  67. return (EOPNOTSUPP);
  68. }
  69. stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
  70. if (stcb == NULL) {
  71. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOENT);
  72. return (ENOENT);
  73. }
  74. state = SCTP_GET_STATE(stcb);
  75. if ((state == SCTP_STATE_EMPTY) ||
  76. (state == SCTP_STATE_INUSE)) {
  77. SCTP_TCB_UNLOCK(stcb);
  78. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
  79. return (ENOTCONN);
  80. }
  81. SCTP_TCB_UNLOCK(stcb);
  82. /* We are clear to peel this one off */
  83. return (0);
  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. inp = (struct sctp_inpcb *)head->so_pcb;
  92. if (inp == NULL) {
  93. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
  94. return (EFAULT);
  95. }
  96. stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
  97. if (stcb == NULL) {
  98. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
  99. return (ENOTCONN);
  100. }
  101. state = SCTP_GET_STATE(stcb);
  102. if ((state == SCTP_STATE_EMPTY) ||
  103. (state == SCTP_STATE_INUSE)) {
  104. SCTP_TCB_UNLOCK(stcb);
  105. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
  106. return (ENOTCONN);
  107. }
  108. n_inp = (struct sctp_inpcb *)so->so_pcb;
  109. n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
  110. SCTP_PCB_FLAGS_CONNECTED |
  111. SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */
  112. (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
  113. n_inp->sctp_socket = so;
  114. n_inp->sctp_features = inp->sctp_features;
  115. n_inp->sctp_mobility_features = inp->sctp_mobility_features;
  116. n_inp->sctp_frag_point = inp->sctp_frag_point;
  117. n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
  118. n_inp->ecn_supported = inp->ecn_supported;
  119. n_inp->prsctp_supported = inp->prsctp_supported;
  120. n_inp->auth_supported = inp->auth_supported;
  121. n_inp->asconf_supported = inp->asconf_supported;
  122. n_inp->reconfig_supported = inp->reconfig_supported;
  123. n_inp->nrsack_supported = inp->nrsack_supported;
  124. n_inp->pktdrop_supported = inp->pktdrop_supported;
  125. n_inp->partial_delivery_point = inp->partial_delivery_point;
  126. n_inp->sctp_context = inp->sctp_context;
  127. n_inp->max_cwnd = inp->max_cwnd;
  128. n_inp->local_strreset_support = inp->local_strreset_support;
  129. /* copy in the authentication parameters from the original endpoint */
  130. if (n_inp->sctp_ep.local_hmacs)
  131. sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
  132. n_inp->sctp_ep.local_hmacs =
  133. sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
  134. if (n_inp->sctp_ep.local_auth_chunks)
  135. sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
  136. n_inp->sctp_ep.local_auth_chunks =
  137. sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
  138. (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
  139. &n_inp->sctp_ep.shared_keys);
  140. #if defined(__Userspace__)
  141. n_inp->ulp_info = inp->ulp_info;
  142. n_inp->recv_callback = inp->recv_callback;
  143. n_inp->send_callback = inp->send_callback;
  144. n_inp->send_sb_threshold = inp->send_sb_threshold;
  145. #endif
  146. /*
  147. * Now we must move it from one hash table to another and get the
  148. * stcb in the right place.
  149. */
  150. sctp_move_pcb_and_assoc(inp, n_inp, stcb);
  151. atomic_add_int(&stcb->asoc.refcnt, 1);
  152. SCTP_TCB_UNLOCK(stcb);
  153. #if defined(__FreeBSD__) && !defined(__Userspace__)
  154. sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
  155. #else
  156. sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK);
  157. #endif
  158. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  159. return (0);
  160. }
  161. #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
  162. struct socket *
  163. sctp_get_peeloff(struct socket *head, sctp_assoc_t assoc_id, int *error)
  164. {
  165. struct socket *newso;
  166. struct sctp_inpcb *inp, *n_inp;
  167. struct sctp_tcb *stcb;
  168. SCTPDBG(SCTP_DEBUG_PEEL1, "SCTP peel-off called\n");
  169. inp = (struct sctp_inpcb *)head->so_pcb;
  170. if (inp == NULL) {
  171. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, EFAULT);
  172. *error = EFAULT;
  173. return (NULL);
  174. }
  175. stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1);
  176. if (stcb == NULL) {
  177. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PEELOFF, ENOTCONN);
  178. *error = ENOTCONN;
  179. return (NULL);
  180. }
  181. atomic_add_int(&stcb->asoc.refcnt, 1);
  182. SCTP_TCB_UNLOCK(stcb);
  183. #if defined(__FreeBSD__) && !defined(__Userspace__)
  184. CURVNET_SET(head->so_vnet);
  185. #endif
  186. newso = sonewconn(head, SS_ISCONNECTED
  187. #if defined(__APPLE__) && !defined(__Userspace__)
  188. , NULL
  189. #endif
  190. );
  191. #if defined(__FreeBSD__) && !defined(__Userspace__)
  192. CURVNET_RESTORE();
  193. #endif
  194. if (newso == NULL) {
  195. SCTPDBG(SCTP_DEBUG_PEEL1, "sctp_peeloff:sonewconn failed\n");
  196. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_PEELOFF, ENOMEM);
  197. *error = ENOMEM;
  198. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  199. return (NULL);
  200. }
  201. #if defined(__APPLE__) && !defined(__Userspace__)
  202. else {
  203. SCTP_SOCKET_LOCK(newso, 1);
  204. }
  205. #endif
  206. SCTP_TCB_LOCK(stcb);
  207. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  208. n_inp = (struct sctp_inpcb *)newso->so_pcb;
  209. SOCK_LOCK(head);
  210. n_inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
  211. SCTP_PCB_FLAGS_CONNECTED |
  212. SCTP_PCB_FLAGS_IN_TCPPOOL | /* Turn on Blocking IO */
  213. (SCTP_PCB_COPY_FLAGS & inp->sctp_flags));
  214. n_inp->sctp_features = inp->sctp_features;
  215. n_inp->sctp_frag_point = inp->sctp_frag_point;
  216. n_inp->sctp_cmt_on_off = inp->sctp_cmt_on_off;
  217. n_inp->ecn_supported = inp->ecn_supported;
  218. n_inp->prsctp_supported = inp->prsctp_supported;
  219. n_inp->auth_supported = inp->auth_supported;
  220. n_inp->asconf_supported = inp->asconf_supported;
  221. n_inp->reconfig_supported = inp->reconfig_supported;
  222. n_inp->nrsack_supported = inp->nrsack_supported;
  223. n_inp->pktdrop_supported = inp->pktdrop_supported;
  224. n_inp->partial_delivery_point = inp->partial_delivery_point;
  225. n_inp->sctp_context = inp->sctp_context;
  226. n_inp->max_cwnd = inp->max_cwnd;
  227. n_inp->local_strreset_support = inp->local_strreset_support;
  228. n_inp->inp_starting_point_for_iterator = NULL;
  229. #if defined(__Userspace__)
  230. n_inp->ulp_info = inp->ulp_info;
  231. n_inp->recv_callback = inp->recv_callback;
  232. n_inp->send_callback = inp->send_callback;
  233. n_inp->send_sb_threshold = inp->send_sb_threshold;
  234. #endif
  235. /* copy in the authentication parameters from the original endpoint */
  236. if (n_inp->sctp_ep.local_hmacs)
  237. sctp_free_hmaclist(n_inp->sctp_ep.local_hmacs);
  238. n_inp->sctp_ep.local_hmacs =
  239. sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
  240. if (n_inp->sctp_ep.local_auth_chunks)
  241. sctp_free_chunklist(n_inp->sctp_ep.local_auth_chunks);
  242. n_inp->sctp_ep.local_auth_chunks =
  243. sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
  244. (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
  245. &n_inp->sctp_ep.shared_keys);
  246. n_inp->sctp_socket = newso;
  247. if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
  248. sctp_feature_off(n_inp, SCTP_PCB_FLAGS_AUTOCLOSE);
  249. n_inp->sctp_ep.auto_close_time = 0;
  250. sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, n_inp, stcb, NULL,
  251. SCTP_FROM_SCTP_PEELOFF + SCTP_LOC_1);
  252. }
  253. /* Turn off any non-blocking semantic. */
  254. SOCK_LOCK(newso);
  255. SCTP_CLEAR_SO_NBIO(newso);
  256. newso->so_state |= SS_ISCONNECTED;
  257. SOCK_UNLOCK(newso);
  258. /* We remove it right away */
  259. #ifdef SCTP_LOCK_LOGGING
  260. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
  261. sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
  262. }
  263. #endif
  264. TAILQ_REMOVE(&head->so_comp, newso, so_list);
  265. head->so_qlen--;
  266. SOCK_UNLOCK(head);
  267. /*
  268. * Now we must move it from one hash table to another and get the
  269. * stcb in the right place.
  270. */
  271. sctp_move_pcb_and_assoc(inp, n_inp, stcb);
  272. atomic_add_int(&stcb->asoc.refcnt, 1);
  273. SCTP_TCB_UNLOCK(stcb);
  274. /*
  275. * And now the final hack. We move data in the pending side i.e.
  276. * head to the new socket buffer. Let the GRUBBING begin :-0
  277. */
  278. #if defined(__FreeBSD__) && !defined(__Userspace__)
  279. sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, SBL_WAIT);
  280. #else
  281. sctp_pull_off_control_to_new_inp(inp, n_inp, stcb, M_WAITOK);
  282. #endif
  283. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  284. return (newso);
  285. }
  286. #endif