• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/lib/rtnl.c

  1 /*
  2 Copyright 2021 Jo-Philipp Wich <jo@mein.io>
  3 
  4 Licensed under the Apache License, Version 2.0 (the "License");
  5 you may not use this file except in compliance with the License.
  6 You may obtain a copy of the License at
  7 
  8         http://www.apache.org/licenses/LICENSE-2.0
  9 
 10 Unless required by applicable law or agreed to in writing, software
 11 distributed under the License is distributed on an "AS IS" BASIS,
 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 See the License for the specific language governing permissions and
 14 limitations under the License.
 15 */
 16 
 17 /**
 18  * # Routing Netlink
 19  *
 20  * The `rtnl` module provides functions for interacting with the routing netlink interface.
 21  *
 22  * Functions can be individually imported and directly accessed using the
 23  * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#named_import named import}
 24  * syntax:
 25  *
 26  *   ```javascript
 27  *   import { error, request, listener, RTM_GETROUTE, RTM_NEWROUTE, RTM_DELROUTE, AF_INET } from 'rtnl';
 28  *
 29  *   // Send a netlink request
 30  *   let response = request(RTM_GETROUTE, 0, { family: AF_INET });
 31  *
 32  *   // Create a listener for route changes
 33  *   let routeListener = listener((msg) => {
 34  *       print('Received route message:', msg, '\n');
 35  *   }, [RTM_NEWROUTE, RTM_DELROUTE]);
 36  *   ```
 37  *
 38  * Alternatively, the module namespace can be imported
 39  * using a wildcard import statement:
 40  *
 41  *   ```javascript
 42  *   import * as rtnl from 'rtnl';
 43  *
 44  *   // Send a netlink request
 45  *   let response = rtnl.request(rtnl.RTM_GETROUTE, 0, { family: rtnl.AF_INET });
 46  *
 47  *   // Create a listener for route changes
 48  *   let listener = rtnl.listener((msg) => {
 49  *       print('Received route message:', msg, '\n');
 50  *   }, [rtnl.RTM_NEWROUTE, rtnl.RTM_DELROUTE]);
 51  *   ```
 52  *
 53  * Additionally, the rtnl module namespace may also be imported by invoking
 54  * the `ucode` interpreter with the `-lrtnl` switch.
 55  *
 56  * @module rtnl
 57  */
 58 
 59 #include <errno.h>
 60 #include <assert.h>
 61 
 62 #include <netinet/ether.h>
 63 #include <arpa/inet.h>
 64 #include <netlink/attr.h>
 65 
 66 #include <linux/if_tunnel.h>
 67 #include <linux/ip6_tunnel.h>
 68 #include <linux/lwtunnel.h>
 69 #include <linux/mpls.h>
 70 #include <linux/mpls_iptunnel.h>
 71 #include <linux/seg6_iptunnel.h>
 72 #include <linux/seg6_hmac.h>
 73 #include <linux/veth.h>
 74 #include <linux/ila.h>
 75 #include <linux/fib_rules.h>
 76 #include <linux/if_addrlabel.h>
 77 #include <linux/if_bridge.h>
 78 #include <linux/netconf.h>
 79 #include <linux/ipv6.h>
 80 #include <linux/can/netlink.h>
 81 #include <linux/can/vxcan.h>
 82 
 83 #include <libubox/uloop.h>
 84 
 85 #include "ucode/module.h"
 86 
 87 #ifndef AF_MPLS
 88 # define AF_MPLS 28
 89 #endif
 90 
 91 #ifndef SOL_NETLINK
 92 # define SOL_NETLINK 270
 93 #endif
 94 
 95 #define DIV_ROUND_UP(n, d)      (((n) + (d) - 1) / (d))
 96 
 97 #define err_return(code, ...) do { set_error(code, __VA_ARGS__); return NULL; } while(0)
 98 
 99 #define NLM_F_STRICT_CHK (1 << 15)
