2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #ifndef _NETLINK_NETLINK_SNL_H_
28 #define _NETLINK_NETLINK_SNL_H_
31 * Simple Netlink Library
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netlink/netlink.h>
47 #include <netlink/netlink_bitset.h>
49 #define _roundup2(x, y) (((x)+((y)-1))&(~((y)-1)))
51 #define NETLINK_ALIGN_SIZE sizeof(uint32_t)
52 #define NETLINK_ALIGN(_len) _roundup2(_len, NETLINK_ALIGN_SIZE)
54 #define NLA_ALIGN_SIZE sizeof(uint32_t)
55 #define NLA_HDRLEN ((int)sizeof(struct nlattr))
56 #define NLA_DATA_LEN(_nla) ((int)((_nla)->nla_len - NLA_HDRLEN))
57 #define NLA_DATA(_nla) NL_ITEM_DATA(_nla, NLA_HDRLEN)
58 #define NLA_DATA_CONST(_nla) NL_ITEM_DATA_CONST(_nla, NLA_HDRLEN)
60 #define NLA_TYPE(_nla) ((_nla)->nla_type & 0x3FFF)
62 #define NLA_NEXT(_attr) (struct nlattr *)(void *)((char *)_attr + NLA_ALIGN(_attr->nla_len))
64 #define _NLA_END(_start, _len) ((char *)(_start) + (_len))
65 #define NLA_FOREACH(_attr, _start, _len) \
66 for (_attr = (_start); \
67 ((char *)_attr < _NLA_END(_start, _len)) && \
68 ((char *)NLA_NEXT(_attr) <= _NLA_END(_start, _len)); \
69 _attr = NLA_NEXT(_attr))
71 #define NL_ARRAY_LEN(_a) (sizeof(_a) / sizeof((_a)[0]))
73 struct linear_buffer {
74 char *base; /* Base allocated memory pointer */
75 uint32_t offset; /* Currently used offset */
76 uint32_t size; /* Total buffer size */
77 struct linear_buffer *next; /* Buffer chaining */
78 } __aligned(alignof(__max_align_t));
80 static inline struct linear_buffer *
81 lb_init(uint32_t size)
83 struct linear_buffer *lb = calloc(1, size);
86 lb->base = (char *)(lb + 1);
87 lb->size = size - sizeof(*lb);
94 lb_free(struct linear_buffer *lb)
100 lb_allocz(struct linear_buffer *lb, int len)
102 len = roundup2(len, alignof(__max_align_t));
103 if (lb->offset + len > lb->size)
105 void *data = (void *)(lb->base + lb->offset);
111 lb_clear(struct linear_buffer *lb)
113 memset(lb->base, 0, lb->offset);
125 struct linear_buffer *lb;
127 #define SCRATCH_BUFFER_SIZE 1024
128 #define SNL_WRITER_BUFFER_SIZE 256
130 typedef void snl_parse_field_f(struct snl_state *ss, void *hdr, void *target);
131 struct snl_field_parser {
134 snl_parse_field_f *cb;
137 typedef bool snl_parse_attr_f(struct snl_state *ss, struct nlattr *attr,
138 const void *arg, void *target);
139 struct snl_attr_parser {
140 uint16_t type; /* Attribute type */
141 uint16_t off; /* field offset in the target structure */
142 snl_parse_attr_f *cb; /* parser function to call */
144 /* Optional parser argument */
147 const uint32_t arg_u32;
151 typedef bool snl_parse_post_f(struct snl_state *ss, void *target);
153 struct snl_hdr_parser {
154 uint16_t in_hdr_size; /* Input header size */
155 uint16_t out_size; /* Output structure size */
156 uint16_t fp_size; /* Number of items in field parser */
157 uint16_t np_size; /* Number of items in attribute parser */
158 const struct snl_field_parser *fp; /* array of header field parsers */
159 const struct snl_attr_parser *np; /* array of attribute parsers */
160 snl_parse_post_f *cb_post; /* post-parse callback */
163 #define SNL_DECLARE_PARSER_EXT(_name, _sz_h_in, _sz_out, _fp, _np, _cb) \
164 static const struct snl_hdr_parser _name = { \
165 .in_hdr_size = _sz_h_in, \
166 .out_size = _sz_out, \
169 .fp_size = NL_ARRAY_LEN(_fp), \
170 .np_size = NL_ARRAY_LEN(_np), \
174 #define SNL_DECLARE_PARSER(_name, _t, _fp, _np) \
175 SNL_DECLARE_PARSER_EXT(_name, sizeof(_t), 0, _fp, _np, NULL)
177 #define SNL_DECLARE_ATTR_PARSER_EXT(_name, _sz_out, _np, _cb) \
178 static const struct snl_hdr_parser _name = { \
179 .out_size = _sz_out, \
181 .np_size = NL_ARRAY_LEN(_np), \
185 #define SNL_DECLARE_ATTR_PARSER(_name, _np) \
186 SNL_DECLARE_ATTR_PARSER_EXT(_name, 0, _np, NULL)
190 snl_allocz(struct snl_state *ss, int len)
192 void *data = lb_allocz(ss->lb, len);
195 uint32_t size = ss->lb->size * 2;
197 while (size < len + sizeof(struct linear_buffer))
200 struct linear_buffer *lb = lb_init(size);
205 data = lb_allocz(ss->lb, len);
213 snl_clear_lb(struct snl_state *ss)
215 struct linear_buffer *lb = ss->lb;
220 /* Remove all linear bufs except the largest one */
222 struct linear_buffer *lb_next = lb->next;
229 snl_free(struct snl_state *ss)
235 if (ss->lb != NULL) {
243 snl_init(struct snl_state *ss, int netlink_family)
245 memset(ss, 0, sizeof(*ss));
247 ss->fd = socket(AF_NETLINK, SOCK_RAW, netlink_family);
250 ss->init_done = true;
253 socklen_t optlen = sizeof(val);
254 if (setsockopt(ss->fd, SOL_NETLINK, NETLINK_EXT_ACK, &val, optlen) == -1) {
260 if (getsockopt(ss->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen) == -1) {
265 ss->bufsize = rcvbuf;
266 ss->buf = malloc(ss->bufsize);
267 if (ss->buf == NULL) {
272 ss->lb = lb_init(SCRATCH_BUFFER_SIZE);
273 if (ss->lb == NULL) {
282 snl_send(struct snl_state *ss, void *data, int sz)
284 return (send(ss->fd, data, sz, 0) == sz);
288 snl_send_message(struct snl_state *ss, struct nlmsghdr *hdr)
290 ssize_t sz = NLMSG_ALIGN(hdr->nlmsg_len);
292 return (send(ss->fd, hdr, sz, 0) == sz);
295 static inline uint32_t
296 snl_get_seq(struct snl_state *ss)
301 struct snl_msg_info {
308 static inline bool parse_cmsg(struct snl_state *ss, const struct msghdr *msg,
309 struct snl_msg_info *attrs);
311 static inline struct nlmsghdr *
312 snl_read_message_dbg(struct snl_state *ss, struct snl_msg_info *cinfo)
314 memset(cinfo, 0, sizeof(*cinfo));
316 if (ss->off == ss->datalen) {
317 struct sockaddr_nl nladdr;
322 .iov_len = ss->bufsize,
324 struct msghdr msg = {
326 .msg_namelen = sizeof(nladdr),
330 .msg_controllen = sizeof(cbuf),
335 ssize_t datalen = recvmsg(ss->fd, &msg, 0);
337 ss->datalen = datalen;
338 parse_cmsg(ss, &msg, cinfo);
340 } else if (errno != EINTR)
344 struct nlmsghdr *hdr = (struct nlmsghdr *)(void *)&ss->buf[ss->off];
345 ss->off += NLMSG_ALIGN(hdr->nlmsg_len);
350 static inline struct nlmsghdr *
351 snl_read_message(struct snl_state *ss)
353 if (ss->off == ss->datalen) {
354 struct sockaddr_nl nladdr;
357 .iov_len = ss->bufsize,
359 struct msghdr msg = {
361 .msg_namelen = sizeof(nladdr),
368 ssize_t datalen = recvmsg(ss->fd, &msg, 0);
370 ss->datalen = datalen;
372 } else if (errno != EINTR)
376 struct nlmsghdr *hdr = (struct nlmsghdr *)(void *)&ss->buf[ss->off];
377 ss->off += NLMSG_ALIGN(hdr->nlmsg_len);
381 static inline struct nlmsghdr *
382 snl_read_reply(struct snl_state *ss, uint32_t nlmsg_seq)
384 struct nlmsghdr *hdr;
386 while ((hdr = snl_read_message(ss)) != NULL) {
387 if (hdr->nlmsg_seq == nlmsg_seq)
395 * Checks that attributes are sorted by attribute type.
398 snl_verify_parsers(const struct snl_hdr_parser **parser, int count)
400 for (int i = 0; i < count; i++) {
401 const struct snl_hdr_parser *p = parser[i];
403 for (int j = 0; j < p->np_size; j++) {
404 assert(p->np[j].type > attr_type);
405 attr_type = p->np[j].type;
409 #define SNL_VERIFY_PARSERS(_p) snl_verify_parsers((_p), NL_ARRAY_LEN(_p))
411 static const struct snl_attr_parser *
412 find_parser(const struct snl_attr_parser *ps, int pslen, int key)
414 int left_i = 0, right_i = pslen - 1;
416 if (key < ps[0].type || key > ps[pslen - 1].type)
419 while (left_i + 1 < right_i) {
420 int mid_i = (left_i + right_i) / 2;
421 if (key < ps[mid_i].type)
423 else if (key > ps[mid_i].type)
428 if (ps[left_i].type == key)
429 return (&ps[left_i]);
430 else if (ps[right_i].type == key)
431 return (&ps[right_i]);
436 snl_parse_attrs_raw(struct snl_state *ss, struct nlattr *nla_head, int len,
437 const struct snl_attr_parser *ps, int pslen, void *target)
441 NLA_FOREACH(nla, nla_head, len) {
442 if (nla->nla_len < sizeof(struct nlattr))
444 int nla_type = nla->nla_type & NLA_TYPE_MASK;
445 const struct snl_attr_parser *s = find_parser(ps, pslen, nla_type);
447 void *ptr = (void *)((char *)target + s->off);
448 if (!s->cb(ss, nla, s->arg, ptr))
456 snl_parse_attrs(struct snl_state *ss, struct nlmsghdr *hdr, int hdrlen,
457 const struct snl_attr_parser *ps, int pslen, void *target)
459 int off = NLMSG_HDRLEN + NETLINK_ALIGN(hdrlen);
460 int len = hdr->nlmsg_len - off;
461 struct nlattr *nla_head = (struct nlattr *)(void *)((char *)hdr + off);
463 return (snl_parse_attrs_raw(ss, nla_head, len, ps, pslen, target));
467 snl_parse_fields(struct snl_state *ss, struct nlmsghdr *hdr, int hdrlen __unused,
468 const struct snl_field_parser *ps, int pslen, void *target)
470 for (int i = 0; i < pslen; i++) {
471 const struct snl_field_parser *fp = &ps[i];
472 void *src = (char *)hdr + fp->off_in;
473 void *dst = (char *)target + fp->off_out;
475 fp->cb(ss, src, dst);
480 snl_parse_header(struct snl_state *ss, void *hdr, int len,
481 const struct snl_hdr_parser *parser, void *target)
483 struct nlattr *nla_head;
485 /* Extract fields first (if any) */
486 snl_parse_fields(ss, hdr, parser->in_hdr_size, parser->fp, parser->fp_size, target);
488 nla_head = (struct nlattr *)(void *)((char *)hdr + parser->in_hdr_size);
489 bool result = snl_parse_attrs_raw(ss, nla_head, len - parser->in_hdr_size,
490 parser->np, parser->np_size, target);
492 if (result && parser->cb_post != NULL)
493 result = parser->cb_post(ss, target);
499 snl_parse_nlmsg(struct snl_state *ss, struct nlmsghdr *hdr,
500 const struct snl_hdr_parser *parser, void *target)
502 return (snl_parse_header(ss, hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser, target));
506 snl_attr_get_flag(struct snl_state *ss __unused, struct nlattr *nla, const void *arg __unused,
509 if (NLA_DATA_LEN(nla) == 0) {
510 *((uint8_t *)target) = 1;
517 snl_attr_get_uint8(struct snl_state *ss __unused, struct nlattr *nla,
518 const void *arg __unused, void *target)
520 if (NLA_DATA_LEN(nla) == sizeof(uint8_t)) {
521 *((uint8_t *)target) = *((const uint8_t *)NLA_DATA_CONST(nla));
528 snl_attr_get_uint16(struct snl_state *ss __unused, struct nlattr *nla,
529 const void *arg __unused, void *target)
531 if (NLA_DATA_LEN(nla) == sizeof(uint16_t)) {
532 *((uint16_t *)target) = *((const uint16_t *)NLA_DATA_CONST(nla));
539 snl_attr_get_uint32(struct snl_state *ss __unused, struct nlattr *nla,
540 const void *arg __unused, void *target)
542 if (NLA_DATA_LEN(nla) == sizeof(uint32_t)) {
543 *((uint32_t *)target) = *((const uint32_t *)NLA_DATA_CONST(nla));
550 snl_attr_get_uint64(struct snl_state *ss __unused, struct nlattr *nla,
551 const void *arg __unused, void *target)
553 if (NLA_DATA_LEN(nla) == sizeof(uint64_t)) {
554 memcpy(target, NLA_DATA_CONST(nla), sizeof(uint64_t));
561 snl_attr_get_int8(struct snl_state *ss, struct nlattr *nla, const void *arg,
564 return (snl_attr_get_uint8(ss, nla, arg, target));
568 snl_attr_get_int16(struct snl_state *ss, struct nlattr *nla, const void *arg,
571 return (snl_attr_get_uint16(ss, nla, arg, target));
575 snl_attr_get_int32(struct snl_state *ss, struct nlattr *nla, const void *arg,
578 return (snl_attr_get_uint32(ss, nla, arg, target));
582 snl_attr_get_int64(struct snl_state *ss, struct nlattr *nla, const void *arg,
585 return (snl_attr_get_uint64(ss, nla, arg, target));
589 snl_attr_get_string(struct snl_state *ss __unused, struct nlattr *nla,
590 const void *arg __unused, void *target)
592 size_t maxlen = NLA_DATA_LEN(nla);
594 if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
595 *((char **)target) = (char *)NLA_DATA(nla);
602 snl_attr_get_stringn(struct snl_state *ss, struct nlattr *nla,
603 const void *arg __unused, void *target)
605 int maxlen = NLA_DATA_LEN(nla);
607 char *buf = snl_allocz(ss, maxlen + 1);
611 memcpy(buf, NLA_DATA(nla), maxlen);
613 *((char **)target) = buf;
618 snl_attr_copy_string(struct snl_state *ss, struct nlattr *nla,
619 const void *arg, void *target)
623 if (snl_attr_get_string(ss, nla, NULL, &tmp)) {
624 strlcpy(target, tmp, (size_t)arg);
631 snl_attr_dup_string(struct snl_state *ss __unused, struct nlattr *nla,
632 const void *arg __unused, void *target)
634 size_t maxlen = NLA_DATA_LEN(nla);
636 if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
637 char *buf = snl_allocz(ss, maxlen);
640 memcpy(buf, NLA_DATA(nla), maxlen);
641 *((char **)target) = buf;
648 snl_attr_get_nested(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
650 const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
652 /* Assumes target points to the beginning of the structure */
653 return (snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, target));
662 snl_attr_get_parray_sz(struct snl_state *ss, struct nlattr *container_nla,
663 uint32_t start_size, const void *arg, void *target)
665 const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
666 struct snl_parray *array = target;
668 uint32_t count = 0, size = start_size;
670 if (p->out_size == 0)
673 array->items = snl_allocz(ss, size * sizeof(void *));
674 if (array->items == NULL)
678 * If the provided parser is an attribute parser, assume that each
679 * nla in the container nla is the container nla itself and parse
680 * the contents of this nla.
681 * Otherwise, run the parser on raw data, assuming the header of this
682 * data has u16 field with total size in the beginning.
684 uint32_t data_off = 0;
686 if (p->in_hdr_size == 0)
687 data_off = sizeof(struct nlattr);
689 NLA_FOREACH(nla, NLA_DATA(container_nla), NLA_DATA_LEN(container_nla)) {
690 void *item = snl_allocz(ss, p->out_size);
695 void *data = (char *)(void *)nla + data_off;
696 int data_len = nla->nla_len - data_off;
698 if (!(snl_parse_header(ss, data, data_len, p, item)))
702 uint32_t new_size = size * 2;
703 void **new_array = snl_allocz(ss, new_size *sizeof(void *));
705 memcpy(new_array, array->items, size * sizeof(void *));
706 array->items = new_array;
709 array->items[count++] = item;
711 array->count = count;
717 * Parses and stores the unknown-size array.
718 * Assumes each array item is a container and the NLAs in the container are parsable
719 * by the parser provided in @arg.
720 * Assumes @target is struct snl_parray
723 snl_attr_get_parray(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
725 return (snl_attr_get_parray_sz(ss, nla, 8, arg, target));
729 snl_attr_get_nla(struct snl_state *ss __unused, struct nlattr *nla,
730 const void *arg __unused, void *target)
732 *((struct nlattr **)target) = nla;
737 snl_attr_dup_nla(struct snl_state *ss, struct nlattr *nla,
738 const void *arg __unused, void *target)
740 void *ptr = snl_allocz(ss, nla->nla_len);
743 memcpy(ptr, nla, nla->nla_len);
744 *((void **)target) = ptr;
751 snl_attr_copy_struct(struct snl_state *ss, struct nlattr *nla,
752 const void *arg __unused, void *target)
754 void *ptr = snl_allocz(ss, NLA_DATA_LEN(nla));
757 memcpy(ptr, NLA_DATA(nla), NLA_DATA_LEN(nla));
758 *((void **)target) = ptr;
765 snl_attr_dup_struct(struct snl_state *ss, struct nlattr *nla,
766 const void *arg __unused, void *target)
768 void *ptr = snl_allocz(ss, NLA_DATA_LEN(nla));
771 memcpy(ptr, NLA_DATA(nla), NLA_DATA_LEN(nla));
772 *((void **)target) = ptr;
778 struct snl_attr_bit {
784 struct snl_attr_bits {
786 struct snl_attr_bit **bits;
789 #define _OUT(_field) offsetof(struct snl_attr_bit, _field)
790 static const struct snl_attr_parser _nla_p_bit[] = {
791 { .type = NLA_BITSET_BIT_INDEX, .off = _OUT(bit_index), .cb = snl_attr_get_uint32 },
792 { .type = NLA_BITSET_BIT_NAME, .off = _OUT(bit_name), .cb = snl_attr_dup_string },
793 { .type = NLA_BITSET_BIT_VALUE, .off = _OUT(bit_value), .cb = snl_attr_get_flag },
796 SNL_DECLARE_ATTR_PARSER_EXT(_nla_bit_parser, sizeof(struct snl_attr_bit), _nla_p_bit, NULL);
798 struct snl_attr_bitset {
799 uint32_t nla_bitset_size;
800 uint32_t *nla_bitset_mask;
801 uint32_t *nla_bitset_value;
802 struct snl_attr_bits bits;
805 #define _OUT(_field) offsetof(struct snl_attr_bitset, _field)
806 static const struct snl_attr_parser _nla_p_bitset[] = {
807 { .type = NLA_BITSET_SIZE, .off = _OUT(nla_bitset_size), .cb = snl_attr_get_uint32 },
808 { .type = NLA_BITSET_BITS, .off = _OUT(bits), .cb = snl_attr_get_parray, .arg = &_nla_bit_parser },
809 { .type = NLA_BITSET_VALUE, .off = _OUT(nla_bitset_mask), .cb = snl_attr_dup_nla },
810 { .type = NLA_BITSET_MASK, .off = _OUT(nla_bitset_value), .cb = snl_attr_dup_nla },
814 _cb_p_bitset(struct snl_state *ss __unused, void *_target)
816 struct snl_attr_bitset *target = _target;
818 uint32_t sz_bytes = _roundup2(target->nla_bitset_size, 32) / 8;
820 if (target->nla_bitset_mask != NULL) {
821 struct nlattr *nla = (struct nlattr *)target->nla_bitset_mask;
822 uint32_t data_len = NLA_DATA_LEN(nla);
824 if (data_len != sz_bytes || _roundup2(data_len, 4) != data_len)
826 target->nla_bitset_mask = (uint32_t *)NLA_DATA(nla);
829 if (target->nla_bitset_value != NULL) {
830 struct nlattr *nla = (struct nlattr *)target->nla_bitset_value;
831 uint32_t data_len = NLA_DATA_LEN(nla);
833 if (data_len != sz_bytes || _roundup2(data_len, 4) != data_len)
835 target->nla_bitset_value = (uint32_t *)NLA_DATA(nla);
840 SNL_DECLARE_ATTR_PARSER_EXT(_nla_bitset_parser,
841 sizeof(struct snl_attr_bitset),
842 _nla_p_bitset, _cb_p_bitset);
845 * Parses the compact bitset representation.
848 snl_attr_get_bitset_c(struct snl_state *ss, struct nlattr *nla,
849 const void *arg __unused, void *_target)
851 const struct snl_hdr_parser *p = &_nla_bitset_parser;
852 struct snl_attr_bitset *target = _target;
854 /* Assumes target points to the beginning of the structure */
855 if (!snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, _target))
857 if (target->nla_bitset_mask == NULL || target->nla_bitset_value == NULL)
863 snl_field_get_uint8(struct snl_state *ss __unused, void *src, void *target)
865 *((uint8_t *)target) = *((uint8_t *)src);
869 snl_field_get_uint16(struct snl_state *ss __unused, void *src, void *target)
871 *((uint16_t *)target) = *((uint16_t *)src);
875 snl_field_get_uint32(struct snl_state *ss __unused, void *src, void *target)
877 *((uint32_t *)target) = *((uint32_t *)src);
881 snl_field_get_ptr(struct snl_state *ss __unused, void *src, void *target)
883 *((void **)target) = src;
886 struct snl_errmsg_data {
887 struct nlmsghdr *orig_hdr;
891 struct nlattr *cookie;
894 #define _IN(_field) offsetof(struct nlmsgerr, _field)
895 #define _OUT(_field) offsetof(struct snl_errmsg_data, _field)
896 static const struct snl_attr_parser nla_p_errmsg[] = {
897 { .type = NLMSGERR_ATTR_MSG, .off = _OUT(error_str), .cb = snl_attr_get_string },
898 { .type = NLMSGERR_ATTR_OFFS, .off = _OUT(error_offs), .cb = snl_attr_get_uint32 },
899 { .type = NLMSGERR_ATTR_COOKIE, .off = _OUT(cookie), .cb = snl_attr_get_nla },
902 static const struct snl_field_parser nlf_p_errmsg[] = {
903 { .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
904 { .off_in = _IN(msg), .off_out = _OUT(orig_hdr), .cb = snl_field_get_ptr },
908 SNL_DECLARE_PARSER(snl_errmsg_parser, struct nlmsgerr, nlf_p_errmsg, nla_p_errmsg);
910 #define _IN(_field) offsetof(struct nlmsgerr, _field)
911 #define _OUT(_field) offsetof(struct snl_errmsg_data, _field)
912 static const struct snl_attr_parser nla_p_donemsg[] = {};
914 static const struct snl_field_parser nlf_p_donemsg[] = {
915 { .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
919 SNL_DECLARE_PARSER(snl_donemsg_parser, struct nlmsgerr, nlf_p_donemsg, nla_p_donemsg);
922 snl_parse_errmsg(struct snl_state *ss, struct nlmsghdr *hdr, struct snl_errmsg_data *e)
924 if ((hdr->nlmsg_flags & NLM_F_CAPPED) != 0)
925 return (snl_parse_nlmsg(ss, hdr, &snl_errmsg_parser, e));
927 const struct snl_hdr_parser *ps = &snl_errmsg_parser;
928 struct nlmsgerr *errmsg = (struct nlmsgerr *)(hdr + 1);
929 int hdrlen = sizeof(int) + NLMSG_ALIGN(errmsg->msg.nlmsg_len);
930 struct nlattr *attr_head = (struct nlattr *)(void *)((char *)errmsg + hdrlen);
931 int attr_len = hdr->nlmsg_len - sizeof(struct nlmsghdr) - hdrlen;
933 snl_parse_fields(ss, (struct nlmsghdr *)errmsg, hdrlen, ps->fp, ps->fp_size, e);
934 return (snl_parse_attrs_raw(ss, attr_head, attr_len, ps->np, ps->np_size, e));
938 snl_read_reply_code(struct snl_state *ss, uint32_t nlmsg_seq, struct snl_errmsg_data *e)
940 struct nlmsghdr *hdr = snl_read_reply(ss, nlmsg_seq);
944 } else if (hdr->nlmsg_type == NLMSG_ERROR) {
945 if (!snl_parse_errmsg(ss, hdr, e))
947 return (e->error == 0);
953 #define _OUT(_field) offsetof(struct snl_msg_info, _field)
954 static const struct snl_attr_parser _nla_p_cinfo[] = {
955 { .type = NLMSGINFO_ATTR_PROCESS_ID, .off = _OUT(process_id), .cb = snl_attr_get_uint32 },
956 { .type = NLMSGINFO_ATTR_PORT_ID, .off = _OUT(port_id), .cb = snl_attr_get_uint32 },
957 { .type = NLMSGINFO_ATTR_SEQ_ID, .off = _OUT(seq_id), .cb = snl_attr_get_uint32 },
960 SNL_DECLARE_ATTR_PARSER(snl_msg_info_parser, _nla_p_cinfo);
963 parse_cmsg(struct snl_state *ss, const struct msghdr *msg, struct snl_msg_info *attrs)
965 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
966 cmsg = CMSG_NXTHDR(msg, cmsg)) {
967 if (cmsg->cmsg_level != SOL_NETLINK || cmsg->cmsg_type != NETLINK_MSG_INFO)
970 void *data = CMSG_DATA(cmsg);
971 int len = cmsg->cmsg_len - ((char *)data - (char *)cmsg);
972 const struct snl_hdr_parser *ps = &snl_msg_info_parser;
974 return (snl_parse_attrs_raw(ss, data, len, ps->np, ps->np_size, attrs));
981 * Assumes e is zeroed
983 static inline struct nlmsghdr *
984 snl_read_reply_multi(struct snl_state *ss, uint32_t nlmsg_seq, struct snl_errmsg_data *e)
986 struct nlmsghdr *hdr = snl_read_reply(ss, nlmsg_seq);
990 } else if (hdr->nlmsg_type == NLMSG_ERROR) {
991 if (!snl_parse_errmsg(ss, hdr, e))
993 } else if (hdr->nlmsg_type == NLMSG_DONE) {
994 snl_parse_nlmsg(ss, hdr, &snl_donemsg_parser, e);
1007 struct nlmsghdr *hdr;
1008 struct snl_state *ss;
1013 snl_init_writer(struct snl_state *ss, struct snl_writer *nw)
1015 nw->size = SNL_WRITER_BUFFER_SIZE;
1016 nw->base = snl_allocz(ss, nw->size);
1017 if (nw->base == NULL) {
1029 snl_realloc_msg_buffer(struct snl_writer *nw, size_t sz)
1031 uint32_t new_size = nw->size * 2;
1033 while (new_size < nw->size + sz)
1039 void *new_base = snl_allocz(nw->ss, new_size);
1040 if (new_base == NULL) {
1045 memcpy(new_base, nw->base, nw->offset);
1046 if (nw->hdr != NULL) {
1047 int hdr_off = (char *)(nw->hdr) - nw->base;
1049 nw->hdr = (struct nlmsghdr *)(void *)((char *)new_base + hdr_off);
1051 nw->base = new_base;
1056 static inline void *
1057 snl_reserve_msg_data_raw(struct snl_writer *nw, size_t sz)
1059 sz = NETLINK_ALIGN(sz);
1061 if (__predict_false(nw->offset + sz > nw->size)) {
1062 if (!snl_realloc_msg_buffer(nw, sz))
1066 void *data_ptr = &nw->base[nw->offset];
1071 #define snl_reserve_msg_object(_ns, _t) ((_t *)snl_reserve_msg_data_raw(_ns, sizeof(_t)))
1072 #define snl_reserve_msg_data(_ns, _sz, _t) ((_t *)snl_reserve_msg_data_raw(_ns, _sz))
1074 static inline void *
1075 _snl_reserve_msg_attr(struct snl_writer *nw, uint16_t nla_type, uint16_t sz)
1077 sz += sizeof(struct nlattr);
1079 struct nlattr *nla = snl_reserve_msg_data(nw, sz, struct nlattr);
1080 if (__predict_false(nla == NULL))
1082 nla->nla_type = nla_type;
1085 return ((void *)(nla + 1));
1087 #define snl_reserve_msg_attr(_ns, _at, _t) ((_t *)_snl_reserve_msg_attr(_ns, _at, sizeof(_t)))
1090 snl_add_msg_attr(struct snl_writer *nw, int attr_type, int attr_len, const void *data)
1092 int required_len = NLA_ALIGN(attr_len + sizeof(struct nlattr));
1094 if (__predict_false(nw->offset + required_len > nw->size)) {
1095 if (!snl_realloc_msg_buffer(nw, required_len))
1099 struct nlattr *nla = (struct nlattr *)(void *)(&nw->base[nw->offset]);
1101 nla->nla_len = attr_len + sizeof(struct nlattr);
1102 nla->nla_type = attr_type;
1104 if ((attr_len % 4) != 0) {
1105 /* clear padding bytes */
1106 bzero((char *)nla + required_len - 4, 4);
1108 memcpy((nla + 1), data, attr_len);
1110 nw->offset += required_len;
1115 snl_add_msg_attr_raw(struct snl_writer *nw, const struct nlattr *nla_src)
1117 int attr_len = nla_src->nla_len - sizeof(struct nlattr);
1119 assert(attr_len >= 0);
1121 return (snl_add_msg_attr(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1)));
1125 snl_add_msg_attr_u8(struct snl_writer *nw, int attrtype, uint8_t value)
1127 return (snl_add_msg_attr(nw, attrtype, sizeof(uint8_t), &value));
1131 snl_add_msg_attr_u16(struct snl_writer *nw, int attrtype, uint16_t value)
1133 return (snl_add_msg_attr(nw, attrtype, sizeof(uint16_t), &value));
1137 snl_add_msg_attr_u32(struct snl_writer *nw, int attrtype, uint32_t value)
1139 return (snl_add_msg_attr(nw, attrtype, sizeof(uint32_t), &value));
1143 snl_add_msg_attr_u64(struct snl_writer *nw, int attrtype, uint64_t value)
1145 return (snl_add_msg_attr(nw, attrtype, sizeof(uint64_t), &value));
1149 snl_add_msg_attr_s8(struct snl_writer *nw, int attrtype, int8_t value)
1151 return (snl_add_msg_attr(nw, attrtype, sizeof(int8_t), &value));
1155 snl_add_msg_attr_s16(struct snl_writer *nw, int attrtype, int16_t value)
1157 return (snl_add_msg_attr(nw, attrtype, sizeof(int16_t), &value));
1161 snl_add_msg_attr_s32(struct snl_writer *nw, int attrtype, int32_t value)
1163 return (snl_add_msg_attr(nw, attrtype, sizeof(int32_t), &value));
1167 snl_add_msg_attr_s64(struct snl_writer *nw, int attrtype, int64_t value)
1169 return (snl_add_msg_attr(nw, attrtype, sizeof(int64_t), &value));
1173 snl_add_msg_attr_flag(struct snl_writer *nw, int attrtype)
1175 return (snl_add_msg_attr(nw, attrtype, 0, NULL));
1179 snl_add_msg_attr_string(struct snl_writer *nw, int attrtype, const char *str)
1181 return (snl_add_msg_attr(nw, attrtype, strlen(str) + 1, str));
1186 snl_get_msg_offset(const struct snl_writer *nw)
1188 return (nw->offset - ((char *)nw->hdr - nw->base));
1191 static inline void *
1192 _snl_restore_msg_offset(const struct snl_writer *nw, int off)
1194 return ((void *)((char *)nw->hdr + off));
1196 #define snl_restore_msg_offset(_ns, _off, _t) ((_t *)_snl_restore_msg_offset(_ns, _off))
1199 snl_add_msg_attr_nested(struct snl_writer *nw, int attrtype)
1201 int off = snl_get_msg_offset(nw);
1202 struct nlattr *nla = snl_reserve_msg_data(nw, sizeof(struct nlattr), struct nlattr);
1203 if (__predict_false(nla == NULL))
1205 nla->nla_type = attrtype;
1210 snl_end_attr_nested(const struct snl_writer *nw, int off)
1213 struct nlattr *nla = snl_restore_msg_offset(nw, off, struct nlattr);
1214 nla->nla_len = NETLINK_ALIGN(snl_get_msg_offset(nw) - off);
1218 static inline struct nlmsghdr *
1219 snl_create_msg_request(struct snl_writer *nw, int nlmsg_type)
1221 assert(nw->hdr == NULL);
1223 struct nlmsghdr *hdr = snl_reserve_msg_object(nw, struct nlmsghdr);
1224 hdr->nlmsg_type = nlmsg_type;
1225 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1232 snl_abort_msg(struct snl_writer *nw)
1234 if (nw->hdr != NULL) {
1235 int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
1237 nw->offset -= offset;
1242 static inline struct nlmsghdr *
1243 snl_finalize_msg(struct snl_writer *nw)
1247 if (nw->hdr != NULL) {
1248 struct nlmsghdr *hdr = nw->hdr;
1250 int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
1251 hdr->nlmsg_len = offset;
1252 hdr->nlmsg_seq = snl_get_seq(nw->ss);
1261 snl_send_msgs(struct snl_writer *nw)
1263 int offset = nw->offset;
1265 assert(nw->hdr == NULL);
1268 return (snl_send(nw->ss, nw->base, offset));
1271 static const struct snl_hdr_parser *snl_all_core_parsers[] = {
1272 &snl_errmsg_parser, &snl_donemsg_parser,
1273 &_nla_bit_parser, &_nla_bitset_parser,