sctp_module.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2019-2020 The FreeBSD Foundation
  5. *
  6. * This software was developed by Mark Johnston under sponsorship from
  7. * the FreeBSD Foundation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. 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. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. __FBSDID("$FreeBSD$");
  32. #include "opt_inet.h"
  33. #include "opt_inet6.h"
  34. #include <sys/param.h>
  35. #include <sys/systm.h>
  36. #include <sys/kernel.h>
  37. #include <sys/module.h>
  38. #include <sys/protosw.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <netinet/ip.h>
  42. #include <netinet/ip_var.h>
  43. #include <netinet/sctp.h>
  44. #include <netinet/sctp_pcb.h>
  45. #include <netinet/sctp_var.h>
  46. #include <netinet/sctp_os_bsd.h>
  47. #include <netinet6/ip6_var.h>
  48. #include <netinet6/sctp6_var.h>
  49. #ifdef INET
  50. extern struct domain inetdomain;
  51. struct protosw sctp_stream_protosw = {
  52. .pr_type = SOCK_STREAM,
  53. .pr_domain = &inetdomain,
  54. .pr_protocol = IPPROTO_SCTP,
  55. .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LASTHDR,
  56. .pr_input = sctp_input,
  57. .pr_ctlinput = sctp_ctlinput,
  58. .pr_ctloutput = sctp_ctloutput,
  59. .pr_drain = sctp_drain,
  60. .pr_usrreqs = &sctp_usrreqs,
  61. };
  62. struct protosw sctp_seqpacket_protosw = {
  63. .pr_type = SOCK_SEQPACKET,
  64. .pr_domain = &inetdomain,
  65. .pr_protocol = IPPROTO_SCTP,
  66. .pr_flags = PR_WANTRCVD|PR_LASTHDR,
  67. .pr_input = sctp_input,
  68. .pr_ctlinput = sctp_ctlinput,
  69. .pr_ctloutput = sctp_ctloutput,
  70. .pr_drain = sctp_drain,
  71. .pr_usrreqs = &sctp_usrreqs,
  72. };
  73. #endif
  74. #ifdef INET6
  75. extern struct domain inet6domain;
  76. struct protosw sctp6_stream_protosw = {
  77. .pr_type = SOCK_STREAM,
  78. .pr_domain = &inet6domain,
  79. .pr_protocol = IPPROTO_SCTP,
  80. .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LASTHDR,
  81. .pr_input = sctp6_input,
  82. .pr_ctlinput = sctp6_ctlinput,
  83. .pr_ctloutput = sctp_ctloutput,
  84. .pr_init = sctp_init,
  85. .pr_drain = sctp_drain,
  86. .pr_usrreqs = &sctp6_usrreqs,
  87. };
  88. struct protosw sctp6_seqpacket_protosw = {
  89. .pr_type = SOCK_SEQPACKET,
  90. .pr_domain = &inet6domain,
  91. .pr_protocol = IPPROTO_SCTP,
  92. .pr_flags = PR_WANTRCVD|PR_LASTHDR,
  93. .pr_input = sctp6_input,
  94. .pr_ctlinput = sctp6_ctlinput,
  95. .pr_ctloutput = sctp_ctloutput,
  96. #ifndef INET /* Do not call initialization and drain routines twice. */
  97. .pr_init = sctp_init,
  98. .pr_drain = sctp_drain,
  99. #endif
  100. .pr_usrreqs = &sctp6_usrreqs,
  101. };
  102. #endif
  103. static int
  104. sctp_module_load(void)
  105. {
  106. int error;
  107. #ifdef INET
  108. error = pf_proto_register(PF_INET, &sctp_stream_protosw);
  109. if (error != 0)
  110. return (error);
  111. error = pf_proto_register(PF_INET, &sctp_seqpacket_protosw);
  112. if (error != 0)
  113. return (error);
  114. error = ipproto_register(IPPROTO_SCTP);
  115. if (error != 0)
  116. return (error);
  117. #endif
  118. #ifdef INET6
  119. error = pf_proto_register(PF_INET6, &sctp6_stream_protosw);
  120. if (error != 0)
  121. return (error);
  122. error = pf_proto_register(PF_INET6, &sctp6_seqpacket_protosw);
  123. if (error != 0)
  124. return (error);
  125. error = ip6proto_register(IPPROTO_SCTP);
  126. if (error != 0)
  127. return (error);
  128. #endif
  129. error = sctp_syscalls_init();
  130. if (error != 0)
  131. return (error);
  132. return (0);
  133. }
  134. static int __unused
  135. sctp_module_unload(void)
  136. {
  137. (void)sctp_syscalls_uninit();
  138. #ifdef INET
  139. (void)ipproto_unregister(IPPROTO_SCTP);
  140. (void)pf_proto_unregister(PF_INET, IPPROTO_SCTP, SOCK_STREAM);
  141. (void)pf_proto_unregister(PF_INET, IPPROTO_SCTP, SOCK_SEQPACKET);
  142. #endif
  143. #ifdef INET6
  144. (void)ip6proto_unregister(IPPROTO_SCTP);
  145. (void)pf_proto_unregister(PF_INET6, IPPROTO_SCTP, SOCK_STREAM);
  146. (void)pf_proto_unregister(PF_INET6, IPPROTO_SCTP, SOCK_SEQPACKET);
  147. #endif
  148. return (0);
  149. }
  150. static int
  151. sctp_modload(struct module *module, int cmd, void *arg)
  152. {
  153. int error;
  154. switch (cmd) {
  155. case MOD_LOAD:
  156. error = sctp_module_load();
  157. break;
  158. case MOD_UNLOAD:
  159. /*
  160. * Unloading SCTP is currently unsupported. Currently, SCTP
  161. * iterator threads are not stopped during unload.
  162. */
  163. error = EOPNOTSUPP;
  164. break;
  165. default:
  166. error = 0;
  167. break;
  168. }
  169. return (error);
  170. }
  171. static moduledata_t sctp_mod = {
  172. "sctp",
  173. &sctp_modload,
  174. NULL,
  175. };
  176. DECLARE_MODULE(sctp, sctp_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
  177. MODULE_VERSION(sctp, 1);