sctp_peeloff.c 5.7 KB

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