100 
101 #define RTNL_CMDS_BITMAP_SIZE   DIV_ROUND_UP(__RTM_MAX, 32)
102 #define RTNL_GRPS_BITMAP_SIZE   DIV_ROUND_UP(__RTNLGRP_MAX, 32)
103 
104 #define RTNL_EVSOCK_RCVBUF_INIT (1024 * 1024)
105 #define RTNL_EVSOCK_RCVBUF_MAX  (8 * 1024 * 1024)
106 
107 enum {
108         LISTENER_SLOT_RES,
109         LISTENER_SLOT_CB,
110         LISTENER_SLOT_OVERRUN_CB,
111         LISTENER_SLOTS
112 };
113 
114 /* Can't use net/if.h for declarations as it clashes with linux/if.h
115  * on certain musl versions.
116  * Ref: https://www.openwall.com/lists/musl/2017/04/16/1 */
117 extern unsigned int if_nametoindex (const char *);
118 extern char *if_indextoname (unsigned int ifindex, char *ifname);
119 
120 static struct {
121         int code;
122         char *msg;
123 } last_error;
124 
125 __attribute__((format(printf, 2, 3))) static void
126 set_error(int errcode, const char *fmt, ...) {
127         va_list ap;
128 
129         free(last_error.msg);
130 
131         last_error.code = errcode;
132         last_error.msg = NULL;
133 
134         if (fmt) {
135                 va_start(ap, fmt);
136                 xvasprintf(&last_error.msg, fmt, ap);
137                 va_end(ap);
138         }
139 }
140 
141 static uc_resource_type_t *listener_type;
142 static uc_value_t *listener_registry;
143 static uc_vm_t *listener_vm;
144 
145 typedef struct {
146         uint32_t cmds[RTNL_CMDS_BITMAP_SIZE];
147         size_t index;
148 } uc_nl_listener_t;
149 
150 typedef struct {
151         uint8_t family;
152         uint8_t mask;
153         uint8_t alen;
154         uint8_t bitlen;
155         union {
156                 struct in_addr in;
157                 struct in6_addr in6;
158                 struct mpls_label mpls[16];
159         } addr;
160 } uc_nl_cidr_t;
161 
162 static bool
163 uc_nl_parse_u32(uc_value_t *val, uint32_t *n)
164 {
165         uint64_t u;
166 
167         u = ucv_to_unsigned(val);
168 
169         if (errno != 0 || u > UINT32_MAX)
170                 return false;
171 
172         *n = (uint32_t)u;
173 
174         return true;
175 }
176 
177 static bool
178 uc_nl_parse_s32(uc_value_t *val, uint32_t *n)
179 {
180         int64_t i;
181 
182         i = ucv_to_integer(val);
183 
184         if (errno != 0 || i < INT32_MIN || i > INT32_MAX)
185                 return false;
186 
187         *n = (uint32_t)i;
188 
189         return true;
190 }
191 
192 static bool
193 uc_nl_parse_u64(uc_value_t *val, uint64_t *n)
194 {
195         *n = ucv_to_unsigned(val);
196 
197         return (errno == 0);
198 }
199 
200 static const char *
201 addr64_ntop(const void *addr, char *buf, size_t buflen)
202 {
203         const union { uint64_t u64; uint16_t u16[4]; } *a64 = addr;
204         int len;
205 
206         errno = 0;
207 
208         len = snprintf(buf, buflen, "%04x:%04x:%04x:%04x",
209                        ntohs(a64->u16[0]), ntohs(a64->u16[1]),
210                        ntohs(a64->u16[2]), ntohs(a64->u16[3]));
211 
212         if ((size_t)len >= buflen) {
213                 errno = ENOSPC;
214 
215                 return NULL;
216         }
217 
218         return buf;
219 }
220 
221 static int
222 addr64_pton(const char *src, void *dst)
223 {
224         union { uint64_t u64; uint16_t u16[4]; } *a64 = dst;
225         unsigned long n;
226         size_t i;
227         char *e;
228 
229         for (i = 0; i < ARRAY_SIZE(a64->u16); i++) {
230                 n = strtoul(src, &e, 16);
231 
232                 if (e == src || n > 0xffff)
233                         return -1;
234 
235                 a64->u16[i] = htons(n);
236 
237                 if (*e == 0)
238                         break;
239 
240                 if (i >= 3 || *e != ':')
241                         return -1;
242 
243                 src += (e - src) + 1;
244         }
245 
246         return 0;
247 }
248 
249 static const char *
250 mpls_ntop(const void *addr, size_t addrlen, char *buf, size_t buflen)
251 {
252         const struct mpls_label *p = addr;
253         size_t remlen = buflen;
254         uint32_t entry, label;
255         char *s = buf;
256         int len;
257 
258         errno = 0;
259 
260         while (addrlen >= sizeof(*p)) {
261                 entry = ntohl(p->entry);
262                 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
263 
264                 len = snprintf(s, remlen, "%u", label);
265 
266                 if ((size_t)len >= remlen)
267                         break;
268 
269                 if (entry & MPLS_LS_S_MASK)
270                         return buf;
271 
272                 s += len;
273                 remlen -= len;
274 
275                 if (remlen) {
276                         *s++ = '/';
277                         remlen--;
278                 }
279 
280                 p++;
281 
282                 addrlen -= sizeof(*p);
283         }
284 
285         errno = ENOSPC;
286 
287         return NULL;
288 }
289 
290 static int
291 mpls_pton(int af, const char *src, void *dst, size_t dstlen)
292 {
293         size_t max = dstlen / sizeof(struct mpls_label);
294         struct mpls_label *p = dst;
295         uint32_t label;
296         char *e;
297 
298         errno = 0;
299 
300         if (af != AF_MPLS) {
301                 errno = EAFNOSUPPORT;
302 
303                 return -1;
304         }
305 
306         while (max > 0) {
307                 label = strtoul(src, &e, 0);
308 
309                 if (label >= (1 << 20))
310                         return 0;
311 
312                 if (e == src)
313                         return 0;
314 
315                 p->entry = htonl(label << MPLS_LS_LABEL_SHIFT);
316 
317                 if (*e == 0) {
318                         p->entry |= htonl(1 << MPLS_LS_S_SHIFT);
319 
320                         return 1;
321                 }
322 
323                 if (*e != '/')
324                         return 0;
325 
326                 src += (e - src) + 1;
327                 max--;
328                 p++;
329         }
330 
331         errno = ENOSPC;
332 
333         return -1;
334 }
335 
336 static bool
337 uc_nl_parse_cidr(uc_vm_t *vm, uc_value_t *val, uc_nl_cidr_t *p)
338 {
339         char *s = ucv_to_string(vm, val);
340         struct in6_addr mask6 = { 0 };
341         struct in_addr mask = { 0 };
342         bool valid = true;
343         char *m, *e;
344         long n = 0;
345         size_t i;
346 
347         if (!s)
348                 return false;
349 
350         m = strchr(s, '/');
351 
352         if (m)
353                 *m++ = '\0';
354 
355         if (inet_pton(AF_INET6, s, &p->addr.in6) == 1) {
356                 if (m) {
357                         if (inet_pton(AF_INET6, m, &mask6) == 1) {
358                                 while (n < 128 && (mask6.s6_addr[n / 8] << (n % 8)) & 128)
359                                         n++;
360                         }
361                         else {
362                                 n = strtol(m, &e, 10);
363 
364                                 if (e == m || *e || n < 0 || n > 128)
365                                         valid = false;
366                         }
367 
368                         p->mask = (uint8_t)n;
369                 }
370                 else {
371                         p->mask = 128;
372                 }
373 
374                 p->family = AF_INET6;
375                 p->alen = sizeof(mask6);
376                 p->bitlen = p->alen * 8;
377         }
378         else if (strchr(s, '.') && inet_pton(AF_INET, s, &p->addr.in) == 1) {
379                 if (m) {
380                         if (inet_pton(AF_INET, m, &mask) == 1) {
381                                 mask.s_addr = ntohl(mask.s_addr);
382 
383                                 while (n < 32 && (mask.s_addr << n) & 0x80000000)
384                                         n++;
385                         }
386                         else {
387                                 n = strtol(m, &e, 10);
388 
389                                 if (e == m || *e || n < 0 || n > 32)
390                                         valid = false;
391                         }
392 
393                         p->mask = (uint8_t)n;
394                 }
395                 else {
396                         p->mask = 32;
397                 }
398 
399                 p->family = AF_INET;
400                 p->alen = sizeof(mask);
401                 p->bitlen = p->alen * 8;
402         }
403         else {
404                 if (m)
405                         m[-1] = '/';
406 
407                 if (mpls_pton(AF_MPLS, s, &p->addr.mpls, sizeof(p->addr.mpls)) == 1) {
408                         p->family = AF_MPLS;
409                         p->alen = 0;
410 
411                         for (i = 0; i < ARRAY_SIZE(p->addr.mpls); i++) {
412                                 p->alen += sizeof(struct mpls_label);
413 
414                                 if (ntohl(p->addr.mpls[i].entry) & MPLS_LS_S_MASK)
415                                         break;
416                         }
417 
418                         p->bitlen = p->alen * 8;
419                         p->mask = p->bitlen;
420                 }
421                 else {
422                         valid = false;
423                 }
424         }
425 
426         free(s);
427 
428         return valid;
429 }
430 
431 typedef enum {
432         DT_FLAG,
433         DT_BOOL,
434         DT_U8,
435         DT_U16,
436         DT_U32,
437         DT_S32,
438         DT_U64,
439         DT_STRING,
440         DT_NETDEV,
441         DT_LLADDR,
442         DT_INADDR,
443         DT_IN6ADDR,
444         DT_U64ADDR,
445         DT_MPLSADDR,
446         DT_ANYADDR,
447         DT_BRIDGEID,
448         DT_LINKINFO,
449         DT_MULTIPATH,
450         DT_NUMRANGE,
451         DT_AFSPEC,
452         DT_FLAGS,
453         DT_ENCAP,
454         DT_SRH,
455         DT_IPOPTS,
456         DT_U32_OR_MEMBER,
457         DT_NESTED,
458 } uc_nl_attr_datatype_t;
459 
460 enum {
461         DF_NO_SET = (1 << 0),
462         DF_NO_GET = (1 << 1),
463         DF_ALLOW_NONE = (1 << 2),
464         DF_BYTESWAP = (1 << 3),
465         DF_MAX_1 = (1 << 4),
466         DF_MAX_255 = (1 << 5),
467         DF_MAX_65535 = (1 << 6),
468         DF_MAX_16777215 = (1 << 7),
469         DF_STORE_MASK = (1 << 8),
470         DF_MULTIPLE = (1 << 9),
471         DF_FLAT = (1 << 10),
472         DF_FAMILY_HINT = (1 << 11),
473 };
474 
475 typedef struct uc_nl_attr_spec {
476         size_t attr;
477         const char *key;
478         uc_nl_attr_datatype_t type;
479         uint32_t flags;
480         const void *auxdata;
481 } uc_nl_attr_spec_t;
482 
483 typedef struct uc_nl_nested_spec {
484         size_t headsize;
485         size_t nattrs;
486         const uc_nl_attr_spec_t attrs[];
487 } uc_nl_nested_spec_t;
488 
489 #define SIZE(type) (void *)(uintptr_t)sizeof(struct type)
490 #define MEMBER(type, field) (void *)(uintptr_t)offsetof(struct type, field)
491 
492 static const uc_nl_nested_spec_t route_cacheinfo_rta = {
493         .headsize = NLA_ALIGN(sizeof(struct rta_cacheinfo)),
494         .nattrs = 8,
495         .attrs = {
496                 { RTA_UNSPEC, "clntref", DT_U32, 0, MEMBER(rta_cacheinfo, rta_clntref) },
497                 { RTA_UNSPEC, "lastuse", DT_U32, 0, MEMBER(rta_cacheinfo, rta_lastuse) },
498                 { RTA_UNSPEC, "expires", DT_S32, 0, MEMBER(rta_cacheinfo, rta_expires) },
499                 { RTA_UNSPEC, "error", DT_U32, 0, MEMBER(rta_cacheinfo, rta_error) },
500                 { RTA_UNSPEC, "used", DT_U32, 0, MEMBER(rta_cacheinfo, rta_used) },
501                 { RTA_UNSPEC, "id", DT_U32, 0, MEMBER(rta_cacheinfo, rta_id) },
502                 { RTA_UNSPEC, "ts", DT_U32, 0, MEMBER(rta_cacheinfo, rta_ts) },
503                 { RTA_UNSPEC, "tsage", DT_U32, 0, MEMBER(rta_cacheinfo, rta_tsage) },
504         }
505 };
506 
507 static const uc_nl_nested_spec_t route_metrics_rta = {
508         .headsize = 0,
509         .nattrs = 16,
510         .attrs = {
511                 { RTAX_MTU, "mtu", DT_U32, 0, NULL },
512                 { RTAX_HOPLIMIT, "hoplimit", DT_U32, DF_MAX_255, NULL },
513                 { RTAX_ADVMSS, "advmss", DT_U32, 0, NULL },
514                 { RTAX_REORDERING, "reordering", DT_U32, 0, NULL },
515                 { RTAX_RTT, "rtt", DT_U32, 0, NULL },
516                 { RTAX_WINDOW, "window", DT_U32, 0, NULL },
517                 { RTAX_CWND, "cwnd", DT_U32, 0, NULL },
518                 { RTAX_INITCWND, "initcwnd", DT_U32, 0, NULL },
519                 { RTAX_INITRWND, "initrwnd", DT_U32, 0, NULL },
520                 { RTAX_FEATURES, "ecn", DT_U32, DF_MAX_1, NULL },
521                 { RTAX_QUICKACK, "quickack", DT_U32, DF_MAX_1, NULL },
522                 { RTAX_CC_ALGO, "cc_algo", DT_STRING, 0, NULL },
523                 { RTAX_RTTVAR, "rttvar", DT_U32, 0, NULL },
524                 { RTAX_SSTHRESH, "ssthresh", DT_U32, 0, NULL },
525                 { RTAX_FASTOPEN_NO_COOKIE, "fastopen_no_cookie", DT_U32, DF_MAX_1, NULL },
526                 { RTAX_LOCK, "lock", DT_U32, 0, NULL },
527         }
528 };
529 
530 static const uc_nl_nested_spec_t route_msg = {
531         .headsize = NLA_ALIGN(sizeof(struct rtmsg)),
532         .nattrs = 28,
533         .attrs = {
534                 { RTA_UNSPEC, "family", DT_U8, 0, MEMBER(rtmsg, rtm_family) },
535                 { RTA_UNSPEC, "tos", DT_U8, 0, MEMBER(rtmsg, rtm_tos) },
536                 { RTA_UNSPEC, "protocol", DT_U8, 0, MEMBER(rtmsg, rtm_protocol) },
537                 { RTA_UNSPEC, "scope", DT_U8, 0, MEMBER(rtmsg, rtm_scope) },
538                 { RTA_UNSPEC, "type", DT_U8, 0, MEMBER(rtmsg, rtm_type) },
539                 { RTA_UNSPEC, "flags", DT_U32, 0, MEMBER(rtmsg, rtm_flags) },
540                 { RTA_SRC, "src", DT_ANYADDR, DF_STORE_MASK|DF_FAMILY_HINT, MEMBER(rtmsg, rtm_src_len) },
541                 { RTA_DST, "dst", DT_ANYADDR, DF_STORE_MASK|DF_FAMILY_HINT, MEMBER(rtmsg, rtm_dst_len) },
542                 { RTA_IIF, "iif", DT_NETDEV, 0, NULL },
543                 { RTA_OIF, "oif", DT_NETDEV, 0, NULL },
544                 { RTA_GATEWAY, "gateway", DT_ANYADDR, DF_FAMILY_HINT, NULL },
545                 { RTA_PRIORITY, "priority", DT_U32, 0, NULL },
546                 { RTA_PREFSRC, "prefsrc", DT_ANYADDR, DF_FAMILY_HINT, NULL },
547                 { RTA_METRICS, "metrics", DT_NESTED, 0, &route_metrics_rta },
548                 { RTA_MULTIPATH, "multipath", DT_MULTIPATH, 0, NULL },
549                 { RTA_FLOW, "flow", DT_U32, 0, NULL },
550                 { RTA_CACHEINFO, "cacheinfo", DT_NESTED, DF_NO_SET, &route_cacheinfo_rta },
551                 { RTA_TABLE, "table", DT_U32_OR_MEMBER, DF_MAX_255, MEMBER(rtmsg, rtm_table) },
552                 { RTA_MARK, "mark", DT_U32, 0, NULL },
553                 //RTA_MFC_STATS,
554                 { RTA_PREF, "pref", DT_U8, 0, NULL },
555                 { RTA_ENCAP, "encap", DT_ENCAP, 0, NULL },
556                 { RTA_EXPIRES, "expires", DT_U32, 0, NULL },
557                 { RTA_UID, "uid", DT_U32, 0, NULL },
558                 { RTA_TTL_PROPAGATE, "ttl_propagate", DT_BOOL, 0, NULL },
559                 { RTA_IP_PROTO, "ip_proto", DT_U8, 0, NULL },
560                 { RTA_SPORT, "sport", DT_U16, DF_BYTESWAP, NULL },
561                 { RTA_DPORT, "dport", DT_U16, DF_BYTESWAP, NULL },
562                 { RTA_NH_ID, "nh_id", DT_U32, 0, NULL },
563         }
564 };
565 
566 static const uc_nl_attr_spec_t route_encap_mpls_attrs[] = {
567         { MPLS_IPTUNNEL_DST, "dst", DT_MPLSADDR, 0, NULL },
568         { MPLS_IPTUNNEL_TTL, "ttl", DT_U8, 0, NULL },
569 };
570 
571 static const uc_nl_attr_spec_t route_encap_ip_attrs[] = {
572         { LWTUNNEL_IP_ID, "id", DT_U64, DF_BYTESWAP, NULL },
573         { LWTUNNEL_IP_DST, "dst", DT_INADDR, 0, NULL },
574         { LWTUNNEL_IP_SRC, "src", DT_INADDR, 0, NULL },
575         { LWTUNNEL_IP_TOS, "tos", DT_U8, 0, NULL },
576         { LWTUNNEL_IP_TTL, "ttl", DT_U8, 0, NULL },
577         { LWTUNNEL_IP_OPTS, "opts", DT_IPOPTS, 0, NULL },
578         { LWTUNNEL_IP_FLAGS, "flags", DT_U16, 0, NULL },
579 };
580 
581 static const uc_nl_attr_spec_t route_encap_ila_attrs[] = {
582         { ILA_ATTR_LOCATOR, "locator", DT_U64ADDR, 0, NULL },
583         { ILA_ATTR_CSUM_MODE, "csum_mode", DT_U8, 0, NULL },
584         { ILA_ATTR_IDENT_TYPE, "ident_type", DT_U8, 0, NULL },
585         { ILA_ATTR_HOOK_TYPE, "hook_type", DT_U8, 0, NULL },
586 };
587 
588 static const uc_nl_attr_spec_t route_encap_ip6_attrs[] = {
589         { LWTUNNEL_IP6_ID, "id", DT_U64, DF_BYTESWAP, NULL },
590         { LWTUNNEL_IP6_DST, "dst", DT_IN6ADDR, 0, NULL },
591         { LWTUNNEL_IP6_SRC, "src", DT_IN6ADDR, 0, NULL },
592         { LWTUNNEL_IP6_TC, "tc", DT_U32, 0, NULL },
593         { LWTUNNEL_IP6_HOPLIMIT, "hoplimit", DT_U8, 0, NULL },
594         { LWTUNNEL_IP6_OPTS, "opts", DT_IPOPTS, 0, NULL },
595         { LWTUNNEL_IP6_FLAGS, "flags", DT_U16, 0, NULL },
596 };
597 
598 static const uc_nl_attr_spec_t route_encap_seg6_attrs[] = {
599         { SEG6_IPTUNNEL_SRH, "srh", DT_SRH, 0, NULL },
600 };
601 
602 #define IPV4_DEVCONF_ENTRY(name) ((void *)((IPV4_DEVCONF_##name - 1) * sizeof(uint32_t)))
603 
604 static const uc_nl_nested_spec_t link_attrs_af_spec_inet_devconf_rta = {
605         .headsize = NLA_ALIGN(IPV4_DEVCONF_MAX * sizeof(uint32_t)),
606         .nattrs = 32,
607         .attrs = {
608                 { 0, "forwarding", DT_U32, 0, IPV4_DEVCONF_ENTRY(FORWARDING) },
609                 { 0, "mc_forwarding", DT_U32, 0, IPV4_DEVCONF_ENTRY(MC_FORWARDING) },
610                 { 0, "proxy_arp", DT_U32, 0, IPV4_DEVCONF_ENTRY(PROXY_ARP) },
611                 { 0, "accept_redirects", DT_U32, 0, IPV4_DEVCONF_ENTRY(ACCEPT_REDIRECTS) },
612                 { 0, "secure_redirects", DT_U32, 0, IPV4_DEVCONF_ENTRY(SECURE_REDIRECTS) },
613                 { 0, "send_redirects", DT_U32, 0, IPV4_DEVCONF_ENTRY(SEND_REDIRECTS) },
614                 { 0, "shared_media", DT_U32, 0, IPV4_DEVCONF_ENTRY(SHARED_MEDIA) },
615                 { 0, "rp_filter", DT_U32, 0, IPV4_DEVCONF_ENTRY(RP_FILTER) },
616                 { 0, "accept_source_route", DT_U32, 0, IPV4_DEVCONF_ENTRY(ACCEPT_SOURCE_ROUTE) },
617                 { 0, "bootp_relay", DT_U32, 0, IPV4_DEVCONF_ENTRY(BOOTP_RELAY) },
618                 { 0, "log_martians", DT_U32, 0, IPV4_DEVCONF_ENTRY(LOG_MARTIANS) },
619                 { 0, "tag", DT_U32, 0, IPV4_DEVCONF_ENTRY(TAG) },
620                 { 0, "arpfilter", DT_U32, 0, IPV4_DEVCONF_ENTRY(ARPFILTER) },
621                 { 0, "medium_id", DT_U32, 0, IPV4_DEVCONF_ENTRY(MEDIUM_ID) },
622                 { 0, "noxfrm", DT_U32, 0, IPV4_DEVCONF_ENTRY(NOXFRM) },
623                 { 0, "nopolicy", DT_U32, 0, IPV4_DEVCONF_ENTRY(NOPOLICY) },
624                 { 0, "force_igmp_version", DT_U32, 0, IPV4_DEVCONF_ENTRY(FORCE_IGMP_VERSION) },
625                 { 0, "arp_announce", DT_U32, 0, IPV4_DEVCONF_ENTRY(ARP_ANNOUNCE) },
626                 { 0, "arp_ignore", DT_U32, 0, IPV4_DEVCONF_ENTRY(ARP_IGNORE) },
627                 { 0, "promote_secondaries", DT_U32, 0, IPV4_DEVCONF_ENTRY(PROMOTE_SECONDARIES) },
628                 { 0, "arp_accept", DT_U32, 0, IPV4_DEVCONF_ENTRY(ARP_ACCEPT) },
629                 { 0, "arp_notify", DT_U32, 0, IPV4_DEVCONF_ENTRY(ARP_NOTIFY) },
630                 { 0, "accept_local", DT_U32, 0, IPV4_DEVCONF_ENTRY(ACCEPT_LOCAL) },
631                 { 0, "src_vmark", DT_U32, 0, IPV4_DEVCONF_ENTRY(SRC_VMARK) },
632                 { 0, "proxy_arp_pvlan", DT_U32, 0, IPV4_DEVCONF_ENTRY(PROXY_ARP_PVLAN) },
633                 { 0, "route_localnet", DT_U32, 0, IPV4_DEVCONF_ENTRY(ROUTE_LOCALNET) },
634                 { 0, "igmpv2_unsolicited_report_interval", DT_U32, 0, IPV4_DEVCONF_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL) },
635                 { 0, "igmpv3_unsolicited_report_interval", DT_U32, 0, IPV4_DEVCONF_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL) },
636                 { 0, "ignore_routes_with_linkdown", DT_U32, 0, IPV4_DEVCONF_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN) },
637                 { 0, "drop_unicast_in_l2_multicast", DT_U32, 0, IPV4_DEVCONF_ENTRY(DROP_UNICAST_IN_L2_MULTICAST) },
638                 { 0, "drop_gratuitous_arp", DT_U32, 0, IPV4_DEVCONF_ENTRY(DROP_GRATUITOUS_ARP) },
639                 { 0, "bc_forwarding", DT_U32, 0, IPV4_DEVCONF_ENTRY(BC_FORWARDING) },
640         }
641 };
642 
643 static const uc_nl_nested_spec_t link_attrs_af_spec_inet_rta = {
644         .headsize = 0,
645         .nattrs = 1,
646         .attrs = {
647                 { IFLA_INET_CONF, "conf", DT_NESTED, 0, &link_attrs_af_spec_inet_devconf_rta },
648         }
649 };
650 
651 #define IPV6_DEVCONF_ENTRY(name) ((void *)(DEVCONF_##name * sizeof(uint32_t)))
652 
653 static const uc_nl_nested_spec_t link_attrs_af_spec_inet6_devconf_rta = {
654         .headsize = NLA_ALIGN(DEVCONF_MAX * sizeof(uint32_t)),
655         .nattrs = 53,
656         .attrs = {
657                 { 0, "forwarding", DT_S32, 0, IPV6_DEVCONF_ENTRY(FORWARDING) },
658                 { 0, "hoplimit", DT_S32, 0, IPV6_DEVCONF_ENTRY(HOPLIMIT) },
659                 { 0, "mtu6", DT_S32, 0, IPV6_DEVCONF_ENTRY(MTU6) },
660                 { 0, "accept_ra", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA) },
661                 { 0, "accept_redirects", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_REDIRECTS) },
662                 { 0, "autoconf", DT_S32, 0, IPV6_DEVCONF_ENTRY(AUTOCONF) },
663                 { 0, "dad_transmits", DT_S32, 0, IPV6_DEVCONF_ENTRY(DAD_TRANSMITS) },
664                 { 0, "rtr_solicits", DT_S32, 0, IPV6_DEVCONF_ENTRY(RTR_SOLICITS) },
665                 { 0, "rtr_solicit_interval", DT_S32, 0, IPV6_DEVCONF_ENTRY(RTR_SOLICIT_INTERVAL) },
666                 { 0, "rtr_solicit_delay", DT_S32, 0, IPV6_DEVCONF_ENTRY(RTR_SOLICIT_DELAY) },
667                 { 0, "use_tempaddr", DT_S32, 0, IPV6_DEVCONF_ENTRY(USE_TEMPADDR) },
668                 { 0, "temp_valid_lft", DT_S32, 0, IPV6_DEVCONF_ENTRY(TEMP_VALID_LFT) },
669                 { 0, "temp_prefered_lft", DT_S32, 0, IPV6_DEVCONF_ENTRY(TEMP_PREFERED_LFT) },
670                 { 0, "regen_max_retry", DT_S32, 0, IPV6_DEVCONF_ENTRY(REGEN_MAX_RETRY) },
671                 { 0, "max_desync_factor", DT_S32, 0, IPV6_DEVCONF_ENTRY(MAX_DESYNC_FACTOR) },
672                 { 0, "max_addresses", DT_S32, 0, IPV6_DEVCONF_ENTRY(MAX_ADDRESSES) },
673                 { 0, "force_mld_version", DT_S32, 0, IPV6_DEVCONF_ENTRY(FORCE_MLD_VERSION) },
674                 { 0, "accept_ra_defrtr", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA_DEFRTR) },
675                 { 0, "accept_ra_pinfo", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA_PINFO) },
676                 { 0, "accept_ra_rtr_pref", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA_RTR_PREF) },
677                 { 0, "rtr_probe_interval", DT_S32, 0, IPV6_DEVCONF_ENTRY(RTR_PROBE_INTERVAL) },
678                 { 0, "accept_ra_rt_info_max_plen", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA_RT_INFO_MAX_PLEN) },
679                 { 0, "proxy_ndp", DT_S32, 0, IPV6_DEVCONF_ENTRY(PROXY_NDP) },
680                 { 0, "optimistic_dad", DT_S32, 0, IPV6_DEVCONF_ENTRY(OPTIMISTIC_DAD) },
681                 { 0, "accept_source_route", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_SOURCE_ROUTE) },
682                 { 0, "mc_forwarding", DT_S32, 0, IPV6_DEVCONF_ENTRY(MC_FORWARDING) },
683                 { 0, "disable_ipv6", DT_S32, 0, IPV6_DEVCONF_ENTRY(DISABLE_IPV6) },
684                 { 0, "accept_dad", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_DAD) },
685                 { 0, "force_tllao", DT_S32, 0, IPV6_DEVCONF_ENTRY(FORCE_TLLAO) },
686                 { 0, "ndisc_notify", DT_S32, 0, IPV6_DEVCONF_ENTRY(NDISC_NOTIFY) },
687                 { 0, "mldv1_unsolicited_report_interval", DT_S32, 0, IPV6_DEVCONF_ENTRY(MLDV1_UNSOLICITED_REPORT_INTERVAL) },
688                 { 0, "mldv2_unsolicited_report_interval", DT_S32, 0, IPV6_DEVCONF_ENTRY(MLDV2_UNSOLICITED_REPORT_INTERVAL) },
689                 { 0, "suppress_frag_ndisc", DT_S32, 0, IPV6_DEVCONF_ENTRY(SUPPRESS_FRAG_NDISC) },
690                 { 0, "accept_ra_from_local", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA_FROM_LOCAL) },
691                 { 0, "use_optimistic", DT_S32, 0, IPV6_DEVCONF_ENTRY(USE_OPTIMISTIC) },
692                 { 0, "accept_ra_mtu", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA_MTU) },
693                 { 0, "stable_secret", DT_S32, 0, IPV6_DEVCONF_ENTRY(STABLE_SECRET) },
694                 { 0, "use_oif_addrs_only", DT_S32, 0, IPV6_DEVCONF_ENTRY(USE_OIF_ADDRS_ONLY) },
695                 { 0, "accept_ra_min_hop_limit", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA_MIN_HOP_LIMIT) },
696                 { 0, "ignore_routes_with_linkdown", DT_S32, 0, IPV6_DEVCONF_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN) },
697                 { 0, "drop_unicast_in_l2_multicast", DT_S32, 0, IPV6_DEVCONF_ENTRY(DROP_UNICAST_IN_L2_MULTICAST) },
698                 { 0, "drop_unsolicited_na", DT_S32, 0, IPV6_DEVCONF_ENTRY(DROP_UNSOLICITED_NA) },
699                 { 0, "keep_addr_on_down", DT_S32, 0, IPV6_DEVCONF_ENTRY(KEEP_ADDR_ON_DOWN) },
700                 { 0, "rtr_solicit_max_interval", DT_S32, 0, IPV6_DEVCONF_ENTRY(RTR_SOLICIT_MAX_INTERVAL) },
701                 { 0, "seg6_enabled", DT_S32, 0, IPV6_DEVCONF_ENTRY(SEG6_ENABLED) },
702                 { 0, "seg6_require_hmac", DT_S32, 0, IPV6_DEVCONF_ENTRY(SEG6_REQUIRE_HMAC) },
703                 { 0, "enhanced_dad", DT_S32, 0, IPV6_DEVCONF_ENTRY(ENHANCED_DAD) },
704                 { 0, "addr_gen_mode", DT_S32, 0, IPV6_DEVCONF_ENTRY(ADDR_GEN_MODE) },
705                 { 0, "disable_policy", DT_S32, 0, IPV6_DEVCONF_ENTRY(DISABLE_POLICY) },
706                 { 0, "accept_ra_rt_info_min_plen", DT_S32, 0, IPV6_DEVCONF_ENTRY(ACCEPT_RA_RT_INFO_MIN_PLEN) },
707                 { 0, "ndisc_tclass", DT_S32, 0, IPV6_DEVCONF_ENTRY(NDISC_TCLASS) },
708                 { 0, "rpl_seg_enabled", DT_S32, 0, IPV6_DEVCONF_ENTRY(RPL_SEG_ENABLED) },
709                 { 0, "ra_defrtr_metric", DT_S32, 0, IPV6_DEVCONF_ENTRY(RA_DEFRTR_METRIC) },
710         }
711 };
712 
713 static const uc_nl_nested_spec_t link_attrs_af_spec_inet6_rta = {
714         .headsize = 0,
715         .nattrs = 3,
716         .attrs = {
717                 { IFLA_INET6_ADDR_GEN_MODE, "mode", DT_U8, 0, NULL },
718                 { IFLA_INET6_FLAGS, "flags", DT_U32, DF_NO_SET, NULL },
719                 { IFLA_INET6_CONF, "conf", DT_NESTED, DF_NO_SET, &link_attrs_af_spec_inet6_devconf_rta },
720         }
721 };
722 
723 static const uc_nl_nested_spec_t link_attrs_af_spec_rta = {
724         .headsize = 0,
725         .nattrs = 2,
726         .attrs = {
727                 { AF_INET, "inet", DT_NESTED, DF_NO_SET, &link_attrs_af_spec_inet_rta },
728                 { AF_INET6, "inet6", DT_NESTED, 0, &link_attrs_af_spec_inet6_rta },
729         }
730 };
731 
732 static const uc_nl_nested_spec_t link_attrs_stats64_rta = {
733         .headsize = NLA_ALIGN(sizeof(struct rtnl_link_stats64)),
734         .nattrs = 24,
735         .attrs = {
736                 { IFLA_UNSPEC, "rx_packets", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_packets) },
737                 { IFLA_UNSPEC, "tx_packets", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_packets) },
738                 { IFLA_UNSPEC, "rx_bytes", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_bytes) },
739                 { IFLA_UNSPEC, "tx_bytes", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_bytes) },
740                 { IFLA_UNSPEC, "rx_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_errors) },
741                 { IFLA_UNSPEC, "tx_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_errors) },
742                 { IFLA_UNSPEC, "rx_dropped", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_dropped) },
743                 { IFLA_UNSPEC, "tx_dropped", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_dropped) },
744                 { IFLA_UNSPEC, "multicast", DT_U64, 0, MEMBER(rtnl_link_stats64, multicast) },
745                 { IFLA_UNSPEC, "collisions", DT_U64, 0, MEMBER(rtnl_link_stats64, collisions) },
746                 { IFLA_UNSPEC, "rx_length_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_length_errors) },
747                 { IFLA_UNSPEC, "rx_over_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_over_errors) },
748                 { IFLA_UNSPEC, "rx_crc_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_crc_errors) },
749                 { IFLA_UNSPEC, "rx_frame_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_frame_errors) },
750                 { IFLA_UNSPEC, "rx_fifo_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_fifo_errors) },
751                 { IFLA_UNSPEC, "rx_missed_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_missed_errors) },
752                 { IFLA_UNSPEC, "tx_aborted_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_aborted_errors) },
753                 { IFLA_UNSPEC, "tx_carrier_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_carrier_errors) },
754                 { IFLA_UNSPEC, "tx_fifo_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_fifo_errors) },
755                 { IFLA_UNSPEC, "tx_heartbeat_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_heartbeat_errors) },
756                 { IFLA_UNSPEC, "tx_window_errors", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_window_errors) },
757                 { IFLA_UNSPEC, "rx_compressed", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_compressed) },
758                 { IFLA_UNSPEC, "tx_compressed", DT_U64, 0, MEMBER(rtnl_link_stats64, tx_compressed) },
759                 { IFLA_UNSPEC, "rx_nohandler", DT_U64, 0, MEMBER(rtnl_link_stats64, rx_nohandler) },
760         }
761 };
762 
763 static const uc_nl_nested_spec_t link_msg = {
764         .headsize = NLA_ALIGN(sizeof(struct ifinfomsg)),
765         .nattrs = 26,
766         .attrs = {
767                 { IFLA_UNSPEC, "family", DT_U8, 0, MEMBER(ifinfomsg, ifi_family) },
768                 { IFLA_UNSPEC, "type", DT_U16, 0, MEMBER(ifinfomsg, ifi_type) },
769                 { IFLA_UNSPEC, "dev", DT_NETDEV, 0, MEMBER(ifinfomsg, ifi_index) },
770                 { IFLA_UNSPEC, "flags", DT_FLAGS, 0, MEMBER(ifinfomsg, ifi_flags) },
771                 { IFLA_UNSPEC, "change", DT_FLAGS, 0, MEMBER(ifinfomsg, ifi_change) },
772                 { IFLA_ADDRESS, "address", DT_LLADDR, 0, NULL },
773                 { IFLA_BROADCAST, "broadcast", DT_LLADDR, 0, NULL },
774                 { IFLA_TXQLEN, "txqlen", DT_U32, 0, NULL },
775                 { IFLA_MTU, "mtu", DT_U32, 0, NULL },
776                 { IFLA_CARRIER, "carrier", DT_BOOL, 0, NULL },
777                 { IFLA_MASTER, "master", DT_NETDEV, DF_ALLOW_NONE, NULL },
778                 { IFLA_IFALIAS, "ifalias", DT_STRING, 0, NULL },
779                 { IFLA_LINKMODE, "linkmode", DT_U8, 0, NULL },
780                 { IFLA_OPERSTATE, "operstate", DT_U8, 0, NULL },
781                 { IFLA_NUM_TX_QUEUES, "num_tx_queues", DT_U32, 0, NULL },
782                 { IFLA_NUM_RX_QUEUES, "num_rx_queues", DT_U32, 0, NULL },
783                 { IFLA_AF_SPEC, "af_spec", DT_AFSPEC, 0, NULL },
784                 { IFLA_LINK_NETNSID, "link_netnsid", DT_U32, 0, NULL },
785                 { IFLA_TARGET_NETNSID, "target_netnsid", DT_S32, 0, NULL },
786                 { IFLA_PROTO_DOWN, "proto_down", DT_BOOL, 0, NULL },
787                 { IFLA_GROUP, "group", DT_U32, 0, NULL },
788                 { IFLA_LINK, "link", DT_NETDEV, 0, NULL },
789                 { IFLA_IFNAME, "ifname", DT_STRING, 0, NULL },
790                 { IFLA_LINKINFO, "linkinfo", DT_LINKINFO, 0, NULL }, /* XXX: DF_NO_GET ? */
791                 { IFLA_EXT_MASK, "ext_mask", DT_U32, 0, NULL },
792                 { IFLA_STATS64, "stats64", DT_NESTED, DF_NO_SET, &link_attrs_stats64_rta },
793                 /* TODO: IFLA_VFINFO_LIST */
794                 /* TODO: the following two should be straightforward, just uncomment and test */
795                 /* { IFLA_NET_NS_PID, "net_ns_pid", DT_S32, 0, NULL }, */
796                 /* { IFLA_NET_NS_FD, "net_ns_fd", DT_S32, 0, NULL }, */
797         }
798 };
799 
800 static const uc_nl_attr_spec_t link_bareudp_attrs[] = {
801         { IFLA_BAREUDP_ETHERTYPE, "ethertype", DT_U16, 0, NULL },
802         { IFLA_BAREUDP_MULTIPROTO_MODE, "multiproto_mode", DT_FLAG, 0, NULL },
803         { IFLA_BAREUDP_PORT, "port", DT_U16, 0, NULL },
804         { IFLA_BAREUDP_SRCPORT_MIN, "srcport_min", DT_U16, 0, NULL },
805 };
806 
807 static const uc_nl_nested_spec_t link_bond_ad_info_rta = {
808         .headsize = 0,
809         .nattrs = 5,
810         .attrs = {
811                 { IFLA_BOND_AD_INFO_ACTOR_KEY, "ad_info_actor_key", DT_U16, DF_NO_SET, NULL },
812                 { IFLA_BOND_AD_INFO_AGGREGATOR, "ad_info_aggregator", DT_U16, DF_NO_SET, NULL },
813                 { IFLA_BOND_AD_INFO_NUM_PORTS, "ad_info_num_ports", DT_U16, DF_NO_SET, NULL },
814                 { IFLA_BOND_AD_INFO_PARTNER_KEY, "ad_info_partner_key", DT_U16, DF_NO_SET, NULL },
815                 { IFLA_BOND_AD_INFO_PARTNER_MAC, "ad_info_partner_mac", DT_LLADDR, DF_NO_SET, NULL },
816         }
817 };
818 
819 static const uc_nl_attr_spec_t link_bond_attrs[] = {
820         { IFLA_BOND_ACTIVE_SLAVE, "active_slave", DT_NETDEV, DF_ALLOW_NONE, NULL },
821         { IFLA_BOND_AD_ACTOR_SYSTEM, "ad_actor_system", DT_LLADDR, 0, NULL },
822         { IFLA_BOND_AD_ACTOR_SYS_PRIO, "ad_actor_sys_prio", DT_U16, 0, NULL },
823         { IFLA_BOND_AD_INFO, "ad_info", DT_NESTED, DF_NO_SET, &link_bond_ad_info_rta },
824         { IFLA_BOND_AD_LACP_RATE, "ad_lacp_rate", DT_U8, 0, NULL },
825         { IFLA_BOND_AD_SELECT, "ad_select", DT_U8, 0, NULL },
826         { IFLA_BOND_AD_USER_PORT_KEY, "ad_user_port_key", DT_U16, 0, NULL },
827         { IFLA_BOND_ALL_SLAVES_ACTIVE, "all_slaves_active", DT_U8, 0, NULL },
828         { IFLA_BOND_ARP_ALL_TARGETS, "arp_all_targets", DT_U32, 0, NULL },
829         { IFLA_BOND_ARP_INTERVAL, "arp_interval", DT_U32, 0, NULL },
830         { IFLA_BOND_ARP_IP_TARGET, "arp_ip_target", DT_INADDR, DF_MULTIPLE, NULL },
831         { IFLA_BOND_ARP_VALIDATE, "arp_validate", DT_U32, 0, NULL },
832         { IFLA_BOND_DOWNDELAY, "downdelay", DT_U32, 0, NULL },
833         { IFLA_BOND_FAIL_OVER_MAC, "fail_over_mac", DT_U8, 0, NULL },
834         { IFLA_BOND_LP_INTERVAL, "lp_interval", DT_U32, 0, NULL },
835         { IFLA_BOND_MIIMON, "miimon", DT_U32, 0, NULL },
836         { IFLA_BOND_MIN_LINKS, "min_links", DT_U32, 0, NULL },
837         { IFLA_BOND_MODE, "mode", DT_U8, 0, NULL },
838         { IFLA_BOND_NUM_PEER_NOTIF, "num_peer_notif", DT_U8, 0, NULL },
839         { IFLA_BOND_PACKETS_PER_SLAVE, "packets_per_slave", DT_U32, 0, NULL },
840         { IFLA_BOND_PRIMARY, "primary", DT_NETDEV, 0, NULL },
841         { IFLA_BOND_PRIMARY_RESELECT, "primary_reselect", DT_U8, 0, NULL },
842         { IFLA_BOND_RESEND_IGMP, "resend_igmp", DT_U32, 0, NULL },
843         { IFLA_BOND_TLB_DYNAMIC_LB, "tlb_dynamic_lb", DT_U8, 0, NULL },
844         { IFLA_BOND_UPDELAY, "updelay", DT_U32, 0, NULL },
845         { IFLA_BOND_USE_CARRIER, "use_carrier", DT_U8, 0, NULL },
846         { IFLA_BOND_XMIT_HASH_POLICY, "xmit_hash_policy", DT_U8, 0, NULL },
847 };
848 
849 static const uc_nl_attr_spec_t link_bond_slave_attrs[] = {
850         { IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE, "ad_actor_oper_port_state", DT_U8, DF_NO_SET, NULL },
851         { IFLA_BOND_SLAVE_AD_AGGREGATOR_ID, "ad_aggregator_id", DT_U16, DF_NO_SET, NULL },
852         { IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE, "ad_partner_oper_port_state", DT_U8, DF_NO_SET, NULL },
853         { IFLA_BOND_SLAVE_LINK_FAILURE_COUNT, "link_failure_count", DT_U32, DF_NO_SET, NULL },
854         { IFLA_BOND_SLAVE_MII_STATUS, "mii_status", DT_U8, DF_NO_SET, NULL },
855         { IFLA_BOND_SLAVE_PERM_HWADDR, "perm_hwaddr", DT_LLADDR, DF_NO_SET, NULL },
856         { IFLA_BOND_SLAVE_QUEUE_ID, "queue_id", DT_U16, 0, NULL },
857         { IFLA_BOND_SLAVE_STATE, "state", DT_U8, DF_NO_SET, NULL },
858 };
859 
860 static const uc_nl_attr_spec_t link_bridge_attrs[] = {
861         { IFLA_BR_AGEING_TIME, "ageing_time", DT_U32, 0, NULL },
862         { IFLA_BR_BRIDGE_ID, "bridge_id", DT_BRIDGEID, DF_NO_SET, NULL },
863         { IFLA_BR_FDB_FLUSH, "fdb_flush", DT_FLAG, DF_NO_GET, NULL },
864         { IFLA_BR_FORWARD_DELAY, "forward_delay", DT_U32, 0, NULL },
865         { IFLA_BR_GC_TIMER, "gc_timer", DT_U64, DF_NO_SET, NULL },
866         { IFLA_BR_GROUP_ADDR, "group_addr", DT_LLADDR, 0, NULL },
867         { IFLA_BR_GROUP_FWD_MASK, "group_fwd_mask", DT_U16, 0, NULL },
868         { IFLA_BR_HELLO_TIME, "hello_time", DT_U32, 0, NULL },
869         { IFLA_BR_HELLO_TIMER, "hello_timer", DT_U64, DF_NO_SET, NULL },
870         { IFLA_BR_MAX_AGE, "max_age", DT_U32, 0, NULL },
871         { IFLA_BR_MCAST_HASH_ELASTICITY, "mcast_hash_elasticity", DT_U32, 0, NULL },
872         { IFLA_BR_MCAST_HASH_MAX, "mcast_hash_max", DT_U32, 0, NULL },
873         { IFLA_BR_MCAST_IGMP_VERSION, "mcast_igmp_version", DT_U8, 0, NULL },
874         { IFLA_BR_MCAST_LAST_MEMBER_CNT, "mcast_last_member_cnt", DT_U32, 0, NULL },
875         { IFLA_BR_MCAST_LAST_MEMBER_INTVL, "mcast_last_member_intvl", DT_U64, 0, NULL },
876         { IFLA_BR_MCAST_MEMBERSHIP_INTVL, "mcast_membership_intvl", DT_U64, 0, NULL },
877         { IFLA_BR_MCAST_MLD_VERSION, "mcast_mld_version", DT_U8, 0, NULL },
878         { IFLA_BR_MCAST_QUERIER, "mcast_querier", DT_U8, 0, NULL },
879         { IFLA_BR_MCAST_QUERIER_INTVL, "mcast_querier_intvl", DT_U64, 0, NULL },
880         { IFLA_BR_MCAST_QUERY_INTVL, "mcast_query_intvl", DT_U64, 0, NULL },
881         { IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, "mcast_query_response_intvl", DT_U64, 0, NULL },
882         { IFLA_BR_MCAST_QUERY_USE_IFADDR, "mcast_query_use_ifaddr", DT_U8, 0, NULL },
883         { IFLA_BR_MCAST_ROUTER, "mcast_router", DT_U8, 0, NULL },
884         { IFLA_BR_MCAST_SNOOPING, "mcast_snooping", DT_U8, 0, NULL },
885         { IFLA_BR_MCAST_STARTUP_QUERY_CNT, "mcast_startup_query_cnt", DT_U32, 0, NULL },
886         { IFLA_BR_MCAST_STARTUP_QUERY_INTVL, "mcast_startup_query_intvl", DT_U64, 0, NULL },
887         { IFLA_BR_MCAST_STATS_ENABLED, "mcast_stats_enabled", DT_U8, 0, NULL },
888         { IFLA_BR_NF_CALL_ARPTABLES, "nf_call_arptables", DT_U8, 0, NULL },
889         { IFLA_BR_NF_CALL_IP6TABLES, "nf_call_ip6tables", DT_U8, 0, NULL },
890         { IFLA_BR_NF_CALL_IPTABLES, "nf_call_iptables", DT_U8, 0, NULL },
891         { IFLA_BR_PRIORITY, "priority", DT_U16, 0, NULL },
892         { IFLA_BR_ROOT_ID, "root_id", DT_BRIDGEID, DF_NO_SET, NULL },
893         { IFLA_BR_ROOT_PATH_COST, "root_path_cost", DT_U32, DF_NO_SET, NULL },
894         { IFLA_BR_ROOT_PORT, "root_port", DT_U16, DF_NO_SET, NULL },
895         { IFLA_BR_STP_STATE, "stp_state", DT_U32, 0, NULL },
896         { IFLA_BR_TCN_TIMER, "tcn_timer", DT_U64, DF_NO_SET, NULL },
897         { IFLA_BR_TOPOLOGY_CHANGE, "topology_change", DT_U8, DF_NO_SET, NULL },
898         { IFLA_BR_TOPOLOGY_CHANGE_DETECTED, "topology_change_detected", DT_U8, DF_NO_SET, NULL },
899         { IFLA_BR_TOPOLOGY_CHANGE_TIMER, "topology_change_timer", DT_U64, DF_NO_SET, NULL },
900         { IFLA_BR_VLAN_DEFAULT_PVID, "vlan_default_pvid", DT_U16, 0, NULL },
901         { IFLA_BR_VLAN_FILTERING, "vlan_filtering", DT_U8, 0, NULL },
902         { IFLA_BR_VLAN_PROTOCOL, "vlan_protocol", DT_U16, 0, NULL },
903         { IFLA_BR_VLAN_STATS_ENABLED, "vlan_stats_enabled", DT_U8, 0, NULL },
904 };
905 
906 static const uc_nl_attr_spec_t link_bridge_slave_attrs[] = {
907         { IFLA_BRPORT_BACKUP_PORT, "backup_port", DT_NETDEV, 0, NULL },
908         //{ IFLA_BRPORT_BCAST_FLOOD, "bcast-flood", DT_??, 0, NULL },
909         { IFLA_BRPORT_BRIDGE_ID, "bridge_id", DT_BRIDGEID, DF_NO_SET, NULL },
910         { IFLA_BRPORT_CONFIG_PENDING, "config_pending", DT_U8, DF_NO_SET, NULL },
911         { IFLA_BRPORT_COST, "cost", DT_U32, 0, NULL },
912         { IFLA_BRPORT_DESIGNATED_COST, "designated_cost", DT_U16, DF_NO_SET, NULL },
913         { IFLA_BRPORT_DESIGNATED_PORT, "designated_port", DT_U16, DF_NO_SET, NULL },
914         { IFLA_BRPORT_FAST_LEAVE, "fast_leave", DT_U8, 0, NULL },
915         { IFLA_BRPORT_FLUSH, "flush", DT_FLAG, DF_NO_GET, NULL },
916         { IFLA_BRPORT_FORWARD_DELAY_TIMER, "forward_delay_timer", DT_U64, DF_NO_SET, NULL },
917         { IFLA_BRPORT_GROUP_FWD_MASK, "group_fwd_mask", DT_U16, 0, NULL },
918         { IFLA_BRPORT_GUARD, "guard", DT_U8, 0, NULL },
919         { IFLA_BRPORT_HOLD_TIMER, "hold_timer", DT_U64, DF_NO_SET, NULL },
920         { IFLA_BRPORT_ID, "id", DT_U16, DF_NO_SET, NULL },
921         { IFLA_BRPORT_ISOLATED, "isolated", DT_U8, 0, NULL },
922         { IFLA_BRPORT_LEARNING, "learning", DT_U8, 0, NULL },
923         { IFLA_BRPORT_LEARNING_SYNC, "learning_sync", DT_U8, 0, NULL },
924         { IFLA_BRPORT_MCAST_FLOOD, "mcast_flood", DT_U8, 0, NULL },
925         { IFLA_BRPORT_MCAST_TO_UCAST, "mcast_to_ucast", DT_U8, 0, NULL },
926         { IFLA_BRPORT_MESSAGE_AGE_TIMER, "message_age_timer", DT_U64, DF_NO_SET, NULL },
927         { IFLA_BRPORT_MODE, "mode", DT_U8, 0, NULL },
928         { IFLA_BRPORT_MULTICAST_ROUTER, "multicast_router", DT_U8, 0, NULL },
929         { IFLA_BRPORT_NEIGH_SUPPRESS, "neigh_suppress", DT_U8, 0, NULL },
930         { IFLA_BRPORT_NO, "no", DT_U16, DF_NO_SET, NULL },
931         { IFLA_BRPORT_PRIORITY, "priority", DT_U16, 0, NULL },
932         { IFLA_BRPORT_PROTECT, "protect", DT_U8, 0, NULL },
933         { IFLA_BRPORT_PROXYARP, "proxyarp", DT_U8, DF_NO_SET, NULL },
934         { IFLA_BRPORT_PROXYARP_WIFI, "proxyarp_wifi", DT_U8, DF_NO_SET, NULL },
935         { IFLA_BRPORT_ROOT_ID, "root_id", DT_BRIDGEID, DF_NO_SET, NULL },
936         { IFLA_BRPORT_STATE, "state", DT_U8, 0, NULL },
937         { IFLA_BRPORT_TOPOLOGY_CHANGE_ACK, "topology_change_ack", DT_U8, DF_NO_SET, NULL },
938         { IFLA_BRPORT_UNICAST_FLOOD, "unicast_flood", DT_U8, 0, NULL },
939         { IFLA_BRPORT_VLAN_TUNNEL, "vlan_tunnel", DT_U8, 0, NULL },
940 };
941 
942 static const uc_nl_nested_spec_t link_can_bittiming_rta = {
943         .headsize = NLA_ALIGN(sizeof(struct can_bittiming)),
944         .nattrs = 8,
945         .attrs = {
946                 { RTA_UNSPEC, "bitrate", DT_U32, 0, MEMBER(can_bittiming, bitrate) },
947                 { RTA_UNSPEC, "sample_point", DT_U32, 0, MEMBER(can_bittiming, sample_point) },
948                 { RTA_UNSPEC, "tq", DT_U32, 0, MEMBER(can_bittiming, tq) },
949                 { RTA_UNSPEC, "prop_seg", DT_U32, 0, MEMBER(can_bittiming, prop_seg) },
950                 { RTA_UNSPEC, "phase_seg1", DT_U32, 0, MEMBER(can_bittiming, phase_seg1) },
951                 { RTA_UNSPEC, "phase_seg2", DT_U32, 0, MEMBER(can_bittiming, phase_seg2) },
952                 { RTA_UNSPEC, "sjw", DT_U32, 0, MEMBER(can_bittiming, sjw) },
953                 { RTA_UNSPEC, "brp", DT_U32, 0, MEMBER(can_bittiming, brp) },
954         }
955 };
956 
957 static const uc_nl_nested_spec_t link_can_bittiming_const_rta = {
958         .headsize = NLA_ALIGN(sizeof(struct can_bittiming_const)),
959         .nattrs = 8,
960         .attrs = {
961                 { RTA_UNSPEC, "tseg1_min", DT_U32, 0, MEMBER(can_bittiming_const, tseg1_min) },
962                 { RTA_UNSPEC, "tseg1_max", DT_U32, 0, MEMBER(can_bittiming_const, tseg1_max) },
963                 { RTA_UNSPEC, "tseg2_min", DT_U32, 0, MEMBER(can_bittiming_const, tseg2_min) },
964                 { RTA_UNSPEC, "tseg2_max", DT_U32, 0, MEMBER(can_bittiming_const, tseg2_max) },
965                 { RTA_UNSPEC, "sjw_max", DT_U32, 0, MEMBER(can_bittiming_const, sjw_max) },
966                 { RTA_UNSPEC, "brp_min", DT_U32, 0, MEMBER(can_bittiming_const, brp_min) },
967                 { RTA_UNSPEC, "brp_max", DT_U32, 0, MEMBER(can_bittiming_const, brp_max) },
968                 { RTA_UNSPEC, "brp_inc", DT_U32, 0, MEMBER(can_bittiming_const, brp_inc) },
969         }
970 };
971 
972 static const uc_nl_nested_spec_t link_can_clock_rta = {
973         .headsize = NLA_ALIGN(sizeof(struct can_clock)),
974         .nattrs = 1,
975         .attrs = {
976                 { RTA_UNSPEC, "freq", DT_U32, 0, MEMBER(can_clock, freq) },
977         }
978 };
979 
980 static const uc_nl_nested_spec_t link_can_ctrlmode_rta = {
981         .headsize = NLA_ALIGN(sizeof(struct can_ctrlmode)),
982         .nattrs = 2,
983         .attrs = {
984                 { RTA_UNSPEC, "mask", DT_U32, 0, MEMBER(can_ctrlmode, mask) },
985                 { RTA_UNSPEC, "flags", DT_U32, 0, MEMBER(can_ctrlmode, flags) },
986         }
987 };
988 
989 static const uc_nl_nested_spec_t link_can_berr_counter_rta = {
990         .headsize = NLA_ALIGN(sizeof(struct can_berr_counter)),
991         .nattrs = 2,
992         .attrs = {
993                 { RTA_UNSPEC, "txerr", DT_U16, 0, MEMBER(can_berr_counter, txerr) },
994                 { RTA_UNSPEC, "rxerr", DT_U16, 0, MEMBER(can_berr_counter, rxerr) },
995         }
996 };
997 
998 static const uc_nl_attr_spec_t link_can_attrs[] = {
999         { IFLA_CAN_BERR_COUNTER, "berr_counter", DT_NESTED, DF_NO_SET, &link_can_berr_counter_rta },
1000         { IFLA_CAN_BITTIMING, "bittiming", DT_NESTED, 0, &link_can_bittiming_rta },
1001         { IFLA_CAN_BITTIMING_CONST, "bittiming_const", DT_NESTED, DF_NO_SET, &link_can_bittiming_const_rta },
1002         { IFLA_CAN_CLOCK, "clock", DT_NESTED, DF_NO_SET, &link_can_clock_rta },
1003         { IFLA_CAN_CTRLMODE, "ctrlmode", DT_NESTED, 0, &link_can_ctrlmode_rta },
1004         { IFLA_CAN_DATA_BITTIMING, "data_bittiming", DT_NESTED, 0, &link_can_bittiming_rta },
1005         { IFLA_CAN_DATA_BITTIMING_CONST, "data_bittiming_const", DT_NESTED, DF_NO_SET, &link_can_bittiming_const_rta },
1006         { IFLA_CAN_RESTART, "restart", DT_U32, DF_NO_GET, NULL },
1007         { IFLA_CAN_RESTART_MS, "restart_ms", DT_U32, 0, NULL },
1008         { IFLA_CAN_STATE, "state", DT_U32, DF_NO_SET, NULL },
1009         { IFLA_CAN_TERMINATION, "termination", DT_U16, 0, NULL },
1010 };
1011 
1012 static const uc_nl_attr_spec_t link_geneve_attrs[] = {
1013         { IFLA_GENEVE_COLLECT_METADATA, "collect_metadata", DT_FLAG, DF_NO_GET, NULL },
1014         { IFLA_GENEVE_ID, "id", DT_U32, 0, NULL },
1015         { IFLA_GENEVE_LABEL, "label", DT_U32, 0, NULL },
1016         { IFLA_GENEVE_PORT, "port", DT_U16, 0, NULL },
1017         { IFLA_GENEVE_REMOTE, "remote", DT_INADDR, 0, NULL },
1018         { IFLA_GENEVE_REMOTE6, "remote6", DT_IN6ADDR, 0, NULL },
1019         { IFLA_GENEVE_TOS, "tos", DT_U8, 0, NULL },
1020         { IFLA_GENEVE_TTL, "ttl", DT_U8, 0, NULL },
1021         { IFLA_GENEVE_UDP_CSUM, "udp_csum", DT_U8, 0, NULL },
1022         { IFLA_GENEVE_UDP_ZERO_CSUM6_RX, "udp_zero_csum6_rx", DT_U8, 0, NULL },
1023         { IFLA_GENEVE_UDP_ZERO_CSUM6_TX, "udp_zero_csum6_tx", DT_U8, 0, NULL },
1024 };
1025 
1026 static const uc_nl_attr_spec_t link_hsr_attrs[] = {
1027         { IFLA_HSR_MULTICAST_SPEC, "multicast_spec", DT_STRING, DF_NO_GET, NULL },
1028         { IFLA_HSR_SEQ_NR, "seq_nr", DT_U16, DF_NO_SET, NULL },
1029         { IFLA_HSR_SLAVE1, "slave1", DT_NETDEV, 0, NULL },
1030         { IFLA_HSR_SLAVE2, "slave2", DT_NETDEV, 0, NULL },
1031         { IFLA_HSR_SUPERVISION_ADDR, "supervision_addr", DT_LLADDR, DF_NO_SET, NULL },
1032         { IFLA_HSR_VERSION, "version", DT_STRING, DF_NO_GET, NULL },
1033 };
1034 
1035 static const uc_nl_attr_spec_t link_ipoib_attrs[] = {
1036         { IFLA_IPOIB_MODE, "mode", DT_U16, 0, NULL },
1037         { IFLA_IPOIB_PKEY, "pkey", DT_U16, 0, NULL },
1038         { IFLA_IPOIB_UMCAST, "umcast", DT_U16, 0, NULL },
1039 };
1040 
1041 static const uc_nl_attr_spec_t link_ipvlan_attrs[] = {
1042         { IFLA_IPVLAN_FLAGS, "flags", DT_U16, 0, NULL },
1043         { IFLA_IPVLAN_MODE, "mode", DT_U16, 0, NULL },
1044 };
1045 
1046 static const uc_nl_attr_spec_t link_macvlan_attrs[] = {
1047         { IFLA_MACVLAN_FLAGS, "flags", DT_U16, 0, NULL },
1048         { IFLA_MACVLAN_MACADDR, "macaddr", DT_LLADDR, DF_NO_GET, NULL },
1049         { IFLA_MACVLAN_MACADDR_COUNT, "macaddr_count", DT_U32, DF_NO_SET, NULL },
1050         { IFLA_MACVLAN_MACADDR_DATA, "macaddr_data", DT_LLADDR, DF_MULTIPLE, (void *)IFLA_MACVLAN_MACADDR },
1051         { IFLA_MACVLAN_MACADDR_MODE, "macaddr_mode", DT_U32, DF_NO_GET, NULL },
1052         { IFLA_MACVLAN_MODE, "mode", DT_U32, 0, NULL },
1053 };
1054 
1055 static const uc_nl_attr_spec_t link_rmnet_attrs[] = {
1056         //{ IFLA_RMNET_FLAGS, "flags", DT_??, 0, NULL },
1057         { IFLA_RMNET_MUX_ID, "mux_id", DT_U16, 0, NULL },
1058 };
1059 
1060 
1061 static const uc_nl_attr_spec_t link_vlan_attrs[] = {
1062         { IFLA_VLAN_EGRESS_QOS, "egress_qos_map", DT_NUMRANGE, DF_MULTIPLE, (void *)IFLA_VLAN_QOS_MAPPING },
1063         { IFLA_VLAN_FLAGS, "flags", DT_FLAGS, 0, NULL },
1064         { IFLA_VLAN_ID, "id", DT_U16, 0, NULL },
1065         { IFLA_VLAN_INGRESS_QOS, "ingress_qos_map", DT_NUMRANGE, DF_MULTIPLE, (void *)IFLA_VLAN_QOS_MAPPING },
1066         { IFLA_VLAN_PROTOCOL, "protocol", DT_U16, 0, NULL },
1067 };
1068 
1069 static const uc_nl_attr_spec_t link_vrf_attrs[] = {
1070         { IFLA_VRF_TABLE, "table", DT_U32, 0, NULL },
1071 };
1072 
1073 static const uc_nl_attr_spec_t link_vrf_slave_attrs[] = {
1074         { IFLA_VRF_PORT_TABLE, "port_table", DT_U32, DF_NO_SET, NULL },
1075 };
1076 
1077 static const uc_nl_attr_spec_t link_vxcan_attrs[] = {
1078         { VXCAN_INFO_PEER, "info_peer", DT_NESTED, 0, &link_msg },
1079 };
1080 
1081 static const uc_nl_attr_spec_t link_vxlan_attrs[] = {
1082         { IFLA_VXLAN_AGEING, "ageing", DT_U32, 0, NULL },
1083         { IFLA_VXLAN_COLLECT_METADATA, "collect_metadata", DT_U8, 0, NULL },
1084         { IFLA_VXLAN_GBP, "gbp", DT_FLAG, 0, NULL },
1085         { IFLA_VXLAN_GPE, "gpe", DT_FLAG, 0, NULL },
1086         { IFLA_VXLAN_GROUP, "group", DT_INADDR, 0, NULL },
1087         { IFLA_VXLAN_GROUP6, "group6", DT_IN6ADDR, 0, NULL },
1088         { IFLA_VXLAN_ID, "id", DT_U32, 0, NULL },
1089         { IFLA_VXLAN_L2MISS, "l2miss", DT_U8, 0, NULL },
1090         { IFLA_VXLAN_L3MISS, "l3miss", DT_U8, 0, NULL },
1091         { IFLA_VXLAN_LABEL, "label", DT_U32, 0, NULL },
1092         { IFLA_VXLAN_LEARNING, "learning", DT_U8, 0, NULL },
1093         { IFLA_VXLAN_LIMIT, "limit", DT_U32, 0, NULL },
1094         { IFLA_VXLAN_LINK, "link", DT_U32, 0, NULL },
1095         { IFLA_VXLAN_LOCAL, "local", DT_INADDR, 0, NULL },
1096         { IFLA_VXLAN_LOCAL6, "local6", DT_IN6ADDR, 0, NULL },
1097         { IFLA_VXLAN_PORT, "port", DT_U16, DF_BYTESWAP, NULL },
1098         { IFLA_VXLAN_PORT_RANGE, "port_range", DT_NUMRANGE, DF_MAX_65535|DF_BYTESWAP, NULL },
1099         { IFLA_VXLAN_PROXY, "proxy", DT_U8, 0, NULL },
1100         //{ IFLA_VXLAN_REMCSUM_NOPARTIAL, "remcsum-nopartial", DT_??, 0, NULL },
1101         { IFLA_VXLAN_REMCSUM_RX, "remcsum_rx", DT_BOOL, 0, NULL },
1102         { IFLA_VXLAN_REMCSUM_TX, "remcsum_tx", DT_BOOL, 0, NULL },
1103         { IFLA_VXLAN_RSC, "rsc", DT_BOOL, 0, NULL },
1104         { IFLA_VXLAN_TOS, "tos", DT_U8, 0, NULL },
1105         { IFLA_VXLAN_TTL, "ttl", DT_U8, 0, NULL },
1106         { IFLA_VXLAN_TTL_INHERIT, "ttl_inherit", DT_FLAG, 0, NULL },
1107         { IFLA_VXLAN_UDP_CSUM, "udp_csum", DT_BOOL, 0, NULL },
1108         { IFLA_VXLAN_UDP_ZERO_CSUM6_RX, "udp_zero_csum6_rx", DT_BOOL, 0, NULL },
1109         { IFLA_VXLAN_UDP_ZERO_CSUM6_TX, "udp_zero_csum6_tx", DT_BOOL, 0, NULL },
1110 };
1111 
1112 static const uc_nl_attr_spec_t link_gre_attrs[] = {
1113         { IFLA_GRE_COLLECT_METADATA, "collect_metadata", DT_FLAG, 0, NULL },
1114         { IFLA_GRE_ENCAP_DPORT, "encap_dport", DT_U16, DF_BYTESWAP, NULL },
1115         { IFLA_GRE_ENCAP_FLAGS, "encap_flags", DT_U16, 0, NULL },
1116         { IFLA_GRE_ENCAP_LIMIT, "encap_limit", DT_U8, 0, NULL },
1117         { IFLA_GRE_ENCAP_SPORT, "encap_sport", DT_U16, DF_BYTESWAP, NULL },
1118         { IFLA_GRE_ENCAP_TYPE, "encap_type", DT_U16, 0, NULL },
1119         { IFLA_GRE_ERSPAN_DIR, "erspan_dir", DT_U8, 0, NULL },
1120         { IFLA_GRE_ERSPAN_HWID, "erspan_hwid", DT_U16, 0, NULL },
1121         { IFLA_GRE_ERSPAN_INDEX, "erspan_index", DT_U32, 0, NULL },
1122         { IFLA_GRE_ERSPAN_VER, "erspan_ver", DT_U8, 0, NULL },
1123         { IFLA_GRE_FLAGS, "flags", DT_U32, 0, NULL },
1124         { IFLA_GRE_FLOWINFO, "flowinfo", DT_U32, DF_BYTESWAP, NULL },
1125         { IFLA_GRE_FWMARK, "fwmark", DT_U32, 0, NULL },
1126         { IFLA_GRE_IFLAGS, "iflags", DT_U16, 0, NULL },
1127         { IFLA_GRE_IGNORE_DF, "ignore_df", DT_BOOL, 0, NULL },
1128         { IFLA_GRE_IKEY, "ikey", DT_U32, 0, NULL },
1129         { IFLA_GRE_LINK, "link", DT_NETDEV, 0, NULL },
1130         { IFLA_GRE_LOCAL, "local", DT_ANYADDR, 0, NULL },
1131         { IFLA_GRE_OFLAGS, "oflags", DT_U16, 0, NULL },
1132         { IFLA_GRE_OKEY, "okey", DT_U32, 0, NULL },
1133         { IFLA_GRE_PMTUDISC, "pmtudisc", DT_BOOL, 0, NULL },
1134         { IFLA_GRE_REMOTE, "remote", DT_ANYADDR, 0, NULL },
1135         { IFLA_GRE_TOS, "tos", DT_U8, 0, NULL },
1136         { IFLA_GRE_TTL, "ttl", DT_U8, 0, NULL },
1137 };
1138 
1139 #define link_gretap_attrs link_gre_attrs
1140 #define link_erspan_attrs link_gre_attrs
1141 #define link_ip6gre_attrs link_gre_attrs
1142 #define link_ip6gretap_attrs link_gre_attrs
1143 #define link_ip6erspan_attrs link_gre_attrs
1144 
1145 static const uc_nl_attr_spec_t link_ip6tnl_attrs[] = {
1146         { IFLA_IPTUN_6RD_PREFIX, "6rd_prefix", DT_IN6ADDR, 0, NULL },
1147         { IFLA_IPTUN_6RD_PREFIXLEN, "6rd_prefixlen", DT_U16, 0, NULL },
1148         { IFLA_IPTUN_6RD_RELAY_PREFIX, "6rd_relay_prefix", DT_INADDR, 0, NULL },
1149         { IFLA_IPTUN_6RD_RELAY_PREFIXLEN, "6rd_relay_prefixlen", DT_U16, 0, NULL },
1150         { IFLA_IPTUN_COLLECT_METADATA, "collect_metadata", DT_BOOL, 0, NULL },
1151         { IFLA_IPTUN_ENCAP_DPORT, "encap_dport", DT_U16, DF_BYTESWAP, NULL },
1152         { IFLA_IPTUN_ENCAP_FLAGS, "encap_flags", DT_U16, 0, NULL },
1153         { IFLA_IPTUN_ENCAP_LIMIT, "encap_limit", DT_U8, 0, NULL },
1154         { IFLA_IPTUN_ENCAP_SPORT, "encap_sport", DT_U16, DF_BYTESWAP, NULL },
1155         { IFLA_IPTUN_ENCAP_TYPE, "encap_type", DT_U16, 0, NULL },
1156         { IFLA_IPTUN_FLAGS, "flags", DT_U16, 0, NULL },
1157         { IFLA_IPTUN_FLOWINFO, "flowinfo", DT_U32, DF_BYTESWAP, NULL },
1158         { IFLA_IPTUN_FWMARK, "fwmark", DT_U32, 0, NULL },
1159         { IFLA_IPTUN_LINK, "link", DT_NETDEV, 0, NULL },
1160         { IFLA_IPTUN_LOCAL, "local", DT_ANYADDR, 0, NULL },
1161         { IFLA_IPTUN_PMTUDISC, "pmtudisc", DT_BOOL, 0, NULL },
1162         { IFLA_IPTUN_PROTO, "proto", DT_U8, 0, NULL },
1163         { IFLA_IPTUN_REMOTE, "remote", DT_ANYADDR, 0, NULL },
1164         { IFLA_IPTUN_TOS, "tos", DT_U8, 0, NULL },
1165         { IFLA_IPTUN_TTL, "ttl", DT_U8, 0, NULL },
1166 };
1167 
1168 #define link_ipip_attrs link_ip6tnl_attrs
1169 #define link_sit_attrs link_ip6tnl_attrs
1170 
1171 static const uc_nl_attr_spec_t link_veth_attrs[] = {
1172         { VETH_INFO_PEER, "info_peer", DT_NESTED, 0, &link_msg },
1173 };
1174 
1175 static const uc_nl_attr_spec_t link_vti_attrs[] = {
1176         { IFLA_VTI_FWMARK, "fwmark", DT_U32, 0, NULL },
1177         { IFLA_VTI_IKEY, "ikey", DT_U32, 0, NULL },
1178         { IFLA_VTI_LINK, "link", DT_U32, 0, NULL },
1179         { IFLA_VTI_LOCAL, "local", DT_ANYADDR, 0, NULL },
1180         { IFLA_VTI_OKEY, "okey", DT_U32, 0, NULL },
1181         { IFLA_VTI_REMOTE, "remote", DT_ANYADDR, 0, NULL },
1182 };
1183 
1184 #define link_vti6_attrs link_vti_attrs
1185 
1186 static const uc_nl_attr_spec_t link_xfrm_attrs[] = {
1187         { IFLA_XFRM_IF_ID, "if_id", DT_U32, 0, NULL },
1188         { IFLA_XFRM_LINK, "link", DT_NETDEV, 0, NULL },
1189 };
1190 
1191 static const uc_nl_attr_spec_t lwtipopt_erspan_attrs[] = {
1192         { LWTUNNEL_IP_OPT_ERSPAN_VER, "ver", DT_U8, 0, NULL },
1193         { LWTUNNEL_IP_OPT_ERSPAN_INDEX, "index", DT_U16, DF_BYTESWAP, NULL },
1194         { LWTUNNEL_IP_OPT_ERSPAN_DIR, "dir", DT_U8, 0, NULL },
1195         { LWTUNNEL_IP_OPT_ERSPAN_HWID, "hwid", DT_U8, 0, NULL },
1196 };
1197 
1198 static const uc_nl_attr_spec_t lwtipopt_geneve_attrs[] = {
1199         { LWTUNNEL_IP_OPT_GENEVE_CLASS, "class", DT_U16, DF_BYTESWAP, NULL },
1200         { LWTUNNEL_IP_OPT_GENEVE_TYPE, "type", DT_U8, 0, NULL },
1201         { LWTUNNEL_IP_OPT_GENEVE_DATA, "data", DT_STRING, 0, NULL },
1202 };
1203 
1204 static const uc_nl_attr_spec_t lwtipopt_vxlan_attrs[] = {
1205         { LWTUNNEL_IP_OPT_VXLAN_GBP, "gbp", DT_U32, 0, NULL },
1206 };
1207 
1208 static const uc_nl_nested_spec_t neigh_cacheinfo_rta = {
1209         .headsize = NLA_ALIGN(sizeof(struct nda_cacheinfo)),
1210         .nattrs = 4,
1211         .attrs = {
1212                 { NDA_UNSPEC, "confirmed", DT_U32, 0, MEMBER(nda_cacheinfo, ndm_confirmed) },
1213                 { NDA_UNSPEC, "used", DT_U32, 0, MEMBER(nda_cacheinfo, ndm_used) },
1214                 { NDA_UNSPEC, "updated", DT_U32, 0, MEMBER(nda_cacheinfo, ndm_updated) },
1215                 { NDA_UNSPEC, "refcnt", DT_U32, 0, MEMBER(nda_cacheinfo, ndm_refcnt) },
1216         }
1217 };
1218 
1219 static const uc_nl_nested_spec_t neigh_msg = {
1220         .headsize = NLA_ALIGN(sizeof(struct ndmsg)),
1221         .nattrs = 16,
1222         .attrs = {
1223                 { NDA_UNSPEC, "family", DT_U8, 0, MEMBER(ndmsg, ndm_family) },
1224                 { NDA_UNSPEC, "dev" /* actually ifindex, but avoid clash with NDA_IFINDEX */, DT_NETDEV, DF_ALLOW_NONE, MEMBER(ndmsg, ndm_ifindex) },
1225                 { NDA_UNSPEC, "state", DT_U16, 0, MEMBER(ndmsg, ndm_state) },
1226                 { NDA_UNSPEC, "flags", DT_U8, 0, MEMBER(ndmsg, ndm_flags) },
1227                 { NDA_UNSPEC, "type", DT_U8, 0, MEMBER(ndmsg, ndm_type) },
1228                 { NDA_CACHEINFO, "cacheinfo", DT_NESTED, DF_NO_SET, &neigh_cacheinfo_rta },
1229                 { NDA_DST, "dst", DT_ANYADDR, 0, NULL },
1230                 { NDA_IFINDEX, "ifindex", DT_NETDEV, 0, NULL },
1231                 { NDA_LINK_NETNSID, "link_netnsid", DT_U32, DF_NO_SET, NULL },
1232                 { NDA_LLADDR, "lladdr", DT_LLADDR, 0, NULL },
1233                 { NDA_MASTER, "master", DT_NETDEV, 0, NULL },
1234                 { NDA_PORT, "port", DT_U16, DF_BYTESWAP, NULL },
1235                 { NDA_PROBES, "probes", DT_U32, DF_NO_SET, NULL },
1236                 { NDA_SRC_VNI, "src_vni", DT_U32, DF_NO_SET, NULL },
1237                 { NDA_VLAN, "vlan", DT_U16, 0, NULL },
1238                 { NDA_VNI, "vni", DT_U32, DF_MAX_16777215, NULL },
1239         }
1240 };
1241 
1242 static const uc_nl_nested_spec_t addr_cacheinfo_rta = {
1243         .headsize = NLA_ALIGN(sizeof(struct ifa_cacheinfo)),
1244         .nattrs = 4,
1245         .attrs = {
1246                 { IFA_UNSPEC, "preferred", DT_U32, 0, MEMBER(ifa_cacheinfo, ifa_prefered) },
1247                 { IFA_UNSPEC, "valid", DT_U32, 0, MEMBER(ifa_cacheinfo, ifa_valid) },
1248                 { IFA_UNSPEC, "cstamp", DT_U32, 0, MEMBER(ifa_cacheinfo, cstamp) },
1249                 { IFA_UNSPEC, "tstamp", DT_U32, 0, MEMBER(ifa_cacheinfo, tstamp) },
1250         }
1251 };
1252 
1253 static const uc_nl_nested_spec_t addr_msg = {
1254         .headsize = NLA_ALIGN(sizeof(struct ifaddrmsg)),
1255         .nattrs = 11,
1256         .attrs = {
1257                 { IFA_UNSPEC, "family", DT_U8, 0, MEMBER(ifaddrmsg, ifa_family) },
1258                 { IFA_FLAGS, "flags", DT_U32_OR_MEMBER, DF_MAX_255, MEMBER(ifaddrmsg, ifa_flags) },
1259                 { IFA_UNSPEC, "scope", DT_U8, 0, MEMBER(ifaddrmsg, ifa_scope) },
1260                 { IFA_UNSPEC, "dev", DT_NETDEV, 0, MEMBER(ifaddrmsg, ifa_index) },
1261                 { IFA_ADDRESS, "address", DT_ANYADDR, DF_STORE_MASK, MEMBER(ifaddrmsg, ifa_prefixlen) },
1262                 { IFA_LOCAL, "local", DT_ANYADDR, 0, NULL },
1263                 { IFA_LABEL, "label", DT_STRING, 0, NULL },
1264                 { IFA_BROADCAST, "broadcast", DT_ANYADDR, 0, NULL },
1265                 { IFA_ANYCAST, "anycast", DT_ANYADDR, 0, NULL },
1266                 { IFA_CACHEINFO, "cacheinfo", DT_NESTED, DF_NO_SET, &addr_cacheinfo_rta },
1267                 { IFA_RT_PRIORITY, "metric", DT_U32, 0, NULL },
1268         }
1269 };
1270 
1271 static const uc_nl_nested_spec_t rule_msg = {
1272         .headsize = NLA_ALIGN(sizeof(struct fib_rule_hdr)),
1273         .nattrs = 23,
1274         .attrs = {
1275                 { FRA_UNSPEC, "family", DT_U8, 0, MEMBER(fib_rule_hdr, family) },
1276                 { FRA_UNSPEC, "tos", DT_U8, 0, MEMBER(fib_rule_hdr, tos) },
1277                 { FRA_UNSPEC, "action", DT_U8, 0, MEMBER(fib_rule_hdr, action) },
1278                 { FRA_UNSPEC, "flags", DT_U32, 0, MEMBER(fib_rule_hdr, flags) },
1279                 { FRA_PRIORITY, "priority", DT_U32, 0, NULL },
1280                 { FRA_SRC, "src", DT_ANYADDR, DF_STORE_MASK|DF_FAMILY_HINT, MEMBER(fib_rule_hdr, src_len) },
1281                 { FRA_DST, "dst", DT_ANYADDR, DF_STORE_MASK|DF_FAMILY_HINT, MEMBER(fib_rule_hdr, dst_len) },
1282                 { FRA_FWMARK, "fwmark", DT_U32, 0, NULL },
1283                 { FRA_FWMASK, "fwmask", DT_U32, 0, NULL },
1284                 { FRA_IFNAME, "iif", DT_NETDEV, 0, NULL },
1285                 { FRA_OIFNAME, "oif", DT_NETDEV, 0, NULL },
1286                 { FRA_L3MDEV, "l3mdev", DT_U8, 0, NULL },
1287                 { FRA_UID_RANGE, "uid_range", DT_NUMRANGE, 0, NULL },
1288                 { FRA_IP_PROTO, "ip_proto", DT_U8, 0, NULL },
1289                 { FRA_SPORT_RANGE, "sport_range", DT_NUMRANGE, DF_MAX_65535, NULL },
1290                 { FRA_DPORT_RANGE, "dport_range", DT_NUMRANGE, DF_MAX_65535, NULL },
1291                 { FRA_TABLE, "table", DT_U32_OR_MEMBER, DF_MAX_255, MEMBER(fib_rule_hdr, table) },
1292                 { FRA_SUPPRESS_PREFIXLEN, "suppress_prefixlen", DT_S32, 0, NULL },
1293                 { FRA_SUPPRESS_IFGROUP, "suppress_ifgroup", DT_U32, 0, NULL },
1294                 { FRA_FLOW, "flow", DT_U32, 0, NULL },
1295                 { RTA_GATEWAY, "gateway", DT_ANYADDR, DF_FAMILY_HINT, NULL },
1296                 { FRA_GOTO, "goto", DT_U32, 0, NULL },
1297                 { FRA_PROTOCOL, "protocol", DT_U8, 0, NULL },
1298         }
1299 };
1300 
1301 #define IFAL_UNSPEC 0
1302 
1303 static const uc_nl_nested_spec_t addrlabel_msg = {
1304         .headsize = NLA_ALIGN(sizeof(struct ifaddrlblmsg)),
1305         .nattrs = 6,
1306         .attrs = {
1307                 { IFAL_UNSPEC, "family", DT_U8, 0, MEMBER(ifaddrlblmsg, ifal_family) },
1308                 { IFAL_UNSPEC, "flags", DT_U8, 0, MEMBER(ifaddrlblmsg, ifal_flags) },
1309                 { IFAL_UNSPEC, "dev", DT_NETDEV, 0, MEMBER(ifaddrlblmsg, ifal_index) },
1310                 { IFAL_UNSPEC, "seq", DT_U32, 0, MEMBER(ifaddrlblmsg, ifal_seq) },
1311                 { IFAL_ADDRESS, "address", DT_ANYADDR, DF_STORE_MASK, MEMBER(ifaddrlblmsg, ifal_prefixlen) },
1312                 { IFAL_LABEL, "label", DT_U32, 0, NULL },
1313         }
1314 };
1315 
1316 static const uc_nl_nested_spec_t neightbl_params_rta = {
1317         .headsize = 0,
1318         .nattrs = 13,
1319         .attrs = {
1320                 { NDTPA_IFINDEX, "dev", DT_NETDEV, 0, NULL },
1321                 { NDTPA_BASE_REACHABLE_TIME, "base_reachable_time", DT_U64, 0, NULL },
1322                 { NDTPA_RETRANS_TIME, "retrans_time", DT_U64, 0, NULL },
1323                 { NDTPA_GC_STALETIME, "gc_staletime", DT_U64, 0, NULL },
1324                 { NDTPA_DELAY_PROBE_TIME, "delay_probe_time", DT_U64, 0, NULL },
1325                 { NDTPA_QUEUE_LEN, "queue_len", DT_U32, 0, NULL },
1326                 { NDTPA_APP_PROBES, "app_probes", DT_U32, 0, NULL },
1327                 { NDTPA_UCAST_PROBES, "ucast_probes", DT_U32, 0, NULL },
1328                 { NDTPA_MCAST_PROBES, "mcast_probes", DT_U32, 0, NULL },
1329                 { NDTPA_ANYCAST_DELAY, "anycast_delay", DT_U64, 0, NULL },
1330                 { NDTPA_PROXY_DELAY, "proxy_delay", DT_U64, 0, NULL },
1331                 { NDTPA_PROXY_QLEN, "proxy_qlen", DT_U32, 0, NULL },
1332                 { NDTPA_LOCKTIME, "locktime", DT_U64, 0, NULL },
1333         }
1334 };
1335 
1336 static const uc_nl_nested_spec_t neightbl_config_rta = {
1337         .headsize = NLA_ALIGN(sizeof(struct ndt_config)),
1338         .nattrs = 9,
1339         .attrs = {
1340                 { NDTA_UNSPEC, "key_len", DT_U16, 0, MEMBER(ndt_config, ndtc_key_len) },
1341                 { NDTA_UNSPEC, "entry_size", DT_U16, 0, MEMBER(ndt_config, ndtc_entry_size) },
1342                 { NDTA_UNSPEC, "entries", DT_U32, 0, MEMBER(ndt_config, ndtc_entries) },
1343                 { NDTA_UNSPEC, "last_flush", DT_U32, 0, MEMBER(ndt_config, ndtc_last_flush) },
1344                 { NDTA_UNSPEC, "last_rand", DT_U32, 0, MEMBER(ndt_config, ndtc_last_rand) },
1345                 { NDTA_UNSPEC, "hash_rnd", DT_U32, 0, MEMBER(ndt_config, ndtc_hash_rnd) },
1346                 { NDTA_UNSPEC, "hash_mask", DT_U32, 0, MEMBER(ndt_config, ndtc_hash_mask) },
1347                 { NDTA_UNSPEC, "hash_chain_gc", DT_U32, 0, MEMBER(ndt_config, ndtc_hash_chain_gc) },
1348                 { NDTA_UNSPEC, "proxy_qlen", DT_U32, 0, MEMBER(ndt_config, ndtc_proxy_qlen) },
1349         }
1350 };
1351 
1352 static const uc_nl_nested_spec_t neightbl_stats_rta = {
1353         .headsize = NLA_ALIGN(sizeof(struct ndt_stats)),
1354         .nattrs = 10,
1355         .attrs = {
1356                 { NDTA_UNSPEC, "allocs", DT_U64, 0, MEMBER(ndt_stats, ndts_allocs) },
1357                 { NDTA_UNSPEC, "destroys", DT_U64, 0, MEMBER(ndt_stats, ndts_destroys) },
1358                 { NDTA_UNSPEC, "hash_grows", DT_U64, 0, MEMBER(ndt_stats, ndts_hash_grows) },
1359                 { NDTA_UNSPEC, "res_failed", DT_U64, 0, MEMBER(ndt_stats, ndts_res_failed) },
1360                 { NDTA_UNSPEC, "lookups", DT_U64, 0, MEMBER(ndt_stats, ndts_lookups) },
1361                 { NDTA_UNSPEC, "hits", DT_U64, 0, MEMBER(ndt_stats, ndts_hits) },
1362                 { NDTA_UNSPEC, "rcv_probes_mcast", DT_U64, 0, MEMBER(ndt_stats, ndts_rcv_probes_mcast) },
1363                 { NDTA_UNSPEC, "rcv_probes_ucast", DT_U64, 0, MEMBER(ndt_stats, ndts_rcv_probes_ucast) },
1364                 { NDTA_UNSPEC, "periodic_gc_runs", DT_U64, 0, MEMBER(ndt_stats, ndts_periodic_gc_runs) },
1365                 { NDTA_UNSPEC, "forced_gc_runs", DT_U64, 0, MEMBER(ndt_stats, ndts_forced_gc_runs) },
1366         }
1367 };
1368 
1369 static const uc_nl_nested_spec_t neightbl_msg = {
1370         .headsize = NLA_ALIGN(sizeof(struct ndtmsg)),
1371         .nattrs = 9,
1372         .attrs = {
1373                 { NDTA_UNSPEC, "family", DT_U8, 0, MEMBER(ndtmsg, ndtm_family) },
1374                 { NDTA_NAME, "name", DT_STRING, 0, NULL },
1375                 { NDTA_THRESH1, "thresh1", DT_U32, 0, NULL },
1376                 { NDTA_THRESH2, "thresh2", DT_U32, 0, NULL },
1377                 { NDTA_THRESH3, "thresh3", DT_U32, 0, NULL },
1378                 { NDTA_GC_INTERVAL, "gc_interval", DT_U64, 0, NULL },
1379                 { NDTA_PARMS, "params", DT_NESTED, 0, &neightbl_params_rta },
1380                 { NDTA_CONFIG, "config", DT_NESTED, DF_NO_SET, &neightbl_config_rta },
1381                 { NDTA_STATS, "stats", DT_NESTED, DF_NO_SET, &neightbl_stats_rta },
1382         }
1383 };
1384 
1385 static const uc_nl_nested_spec_t netconf_msg = {
1386         .headsize = NLA_ALIGN(sizeof(struct netconfmsg)),
1387         .nattrs = 8,
1388         .attrs = {
1389                 { NETCONFA_UNSPEC, "family", DT_U8, 0, MEMBER(netconfmsg, ncm_family) },
1390                 { NETCONFA_IFINDEX, "dev", DT_NETDEV, 0, NULL },
1391                 { NETCONFA_FORWARDING, "forwarding", DT_U32, DF_NO_SET, NULL },
1392                 { NETCONFA_RP_FILTER, "rp_filter", DT_U32, DF_NO_SET, NULL },
1393                 { NETCONFA_MC_FORWARDING, "mc_forwarding", DT_U32, DF_NO_SET, NULL },
1394                 { NETCONFA_PROXY_NEIGH, "proxy_neigh", DT_U32, DF_NO_SET, NULL },
1395                 { NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, "ignore_routes_with_linkdown", DT_U32, DF_NO_SET, NULL },
1396                 { NETCONFA_INPUT, "input", DT_U32, DF_NO_SET, NULL },
1397         }
1398 };
1399 
1400 
1401 static bool
1402 nla_check_len(struct nlattr *nla, size_t sz)
1403 {
1404         return (nla && nla_len(nla) >= (ssize_t)sz);
1405 }
1406 
1407 static bool
1408 nla_parse_error(const uc_nl_attr_spec_t *spec, uc_vm_t *vm, uc_value_t *v, const char *msg)
1409 {
1410         char *s;
1411 
1412         s = ucv_to_string(vm, v);
1413 
1414         set_error(NLE_INVAL, "%s `%s` has invalid value `%s`: %s",
1415                 spec->attr ? "attribute" : "field",
1416                 spec->key,
1417                 s,
1418                 msg);
1419 
1420         free(s);
1421 
1422         return false;
1423 }
1424 
1425 static void
1426 uc_nl_put_struct_member(char *base, const void *offset, size_t datalen, void *data)
1427 {
1428         memcpy(base + (uintptr_t)offset, data, datalen);
1429 }
1430 
1431 static void
1432 uc_nl_put_struct_member_u8(char *base, const void *offset, uint8_t u8)
1433 {
1434         base[(uintptr_t)offset] = u8;
1435 }
1436 
1437 static void
1438 uc_nl_put_struct_member_u16(char *base, const void *offset, uint16_t u16)
1439 {
1440         uc_nl_put_struct_member(base, offset, sizeof(u16), &u16);
1441 }
1442 
1443 static void
1444 uc_nl_put_struct_member_u32(char *base, const void *offset, uint32_t u32)
1445 {
1446         uc_nl_put_struct_member(base, offset, sizeof(u32), &u32);
1447 }
1448 
1449 static void *
1450 uc_nl_get_struct_member(char *base, const void *offset, size_t datalen, void *data)
1451 {
1452         memcpy(data, base + (uintptr_t)offset, datalen);
1453 
1454         return data;
1455 }
1456 
1457 static uint8_t
1458 uc_nl_get_struct_member_u8(char *base, const void *offset)
1459 {
1460         return (uint8_t)base[(uintptr_t)offset];
1461 }
1462 
1463 static uint16_t
1464 uc_nl_get_struct_member_u16(char *base, const void *offset)
1465 {
1466         uint16_t u16;
1467 
1468         uc_nl_get_struct_member(base, offset, sizeof(u16), &u16);
1469 
1470         return u16;
1471 }
1472 
1473 static uint32_t
1474 uc_nl_get_struct_member_u32(char *base, const void *offset)
1475 {
1476         uint32_t u32;
1477 
1478         uc_nl_get_struct_member(base, offset, sizeof(u32), &u32);
1479 
1480         return u32;
1481 }
1482 
1483 static uint64_t
1484 uc_nl_get_struct_member_u64(char *base, const void *offset)
1485 {
1486         uint64_t u64;
1487 
1488         uc_nl_get_struct_member(base, offset, sizeof(u64), &u64);
1489 
1490         return u64;
1491 }
1492 
1493 static bool
1494 uc_nl_parse_attr(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val, size_t idx);
1495 
1496 static uc_value_t *
1497 uc_nl_convert_attr(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, struct nlattr **tb, uc_vm_t *vm);
1498 
1499 static bool
1500 uc_nl_convert_attrs(struct nl_msg *msg, void *buf, size_t buflen, size_t headsize, const uc_nl_attr_spec_t *attrs, size_t nattrs, uc_vm_t *vm, uc_value_t *obj)
1501 {
1502         size_t i, maxattr = 0, structlen = headsize;
1503         struct nlattr **tb, *nla, *nla_nest;
1504         uc_value_t *v, *arr;
1505         int rem;
1506 
1507         for (i = 0; i < nattrs; i++)
1508                 if (attrs[i].attr > maxattr)
1509                         maxattr = attrs[i].attr;
1510 
1511         tb = calloc(maxattr + 1, sizeof(struct nlattr *));
1512 
1513         if (!tb)
1514                 return false;
1515 
1516         if (buflen > headsize) {
1517                 if (maxattr)
1518                         nla_parse(tb, maxattr, buf + headsize, buflen - headsize, NULL);
1519         }
1520         else {
1521                 structlen = buflen;
1522         }
1523 
1524         for (i = 0; i < nattrs; i++) {
1525                 if (attrs[i].attr == 0 && (uintptr_t)attrs[i].auxdata >= structlen)
1526                         continue;
1527 
1528                 if (attrs[i].attr != 0 && !tb[attrs[i].attr])
1529                         continue;
1530 
1531                 if (attrs[i].flags & DF_NO_GET)
1532                         continue;
1533 
1534                 if (attrs[i].flags & DF_MULTIPLE) {
1535                         /* can't happen, but needed to nudge clang-analyzer */
1536                         if (!tb[attrs[i].attr])
1537                                 continue;
1538 
1539                         arr = ucv_array_new(vm);
1540                         nla_nest = tb[attrs[i].attr];
1541 
1542                         nla_for_each_attr(nla, nla_data(nla_nest), nla_len(nla_nest), rem) {
1543                                 if (attrs[i].auxdata && nla_type(nla) != (intptr_t)attrs[i].auxdata)
1544                                         continue;
1545 
1546                                 tb[attrs[i].attr] = nla;
1547 
1548                                 v = uc_nl_convert_attr(&attrs[i], msg, (char *)buf, tb, vm);
1549 
1550                                 if (!v)
1551                                         continue;
1552 
1553                                 ucv_array_push(arr, v);
1554                         }
1555 
1556                         if (!ucv_array_length(arr)) {
1557                                 ucv_put(arr);
1558 
1559                                 continue;
1560                         }
1561 
1562                         v = arr;
1563                 }
1564                 else {
1565                         v = uc_nl_convert_attr(&attrs[i], msg, (char *)buf, tb, vm);
1566 
1567                         if (!v)
1568                                 continue;
1569                 }
1570 
1571                 ucv_object_add(obj, attrs[i].key, v);
1572         }
1573 
1574         free(tb);
1575 
1576         return true;
1577 }
1578 
1579 static bool
1580 uc_nl_parse_attrs(struct nl_msg *msg, char *base, const uc_nl_attr_spec_t *attrs, size_t nattrs, uc_vm_t *vm, uc_value_t *obj)
1581 {
1582         struct nlattr *nla_nest = NULL;
1583         size_t i, j, idx;
1584         uc_value_t *v;
1585         bool exists;
1586 
1587         for (i = 0; i < nattrs; i++) {
1588                 v = ucv_object_get(obj, attrs[i].key, &exists);
1589 
1590                 if (!exists)
1591                         continue;
1592 
1593                 if (attrs[i].flags & DF_MULTIPLE) {
1594                         if (!(attrs[i].flags & DF_FLAT)) {
1595                                 nla_nest = nla_nest_start(msg, attrs[i].attr);
1596 
1597                                 if (!nla_nest)
1598                                         return nla_parse_error(&attrs[i], vm, v,
1599                                                 "netlink message is full");
1600                         }
1601 
1602                         if (ucv_type(v) == UC_ARRAY) {
1603                                 for (j = 0; j < ucv_array_length(v); j++) {
1604                                         if (attrs[i].flags & DF_FLAT)
1605                                                 idx = attrs[i].attr;
1606                                         else if (attrs[i].auxdata)
1607                                                 idx = (uintptr_t)attrs[i].auxdata;
1608                                         else
1609                                                 idx = j;
1610 
1611                                         if (!uc_nl_parse_attr(&attrs[i], msg, base, vm, ucv_array_get(v, j), idx))
1612                                                 return false;
1613                                 }
1614                         }
1615                         else {
1616                                 if (attrs[i].flags & DF_FLAT)
1617                                         idx = attrs[i].attr;
1618                                 else if (attrs[i].auxdata)
1619                                         idx = (uintptr_t)attrs[i].auxdata;
1620                                 else
1621                                         idx = 0;
1622 
1623                                 if (!uc_nl_parse_attr(&attrs[i], msg, base, vm, v, idx))
1624                                         return false;
1625                         }
1626 
1627                         if (nla_nest)
1628                                 nla_nest_end(msg, nla_nest);
1629                 }
1630                 else if (!uc_nl_parse_attr(&attrs[i], msg, base, vm, v, 0)) {
1631                         return false;
1632                 }
1633         }
1634 
1635         return true;
1636 }
1637 
1638 static bool
1639 uc_nl_parse_rta_nexthop(struct nl_msg *msg, uc_vm_t *vm, uc_value_t *val)
1640 {
1641         struct { uint16_t family; char addr[sizeof(struct in6_addr)]; } via;
1642         struct nlmsghdr *hdr = nlmsg_hdr(msg);
1643         struct rtmsg *rtm = NLMSG_DATA(hdr);
1644         struct nlattr *rta_gateway;
1645         struct rtnexthop *rtnh;
1646         uc_nl_cidr_t cidr = { 0 };
1647         uc_value_t *v;
1648         uint32_t u;
1649         int aflen;
1650         char *s;
1651 
1652         if (ucv_type(val) != UC_OBJECT)
1653                 return false;
1654 
1655         if (!uc_nl_parse_cidr(vm, ucv_object_get(val, "via", NULL), &cidr))
1656                 return false;
1657 
1658         aflen = (cidr.family == AF_INET6 ? sizeof(cidr.addr.in6) : sizeof(cidr.addr.in));
1659 
1660         if (cidr.mask != (aflen * 8))
1661                 return false;
1662 
1663         rta_gateway = nla_reserve(msg, RTA_GATEWAY, sizeof(*rtnh));
1664 
1665         if (!rta_gateway)
1666                 return false;
1667 
1668         rtnh = nla_data(rta_gateway);
1669         rtnh->rtnh_len = sizeof(*rtnh);
1670 
1671         if (rtm->rtm_family == AF_UNSPEC)
1672                 rtm->rtm_family = cidr.family;
1673 
1674         if (cidr.family == rtm->rtm_family) {
1675                 nla_put(msg, RTA_GATEWAY, aflen, &cidr.addr.in6);
1676                 rtnh->rtnh_len += nla_total_size(aflen);
1677         }
1678         else {
1679                 via.family = cidr.family;
1680                 memcpy(via.addr, &cidr.addr.in6, aflen);
1681                 nla_put(msg, RTA_VIA, sizeof(via.family) + aflen, &via);
1682                 rtnh->rtnh_len += nla_total_size(sizeof(via.family) + aflen);
1683         }
1684 
1685         v = ucv_object_get(val, "dev", NULL);
1686         s = ucv_string_get(v);
1687 
1688         if (s) {
1689                 rtnh->rtnh_ifindex = if_nametoindex(s);
1690 
1691                 if (rtnh->rtnh_ifindex == 0)
1692                         return false;
1693         }
1694 
1695         v = ucv_object_get(val, "weight", NULL);
1696 
1697         if (v) {
1698                 if (!uc_nl_parse_u32(v, &u) || u == 0 || u > 256)
1699                         return false;
1700 
1701                 rtnh->rtnh_hops = u - 1;
1702         }
1703 
1704         if (ucv_is_truish(ucv_object_get(val, "onlink", NULL)))
1705                 rtnh->rtnh_flags |= RTNH_F_ONLINK;
1706 
1707         v = ucv_object_get(val, "realm", NULL);
1708 
1709         if (v) {
1710                 if (!uc_nl_parse_u32(v, &u))
1711                         return false;
1712 
1713                 nla_put_u32(msg, RTA_FLOW, u);
1714                 rtnh->rtnh_len += nla_total_size(sizeof(uint32_t));
1715         }
1716 
1717         v = ucv_object_get(val, "as", NULL);
1718 
1719         if (v) {
1720                 if (!uc_nl_parse_cidr(vm, v, &cidr) || cidr.family != rtm->rtm_family)
1721                         return false;
1722 
1723                 if (cidr.mask != cidr.bitlen)
1724                         return false;
1725 
1726                 nla_put(msg, RTA_NEWDST, cidr.alen, &cidr.addr.in6);
1727                 rtnh->rtnh_len += nla_total_size(cidr.alen);
1728         }
1729 
1730         /* XXX: nla_nest_end(rta_gateway) ? */
1731 
1732         return true;
1733 }
1734 
1735 static bool
1736 uc_nl_parse_rta_multipath(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
1737 {
1738         struct nlattr *rta_multipath = nla_nest_start(msg, spec->attr);
1739         size_t i;
1740 
1741         if (!rta_multipath)
1742                 return nla_parse_error(spec, vm, val, "netlink message is full");
1743 
1744         for (i = 0; i < ucv_array_length(val); i++)
1745                 if (!uc_nl_parse_rta_nexthop(msg, vm, ucv_array_get(val, i)))
1746                         return false;
1747 
1748         nla_nest_end(msg, rta_multipath);
1749 
1750         return true;
1751 }
1752 
1753 static uc_value_t *
1754 uc_nl_convert_rta_encap(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm);
1755 
1756 static uc_value_t *
1757 uc_nl_convert_rta_multipath(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
1758 {
1759         uc_nl_attr_spec_t encap_spec = { .attr = RTA_ENCAP };
1760         struct rtnexthop *nh = nla_data(tb[spec->attr]);
1761         struct nlattr *multipath_tb[RTA_MAX + 1];
1762         size_t len = nla_len(tb[spec->attr]);
1763         uc_value_t *nh_obj, *nh_arr;
1764         char buf[INET6_ADDRSTRLEN];
1765         struct rtvia *via;
1766         int af;
1767 
1768         nh_arr = ucv_array_new(vm);
1769 
1770         while (len >= sizeof(*nh)) {
1771                 if ((size_t)NLA_ALIGN(nh->rtnh_len) > len)
1772                         break;
1773 
1774                 nh_obj = ucv_object_new(vm);
1775                 ucv_array_push(nh_arr, nh_obj);
1776 
1777                 nla_parse(multipath_tb, RTA_MAX, (struct nlattr *)RTNH_DATA(nh), nh->rtnh_len - sizeof(*nh), NULL);
1778 
1779                 if (multipath_tb[RTA_GATEWAY]) {
1780                         switch (nla_len(multipath_tb[RTA_GATEWAY])) {
1781                         case 4: af = AF_INET; break;
1782                         case 16: af = AF_INET6; break;
1783                         default: af = AF_UNSPEC; break;
1784                         }
1785 
1786                         if (inet_ntop(af, nla_data(multipath_tb[RTA_GATEWAY]), buf, sizeof(buf)))
1787                                 ucv_object_add(nh_obj, "via", ucv_string_new(buf));
1788                 }
1789 
1790                 if (multipath_tb[RTA_VIA]) {
1791                         if (nla_len(multipath_tb[RTA_VIA]) > (ssize_t)sizeof(*via)) {
1792                                 via = nla_data(multipath_tb[RTA_VIA]);
1793                                 af = via->rtvia_family;
1794 
1795                                 if ((af == AF_INET &&
1796                                      nla_len(multipath_tb[RTA_VIA]) == sizeof(*via) + sizeof(struct in_addr)) ||
1797                                         (af == AF_INET6 &&
1798                                      nla_len(multipath_tb[RTA_VIA]) == sizeof(*via) + sizeof(struct in6_addr))) {
1799                                         if (inet_ntop(af, via->rtvia_addr, buf, sizeof(buf)))
1800                                                 ucv_object_add(nh_obj, "via", ucv_string_new(buf));
1801                                 }
1802                         }
1803                 }
1804 
1805                 if (if_indextoname(nh->rtnh_ifindex, buf))
1806                         ucv_object_add(nh_obj, "dev", ucv_string_new(buf));
1807 
1808                 ucv_object_add(nh_obj, "weight", ucv_int64_new(nh->rtnh_hops + 1));
1809                 ucv_object_add(nh_obj, "onlink", ucv_boolean_new(nh->rtnh_flags & RTNH_F_ONLINK));
1810 
1811                 if (multipath_tb[RTA_FLOW] && nla_len(multipath_tb[RTA_FLOW]) == sizeof(uint32_t))
1812                         ucv_object_add(nh_obj, "realm", ucv_int64_new(nla_get_u32(multipath_tb[RTA_FLOW])));
1813 
1814                 if (multipath_tb[RTA_ENCAP])
1815                         ucv_object_add(nh_obj, "encap",
1816                                 uc_nl_convert_rta_encap(&encap_spec, msg, multipath_tb, vm));
1817 
1818                 if (multipath_tb[RTA_NEWDST]) {
1819                         switch (nla_len(multipath_tb[RTA_NEWDST])) {
1820                         case 4: af = AF_INET; break;
1821                         case 16: af = AF_INET6; break;
1822                         default: af = AF_UNSPEC; break;
1823                         }
1824 
1825                         if (inet_ntop(af, nla_data(multipath_tb[RTA_NEWDST]), buf, sizeof(buf)))
1826                                 ucv_object_add(nh_obj, "as", ucv_string_new(buf));
1827                 }
1828 
1829                 len -= NLA_ALIGN(nh->rtnh_len);
1830                 nh = RTNH_NEXT(nh);
1831         }
1832 
1833         return nh_arr;
1834 }
1835 
1836 static bool
1837 parse_num(const uc_nl_attr_spec_t *spec, uc_vm_t *vm, uc_value_t *val, void *dst)
1838 {
1839         int64_t n = ucv_int64_get(val);
1840         uint32_t *u32;
1841         uint16_t *u16;
1842         uint8_t *u8;
1843 
1844         if (spec->flags & DF_MAX_255) {
1845                 if (n < 0 || n > 255)
1846                         return nla_parse_error(spec, vm, val, "number out of range 0-255");
1847 
1848                 u8 = dst; *u8 = n;
1849         }
1850         else if (spec->flags & DF_MAX_65535) {
1851                 if (n < 0 || n > 65535)
1852                         return nla_parse_error(spec, vm, val, "number out of range 0-65535");
1853 
1854                 u16 = dst; *u16 = n;
1855 
1856                 if (spec->flags & DF_BYTESWAP)
1857                         *u16 = htons(*u16);
1858         }
1859         else if (spec->flags & DF_MAX_16777215) {
1860                 if (n < 0 || n > 16777215)
1861                         return nla_parse_error(spec, vm, val, "number out of range 0-16777215");
1862 
1863                 u32 = dst; *u32 = n;
1864 
1865                 if (spec->flags & DF_BYTESWAP)
1866                         *u32 = htonl(*u32);
1867         }
1868         else {
1869                 if (n < 0 || n > 4294967295)
1870                         return nla_parse_error(spec, vm, val, "number out of range 0-4294967295");
1871 
1872                 u32 = dst; *u32 = n;
1873 
1874                 if (spec->flags & DF_BYTESWAP)
1875                         *u32 = htonl(*u32);
1876         }
1877 
1878         return true;
1879 }
1880 
1881 static bool
1882 uc_nl_parse_rta_numrange(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
1883 {
1884         union {
1885                 struct { uint8_t low; uint8_t high; } u8;
1886                 struct { uint16_t low; uint16_t high; } u16;
1887                 struct { uint32_t low; uint32_t high; } u32;
1888         } ranges = { 0 };
1889 
1890         void *d1, *d2;
1891         size_t len;
1892 
1893         if (ucv_array_length(val) != 2 ||
1894             ucv_type(ucv_array_get(val, 0)) != UC_INTEGER ||
1895             ucv_type(ucv_array_get(val, 1)) != UC_INTEGER)
1896                 return nla_parse_error(spec, vm, val, "not a two-element array of numbers");
1897 
1898         if (spec->flags & DF_MAX_255) {
1899                 len = sizeof(ranges.u8);
1900                 d1 = &ranges.u8.low;
1901                 d2 = &ranges.u8.high;
1902         }
1903         else if (spec->flags & DF_MAX_65535) {
1904                 len = sizeof(ranges.u16);
1905                 d1 = &ranges.u16.low;
1906                 d2 = &ranges.u16.high;
1907         }
1908         else {
1909                 len = sizeof(ranges.u32);
1910                 d1 = &ranges.u32.low;
1911                 d2 = &ranges.u32.high;
1912         }
1913 
1914         if (!parse_num(spec, vm, ucv_array_get(val, 0), d1) ||
1915             !parse_num(spec, vm, ucv_array_get(val, 1), d2))
1916             return false;
1917 
1918         nla_put(msg, spec->attr, len, d1);
1919 
1920         return true;
1921 }
1922 
1923 static uc_value_t *
1924 uc_nl_convert_rta_numrange(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
1925 {
1926         union {
1927                 struct { uint8_t low; uint8_t high; } *u8;
1928                 struct { uint16_t low; uint16_t high; } *u16;
1929                 struct { uint32_t low; uint32_t high; } *u32;
1930         } ranges = { 0 };
1931 
1932         bool swap = (spec->flags & DF_BYTESWAP);
1933         uc_value_t *arr, *n1, *n2;
1934 
1935         if (spec->flags & DF_MAX_255) {
1936                 if (!nla_check_len(tb[spec->attr], sizeof(*ranges.u8)))
1937                         return NULL;
1938 
1939                 ranges.u8 = nla_data(tb[spec->attr]);
1940                 n1 = ucv_int64_new(ranges.u8->low);
1941                 n2 = ucv_int64_new(ranges.u8->high);
1942         }
1943         else if (spec->flags & DF_MAX_65535) {
1944                 if (!nla_check_len(tb[spec->attr], sizeof(*ranges.u16)))
1945                         return NULL;
1946 
1947                 ranges.u16 = nla_data(tb[spec->attr]);
1948                 n1 = ucv_int64_new(swap ? ntohs(ranges.u16->low) : ranges.u16->low);
1949                 n2 = ucv_int64_new(swap ? ntohs(ranges.u16->high) : ranges.u16->high);
1950         }
1951         else {
1952                 if (!nla_check_len(tb[spec->attr], sizeof(*ranges.u32)))
1953                         return NULL;
1954 
1955                 ranges.u32 = nla_data(tb[spec->attr]);
1956                 n1 = ucv_int64_new(swap ? ntohl(ranges.u32->low) : ranges.u32->low);
1957                 n2 = ucv_int64_new(swap ? ntohl(ranges.u32->high) : ranges.u32->high);
1958         }
1959 
1960         arr = ucv_array_new(vm);
1961 
1962         ucv_array_push(arr, n1);
1963         ucv_array_push(arr, n2);
1964 
1965         return arr;
1966 }
1967 
1968 
1969 #define LINK_TYPE(name) \
1970         { #name, link_##name##_attrs, ARRAY_SIZE(link_##name##_attrs) }
1971 
1972 #define LINK_SLAVE_TYPE(name) \
1973         { #name, link_##name##_slave_attrs, ARRAY_SIZE(link_##name##_slave_attrs) }
1974 
1975 static const struct link_type {
1976         const char *name;
1977         const uc_nl_attr_spec_t *attrs;
1978         size_t nattrs;
1979 } link_types[] = {
1980         LINK_TYPE(bareudp),
1981         LINK_TYPE(bond),
1982         LINK_TYPE(bridge),
1983         LINK_TYPE(can),
1984         LINK_TYPE(geneve),
1985         LINK_TYPE(hsr),
1986         LINK_TYPE(ipoib),
1987         LINK_TYPE(ipvlan),
1988         LINK_TYPE(macvlan),
1989         LINK_TYPE(rmnet),
1990         LINK_TYPE(vlan),
1991         LINK_TYPE(vrf),
1992         LINK_TYPE(vxcan),
1993         LINK_TYPE(vxlan),
1994         //LINK_TYPE(xdp),
1995         //LINK_TYPE(xstats),
1996         LINK_TYPE(gre),
1997         LINK_TYPE(gretap),
1998         LINK_TYPE(erspan),
1999         LINK_TYPE(ip6gre),
2000         LINK_TYPE(ip6gretap),
2001         LINK_TYPE(ip6erspan),
2002         LINK_TYPE(ip6tnl),
2003         LINK_TYPE(ipip),
2004         LINK_TYPE(sit),
2005         LINK_TYPE(veth),
2006         LINK_TYPE(vti),
2007         LINK_TYPE(vti6),
2008         LINK_TYPE(xfrm),
2009 };
2010 
2011 static const struct link_type link_slave_types[] = {
2012         LINK_SLAVE_TYPE(bond),
2013         LINK_SLAVE_TYPE(bridge),
2014         LINK_SLAVE_TYPE(vrf),
2015 };
2016 
2017 static const uc_nl_attr_spec_t *
2018 link_attrs_lookup(const char *kind, size_t len, bool slave, size_t *nattrs)
2019 {
2020         const struct link_type *types = slave ? link_slave_types : link_types;
2021         size_t i, ntypes = slave ? ARRAY_SIZE(link_slave_types) : ARRAY_SIZE(link_types);
2022 
2023         for (i = 0; i < ntypes; i++) {
2024                 if (strlen(types[i].name) != len || memcmp(types[i].name, kind, len))
2025                         continue;
2026 
2027                 *nattrs = types[i].nattrs;
2028 
2029                 return types[i].attrs;
2030         }
2031 
2032         *nattrs = 0;
2033 
2034         return NULL;
2035 }
2036 
2037 static bool
2038 uc_nl_parse_rta_linkinfo(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
2039 {
2040         const uc_nl_attr_spec_t *attrs;
2041         struct nlattr *li_nla, *info_nla;
2042         size_t nattrs;
2043         char *kind, *p;
2044         uc_value_t *k;
2045         bool slave;
2046 
2047         k = ucv_object_get(val, "type", NULL);
2048         kind = ucv_string_get(k);
2049 
2050         if (!kind)
2051                 return nla_parse_error(spec, vm, val, "linkinfo does not specify kind");
2052 
2053         li_nla = nla_nest_start(msg, spec->attr);
2054 
2055         if (!li_nla)
2056                 return nla_parse_error(spec, vm, val, "netlink message is full");
2057 
2058         p = strchr(kind, '_');
2059         slave = p && !strcmp(p, "_slave");
2060 
2061         if (!slave)
2062                 nla_put_string(msg, IFLA_INFO_KIND, kind);
2063 
2064         attrs = link_attrs_lookup(kind, slave ? (size_t)(p - kind) : strlen(kind),
2065                 slave, &nattrs);
2066 
2067         info_nla = nla_nest_start(msg, slave ? IFLA_INFO_SLAVE_DATA : IFLA_INFO_DATA);
2068 
2069         if (!info_nla)
2070                 return nla_parse_error(spec, vm, val, "netlink message is full");
2071 
2072         if (!uc_nl_parse_attrs(msg, base, attrs, nattrs, vm, val))
2073                 return false;
2074 
2075         nla_nest_end(msg, info_nla);
2076         nla_nest_end(msg, li_nla);
2077 
2078         return true;
2079 }
2080 
2081 static uc_value_t *
2082 uc_nl_convert_rta_linkinfo_data(uc_value_t *obj, size_t attr, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
2083 {
2084         const uc_nl_attr_spec_t *attrs;
2085         size_t nattrs;
2086         uc_value_t *v;
2087         bool rv;
2088 
2089         if (!tb[attr] || nla_len(tb[attr]) < 1)
2090                 return NULL;
2091 
2092         v = ucv_string_new_length(nla_data(tb[attr]), nla_len(tb[attr]) - 1);
2093 
2094         ucv_object_add(obj, "type", v);
2095 
2096         attrs = link_attrs_lookup(ucv_string_get(v), ucv_string_length(v),
2097                 attr == IFLA_INFO_SLAVE_KIND, &nattrs);
2098 
2099         attr = (attr == IFLA_INFO_KIND) ? IFLA_INFO_DATA : IFLA_INFO_SLAVE_DATA;
2100 
2101         if (nattrs > 0 && tb[attr]) {
2102                 rv = uc_nl_convert_attrs(msg, nla_data(tb[attr]), nla_len(tb[attr]), 0, attrs, nattrs, vm, obj);
2103 
2104                 if (!rv)
2105                         return NULL;
2106         }
2107 
2108         return obj;
2109 }
2110 
2111 static uc_value_t *
2112 uc_nl_convert_rta_linkinfo(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
2113 {
2114         struct nlattr *linkinfo_tb[IFLA_INFO_MAX + 1];
2115         uc_value_t *info_obj, *slave_obj;
2116 
2117         if (!tb[spec->attr])
2118                 return NULL;
2119 
2120         nla_parse(linkinfo_tb, IFLA_INFO_MAX, nla_data(tb[spec->attr]), nla_len(tb[spec->attr]), NULL);
2121 
2122         info_obj = ucv_object_new(vm);
2123 
2124         if (linkinfo_tb[IFLA_INFO_KIND]) {
2125                 if (!uc_nl_convert_rta_linkinfo_data(info_obj, IFLA_INFO_KIND, msg, linkinfo_tb, vm)) {
2126                         ucv_put(info_obj);
2127 
2128                         return NULL;
2129                 }
2130         }
2131 
2132         if (linkinfo_tb[IFLA_INFO_SLAVE_KIND]) {
2133                 slave_obj = ucv_object_new(vm);
2134 
2135                 if (!uc_nl_convert_rta_linkinfo_data(slave_obj, IFLA_INFO_SLAVE_KIND, msg, linkinfo_tb, vm)) {
2136                         ucv_put(info_obj);
2137                         ucv_put(slave_obj);
2138 
2139                         return NULL;
2140                 }
2141 
2142                 ucv_object_add(info_obj, "slave", slave_obj);
2143         }
2144 
2145         return info_obj;
2146 }
2147 
2148 static uc_value_t *
2149 uc_nl_convert_rta_bridgeid(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
2150 {
2151         char buf[sizeof("ffff.ff:ff:ff:ff:ff:ff")];
2152         struct ifla_bridge_id *id;
2153 
2154         if (!nla_check_len(tb[spec->attr], sizeof(*id)))
2155                 return NULL;
2156 
2157         id = nla_data(tb[spec->attr]);
2158 
2159         snprintf(buf, sizeof(buf), "%02x%02x.%02x:%02x:%02x:%02x:%02x:%02x",
2160                 id->prio[0], id->prio[1],
2161                 id->addr[0], id->addr[1],
2162                 id->addr[2], id->addr[3],
2163                 id->addr[4], id->addr[5]);
2164 
2165         return ucv_string_new(buf);
2166 }
2167 
2168 static bool
2169 uc_nl_parse_rta_srh(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
2170 {
2171         uc_value_t *mode, *hmac, *segs, *seg;
2172         struct seg6_iptunnel_encap *tun;
2173         struct sr6_tlv_hmac *tlv;
2174         struct ipv6_sr_hdr *srh;
2175         size_t i, nsegs, srhlen;
2176         char *s;
2177 
2178         mode = ucv_object_get(val, "mode", NULL);
2179         hmac = ucv_object_get(val, "hmac", NULL);
2180         segs = ucv_object_get(val, "segs", NULL);
2181 
2182         if (mode != NULL &&
2183             (ucv_type(mode) != UC_INTEGER ||
2184              ucv_int64_get(mode) < 0 ||
2185              ucv_int64_get(mode) > UINT32_MAX))
2186                 return nla_parse_error(spec, vm, val, "srh mode not an integer in range 0-4294967295");
2187 
2188         if (hmac != NULL &&
2189             (ucv_type(hmac) != UC_INTEGER ||
2190              ucv_int64_get(hmac) < 0 ||
2191              ucv_int64_get(hmac) > UINT32_MAX))
2192                 return nla_parse_error(spec, vm, val, "srh hmac not an integer in range 0-4294967295");
2193 
2194         if (ucv_type(segs) != UC_ARRAY ||
2195             ucv_array_length(segs) == 0)
2196                 return nla_parse_error(spec, vm, val, "srh segs array missing or empty");
2197 
2198         nsegs = ucv_array_length(segs);
2199 
2200         if (!mode || !ucv_int64_get(mode))
2201                 nsegs++;
2202 
2203         srhlen = 8 + 16 * nsegs;
2204 
2205         if (hmac && ucv_int64_get(hmac))
2206                 srhlen += 40;
2207 
2208 
2209         tun = calloc(1, sizeof(*tun) + srhlen);
2210 
2211         if (!tun)
2212                 return nla_parse_error(spec, vm, val, "cannot allocate srh header");
2213 
2214         tun->mode = (int)ucv_int64_get(mode);
2215 
2216         srh = tun->srh;
2217         srh->hdrlen = (srhlen >> 3) - 1;
2218         srh->type = 4;
2219         srh->segments_left = nsegs - 1;
2220         srh->first_segment = nsegs - 1;
2221 
2222         if (hmac && ucv_int64_get(hmac))
2223                 srh->flags |= SR6_FLAG1_HMAC;
2224 
2225         for (i = 0; i < ucv_array_length(segs); i++) {
2226                 seg = ucv_array_get(segs, i);
2227                 s = ucv_string_get(seg);
2228 
2229                 if (!s || inet_pton(AF_INET6, s, &srh->segments[--nsegs]) != 1) {
2230                         free(tun);
2231 
2232                         return nla_parse_error(spec, vm, val, "srh segs array contains invalid IPv6 address");
2233                 }
2234         }
2235 
2236         if (hmac && ucv_int64_get(hmac)) {
2237                 tlv = (struct sr6_tlv_hmac *)((char *)srh + srhlen - 40);
2238                 tlv->tlvhdr.type = SR6_TLV_HMAC;
2239                 tlv->tlvhdr.len = 38;
2240                 tlv->hmackeyid = htonl((uint32_t)ucv_int64_get(hmac));
2241         }
2242 
2243         nla_put(msg, spec->attr, sizeof(*tun) + srhlen, tun);
2244         free(tun);
2245 
2246         return true;
2247 }
2248 
2249 static uc_value_t *
2250 uc_nl_convert_rta_srh(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
2251 {
2252         char buf[INET6_ADDRSTRLEN], *p, *e;
2253         struct seg6_iptunnel_encap *tun;
2254         uc_value_t *tun_obj, *seg_arr;
2255         struct sr6_tlv_hmac *tlv;
2256         size_t i;
2257 
2258         if (!nla_check_len(tb[spec->attr], sizeof(*tun)))
2259                 return NULL;
2260 
2261         tun = nla_data(tb[spec->attr]);
2262         tun_obj = ucv_object_new(vm);
2263 
2264         ucv_object_add(tun_obj, "mode", ucv_int64_new(tun->mode));
2265 
2266         seg_arr = ucv_array_new(vm);
2267 
2268         p = (char *)tun->srh->segments;
2269         e = (char *)tun + nla_len(tb[spec->attr]);
2270 
2271         for (i = tun->srh->first_segment + 1;
2272              p + sizeof(struct in6_addr) <= e && i > 0;
2273              i--, p += sizeof(struct in6_addr)) {
2274                 if (inet_ntop(AF_INET6, p, buf, sizeof(buf)))
2275                         ucv_array_push(seg_arr, ucv_string_new(buf));
2276                 else
2277                         ucv_array_push(seg_arr, NULL);
2278         }
2279 
2280         ucv_object_add(tun_obj, "segs", seg_arr);
2281 
2282         if (sr_has_hmac(tun->srh)) {
2283                 i = ((tun->srh->hdrlen + 1) << 3) - 40;
2284                 tlv = (struct sr6_tlv_hmac *)((char *)tun->srh + i);
2285 
2286                 ucv_object_add(tun_obj, "hmac", ucv_int64_new(ntohl(tlv->hmackeyid)));
2287         }
2288 
2289         return tun_obj;
2290 }
2291 
2292 #define ENCAP_TYPE(name, type) \
2293         { #name, LWTUNNEL_ENCAP_##type, route_encap_##name##_attrs, ARRAY_SIZE(route_encap_##name##_attrs) }
2294 
2295 static const struct {
2296         const char *name;
2297         uint16_t type;
2298         const uc_nl_attr_spec_t *attrs;
2299         size_t nattrs;
2300 } encap_types[] = {
2301         ENCAP_TYPE(mpls, MPLS),
2302         ENCAP_TYPE(ip, IP),
2303         ENCAP_TYPE(ip6, IP6),
2304         ENCAP_TYPE(ila, ILA),
2305         //ENCAP_TYPE(bpf, BPF),
2306         ENCAP_TYPE(seg6, SEG6),
2307         //ENCAP_TYPE(seg6local, SEG6_LOCAL),
2308         //ENCAP_TYPE(rpl, RPL),
2309 };
2310 
2311 static bool
2312 uc_nl_parse_rta_encap(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
2313 {
2314         const uc_nl_attr_spec_t *attrs = NULL;
2315         struct nlattr *enc_nla;
2316         size_t i, nattrs = 0;
2317         uint16_t ntype = 0;
2318         uc_value_t *t;
2319         char *type;
2320 
2321         t = ucv_object_get(val, "type", NULL);
2322         type = ucv_string_get(t);
2323 
2324         if (!type)
2325                 return nla_parse_error(spec, vm, val, "encap does not specify type");
2326 
2327         for (i = 0; i < ARRAY_SIZE(encap_types); i++) {
2328                 if (!strcmp(encap_types[i].name, type)) {
2329                         ntype = encap_types[i].type;
2330                         attrs = encap_types[i].attrs;
2331                         nattrs = encap_types[i].nattrs;
2332                         break;
2333                 }
2334         }
2335 
2336         if (!ntype)
2337                 return nla_parse_error(spec, vm, val, "encap specifies unknown type");
2338 
2339         nla_put_u16(msg, RTA_ENCAP_TYPE, ntype);
2340 
2341         enc_nla = nla_nest_start(msg, spec->attr);
2342 
2343         if (!enc_nla)
2344                 return nla_parse_error(spec, vm, val, "netlink message is full");
2345 
2346         if (!uc_nl_parse_attrs(msg, base, attrs, nattrs, vm, val))
2347                 return false;
2348 
2349         nla_nest_end(msg, enc_nla);
2350 
2351         return true;
2352 }
2353 
2354 static uc_value_t *
2355 uc_nl_convert_rta_encap(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
2356 {
2357         const uc_nl_attr_spec_t *attrs = NULL;
2358         const char *name = NULL;
2359         uc_value_t *encap_obj;
2360         size_t i, nattrs = 0;
2361         bool rv;
2362 
2363         if (!tb[spec->attr] ||
2364             !nla_check_len(tb[RTA_ENCAP_TYPE], sizeof(uint16_t)))
2365                 return NULL;
2366 
2367         for (i = 0; i < ARRAY_SIZE(encap_types); i++) {
2368                 if (encap_types[i].type != nla_get_u16(tb[RTA_ENCAP_TYPE]))
2369                         continue;
2370 
2371                 name = encap_types[i].name;
2372                 attrs = encap_types[i].attrs;
2373                 nattrs = encap_types[i].nattrs;
2374 
2375                 break;
2376         }
2377 
2378         if (!name)
2379                 return NULL;
2380 
2381         encap_obj = ucv_object_new(vm);
2382 
2383         rv = uc_nl_convert_attrs(msg,
2384                 nla_data(tb[spec->attr]), nla_len(tb[spec->attr]), 0,
2385                 attrs, nattrs, vm, encap_obj);
2386 
2387         if (!rv) {
2388                 ucv_put(encap_obj);
2389 
2390                 return NULL;
2391         }
2392 
2393         ucv_object_add(encap_obj, "type", ucv_string_new(name));
2394 
2395         return encap_obj;
2396 }
2397 
2398 #define IPOPTS_TYPE(name, type, multiple) \
2399         { #name, LWTUNNEL_IP_OPTS_##type, multiple, lwtipopt_##name##_attrs, ARRAY_SIZE(lwtipopt_##name##_attrs) }
2400 
2401 static const struct {
2402         const char *name;
2403         uint16_t type;
2404         bool multiple;
2405         const uc_nl_attr_spec_t *attrs;
2406         size_t nattrs;
2407 } lwtipopt_types[] = {
2408         IPOPTS_TYPE(erspan, ERSPAN, false),
2409         IPOPTS_TYPE(geneve, GENEVE, true),
2410         IPOPTS_TYPE(vxlan, VXLAN, false),
2411 };
2412 
2413 static bool
2414 uc_nl_parse_rta_ipopts(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
2415 {
2416         const uc_nl_attr_spec_t *attrs = NULL;
2417         struct nlattr *opt_nla, *type_nla;
2418         bool exists, multiple = false;
2419         size_t i, j, nattrs = 0;
2420         uint16_t ntype = 0;
2421         uc_value_t *item;
2422 
2423         ucv_object_foreach(val, type, v) {
2424                 for (i = 0; i < ARRAY_SIZE(lwtipopt_types); i++) {
2425                         if (!strcmp(lwtipopt_types[i].name, type)) {
2426                                 val = v;
2427                                 ntype = lwtipopt_types[i].type;
2428                                 attrs = lwtipopt_types[i].attrs;
2429                                 nattrs = lwtipopt_types[i].nattrs;
2430                                 multiple = lwtipopt_types[i].multiple;
2431                                 break;
2432                         }
2433                 }
2434         }
2435 
2436         if (!ntype)
2437                 return nla_parse_error(spec, vm, val, "unknown IP options type specified");
2438 
2439         opt_nla = nla_nest_start(msg, spec->attr);
2440 
2441         if (!opt_nla)
2442                 return nla_parse_error(spec, vm, val, "netlink message is full");
2443 
2444         j = 0;
2445         item = (ucv_type(val) == UC_ARRAY) ? ucv_array_get(val, j++) : val;
2446 
2447         while (true) {
2448                 type_nla = nla_nest_start(msg, ntype);
2449 
2450                 if (!type_nla)
2451                         return nla_parse_error(spec, vm, val, "netlink message is full");
2452 
2453                 for (i = 0; i < nattrs; i++) {
2454                         v = ucv_object_get(item, attrs[i].key, &exists);
2455 
2456                         if (!exists)
2457                                 continue;
2458 
2459                         if (!uc_nl_parse_attr(&attrs[i], msg, nla_data(type_nla), vm, v, 0))
2460                                 return false;
2461                 }
2462 
2463                 nla_nest_end(msg, type_nla);
2464 
2465                 if (!multiple || ucv_type(val) != UC_ARRAY || j >= ucv_array_length(val))
2466                         break;
2467 
2468                 item = ucv_array_get(val, j++);
2469         }
2470 
2471         nla_nest_end(msg, opt_nla);
2472 
2473         return true;
2474 }
2475 
2476 static uc_value_t *
2477 uc_nl_convert_rta_ipopts(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
2478 {
2479         struct nlattr *opt_tb[LWTUNNEL_IP_OPTS_MAX + 1];
2480         const uc_nl_attr_spec_t *attrs = NULL;
2481         uc_value_t *opt_obj, *type_obj;
2482         const char *name = NULL;
2483         size_t i, nattrs = 0;
2484         uint16_t type = 0;
2485         bool rv;
2486 
2487         if (!tb[spec->attr] ||
2488                 !nla_parse(opt_tb, LWTUNNEL_IP_OPTS_MAX, nla_data(tb[spec->attr]), nla_len(tb[spec->attr]), NULL))
2489                 return NULL;
2490 
2491         for (i = 0; i < ARRAY_SIZE(lwtipopt_types); i++) {
2492                 if (!opt_tb[lwtipopt_types[i].type])
2493                         continue;
2494 
2495                 type = lwtipopt_types[i].type;
2496                 name = lwtipopt_types[i].name;
2497                 attrs = lwtipopt_types[i].attrs;
2498                 nattrs = lwtipopt_types[i].nattrs;
2499 
2500                 break;
2501         }
2502 
2503         if (!name)
2504                 return NULL;
2505 
2506         type_obj = ucv_object_new(vm);
2507 
2508         rv = uc_nl_convert_attrs(msg,
2509                 nla_data(opt_tb[type]), nla_len(opt_tb[type]), 0,
2510                 attrs, nattrs, vm, type_obj);
2511 
2512         if (!rv) {
2513                 ucv_put(type_obj);
2514 
2515                 return NULL;
2516         }
2517 
2518         opt_obj = ucv_object_new(vm);
2519 
2520         ucv_object_add(opt_obj, name, type_obj);
2521 
2522         return opt_obj;
2523 }
2524 
2525 static bool
2526 uc_nl_parse_rta_afspec(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
2527 {
2528         struct rtgenmsg *rtg = nlmsg_data(nlmsg_hdr(msg));
2529         struct bridge_vlan_info vinfo = { 0 };
2530         uc_value_t *vlans, *vlan, *vv;
2531         struct nlattr *nla, *af_nla;
2532         uint32_t num;
2533         size_t i;
2534 
2535         nla = nla_reserve(msg, spec->attr, 0);
2536 
2537         if (!nla)
2538                 return nla_parse_error(spec, vm, val, "netlink message is full");
2539 
2540         ucv_object_foreach(val, type, v) {
2541                 if (!strcmp(type, "bridge")) {
2542                         if (rtg->rtgen_family == AF_UNSPEC)
2543                                 rtg->rtgen_family = AF_BRIDGE;
2544 
2545                         vv = ucv_object_get(v, "bridge_flags", NULL);
2546 
2547                         if (vv) {
2548                                 if (!uc_nl_parse_u32(vv, &num) || num > 0xffff)
2549                                         return nla_parse_error(spec, vm, vv, "field bridge.bridge_flags not an integer or out of range 0-65535");
2550 
2551                                 nla_put_u16(msg, IFLA_BRIDGE_FLAGS, num);
2552                         }
2553 
2554                         vv = ucv_object_get(v, "bridge_mode", NULL);
2555 
2556                         if (vv) {
2557                                 if (!uc_nl_parse_u32(vv, &num) || num > 0xffff)
2558                                         return nla_parse_error(spec, vm, vv, "field bridge.bridge_mode not an integer or out of range 0-65535");
2559 
2560                                 nla_put_u16(msg, IFLA_BRIDGE_MODE, num);
2561                         }
2562 
2563                         vlans = ucv_object_get(v, "bridge_vlan_info", NULL);
2564 
2565                         for (vlan = (ucv_type(vlans) == UC_ARRAY) ? ucv_array_get(vlans, 0) : vlans, i = 0;
2566                              ucv_type(vlan) == UC_OBJECT;
2567                              vlan = (ucv_type(vlans) == UC_ARRAY) ? ucv_array_get(vlans, ++i) : NULL) {
2568 
2569                                 vinfo.vid = 0;
2570                                 vinfo.flags = 0;
2571 
2572                                 vv = ucv_object_get(vlan, "flags", NULL);
2573 
2574                                 if (vv) {
2575                                         if (!uc_nl_parse_u32(vv, &num) || num > 0xffff)
2576                                                 return nla_parse_error(spec, vm, vv, "field bridge.bridge_vlan_info.flags not an integer or out of range 0-65535");
2577 
2578                                         vinfo.flags = num;
2579                                 }
2580 
2581                                 vv = ucv_object_get(vlan, "vid", NULL);
2582 
2583                                 if (!uc_nl_parse_u32(vv, &num) || num > 0xfff)
2584                                         return nla_parse_error(spec, vm, vv, "field bridge.bridge_vlan_info.vid not an integer or out of range 0-4095");
2585 
2586                                 vinfo.vid = num;
2587 
2588                                 vv = ucv_object_get(vlan, "vid_end", NULL);
2589 
2590                                 if (vv) {
2591                                         if (!uc_nl_parse_u32(vv, &num) || num > 0xfff)
2592                                                 return nla_parse_error(spec, vm, vv, "field bridge.bridge_vlan_info.vid_end not an integer or out of range 0-4095");
2593 
2594                                         vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_END;
2595                                         vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
2596                                         nla_put(msg, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
2597 
2598                                         vinfo.vid = num;
2599                                         vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
2600                                         vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
2601                                         nla_put(msg, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
2602                                 }
2603                                 else {
2604                                         vinfo.flags &= ~(BRIDGE_VLAN_INFO_RANGE_BEGIN|BRIDGE_VLAN_INFO_RANGE_END);
2605                                         nla_put(msg, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
2606                                 }
2607                         }
2608                 }
2609                 else if (!strcmp(type, "inet")) {
2610                         af_nla = nla_reserve(msg, AF_INET, link_attrs_af_spec_inet_rta.headsize);
2611 
2612                         if (!af_nla)
2613                                 return nla_parse_error(spec, vm, val,
2614                                         "netlink message is full");
2615 
2616                         if (!uc_nl_parse_attrs(msg, nla_data(af_nla),
2617                                                link_attrs_af_spec_inet_rta.attrs,
2618                                                link_attrs_af_spec_inet_rta.nattrs,
2619                                                vm, v))
2620                                 return false;
2621 
2622                         nla_nest_end(msg, af_nla);
2623                 }
2624                 else if (!strcmp(type, "inet6")) {
2625                         af_nla = nla_reserve(msg, AF_INET6, link_attrs_af_spec_inet6_rta.headsize);
2626 
2627                         if (!af_nla)
2628                                 return nla_parse_error(spec, vm, val,
2629                                         "netlink message is full");
2630 
2631                         if (!uc_nl_parse_attrs(msg, nla_data(af_nla),
2632                                                link_attrs_af_spec_inet6_rta.attrs,
2633                                                link_attrs_af_spec_inet6_rta.nattrs,
2634                                                vm, v))
2635                                 return false;
2636 
2637                         nla_nest_end(msg, af_nla);
2638                 }
2639                 else {
2640                         return nla_parse_error(spec, vm, val, "unknown address family specified");
2641                 }
2642         }
2643 
2644         nla_nest_end(msg, nla);
2645 
2646         return true;
2647 }
2648 
2649 static uc_value_t *
2650 uc_nl_convert_rta_afspec(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
2651 {
2652         struct rtgenmsg *rtg = nlmsg_data(nlmsg_hdr(msg));
2653         uc_value_t *obj, *bridge, *vlans = NULL, *vlan;
2654         struct bridge_vlan_info vinfo;
2655         struct nlattr *nla;
2656         uint16_t vid = 0;
2657         int rem;
2658 
2659         if (!tb[spec->attr])
2660                 return NULL;
2661 
2662         obj = ucv_object_new(vm);
2663 
2664         if (rtg->rtgen_family == AF_BRIDGE) {
2665                 bridge = ucv_object_new(vm);
2666 
2667                 nla_for_each_attr(nla, nla_data(tb[spec->attr]), nla_len(tb[spec->attr]), rem) {
2668                         switch (nla_type(nla)) {
2669                         case IFLA_BRIDGE_FLAGS:
2670                                 if (nla_check_len(nla, sizeof(uint16_t)))
2671                                         ucv_object_add(bridge, "bridge_flags", ucv_uint64_new(nla_get_u16(nla)));
2672 
2673                                 break;
2674 
2675                         case IFLA_BRIDGE_MODE:
2676                                 if (nla_check_len(nla, sizeof(uint16_t)))
2677                                         ucv_object_add(bridge, "bridge_mode", ucv_uint64_new(nla_get_u16(nla)));
2678 
2679                                 break;
2680 
2681                         case IFLA_BRIDGE_VLAN_INFO:
2682                                 if (nla_check_len(nla, sizeof(vinfo))) {
2683                                         memcpy(&vinfo, nla_data(nla), sizeof(vinfo));
2684 
2685                                         if (!(vinfo.flags & BRIDGE_VLAN_INFO_RANGE_END))
2686                                                 vid = vinfo.vid;
2687 
2688                                         if (vinfo.flags & BRIDGE_VLAN_INFO_RANGE_BEGIN)
2689                                                 continue;
2690 
2691                                         if (!vlans) {
2692                                                 vlans = ucv_array_new(vm);
2693                                                 ucv_object_add(bridge, "bridge_vlan_info", vlans);
2694                                         }
2695 
2696                                         vlan = ucv_object_new(vm);
2697 
2698                                         ucv_object_add(vlan, "vid", ucv_uint64_new(vid));
2699 
2700                                         if (vid != vinfo.vid)
2701                                                 ucv_object_add(vlan, "vid_end", ucv_uint64_new(vinfo.vid));
2702 
2703                                         ucv_object_add(vlan, "flags", ucv_uint64_new(vinfo.flags & ~BRIDGE_VLAN_INFO_RANGE_END));
2704 
2705                                         ucv_array_push(vlans, vlan);
2706                                 }
2707 
2708                                 break;
2709                         }
2710                 }
2711 
2712                 ucv_object_add(obj, "bridge", bridge);
2713         }
2714         else {
2715                 if (!uc_nl_convert_attrs(msg, nla_data(tb[spec->attr]), nla_len(tb[spec->attr]),
2716                                          link_attrs_af_spec_rta.headsize, link_attrs_af_spec_rta.attrs,
2717                                          link_attrs_af_spec_rta.nattrs, vm, obj)) {
2718                         ucv_put(obj);
2719 
2720                         return NULL;
2721                 }
2722         }
2723 
2724         return obj;
2725 }
2726 
2727 static bool
2728 uc_nl_parse_rta_u32_or_member(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
2729 {
2730         uint32_t u32;
2731 
2732         if (!uc_nl_parse_u32(val, &u32))
2733                 return nla_parse_error(spec, vm, val, "not an integer or out of range 0-4294967295");
2734 
2735         if (spec->flags & DF_MAX_255) {
2736                 if (u32 <= 255) {
2737                         uc_nl_put_struct_member_u8(base, spec->auxdata, u32);
2738 
2739                         return true;
2740                 }
2741 
2742                 uc_nl_put_struct_member_u8(base, spec->auxdata, 0);
2743         }
2744         else if (spec->flags & DF_MAX_65535) {
2745                 if (u32 <= 65535) {
2746                         uc_nl_put_struct_member_u16(base, spec->auxdata,
2747                                 (spec->flags & DF_BYTESWAP) ? htons((uint16_t)u32) : (uint16_t)u32);
2748 
2749                         return true;
2750                 }
2751 
2752                 uc_nl_put_struct_member_u16(base, spec->auxdata, 0);
2753         }
2754         else if (spec->flags & DF_MAX_16777215) {
2755                 if (u32 <= 16777215) {
2756                         uc_nl_put_struct_member_u32(base, spec->auxdata,
2757                                 (spec->flags & DF_BYTESWAP) ? htonl(u32) : u32);
2758 
2759                         return true;
2760                 }
2761 
2762                 uc_nl_put_struct_member_u32(base, spec->auxdata, 0);
2763         }
2764 
2765         nla_put_u32(msg, spec->attr,
2766                 (spec->flags & DF_BYTESWAP) ? htonl(u32) : u32);
2767 
2768         return true;
2769 }
2770 
2771 static uc_value_t *
2772 uc_nl_convert_rta_u32_or_member(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, struct nlattr **tb, uc_vm_t *vm)
2773 {
2774         uint32_t u32 = 0;
2775 
2776         if (nla_check_len(tb[spec->attr], sizeof(uint32_t))) {
2777                 if (spec->flags & DF_BYTESWAP)
2778                         u32 = ntohl(nla_get_u32(tb[spec->attr]));
2779                 else
2780                         u32 = nla_get_u32(tb[spec->attr]);
2781         }
2782         else if (spec->flags & DF_MAX_255) {
2783                 u32 = uc_nl_get_struct_member_u8(base, spec->auxdata);
2784         }
2785         else if (spec->flags & DF_MAX_65535) {
2786                 if (spec->flags & DF_BYTESWAP)
2787                         u32 = ntohs(uc_nl_get_struct_member_u16(base, spec->auxdata));
2788                 else
2789                         u32 = uc_nl_get_struct_member_u16(base, spec->auxdata);
2790         }
2791         else if (spec->flags & DF_MAX_16777215) {
2792                 if (spec->flags & DF_BYTESWAP)
2793                         u32 = ntohl(uc_nl_get_struct_member_u32(base, spec->auxdata));
2794                 else
2795                         u32 = uc_nl_get_struct_member_u32(base, spec->auxdata);
2796         }
2797         else {
2798                 return NULL;
2799         }
2800 
2801         return ucv_uint64_new(u32);
2802 }
2803 
2804 static bool
2805 uc_nl_parse_rta_nested(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val)
2806 {
2807         const uc_nl_nested_spec_t *nest = spec->auxdata;
2808         struct nlattr *nested_nla;
2809 
2810         nested_nla = nla_reserve(msg, spec->attr, nest->headsize);
2811 
2812         if (!nested_nla)
2813                 return false;
2814 
2815         /* nla_reserve() only zeroes the padding, not the payload, so struct
2816          * headers would otherwise carry heap garbage into the kernel */
2817         if (nest->headsize)
2818                 memset(nla_data(nested_nla), 0, nest->headsize);
2819 
2820         if (!uc_nl_parse_attrs(msg, nla_data(nested_nla), nest->attrs, nest->nattrs, vm, val))
2821                 return false;
2822 
2823         nla_nest_end(msg, nested_nla);
2824 
2825         return true;
2826 }
2827 
2828 static uc_value_t *
2829 uc_nl_convert_rta_nested(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, struct nlattr **tb, uc_vm_t *vm)
2830 {
2831         const uc_nl_nested_spec_t *nest = spec->auxdata;
2832         uc_value_t *nested_obj;
2833         bool rv;
2834 
2835         nested_obj = ucv_object_new(vm);
2836 
2837         rv = uc_nl_convert_attrs(msg,
2838                 nla_data(tb[spec->attr]), nla_len(tb[spec->attr]), nest->headsize,
2839                 nest->attrs, nest->nattrs,
2840                 vm, nested_obj);
2841 
2842         if (!rv) {
2843                 ucv_put(nested_obj);
2844 
2845                 return NULL;
2846         }
2847 
2848         return nested_obj;
2849 }
2850 
2851 
2852 static bool
2853 uc_nl_parse_attr(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, uc_vm_t *vm, uc_value_t *val, size_t idx)
2854 {
2855         uc_nl_cidr_t cidr = { 0 };
2856         struct ether_addr *ea;
2857         struct rtgenmsg *rtg;
2858         uint64_t u64;
2859         uint32_t u32;
2860         uint16_t u16;
2861         size_t attr;
2862         char *s;
2863 
2864         if (spec->flags & DF_MULTIPLE)
2865                 attr = idx;
2866         else
2867                 attr = spec->attr;
2868 
2869         switch (spec->type) {
2870         case DT_U8:
2871                 if (!uc_nl_parse_u32(val, &u32) || u32 > 255)
2872                         return nla_parse_error(spec, vm, val, "not an integer or out of range 0-255");
2873 
2874                 if ((spec->flags & DF_MAX_1) && u32 > 1)
2875                         return nla_parse_error(spec, vm, val, "integer must be 0 or 1");
2876 
2877                 if (spec->attr == 0)
2878                         uc_nl_put_struct_member_u8(base, spec->auxdata, u32);
2879                 else
2880                         nla_put_u8(msg, attr, u32);
2881 
2882                 break;
2883 
2884         case DT_U16:
2885                 if (!uc_nl_parse_u32(val, &u32) || u32 > 65535)
2886                         return nla_parse_error(spec, vm, val, "not an integer or out of range 0-65535");
2887 
2888                 u16 = (uint16_t)u32;
2889 
2890                 if (spec->flags & DF_BYTESWAP)
2891                         u16 = htons(u16);
2892 
2893                 if ((spec->flags & DF_MAX_1) && u32 > 1)
2894                         return nla_parse_error(spec, vm, val, "integer must be 0 or 1");
2895                 else if ((spec->flags & DF_MAX_255) && u32 > 255)
2896                         return nla_parse_error(spec, vm, val, "integer out of range 0-255");
2897 
2898                 if (spec->attr == 0)
2899                         uc_nl_put_struct_member_u16(base, spec->auxdata, u16);
2900                 else
2901                         nla_put_u16(msg, attr, u16);
2902 
2903                 break;
2904 
2905         case DT_S32:
2906         case DT_U32:
2907                 if (spec->type == DT_S32 && !uc_nl_parse_s32(val, &u32))
2908                         return nla_parse_error(spec, vm, val, "not an integer or out of range -2147483648-2147483647");
2909                 else if (spec->type == DT_U32 && !uc_nl_parse_u32(val, &u32))
2910                         return nla_parse_error(spec, vm, val, "not an integer or out of range 0-4294967295");
2911 
2912                 if (spec->flags & DF_BYTESWAP)
2913                         u32 = htonl(u32);
2914 
2915                 if ((spec->flags & DF_MAX_1) && u32 > 1)
2916                         return nla_parse_error(spec, vm, val, "integer must be 0 or 1");
2917                 else if ((spec->flags & DF_MAX_255) && u32 > 255)
2918                         return nla_parse_error(spec, vm, val, "integer out of range 0-255");
2919                 else if ((spec->flags & DF_MAX_65535) && u32 > 65535)
2920                         return nla_parse_error(spec, vm, val, "integer out of range 0-65535");
2921                 else if ((spec->flags & DF_MAX_16777215) && u32 > 16777215)
2922                         return nla_parse_error(spec, vm, val, "integer out of range 0-16777215");
2923 
2924                 if (spec->attr == 0)
2925                         uc_nl_put_struct_member_u32(base, spec->auxdata, u32);
2926                 else
2927                         nla_put_u32(msg, attr, u32);
2928 
2929                 break;
2930 
2931         case DT_U64:
2932                 assert(spec->attr != 0);
2933 
2934                 if (!uc_nl_parse_u64(val, &u64))
2935                         return nla_parse_error(spec, vm, val, "not an integer or negative");
2936 
2937                 if (spec->flags & DF_BYTESWAP)
2938                         u64 = htobe64(u64);
2939 
2940                 nla_put_u64(msg, attr, u64);
2941                 break;
2942 
2943         case DT_BOOL:
2944                 u32 = (uint32_t)ucv_is_truish(val);
2945 
2946                 if (spec->attr == 0)
2947                         uc_nl_put_struct_member_u8(base, spec->auxdata, u32);
2948                 else
2949                         nla_put_u8(msg, attr, u32);
2950 
2951                 break;
2952 
2953         case DT_FLAG:
2954                 u32 = (uint32_t)ucv_is_truish(val);
2955 
2956                 if (spec->attr == 0)
2957                         uc_nl_put_struct_member_u8(base, spec->auxdata, u32);
2958                 else if (u32 == 1)
2959                         nla_put_flag(msg, attr);
2960 
2961                 break;
2962 
2963         case DT_STRING:
2964                 assert(spec->attr != 0);
2965 
2966                 if (ucv_type(val) == UC_STRING) {
2967                         nla_put(msg, attr, ucv_string_length(val), ucv_string_get(val));
2968                 }
2969                 else {
2970                         s = ucv_to_string(vm, val);
2971 
2972                         if (!s)
2973                                 return nla_parse_error(spec, vm, val, "out of memory");
2974 
2975                         nla_put_string(msg, attr, s);
2976                         free(s);
2977                 }
2978 
2979                 break;
2980 
2981         case DT_NETDEV:
2982                 if (ucv_type(val) == UC_INTEGER) {
2983                         if (ucv_int64_get(val) < 0 ||
2984                             ucv_int64_get(val) > UINT32_MAX)
2985                                 return nla_parse_error(spec, vm, val, "interface index out of range 0-4294967295");
2986 
2987                         u32 = (uint32_t)ucv_int64_get(val);
2988                 }
2989                 else {
2990                         s = ucv_to_string(vm, val);
2991 
2992                         if (!s)
2993                                 return nla_parse_error(spec, vm, val, "out of memory");
2994 
2995                         u32 = if_nametoindex(s);
2996 
2997                         free(s);
2998                 }
2999 
3000                 if (u32 == 0 && !(spec->flags & DF_ALLOW_NONE))
3001                         return nla_parse_error(spec, vm, val, "interface not found");
3002 
3003                 if (spec->attr == 0)
3004                         uc_nl_put_struct_member_u32(base, spec->auxdata, u32);
3005                 else
3006                         nla_put_u32(msg, attr, u32);
3007 
3008                 break;
3009 
3010         case DT_LLADDR:
3011                 assert(spec->attr != 0);
3012 
3013                 s = ucv_to_string(vm, val);
3014 
3015                 if (!s)
3016                         return nla_parse_error(spec, vm, val, "out of memory");
3017 
3018                 ea = ether_aton(s);
3019 
3020                 free(s);
3021 
3022                 if (!ea)
3023                         return nla_parse_error(spec, vm, val, "invalid MAC address");
3024 
3025                 nla_put(msg, attr, sizeof(*ea), ea);
3026 
3027                 break;
3028 
3029         case DT_U64ADDR:
3030                 assert(spec->attr != 0);
3031 
3032                 if (ucv_type(val) == UC_INTEGER) {
3033                         u64 = ucv_uint64_get(val);
3034                 }
3035                 else {
3036                         s = ucv_to_string(vm, val);
3037 
3038                         if (!s)
3039                                 return nla_parse_error(spec, vm, val, "out of memory");
3040 
3041                         u16 = addr64_pton(s, &u64);
3042 
3043                         free(s);
3044 
3045                         if (u16 != 1)
3046                                 return nla_parse_error(spec, vm, val, "invalid address");
3047                 }
3048 
3049                 nla_put_u64(msg, attr, u64);
3050 
3051                 break;
3052 
3053         case DT_INADDR:
3054         case DT_IN6ADDR:
3055         case DT_MPLSADDR:
3056         case DT_ANYADDR:
3057                 assert(spec->attr != 0);
3058 
3059                 rtg = nlmsg_data(nlmsg_hdr(msg));
3060 
3061                 if (!uc_nl_parse_cidr(vm, val, &cidr))
3062                         return nla_parse_error(spec, vm, val, "invalid IP address");
3063 
3064                 if ((spec->type == DT_INADDR && cidr.family != AF_INET) ||
3065                     (spec->type == DT_IN6ADDR && cidr.family != AF_INET6) ||
3066                     (spec->type == DT_MPLSADDR && cidr.family != AF_MPLS))
3067                     return nla_parse_error(spec, vm, val, "wrong address family");
3068 
3069                 if (spec->flags & DF_STORE_MASK)
3070                         uc_nl_put_struct_member_u8(base, spec->auxdata, cidr.mask);
3071                 else if (cidr.mask != cidr.bitlen)
3072                         return nla_parse_error(spec, vm, val, "address range given but single address expected");
3073 
3074                 nla_put(msg, attr, cidr.alen, &cidr.addr.in6);
3075 
3076                 if ((rtg->rtgen_family == AF_UNSPEC) && (spec->flags & DF_FAMILY_HINT))
3077                         rtg->rtgen_family = cidr.family;
3078 
3079                 break;
3080 
3081         case DT_MULTIPATH:
3082                 if (!uc_nl_parse_rta_multipath(spec, msg, base, vm, val))
3083                         return nla_parse_error(spec, vm, val, "invalid nexthop data");
3084 
3085                 break;
3086 
3087         case DT_NUMRANGE:
3088                 if (!uc_nl_parse_rta_numrange(spec, msg, base, vm, val))
3089                         return false;
3090 
3091                 break;
3092 
3093         case DT_FLAGS:
3094                 if (ucv_array_length(val) == 2) {
3095                         if (ucv_type(ucv_array_get(val, 0)) != UC_INTEGER ||
3096                             ucv_type(ucv_array_get(val, 1)) != UC_INTEGER)
3097                                 return nla_parse_error(spec, vm, val, "flag or mask value not an integer");
3098 
3099                         if (!uc_nl_parse_u32(ucv_array_get(val, 0), &u32))
3100                                 return nla_parse_error(spec, vm, val, "flag value not an integer or out of range 0-4294967295");
3101 
3102                         memcpy(&u64, &u32, sizeof(u32));
3103 
3104                         if (!uc_nl_parse_u32(ucv_array_get(val, 1), &u32))
3105                                 return nla_parse_error(spec, vm, val, "mask value not an integer or out of range 0-4294967295");
3106 
3107                         memcpy((char *)&u64 + sizeof(u32), &u32, sizeof(u32));
3108                 }
3109                 else if (ucv_type(val) == UC_INTEGER) {
3110                         if (!uc_nl_parse_u32(val, &u32))
3111                                 return nla_parse_error(spec, vm, val, "flag value not an integer or out of range 0-4294967295");
3112 
3113                         memcpy(&u64, &u32, sizeof(u32));
3114                         memset((char *)&u64 + sizeof(u32), 0xff, sizeof(u32));
3115                 }
3116                 else {
3117                         return nla_parse_error(spec, vm, val, "value neither an array of flags, mask nor an integer");
3118                 }
3119 
3120                 if (spec->attr == 0)
3121                         uc_nl_put_struct_member(base, spec->auxdata, sizeof(u64), &u64);
3122                 else
3123                         nla_put_u64(msg, attr, u64);
3124 
3125                 break;
3126 
3127         case DT_LINKINFO:
3128                 if (!uc_nl_parse_rta_linkinfo(spec, msg, base, vm, val))
3129                         return false;
3130 
3131                 break;
3132 
3133         case DT_SRH:
3134                 if (!uc_nl_parse_rta_srh(spec, msg, base, vm, val))
3135                         return false;
3136 
3137                 break;
3138 
3139         case DT_ENCAP:
3140                 if (!uc_nl_parse_rta_encap(spec, msg, base, vm, val))
3141                         return false;
3142 
3143                 break;
3144 
3145         case DT_IPOPTS:
3146                 if (!uc_nl_parse_rta_ipopts(spec, msg, base, vm, val))
3147                         return false;
3148 
3149                 break;
3150 
3151         case DT_AFSPEC:
3152                 if (!uc_nl_parse_rta_afspec(spec, msg, base, vm, val))
3153                         return false;
3154 
3155                 break;
3156 
3157         case DT_U32_OR_MEMBER:
3158                 if (!uc_nl_parse_rta_u32_or_member(spec, msg, base, vm, val))
3159                         return false;
3160 
3161                 break;
3162 
3163         case DT_NESTED:
3164                 if (!uc_nl_parse_rta_nested(spec, msg, base, vm, val))
3165                         return false;
3166 
3167                 break;
3168 
3169         default:
3170                 assert(0);
3171         }
3172 
3173         return true;
3174 }
3175 
3176 static uc_value_t *
3177 uc_nl_convert_attr(const uc_nl_attr_spec_t *spec, struct nl_msg *msg, char *base, struct nlattr **tb, uc_vm_t *vm)
3178 {
3179         union { uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; size_t sz; } t = { 0 };
3180         struct { uint32_t flags; uint32_t mask; } flags;
3181         char buf[sizeof(struct mpls_label) * 16];
3182         struct nlmsghdr *hdr = nlmsg_hdr(msg);
3183         struct rtgenmsg *rtg = nlmsg_data(hdr);
3184         struct ether_addr *ea;
3185         uc_value_t *v;
3186         char *s;
3187 
3188         switch (spec->type) {
3189         case DT_U8:
3190                 if (spec->attr == 0)
3191                         t.u8 = uc_nl_get_struct_member_u8(base, spec->auxdata);
3192                 else if (nla_check_len(tb[spec->attr], sizeof(t.u8)))
3193                         t.u8 = nla_get_u8(tb[spec->attr]);
3194 
3195                 return ucv_uint64_new(t.u8);
3196 
3197         case DT_U16:
3198                 if (spec->attr == 0)
3199                         t.u16 = uc_nl_get_struct_member_u16(base, spec->auxdata);
3200                 else if (nla_check_len(tb[spec->attr], sizeof(t.u16)))
3201                         t.u16 = nla_get_u16(tb[spec->attr]);
3202 
3203                 if (spec->flags & DF_BYTESWAP)
3204                         t.u16 = ntohs(t.u16);
3205 
3206                 return ucv_uint64_new(t.u16);
3207 
3208         case DT_U32:
3209         case DT_S32:
3210                 if (spec->attr == 0)
3211                         t.u32 = uc_nl_get_struct_member_u32(base, spec->auxdata);
3212                 else if (nla_check_len(tb[spec->attr], sizeof(t.u32)))
3213                         t.u32 = nla_get_u32(tb[spec->attr]);
3214 
3215                 if (spec->flags & DF_BYTESWAP)
3216                         t.u32 = ntohl(t.u32);
3217 
3218                 if (spec->type == DT_S32)
3219                         return ucv_int64_new((int32_t)t.u32);
3220 
3221                 return ucv_uint64_new(t.u32);
3222 
3223         case DT_U64:
3224                 if (spec->attr == 0)
3225                         t.u64 = uc_nl_get_struct_member_u64(base, spec->auxdata);
3226                 else if (nla_check_len(tb[spec->attr], sizeof(t.u64)))
3227                         memcpy(&t.u64, nla_data(tb[spec->attr]), sizeof(t.u64));
3228 
3229                 return ucv_uint64_new(t.u64);
3230 
3231         case DT_BOOL:
3232                 if (spec->attr == 0)
3233                         t.u8 = uc_nl_get_struct_member_u8(base, spec->auxdata);
3234                 else if (nla_check_len(tb[spec->attr], sizeof(t.u8)))
3235                         t.u8 = nla_get_u8(tb[spec->attr]);
3236 
3237                 return ucv_boolean_new(t.u8 != 0);
3238 
3239         case DT_FLAG:
3240                 if (spec->attr == 0)
3241                         t.u8 = uc_nl_get_struct_member_u8(base, spec->auxdata);
3242                 else if (tb[spec->attr] != NULL)
3243                         t.u8 = 1;
3244 
3245                 return ucv_boolean_new(t.u8 != 0);
3246 
3247         case DT_STRING:
3248                 assert(spec->attr != 0);
3249 
3250                 if (!nla_check_len(tb[spec->attr], 1))
3251                         return NULL;
3252 
3253                 return ucv_string_new_length(
3254                         nla_data(tb[spec->attr]), nla_len(tb[spec->attr]) - 1);
3255 
3256         case DT_NETDEV:
3257                 if (spec->attr == 0)
3258                         t.u32 = uc_nl_get_struct_member_u32(base, spec->auxdata);
3259                 else if (nla_check_len(tb[spec->attr], sizeof(t.u32)))
3260                         t.u32 = nla_get_u32(tb[spec->attr]);
3261 
3262                 if (if_indextoname(t.u32, buf))
3263                         return ucv_string_new(buf);
3264                 else if (spec->flags & DF_ALLOW_NONE)
3265                         return ucv_int64_new(0);
3266 
3267                 return NULL;
3268 
3269         case DT_LLADDR:
3270                 assert(spec->attr != 0);
3271 
3272                 if (!nla_check_len(tb[spec->attr], sizeof(*ea)))
3273                         return NULL;
3274 
3275                 ea = nla_data(tb[spec->attr]);
3276 
3277                 snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
3278                         ea->ether_addr_octet[0], ea->ether_addr_octet[1],
3279                         ea->ether_addr_octet[2], ea->ether_addr_octet[3],
3280                         ea->ether_addr_octet[4], ea->ether_addr_octet[5]);
3281 
3282                 return ucv_string_new(buf);
3283 
3284         case DT_U64ADDR:
3285                 assert(spec->attr != 0);
3286 
3287                 if (!nla_check_len(tb[spec->attr], sizeof(uint64_t)) ||
3288                     !addr64_ntop(nla_data(tb[spec->attr]), buf, sizeof(buf)))
3289                         return NULL;
3290 
3291                 return ucv_string_new(buf);
3292 
3293         case DT_INADDR:
3294         case DT_IN6ADDR:
3295         case DT_MPLSADDR:
3296         case DT_ANYADDR:
3297                 assert(spec->attr != 0);
3298 
3299                 t.sz = (size_t)nla_len(tb[spec->attr]);
3300 
3301                 switch (spec->type) {
3302                 case DT_INADDR:
3303                         if (t.sz < sizeof(struct in_addr) ||
3304                             !inet_ntop(AF_INET, nla_data(tb[spec->attr]), buf, sizeof(buf)))
3305                                 return NULL;
3306 
3307                         break;
3308 
3309                 case DT_IN6ADDR:
3310                         if (t.sz < sizeof(struct in6_addr) ||
3311                             !inet_ntop(AF_INET6, nla_data(tb[spec->attr]), buf, sizeof(buf)))
3312                                 return NULL;
3313 
3314                         break;
3315 
3316                 case DT_MPLSADDR:
3317                         if (t.sz < sizeof(struct mpls_label) ||
3318                             !mpls_ntop(nla_data(tb[spec->attr]), t.sz, buf, sizeof(buf)))
3319                                 return NULL;
3320 
3321                         break;
3322 
3323                 default:
3324                         switch (rtg->rtgen_family) {
3325                         case AF_MPLS:
3326                                 if (t.sz < sizeof(struct mpls_label) ||
3327                                     !mpls_ntop(nla_data(tb[spec->attr]), t.sz, buf, sizeof(buf)))
3328                                         return NULL;
3329 
3330                                 break;
3331 
3332                         case AF_INET6:
3333                                 if (t.sz < sizeof(struct in6_addr) ||
3334                                     !inet_ntop(AF_INET6, nla_data(tb[spec->attr]), buf, sizeof(buf)))
3335                                         return NULL;
3336 
3337                                 break;
3338 
3339                         case AF_INET:
3340                                 if (t.sz < sizeof(struct in_addr) ||
3341                                     !inet_ntop(AF_INET, nla_data(tb[spec->attr]), buf, sizeof(buf)))
3342                                         return NULL;
3343 
3344                                 break;
3345 
3346                         default:
3347                                 return NULL;
3348                         }
3349 
3350                         break;
3351                 }
3352 
3353                 if (spec->flags & DF_STORE_MASK) {
3354                         s = buf + strlen(buf);
3355                         snprintf(s, buf + sizeof(buf) - s, "/%hhu",
3356                                 uc_nl_get_struct_member_u8(base, spec->auxdata));
3357                 }
3358 
3359                 return ucv_string_new(buf);
3360 
3361         case DT_MULTIPATH:
3362                 return uc_nl_convert_rta_multipath(spec, msg, tb, vm);
3363 
3364         case DT_NUMRANGE:
3365                 return uc_nl_convert_rta_numrange(spec, msg, tb, vm);
3366 
3367         case DT_FLAGS:
3368                 if (spec->attr == 0)
3369                         uc_nl_get_struct_member(base, spec->auxdata, sizeof(flags), &flags);
3370                 else if (nla_check_len(tb[spec->attr], sizeof(flags)))
3371                         memcpy(&flags, nla_data(tb[spec->attr]), sizeof(flags));
3372                 else
3373                         return NULL;
3374 
3375                 if (flags.mask == 0)
3376                         return ucv_uint64_new(flags.flags);
3377 
3378                 v = ucv_array_new(vm);
3379 
3380                 ucv_array_push(v, ucv_uint64_new(flags.flags));
3381                 ucv_array_push(v, ucv_uint64_new(flags.mask));
3382 
3383                 return v;
3384 
3385         case DT_LINKINFO:
3386                 return uc_nl_convert_rta_linkinfo(spec, msg, tb, vm);
3387 
3388         case DT_BRIDGEID:
3389                 return uc_nl_convert_rta_bridgeid(spec, msg, tb, vm);
3390 
3391         case DT_SRH:
3392                 return uc_nl_convert_rta_srh(spec, msg, tb, vm);
3393 
3394         case DT_ENCAP:
3395                 return uc_nl_convert_rta_encap(spec, msg, tb, vm);
3396 
3397         case DT_IPOPTS:
3398                 return uc_nl_convert_rta_ipopts(spec, msg, tb, vm);
3399 
3400         case DT_AFSPEC:
3401                 return uc_nl_convert_rta_afspec(spec, msg, tb, vm);
3402 
3403         case DT_U32_OR_MEMBER:
3404                 return uc_nl_convert_rta_u32_or_member(spec, msg, base, tb, vm);
3405 
3406         case DT_NESTED:
3407                 return uc_nl_convert_rta_nested(spec, msg, tb, vm);
3408 
3409         default:
3410                 assert(0);
3411         }
3412 
3413         return NULL;
3414 }
3415 
3416 
3417 static struct nl_sock *sock = NULL;
3418 static struct {
3419         struct nl_sock *evsock;
3420         struct uloop_fd evsock_fd;
3421         uint32_t groups[RTNL_GRPS_BITMAP_SIZE];
3422         int rcvbuf;
3423 } nl_conn;
3424 
3425 typedef enum {
3426         STATE_UNREPLIED,
3427         STATE_CONTINUE,
3428         STATE_REPLIED,
3429         STATE_ERROR
3430 } reply_state_t;
3431 
3432 typedef struct {
3433         reply_state_t state;
3434         uc_vm_t *vm;
3435         uc_value_t *res;
3436         int family;
3437         const uc_nl_nested_spec_t *spec;
3438 } request_state_t;
3439 
3440 
3441 /**
3442  * Query error information.
3443  *
3444  * Returns a string containing a description of the last occurred error or
3445  * `null` if there is no error information.
3446  *
3447  * @function module:rtnl#error
3448  *
3449  * @returns {?string}
3450  *
3451  * @example
3452  * // Trigger rtnl error
3453  * request('invalid_command', {}, {});
3454  *
3455  * // Print error (should yield error description)
3456  * print(error(), "\n");
3457  */
3458 static uc_value_t *
3459 uc_nl_error(uc_vm_t *vm, size_t nargs)
3460 {
3461         uc_stringbuf_t *buf;
3462         const char *s;
3463 
3464         if (last_error.code == 0)
3465                 return NULL;
3466 
3467         buf = ucv_stringbuf_new();
3468 
3469         if (last_error.code == NLE_FAILURE && last_error.msg) {
3470                 ucv_stringbuf_addstr(buf, last_error.msg, strlen(last_error.msg));
3471         }
3472         else {
3473                 s = nl_geterror(last_error.code);
3474 
3475                 ucv_stringbuf_addstr(buf, s, strlen(s));
3476 
3477                 if (last_error.msg)
3478                         ucv_stringbuf_printf(buf, ": %s", last_error.msg);
3479         }
3480 
3481         set_error(0, NULL);
3482 
3483         return ucv_stringbuf_finish(buf);
3484 }
3485 
3486 /*
3487  * route functions
3488  */
3489 
3490 static int
3491 cb_done(struct nl_msg *msg, void *arg)
3492 {
3493         request_state_t *s = arg;
3494 
3495         s->state = STATE_REPLIED;
3496 
3497         return NL_STOP;
3498 }
3499 
3500 static int
3501 cb_done_ack(struct nl_msg *msg, void *arg)
3502 {
3503         request_state_t *s = arg;
3504 
3505         if (s->state == STATE_UNREPLIED)
3506                 s->res = ucv_boolean_new(true);
3507 
3508         s->state = STATE_REPLIED;
3509 
3510         return NL_STOP;
3511 }
3512 
3513 static int
3514 cb_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
3515 {
3516         request_state_t *s = arg;
3517         int errnum = err->error;
3518 
3519         set_error(NLE_FAILURE, "RTNETLINK answers: %s",
3520                   strerror(errnum < 0 ? -errnum : errnum));
3521 
3522         s->state = STATE_ERROR;
3523 
3524         return NL_STOP;
3525 }
3526 
3527 static int
3528 cb_reply(struct nl_msg *msg, void *arg)
3529 {
3530         struct nlmsghdr *hdr = nlmsg_hdr(msg);
3531         request_state_t *s = arg;
3532         uc_value_t *o;
3533         bool rv;
3534 
3535         if (RTM_FAM(hdr->nlmsg_type) != s->family)
3536                 return NL_SKIP;
3537 
3538         if (s->spec) {
3539                 if (nlmsg_attrlen(hdr, 0) < (ssize_t)s->spec->headsize)
3540                         return NL_SKIP;
3541 
3542                 o = ucv_object_new(s->vm);
3543 
3544                 rv = uc_nl_convert_attrs(msg,
3545                         nlmsg_attrdata(hdr, 0),
3546                         nlmsg_attrlen(hdr, 0),
3547                         s->spec->headsize,
3548                         s->spec->attrs, s->spec->nattrs, s->vm, o);
3549 
3550                 if (rv) {
3551                         if (hdr->nlmsg_flags & NLM_F_MULTI) {
3552                                 if (!s->res)
3553                                         s->res = ucv_array_new(s->vm);
3554 
3555                                 ucv_array_push(s->res, o);
3556                         }
3557                         else {
3558                                 s->res = o;
3559                         }
3560                 }
3561                 else {
3562                         ucv_put(o);
3563                 }
3564         }
3565 
3566         s->state = STATE_CONTINUE;
3567 
3568         return NL_SKIP;
3569 }
3570 
3571 
3572 static const struct {
3573         int family;
3574         const uc_nl_nested_spec_t *spec;
3575 } rtm_families[] = {
3576         { RTM_FAM(RTM_GETLINK), &link_msg },
3577         { RTM_FAM(RTM_GETROUTE), &route_msg },
3578         { RTM_FAM(RTM_GETNEIGH), &neigh_msg },
3579         { RTM_FAM(RTM_GETADDR), &addr_msg },
3580         { RTM_FAM(RTM_GETRULE), &rule_msg },
3581         { RTM_FAM(RTM_GETADDRLABEL), &addrlabel_msg },
3582         { RTM_FAM(RTM_GETNEIGHTBL), &neightbl_msg },
3583         { RTM_FAM(RTM_GETNETCONF), &netconf_msg },
3584 };
3585 
3586 /**
3587  * Send a netlink request.
3588  *
3589  * Sends a netlink request with the specified command, flags, and payload.
3590  *
3591  * @function module:rtnl#request
3592  *
3593  * @param {string} cmd - The netlink command to send
3594  * @param {number} flags - The netlink flags for the request
3595  * @param {*} payload - The payload data for the request
3596  *
3597  * @returns {?(Object|Object[]|boolean)} - The response data, true for a
3598  *                                         successful acknowledgement without
3599  *                                         data, false when the kernel refuses
3600  *                                         the request or the reply cannot be
3601  *                                         received, or null for a dump
3602  *                                         without entries and when the
3603  *                                         request cannot be sent
3604  *
3605  * @example
3606  * // Send a route request
3607  * let response = request('RTM_GETROUTE', 0, { family: AF_INET });
3608  */
3609 static uc_value_t *
3610 uc_nl_request(uc_vm_t *vm, size_t nargs)
3611 {
3612         uc_value_t *cmd = uc_fn_arg(0);
3613         uc_value_t *flags = uc_fn_arg(1);
3614         uc_value_t *payload = uc_fn_arg(2);
3615         request_state_t st = { .vm = vm };
3616         uint16_t flagval = 0;
3617         struct nl_msg *msg;
3618         struct nl_cb *cb;
3619         socklen_t optlen;
3620         int enable, err;
3621         void *buf;
3622         size_t i;
3623 
3624         if (ucv_type(cmd) != UC_INTEGER || ucv_int64_get(cmd) < 0 ||
3625             (flags != NULL && ucv_type(flags) != UC_INTEGER) ||
3626             (payload != NULL && ucv_type(payload) != UC_OBJECT))
3627                 err_return(NLE_INVAL, NULL);
3628 
3629         if (flags) {
3630                 if (ucv_int64_get(flags) < 0 || ucv_int64_get(flags) > 0xffff)
3631                         err_return(NLE_INVAL, NULL);
3632                 else
3633                         flagval = (uint16_t)ucv_int64_get(flags);
3634         }
3635 
3636         for (i = 0; i < ARRAY_SIZE(rtm_families); i++) {
3637                 if (rtm_families[i].family == RTM_FAM(ucv_int64_get(cmd))) {
3638                         st.spec = rtm_families[i].spec;
3639                         st.family = rtm_families[i].family;
3640                         break;
3641                 }
3642         }
3643 
3644         if (!sock) {
3645                 sock = nl_socket_alloc();
3646 
3647                 if (!sock)
3648                         err_return(NLE_NOMEM, NULL);
3649 
3650                 err = nl_connect(sock, NETLINK_ROUTE);
3651 
3652                 if (err != 0)
3653                         err_return(err, NULL);
3654         }
3655 
3656         optlen = sizeof(enable);
3657 
3658         if (getsockopt(sock->s_fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK, &enable, &optlen) < 0)
3659                 enable = 0;
3660 
3661         if (!!(flagval & NLM_F_STRICT_CHK) != enable) {
3662                 enable = !!(flagval & NLM_F_STRICT_CHK);
3663 
3664                 if (setsockopt(sock->s_fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK, &enable, sizeof(enable)) < 0)
3665                         err_return(nl_syserr2nlerr(errno), "Unable to toggle NETLINK_GET_STRICT_CHK");
3666         }
3667 
3668         msg = nlmsg_alloc_simple(ucv_int64_get(cmd), NLM_F_REQUEST | (flagval & ~NLM_F_STRICT_CHK));
3669 
3670         if (!msg)
3671                 err_return(NLE_NOMEM, NULL);
3672 
3673         if (st.spec) {
3674                 if (st.spec->headsize) {
3675                         buf = nlmsg_reserve(msg, st.spec->headsize, 0);
3676 
3677                         if (!buf) {
3678                                 nlmsg_free(msg);
3679 
3680                                 return NULL;
3681                         }
3682 
3683                         memset(buf, 0, st.spec->headsize);
3684                 }
3685 
3686                 if (!uc_nl_parse_attrs(msg, NLMSG_DATA(nlmsg_hdr(msg)), st.spec->attrs, st.spec->nattrs, vm, payload)) {
3687                         nlmsg_free(msg);
3688 
3689                         return NULL;
3690                 }
3691         }
3692 
3693         cb = nl_cb_alloc(NL_CB_DEFAULT);
3694 
3695         if (!cb) {
3696                 nlmsg_free(msg);
3697                 err_return(NLE_NOMEM, NULL);
3698         }
3699 
3700         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_reply, &st);
3701         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_done, &st);
3702         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, cb_done_ack, &st);
3703         nl_cb_err(cb, NL_CB_CUSTOM, cb_error, &st);
3704 
3705         nl_send_auto_complete(sock, msg);
3706 
3707         do {
3708                 err = nl_recvmsgs(sock, cb);
3709 
3710                 if (err && st.state != STATE_ERROR) {
3711                         set_error(err, NULL);
3712 
3713                         st.state = STATE_ERROR;
3714                 }
3715         }
3716         while (st.state < STATE_REPLIED);
3717 
3718         nlmsg_free(msg);
3719         nl_cb_put(cb);
3720 
3721         switch (st.state) {
3722         case STATE_REPLIED:
3723                 return st.res;
3724 
3725         case STATE_UNREPLIED:
3726                 return ucv_boolean_new(true);
3727 
3728         case STATE_ERROR:
3729                 ucv_put(st.res);
3730 
3731                 return ucv_boolean_new(false);
3732 
3733         default:
3734                 ucv_put(st.res);
3735                 set_error(NLE_FAILURE, "Interrupted reply");
3736 
3737                 return ucv_boolean_new(false);
3738         }
3739 }
3740 
3741 static const uc_nl_nested_spec_t *
3742 uc_nl_msg_spec(int type)
3743 {
3744         switch (type) {
3745         case RTM_NEWLINK:
3746         case RTM_DELLINK:
3747                 return &link_msg;
3748         case RTM_NEWROUTE:
3749         case RTM_DELROUTE:
3750                 return &route_msg;
3751         case RTM_NEWNEIGH:
3752         case RTM_DELNEIGH:
3753                 return &neigh_msg;
3754         case RTM_NEWADDR:
3755         case RTM_DELADDR:
3756                 return &addr_msg;
3757         case RTM_NEWRULE:
3758         case RTM_DELRULE:
3759                 return &rule_msg;
3760         case RTM_NEWADDRLABEL:
3761         case RTM_DELADDRLABEL:
3762                 return &addrlabel_msg;
3763         case RTM_NEWNEIGHTBL:
3764                 return &neightbl_msg;
3765         case RTM_NEWNETCONF:
3766         case RTM_DELNETCONF:
3767                 return &netconf_msg;
3768         default:
3769                 return NULL;
3770         }
3771 }
3772 
3773 static void
3774 uc_nl_prepare_event(uc_vm_t *vm, uc_value_t *dest, struct nl_msg *msg)
3775 {
3776         struct nlmsghdr *hdr = nlmsg_hdr(msg);
3777         const uc_nl_nested_spec_t *spec;
3778         const uc_nl_attr_spec_t *attrs = NULL;
3779         size_t nattrs = 0, headsize = 0;
3780         uc_value_t *o;
3781 
3782         spec = uc_nl_msg_spec(hdr->nlmsg_type);
3783         if (spec) {
3784                 attrs = spec->attrs;
3785                 nattrs = spec->nattrs;
3786                 headsize = spec->headsize;
3787         }
3788 
3789         o = ucv_object_new(vm);
3790         if (!uc_nl_convert_attrs(msg, nlmsg_attrdata(hdr, 0),
3791                 nlmsg_attrlen(hdr, 0), headsize, attrs, nattrs, vm, o)) {
3792                 ucv_put(o);
3793                 return;
3794         }
3795 
3796         ucv_object_add(dest, "msg", o);
3797         if (headsize)
3798                 ucv_object_add(dest, "head", ucv_string_new_length(NLMSG_DATA(hdr), headsize));
3799 }
3800 
3801 static bool
3802 uc_nl_fill_cmds(uint32_t *cmd_bits, uc_value_t *cmds)
3803 {
3804         if (ucv_type(cmds) == UC_ARRAY) {
3805                 for (size_t i = 0; i < ucv_array_length(cmds); i++) {
3806                         int64_t n = ucv_int64_get(ucv_array_get(cmds, i));
3807 
3808                         if (errno || n < 0 || n >= __RTM_MAX)
3809                                 return false;
3810 
3811                         cmd_bits[n / 32] |= (1 << (n % 32));
3812                 }
3813         }
3814         else if (ucv_type(cmds) == UC_INTEGER) {
3815                 int64_t n = ucv_int64_get(cmds);
3816 
3817                 if (errno || n < 0 || n > 255)
3818                         return false;
3819 
3820                 cmd_bits[n / 32] |= (1 << (n % 32));
3821         }
3822         else if (!cmds)
3823                 memset(cmd_bits, 0xff, RTNL_CMDS_BITMAP_SIZE * sizeof(*cmd_bits));
3824         else
3825                 return false;
3826 
3827         return true;
3828 }
3829 
3830 static int
3831 cb_listener_event(struct nl_msg *msg, void *arg)
3832 {
3833         struct nlmsghdr *hdr = nlmsg_hdr(msg);
3834         uc_vm_t *vm = listener_vm;
3835         int cmd = hdr->nlmsg_type;
3836 
3837         if (!nl_conn.evsock_fd.registered || !vm)
3838                 return NL_SKIP;
3839 
3840         for (size_t i = 0; i < ucv_array_length(listener_registry); i += LISTENER_SLOTS) {
3841                 uc_value_t *this = ucv_array_get(listener_registry, i + LISTENER_SLOT_RES);
3842                 uc_value_t *func = ucv_array_get(listener_registry, i + LISTENER_SLOT_CB);
3843                 uc_nl_listener_t *l;
3844                 uc_value_t *o;
3845 
3846                 l = ucv_resource_data(this, "rtnl.listener");
3847                 if (!l)
3848                         continue;
3849 
3850                 if (cmd > __RTM_MAX || !(l->cmds[cmd / 32] & (1 << (cmd % 32))))
3851                         continue;
3852 
3853                 if (!ucv_is_callable(func))
3854                         continue;
3855 
3856                 o = ucv_object_new(vm);
3857                 uc_nl_prepare_event(vm, o, msg);
3858                 ucv_object_add(o, "cmd", ucv_int64_new(cmd));
3859 
3860                 uc_vm_stack_push(vm, ucv_get(this));
3861                 uc_vm_stack_push(vm, ucv_get(func));
3862                 uc_vm_stack_push(vm, o);
3863 
3864                 if (uc_vm_call(vm, true, 1) != EXCEPTION_NONE) {
3865                         uloop_end();
3866                         set_error(NLE_FAILURE, "Runtime exception in callback");
3867 
3868                         errno = EINVAL;
3869 
3870                         return NL_STOP;
3871                 }
3872 
3873                 ucv_put(uc_vm_stack_pop(vm));
3874         }
3875 
3876         errno = 0;
3877 
3878         return NL_SKIP;
3879 }
3880 
3881 static void
3882 uc_nl_listener_overrun_notify(void)
3883 {
3884         uc_vm_t *vm = listener_vm;
3885 
3886         for (size_t i = 0; i < ucv_array_length(listener_registry); i += LISTENER_SLOTS) {
3887                 uc_value_t *this = ucv_array_get(listener_registry, i + LISTENER_SLOT_RES);
3888                 uc_value_t *func = ucv_array_get(listener_registry, i + LISTENER_SLOT_OVERRUN_CB);
3889 
3890                 if (!ucv_resource_data(this, "rtnl.listener") || !ucv_is_callable(func))
3891                         continue;
3892 
3893                 uc_vm_stack_push(vm, ucv_get(this));
3894                 uc_vm_stack_push(vm, ucv_get(func));
3895 
3896                 if (uc_vm_call(vm, true, 0) != EXCEPTION_NONE) {
3897                         uloop_end();
3898                         set_error(NLE_FAILURE, "Runtime exception in callback");
3899 
3900                         return;
3901                 }
3902 
3903                 ucv_put(uc_vm_stack_pop(vm));
3904         }
3905 }
3906 
3907 static void
3908 uc_nl_evsock_rcvbuf_set(int size)
3909 {
3910         int fd = nl_socket_get_fd(nl_conn.evsock);
3911 
3912         /* SO_RCVBUFFORCE exceeds net.core.rmem_max but needs CAP_NET_ADMIN */
3913         if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size)) < 0)
3914                 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
3915 
3916         nl_conn.rcvbuf = size;
3917 }
3918 
3919 static void
3920 uc_nl_evsock_overrun(void)
3921 {
3922         if (nl_conn.rcvbuf < RTNL_EVSOCK_RCVBUF_MAX)
3923                 uc_nl_evsock_rcvbuf_set(nl_conn.rcvbuf * 2);
3924 
3925         if (uloop_cancelled)
3926                 return;
3927 
3928         uc_nl_listener_overrun_notify();
3929 }
3930 
3931 /* any error the kernel sets on a multicast socket means a lost event */
3932 static bool
3933 uc_nl_evsock_error_take(struct uloop_fd *fd)
3934 {
3935         socklen_t len = sizeof(int);
3936         int err = 0;
3937 
3938         if (!fd->error)
3939                 return false;
3940 
3941         fd->error = false;
3942 
3943         if (getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
3944                 return false;
3945 
3946         return err != 0;
3947 }
3948 
3949 /*
3950  * The kernel sets ENOBUFS once per congestion and ends the congestion only
3951  * when the queue is empty, so drain the queue fully.
3952  */
3953 static void
3954 uc_nl_listener_cb(struct uloop_fd *fd, unsigned int events)
3955 {
3956         bool overrun = uc_nl_evsock_error_take(fd);
3957         int err;
3958 
3959         while (true) {
3960                 errno = 0;
3961 
3962                 err = nl_recvmsgs_default(nl_conn.evsock);
3963 
3964                 if (err == -NLE_NOMEM && errno == ENOBUFS) {
3965                         overrun = true;
3966                         continue;
3967                 }
3968 
3969                 if (err < 0 || errno != 0)
3970                         break;
3971         }
3972 
3973         if (overrun)
3974                 uc_nl_evsock_overrun();
3975 }
3976 
3977 static void
3978 uc_nl_add_group(unsigned int idx)
3979 {
3980         if (idx >= __RTNLGRP_MAX)
3981                 return;
3982 
3983         if (nl_conn.groups[idx / 32] & (1 << (idx % 32)))
3984                 return;
3985 
3986         nl_conn.groups[idx / 32] |= (1 << (idx % 32));
3987         nl_socket_add_membership(nl_conn.evsock, idx);
3988 }
3989 
3990 static bool
3991 uc_nl_evsock_init(void)
3992 {
3993         struct uloop_fd *fd = &nl_conn.evsock_fd;
3994         struct nl_sock *sock;
3995 
3996         if (nl_conn.evsock)
3997                 return true;
3998 
3999         sock = nl_socket_alloc();
4000 
4001         if (nl_connect(sock, NETLINK_ROUTE))
4002                 goto free;
4003 
4004         nl_conn.evsock = sock;
4005 
4006         fd->fd = nl_socket_get_fd(sock);
4007         fd->cb = uc_nl_listener_cb;
4008         uloop_fd_add(fd, ULOOP_READ | ULOOP_ERROR_CB);
4009 
4010         uc_nl_evsock_rcvbuf_set(RTNL_EVSOCK_RCVBUF_INIT);
4011         nl_socket_disable_seq_check(sock);
4012         nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, cb_listener_event, NULL);
4013 
4014         return true;
4015 
4016 free:
4017         nl_socket_free(sock);
4018         return false;
4019 }
4020 
4021 /**
4022  * Represents a netlink listener resource.
4023  *
4024  * @class module:rtnl.listener
4025  * @hideconstructor
4026  *
4027  * @see {@link module:rtnl#listener|listener()}
4028  *
4029  * @example
4030  * const nlListener = listener((msg) => {
4031  *     print('Received netlink message:', msg, '\n');
4032  * }, [RTM_NEWROUTE, RTM_DELROUTE]);
4033  *
4034  * nlListener.set_commands([RTM_GETLINK, RTM_SETLINK]);
4035  *
4036  * nlListener.close();
4037  */
4038 
4039 /**
4040  * Create a netlink listener.
4041  *
4042  * Creates a new netlink listener that will receive messages matching the specified
4043  * commands and multicast groups.
4044  *
4045  * @function module:rtnl#listener
4046  *
4047  * @param {function} callback - The callback function to invoke when a message is received
4048  * @param {Array<string>} [commands] - Array of netlink commands to listen for (optional)
4049  * @param {Array<number>} [groups] - Array of multicast groups to join (optional)
4050  *
4051  * @returns {module:rtnl.listener} - A listener object with methods to control the listener
4052  *
4053  * @example
4054  * // Create a listener for route changes
4055  * let routeListener = listener((msg) => {
4056  *     print('Received route message:', msg, '\n');
4057  * }, [RTM_NEWROUTE, RTM_DELROUTE]);
4058  */
4059 static uc_value_t *
4060 uc_nl_listener(uc_vm_t *vm, size_t nargs)
4061 {
4062         uc_nl_listener_t *l;
4063         uc_value_t *cb_func = uc_fn_arg(0);
4064         uc_value_t *cmds = uc_fn_arg(1);
4065         uc_value_t *groups = uc_fn_arg(2);
4066         uc_value_t *rv;
4067         size_t i;
4068 
4069         if (!ucv_is_callable(cb_func)) {
4070                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid callback");
4071                 return NULL;
4072         }
4073 
4074         if (!uc_nl_evsock_init())
4075                 return NULL;
4076 
4077         if (ucv_type(groups) == UC_ARRAY) {
4078                 for (i = 0; i < ucv_array_length(groups); i++) {
4079                         int64_t n = ucv_int64_get(ucv_array_get(groups, i));
4080 
4081                         if (errno || n < 0 || n >= __RTNLGRP_MAX)
4082                                 err_return(NLE_INVAL, NULL);
4083 
4084                         uc_nl_add_group(n);
4085                 }
4086         } else {
4087                 uc_nl_add_group(RTNLGRP_LINK);
4088         }
4089 
4090         for (i = 0; i < ucv_array_length(listener_registry); i += LISTENER_SLOTS) {
4091                 if (!ucv_array_get(listener_registry, i + LISTENER_SLOT_RES))
4092                         break;
4093         }
4094 
4095         l = xalloc(sizeof(*l));
4096         l->index = i;
4097 
4098         if (!uc_nl_fill_cmds(l->cmds, cmds)) {
4099                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid command ID");
4100                 free(l);
4101                 return NULL;
4102         }
4103 
4104         rv = uc_resource_new(listener_type, l);
4105 
4106         ucv_array_set(listener_registry, i + LISTENER_SLOT_RES, ucv_get(rv));
4107         ucv_array_set(listener_registry, i + LISTENER_SLOT_CB, ucv_get(cb_func));
4108         ucv_array_set(listener_registry, i + LISTENER_SLOT_OVERRUN_CB, NULL);
4109 
4110         listener_vm = vm;
4111 
4112         return rv;
4113 }
4114 
4115 static void
4116 uc_nl_listener_free(void *arg)
4117 {
4118         uc_nl_listener_t *l = arg;
4119 
4120         if (!l)
4121                 return;
4122 
4123         ucv_array_set(listener_registry, l->index + LISTENER_SLOT_RES, NULL);
4124         ucv_array_set(listener_registry, l->index + LISTENER_SLOT_CB, NULL);
4125         ucv_array_set(listener_registry, l->index + LISTENER_SLOT_OVERRUN_CB, NULL);
4126         free(l);
4127 }
4128 
4129 /**
4130  * Set the commands for a netlink listener.
4131  *
4132  * Updates the set of netlink commands that the listener will receive.
4133  *
4134  * @function module:rtnl.listener#set_commands
4135  *
4136  * @param {Array<string>} commands - Array of netlink commands to listen for
4137  *
4138  * @returns {boolean} - true if successful, false on error
4139  *
4140  * @example
4141  * // Update listener to only receive route messages
4142  * listener.set_commands([RTM_NEWROUTE, RTM_DELROUTE]);
4143  */
4144 static uc_value_t *
4145 uc_nl_listener_set_commands(uc_vm_t *vm, size_t nargs)
4146 {
4147         uc_nl_listener_t *l = uc_fn_thisval("rtnl.listener");
4148         uc_value_t *cmds = uc_fn_arg(0);
4149 
4150         if (!l)
4151                 return NULL;
4152 
4153         memset(l->cmds, 0, sizeof(l->cmds));
4154         if (!uc_nl_fill_cmds(l->cmds, cmds))
4155                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid command ID");
4156 
4157         return NULL;
4158 }
4159 
4160 /**
4161  * Set the overrun handler of a netlink listener.
4162  *
4163  * The kernel drops event messages when the receive buffer of the event
4164  * socket is full. The module then enlarges the buffer and calls the
4165  * overrun handler of each listener without arguments.
4166  *
4167  * Events of every joined group can be lost, including deletions. The
4168  * handler must dump each table that the script keeps state for. It must
4169  * treat every entry of the dump as new or changed, and every cached entry
4170  * that is missing from the dump as deleted.
4171  *
4172  * @function module:rtnl.listener#set_overrun_handler
4173  *
4174  * @param {?function} handler - The function to call after an overrun, or
4175  * `null` to remove it
4176  *
4177  * @returns {?boolean} - true if successful, null if the listener is closed
4178  *
4179  * @example
4180  * // Rebuild the link cache after lost events
4181  * listener.set_overrun_handler(() => {
4182  *     let dump = request(RTM_GETLINK, NLM_F_DUMP, {});
4183  *     let seen = {};
4184  *
4185  *     if (dump === false)
4186  *         return;
4187  *
4188  *     for (let link in dump ?? []) {
4189  *         seen[link.ifname] = true;
4190  *         links[link.ifname] = link;
4191  *     }
4192  *
4193  *     for (let name in keys(links))
4194  *         if (!seen[name])
4195  *             delete links[name];
4196  * });
4197  */
4198 static uc_value_t *
4199 uc_nl_listener_set_overrun_handler(uc_vm_t *vm, size_t nargs)
4200 {
4201         uc_nl_listener_t *l = uc_fn_thisval("rtnl.listener");
4202         uc_value_t *handler = uc_fn_arg(0);
4203 
4204         if (!l)
4205                 return NULL;
4206 
4207         if (handler && !ucv_is_callable(handler)) {
4208                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid overrun handler");
4209                 return NULL;
4210         }
4211 
4212         ucv_array_set(listener_registry, l->index + LISTENER_SLOT_OVERRUN_CB, ucv_get(handler));
4213 
4214         return ucv_boolean_new(true);
4215 }
4216 
4217 /**
4218  * Close a netlink listener.
4219  *
4220  * Closes the netlink listener and stops receiving messages.
4221  *
4222  * @function module:rtnl.listener#close
4223  *
4224  * @returns {boolean} - true if successful, false on error
4225  *
4226  * @example
4227  * // Close the listener
4228  * listener.close();
4229  */
4230 static uc_value_t *
4231 uc_nl_listener_close(uc_vm_t *vm, size_t nargs)
4232 {
4233         uc_nl_listener_t **lptr = uc_fn_this("rtnl.listener");
4234         uc_nl_listener_t *l;
4235 
4236         if (!lptr)
4237                 return NULL;
4238 
4239         l = *lptr;
4240         if (!l)
4241                 return NULL;
4242 
4243         *lptr = NULL;
4244         uc_nl_listener_free(l);
4245 
4246         return NULL;
4247 }
4248 
4249 
4250 static void
4251 register_constants(uc_vm_t *vm, uc_value_t *scope)
4252 {
4253         uc_value_t *c = ucv_object_new(vm);
4254 
4255 #define ADD_CONST(x) ucv_object_add(c, #x, ucv_int64_new(x))
4256 
4257         /**
4258          * @typedef
4259          * @name Netlink message flags
4260          * @property {number} NLM_F_ACK - Request for acknowledgment
4261          * @property {number} NLM_F_ACK_TLVS - Request for acknowledgment with TLVs
4262          * @property {number} NLM_F_APPEND - Append to existing list
4263          * @property {number} NLM_F_ATOMIC - Atomic operation
4264          * @property {number} NLM_F_CAPPED - Request capped
4265          * @property {number} NLM_F_CREATE - Create if not exists
4266          * @property {number} NLM_F_DUMP - Dump request
4267          * @property {number} NLM_F_DUMP_FILTERED - Dump filtered request
4268          * @property {number} NLM_F_DUMP_INTR - Dump interrupted
4269          * @property {number} NLM_F_ECHO - Echo request
4270          * @property {number} NLM_F_EXCL - Exclusive creation
4271          * @property {number} NLM_F_MATCH - Match request
4272          * @property {number} NLM_F_MULTI - Multi-part message
4273          * @property {number} NLM_F_NONREC - Non-recursive operation
4274          * @property {number} NLM_F_REPLACE - Replace existing
4275          * @property {number} NLM_F_REQUEST - Request message
4276          * @property {number} NLM_F_ROOT - Root operation
4277          * @property {number} NLM_F_STRICT_CHK - Strict checking
4278          */
4279         ADD_CONST(NLM_F_ACK);
4280         ADD_CONST(NLM_F_ACK);
4281         ADD_CONST(NLM_F_ACK_TLVS);
4282         ADD_CONST(NLM_F_APPEND);
4283         ADD_CONST(NLM_F_ATOMIC);
4284         ADD_CONST(NLM_F_CAPPED);
4285         ADD_CONST(NLM_F_CREATE);
4286         ADD_CONST(NLM_F_DUMP);
4287         ADD_CONST(NLM_F_DUMP_FILTERED);
4288         ADD_CONST(NLM_F_DUMP_INTR);
4289         ADD_CONST(NLM_F_ECHO);
4290         ADD_CONST(NLM_F_EXCL);
4291         ADD_CONST(NLM_F_MATCH);
4292         ADD_CONST(NLM_F_MULTI);
4293         ADD_CONST(NLM_F_NONREC);
4294         ADD_CONST(NLM_F_REPLACE);
4295         ADD_CONST(NLM_F_REQUEST);
4296         ADD_CONST(NLM_F_ROOT);
4297         ADD_CONST(NLM_F_STRICT_CHK); /* custom */
4298 
4299         /**
4300          * @typedef
4301          * @name IPv6 address generation modes
4302          * @property {number} IN6_ADDR_GEN_MODE_EUI64 - EUI-64 mode
4303          * @property {number} IN6_ADDR_GEN_MODE_NONE - No mode
4304          * @property {number} IN6_ADDR_GEN_MODE_STABLE_PRIVACY - Stable privacy mode
4305          * @property {number} IN6_ADDR_GEN_MODE_RANDOM - Random mode
4306          */
4307         ADD_CONST(IN6_ADDR_GEN_MODE_EUI64);
4308         ADD_CONST(IN6_ADDR_GEN_MODE_NONE);
4309         ADD_CONST(IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
4310         ADD_CONST(IN6_ADDR_GEN_MODE_RANDOM);
4311 
4312         /**
4313          * @typedef
4314          * @name MACVLAN modes
4315          * @property {number} MACVLAN_MODE_PRIVATE - Private mode
4316          * @property {number} MACVLAN_MODE_VEPA - VEPA mode
4317          * @property {number} MACVLAN_MODE_BRIDGE - Bridge mode
4318          * @property {number} MACVLAN_MODE_PASSTHRU - Pass-through mode
4319          * @property {number} MACVLAN_MODE_SOURCE - Source mode
4320          */
4321         ADD_CONST(MACVLAN_MODE_PRIVATE);
4322         ADD_CONST(MACVLAN_MODE_VEPA);
4323         ADD_CONST(MACVLAN_MODE_BRIDGE);
4324         ADD_CONST(MACVLAN_MODE_PASSTHRU);
4325         ADD_CONST(MACVLAN_MODE_SOURCE);
4326 
4327         /**
4328          * @typedef
4329          * @name MACVLAN MAC address commands
4330          * @property {number} MACVLAN_MACADDR_ADD - Add MAC address
4331          * @property {number} MACVLAN_MACADDR_DEL - Delete MAC address
4332          * @property {number} MACVLAN_MACADDR_FLUSH - Flush MAC addresses
4333          * @property {number} MACVLAN_MACADDR_SET - Set MAC address
4334          */
4335         ADD_CONST(MACVLAN_MACADDR_ADD);
4336         ADD_CONST(MACVLAN_MACADDR_DEL);
4337         ADD_CONST(MACVLAN_MACADDR_FLUSH);
4338         ADD_CONST(MACVLAN_MACADDR_SET);
4339 
4340         /**
4341          * @typedef
4342          * @name MACsec validation levels
4343          * @property {number} MACSEC_VALIDATE_DISABLED - Disabled validation
4344          * @property {number} MACSEC_VALIDATE_CHECK - Check validation
4345          * @property {number} MACSEC_VALIDATE_STRICT - Strict validation
4346          * @property {number} MACSEC_VALIDATE_MAX - Maximum validation
4347          */
4348         ADD_CONST(MACSEC_VALIDATE_DISABLED);
4349         ADD_CONST(MACSEC_VALIDATE_CHECK);
4350         ADD_CONST(MACSEC_VALIDATE_STRICT);
4351         ADD_CONST(MACSEC_VALIDATE_MAX);
4352 
4353         /**
4354          * @typedef
4355          * @name MACsec offload modes
4356          * @property {number} MACSEC_OFFLOAD_OFF - Offload off
4357          * @property {number} MACSEC_OFFLOAD_PHY - Physical offload
4358          * @property {number} MACSEC_OFFLOAD_MAC - MAC offload
4359          * @property {number} MACSEC_OFFLOAD_MAX - Maximum offload
4360          */
4361         ADD_CONST(MACSEC_OFFLOAD_OFF);
4362         ADD_CONST(MACSEC_OFFLOAD_PHY);
4363         ADD_CONST(MACSEC_OFFLOAD_MAC);
4364         ADD_CONST(MACSEC_OFFLOAD_MAX);
4365 
4366         /**
4367          * @typedef
4368          * @name IPVLAN modes
4369          * @property {number} IPVLAN_MODE_L2 - Layer 2 mode
4370          * @property {number} IPVLAN_MODE_L3 - Layer 3 mode
4371          * @property {number} IPVLAN_MODE_L3S - Layer 3 symmetric mode
4372          */
4373         ADD_CONST(IPVLAN_MODE_L2);
4374         ADD_CONST(IPVLAN_MODE_L3);
4375         ADD_CONST(IPVLAN_MODE_L3S);
4376 
4377         /**
4378          * @typedef
4379          * @name VXLAN data frame flags
4380          * @property {number} VXLAN_DF_UNSET - Data frame unset
4381          * @property {number} VXLAN_DF_SET - Data frame set
4382          * @property {number} VXLAN_DF_INHERIT - Data frame inherit
4383          * @property {number} VXLAN_DF_MAX - Maximum data frame
4384          */
4385         ADD_CONST(VXLAN_DF_UNSET);
4386         ADD_CONST(VXLAN_DF_SET);
4387         ADD_CONST(VXLAN_DF_INHERIT);
4388         ADD_CONST(VXLAN_DF_MAX);
4389 
4390         /**
4391          * @typedef
4392          * @name Geneve data frame flags
4393          * @property {number} GENEVE_DF_UNSET - Data frame unset
4394          * @property {number} GENEVE_DF_SET - Data frame set
4395          * @property {number} GENEVE_DF_INHERIT - Data frame inherit
4396          * @property {number} GENEVE_DF_MAX - Maximum data frame
4397          */
4398         ADD_CONST(GENEVE_DF_UNSET);
4399         ADD_CONST(GENEVE_DF_SET);
4400         ADD_CONST(GENEVE_DF_INHERIT);
4401         ADD_CONST(GENEVE_DF_MAX);
4402 
4403         /**
4404          * @typedef
4405          * @name GTP roles
4406          * @property {number} GTP_ROLE_GGSN - GGSN role
4407          * @property {number} GTP_ROLE_SGSN - SGSN role
4408          */
4409         ADD_CONST(GTP_ROLE_GGSN);
4410         ADD_CONST(GTP_ROLE_SGSN);
4411 
4412         /**
4413          * @typedef
4414          * @name Port request types
4415          * @property {number} PORT_REQUEST_PREASSOCIATE - Pre-associate request
4416          * @property {number} PORT_REQUEST_PREASSOCIATE_RR - Pre-associate round-robin request
4417          * @property {number} PORT_REQUEST_ASSOCIATE - Associate request
4418          * @property {number} PORT_REQUEST_DISASSOCIATE - Disassociate request
4419          */
4420         ADD_CONST(PORT_REQUEST_PREASSOCIATE);
4421         ADD_CONST(PORT_REQUEST_PREASSOCIATE_RR);
4422         ADD_CONST(PORT_REQUEST_ASSOCIATE);
4423         ADD_CONST(PORT_REQUEST_DISASSOCIATE);
4424 
4425         /**
4426          * @typedef
4427          * @name Port VDP responses
4428          * @property {number} PORT_VDP_RESPONSE_SUCCESS - Success response
4429          * @property {number} PORT_VDP_RESPONSE_INVALID_FORMAT - Invalid format response
4430          * @property {number} PORT_VDP_RESPONSE_INSUFFICIENT_RESOURCES - Insufficient resources response
4431          * @property {number} PORT_VDP_RESPONSE_UNUSED_VTID - Unused VTID response
4432          * @property {number} PORT_VDP_RESPONSE_VTID_VIOLATION - VTID violation response
4433          * @property {number} PORT_VDP_RESPONSE_VTID_VERSION_VIOALTION - VTID version violation response
4434          * @property {number} PORT_VDP_RESPONSE_OUT_OF_SYNC - Out of sync response
4435          */
4436         ADD_CONST(PORT_VDP_RESPONSE_SUCCESS);
4437         ADD_CONST(PORT_VDP_RESPONSE_INVALID_FORMAT);
4438         ADD_CONST(PORT_VDP_RESPONSE_INSUFFICIENT_RESOURCES);
4439         ADD_CONST(PORT_VDP_RESPONSE_UNUSED_VTID);
4440         ADD_CONST(PORT_VDP_RESPONSE_VTID_VIOLATION);
4441         ADD_CONST(PORT_VDP_RESPONSE_VTID_VERSION_VIOALTION);
4442         ADD_CONST(PORT_VDP_RESPONSE_OUT_OF_SYNC);
4443 
4444         /**
4445          * @typedef
4446          * @name Port profile responses
4447          * @property {number} PORT_PROFILE_RESPONSE_SUCCESS - Success response
4448          * @property {number} PORT_PROFILE_RESPONSE_INPROGRESS - In progress response
4449          * @property {number} PORT_PROFILE_RESPONSE_INVALID - Invalid response
4450          * @property {number} PORT_PROFILE_RESPONSE_BADSTATE - Bad state response
4451          * @property {number} PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES - Insufficient resources response
4452          * @property {number} PORT_PROFILE_RESPONSE_ERROR - Error response
4453          */
4454         ADD_CONST(PORT_PROFILE_RESPONSE_SUCCESS);
4455         ADD_CONST(PORT_PROFILE_RESPONSE_INPROGRESS);
4456         ADD_CONST(PORT_PROFILE_RESPONSE_INVALID);
4457         ADD_CONST(PORT_PROFILE_RESPONSE_BADSTATE);
4458         ADD_CONST(PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES);
4459         ADD_CONST(PORT_PROFILE_RESPONSE_ERROR);
4460 
4461         /**
4462          * @typedef
4463          * @name IPoIB modes
4464          * @property {number} IPOIB_MODE_DATAGRAM - Datagram mode
4465          * @property {number} IPOIB_MODE_CONNECTED - Connected mode
4466          */
4467         ADD_CONST(IPOIB_MODE_DATAGRAM);
4468         ADD_CONST(IPOIB_MODE_CONNECTED);
4469 
4470         /**
4471          * @typedef
4472          * @name HSR protocols
4473          * @property {number} HSR_PROTOCOL_HSR - HSR protocol
4474          * @property {number} HSR_PROTOCOL_PRP - PRP protocol
4475          */
4476         ADD_CONST(HSR_PROTOCOL_HSR);
4477         ADD_CONST(HSR_PROTOCOL_PRP);
4478 
4479         /**
4480          * @typedef
4481          * @name CAN controller modes
4482          * @description
4483          * Flag bits used in the `mask` and `flags` members of the `ctrlmode`
4484          * attribute of `can` type links.
4485          * @property {number} CAN_CTRLMODE_LOOPBACK - Loopback mode
4486          * @property {number} CAN_CTRLMODE_LISTENONLY - Listen-only mode
4487          * @property {number} CAN_CTRLMODE_3_SAMPLES - Triple sampling mode
4488          * @property {number} CAN_CTRLMODE_ONE_SHOT - One-shot mode
4489          * @property {number} CAN_CTRLMODE_BERR_REPORTING - Bus error reporting
4490          * @property {number} CAN_CTRLMODE_FD - CAN FD mode
4491          * @property {number} CAN_CTRLMODE_PRESUME_ACK - Ignore missing CAN ACKs
4492          * @property {number} CAN_CTRLMODE_FD_NON_ISO - CAN FD in non-ISO mode
4493          * @property {number} CAN_CTRLMODE_CC_LEN8_DLC - Classic CAN DLC option
4494          */
4495         ADD_CONST(CAN_CTRLMODE_LOOPBACK);
4496         ADD_CONST(CAN_CTRLMODE_LISTENONLY);
4497         ADD_CONST(CAN_CTRLMODE_3_SAMPLES);
4498         ADD_CONST(CAN_CTRLMODE_ONE_SHOT);
4499         ADD_CONST(CAN_CTRLMODE_BERR_REPORTING);
4500         ADD_CONST(CAN_CTRLMODE_FD);
4501         ADD_CONST(CAN_CTRLMODE_PRESUME_ACK);
4502         ADD_CONST(CAN_CTRLMODE_FD_NON_ISO);
4503 #ifdef CAN_CTRLMODE_CC_LEN8_DLC
4504         ADD_CONST(CAN_CTRLMODE_CC_LEN8_DLC);
4505 #endif
4506 #ifdef CAN_CTRLMODE_TDC_AUTO
4507         ADD_CONST(CAN_CTRLMODE_TDC_AUTO);
4508         ADD_CONST(CAN_CTRLMODE_TDC_MANUAL);
4509 #endif
4510 
4511         /**
4512          * @typedef
4513          * @name CAN operational states
4514          * @description
4515          * Values of the `state` attribute of `can` type links.
4516          * @property {number} CAN_STATE_ERROR_ACTIVE - RX/TX error count < 96
4517          * @property {number} CAN_STATE_ERROR_WARNING - RX/TX error count < 128
4518          * @property {number} CAN_STATE_ERROR_PASSIVE - RX/TX error count < 256
4519          * @property {number} CAN_STATE_BUS_OFF - RX/TX error count >= 256
4520          * @property {number} CAN_STATE_STOPPED - Device is stopped
4521          * @property {number} CAN_STATE_SLEEPING - Device is sleeping
4522          */
4523         ADD_CONST(CAN_STATE_ERROR_ACTIVE);
4524         ADD_CONST(CAN_STATE_ERROR_WARNING);
4525         ADD_CONST(CAN_STATE_ERROR_PASSIVE);
4526         ADD_CONST(CAN_STATE_BUS_OFF);
4527         ADD_CONST(CAN_STATE_STOPPED);
4528         ADD_CONST(CAN_STATE_SLEEPING);
4529 
4530         /**
4531          * @typedef
4532          * @name Link extended statistics types
4533          * @property {number} LINK_XSTATS_TYPE_UNSPEC - Unspecified type
4534          * @property {number} LINK_XSTATS_TYPE_BRIDGE - Bridge type
4535          * @property {number} LINK_XSTATS_TYPE_BOND - Bond type
4536          */
4537         ADD_CONST(LINK_XSTATS_TYPE_UNSPEC);
4538         ADD_CONST(LINK_XSTATS_TYPE_BRIDGE);
4539         ADD_CONST(LINK_XSTATS_TYPE_BOND);
4540 
4541         /**
4542          * @typedef
4543          * @name XDP attach types
4544          * @property {number} XDP_ATTACHED_NONE - Not attached
4545          * @property {number} XDP_ATTACHED_DRV - Driver attached
4546          * @property {number} XDP_ATTACHED_SKB - SKB attached
4547          * @property {number} XDP_ATTACHED_HW - Hardware attached
4548          * @property {number} XDP_ATTACHED_MULTI - Multi attached
4549          */
4550         ADD_CONST(XDP_ATTACHED_NONE);
4551         ADD_CONST(XDP_ATTACHED_DRV);
4552         ADD_CONST(XDP_ATTACHED_SKB);
4553         ADD_CONST(XDP_ATTACHED_HW);
4554         ADD_CONST(XDP_ATTACHED_MULTI);
4555 
4556         /**
4557          * @typedef
4558          * @name FDB notification bits
4559          * @property {number} FDB_NOTIFY_BIT - Notify bit
4560          * @property {number} FDB_NOTIFY_INACTIVE_BIT - Inactive notify bit
4561          */
4562 
4563         ADD_CONST(FDB_NOTIFY_BIT);
4564         ADD_CONST(FDB_NOTIFY_INACTIVE_BIT);
4565 
4566         /**
4567          * @typedef
4568          * @name Route commands
4569          * @property {number} RTM_BASE - Base command
4570          * @property {number} RTM_NEWLINK - New link
4571          * @property {number} RTM_DELLINK - Delete link
4572          * @property {number} RTM_GETLINK - Get link
4573          * @property {number} RTM_SETLINK - Set link
4574          * @property {number} RTM_NEWADDR - New address
4575          * @property {number} RTM_DELADDR - Delete address
4576          * @property {number} RTM_GETADDR - Get address
4577          * @property {number} RTM_NEWROUTE - New route
4578          * @property {number} RTM_DELROUTE - Delete route
4579          * @property {number} RTM_GETROUTE - Get route
4580          * @property {number} RTM_NEWRULE - New rule
4581          * @property {number} RTM_DELRULE - Delete rule
4582          * @property {number} RTM_GETRULE - Get rule
4583          * @property {number} RTM_NEWQDISC - New queue discipline
4584          * @property {number} RTM_DELQDISC - Delete queue discipline
4585          * @property {number} RTM_GETQDISC - Get queue discipline
4586          * @property {number} RTM_NEWTCLASS - New traffic class
4587          * @property {number} RTM_DELTCLASS - Delete traffic class
4588          * @property {number} RTM_GETTCLASS - Get traffic class
4589          * @property {number} RTM_NEWTFILTER - New traffic filter
4590          * @property {number} RTM_DELTFILTER - Delete traffic filter
4591          * @property {number} RTM_GETTFILTER - Get traffic filter
4592          * @property {number} RTM_NEWACTION - New action
4593          * @property {number} RTM_DELACTION - Delete action
4594          * @property {number} RTM_GETACTION - Get action
4595          * @property {number} RTM_NEWPREFIX - New prefix
4596          * @property {number} RTM_GETMULTICAST - Get multicast
4597          * @property {number} RTM_GETANYCAST - Get anycast
4598          * @property {number} RTM_NEWNEIGHTBL - New neighbor table
4599          * @property {number} RTM_GETNEIGHTBL - Get neighbor table
4600          * @property {number} RTM_SETNEIGHTBL - Set neighbor table
4601          * @property {number} RTM_NEWNDUSEROPT - New neighbor user option
4602          * @property {number} RTM_NEWADDRLABEL - New address label
4603          * @property {number} RTM_DELADDRLABEL - Delete address label
4604          * @property {number} RTM_GETADDRLABEL - Get address label
4605          * @property {number} RTM_GETDCB - Get DCB
4606          * @property {number} RTM_SETDCB - Set DCB
4607          * @property {number} RTM_NEWNETCONF - New network configuration
4608          * @property {number} RTM_DELNETCONF - Delete network configuration
4609          * @property {number} RTM_GETNETCONF - Get network configuration
4610          * @property {number} RTM_NEWMDB - New multicast database
4611          * @property {number} RTM_DELMDB - Delete multicast database
4612          * @property {number} RTM_GETMDB - Get multicast database
4613          * @property {number} RTM_NEWNSID - New network namespace ID
4614          * @property {number} RTM_DELNSID - Delete network namespace ID
4615          * @property {number} RTM_GETNSID - Get network namespace ID
4616          * @property {number} RTM_NEWSTATS - New statistics
4617          * @property {number} RTM_GETSTATS - Get statistics
4618          * @property {number} RTM_NEWCACHEREPORT - New cache report
4619          * @property {number} RTM_NEWCHAIN - New chain
4620          * @property {number} RTM_DELCHAIN - Delete chain
4621          * @property {number} RTM_GETCHAIN - Get chain
4622          * @property {number} RTM_NEWNEXTHOP - New next hop
4623          * @property {number} RTM_DELNEXTHOP - Delete next hop
4624          * @property {number} RTM_GETNEXTHOP - Get next hop
4625          * @property {number} RTM_NEWLINKPROP - New link property
4626          * @property {number} RTM_DELLINKPROP - Delete link property
4627          * @property {number} RTM_GETLINKPROP - Get link property
4628          * @property {number} RTM_NEWVLAN - New VLAN
4629          * @property {number} RTM_DELVLAN - Delete VLAN
4630          * @property {number} RTM_GETVLAN - Get VLAN
4631          */
4632         ADD_CONST(RTM_BASE);
4633         ADD_CONST(RTM_NEWLINK);
4634         ADD_CONST(RTM_DELLINK);
4635         ADD_CONST(RTM_GETLINK);
4636         ADD_CONST(RTM_SETLINK);
4637         ADD_CONST(RTM_NEWADDR);
4638         ADD_CONST(RTM_DELADDR);
4639         ADD_CONST(RTM_GETADDR);
4640         ADD_CONST(RTM_NEWROUTE);
4641         ADD_CONST(RTM_DELROUTE);
4642         ADD_CONST(RTM_GETROUTE);
4643         ADD_CONST(RTM_NEWNEIGH);
4644         ADD_CONST(RTM_DELNEIGH);
4645         ADD_CONST(RTM_GETNEIGH);
4646         ADD_CONST(RTM_NEWRULE);
4647         ADD_CONST(RTM_DELRULE);
4648         ADD_CONST(RTM_GETRULE);
4649         ADD_CONST(RTM_NEWQDISC);
4650         ADD_CONST(RTM_DELQDISC);
4651         ADD_CONST(RTM_GETQDISC);
4652         ADD_CONST(RTM_NEWTCLASS);
4653         ADD_CONST(RTM_DELTCLASS);
4654         ADD_CONST(RTM_GETTCLASS);
4655         ADD_CONST(RTM_NEWTFILTER);
4656         ADD_CONST(RTM_DELTFILTER);
4657         ADD_CONST(RTM_GETTFILTER);
4658         ADD_CONST(RTM_NEWACTION);
4659         ADD_CONST(RTM_DELACTION);
4660         ADD_CONST(RTM_GETACTION);
4661         ADD_CONST(RTM_NEWPREFIX);
4662         ADD_CONST(RTM_GETMULTICAST);
4663         ADD_CONST(RTM_GETANYCAST);
4664         ADD_CONST(RTM_NEWNEIGHTBL);
4665         ADD_CONST(RTM_GETNEIGHTBL);
4666         ADD_CONST(RTM_SETNEIGHTBL);
4667         ADD_CONST(RTM_NEWNDUSEROPT);
4668         ADD_CONST(RTM_NEWADDRLABEL);
4669         ADD_CONST(RTM_DELADDRLABEL);
4670         ADD_CONST(RTM_GETADDRLABEL);
4671         ADD_CONST(RTM_GETDCB);
4672         ADD_CONST(RTM_SETDCB);
4673         ADD_CONST(RTM_NEWNETCONF);
4674         ADD_CONST(RTM_DELNETCONF);
4675         ADD_CONST(RTM_GETNETCONF);
4676         ADD_CONST(RTM_NEWMDB);
4677         ADD_CONST(RTM_DELMDB);
4678         ADD_CONST(RTM_GETMDB);
4679         ADD_CONST(RTM_NEWNSID);
4680         ADD_CONST(RTM_DELNSID);
4681         ADD_CONST(RTM_GETNSID);
4682         ADD_CONST(RTM_NEWSTATS);
4683         ADD_CONST(RTM_GETSTATS);
4684         ADD_CONST(RTM_NEWCACHEREPORT);
4685         ADD_CONST(RTM_NEWCHAIN);
4686         ADD_CONST(RTM_DELCHAIN);
4687         ADD_CONST(RTM_GETCHAIN);
4688         ADD_CONST(RTM_NEWNEXTHOP);
4689         ADD_CONST(RTM_DELNEXTHOP);
4690         ADD_CONST(RTM_GETNEXTHOP);
4691         ADD_CONST(RTM_NEWLINKPROP);
4692         ADD_CONST(RTM_DELLINKPROP);
4693         ADD_CONST(RTM_GETLINKPROP);
4694         ADD_CONST(RTM_NEWVLAN);
4695         ADD_CONST(RTM_DELVLAN);
4696         ADD_CONST(RTM_GETVLAN);
4697 
4698         /**
4699          * @typedef
4700          * @name Route types
4701          * @property {number} RTN_UNSPEC - Unspecified route
4702          * @property {number} RTN_UNICAST - Unicast route
4703          * @property {number} RTN_LOCAL - Local route
4704          * @property {number} RTN_BROADCAST - Broadcast route
4705          * @property {number} RTN_ANYCAST - Anycast route
4706          * @property {number} RTN_MULTICAST - Multicast route
4707          * @property {number} RTN_BLACKHOLE - Blackhole route
4708          * @property {number} RTN_UNREACHABLE - Unreachable route
4709          * @property {number} RTN_PROHIBIT - Prohibited route
4710          * @property {number} RTN_THROW - Throw route
4711          * @property {number} RTN_NAT - NAT route
4712          * @property {number} RTN_XRESOLVE - External resolve route
4713          */
4714         ADD_CONST(RTN_UNSPEC);
4715         ADD_CONST(RTN_UNICAST);
4716         ADD_CONST(RTN_LOCAL);
4717         ADD_CONST(RTN_BROADCAST);
4718         ADD_CONST(RTN_ANYCAST);
4719         ADD_CONST(RTN_MULTICAST);
4720         ADD_CONST(RTN_BLACKHOLE);
4721         ADD_CONST(RTN_UNREACHABLE);
4722         ADD_CONST(RTN_PROHIBIT);
4723         ADD_CONST(RTN_THROW);
4724         ADD_CONST(RTN_NAT);
4725         ADD_CONST(RTN_XRESOLVE);
4726 
4727         /**
4728          * @typedef
4729          * @name Route scopes
4730          * @property {number} RT_SCOPE_UNIVERSE - Universe scope
4731          * @property {number} RT_SCOPE_SITE - Site scope
4732          * @property {number} RT_SCOPE_LINK - Link scope
4733          * @property {number} RT_SCOPE_HOST - Host scope
4734          * @property {number} RT_SCOPE_NOWHERE - Nowhere scope
4735          */
4736         ADD_CONST(RT_SCOPE_UNIVERSE);
4737         ADD_CONST(RT_SCOPE_SITE);
4738         ADD_CONST(RT_SCOPE_LINK);
4739         ADD_CONST(RT_SCOPE_HOST);
4740         ADD_CONST(RT_SCOPE_NOWHERE);
4741 
4742         /**
4743          * @typedef
4744          * @name Route tables
4745          * @property {number} RT_TABLE_UNSPEC - Unspecified table
4746          * @property {number} RT_TABLE_COMPAT - Compatibility table
4747          * @property {number} RT_TABLE_DEFAULT - Default table
4748          * @property {number} RT_TABLE_MAIN - Main table
4749          * @property {number} RT_TABLE_LOCAL - Local table
4750          * @property {number} RT_TABLE_MAX - Maximum table
4751          */
4752         ADD_CONST(RT_TABLE_UNSPEC);
4753         ADD_CONST(RT_TABLE_COMPAT);
4754         ADD_CONST(RT_TABLE_DEFAULT);
4755         ADD_CONST(RT_TABLE_MAIN);
4756         ADD_CONST(RT_TABLE_LOCAL);
4757         ADD_CONST(RT_TABLE_MAX);
4758 
4759         /**
4760          * @typedef
4761          * @name Route metrics
4762          * @property {number} RTAX_MTU - Maximum transmission unit
4763          * @property {number} RTAX_HOPLIMIT - Hop limit
4764          * @property {number} RTAX_ADVMSS - Advertised MSS
4765          * @property {number} RTAX_REORDERING - Reordering
4766          * @property {number} RTAX_RTT - Round trip time
4767          * @property {number} RTAX_WINDOW - Window size
4768          * @property {number} RTAX_CWND - Congestion window
4769          * @property {number} RTAX_INITCWND - Initial congestion window
4770          * @property {number} RTAX_INITRWND - Initial receive window
4771          * @property {number} RTAX_FEATURES - Features
4772          * @property {number} RTAX_QUICKACK - Quick acknowledgment
4773          * @property {number} RTAX_CC_ALGO - Congestion control algorithm
4774          * @property {number} RTAX_RTTVAR - RTT variance
4775          * @property {number} RTAX_SSTHRESH - Slow start threshold
4776          * @property {number} RTAX_FASTOPEN_NO_COOKIE - Fast open no cookie
4777          */
4778         /* required to construct RTAX_LOCK */
4779         ADD_CONST(RTAX_MTU);
4780         ADD_CONST(RTAX_HOPLIMIT);
4781         ADD_CONST(RTAX_ADVMSS);
4782         ADD_CONST(RTAX_REORDERING);
4783         ADD_CONST(RTAX_RTT);
4784         ADD_CONST(RTAX_WINDOW);
4785         ADD_CONST(RTAX_CWND);
4786         ADD_CONST(RTAX_INITCWND);
4787         ADD_CONST(RTAX_INITRWND);
4788         ADD_CONST(RTAX_FEATURES);
4789         ADD_CONST(RTAX_QUICKACK);
4790         ADD_CONST(RTAX_CC_ALGO);
4791         ADD_CONST(RTAX_RTTVAR);
4792         ADD_CONST(RTAX_SSTHRESH);
4793         ADD_CONST(RTAX_FASTOPEN_NO_COOKIE);
4794 
4795         /**
4796          * @typedef
4797          * @name Prefix types
4798          * @property {number} PREFIX_UNSPEC - Unspecified prefix
4799          * @property {number} PREFIX_ADDRESS - Address prefix
4800          * @property {number} PREFIX_CACHEINFO - Cache info prefix
4801          */
4802         ADD_CONST(PREFIX_UNSPEC);
4803         ADD_CONST(PREFIX_ADDRESS);
4804         ADD_CONST(PREFIX_CACHEINFO);
4805 
4806         /**
4807          * @typedef
4808          * @name Neighbor discovery user option types
4809          * @property {number} NDUSEROPT_UNSPEC - Unspecified option
4810          * @property {number} NDUSEROPT_SRCADDR - Source address option
4811          */
4812         ADD_CONST(NDUSEROPT_UNSPEC);
4813         ADD_CONST(NDUSEROPT_SRCADDR);
4814 
4815         /**
4816          * @typedef
4817          * @name Multicast groups
4818          * @property {number} RTNLGRP_NONE - No group
4819          * @property {number} RTNLGRP_LINK - Link group
4820          * @property {number} RTNLGRP_NOTIFY - Notify group
4821          * @property {number} RTNLGRP_NEIGH - Neighbor group
4822          * @property {number} RTNLGRP_TC - Traffic control group
4823          * @property {number} RTNLGRP_IPV4_IFADDR - IPv4 interface address group
4824          * @property {number} RTNLGRP_IPV4_MROUTE - IPv4 multicast route group
4825          * @property {number} RTNLGRP_IPV4_ROUTE - IPv4 route group
4826          * @property {number} RTNLGRP_IPV4_RULE - IPv4 rule group
4827          * @property {number} RTNLGRP_IPV6_IFADDR - IPv6 interface address group
4828          * @property {number} RTNLGRP_IPV6_MROUTE - IPv6 multicast route group
4829          * @property {number} RTNLGRP_IPV6_ROUTE - IPv6 route group
4830          * @property {number} RTNLGRP_IPV6_IFINFO - IPv6 interface info group
4831          * @property {number} RTNLGRP_DECnet_IFADDR - DECnet interface address group
4832          * @property {number} RTNLGRP_NOP2 - No operation 2
4833          * @property {number} RTNLGRP_DECnet_ROUTE - DECnet route group
4834          * @property {number} RTNLGRP_DECnet_RULE - DECnet rule group
4835          * @property {number} RTNLGRP_NOP4 - No operation 4
4836          * @property {number} RTNLGRP_IPV6_PREFIX - IPv6 prefix group
4837          * @property {number} RTNLGRP_IPV6_RULE - IPv6 rule group
4838          * @property {number} RTNLGRP_ND_USEROPT - Neighbor discovery user option group
4839          * @property {number} RTNLGRP_PHONET_IFADDR - Phonet interface address group
4840          * @property {number} RTNLGRP_PHONET_ROUTE - Phonet route group
4841          * @property {number} RTNLGRP_DCB - Data Center Bridging group
4842          * @property {number} RTNLGRP_IPV4_NETCONF - IPv4 network configuration group
4843          * @property {number} RTNLGRP_IPV6_NETCONF - IPv6 network configuration group
4844          * @property {number} RTNLGRP_MDB - Multicast database group
4845          * @property {number} RTNLGRP_MPLS_ROUTE - MPLS route group
4846          * @property {number} RTNLGRP_NSID - Network namespace ID group
4847          * @property {number} RTNLGRP_MPLS_NETCONF - MPLS network configuration group
4848          * @property {number} RTNLGRP_IPV4_MROUTE_R - IPv4 multicast route reverse group
4849          * @property {number} RTNLGRP_IPV6_MROUTE_R - IPv6 multicast route reverse group
4850          * @property {number} RTNLGRP_NEXTHOP - Next hop group
4851          * @property {number} RTNLGRP_BRVLAN - Bridge VLAN group
4852          */
4853         ADD_CONST(RTNLGRP_NONE);
4854         ADD_CONST(RTNLGRP_LINK);
4855         ADD_CONST(RTNLGRP_NOTIFY);
4856         ADD_CONST(RTNLGRP_NEIGH);
4857         ADD_CONST(RTNLGRP_TC);
4858         ADD_CONST(RTNLGRP_IPV4_IFADDR);
4859         ADD_CONST(RTNLGRP_IPV4_MROUTE);
4860         ADD_CONST(RTNLGRP_IPV4_ROUTE);
4861         ADD_CONST(RTNLGRP_IPV4_RULE);
4862         ADD_CONST(RTNLGRP_IPV6_IFADDR);
4863         ADD_CONST(RTNLGRP_IPV6_MROUTE);
4864         ADD_CONST(RTNLGRP_IPV6_ROUTE);
4865         ADD_CONST(RTNLGRP_IPV6_IFINFO);
4866         ADD_CONST(RTNLGRP_DECnet_IFADDR);
4867         ADD_CONST(RTNLGRP_NOP2);
4868         ADD_CONST(RTNLGRP_DECnet_ROUTE);
4869         ADD_CONST(RTNLGRP_DECnet_RULE);
4870         ADD_CONST(RTNLGRP_NOP4);
4871         ADD_CONST(RTNLGRP_IPV6_PREFIX);
4872         ADD_CONST(RTNLGRP_IPV6_RULE);
4873         ADD_CONST(RTNLGRP_ND_USEROPT);
4874         ADD_CONST(RTNLGRP_PHONET_IFADDR);
4875         ADD_CONST(RTNLGRP_PHONET_ROUTE);
4876         ADD_CONST(RTNLGRP_DCB);
4877         ADD_CONST(RTNLGRP_IPV4_NETCONF);
4878         ADD_CONST(RTNLGRP_IPV6_NETCONF);
4879         ADD_CONST(RTNLGRP_MDB);
4880         ADD_CONST(RTNLGRP_MPLS_ROUTE);
4881         ADD_CONST(RTNLGRP_NSID);
4882         ADD_CONST(RTNLGRP_MPLS_NETCONF);
4883         ADD_CONST(RTNLGRP_IPV4_MROUTE_R);
4884         ADD_CONST(RTNLGRP_IPV6_MROUTE_R);
4885         ADD_CONST(RTNLGRP_NEXTHOP);
4886         ADD_CONST(RTNLGRP_BRVLAN);
4887 
4888         /**
4889          * @typedef
4890          * @name Route flags
4891          * @property {number} RTM_F_CLONED - Cloned route
4892          * @property {number} RTM_F_EQUALIZE - Equalize route
4893          * @property {number} RTM_F_FIB_MATCH - FIB match
4894          * @property {number} RTM_F_LOOKUP_TABLE - Lookup table
4895          * @property {number} RTM_F_NOTIFY - Notify
4896          * @property {number} RTM_F_PREFIX - Prefix
4897          */
4898         ADD_CONST(RTM_F_CLONED);
4899         ADD_CONST(RTM_F_EQUALIZE);
4900         ADD_CONST(RTM_F_FIB_MATCH);
4901         ADD_CONST(RTM_F_LOOKUP_TABLE);
4902         ADD_CONST(RTM_F_NOTIFY);
4903         ADD_CONST(RTM_F_PREFIX);
4904 
4905         /**
4906          * @typedef
4907          * @name Address families
4908          * @property {number} AF_UNSPEC - Unspecified address family
4909          * @property {number} AF_INET - IPv4 address family
4910          * @property {number} AF_INET6 - IPv6 address family
4911          * @property {number} AF_MPLS - MPLS address family
4912          * @property {number} AF_BRIDGE - Bridge address family
4913          */
4914         ADD_CONST(AF_UNSPEC);
4915         ADD_CONST(AF_INET);
4916         ADD_CONST(AF_INET6);
4917         ADD_CONST(AF_MPLS);
4918         ADD_CONST(AF_BRIDGE);
4919 
4920         /**
4921          * @typedef
4922          * @name Generic Routing Encapsulation flags
4923          * @property {number} GRE_CSUM - Checksum flag
4924          * @property {number} GRE_ROUTING - Routing flag
4925          * @property {number} GRE_KEY - Key flag
4926          * @property {number} GRE_SEQ - Sequence flag
4927          * @property {number} GRE_STRICT - Strict flag
4928          * @property {number} GRE_REC - Record flag
4929          * @property {number} GRE_ACK - Acknowledgment flag
4930          */
4931         ADD_CONST(GRE_CSUM);
4932         ADD_CONST(GRE_ROUTING);
4933         ADD_CONST(GRE_KEY);
4934         ADD_CONST(GRE_SEQ);
4935         ADD_CONST(GRE_STRICT);
4936         ADD_CONST(GRE_REC);
4937         ADD_CONST(GRE_ACK);
4938 
4939         /**
4940          * @typedef
4941          * @name Tunnel encapsulation types
4942          * @property {number} TUNNEL_ENCAP_NONE - No encapsulation
4943          * @property {number} TUNNEL_ENCAP_FOU - Foo over UDP
4944          * @property {number} TUNNEL_ENCAP_GUE - Generic UDP Encapsulation
4945          * @property {number} TUNNEL_ENCAP_MPLS - MPLS encapsulation
4946          */
4947         ADD_CONST(TUNNEL_ENCAP_NONE);
4948         ADD_CONST(TUNNEL_ENCAP_FOU);
4949         ADD_CONST(TUNNEL_ENCAP_GUE);
4950         ADD_CONST(TUNNEL_ENCAP_MPLS);
4951 
4952         /**
4953          * @typedef
4954          * @name Tunnel encapsulation flags
4955          * @property {number} TUNNEL_ENCAP_FLAG_CSUM - Checksum flag
4956          * @property {number} TUNNEL_ENCAP_FLAG_CSUM6 - IPv6 checksum flag
4957          * @property {number} TUNNEL_ENCAP_FLAG_REMCSUM - Remote checksum flag
4958          */
4959         ADD_CONST(TUNNEL_ENCAP_FLAG_CSUM);
4960         ADD_CONST(TUNNEL_ENCAP_FLAG_CSUM6);
4961         ADD_CONST(TUNNEL_ENCAP_FLAG_REMCSUM);
4962 
4963         /**
4964          * @typedef
4965          * @name IPv6 tunnel flags
4966          * @property {number} IP6_TNL_F_ALLOW_LOCAL_REMOTE - Allow local remote
4967          * @property {number} IP6_TNL_F_IGN_ENCAP_LIMIT - Ignore encapsulation limit
4968          * @property {number} IP6_TNL_F_MIP6_DEV - Mobile IPv6 device
4969          * @property {number} IP6_TNL_F_RCV_DSCP_COPY - Receive DSCP copy
4970          * @property {number} IP6_TNL_F_USE_ORIG_FLOWLABEL - Use original flow label
4971          * @property {number} IP6_TNL_F_USE_ORIG_FWMARK - Use original firewall mark
4972          * @property {number} IP6_TNL_F_USE_ORIG_TCLASS - Use original traffic class
4973          */
4974         ADD_CONST(IP6_TNL_F_ALLOW_LOCAL_REMOTE);
4975         ADD_CONST(IP6_TNL_F_IGN_ENCAP_LIMIT);
4976         ADD_CONST(IP6_TNL_F_MIP6_DEV);
4977         ADD_CONST(IP6_TNL_F_RCV_DSCP_COPY);
4978         ADD_CONST(IP6_TNL_F_USE_ORIG_FLOWLABEL);
4979         ADD_CONST(IP6_TNL_F_USE_ORIG_FWMARK);
4980         ADD_CONST(IP6_TNL_F_USE_ORIG_TCLASS);
4981 
4982         /**
4983          * @typedef
4984          * @name Interface flags
4985          * @property {number} NTF_EXT_LEARNED - Externally learned
4986          * @property {number} NTF_MASTER - Master interface
4987          * @property {number} NTF_OFFLOADED - Offloaded
4988          * @property {number} NTF_PROXY - Proxy
4989          * @property {number} NTF_ROUTER - Router
4990          * @property {number} NTF_SELF - Self
4991          * @property {number} NTF_STICKY - Sticky
4992          * @property {number} NTF_USE - Use
4993          */
4994         ADD_CONST(NTF_EXT_LEARNED);
4995         ADD_CONST(NTF_MASTER);
4996         ADD_CONST(NTF_OFFLOADED);
4997         ADD_CONST(NTF_PROXY);
4998         ADD_CONST(NTF_ROUTER);
4999         ADD_CONST(NTF_SELF);
5000         ADD_CONST(NTF_STICKY);
5001         ADD_CONST(NTF_USE);
5002 
5003         /**
5004          * @typedef
5005          * @name Neighbor states
5006          * @property {number} NUD_DELAY - Delay state
5007          * @property {number} NUD_FAILED - Failed state
5008          * @property {number} NUD_INCOMPLETE - Incomplete state
5009          * @property {number} NUD_NOARP - No ARP
5010          * @property {number} NUD_NONE - No state
5011          * @property {number} NUD_PERMANENT - Permanent state
5012          * @property {number} NUD_PROBE - Probe state
5013          * @property {number} NUD_REACHABLE - Reachable state
5014          * @property {number} NUD_STALE - Stale state
5015          */
5016         ADD_CONST(NUD_DELAY);
5017         ADD_CONST(NUD_FAILED);
5018         ADD_CONST(NUD_INCOMPLETE);
5019         ADD_CONST(NUD_NOARP);
5020         ADD_CONST(NUD_NONE);
5021         ADD_CONST(NUD_PERMANENT);
5022         ADD_CONST(NUD_PROBE);
5023         ADD_CONST(NUD_REACHABLE);
5024         ADD_CONST(NUD_STALE);
5025 
5026         /**
5027          * @typedef
5028          * @name Address flags
5029          * @property {number} IFA_F_DADFAILED - DAD failed
5030          * @property {number} IFA_F_DEPRECATED - Deprecated
5031          * @property {number} IFA_F_HOMEADDRESS - Home address
5032          * @property {number} IFA_F_MANAGETEMPADDR - Manage temporary address
5033          * @property {number} IFA_F_MCAUTOJOIN - Multicast auto join
5034          * @property {number} IFA_F_NODAD - No DAD
5035          * @property {number} IFA_F_NOPREFIXROUTE - No prefix route
5036          * @property {number} IFA_F_OPTIMISTIC - Optimistic
5037          * @property {number} IFA_F_PERMANENT - Permanent
5038          * @property {number} IFA_F_SECONDARY - Secondary
5039          * @property {number} IFA_F_STABLE_PRIVACY - Stable privacy
5040          * @property {number} IFA_F_TEMPORARY - Temporary
5041          * @property {number} IFA_F_TENTATIVE - Tentative
5042          */
5043         ADD_CONST(IFA_F_DADFAILED);
5044         ADD_CONST(IFA_F_DEPRECATED);
5045         ADD_CONST(IFA_F_HOMEADDRESS);
5046         ADD_CONST(IFA_F_MANAGETEMPADDR);
5047         ADD_CONST(IFA_F_MCAUTOJOIN);
5048         ADD_CONST(IFA_F_NODAD);
5049         ADD_CONST(IFA_F_NOPREFIXROUTE);
5050         ADD_CONST(IFA_F_OPTIMISTIC);
5051         ADD_CONST(IFA_F_PERMANENT);
5052         ADD_CONST(IFA_F_SECONDARY);
5053         ADD_CONST(IFA_F_STABLE_PRIVACY);
5054         ADD_CONST(IFA_F_TEMPORARY);
5055         ADD_CONST(IFA_F_TENTATIVE);
5056 
5057         /**
5058          * @typedef
5059          * @name FIB rule flags
5060          * @property {number} FIB_RULE_PERMANENT - Permanent rule
5061          * @property {number} FIB_RULE_INVERT - Invert rule
5062          * @property {number} FIB_RULE_UNRESOLVED - Unresolved rule
5063          * @property {number} FIB_RULE_IIF_DETACHED - Interface detached
5064          * @property {number} FIB_RULE_DEV_DETACHED - Device detached
5065          * @property {number} FIB_RULE_OIF_DETACHED - Output interface detached
5066          */
5067         ADD_CONST(FIB_RULE_PERMANENT);
5068         ADD_CONST(FIB_RULE_INVERT);
5069         ADD_CONST(FIB_RULE_UNRESOLVED);
5070         ADD_CONST(FIB_RULE_IIF_DETACHED);
5071         ADD_CONST(FIB_RULE_DEV_DETACHED);
5072         ADD_CONST(FIB_RULE_OIF_DETACHED);
5073 
5074         /**
5075          * @typedef
5076          * @name FIB rule actions
5077          * @property {number} FR_ACT_TO_TBL - To table action
5078          * @property {number} FR_ACT_GOTO - Goto action
5079          * @property {number} FR_ACT_NOP - No operation action
5080          * @property {number} FR_ACT_BLACKHOLE - Blackhole action
5081          * @property {number} FR_ACT_UNREACHABLE - Unreachable action
5082          * @property {number} FR_ACT_PROHIBIT - Prohibit action
5083          */
5084         ADD_CONST(FR_ACT_TO_TBL);
5085         ADD_CONST(FR_ACT_GOTO);
5086         ADD_CONST(FR_ACT_NOP);
5087         ADD_CONST(FR_ACT_BLACKHOLE);
5088         ADD_CONST(FR_ACT_UNREACHABLE);
5089         ADD_CONST(FR_ACT_PROHIBIT);
5090 
5091         /**
5092          * @typedef
5093          * @name Network configuration indices
5094          * @property {number} NETCONFA_IFINDEX_ALL - All interfaces
5095          * @property {number} NETCONFA_IFINDEX_DEFAULT - Default interface
5096          */
5097         ADD_CONST(NETCONFA_IFINDEX_ALL);
5098         ADD_CONST(NETCONFA_IFINDEX_DEFAULT);
5099 
5100         /**
5101          * @typedef
5102          * @name Bridge flags
5103          * @property {number} BRIDGE_FLAGS_MASTER - Master flag
5104          * @property {number} BRIDGE_FLAGS_SELF - Self flag
5105          */
5106         ADD_CONST(BRIDGE_FLAGS_MASTER);
5107         ADD_CONST(BRIDGE_FLAGS_SELF);
5108 
5109         /**
5110          * @typedef
5111          * @name Bridge modes
5112          * @property {number} BRIDGE_MODE_VEB - Virtual Ethernet Bridge mode
5113          * @property {number} BRIDGE_MODE_VEPA - Virtual Ethernet Port Aggregator mode
5114          * @property {number} BRIDGE_MODE_UNDEF - Undefined mode
5115          * @property {number} BRIDGE_MODE_UNSPEC - Unspecified mode
5116          * @property {number} BRIDGE_MODE_HAIRPIN - Hairpin mode
5117          */
5118         ADD_CONST(BRIDGE_MODE_VEB);
5119         ADD_CONST(BRIDGE_MODE_VEPA);
5120         ADD_CONST(BRIDGE_MODE_UNDEF);
5121         ADD_CONST(BRIDGE_MODE_UNSPEC);
5122         ADD_CONST(BRIDGE_MODE_HAIRPIN);
5123 
5124         /**
5125          * @typedef
5126          * @name Bridge VLAN information flags
5127          * @property {number} BRIDGE_VLAN_INFO_MASTER - Master VLAN info
5128          * @property {number} BRIDGE_VLAN_INFO_PVID - Primary VLAN ID
5129          * @property {number} BRIDGE_VLAN_INFO_UNTAGGED - Untagged VLAN
5130          * @property {number} BRIDGE_VLAN_INFO_RANGE_BEGIN - Range begin
5131          * @property {number} BRIDGE_VLAN_INFO_RANGE_END - Range end
5132          * @property {number} BRIDGE_VLAN_INFO_BRENTRY - Bridge entry
5133          */
5134         ADD_CONST(BRIDGE_VLAN_INFO_MASTER);
5135         ADD_CONST(BRIDGE_VLAN_INFO_PVID);
5136         ADD_CONST(BRIDGE_VLAN_INFO_UNTAGGED);
5137         ADD_CONST(BRIDGE_VLAN_INFO_RANGE_BEGIN);
5138         ADD_CONST(BRIDGE_VLAN_INFO_RANGE_END);
5139         ADD_CONST(BRIDGE_VLAN_INFO_BRENTRY);
5140 
5141         ucv_object_add(scope, "const", c);
5142 };
5143 
5144 static const uc_function_list_t global_fns[] = {
5145         { "error",              uc_nl_error },
5146         { "request",    uc_nl_request },
5147         { "listener",   uc_nl_listener },
5148 };
5149 
5150 static const uc_function_list_t listener_fns[] = {
5151         { "set_commands",       uc_nl_listener_set_commands },
5152         { "set_overrun_handler",        uc_nl_listener_set_overrun_handler },
5153         { "close",                      uc_nl_listener_close },
5154 };
5155 
5156 void uc_module_init(uc_vm_t *vm, uc_value_t *scope)
5157 {
5158         uc_function_list_register(scope, global_fns);
5159 
5160         listener_type = uc_type_declare(vm, "rtnl.listener", listener_fns, uc_nl_listener_free);
5161         listener_registry = ucv_array_new(vm);
5162 
5163         uc_vm_registry_set(vm, "rtnl.registry", listener_registry);
5164 
5165         register_constants(vm, scope);
5166 }
5167 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt