xref: /freebsd/sys/netlink/netlink_message_parser.c (revision c07d6445eb89d9dd3950361b065b7bd110e3a043)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include <sys/types.h>
33 #include <sys/malloc.h>
34 #include <sys/rmlock.h>
35 #include <sys/socket.h>
36 
37 #include <machine/stdarg.h>
38 
39 #include <net/if.h>
40 #include <net/route.h>
41 #include <net/route/nhop.h>
42 
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/netlink_route.h>
48 
49 #define	DEBUG_MOD_NAME	nl_parser
50 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
51 #include <netlink/netlink_debug.h>
52 _DECLARE_DEBUG(LOG_DEBUG);
53 
54 bool
55 nlmsg_report_err_msg(struct nl_pstate *npt, const char *fmt, ...)
56 {
57 	va_list ap;
58 
59 	if (npt->err_msg != NULL)
60 		return (false);
61 	char *buf = npt_alloc(npt, NL_MAX_ERROR_BUF);
62 	if (buf == NULL)
63 		return (false);
64 	va_start(ap, fmt);
65 	vsnprintf(buf, NL_MAX_ERROR_BUF, fmt, ap);
66 	va_end(ap);
67 
68 	npt->err_msg = buf;
69 	return (true);
70 }
71 
72 bool
73 nlmsg_report_err_offset(struct nl_pstate *npt, uint32_t off)
74 {
75 	if (npt->err_off != 0)
76 		return (false);
77 	npt->err_off = off;
78 	return (true);
79 }
80 
81 static const struct nlattr_parser *
82 search_states(const struct nlattr_parser *ps, int pslen, int key)
83 {
84 	int left_i = 0, right_i = pslen - 1;
85 
86 	if (key < ps[0].type || key > ps[pslen - 1].type)
87 		return (NULL);
88 
89 	while (left_i + 1 < right_i) {
90 		int mid_i = (left_i + right_i) / 2;
91 		if (key < ps[mid_i].type)
92 			right_i = mid_i;
93 		else if (key > ps[mid_i].type)
94 			left_i = mid_i + 1;
95 		else
96 			return (&ps[mid_i]);
97 	}
98 	if (ps[left_i].type == key)
99 		return (&ps[left_i]);
100 	else if (ps[right_i].type == key)
101 		return (&ps[right_i]);
102 	return (NULL);
103 }
104 
105 int
106 nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser *ps, int pslen,
107     struct nl_pstate *npt, void *target)
108 {
109 	struct nlattr *nla = NULL;
110 	int error = 0;
111 
112 	NL_LOG(LOG_DEBUG3, "parse %p remaining_len %d", nla_head, len);
113 	int orig_len = len;
114 	NLA_FOREACH(nla, nla_head, len) {
115 		NL_LOG(LOG_DEBUG3, ">> parsing %p attr_type %d len %d (rem %d)", nla, nla->nla_type, nla->nla_len, len);
116 		if (nla->nla_len < sizeof(struct nlattr)) {
117 			NLMSG_REPORT_ERR_MSG(npt, "Invalid attr %p type %d len: %d",
118 			    nla, nla->nla_type, nla->nla_len);
119 			uint32_t off = (char *)nla - (char *)npt->hdr;
120 			nlmsg_report_err_offset(npt, off);
121 			return (EINVAL);
122 		}
123 
124 		int nla_type = nla->nla_type & NLA_TYPE_MASK;
125 		const struct nlattr_parser *s = search_states(ps, pslen, nla_type);
126 		if (s != NULL) {
127 			void *ptr = (void *)((char *)target + s->off);
128 			error = s->cb(nla, npt, s->arg, ptr);
129 			if (error != 0) {
130 				uint32_t off = (char *)nla - (char *)npt->hdr;
131 				nlmsg_report_err_offset(npt, off);
132 				NL_LOG(LOG_DEBUG3, "parse failed att offset %u", off);
133 				return (error);
134 			}
135 		} else {
136 			/* Ignore non-specified attributes */
137 			NL_LOG(LOG_DEBUG3, "ignoring attr %d", nla->nla_type);
138 		}
139 	}
140 	if (len >= sizeof(struct nlattr)) {
141 		nla = (struct nlattr *)((char *)nla_head + (orig_len - len));
142 		NL_LOG(LOG_DEBUG3, " >>> end %p attr_type %d len %d", nla,
143 		    nla->nla_type, nla->nla_len);
144 	}
145 	NL_LOG(LOG_DEBUG3, "end parse: %p remaining_len %d", nla, len);
146 
147 	return (0);
148 }
149 
150 void
151 nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len, struct nlattr_bmask *bm)
152 {
153 	struct nlattr *nla = NULL;
154 
155 	bzero(bm->mask, sizeof(bm->mask));
156 
157 	NLA_FOREACH(nla, nla_head, len) {
158 		if (nla->nla_len < sizeof(struct nlattr))
159 			return;
160 		int nla_type = nla->nla_type & NLA_TYPE_MASK;
161 		if (nla_type <= sizeof(bm->mask) * 8)
162 			bm->mask[nla_type / 8] |= 1 << (nla_type % 8);
163 	}
164 }
165 
166 
167 int
168 nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
169 {
170 	if (__predict_false(NLA_DATA_LEN(nla) != 0)) {
171 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not a flag",
172 		    nla->nla_type, NLA_DATA_LEN(nla));
173 		return (EINVAL);
174 	}
175 
176 	*((uint8_t *)target) = 1;
177 	return (0);
178 }
179 
180 static struct sockaddr *
181 parse_rta_ip4(void *rta_data, struct nl_pstate *npt, int *perror)
182 {
183 	struct sockaddr_in *sin;
184 
185 	sin = (struct sockaddr_in *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in));
186 	if (__predict_false(sin == NULL)) {
187 		*perror = ENOBUFS;
188 		return (NULL);
189 	}
190 	sin->sin_len = sizeof(struct sockaddr_in);
191 	sin->sin_family = AF_INET;
192 	memcpy(&sin->sin_addr, rta_data, sizeof(struct in_addr));
193 	return ((struct sockaddr *)sin);
194 }
195 
196 static struct sockaddr *
197 parse_rta_ip6(void *rta_data, struct nl_pstate *npt, int *perror)
198 {
199 	struct sockaddr_in6 *sin6;
200 
201 	sin6 = (struct sockaddr_in6 *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in6));
202 	if (__predict_false(sin6 == NULL)) {
203 		*perror = ENOBUFS;
204 		return (NULL);
205 	}
206 	sin6->sin6_len = sizeof(struct sockaddr_in6);
207 	sin6->sin6_family = AF_INET6;
208 	memcpy(&sin6->sin6_addr, rta_data, sizeof(struct in6_addr));
209 	return ((struct sockaddr *)sin6);
210 }
211 
212 static struct sockaddr *
213 parse_rta_ip(struct rtattr *rta, struct nl_pstate *npt, int *perror)
214 {
215 	void *rta_data = NL_RTA_DATA(rta);
216 	int rta_len = NL_RTA_DATA_LEN(rta);
217 
218 	if (rta_len == sizeof(struct in_addr)) {
219 		return (parse_rta_ip4(rta_data, npt, perror));
220 	} else if (rta_len == sizeof(struct in6_addr)) {
221 		return (parse_rta_ip6(rta_data, npt, perror));
222 	} else {
223 		NLMSG_REPORT_ERR_MSG(npt, "unknown IP len: %d for rta type %d",
224 		    rta_len, rta->rta_type);
225 		*perror = ENOTSUP;
226 		return (NULL);
227 	}
228 	return (NULL);
229 }
230 
231 int
232 nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
233 {
234 	int error = 0;
235 
236 	struct sockaddr *sa = parse_rta_ip((struct rtattr *)nla, npt, &error);
237 
238 	*((struct sockaddr **)target) = sa;
239 	return (error);
240 }
241 
242 static struct sockaddr *
243 parse_rta_via(struct rtattr *rta, struct nl_pstate *npt, int *perror)
244 {
245 	struct rtvia *via = NL_RTA_DATA(rta);
246 	int data_len = NL_RTA_DATA_LEN(rta);
247 
248 	if (__predict_false(data_len) < sizeof(struct rtvia)) {
249 		NLMSG_REPORT_ERR_MSG(npt, "undersized RTA_VIA(%d) attr: len %d",
250 		    rta->rta_type, data_len);
251 		*perror = EINVAL;
252 		return (NULL);
253 	}
254 	data_len -= offsetof(struct rtvia, rtvia_addr);
255 
256 	switch (via->rtvia_family) {
257 	case AF_INET:
258 		if (__predict_false(data_len < sizeof(struct in_addr))) {
259 			*perror = EINVAL;
260 			return (NULL);
261 		}
262 		return (parse_rta_ip4(via->rtvia_addr, npt, perror));
263 	case AF_INET6:
264 		if (__predict_false(data_len < sizeof(struct in6_addr))) {
265 			*perror = EINVAL;
266 			return (NULL);
267 		}
268 		return (parse_rta_ip6(via->rtvia_addr, npt, perror));
269 	default:
270 		*perror = ENOTSUP;
271 		return (NULL);
272 	}
273 }
274 
275 int
276 nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
277 {
278 	int error = 0;
279 
280 	struct sockaddr *sa = parse_rta_via((struct rtattr *)nla, npt, &error);
281 
282 	*((struct sockaddr **)target) = sa;
283 	return (error);
284 }
285 
286 
287 int
288 nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
289 {
290 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint16_t))) {
291 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
292 		    nla->nla_type, NLA_DATA_LEN(nla));
293 		return (EINVAL);
294 	}
295 	*((uint16_t *)target) = *((const uint16_t *)NL_RTA_DATA_CONST(nla));
296 	return (0);
297 }
298 
299 int
300 nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
301 {
302 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
303 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
304 		    nla->nla_type, NLA_DATA_LEN(nla));
305 		return (EINVAL);
306 	}
307 	*((uint32_t *)target) = *((const uint32_t *)NL_RTA_DATA_CONST(nla));
308 	return (0);
309 }
310 
311 int
312 nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
313 {
314 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint64_t))) {
315 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint64",
316 		    nla->nla_type, NLA_DATA_LEN(nla));
317 		return (EINVAL);
318 	}
319 	memcpy(target, NL_RTA_DATA_CONST(nla), sizeof(uint64_t));
320 	return (0);
321 }
322 
323 static int
324 nlattr_get_ifp_internal(struct nlattr *nla, struct nl_pstate *npt,
325     void *target, bool zero_ok)
326 {
327 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
328 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
329 		    nla->nla_type, NLA_DATA_LEN(nla));
330 		return (EINVAL);
331 	}
332 	uint32_t ifindex = *((const uint32_t *)NLA_DATA_CONST(nla));
333 
334 	if (ifindex == 0 && zero_ok) {
335 		*((struct ifnet **)target) = NULL;
336 		return (0);
337 	}
338 
339 	NET_EPOCH_ASSERT();
340 
341 	struct ifnet *ifp = ifnet_byindex(ifindex);
342 	if (__predict_false(ifp == NULL)) {
343 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d: ifindex %u invalid",
344 		    nla->nla_type, ifindex);
345 		return (ENOENT);
346 	}
347 	*((struct ifnet **)target) = ifp;
348 	NL_LOG(LOG_DEBUG3, "nla type %d: ifindex %u -> %s", nla->nla_type,
349 	    ifindex, if_name(ifp));
350 
351 	return (0);
352 }
353 
354 int
355 nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
356 {
357 	return (nlattr_get_ifp_internal(nla, npt, target, false));
358 }
359 
360 int
361 nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
362 {
363 	return (nlattr_get_ifp_internal(nla, npt, target, true));
364 }
365 
366 int
367 nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
368 {
369 	int maxlen = NLA_DATA_LEN(nla);
370 
371 	if (__predict_false(strnlen((char *)NLA_DATA(nla), maxlen) >= maxlen)) {
372 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not NULL-terminated",
373 		    nla->nla_type, maxlen);
374 		return (EINVAL);
375 	}
376 
377 	*((char **)target) = (char *)NLA_DATA(nla);
378 	return (0);
379 }
380 
381 int
382 nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
383 {
384 	int maxlen = NLA_DATA_LEN(nla);
385 
386 	char *buf = npt_alloc(npt, maxlen + 1);
387 	if (buf == NULL)
388 		return (ENOMEM);
389 	buf[maxlen] = '\0';
390 	memcpy(buf, NLA_DATA(nla), maxlen);
391 
392 	*((char **)target) = buf;
393 	return (0);
394 }
395 int
396 nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
397 {
398 	NL_LOG(LOG_DEBUG3, "STORING %p len %d", nla, nla->nla_len);
399 	*((struct nlattr **)target) = nla;
400 	return (0);
401 }
402 
403 int
404 nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
405 {
406 	const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg;
407 	int error;
408 
409 	/* Assumes target points to the beginning of the structure */
410 	error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt, target);
411 	return (error);
412 }
413 
414 int
415 nlf_get_ifp(void *src, struct nl_pstate *npt, void *target)
416 {
417 	int ifindex = *((const int *)src);
418 
419 	NET_EPOCH_ASSERT();
420 
421 	struct ifnet *ifp = ifnet_byindex(ifindex);
422 	if (ifp == NULL) {
423 		NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
424 		return (ENOENT);
425 	}
426 	*((struct ifnet **)target) = ifp;
427 
428 	return (0);
429 }
430 
431 int
432 nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target)
433 {
434 	int ifindex = *((const int *)src);
435 
436 	NET_EPOCH_ASSERT();
437 
438 	struct ifnet *ifp = ifnet_byindex(ifindex);
439 	if (ifindex != 0 && ifp == NULL) {
440 		NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
441 		return (ENOENT);
442 	}
443 	*((struct ifnet **)target) = ifp;
444 
445 	return (0);
446 }
447 
448 int
449 nlf_get_u8(void *src, struct nl_pstate *npt, void *target)
450 {
451 	uint8_t val = *((const uint8_t *)src);
452 
453 	*((uint8_t *)target) = val;
454 
455 	return (0);
456 }
457 
458 int
459 nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target)
460 {
461 	*((uint32_t *)target) = *((const uint8_t *)src);
462 	return (0);
463 }
464 
465 int
466 nlf_get_u16(void *src, struct nl_pstate *npt, void *target)
467 {
468 	*((uint16_t *)target) = *((const uint16_t *)src);
469 	return (0);
470 }
471 
472 int
473 nlf_get_u32(void *src, struct nl_pstate *npt, void *target)
474 {
475 	*((uint32_t *)target) = *((const uint32_t *)src);
476 	return (0);
477 }
478 
479