2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2023 Alexander V. Chernikov <melifaro@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
32 #include <sys/rmlock.h>
33 #include <sys/domain.h>
35 #include <sys/protosw.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/sysent.h>
41 #include <sys/syslog.h>
42 #include <sys/priv.h> /* priv_check */
44 #include <net/route.h>
45 #include <net/route/route_ctl.h>
47 #include <netlink/netlink.h>
48 #include <netlink/netlink_ctl.h>
49 #include <netlink/netlink_var.h>
50 #include <netlink/route/route_var.h>
52 /* Standard bits: built-in the kernel */
53 SYSCTL_NODE(_net, OID_AUTO, netlink, CTLFLAG_RD, 0,
54 "RFC3549 Netlink network state socket family");
55 SYSCTL_NODE(_net_netlink, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
56 "Netlink per-subsystem debug levels");
58 MALLOC_DEFINE(M_NETLINK, "netlink", "Memory used for netlink packets");
60 /* Netlink-related callbacks needed to glue rtsock, netlink and linuxolator */
62 ignore_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
67 ignore_ifmsg_event(struct ifnet *ifp, int if_flags_mask)
71 static struct rtbridge ignore_cb = {
72 .route_f = ignore_route_event,
73 .ifmsg_f = ignore_ifmsg_event,
76 void *linux_netlink_p = NULL; /* Callback pointer for Linux translator functions */
77 struct rtbridge *rtsock_callback_p = &ignore_cb;
78 struct rtbridge *netlink_callback_p = &ignore_cb;
83 * TODO: move to a separate file once the number grows.
86 nlp_has_priv(struct nlpcb *nlp, int priv)
88 return (priv_check_cred(nlp->nl_cred, priv) == 0);
92 nlp_get_cred(struct nlpcb *nlp)
94 return (nlp->nl_cred);
98 nlp_get_pid(const struct nlpcb *nlp)
100 return (nlp->nl_process_id);
104 nlp_unconstrained_vnet(const struct nlpcb *nlp)
106 return (nlp->nl_unconstrained_vnet);
110 /* Stub implementations for the loadable functions */
113 get_stub_writer(struct nl_writer *nw)
115 bzero(nw, sizeof(*nw));
122 nlmsg_get_unicast_writer_stub(struct nl_writer *nw, int size, struct nlpcb *nlp)
124 return (get_stub_writer(nw));
128 nlmsg_get_group_writer_stub(struct nl_writer *nw, int size, int protocol, int group_id)
130 return (get_stub_writer(nw));
134 nlmsg_get_chain_writer_stub(struct nl_writer *nw, int size, struct mbuf **pm)
136 return (get_stub_writer(nw));
140 nlmsg_flush_stub(struct nl_writer *nw __unused)
146 nlmsg_ignore_limit_stub(struct nl_writer *nw __unused)
151 nlmsg_refill_buffer_stub(struct nl_writer *nw __unused, int required_len __unused)
157 nlmsg_add_stub(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
158 uint16_t flags, uint32_t len)
164 nlmsg_end_stub(struct nl_writer *nw __unused)
170 nlmsg_abort_stub(struct nl_writer *nw __unused)
175 nlmsg_end_dump_stub(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
181 nl_modify_ifp_generic_stub(struct ifnet *ifp __unused,
182 struct nl_parsed_link *lattrs __unused, const struct nlattr_bmask *bm __unused,
183 struct nl_pstate *npt __unused)
189 nl_store_ifp_cookie_stub(struct nl_pstate *npt __unused, struct ifnet *ifp __unused)
193 static struct nlpcb *
194 nl_get_thread_nlp_stub(struct thread *td __unused)
199 const static struct nl_function_wrapper nl_stub = {
200 .nlmsg_add = nlmsg_add_stub,
201 .nlmsg_refill_buffer = nlmsg_refill_buffer_stub,
202 .nlmsg_flush = nlmsg_flush_stub,
203 .nlmsg_end = nlmsg_end_stub,
204 .nlmsg_abort = nlmsg_abort_stub,
205 .nlmsg_ignore_limit = nlmsg_ignore_limit_stub,
206 .nlmsg_get_unicast_writer = nlmsg_get_unicast_writer_stub,
207 .nlmsg_get_group_writer = nlmsg_get_group_writer_stub,
208 .nlmsg_get_chain_writer = nlmsg_get_chain_writer_stub,
209 .nlmsg_end_dump = nlmsg_end_dump_stub,
210 .nl_modify_ifp_generic = nl_modify_ifp_generic_stub,
211 .nl_store_ifp_cookie = nl_store_ifp_cookie_stub,
212 .nl_get_thread_nlp = nl_get_thread_nlp_stub,
216 * If the kernel is compiled with netlink as a module,
217 * provide a way to introduce non-stub functioms
219 static const struct nl_function_wrapper *_nl = &nl_stub;
222 nl_set_functions(const struct nl_function_wrapper *nl)
224 _nl = (nl != NULL) ? nl : &nl_stub;
227 /* Function wrappers */
229 nlmsg_get_unicast_writer(struct nl_writer *nw, int size, struct nlpcb *nlp)
231 return (_nl->nlmsg_get_unicast_writer(nw, size, nlp));
235 nlmsg_get_group_writer(struct nl_writer *nw, int size, int protocol, int group_id)
237 return (_nl->nlmsg_get_group_writer(nw, size, protocol, group_id));
241 nlmsg_get_chain_writer(struct nl_writer *nw, int size, struct mbuf **pm)
243 return (_nl->nlmsg_get_chain_writer(nw, size, pm));
247 nlmsg_flush(struct nl_writer *nw)
249 return (_nl->nlmsg_flush(nw));
252 void nlmsg_ignore_limit(struct nl_writer *nw)
254 _nl->nlmsg_ignore_limit(nw);
258 nlmsg_refill_buffer(struct nl_writer *nw, int required_len)
260 return (_nl->nlmsg_refill_buffer(nw, required_len));
264 nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
265 uint16_t flags, uint32_t len)
267 return (_nl->nlmsg_add(nw, portid, seq, type, flags, len));
271 nlmsg_end(struct nl_writer *nw)
273 return (_nl->nlmsg_end(nw));
277 nlmsg_abort(struct nl_writer *nw)
279 _nl->nlmsg_abort(nw);
283 nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
285 return (_nl->nlmsg_end_dump(nw, error, hdr));
289 nl_modify_ifp_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs,
290 const struct nlattr_bmask *bm , struct nl_pstate *npt)
292 return (_nl->nl_modify_ifp_generic(ifp, lattrs, bm, npt));
296 nl_store_ifp_cookie(struct nl_pstate *npt, struct ifnet *ifp)
298 return (_nl->nl_store_ifp_cookie(npt, ifp));
302 nl_get_thread_nlp(struct thread *td)
304 return (_nl->nl_get_thread_nlp(td));
307 #endif /* !NETLINK */