xref: /freebsd/sys/netlink/netlink_message_parser.c (revision d8096b2df282d7a50e56eddba523bcdda1676106)
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 	BIT_ZERO(NL_ATTR_BMASK_SIZE, bm);
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 < NL_ATTR_BMASK_SIZE)
162 			BIT_SET(NL_ATTR_BMASK_SIZE, nla_type, bm);
163 		else
164 			NL_LOG(LOG_DEBUG2, "Skipping type %d in the mask: too short",
165 			    nla_type);
166 	}
167 }
168 
169 bool
170 nl_has_attr(const struct nlattr_bmask *bm, unsigned int nla_type)
171 {
172 	MPASS(nla_type < NL_ATTR_BMASK_SIZE);
173 
174 	return (BIT_ISSET(NL_ATTR_BMASK_SIZE, nla_type, bm));
175 }
176 
177 int
178 nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
179 {
180 	if (__predict_false(NLA_DATA_LEN(nla) != 0)) {
181 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not a flag",
182 		    nla->nla_type, NLA_DATA_LEN(nla));
183 		return (EINVAL);
184 	}
185 
186 	*((uint8_t *)target) = 1;
187 	return (0);
188 }
189 
190 static struct sockaddr *
191 parse_rta_ip4(void *rta_data, struct nl_pstate *npt, int *perror)
192 {
193 	struct sockaddr_in *sin;
194 
195 	sin = (struct sockaddr_in *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in));
196 	if (__predict_false(sin == NULL)) {
197 		*perror = ENOBUFS;
198 		return (NULL);
199 	}
200 	sin->sin_len = sizeof(struct sockaddr_in);
201 	sin->sin_family = AF_INET;
202 	memcpy(&sin->sin_addr, rta_data, sizeof(struct in_addr));
203 	return ((struct sockaddr *)sin);
204 }
205 
206 static struct sockaddr *
207 parse_rta_ip6(void *rta_data, struct nl_pstate *npt, int *perror)
208 {
209 	struct sockaddr_in6 *sin6;
210 
211 	sin6 = (struct sockaddr_in6 *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in6));
212 	if (__predict_false(sin6 == NULL)) {
213 		*perror = ENOBUFS;
214 		return (NULL);
215 	}
216 	sin6->sin6_len = sizeof(struct sockaddr_in6);
217 	sin6->sin6_family = AF_INET6;
218 	memcpy(&sin6->sin6_addr, rta_data, sizeof(struct in6_addr));
219 	return ((struct sockaddr *)sin6);
220 }
221 
222 static struct sockaddr *
223 parse_rta_ip(struct rtattr *rta, struct nl_pstate *npt, int *perror)
224 {
225 	void *rta_data = NL_RTA_DATA(rta);
226 	int rta_len = NL_RTA_DATA_LEN(rta);
227 
228 	if (rta_len == sizeof(struct in_addr)) {
229 		return (parse_rta_ip4(rta_data, npt, perror));
230 	} else if (rta_len == sizeof(struct in6_addr)) {
231 		return (parse_rta_ip6(rta_data, npt, perror));
232 	} else {
233 		NLMSG_REPORT_ERR_MSG(npt, "unknown IP len: %d for rta type %d",
234 		    rta_len, rta->rta_type);
235 		*perror = ENOTSUP;
236 		return (NULL);
237 	}
238 	return (NULL);
239 }
240 
241 int
242 nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
243 {
244 	int error = 0;
245 
246 	struct sockaddr *sa = parse_rta_ip((struct rtattr *)nla, npt, &error);
247 
248 	*((struct sockaddr **)target) = sa;
249 	return (error);
250 }
251 
252 static struct sockaddr *
253 parse_rta_via(struct rtattr *rta, struct nl_pstate *npt, int *perror)
254 {
255 	struct rtvia *via = NL_RTA_DATA(rta);
256 	int data_len = NL_RTA_DATA_LEN(rta);
257 
258 	if (__predict_false(data_len) < sizeof(struct rtvia)) {
259 		NLMSG_REPORT_ERR_MSG(npt, "undersized RTA_VIA(%d) attr: len %d",
260 		    rta->rta_type, data_len);
261 		*perror = EINVAL;
262 		return (NULL);
263 	}
264 	data_len -= offsetof(struct rtvia, rtvia_addr);
265 
266 	switch (via->rtvia_family) {
267 	case AF_INET:
268 		if (__predict_false(data_len < sizeof(struct in_addr))) {
269 			*perror = EINVAL;
270 			return (NULL);
271 		}
272 		return (parse_rta_ip4(via->rtvia_addr, npt, perror));
273 	case AF_INET6:
274 		if (__predict_false(data_len < sizeof(struct in6_addr))) {
275 			*perror = EINVAL;
276 			return (NULL);
277 		}
278 		return (parse_rta_ip6(via->rtvia_addr, npt, perror));
279 	default:
280 		*perror = ENOTSUP;
281 		return (NULL);
282 	}
283 }
284 
285 int
286 nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
287 {
288 	int error = 0;
289 
290 	struct sockaddr *sa = parse_rta_via((struct rtattr *)nla, npt, &error);
291 
292 	*((struct sockaddr **)target) = sa;
293 	return (error);
294 }
295 
296 
297 int
298 nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
299 {
300 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint16_t))) {
301 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
302 		    nla->nla_type, NLA_DATA_LEN(nla));
303 		return (EINVAL);
304 	}
305 	*((uint16_t *)target) = *((const uint16_t *)NL_RTA_DATA_CONST(nla));
306 	return (0);
307 }
308 
309 int
310 nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
311 {
312 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
313 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
314 		    nla->nla_type, NLA_DATA_LEN(nla));
315 		return (EINVAL);
316 	}
317 	*((uint32_t *)target) = *((const uint32_t *)NL_RTA_DATA_CONST(nla));
318 	return (0);
319 }
320 
321 int
322 nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
323 {
324 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint64_t))) {
325 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint64",
326 		    nla->nla_type, NLA_DATA_LEN(nla));
327 		return (EINVAL);
328 	}
329 	memcpy(target, NL_RTA_DATA_CONST(nla), sizeof(uint64_t));
330 	return (0);
331 }
332 
333 static int
334 nlattr_get_ifp_internal(struct nlattr *nla, struct nl_pstate *npt,
335     void *target, bool zero_ok)
336 {
337 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
338 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
339 		    nla->nla_type, NLA_DATA_LEN(nla));
340 		return (EINVAL);
341 	}
342 	uint32_t ifindex = *((const uint32_t *)NLA_DATA_CONST(nla));
343 
344 	if (ifindex == 0 && zero_ok) {
345 		*((struct ifnet **)target) = NULL;
346 		return (0);
347 	}
348 
349 	NET_EPOCH_ASSERT();
350 
351 	struct ifnet *ifp = ifnet_byindex(ifindex);
352 	if (__predict_false(ifp == NULL)) {
353 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d: ifindex %u invalid",
354 		    nla->nla_type, ifindex);
355 		return (ENOENT);
356 	}
357 	*((struct ifnet **)target) = ifp;
358 	NL_LOG(LOG_DEBUG3, "nla type %d: ifindex %u -> %s", nla->nla_type,
359 	    ifindex, if_name(ifp));
360 
361 	return (0);
362 }
363 
364 int
365 nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
366 {
367 	return (nlattr_get_ifp_internal(nla, npt, target, false));
368 }
369 
370 int
371 nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
372 {
373 	return (nlattr_get_ifp_internal(nla, npt, target, true));
374 }
375 
376 int
377 nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
378 {
379 	int maxlen = NLA_DATA_LEN(nla);
380 
381 	if (__predict_false(strnlen((char *)NLA_DATA(nla), maxlen) >= maxlen)) {
382 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not NULL-terminated",
383 		    nla->nla_type, maxlen);
384 		return (EINVAL);
385 	}
386 
387 	*((char **)target) = (char *)NLA_DATA(nla);
388 	return (0);
389 }
390 
391 int
392 nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
393 {
394 	int maxlen = NLA_DATA_LEN(nla);
395 
396 	char *buf = npt_alloc(npt, maxlen + 1);
397 	if (buf == NULL)
398 		return (ENOMEM);
399 	buf[maxlen] = '\0';
400 	memcpy(buf, NLA_DATA(nla), maxlen);
401 
402 	*((char **)target) = buf;
403 	return (0);
404 }
405 int
406 nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
407 {
408 	NL_LOG(LOG_DEBUG3, "STORING %p len %d", nla, nla->nla_len);
409 	*((struct nlattr **)target) = nla;
410 	return (0);
411 }
412 
413 int
414 nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
415 {
416 	const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg;
417 	int error;
418 
419 	/* Assumes target points to the beginning of the structure */
420 	error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt, target);
421 	return (error);
422 }
423 
424 int
425 nlf_get_ifp(void *src, struct nl_pstate *npt, void *target)
426 {
427 	int ifindex = *((const int *)src);
428 
429 	NET_EPOCH_ASSERT();
430 
431 	struct ifnet *ifp = ifnet_byindex(ifindex);
432 	if (ifp == NULL) {
433 		NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
434 		return (ENOENT);
435 	}
436 	*((struct ifnet **)target) = ifp;
437 
438 	return (0);
439 }
440 
441 int
442 nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target)
443 {
444 	int ifindex = *((const int *)src);
445 
446 	NET_EPOCH_ASSERT();
447 
448 	struct ifnet *ifp = ifnet_byindex(ifindex);
449 	if (ifindex != 0 && ifp == NULL) {
450 		NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
451 		return (ENOENT);
452 	}
453 	*((struct ifnet **)target) = ifp;
454 
455 	return (0);
456 }
457 
458 int
459 nlf_get_u8(void *src, struct nl_pstate *npt, void *target)
460 {
461 	uint8_t val = *((const uint8_t *)src);
462 
463 	*((uint8_t *)target) = val;
464 
465 	return (0);
466 }
467 
468 int
469 nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target)
470 {
471 	*((uint32_t *)target) = *((const uint8_t *)src);
472 	return (0);
473 }
474 
475 int
476 nlf_get_u16(void *src, struct nl_pstate *npt, void *target)
477 {
478 	*((uint16_t *)target) = *((const uint16_t *)src);
479 	return (0);
480 }
481 
482 int
483 nlf_get_u32(void *src, struct nl_pstate *npt, void *target)
484 {
485 	*((uint32_t *)target) = *((const uint32_t *)src);
486 	return (0);
487 }
488 
489