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>
29 __FBSDID("$FreeBSD$");
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>
37 #include <machine/stdarg.h>
40 #include <net/route.h>
41 #include <net/route/nhop.h>
43 #include <net/route/route_ctl.h>
44 #include <netinet/in.h>
45 #include <netlink/netlink.h>
46 #include <netlink/netlink_ctl.h>
47 #include <netlink/netlink_var.h>
48 #include <netlink/netlink_route.h>
50 #define DEBUG_MOD_NAME nl_parser
51 #define DEBUG_MAX_LEVEL LOG_DEBUG3
52 #include <netlink/netlink_debug.h>
53 _DECLARE_DEBUG(LOG_INFO);
56 nlmsg_report_err_msg(struct nl_pstate *npt, const char *fmt, ...)
60 if (npt->err_msg != NULL)
62 char *buf = npt_alloc(npt, NL_MAX_ERROR_BUF);
66 vsnprintf(buf, NL_MAX_ERROR_BUF, fmt, ap);
74 nlmsg_report_err_offset(struct nl_pstate *npt, uint32_t off)
76 if (npt->err_off != 0)
83 nlmsg_report_cookie(struct nl_pstate *npt, struct nlattr *nla)
85 MPASS(nla->nla_type == NLMSGERR_ATTR_COOKIE);
86 MPASS(nla->nla_len >= sizeof(struct nlattr));
91 nlmsg_report_cookie_u32(struct nl_pstate *npt, uint32_t val)
93 struct nlattr *nla = npt_alloc(npt, sizeof(*nla) + sizeof(uint32_t));
95 nla->nla_type = NLMSGERR_ATTR_COOKIE;
96 nla->nla_len = sizeof(*nla) + sizeof(uint32_t);
97 memcpy(nla + 1, &val, sizeof(uint32_t));
98 nlmsg_report_cookie(npt, nla);
101 static const struct nlattr_parser *
102 search_states(const struct nlattr_parser *ps, int pslen, int key)
104 int left_i = 0, right_i = pslen - 1;
106 if (key < ps[0].type || key > ps[pslen - 1].type)
109 while (left_i + 1 < right_i) {
110 int mid_i = (left_i + right_i) / 2;
111 if (key < ps[mid_i].type)
113 else if (key > ps[mid_i].type)
118 if (ps[left_i].type == key)
119 return (&ps[left_i]);
120 else if (ps[right_i].type == key)
121 return (&ps[right_i]);
126 nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser *ps, int pslen,
127 struct nl_pstate *npt, void *target)
129 struct nlattr *nla = NULL;
132 NL_LOG(LOG_DEBUG3, "parse %p remaining_len %d", nla_head, len);
134 NLA_FOREACH(nla, nla_head, len) {
135 NL_LOG(LOG_DEBUG3, ">> parsing %p attr_type %d len %d (rem %d)", nla, nla->nla_type, nla->nla_len, len);
136 if (nla->nla_len < sizeof(struct nlattr)) {
137 NLMSG_REPORT_ERR_MSG(npt, "Invalid attr %p type %d len: %d",
138 nla, nla->nla_type, nla->nla_len);
139 uint32_t off = (char *)nla - (char *)npt->hdr;
140 nlmsg_report_err_offset(npt, off);
144 int nla_type = nla->nla_type & NLA_TYPE_MASK;
145 const struct nlattr_parser *s = search_states(ps, pslen, nla_type);
147 void *ptr = (void *)((char *)target + s->off);
148 error = s->cb(nla, npt, s->arg, ptr);
150 uint32_t off = (char *)nla - (char *)npt->hdr;
151 nlmsg_report_err_offset(npt, off);
152 NL_LOG(LOG_DEBUG3, "parse failed att offset %u", off);
156 /* Ignore non-specified attributes */
157 NL_LOG(LOG_DEBUG3, "ignoring attr %d", nla->nla_type);
160 if (len >= sizeof(struct nlattr)) {
161 nla = (struct nlattr *)((char *)nla_head + (orig_len - len));
162 NL_LOG(LOG_DEBUG3, " >>> end %p attr_type %d len %d", nla,
163 nla->nla_type, nla->nla_len);
165 NL_LOG(LOG_DEBUG3, "end parse: %p remaining_len %d", nla, len);
171 nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len, struct nlattr_bmask *bm)
173 struct nlattr *nla = NULL;
175 BIT_ZERO(NL_ATTR_BMASK_SIZE, bm);
177 NLA_FOREACH(nla, nla_head, len) {
178 if (nla->nla_len < sizeof(struct nlattr))
180 int nla_type = nla->nla_type & NLA_TYPE_MASK;
181 if (nla_type < NL_ATTR_BMASK_SIZE)
182 BIT_SET(NL_ATTR_BMASK_SIZE, nla_type, bm);
184 NL_LOG(LOG_DEBUG2, "Skipping type %d in the mask: too short",
190 nl_has_attr(const struct nlattr_bmask *bm, unsigned int nla_type)
192 MPASS(nla_type < NL_ATTR_BMASK_SIZE);
194 return (BIT_ISSET(NL_ATTR_BMASK_SIZE, nla_type, bm));
198 nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
200 if (__predict_false(NLA_DATA_LEN(nla) != 0)) {
201 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not a flag",
202 nla->nla_type, NLA_DATA_LEN(nla));
206 *((uint8_t *)target) = 1;
210 static struct sockaddr *
211 parse_rta_ip4(void *rta_data, struct nl_pstate *npt, int *perror)
213 struct sockaddr_in *sin;
215 sin = (struct sockaddr_in *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in));
216 if (__predict_false(sin == NULL)) {
220 sin->sin_len = sizeof(struct sockaddr_in);
221 sin->sin_family = AF_INET;
222 memcpy(&sin->sin_addr, rta_data, sizeof(struct in_addr));
223 return ((struct sockaddr *)sin);
226 static struct sockaddr *
227 parse_rta_ip6(void *rta_data, struct nl_pstate *npt, int *perror)
229 struct sockaddr_in6 *sin6;
231 sin6 = (struct sockaddr_in6 *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in6));
232 if (__predict_false(sin6 == NULL)) {
236 sin6->sin6_len = sizeof(struct sockaddr_in6);
237 sin6->sin6_family = AF_INET6;
238 memcpy(&sin6->sin6_addr, rta_data, sizeof(struct in6_addr));
239 return ((struct sockaddr *)sin6);
242 static struct sockaddr *
243 parse_rta_ip(struct rtattr *rta, struct nl_pstate *npt, int *perror)
245 void *rta_data = NL_RTA_DATA(rta);
246 int rta_len = NL_RTA_DATA_LEN(rta);
248 if (rta_len == sizeof(struct in_addr)) {
249 return (parse_rta_ip4(rta_data, npt, perror));
250 } else if (rta_len == sizeof(struct in6_addr)) {
251 return (parse_rta_ip6(rta_data, npt, perror));
253 NLMSG_REPORT_ERR_MSG(npt, "unknown IP len: %d for rta type %d",
254 rta_len, rta->rta_type);
262 nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
266 struct sockaddr *sa = parse_rta_ip((struct rtattr *)nla, npt, &error);
268 *((struct sockaddr **)target) = sa;
272 static struct sockaddr *
273 parse_rta_via(struct rtattr *rta, struct nl_pstate *npt, int *perror)
275 struct rtvia *via = NL_RTA_DATA(rta);
276 int data_len = NL_RTA_DATA_LEN(rta);
278 if (__predict_false(data_len) < sizeof(struct rtvia)) {
279 NLMSG_REPORT_ERR_MSG(npt, "undersized RTA_VIA(%d) attr: len %d",
280 rta->rta_type, data_len);
284 data_len -= offsetof(struct rtvia, rtvia_addr);
286 switch (via->rtvia_family) {
288 if (__predict_false(data_len < sizeof(struct in_addr))) {
292 return (parse_rta_ip4(via->rtvia_addr, npt, perror));
294 if (__predict_false(data_len < sizeof(struct in6_addr))) {
298 return (parse_rta_ip6(via->rtvia_addr, npt, perror));
306 nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
310 struct sockaddr *sa = parse_rta_via((struct rtattr *)nla, npt, &error);
312 *((struct sockaddr **)target) = sa;
318 nlattr_get_uint8(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
320 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint8_t))) {
321 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint8",
322 nla->nla_type, NLA_DATA_LEN(nla));
325 *((uint16_t *)target) = *((const uint16_t *)NL_RTA_DATA_CONST(nla));
330 nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
332 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint16_t))) {
333 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint16",
334 nla->nla_type, NLA_DATA_LEN(nla));
337 *((uint16_t *)target) = *((const uint16_t *)NL_RTA_DATA_CONST(nla));
342 nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
344 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
345 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
346 nla->nla_type, NLA_DATA_LEN(nla));
349 *((uint32_t *)target) = *((const uint32_t *)NL_RTA_DATA_CONST(nla));
354 nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
356 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint64_t))) {
357 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint64",
358 nla->nla_type, NLA_DATA_LEN(nla));
361 memcpy(target, NL_RTA_DATA_CONST(nla), sizeof(uint64_t));
366 nlattr_get_in_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
368 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(in_addr_t))) {
369 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not in_addr_t",
370 nla->nla_type, NLA_DATA_LEN(nla));
373 memcpy(target, NLA_DATA_CONST(nla), sizeof(in_addr_t));
378 nlattr_get_in6_addr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
380 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(struct in6_addr))) {
381 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not struct in6_addr",
382 nla->nla_type, NLA_DATA_LEN(nla));
385 memcpy(target, NLA_DATA_CONST(nla), sizeof(struct in6_addr));
390 nlattr_get_ifp_internal(struct nlattr *nla, struct nl_pstate *npt,
391 void *target, bool zero_ok)
393 if (__predict_false(NLA_DATA_LEN(nla) != sizeof(uint32_t))) {
394 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not uint32",
395 nla->nla_type, NLA_DATA_LEN(nla));
398 uint32_t ifindex = *((const uint32_t *)NLA_DATA_CONST(nla));
400 if (ifindex == 0 && zero_ok) {
401 *((struct ifnet **)target) = NULL;
407 struct ifnet *ifp = ifnet_byindex(ifindex);
408 if (__predict_false(ifp == NULL)) {
409 NLMSG_REPORT_ERR_MSG(npt, "nla type %d: ifindex %u invalid",
410 nla->nla_type, ifindex);
413 *((struct ifnet **)target) = ifp;
414 NL_LOG(LOG_DEBUG3, "nla type %d: ifindex %u -> %s", nla->nla_type,
415 ifindex, if_name(ifp));
421 nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
423 return (nlattr_get_ifp_internal(nla, npt, target, false));
427 nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
429 return (nlattr_get_ifp_internal(nla, npt, target, true));
433 nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
435 int maxlen = NLA_DATA_LEN(nla);
437 if (__predict_false(strnlen((char *)NLA_DATA(nla), maxlen) >= maxlen)) {
438 NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not NULL-terminated",
439 nla->nla_type, maxlen);
443 *((char **)target) = (char *)NLA_DATA(nla);
448 nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
450 int maxlen = NLA_DATA_LEN(nla);
452 char *buf = npt_alloc(npt, maxlen + 1);
456 memcpy(buf, NLA_DATA(nla), maxlen);
458 *((char **)target) = buf;
462 nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
464 NL_LOG(LOG_DEBUG3, "STORING %p len %d", nla, nla->nla_len);
465 *((struct nlattr **)target) = nla;
470 nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
472 const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg;
475 /* Assumes target points to the beginning of the structure */
476 error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt, target);
481 nlf_get_ifp(void *src, struct nl_pstate *npt, void *target)
483 int ifindex = *((const int *)src);
487 struct ifnet *ifp = ifnet_byindex(ifindex);
489 NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
492 *((struct ifnet **)target) = ifp;
498 nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target)
500 int ifindex = *((const int *)src);
504 struct ifnet *ifp = ifnet_byindex(ifindex);
505 if (ifindex != 0 && ifp == NULL) {
506 NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
509 *((struct ifnet **)target) = ifp;
515 nlf_get_u8(void *src, struct nl_pstate *npt, void *target)
517 uint8_t val = *((const uint8_t *)src);
519 *((uint8_t *)target) = val;
525 nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target)
527 *((uint32_t *)target) = *((const uint8_t *)src);
532 nlf_get_u16(void *src, struct nl_pstate *npt, void *target)
534 *((uint16_t *)target) = *((const uint16_t *)src);
539 nlf_get_u32(void *src, struct nl_pstate *npt, void *target)
541 *((uint32_t *)target) = *((const uint32_t *)src);