netlink_module.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2021 Ng Peng Nam Sean
  5. * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/param.h>
  29. #include <sys/kernel.h>
  30. #include <sys/malloc.h>
  31. #include <sys/module.h>
  32. #include <sys/lock.h>
  33. #include <sys/rmlock.h>
  34. #include <sys/ck.h>
  35. #include <sys/syslog.h>
  36. #include <netlink/netlink.h>
  37. #include <netlink/netlink_ctl.h>
  38. #include <netlink/netlink_var.h>
  39. #include <netlink/route/route_var.h>
  40. #include <machine/atomic.h>
  41. FEATURE(netlink, "Netlink support");
  42. #define DEBUG_MOD_NAME nl_mod
  43. #define DEBUG_MAX_LEVEL LOG_DEBUG3
  44. #include <netlink/netlink_debug.h>
  45. _DECLARE_DEBUG(LOG_INFO);
  46. #define NL_MAX_HANDLERS 20
  47. struct nl_proto_handler _nl_handlers[NL_MAX_HANDLERS];
  48. struct nl_proto_handler *nl_handlers = _nl_handlers;
  49. CK_LIST_HEAD(nl_control_head, nl_control);
  50. static struct nl_control_head vnets_head = CK_LIST_HEAD_INITIALIZER();
  51. VNET_DEFINE(struct nl_control *, nl_ctl) = NULL;
  52. struct mtx nl_global_mtx;
  53. MTX_SYSINIT(nl_global_mtx, &nl_global_mtx, "global netlink lock", MTX_DEF);
  54. #define NL_GLOBAL_LOCK() mtx_lock(&nl_global_mtx)
  55. #define NL_GLOBAL_UNLOCK() mtx_unlock(&nl_global_mtx)
  56. int netlink_unloading = 0;
  57. static void
  58. free_nl_ctl(struct nl_control *ctl)
  59. {
  60. rm_destroy(&ctl->ctl_lock);
  61. free(ctl, M_NETLINK);
  62. }
  63. struct nl_control *
  64. vnet_nl_ctl_init(void)
  65. {
  66. struct nl_control *ctl;
  67. ctl = malloc(sizeof(struct nl_control), M_NETLINK, M_WAITOK | M_ZERO);
  68. rm_init(&ctl->ctl_lock, "netlink lock");
  69. CK_LIST_INIT(&ctl->ctl_port_head);
  70. CK_LIST_INIT(&ctl->ctl_pcb_head);
  71. NL_GLOBAL_LOCK();
  72. struct nl_control *tmp = atomic_load_ptr(&V_nl_ctl);
  73. if (tmp == NULL) {
  74. atomic_store_ptr(&V_nl_ctl, ctl);
  75. CK_LIST_INSERT_HEAD(&vnets_head, ctl, ctl_next);
  76. NL_LOG(LOG_DEBUG2, "VNET %p init done, inserted %p into global list",
  77. curvnet, ctl);
  78. } else {
  79. NL_LOG(LOG_DEBUG, "per-VNET init clash, dropping this instance");
  80. free_nl_ctl(ctl);
  81. ctl = tmp;
  82. }
  83. NL_GLOBAL_UNLOCK();
  84. return (ctl);
  85. }
  86. static void
  87. vnet_nl_ctl_destroy(const void *unused __unused)
  88. {
  89. struct nl_control *ctl;
  90. /* Assume at the time all of the processes / sockets are dead */
  91. NL_GLOBAL_LOCK();
  92. ctl = atomic_load_ptr(&V_nl_ctl);
  93. atomic_store_ptr(&V_nl_ctl, NULL);
  94. if (ctl != NULL) {
  95. NL_LOG(LOG_DEBUG2, "Removing %p from global list", ctl);
  96. CK_LIST_REMOVE(ctl, ctl_next);
  97. }
  98. NL_GLOBAL_UNLOCK();
  99. if (ctl != NULL)
  100. free_nl_ctl(ctl);
  101. }
  102. VNET_SYSUNINIT(vnet_nl_ctl_destroy, SI_SUB_PROTO_IF, SI_ORDER_ANY,
  103. vnet_nl_ctl_destroy, NULL);
  104. int
  105. nl_verify_proto(int proto)
  106. {
  107. if (proto < 0 || proto >= NL_MAX_HANDLERS) {
  108. return (EINVAL);
  109. }
  110. int handler_defined = nl_handlers[proto].cb != NULL;
  111. return (handler_defined ? 0 : EPROTONOSUPPORT);
  112. }
  113. const char *
  114. nl_get_proto_name(int proto)
  115. {
  116. return (nl_handlers[proto].proto_name);
  117. }
  118. bool
  119. netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler)
  120. {
  121. if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
  122. return (false);
  123. NL_GLOBAL_LOCK();
  124. KASSERT((nl_handlers[proto].cb == NULL), ("netlink handler %d is already set", proto));
  125. nl_handlers[proto].cb = handler;
  126. nl_handlers[proto].proto_name = proto_name;
  127. NL_GLOBAL_UNLOCK();
  128. NL_LOG(LOG_DEBUG2, "Registered netlink %s(%d) handler", proto_name, proto);
  129. return (true);
  130. }
  131. bool
  132. netlink_unregister_proto(int proto)
  133. {
  134. if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
  135. return (false);
  136. NL_GLOBAL_LOCK();
  137. KASSERT((nl_handlers[proto].cb != NULL), ("netlink handler %d is not set", proto));
  138. nl_handlers[proto].cb = NULL;
  139. nl_handlers[proto].proto_name = NULL;
  140. NL_GLOBAL_UNLOCK();
  141. NL_LOG(LOG_DEBUG2, "Unregistered netlink proto %d handler", proto);
  142. return (true);
  143. }
  144. #if !defined(NETLINK) && defined(NETLINK_MODULE)
  145. /* Non-stub function provider */
  146. const static struct nl_function_wrapper nl_module = {
  147. .nlmsg_add = _nlmsg_add,
  148. .nlmsg_refill_buffer = _nlmsg_refill_buffer,
  149. .nlmsg_flush = _nlmsg_flush,
  150. .nlmsg_end = _nlmsg_end,
  151. .nlmsg_abort = _nlmsg_abort,
  152. .nlmsg_get_unicast_writer = _nlmsg_get_unicast_writer,
  153. .nlmsg_get_group_writer = _nlmsg_get_group_writer,
  154. .nlmsg_end_dump = _nlmsg_end_dump,
  155. .nl_modify_ifp_generic = _nl_modify_ifp_generic,
  156. .nl_store_ifp_cookie = _nl_store_ifp_cookie,
  157. .nl_get_thread_nlp = _nl_get_thread_nlp,
  158. };
  159. #endif
  160. static bool
  161. can_unload(void)
  162. {
  163. struct nl_control *ctl;
  164. bool result = true;
  165. NL_GLOBAL_LOCK();
  166. CK_LIST_FOREACH(ctl, &vnets_head, ctl_next) {
  167. NL_LOG(LOG_DEBUG2, "Iterating VNET head %p", ctl);
  168. if (!CK_LIST_EMPTY(&ctl->ctl_pcb_head)) {
  169. NL_LOG(LOG_NOTICE, "non-empty socket list in ctl %p", ctl);
  170. result = false;
  171. break;
  172. }
  173. }
  174. NL_GLOBAL_UNLOCK();
  175. return (result);
  176. }
  177. static int
  178. netlink_modevent(module_t mod __unused, int what, void *priv __unused)
  179. {
  180. int ret = 0;
  181. switch (what) {
  182. case MOD_LOAD:
  183. NL_LOG(LOG_DEBUG2, "Loading");
  184. nl_osd_register();
  185. #if !defined(NETLINK) && defined(NETLINK_MODULE)
  186. nl_set_functions(&nl_module);
  187. #endif
  188. break;
  189. case MOD_UNLOAD:
  190. NL_LOG(LOG_DEBUG2, "Unload called");
  191. if (can_unload()) {
  192. NL_LOG(LOG_WARNING, "unloading");
  193. netlink_unloading = 1;
  194. #if !defined(NETLINK) && defined(NETLINK_MODULE)
  195. nl_set_functions(NULL);
  196. #endif
  197. nl_osd_unregister();
  198. } else
  199. ret = EBUSY;
  200. break;
  201. default:
  202. ret = EOPNOTSUPP;
  203. break;
  204. }
  205. return (ret);
  206. }
  207. static moduledata_t netlink_mod = { "netlink", netlink_modevent, NULL };
  208. DECLARE_MODULE(netlink, netlink_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  209. MODULE_VERSION(netlink, 1);