2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <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_DEBUG);
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)
81 static const struct nlattr_parser *
82 search_states(const struct nlattr_parser *ps, int pslen, int key)
84 int left_i = 0, right_i = pslen - 1;
86 if (key < ps[0].type || key > ps[pslen - 1].type)
89 while (left_i + 1 < right_i) {
90 int mid_i = (left_i + right_i) / 2;
91 if (key < ps[mid_i].type)
93 else if (key > ps[mid_i].type)
98 if (ps[left_i].type == key)
100 else if (ps[right_i].type == key)
101 return (&ps[right_i]);
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)
109 struct nlattr *nla = NULL;
112 NL_LOG(LOG_DEBUG3, "parse %p remaining_len %d", nla_head, 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);
124 int nla_type = nla->nla_type & NLA_TYPE_MASK;
125 const struct nlattr_parser *s = search_states(ps, pslen, nla_type);
127 void *ptr = (void *)((char *)target + s->off);
128 error = s->cb(nla, npt, s->arg, ptr);
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);
136 /* Ignore non-specified attributes */
137 NL_LOG(LOG_DEBUG3, "ignoring attr %d", nla->nla_type);
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);
145 NL_LOG(LOG_DEBUG3, "end parse: %p remaining_len %d", nla, len);
151 nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len, struct nlattr_bmask *bm)
153 struct nlattr *nla = NULL;
155 BIT_ZERO(NL_ATTR_BMASK_SIZE, bm);
157 NLA_FOREACH(nla, nla_head, len) {
158 if (nla->nla_len < sizeof(struct nlattr))
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);
164 NL_LOG(LOG_DEBUG2, "Skipping type %d in the mask: too short",
170 nl_has_attr(const struct nlattr_bmask *bm, unsigned int nla_type)
172 MPASS(nla_type < NL_ATTR_BMASK_SIZE);
174 return (BIT_ISSET(NL_ATTR_BMASK_SIZE, nla_type, bm));
178 nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
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));
186 *((uint8_t *)target) = 1;
190 static struct sockaddr *
191 parse_rta_ip4(void *rta_data, struct nl_pstate *npt, int *perror)
193 struct sockaddr_in *sin;
195 sin = (struct sockaddr_in *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in));
196 if (__predict_false(sin == NULL)) {
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);
206 static struct sockaddr *
207 parse_rta_ip6(void *rta_data, struct nl_pstate *npt, int *perror)
209 struct sockaddr_in6 *sin6;
211 sin6 = (struct sockaddr_in6 *)npt_alloc_sockaddr(npt, sizeof(struct sockaddr_in6));
212 if (__predict_false(sin6 == NULL)) {
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);
222 static struct sockaddr *
223 parse_rta_ip(struct rtattr *rta, struct nl_pstate *npt, int *perror)
225 void *rta_data = NL_RTA_DATA(rta);
226 int rta_len = NL_RTA_DATA_LEN(rta);
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));
233 NLMSG_REPORT_ERR_MSG(npt, "unknown IP len: %d for rta type %d",
234 rta_len, rta->rta_type);
242 nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
246 struct sockaddr *sa = parse_rta_ip((struct rtattr *)nla, npt, &error);
248 *((struct sockaddr **)target) = sa;
252 static struct sockaddr *
253 parse_rta_via(struct rtattr *rta, struct nl_pstate *npt, int *perror)
255 struct rtvia *via = NL_RTA_DATA(rta);
256 int data_len = NL_RTA_DATA_LEN(rta);
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);
264 data_len -= offsetof(struct rtvia, rtvia_addr);
266 switch (via->rtvia_family) {
268 if (__predict_false(data_len < sizeof(struct in_addr))) {
272 return (parse_rta_ip4(via->rtvia_addr, npt, perror));
274 if (__predict_false(data_len < sizeof(struct in6_addr))) {
278 return (parse_rta_ip6(via->rtvia_addr, npt, perror));
286 nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
290 struct sockaddr *sa = parse_rta_via((struct rtattr *)nla, npt, &error);
292 *((struct sockaddr **)target) = sa;
298 nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
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));
305 *((uint16_t *)target) = *((const uint16_t *)NL_RTA_DATA_CONST(nla));
310 nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
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));
317 *((uint32_t *)target) = *((const uint32_t *)NL_RTA_DATA_CONST(nla));
322 nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
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));
329 memcpy(target, NL_RTA_DATA_CONST(nla), sizeof(uint64_t));
334 nlattr_get_ifp_internal(struct nlattr *nla, struct nl_pstate *npt,
335 void *target, bool zero_ok)
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));
342 uint32_t ifindex = *((const uint32_t *)NLA_DATA_CONST(nla));
344 if (ifindex == 0 && zero_ok) {
345 *((struct ifnet **)target) = NULL;
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);
357 *((struct ifnet **)target) = ifp;
358 NL_LOG(LOG_DEBUG3, "nla type %d: ifindex %u -> %s", nla->nla_type,
359 ifindex, if_name(ifp));
365 nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
367 return (nlattr_get_ifp_internal(nla, npt, target, false));
371 nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
373 return (nlattr_get_ifp_internal(nla, npt, target, true));
377 nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
379 int maxlen = NLA_DATA_LEN(nla);
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);
387 *((char **)target) = (char *)NLA_DATA(nla);
392 nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
394 int maxlen = NLA_DATA_LEN(nla);
396 char *buf = npt_alloc(npt, maxlen + 1);
400 memcpy(buf, NLA_DATA(nla), maxlen);
402 *((char **)target) = buf;
406 nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
408 NL_LOG(LOG_DEBUG3, "STORING %p len %d", nla, nla->nla_len);
409 *((struct nlattr **)target) = nla;
414 nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
416 const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg;
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);
425 nlf_get_ifp(void *src, struct nl_pstate *npt, void *target)
427 int ifindex = *((const int *)src);
431 struct ifnet *ifp = ifnet_byindex(ifindex);
433 NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
436 *((struct ifnet **)target) = ifp;
442 nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target)
444 int ifindex = *((const int *)src);
448 struct ifnet *ifp = ifnet_byindex(ifindex);
449 if (ifindex != 0 && ifp == NULL) {
450 NL_LOG(LOG_DEBUG, "ifindex %u invalid", ifindex);
453 *((struct ifnet **)target) = ifp;
459 nlf_get_u8(void *src, struct nl_pstate *npt, void *target)
461 uint8_t val = *((const uint8_t *)src);
463 *((uint8_t *)target) = val;
469 nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target)
471 *((uint32_t *)target) = *((const uint8_t *)src);
476 nlf_get_u16(void *src, struct nl_pstate *npt, void *target)
478 *((uint16_t *)target) = *((const uint16_t *)src);
483 nlf_get_u32(void *src, struct nl_pstate *npt, void *target)
485 *((uint32_t *)target) = *((const uint32_t *)src);