netlink_route.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  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 AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include <sys/types.h>
  28. #include <sys/ck.h>
  29. #include <sys/epoch.h>
  30. #include <sys/kernel.h>
  31. #include <sys/malloc.h>
  32. #include <sys/socket.h>
  33. #include <net/route.h>
  34. #include <net/route/route_ctl.h>
  35. #include <netlink/netlink.h>
  36. #include <netlink/netlink_ctl.h>
  37. #include <netlink/netlink_route.h>
  38. #include <netlink/route/route_var.h>
  39. #define DEBUG_MOD_NAME nl_route_core
  40. #define DEBUG_MAX_LEVEL LOG_DEBUG3
  41. #include <netlink/netlink_debug.h>
  42. _DECLARE_DEBUG(LOG_INFO);
  43. #define HANDLER_MAX_NUM (NL_RTM_MAX + 10)
  44. static const struct rtnl_cmd_handler *rtnl_handler[HANDLER_MAX_NUM] = {};
  45. bool
  46. rtnl_register_messages(const struct rtnl_cmd_handler *handlers, int count)
  47. {
  48. for (int i = 0; i < count; i++) {
  49. if (handlers[i].cmd >= HANDLER_MAX_NUM)
  50. return (false);
  51. MPASS(rtnl_handler[handlers[i].cmd] == NULL);
  52. }
  53. for (int i = 0; i < count; i++)
  54. rtnl_handler[handlers[i].cmd] = &handlers[i];
  55. return (true);
  56. }
  57. /*
  58. * Handler called by netlink subsystem when matching netlink message is received
  59. */
  60. static int
  61. rtnl_handle_message(struct nlmsghdr *hdr, struct nl_pstate *npt)
  62. {
  63. const struct rtnl_cmd_handler *cmd;
  64. struct epoch_tracker et;
  65. struct nlpcb *nlp = npt->nlp;
  66. int error = 0;
  67. if (__predict_false(hdr->nlmsg_type >= HANDLER_MAX_NUM)) {
  68. NLMSG_REPORT_ERR_MSG(npt, "unknown message type: %d", hdr->nlmsg_type);
  69. return (ENOTSUP);
  70. }
  71. cmd = rtnl_handler[hdr->nlmsg_type];
  72. if (__predict_false(cmd == NULL)) {
  73. NLMSG_REPORT_ERR_MSG(npt, "unknown message type: %d", hdr->nlmsg_type);
  74. return (ENOTSUP);
  75. }
  76. NLP_LOG(LOG_DEBUG2, nlp, "received msg %s(%d) len %d", cmd->name,
  77. hdr->nlmsg_type, hdr->nlmsg_len);
  78. if (cmd->priv != 0 && !nlp_has_priv(nlp, cmd->priv)) {
  79. NLP_LOG(LOG_DEBUG2, nlp, "priv %d check failed for msg %s", cmd->priv, cmd->name);
  80. return (EPERM);
  81. } else if (cmd->priv != 0)
  82. NLP_LOG(LOG_DEBUG3, nlp, "priv %d check passed for msg %s", cmd->priv, cmd->name);
  83. if (!nlp_unconstrained_vnet(nlp) && (cmd->flags & RTNL_F_ALLOW_NONVNET_JAIL) == 0) {
  84. NLP_LOG(LOG_DEBUG2, nlp, "jail check failed for msg %s", cmd->name);
  85. return (EPERM);
  86. }
  87. bool need_epoch = !(cmd->flags & RTNL_F_NOEPOCH);
  88. if (need_epoch)
  89. NET_EPOCH_ENTER(et);
  90. error = cmd->cb(hdr, nlp, npt);
  91. if (need_epoch)
  92. NET_EPOCH_EXIT(et);
  93. NLP_LOG(LOG_DEBUG3, nlp, "message %s -> error %d", cmd->name, error);
  94. return (error);
  95. }
  96. static struct rtbridge nlbridge = {
  97. .route_f = rtnl_handle_route_event,
  98. .ifmsg_f = rtnl_handle_ifnet_event,
  99. };
  100. static struct rtbridge *nlbridge_orig_p;
  101. static void
  102. rtnl_load(void *u __unused)
  103. {
  104. NL_LOG(LOG_DEBUG2, "rtnl loading");
  105. nlbridge_orig_p = netlink_callback_p;
  106. netlink_callback_p = &nlbridge;
  107. rtnl_neighs_init();
  108. rtnl_ifaces_init();
  109. rtnl_nexthops_init();
  110. rtnl_routes_init();
  111. netlink_register_proto(NETLINK_ROUTE, "NETLINK_ROUTE", rtnl_handle_message);
  112. }
  113. SYSINIT(rtnl_load, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtnl_load, NULL);
  114. static void
  115. rtnl_unload(void *u __unused)
  116. {
  117. netlink_callback_p = nlbridge_orig_p;
  118. netlink_unregister_proto(NETLINK_ROUTE);
  119. rtnl_ifaces_destroy();
  120. rtnl_neighs_destroy();
  121. /* Wait till all consumers read nlbridge data */
  122. NET_EPOCH_WAIT();
  123. }
  124. SYSUNINIT(rtnl_unload, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtnl_unload, NULL);