2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
30 #include "opt_inet6.h"
31 #include <sys/types.h>
32 #include <sys/malloc.h>
33 #include <sys/rmlock.h>
34 #include <sys/socket.h>
36 #include <machine/stdarg.h>
39 #include <net/route.h>
40 #include <net/route/nhop.h>
42 #include <net/route/route_ctl.h>
43 #include <netinet/in.h>
44 #include <netlink/netlink.h>
45 #include <netlink/netlink_ctl.h>
46 #include <netlink/netlink_var.h>
47 #include <netlink/netlink_route.h>
49 #define DEBUG_MOD_NAME nl_parser
50 #define DEBUG_MAX_LEVEL LOG_DEBUG3
51 #include <netlink/netlink_debug.h>
52 _DECLARE_DEBUG(LOG_INFO);
55 nlmsg_report_err_msg(struct nl_pstate *npt, const char *fmt, ...)
59 if (npt->err_msg != NULL)
61 char *buf = npt_alloc(npt, NL_MAX_ERROR_BUF);
65 vsnprintf(buf, NL_MAX_ERROR_BUF, fmt, ap);
73 nlmsg_report_err_offset(struct nl_pstate *npt, uint32_t off)
75 if (npt->err_off != 0)
82 nlmsg_report_cookie(struct nl_pstate *npt, struct nlattr *nla)
84 MPASS(nla->nla_type == NLMSGERR_ATTR_COOKIE);
85 MPASS(nla->nla_len >= sizeof(struct nlattr));
90 nlmsg_report_cookie_u32(struct nl_pstate *npt, uint32_t val)
92 struct nlattr *nla = npt_alloc(npt, sizeof(*nla) + sizeof(uint32_t));
94 nla->nla_type = NLMSGERR_ATTR_COOKIE;
95 nla->nla_len = sizeof(*nla) + sizeof(uint32_t);
96 memcpy(nla + 1, &val, sizeof(uint32_t));
97 nlmsg_report_cookie(npt, nla);
100 static const struct nlattr_parser *
101 search_states(const struct nlattr_parser *ps, int pslen, int key)
103 int left_i = 0, right_i = pslen - 1;
105 if (key < ps[0].type || key > ps[pslen - 1].type)
108 while (left_i + 1 < right_i) {
109 int mid_i = (left_i + right_i) / 2;
110 if (key < ps[mid_i].type)
112 else if (key > ps[mid_i].type)
117 if (ps[left_i].type == key)
118 return (&ps[left_i]);
119 else if (ps[right_i].type == key)
120 return (&ps[right_i]);
125 nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser *ps, int pslen,
126 struct nl_pstate *npt, void *target)
128 struct nlattr *nla = NULL;
131 NL_LOG(LOG_DEBUG3, "parse %p remaining_len %d", nla_head, len);
133 NLA_FOREACH(nla, nla_head, len) {
134 NL_LOG(LOG_DEBUG3, ">> parsing %p attr_type %d len %d (rem %d)", nla, nla->nla_type, nla->nla_len, len);
135 if (nla->nla_len < sizeof(struct nlattr)) {
136 NLMSG_REPORT_ERR_MSG(npt, "Invalid attr %p type %d len: %d",
137 nla, nla->nla_type, nla->nla_len);
138 uint32_t off = (char *)nla - (char *)npt->hdr;
139 nlmsg_report_err_offset(npt, off);
143 int nla_type = nla->nla_type & NLA_TYPE_MASK;
144 const struct nlattr_parser *s = search_states(ps, pslen, nla_type);
146 void *ptr = (void *)((char *)target + s->off);
147 error = s->cb(nla, npt, s->arg, ptr);
149 uint32_t off = (char *)nla - (char *)npt->hdr;
150 nlmsg_report_err_offset(npt, off);
151 NL_LOG(LOG_DEBUG3, "parse failed at offset %u", off);
155 /* Ignore non-specified attributes */
156 NL_LOG(LOG_DEBUG3, "ignoring attr %d", nla->nla_type);
159 if (len >= sizeof(struct nlattr)) {
160 nla = (struct nlattr *)((char *)nla_head + (orig_len - len));
161 NL_LOG(LOG_DEBUG3, " >>> end %p attr_type %d len %d", nla,
162 nla->nla_type, nla->nla_len);
164 NL_LOG(LOG_DEBUG3, "end parse: %p remaining_len %d", nla, len);
170 nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len, struct nlattr_bmask *bm)
172 struct nlattr *nla = NULL;
174 BIT_ZERO(NL_ATTR_BMASK_SIZE, bm);
176 NLA_FOREACH(nla, nla_head, len) {
177 if (nla->nla_len < sizeof(struct nlattr))
179 int nla_type = nla->nla_type & NLA_TYPE_MASK;
180 if (nla_type < NL_ATTR_BMASK_SIZE)
181 BIT_SET(NL_ATTR_BMASK_SIZE, nla_type, bm);
183 NL_LOG(LOG_DEBUG2, "Skipping type %d in the mask: too short",
189 nl_has_attr(const struct nlattr_bmask *bm, unsigned int nla_type)
191 MPASS(nla_type < NL_ATTR_BMASK_SIZE);
193 return (BIT_ISSET(NL_ATTR_BMASK_SIZE, nla_type, bm));
197 nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
199 if (__predict_false(NLA_DATA_LEN(nla) != 0)) {
200 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not a flag",
201 nla->nla_type, NLA_DATA_LEN(nla));
205 *((uint8_t *)target) = 1;
209 static struct sockaddr *
210 parse_rta_ip4(void *rta_data, struct nl_pstate *npt, int *perror)
212 struct sockaddr_in *sin;
214 sin = (struct sockaddr_in *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in));
215 if (__predict_false(sin == NULL)) {
219 sin->sin_len = sizeof(struct sockaddr_in);
220 sin->sin_family = AF_INET;
221 memcpy(&sin->sin_addr, rta_data, sizeof(struct in_addr));
222 return ((struct sockaddr *)sin);
225 static struct sockaddr *
226 parse_rta_ip6(void *rta_data, struct nl_pstate *npt, int *perror)
228 struct sockaddr_in6 *sin6;
230 sin6 = (struct sockaddr_in6 *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in6));
231 if (__predict_false(sin6 == NULL)) {
235 sin6->sin6_len = sizeof(struct sockaddr_in6);
236 sin6->sin6_family = AF_INET6;
237 memcpy(&sin6->sin6_addr, rta_data, sizeof(struct in6_addr));
238 return ((struct sockaddr *)sin6);
241 static struct sockaddr *
242 parse_rta_ip(struct rtattr *rta, struct nl_pstate *npt, int *perror)
244 void *rta_data = NL_RTA_DATA(rta);
245 int rta_len = NL_RTA_DATA_LEN(rta);
247 if (rta_len == sizeof(struct in_addr)) {
248 return (parse_rta_ip4(rta_data, npt, perror));
249 } else if (rta_len == sizeof(struct in6_addr)) {
250 return (parse_rta_ip6(rta_data, npt, perror));
252 NLMSG_REPORT_ERR_MSG(npt, "unknown IP len: %d for rta type %d",
253 rta_len, rta->rta_type);
261 nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
265 struct sockaddr *sa = parse_rta_ip((struct rtattr *)nla, npt, &error);
267 *((struct sockaddr **)target) = sa;
271 static struct sockaddr *
272 parse_rta_via(struct rtattr *rta, struct nl_pstate *npt, int *perror)
274 struct rtvia *via = NL_RTA_DATA(rta);
275 int data_len = NL_RTA_DATA_LEN(rta);
277 if (__predict_false(data_len) < sizeof(struct rtvia)) {
278 NLMSG_REPORT_ERR_MSG(npt, "undersized RTA_VIA(%d) attr: len %d",
279 rta->rta_type, data_len);
283 data_len -= offsetof(struct rtvia, rtvia_addr);
285 switch (via->rtvia_family) {
287 if (__predict_false(data_len < sizeof(struct in_addr))) {
291 return (parse_rta_ip4(via->rtvia_addr, npt, perror));
293 if (__predict_false(data_len < sizeof(struct in6_addr))) {
297 return (parse_rta_ip6(via->rtvia_addr, npt, perror));
305 nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
309 struct sockaddr *sa = parse_rta_via((struct rtattr *)nla, npt, &error);
311 *((struct sockaddr **)target) = sa;
316 nlattr_get_bool(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
318 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(bool))) {
319 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not bool",
320 nla->nla_type, NLA_DATA_LEN(nla));
323 *((bool *)target) = *((const bool *)NL_RTA_DATA_CONST(nla));
328 nlattr_get_uint8(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
330 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint8_t))) {
331 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint8",
332 nla->nla_type, NLA_DATA_LEN(nla));
335 *((uint8_t *)target) = *((const uint8_t *)NL_RTA_DATA_CONST(nla));
340 nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
342 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint16_t))) {
343 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint16",
344 nla->nla_type, NLA_DATA_LEN(nla));
347 *((uint16_t *)target) = *((const uint16_t *)NL_RTA_DATA_CONST(nla));
352 nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
354 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
355 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
356 nla->nla_type, NLA_DATA_LEN(nla));
359 *((uint32_t *)target) = *((const uint32_t *)NL_RTA_DATA_CONST(nla));
364 nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
366 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint64_t))) {
367 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint64",
368 nla->nla_type, NLA_DATA_LEN(nla));
371 memcpy(target, NL_RTA_DATA_CONST(nla), sizeof(uint64_t));
376 nlattr_get_in_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
378 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(in_addr_t))) {
379 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not in_addr_t",
380 nla->nla_type, NLA_DATA_LEN(nla));
383 memcpy(target, NLA_DATA_CONST(nla), sizeof(in_addr_t));
388 nlattr_get_in6_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
390 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(struct in6_addr))) {
391 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not struct in6_addr",
392 nla->nla_type, NLA_DATA_LEN(nla));
395 memcpy(target, NLA_DATA_CONST(nla), sizeof(struct in6_addr));
400 nlattr_get_ifp_internal(struct nlattr *nla, struct nl_pstate *npt,
401 void *target, bool zero_ok)
403 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
404 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
405 nla->nla_type, NLA_DATA_LEN(nla));
408 uint32_t ifindex = *((const uint32_t *)NLA_DATA_CONST(nla));
410 if (ifindex == 0 && zero_ok) {
411 *((struct ifnet **)target) = NULL;
417 struct ifnet *ifp = ifnet_byindex(ifindex);
418 if (__predict_false(ifp == NULL)) {
419 NLMSG_REPORT_ERR_MSG(npt, "nla type %d: ifindex %u invalid",
420 nla->nla_type, ifindex);
423 *((struct ifnet **)target) = ifp;
424 NL_LOG(LOG_DEBUG3, "nla type %d: ifindex %u -> %s", nla->nla_type,
425 ifindex, if_name(ifp));
431 nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
433 return (nlattr_get_ifp_internal(nla, npt, target, false));
437 nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
439 return (nlattr_get_ifp_internal(nla, npt, target, true));
443 nlattr_get_chara(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
445 int maxlen = NLA_DATA_LEN(nla);
446 int target_size = (size_t)arg;
447 int len = strnlen((char *)NLA_DATA(nla), maxlen);
449 if (__predict_false(len >= maxlen) || __predict_false(len >= target_size)) {
450 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not NULL-terminated or longer than %u",
451 nla->nla_type, maxlen, target_size);
455 strncpy((char *)target, (char *)NLA_DATA(nla), target_size);
460 nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
462 int maxlen = NLA_DATA_LEN(nla);
464 if (__predict_false(strnlen((char *)NLA_DATA(nla), maxlen) >= maxlen)) {
465 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not NULL-terminated",
466 nla->nla_type, maxlen);
470 *((char **)target) = (char *)NLA_DATA(nla);
475 nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
477 int maxlen = NLA_DATA_LEN(nla);
479 char *buf = npt_alloc(npt, maxlen + 1);
483 memcpy(buf, NLA_DATA(nla), maxlen);
485 *((char **)target) = buf;
490 nlattr_get_bytes(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
492 size_t size = (size_t)arg;
494 if (NLA_DATA_LEN(nla) != size)
497 memcpy(target, NLA_DATA(nla), size);
503 nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
505 NL_LOG(LOG_DEBUG3, "STORING %p len %d", nla, nla->nla_len);
506 *((struct nlattr **)target) = nla;
511 nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
513 const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg;
516 /* Assumes target points to the beginning of the structure */
517 error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt, target);
522 nlattr_get_nested_ptr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
524 const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg;
527 /* Assumes target points to the beginning of the structure */
528 error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt, *(void **)target);
533 nlf_get_ifp(void *src, struct nl_pstate *npt, void *target)
535 int ifindex = *((const int *)src);
539 struct ifnet *ifp = ifnet_byindex(ifindex);
541 NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
544 *((struct ifnet **)target) = ifp;
550 nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target)
552 int ifindex = *((const int *)src);
556 struct ifnet *ifp = ifnet_byindex(ifindex);
557 if (ifindex != 0 && ifp == NULL) {
558 NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
561 *((struct ifnet **)target) = ifp;
567 nlf_get_u8(void *src, struct nl_pstate *npt, void *target)
569 uint8_t val = *((const uint8_t *)src);
571 *((uint8_t *)target) = val;
577 nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target)
579 *((uint32_t *)target) = *((const uint8_t *)src);
584 nlf_get_u16(void *src, struct nl_pstate *npt, void *target)
586 *((uint16_t *)target) = *((const uint16_t *)src);
591 nlf_get_u32(void *src, struct nl_pstate *npt, void *target)
593 *((uint32_t *)target) = *((const uint32_t *)src);