netlink_glue.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2023 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/param.h>
  28. #include <sys/kernel.h>
  29. #include <sys/malloc.h>
  30. #include <sys/lock.h>
  31. #include <sys/rmlock.h>
  32. #include <sys/domain.h>
  33. #include <sys/mbuf.h>
  34. #include <sys/protosw.h>
  35. #include <sys/proc.h>
  36. #include <sys/ck.h>
  37. #include <sys/socket.h>
  38. #include <sys/socketvar.h>
  39. #include <sys/sysent.h>
  40. #include <sys/syslog.h>
  41. #include <sys/priv.h> /* priv_check */
  42. #include <net/route.h>
  43. #include <net/route/route_ctl.h>
  44. #include <netlink/netlink.h>
  45. #include <netlink/netlink_ctl.h>
  46. #include <netlink/netlink_var.h>
  47. #include <netlink/route/route_var.h>
  48. /* Standard bits: built-in the kernel */
  49. SYSCTL_NODE(_net, OID_AUTO, netlink, CTLFLAG_RD, 0,
  50. "RFC3549 Netlink network state socket family");
  51. SYSCTL_NODE(_net_netlink, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
  52. "Netlink per-subsystem debug levels");
  53. MALLOC_DEFINE(M_NETLINK, "netlink", "Memory used for netlink packets");
  54. /* Netlink-related callbacks needed to glue rtsock, netlink and linuxolator */
  55. static void
  56. ignore_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
  57. {
  58. }
  59. static void
  60. ignore_ifmsg_event(struct ifnet *ifp, int if_flags_mask)
  61. {
  62. }
  63. static struct rtbridge ignore_cb = {
  64. .route_f = ignore_route_event,
  65. .ifmsg_f = ignore_ifmsg_event,
  66. };
  67. void *linux_netlink_p = NULL; /* Callback pointer for Linux translator functions */
  68. struct rtbridge *rtsock_callback_p = &ignore_cb;
  69. struct rtbridge *netlink_callback_p = &ignore_cb;
  70. /*
  71. * nlp accessors.
  72. * TODO: move to a separate file once the number grows.
  73. */
  74. bool
  75. nlp_has_priv(struct nlpcb *nlp, int priv)
  76. {
  77. return (priv_check_cred(nlp->nl_cred, priv) == 0);
  78. }
  79. struct ucred *
  80. nlp_get_cred(struct nlpcb *nlp)
  81. {
  82. return (nlp->nl_cred);
  83. }
  84. uint32_t
  85. nlp_get_pid(const struct nlpcb *nlp)
  86. {
  87. return (nlp->nl_process_id);
  88. }
  89. bool
  90. nlp_unconstrained_vnet(const struct nlpcb *nlp)
  91. {
  92. return (nlp->nl_unconstrained_vnet);
  93. }
  94. #ifndef NETLINK
  95. /* Stub implementations for the loadable functions */
  96. static bool
  97. get_stub_writer(struct nl_writer *nw)
  98. {
  99. bzero(nw, sizeof(*nw));
  100. nw->enomem = true;
  101. return (false);
  102. }
  103. static bool
  104. nlmsg_get_unicast_writer_stub(struct nl_writer *nw, int size, struct nlpcb *nlp)
  105. {
  106. return (get_stub_writer(nw));
  107. }
  108. static bool
  109. nlmsg_get_group_writer_stub(struct nl_writer *nw, int size, int protocol, int group_id)
  110. {
  111. return (get_stub_writer(nw));
  112. }
  113. static bool
  114. nlmsg_get_chain_writer_stub(struct nl_writer *nw, int size, struct mbuf **pm)
  115. {
  116. return (get_stub_writer(nw));
  117. }
  118. static bool
  119. nlmsg_flush_stub(struct nl_writer *nw __unused)
  120. {
  121. return (false);
  122. }
  123. static void
  124. nlmsg_ignore_limit_stub(struct nl_writer *nw __unused)
  125. {
  126. }
  127. static bool
  128. nlmsg_refill_buffer_stub(struct nl_writer *nw __unused, int required_len __unused)
  129. {
  130. return (false);
  131. }
  132. static bool
  133. nlmsg_add_stub(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
  134. uint16_t flags, uint32_t len)
  135. {
  136. return (false);
  137. }
  138. static bool
  139. nlmsg_end_stub(struct nl_writer *nw __unused)
  140. {
  141. return (false);
  142. }
  143. static void
  144. nlmsg_abort_stub(struct nl_writer *nw __unused)
  145. {
  146. }
  147. static bool
  148. nlmsg_end_dump_stub(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
  149. {
  150. return (false);
  151. }
  152. static int
  153. nl_modify_ifp_generic_stub(struct ifnet *ifp __unused,
  154. struct nl_parsed_link *lattrs __unused, const struct nlattr_bmask *bm __unused,
  155. struct nl_pstate *npt __unused)
  156. {
  157. return (ENOTSUP);
  158. }
  159. static void
  160. nl_store_ifp_cookie_stub(struct nl_pstate *npt __unused, struct ifnet *ifp __unused)
  161. {
  162. }
  163. static struct nlpcb *
  164. nl_get_thread_nlp_stub(struct thread *td __unused)
  165. {
  166. return (NULL);
  167. }
  168. const static struct nl_function_wrapper nl_stub = {
  169. .nlmsg_add = nlmsg_add_stub,
  170. .nlmsg_refill_buffer = nlmsg_refill_buffer_stub,
  171. .nlmsg_flush = nlmsg_flush_stub,
  172. .nlmsg_end = nlmsg_end_stub,
  173. .nlmsg_abort = nlmsg_abort_stub,
  174. .nlmsg_ignore_limit = nlmsg_ignore_limit_stub,
  175. .nlmsg_get_unicast_writer = nlmsg_get_unicast_writer_stub,
  176. .nlmsg_get_group_writer = nlmsg_get_group_writer_stub,
  177. .nlmsg_get_chain_writer = nlmsg_get_chain_writer_stub,
  178. .nlmsg_end_dump = nlmsg_end_dump_stub,
  179. .nl_modify_ifp_generic = nl_modify_ifp_generic_stub,
  180. .nl_store_ifp_cookie = nl_store_ifp_cookie_stub,
  181. .nl_get_thread_nlp = nl_get_thread_nlp_stub,
  182. };
  183. /*
  184. * If the kernel is compiled with netlink as a module,
  185. * provide a way to introduce non-stub functioms
  186. */
  187. static const struct nl_function_wrapper *_nl = &nl_stub;
  188. void
  189. nl_set_functions(const struct nl_function_wrapper *nl)
  190. {
  191. _nl = (nl != NULL) ? nl : &nl_stub;
  192. }
  193. /* Function wrappers */
  194. bool
  195. nlmsg_get_unicast_writer(struct nl_writer *nw, int size, struct nlpcb *nlp)
  196. {
  197. return (_nl->nlmsg_get_unicast_writer(nw, size, nlp));
  198. }
  199. bool
  200. nlmsg_get_group_writer(struct nl_writer *nw, int size, int protocol, int group_id)
  201. {
  202. return (_nl->nlmsg_get_group_writer(nw, size, protocol, group_id));
  203. }
  204. bool
  205. nlmsg_get_chain_writer(struct nl_writer *nw, int size, struct mbuf **pm)
  206. {
  207. return (_nl->nlmsg_get_chain_writer(nw, size, pm));
  208. }
  209. bool
  210. nlmsg_flush(struct nl_writer *nw)
  211. {
  212. return (_nl->nlmsg_flush(nw));
  213. }
  214. void nlmsg_ignore_limit(struct nl_writer *nw)
  215. {
  216. _nl->nlmsg_ignore_limit(nw);
  217. }
  218. bool
  219. nlmsg_refill_buffer(struct nl_writer *nw, int required_len)
  220. {
  221. return (_nl->nlmsg_refill_buffer(nw, required_len));
  222. }
  223. bool
  224. nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
  225. uint16_t flags, uint32_t len)
  226. {
  227. return (_nl->nlmsg_add(nw, portid, seq, type, flags, len));
  228. }
  229. bool
  230. nlmsg_end(struct nl_writer *nw)
  231. {
  232. return (_nl->nlmsg_end(nw));
  233. }
  234. void
  235. nlmsg_abort(struct nl_writer *nw)
  236. {
  237. _nl->nlmsg_abort(nw);
  238. }
  239. bool
  240. nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
  241. {
  242. return (_nl->nlmsg_end_dump(nw, error, hdr));
  243. }
  244. int
  245. nl_modify_ifp_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs,
  246. const struct nlattr_bmask *bm , struct nl_pstate *npt)
  247. {
  248. return (_nl->nl_modify_ifp_generic(ifp, lattrs, bm, npt));
  249. }
  250. void
  251. nl_store_ifp_cookie(struct nl_pstate *npt, struct ifnet *ifp)
  252. {
  253. return (_nl->nl_store_ifp_cookie(npt, ifp));
  254. }
  255. struct nlpcb *
  256. nl_get_thread_nlp(struct thread *td)
  257. {
  258. return (_nl->nl_get_thread_nlp(td));
  259. }
  260. #endif /* !NETLINK */