xref: /freebsd/sys/netlink/netlink_glue.c (revision f1c6edba885fa53cc87f7252ab09d36ec1984697)
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 
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
31 #include <sys/lock.h>
32 #include <sys/rmlock.h>
33 #include <sys/domain.h>
34 #include <sys/mbuf.h>
35 #include <sys/protosw.h>
36 #include <sys/proc.h>
37 #include <sys/ck.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 */
43 
44 #include <net/route.h>
45 #include <net/route/route_ctl.h>
46 
47 #include <netlink/netlink.h>
48 #include <netlink/netlink_ctl.h>
49 #include <netlink/netlink_var.h>
50 #include <netlink/route/route_var.h>
51 
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");
57 
58 MALLOC_DEFINE(M_NETLINK, "netlink", "Memory used for netlink packets");
59 
60 /* Netlink-related callbacks needed to glue rtsock, netlink and linuxolator */
61 static void
ignore_route_event(uint32_t fibnum,const struct rib_cmd_info * rc)62 ignore_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
63 {
64 }
65 
66 static void
ignore_ifmsg_event(struct ifnet * ifp,int if_flags_mask)67 ignore_ifmsg_event(struct ifnet *ifp, int if_flags_mask)
68 {
69 }
70 
71 static struct rtbridge ignore_cb = {
72 	.route_f = ignore_route_event,
73 	.ifmsg_f = ignore_ifmsg_event,
74 };
75 
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;
79 
80 
81 /*
82  * nlp accessors.
83  * TODO: move to a separate file once the number grows.
84  */
85 bool
nlp_has_priv(struct nlpcb * nlp,int priv)86 nlp_has_priv(struct nlpcb *nlp, int priv)
87 {
88 	return (priv_check_cred(nlp->nl_cred, priv) == 0);
89 }
90 
91 struct ucred *
nlp_get_cred(struct nlpcb * nlp)92 nlp_get_cred(struct nlpcb *nlp)
93 {
94 	return (nlp->nl_cred);
95 }
96 
97 uint32_t
nlp_get_pid(const struct nlpcb * nlp)98 nlp_get_pid(const struct nlpcb *nlp)
99 {
100 	return (nlp->nl_process_id);
101 }
102 
103 bool
nlp_unconstrained_vnet(const struct nlpcb * nlp)104 nlp_unconstrained_vnet(const struct nlpcb *nlp)
105 {
106 	return (nlp->nl_unconstrained_vnet);
107 }
108 
109 #ifndef NETLINK
110 /* Stub implementations for the loadable functions */
111 
112 static bool
nl_writer_unicast_stub(struct nl_writer * nw,size_t size,struct nlpcb * nlp,bool waitok)113 nl_writer_unicast_stub(struct nl_writer *nw, size_t size, struct nlpcb *nlp,
114     bool waitok)
115 {
116 	return (get_stub_writer(nw));
117 }
118 
119 static bool
nl_writer_group_stub(struct nl_writer * nw,size_t size,uint16_t protocol,uint16_t group_id,bool waitok)120 nl_writer_group_stub(struct nl_writer *nw, size_t size, uint16_t protocol,
121     uint16_t group_id, bool waitok)
122 {
123 	return (get_stub_writer(nw));
124 }
125 
126 static bool
nlmsg_flush_stub(struct nl_writer * nw __unused)127 nlmsg_flush_stub(struct nl_writer *nw __unused)
128 {
129 	return (false);
130 }
131 
132 static void
nlmsg_ignore_limit_stub(struct nl_writer * nw __unused)133 nlmsg_ignore_limit_stub(struct nl_writer *nw __unused)
134 {
135 }
136 
137 static bool
nlmsg_refill_buffer_stub(struct nl_writer * nw __unused,size_t required_len __unused)138 nlmsg_refill_buffer_stub(struct nl_writer *nw __unused,
139     size_t required_len __unused)
140 {
141 	return (false);
142 }
143 
144 static bool
nlmsg_add_stub(struct nl_writer * nw,uint32_t portid,uint32_t seq,uint16_t type,uint16_t flags,uint32_t len)145 nlmsg_add_stub(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
146     uint16_t flags, uint32_t len)
147 {
148 	return (false);
149 }
150 
151 static bool
nlmsg_end_stub(struct nl_writer * nw __unused)152 nlmsg_end_stub(struct nl_writer *nw __unused)
153 {
154 	return (false);
155 }
156 
157 static void
nlmsg_abort_stub(struct nl_writer * nw __unused)158 nlmsg_abort_stub(struct nl_writer *nw __unused)
159 {
160 }
161 
162 static bool
nlmsg_end_dump_stub(struct nl_writer * nw,int error,struct nlmsghdr * hdr)163 nlmsg_end_dump_stub(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
164 {
165 	return (false);
166 }
167 
168 static int
nl_modify_ifp_generic_stub(struct ifnet * ifp __unused,struct nl_parsed_link * lattrs __unused,const struct nlattr_bmask * bm __unused,struct nl_pstate * npt __unused)169 nl_modify_ifp_generic_stub(struct ifnet *ifp __unused,
170     struct nl_parsed_link *lattrs __unused, const struct nlattr_bmask *bm __unused,
171     struct nl_pstate *npt __unused)
172 {
173 	return (ENOTSUP);
174 }
175 
176 static void
nl_store_ifp_cookie_stub(struct nl_pstate * npt __unused,struct ifnet * ifp __unused)177 nl_store_ifp_cookie_stub(struct nl_pstate *npt __unused, struct ifnet *ifp __unused)
178 {
179 }
180 
181 static struct nlpcb *
nl_get_thread_nlp_stub(struct thread * td __unused)182 nl_get_thread_nlp_stub(struct thread *td __unused)
183 {
184 	return (NULL);
185 }
186 
187 const static struct nl_function_wrapper nl_stub = {
188 	.nlmsg_add = nlmsg_add_stub,
189 	.nlmsg_refill_buffer = nlmsg_refill_buffer_stub,
190 	.nlmsg_flush = nlmsg_flush_stub,
191 	.nlmsg_end = nlmsg_end_stub,
192 	.nlmsg_abort = nlmsg_abort_stub,
193 	.nlmsg_ignore_limit = nlmsg_ignore_limit_stub,
194 	.nl_writer_unicast = nl_writer_unicast_stub,
195 	.nl_writer_group = nl_writer_group_stub,
196 	.nlmsg_end_dump = nlmsg_end_dump_stub,
197 	.nl_modify_ifp_generic = nl_modify_ifp_generic_stub,
198 	.nl_store_ifp_cookie = nl_store_ifp_cookie_stub,
199 	.nl_get_thread_nlp = nl_get_thread_nlp_stub,
200 };
201 
202 /*
203  * If the kernel is compiled with netlink as a module,
204  *  provide a way to introduce non-stub functioms
205  */
206 static const struct nl_function_wrapper *_nl = &nl_stub;
207 
208 void
nl_set_functions(const struct nl_function_wrapper * nl)209 nl_set_functions(const struct nl_function_wrapper *nl)
210 {
211 	_nl = (nl != NULL) ? nl : &nl_stub;
212 }
213 
214 /* Function wrappers */
215 bool
nl_writer_unicast(struct nl_writer * nw,size_t size,struct nlpcb * nlp,bool waitok)216 nl_writer_unicast(struct nl_writer *nw, size_t size, struct nlpcb *nlp,
217     bool waitok)
218 {
219 	return (_nl->nl_writer_unicast(nw, size, nlp, waitok));
220 }
221 
222 bool
nl_writer_group(struct nl_writer * nw,size_t size,uint16_t protocol,uint16_t group_id,bool waitok)223 nl_writer_group(struct nl_writer *nw, size_t size, uint16_t protocol,
224     uint16_t group_id, bool waitok)
225 {
226 	return (_nl->nl_writer_group(nw, size, protocol, group_id, waitok));
227 }
228 
229 bool
nlmsg_flush(struct nl_writer * nw)230 nlmsg_flush(struct nl_writer *nw)
231 {
232 	return (_nl->nlmsg_flush(nw));
233 }
234 
nlmsg_ignore_limit(struct nl_writer * nw)235 void nlmsg_ignore_limit(struct nl_writer *nw)
236 {
237 	_nl->nlmsg_ignore_limit(nw);
238 }
239 
240 bool
nlmsg_refill_buffer(struct nl_writer * nw,size_t required_len)241 nlmsg_refill_buffer(struct nl_writer *nw, size_t required_len)
242 {
243 	return (_nl->nlmsg_refill_buffer(nw, required_len));
244 }
245 
246 bool
nlmsg_add(struct nl_writer * nw,uint32_t portid,uint32_t seq,uint16_t type,uint16_t flags,uint32_t len)247 nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
248     uint16_t flags, uint32_t len)
249 {
250 	return (_nl->nlmsg_add(nw, portid, seq, type, flags, len));
251 }
252 
253 bool
nlmsg_end(struct nl_writer * nw)254 nlmsg_end(struct nl_writer *nw)
255 {
256 	return (_nl->nlmsg_end(nw));
257 }
258 
259 void
nlmsg_abort(struct nl_writer * nw)260 nlmsg_abort(struct nl_writer *nw)
261 {
262 	_nl->nlmsg_abort(nw);
263 }
264 
265 bool
nlmsg_end_dump(struct nl_writer * nw,int error,struct nlmsghdr * hdr)266 nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
267 {
268 	return (_nl->nlmsg_end_dump(nw, error, hdr));
269 }
270 
271 int
nl_modify_ifp_generic(struct ifnet * ifp,struct nl_parsed_link * lattrs,const struct nlattr_bmask * bm,struct nl_pstate * npt)272 nl_modify_ifp_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs,
273     const struct nlattr_bmask *bm , struct nl_pstate *npt)
274 {
275 	return (_nl->nl_modify_ifp_generic(ifp, lattrs, bm, npt));
276 }
277 
278 void
nl_store_ifp_cookie(struct nl_pstate * npt,struct ifnet * ifp)279 nl_store_ifp_cookie(struct nl_pstate *npt, struct ifnet *ifp)
280 {
281 	return (_nl->nl_store_ifp_cookie(npt, ifp));
282 }
283 
284 struct nlpcb *
nl_get_thread_nlp(struct thread * td)285 nl_get_thread_nlp(struct thread *td)
286 {
287 	return (_nl->nl_get_thread_nlp(td));
288 }
289 
290 #endif /* !NETLINK */
291 
292