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