ipsec_mod.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*-
  2. * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "opt_inet.h"
  27. #include "opt_inet6.h"
  28. #include "opt_ipsec.h"
  29. #include <sys/param.h>
  30. #include <sys/systm.h>
  31. #include <sys/kernel.h>
  32. #include <sys/lock.h>
  33. #include <sys/malloc.h>
  34. #include <sys/mbuf.h>
  35. #include <sys/module.h>
  36. #include <sys/priv.h>
  37. #include <sys/rmlock.h>
  38. #include <sys/socket.h>
  39. #include <sys/sockopt.h>
  40. #include <sys/syslog.h>
  41. #include <sys/proc.h>
  42. #include <netinet/in.h>
  43. #include <netinet/in_pcb.h>
  44. #include <netipsec/ipsec.h>
  45. #include <netipsec/ipsec6.h>
  46. #include <netipsec/key.h>
  47. #include <netipsec/key_debug.h>
  48. #include <netipsec/ipsec_support.h>
  49. #ifdef INET
  50. static const struct ipsec_methods ipv4_methods = {
  51. .input = ipsec4_input,
  52. .forward = ipsec4_forward,
  53. .output = ipsec4_output,
  54. .pcbctl = ipsec4_pcbctl,
  55. .capability = ipsec4_capability,
  56. .check_policy = ipsec4_in_reject,
  57. .ctlinput = ipsec4_ctlinput,
  58. .hdrsize = ipsec_hdrsiz_inpcb,
  59. .udp_input = udp_ipsec_input,
  60. .udp_pcbctl = udp_ipsec_pcbctl,
  61. };
  62. #ifndef KLD_MODULE
  63. static const struct ipsec_support ipv4_ipsec = {
  64. .enabled = IPSEC_MODULE_ENABLED,
  65. .methods = &ipv4_methods
  66. };
  67. const struct ipsec_support * const ipv4_ipsec_support = &ipv4_ipsec;
  68. #endif /* !KLD_MODULE */
  69. #endif /* INET */
  70. #ifdef INET6
  71. static const struct ipsec_methods ipv6_methods = {
  72. .input = ipsec6_input,
  73. .forward = ipsec6_forward,
  74. .output = ipsec6_output,
  75. .pcbctl = ipsec6_pcbctl,
  76. .capability = ipsec6_capability,
  77. .check_policy = ipsec6_in_reject,
  78. .ctlinput = ipsec6_ctlinput,
  79. .hdrsize = ipsec_hdrsiz_inpcb,
  80. .udp_input = udp_ipsec_input,
  81. .udp_pcbctl = udp_ipsec_pcbctl,
  82. };
  83. #ifndef KLD_MODULE
  84. static const struct ipsec_support ipv6_ipsec = {
  85. .enabled = IPSEC_MODULE_ENABLED,
  86. .methods = &ipv6_methods
  87. };
  88. const struct ipsec_support * const ipv6_ipsec_support = &ipv6_ipsec;
  89. #endif /* !KLD_MODULE */
  90. #endif /* INET6 */
  91. /*
  92. * Always register ipsec module.
  93. * Even when IPsec is build in the kernel, we need to have
  94. * module registered. This will prevent to load ipsec.ko.
  95. */
  96. static int
  97. ipsec_modevent(module_t mod, int type, void *data)
  98. {
  99. switch (type) {
  100. case MOD_LOAD:
  101. /* All xforms are registered via SYSINIT */
  102. if (!ipsec_initialized())
  103. return (ENOMEM);
  104. #ifdef KLD_MODULE
  105. #ifdef INET
  106. ipsec_support_enable(ipv4_ipsec_support, &ipv4_methods);
  107. #endif
  108. #ifdef INET6
  109. ipsec_support_enable(ipv6_ipsec_support, &ipv6_methods);
  110. #endif
  111. #endif /* KLD_MODULE */
  112. break;
  113. case MOD_UNLOAD:
  114. /* All xforms are unregistered via SYSUNINIT */
  115. #ifdef KLD_MODULE
  116. #ifdef INET
  117. ipsec_support_disable(ipv4_ipsec_support);
  118. #endif
  119. #ifdef INET6
  120. ipsec_support_disable(ipv6_ipsec_support);
  121. #endif
  122. #endif /* KLD_MODULE */
  123. break;
  124. default:
  125. return (EOPNOTSUPP);
  126. }
  127. return (0);
  128. }
  129. static moduledata_t ipsec_mod = {
  130. "ipsec",
  131. ipsec_modevent,
  132. 0
  133. };
  134. DECLARE_MODULE(ipsec, ipsec_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
  135. MODULE_VERSION(ipsec, 1);
  136. #ifdef KLD_MODULE
  137. MODULE_DEPEND(ipsec, ipsec_support, 1, 1, 1);
  138. #endif