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