1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27 #ifndef _NETLINK_NETLINK_SNL_H_
28 #define _NETLINK_NETLINK_SNL_H_
29
30 /*
31 * Simple Netlink Library
32 */
33
34 #include <assert.h>
35 #include <errno.h>
36 #include <stddef.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netlink/netlink.h>
46
47
48 #define _roundup2(x, y) (((x)+((y)-1))&(~((y)-1)))
49
50 #define NETLINK_ALIGN_SIZE sizeof(uint32_t)
51 #define NETLINK_ALIGN(_len) _roundup2(_len, NETLINK_ALIGN_SIZE)
52
53 #define NLA_ALIGN_SIZE sizeof(uint32_t)
54 #define NLA_HDRLEN ((int)sizeof(struct nlattr))
55 #define NLA_DATA_LEN(_nla) ((int)((_nla)->nla_len - NLA_HDRLEN))
56 #define NLA_DATA(_nla) NL_ITEM_DATA(_nla, NLA_HDRLEN)
57 #define NLA_DATA_CONST(_nla) NL_ITEM_DATA_CONST(_nla, NLA_HDRLEN)
58
59 #define NLA_TYPE(_nla) ((_nla)->nla_type & 0x3FFF)
60
61 #define NLA_NEXT(_attr) (struct nlattr *)(void *)((char *)_attr + NLA_ALIGN(_attr->nla_len))
62
63 #define _NLA_END(_start, _len) ((char *)(_start) + (_len))
64 #define NLA_FOREACH(_attr, _start, _len) \
65 for (_attr = (_start); \
66 ((char *)_attr < _NLA_END(_start, _len)) && \
67 ((char *)NLA_NEXT(_attr) <= _NLA_END(_start, _len)); \
68 _attr = NLA_NEXT(_attr))
69
70 #define NL_ARRAY_LEN(_a) (sizeof(_a) / sizeof((_a)[0]))
71
72 struct linear_buffer {
73 char *base; /* Base allocated memory pointer */
74 uint32_t offset; /* Currently used offset */
75 uint32_t size; /* Total buffer size */
76 struct linear_buffer *next; /* Buffer chaining */
77 };
78
79 static inline struct linear_buffer *
lb_init(uint32_t size)80 lb_init(uint32_t size)
81 {
82 struct linear_buffer *lb = calloc(1, size);
83
84 if (lb != NULL) {
85 lb->base = (char *)(lb + 1);
86 lb->size = size - sizeof(*lb);
87 }
88
89 return (lb);
90 }
91
92 static inline void
lb_free(struct linear_buffer * lb)93 lb_free(struct linear_buffer *lb)
94 {
95 free(lb);
96 }
97
98 static inline char *
lb_allocz(struct linear_buffer * lb,int len)99 lb_allocz(struct linear_buffer *lb, int len)
100 {
101 len = roundup2(len, sizeof(uint64_t));
102 if (lb->offset + len > lb->size)
103 return (NULL);
104 void *data = (void *)(lb->base + lb->offset);
105 lb->offset += len;
106 return (data);
107 }
108
109 static inline void
lb_clear(struct linear_buffer * lb)110 lb_clear(struct linear_buffer *lb)
111 {
112 memset(lb->base, 0, lb->offset);
113 lb->offset = 0;
114 }
115
116 struct snl_state {
117 int fd;
118 char *buf;
119 size_t off;
120 size_t bufsize;
121 size_t datalen;
122 uint32_t seq;
123 bool init_done;
124 struct linear_buffer *lb;
125 };
126 #define SCRATCH_BUFFER_SIZE 1024
127 #define SNL_WRITER_BUFFER_SIZE 256
128
129 typedef void snl_parse_field_f(struct snl_state *ss, void *hdr, void *target);
130 struct snl_field_parser {
131 uint16_t off_in;
132 uint16_t off_out;
133 snl_parse_field_f *cb;
134 };
135
136 typedef bool snl_parse_attr_f(struct snl_state *ss, struct nlattr *attr,
137 const void *arg, void *target);
138 struct snl_attr_parser {
139 uint16_t type; /* Attribute type */
140 uint16_t off; /* field offset in the target structure */
141 snl_parse_attr_f *cb; /* parser function to call */
142 const void *arg; /* Optional argument parser */
143 };
144
145 struct snl_hdr_parser {
146 int hdr_off; /* aligned header size */
147 int fp_size;
148 int np_size;
149 const struct snl_field_parser *fp; /* array of header field parsers */
150 const struct snl_attr_parser *np; /* array of attribute parsers */
151 };
152
153 #define SNL_DECLARE_PARSER(_name, _t, _fp, _np) \
154 static const struct snl_hdr_parser _name = { \
155 .hdr_off = sizeof(_t), \
156 .fp = &((_fp)[0]), \
157 .np = &((_np)[0]), \
158 .fp_size = NL_ARRAY_LEN(_fp), \
159 .np_size = NL_ARRAY_LEN(_np), \
160 }
161
162 #define SNL_DECLARE_ATTR_PARSER(_name, _np) \
163 static const struct snl_hdr_parser _name = { \
164 .np = &((_np)[0]), \
165 .np_size = NL_ARRAY_LEN(_np), \
166 }
167
168
169 static inline void *
snl_allocz(struct snl_state * ss,int len)170 snl_allocz(struct snl_state *ss, int len)
171 {
172 void *data = lb_allocz(ss->lb, len);
173
174 if (data == NULL) {
175 uint32_t size = ss->lb->size * 2;
176
177 while (size < len + sizeof(struct linear_buffer))
178 size *= 2;
179
180 struct linear_buffer *lb = lb_init(size);
181
182 if (lb != NULL) {
183 lb->next = ss->lb;
184 ss->lb = lb;
185 data = lb_allocz(ss->lb, len);
186 }
187 }
188
189 return (data);
190 }
191
192 static inline void
snl_clear_lb(struct snl_state * ss)193 snl_clear_lb(struct snl_state *ss)
194 {
195 struct linear_buffer *lb = ss->lb;
196
197 lb_clear(lb);
198 lb = lb->next;
199 ss->lb->next = NULL;
200 /* Remove all linear bufs except the largest one */
201 while (lb != NULL) {
202 struct linear_buffer *lb_next = lb->next;
203 lb_free(lb);
204 lb = lb_next;
205 }
206 }
207
208 static void
snl_free(struct snl_state * ss)209 snl_free(struct snl_state *ss)
210 {
211 if (ss->init_done) {
212 close(ss->fd);
213 if (ss->buf != NULL)
214 free(ss->buf);
215 if (ss->lb != NULL) {
216 snl_clear_lb(ss);
217 lb_free(ss->lb);
218 }
219 }
220 }
221
222 static inline bool
snl_init(struct snl_state * ss,int netlink_family)223 snl_init(struct snl_state *ss, int netlink_family)
224 {
225 memset(ss, 0, sizeof(*ss));
226
227 ss->fd = socket(AF_NETLINK, SOCK_RAW, netlink_family);
228 if (ss->fd == -1)
229 return (false);
230 ss->init_done = true;
231
232 int rcvbuf;
233 socklen_t optlen = sizeof(rcvbuf);
234 if (getsockopt(ss->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen) == -1) {
235 snl_free(ss);
236 return (false);
237 }
238
239 ss->bufsize = rcvbuf;
240 ss->buf = malloc(ss->bufsize);
241 if (ss->buf == NULL) {
242 snl_free(ss);
243 return (false);
244 }
245
246 ss->lb = lb_init(SCRATCH_BUFFER_SIZE);
247 if (ss->lb == NULL) {
248 snl_free(ss);
249 return (false);
250 }
251
252 return (true);
253 }
254
255 static inline bool
snl_send(struct snl_state * ss,void * data,int sz)256 snl_send(struct snl_state *ss, void *data, int sz)
257 {
258 return (send(ss->fd, data, sz, 0) == sz);
259 }
260
261 static inline bool
snl_send_message(struct snl_state * ss,struct nlmsghdr * hdr)262 snl_send_message(struct snl_state *ss, struct nlmsghdr *hdr)
263 {
264 ssize_t sz = NLMSG_ALIGN(hdr->nlmsg_len);
265
266 return (send(ss->fd, hdr, sz, 0) == sz);
267 }
268
269 static inline uint32_t
snl_get_seq(struct snl_state * ss)270 snl_get_seq(struct snl_state *ss)
271 {
272 return (++ss->seq);
273 }
274
275 static inline struct nlmsghdr *
snl_read_message(struct snl_state * ss)276 snl_read_message(struct snl_state *ss)
277 {
278 if (ss->off == ss->datalen) {
279 struct sockaddr_nl nladdr;
280 struct iovec iov = {
281 .iov_base = ss->buf,
282 .iov_len = ss->bufsize,
283 };
284 struct msghdr msg = {
285 .msg_name = &nladdr,
286 .msg_namelen = sizeof(nladdr),
287 .msg_iov = &iov,
288 .msg_iovlen = 1,
289 };
290 ss->off = 0;
291 ss->datalen = 0;
292 for (;;) {
293 ssize_t datalen = recvmsg(ss->fd, &msg, 0);
294 if (datalen > 0) {
295 ss->datalen = datalen;
296 break;
297 } else if (errno != EINTR)
298 return (NULL);
299 }
300 }
301 struct nlmsghdr *hdr = (struct nlmsghdr *)(void *)&ss->buf[ss->off];
302 ss->off += NLMSG_ALIGN(hdr->nlmsg_len);
303 return (hdr);
304 }
305
306 static inline struct nlmsghdr *
snl_read_reply(struct snl_state * ss,uint32_t nlmsg_seq)307 snl_read_reply(struct snl_state *ss, uint32_t nlmsg_seq)
308 {
309 struct nlmsghdr *hdr;
310
311 while ((hdr = snl_read_message(ss)) != NULL) {
312 if (hdr->nlmsg_seq == nlmsg_seq)
313 return (hdr);
314 }
315
316 return (NULL);
317 }
318
319 /*
320 * Checks that attributes are sorted by attribute type.
321 */
322 static inline void
snl_verify_parsers(const struct snl_hdr_parser ** parser,int count)323 snl_verify_parsers(const struct snl_hdr_parser **parser, int count)
324 {
325 for (int i = 0; i < count; i++) {
326 const struct snl_hdr_parser *p = parser[i];
327 int attr_type = 0;
328 for (int j = 0; j < p->np_size; j++) {
329 assert(p->np[j].type > attr_type);
330 attr_type = p->np[j].type;
331 }
332 }
333 }
334 #define SNL_VERIFY_PARSERS(_p) snl_verify_parsers((_p), NL_ARRAY_LEN(_p))
335
336 static const struct snl_attr_parser *
find_parser(const struct snl_attr_parser * ps,int pslen,int key)337 find_parser(const struct snl_attr_parser *ps, int pslen, int key)
338 {
339 int left_i = 0, right_i = pslen - 1;
340
341 if (key < ps[0].type || key > ps[pslen - 1].type)
342 return (NULL);
343
344 while (left_i + 1 < right_i) {
345 int mid_i = (left_i + right_i) / 2;
346 if (key < ps[mid_i].type)
347 right_i = mid_i;
348 else if (key > ps[mid_i].type)
349 left_i = mid_i + 1;
350 else
351 return (&ps[mid_i]);
352 }
353 if (ps[left_i].type == key)
354 return (&ps[left_i]);
355 else if (ps[right_i].type == key)
356 return (&ps[right_i]);
357 return (NULL);
358 }
359
360 static inline bool
snl_parse_attrs_raw(struct snl_state * ss,struct nlattr * nla_head,int len,const struct snl_attr_parser * ps,int pslen,void * target)361 snl_parse_attrs_raw(struct snl_state *ss, struct nlattr *nla_head, int len,
362 const struct snl_attr_parser *ps, int pslen, void *target)
363 {
364 struct nlattr *nla;
365
366 NLA_FOREACH(nla, nla_head, len) {
367 if (nla->nla_len < sizeof(struct nlattr))
368 return (false);
369 int nla_type = nla->nla_type & NLA_TYPE_MASK;
370 const struct snl_attr_parser *s = find_parser(ps, pslen, nla_type);
371 if (s != NULL) {
372 void *ptr = (void *)((char *)target + s->off);
373 if (!s->cb(ss, nla, s->arg, ptr))
374 return (false);
375 }
376 }
377 return (true);
378 }
379
380 static inline bool
snl_parse_attrs(struct snl_state * ss,struct nlmsghdr * hdr,int hdrlen,const struct snl_attr_parser * ps,int pslen,void * target)381 snl_parse_attrs(struct snl_state *ss, struct nlmsghdr *hdr, int hdrlen,
382 const struct snl_attr_parser *ps, int pslen, void *target)
383 {
384 int off = NLMSG_HDRLEN + NETLINK_ALIGN(hdrlen);
385 int len = hdr->nlmsg_len - off;
386 struct nlattr *nla_head = (struct nlattr *)(void *)((char *)hdr + off);
387
388 return (snl_parse_attrs_raw(ss, nla_head, len, ps, pslen, target));
389 }
390
391 static inline void
snl_parse_fields(struct snl_state * ss,struct nlmsghdr * hdr,int hdrlen __unused,const struct snl_field_parser * ps,int pslen,void * target)392 snl_parse_fields(struct snl_state *ss, struct nlmsghdr *hdr, int hdrlen __unused,
393 const struct snl_field_parser *ps, int pslen, void *target)
394 {
395 for (int i = 0; i < pslen; i++) {
396 const struct snl_field_parser *fp = &ps[i];
397 void *src = (char *)hdr + fp->off_in;
398 void *dst = (char *)target + fp->off_out;
399
400 fp->cb(ss, src, dst);
401 }
402 }
403
404 static inline bool
snl_parse_header(struct snl_state * ss,void * hdr,int len,const struct snl_hdr_parser * parser,void * target)405 snl_parse_header(struct snl_state *ss, void *hdr, int len,
406 const struct snl_hdr_parser *parser, void *target)
407 {
408 /* Extract fields first (if any) */
409 snl_parse_fields(ss, hdr, parser->hdr_off, parser->fp, parser->fp_size, target);
410
411 struct nlattr *nla_head = (struct nlattr *)(void *)((char *)hdr + parser->hdr_off);
412 bool result = snl_parse_attrs_raw(ss, nla_head, len - parser->hdr_off,
413 parser->np, parser->np_size, target);
414
415 return (result);
416 }
417
418 static inline bool
snl_parse_nlmsg(struct snl_state * ss,struct nlmsghdr * hdr,const struct snl_hdr_parser * parser,void * target)419 snl_parse_nlmsg(struct snl_state *ss, struct nlmsghdr *hdr,
420 const struct snl_hdr_parser *parser, void *target)
421 {
422 return (snl_parse_header(ss, hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser, target));
423 }
424
425 static inline bool
snl_attr_get_flag(struct snl_state * ss __unused,struct nlattr * nla,void * target)426 snl_attr_get_flag(struct snl_state *ss __unused, struct nlattr *nla, void *target)
427 {
428 if (NLA_DATA_LEN(nla) == 0) {
429 *((uint8_t *)target) = 1;
430 return (true);
431 }
432 return (false);
433 }
434
435 static inline bool
snl_attr_get_uint8(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)436 snl_attr_get_uint8(struct snl_state *ss __unused, struct nlattr *nla,
437 const void *arg __unused, void *target)
438 {
439 if (NLA_DATA_LEN(nla) == sizeof(uint8_t)) {
440 *((uint8_t *)target) = *((const uint8_t *)NLA_DATA_CONST(nla));
441 return (true);
442 }
443 return (false);
444 }
445
446 static inline bool
snl_attr_get_uint16(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)447 snl_attr_get_uint16(struct snl_state *ss __unused, struct nlattr *nla,
448 const void *arg __unused, void *target)
449 {
450 if (NLA_DATA_LEN(nla) == sizeof(uint16_t)) {
451 *((uint16_t *)target) = *((const uint16_t *)NLA_DATA_CONST(nla));
452 return (true);
453 }
454 return (false);
455 }
456
457 static inline bool
snl_attr_get_uint32(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)458 snl_attr_get_uint32(struct snl_state *ss __unused, struct nlattr *nla,
459 const void *arg __unused, void *target)
460 {
461 if (NLA_DATA_LEN(nla) == sizeof(uint32_t)) {
462 *((uint32_t *)target) = *((const uint32_t *)NLA_DATA_CONST(nla));
463 return (true);
464 }
465 return (false);
466 }
467
468 static inline bool
snl_attr_get_uint64(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)469 snl_attr_get_uint64(struct snl_state *ss __unused, struct nlattr *nla,
470 const void *arg __unused, void *target)
471 {
472 if (NLA_DATA_LEN(nla) == sizeof(uint64_t)) {
473 memcpy(target, NLA_DATA_CONST(nla), sizeof(uint64_t));
474 return (true);
475 }
476 return (false);
477 }
478
479 static inline bool
snl_attr_get_int8(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)480 snl_attr_get_int8(struct snl_state *ss, struct nlattr *nla, const void *arg,
481 void *target)
482 {
483 return (snl_attr_get_uint8(ss, nla, arg, target));
484 }
485
486 static inline bool
snl_attr_get_int16(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)487 snl_attr_get_int16(struct snl_state *ss, struct nlattr *nla, const void *arg,
488 void *target)
489 {
490 return (snl_attr_get_uint16(ss, nla, arg, target));
491 }
492
493 static inline bool
snl_attr_get_int32(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)494 snl_attr_get_int32(struct snl_state *ss, struct nlattr *nla, const void *arg,
495 void *target)
496 {
497 return (snl_attr_get_uint32(ss, nla, arg, target));
498 }
499
500 static inline bool
snl_attr_get_int64(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)501 snl_attr_get_int64(struct snl_state *ss, struct nlattr *nla, const void *arg,
502 void *target)
503 {
504 return (snl_attr_get_uint64(ss, nla, arg, target));
505 }
506
507 static inline bool
snl_attr_get_string(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)508 snl_attr_get_string(struct snl_state *ss __unused, struct nlattr *nla,
509 const void *arg __unused, void *target)
510 {
511 size_t maxlen = NLA_DATA_LEN(nla);
512
513 if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
514 *((char **)target) = (char *)NLA_DATA(nla);
515 return (true);
516 }
517 return (false);
518 }
519
520 static inline bool
snl_attr_get_stringn(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)521 snl_attr_get_stringn(struct snl_state *ss, struct nlattr *nla,
522 const void *arg __unused, void *target)
523 {
524 int maxlen = NLA_DATA_LEN(nla);
525
526 char *buf = snl_allocz(ss, maxlen + 1);
527 if (buf == NULL)
528 return (false);
529 buf[maxlen] = '\0';
530 memcpy(buf, NLA_DATA(nla), maxlen);
531
532 *((char **)target) = buf;
533 return (true);
534 }
535
536 static inline bool
snl_attr_get_nested(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)537 snl_attr_get_nested(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
538 {
539 const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
540
541 /* Assumes target points to the beginning of the structure */
542 return (snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, target));
543 }
544
545 static inline bool
snl_attr_get_nla(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)546 snl_attr_get_nla(struct snl_state *ss __unused, struct nlattr *nla,
547 const void *arg __unused, void *target)
548 {
549 *((struct nlattr **)target) = nla;
550 return (true);
551 }
552
553 static inline bool
snl_attr_copy_struct(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)554 snl_attr_copy_struct(struct snl_state *ss, struct nlattr *nla,
555 const void *arg __unused, void *target)
556 {
557 void *ptr = snl_allocz(ss, NLA_DATA_LEN(nla));
558
559 if (ptr != NULL) {
560 memcpy(ptr, NLA_DATA(nla), NLA_DATA_LEN(nla));
561 *((void **)target) = ptr;
562 return (true);
563 }
564 return (false);
565 }
566
567 static inline void
snl_field_get_uint8(struct snl_state * ss __unused,void * src,void * target)568 snl_field_get_uint8(struct snl_state *ss __unused, void *src, void *target)
569 {
570 *((uint8_t *)target) = *((uint8_t *)src);
571 }
572
573 static inline void
snl_field_get_uint16(struct snl_state * ss __unused,void * src,void * target)574 snl_field_get_uint16(struct snl_state *ss __unused, void *src, void *target)
575 {
576 *((uint16_t *)target) = *((uint16_t *)src);
577 }
578
579 static inline void
snl_field_get_uint32(struct snl_state * ss __unused,void * src,void * target)580 snl_field_get_uint32(struct snl_state *ss __unused, void *src, void *target)
581 {
582 *((uint32_t *)target) = *((uint32_t *)src);
583 }
584
585 static inline void
snl_field_get_ptr(struct snl_state * ss __unused,void * src,void * target)586 snl_field_get_ptr(struct snl_state *ss __unused, void *src, void *target)
587 {
588 *((void **)target) = src;
589 }
590
591 struct snl_errmsg_data {
592 struct nlmsghdr *orig_hdr;
593 int error;
594 uint32_t error_offs;
595 char *error_str;
596 struct nlattr *cookie;
597 };
598
599 #define _IN(_field) offsetof(struct nlmsgerr, _field)
600 #define _OUT(_field) offsetof(struct snl_errmsg_data, _field)
601 static const struct snl_attr_parser nla_p_errmsg[] = {
602 { .type = NLMSGERR_ATTR_MSG, .off = _OUT(error_str), .cb = snl_attr_get_string },
603 { .type = NLMSGERR_ATTR_OFFS, .off = _OUT(error_offs), .cb = snl_attr_get_uint32 },
604 { .type = NLMSGERR_ATTR_COOKIE, .off = _OUT(cookie), .cb = snl_attr_get_nla },
605 };
606
607 static const struct snl_field_parser nlf_p_errmsg[] = {
608 { .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
609 { .off_in = _IN(msg), .off_out = _OUT(orig_hdr), .cb = snl_field_get_ptr },
610 };
611 #undef _IN
612 #undef _OUT
613 SNL_DECLARE_PARSER(snl_errmsg_parser, struct nlmsgerr, nlf_p_errmsg, nla_p_errmsg);
614
615 #define _IN(_field) offsetof(struct nlmsgerr, _field)
616 #define _OUT(_field) offsetof(struct snl_errmsg_data, _field)
617 static const struct snl_attr_parser nla_p_donemsg[] = {};
618
619 static const struct snl_field_parser nlf_p_donemsg[] = {
620 { .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
621 };
622 #undef _IN
623 #undef _OUT
624 SNL_DECLARE_PARSER(snl_donemsg_parser, struct nlmsgerr, nlf_p_donemsg, nla_p_donemsg);
625
626 static inline bool
snl_parse_errmsg(struct snl_state * ss,struct nlmsghdr * hdr,struct snl_errmsg_data * e)627 snl_parse_errmsg(struct snl_state *ss, struct nlmsghdr *hdr, struct snl_errmsg_data *e)
628 {
629 if ((hdr->nlmsg_flags & NLM_F_CAPPED) != 0)
630 return (snl_parse_nlmsg(ss, hdr, &snl_errmsg_parser, e));
631
632 const struct snl_hdr_parser *ps = &snl_errmsg_parser;
633 struct nlmsgerr *errmsg = (struct nlmsgerr *)(hdr + 1);
634 int hdrlen = sizeof(int) + NLMSG_ALIGN(errmsg->msg.nlmsg_len);
635 struct nlattr *attr_head = (struct nlattr *)(void *)((char *)errmsg + hdrlen);
636 int attr_len = hdr->nlmsg_len - sizeof(struct nlmsghdr) - hdrlen;
637
638 snl_parse_fields(ss, (struct nlmsghdr *)errmsg, hdrlen, ps->fp, ps->fp_size, e);
639 return (snl_parse_attrs_raw(ss, attr_head, attr_len, ps->np, ps->np_size, e));
640 }
641
642 static inline bool
snl_read_reply_code(struct snl_state * ss,uint32_t nlmsg_seq,struct snl_errmsg_data * e)643 snl_read_reply_code(struct snl_state *ss, uint32_t nlmsg_seq, struct snl_errmsg_data *e)
644 {
645 struct nlmsghdr *hdr = snl_read_reply(ss, nlmsg_seq);
646
647 if (hdr == NULL) {
648 e->error = EINVAL;
649 } else if (hdr->nlmsg_type == NLMSG_ERROR) {
650 if (!snl_parse_errmsg(ss, hdr, e))
651 e->error = EINVAL;
652 return (e->error == 0);
653 }
654
655 return (false);
656 }
657
658 /*
659 * Assumes e is zeroed
660 */
661 static inline struct nlmsghdr *
snl_read_reply_multi(struct snl_state * ss,uint32_t nlmsg_seq,struct snl_errmsg_data * e)662 snl_read_reply_multi(struct snl_state *ss, uint32_t nlmsg_seq, struct snl_errmsg_data *e)
663 {
664 struct nlmsghdr *hdr = snl_read_reply(ss, nlmsg_seq);
665
666 if (hdr == NULL) {
667 e->error = EINVAL;
668 } else if (hdr->nlmsg_type == NLMSG_ERROR) {
669 if (!snl_parse_errmsg(ss, hdr, e))
670 e->error = EINVAL;
671 } if (hdr->nlmsg_type == NLMSG_DONE) {
672 snl_parse_nlmsg(ss, hdr, &snl_donemsg_parser, e);
673 } else
674 return (hdr);
675
676 return (NULL);
677 }
678
679
680 /* writer logic */
681 struct snl_writer {
682 char *base;
683 uint32_t offset;
684 uint32_t size;
685 struct nlmsghdr *hdr;
686 struct snl_state *ss;
687 bool error;
688 };
689
690 static inline void
snl_init_writer(struct snl_state * ss,struct snl_writer * nw)691 snl_init_writer(struct snl_state *ss, struct snl_writer *nw)
692 {
693 nw->size = SNL_WRITER_BUFFER_SIZE;
694 nw->base = snl_allocz(ss, nw->size);
695 if (nw->base == NULL) {
696 nw->error = true;
697 nw->size = 0;
698 }
699
700 nw->offset = 0;
701 nw->hdr = NULL;
702 nw->error = false;
703 nw->ss = ss;
704 }
705
706 static inline bool
snl_realloc_msg_buffer(struct snl_writer * nw,size_t sz)707 snl_realloc_msg_buffer(struct snl_writer *nw, size_t sz)
708 {
709 uint32_t new_size = nw->size * 2;
710
711 while (new_size < nw->size + sz)
712 new_size *= 2;
713
714 if (nw->error)
715 return (false);
716
717 if (snl_allocz(nw->ss, new_size) == NULL) {
718 nw->error = true;
719 return (false);
720 }
721 nw->size = new_size;
722
723 void *new_base = nw->ss->lb->base;
724 if (new_base != nw->base) {
725 memcpy(new_base, nw->base, nw->offset);
726 if (nw->hdr != NULL) {
727 int hdr_off = (char *)(nw->hdr) - nw->base;
728
729 nw->hdr = (struct nlmsghdr *)
730 (void *)((char *)new_base + hdr_off);
731 }
732 nw->base = new_base;
733 }
734
735 return (true);
736 }
737
738 static inline void *
snl_reserve_msg_data_raw(struct snl_writer * nw,size_t sz)739 snl_reserve_msg_data_raw(struct snl_writer *nw, size_t sz)
740 {
741 sz = NETLINK_ALIGN(sz);
742
743 if (__predict_false(nw->offset + sz > nw->size)) {
744 if (!snl_realloc_msg_buffer(nw, sz))
745 return (NULL);
746 }
747
748 void *data_ptr = &nw->base[nw->offset];
749 nw->offset += sz;
750
751 return (data_ptr);
752 }
753 #define snl_reserve_msg_object(_ns, _t) ((_t *)snl_reserve_msg_data_raw(_ns, sizeof(_t)))
754 #define snl_reserve_msg_data(_ns, _sz, _t) ((_t *)snl_reserve_msg_data_raw(_ns, _sz))
755
756 static inline void *
_snl_reserve_msg_attr(struct snl_writer * nw,uint16_t nla_type,uint16_t sz)757 _snl_reserve_msg_attr(struct snl_writer *nw, uint16_t nla_type, uint16_t sz)
758 {
759 sz += sizeof(struct nlattr);
760
761 struct nlattr *nla = snl_reserve_msg_data(nw, sz, struct nlattr);
762 if (__predict_false(nla == NULL))
763 return (NULL);
764 nla->nla_type = nla_type;
765 nla->nla_len = sz;
766
767 return ((void *)(nla + 1));
768 }
769 #define snl_reserve_msg_attr(_ns, _at, _t) ((_t *)_snl_reserve_msg_attr(_ns, _at, sizeof(_t)))
770
771 static inline bool
snl_add_msg_attr(struct snl_writer * nw,int attr_type,int attr_len,const void * data)772 snl_add_msg_attr(struct snl_writer *nw, int attr_type, int attr_len, const void *data)
773 {
774 int required_len = NLA_ALIGN(attr_len + sizeof(struct nlattr));
775
776 if (__predict_false(nw->offset + required_len > nw->size)) {
777 if (!snl_realloc_msg_buffer(nw, required_len))
778 return (false);
779 }
780
781 struct nlattr *nla = (struct nlattr *)(void *)(&nw->base[nw->offset]);
782
783 nla->nla_len = attr_len + sizeof(struct nlattr);
784 nla->nla_type = attr_type;
785 if (attr_len > 0) {
786 if ((attr_len % 4) != 0) {
787 /* clear padding bytes */
788 bzero((char *)nla + required_len - 4, 4);
789 }
790 memcpy((nla + 1), data, attr_len);
791 }
792 nw->offset += required_len;
793 return (true);
794 }
795
796 static inline bool
snl_add_msg_attr_raw(struct snl_writer * nw,const struct nlattr * nla_src)797 snl_add_msg_attr_raw(struct snl_writer *nw, const struct nlattr *nla_src)
798 {
799 int attr_len = nla_src->nla_len - sizeof(struct nlattr);
800
801 assert(attr_len >= 0);
802
803 return (snl_add_msg_attr(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1)));
804 }
805
806 static inline bool
snl_add_msg_attr_u8(struct snl_writer * nw,int attrtype,uint8_t value)807 snl_add_msg_attr_u8(struct snl_writer *nw, int attrtype, uint8_t value)
808 {
809 return (snl_add_msg_attr(nw, attrtype, sizeof(uint8_t), &value));
810 }
811
812 static inline bool
snl_add_msg_attr_u16(struct snl_writer * nw,int attrtype,uint16_t value)813 snl_add_msg_attr_u16(struct snl_writer *nw, int attrtype, uint16_t value)
814 {
815 return (snl_add_msg_attr(nw, attrtype, sizeof(uint16_t), &value));
816 }
817
818 static inline bool
snl_add_msg_attr_u32(struct snl_writer * nw,int attrtype,uint32_t value)819 snl_add_msg_attr_u32(struct snl_writer *nw, int attrtype, uint32_t value)
820 {
821 return (snl_add_msg_attr(nw, attrtype, sizeof(uint32_t), &value));
822 }
823
824 static inline bool
snl_add_msg_attr_u64(struct snl_writer * nw,int attrtype,uint64_t value)825 snl_add_msg_attr_u64(struct snl_writer *nw, int attrtype, uint64_t value)
826 {
827 return (snl_add_msg_attr(nw, attrtype, sizeof(uint64_t), &value));
828 }
829
830 static inline bool
snl_add_msg_attr_s8(struct snl_writer * nw,int attrtype,int8_t value)831 snl_add_msg_attr_s8(struct snl_writer *nw, int attrtype, int8_t value)
832 {
833 return (snl_add_msg_attr(nw, attrtype, sizeof(int8_t), &value));
834 }
835
836 static inline bool
snl_add_msg_attr_s16(struct snl_writer * nw,int attrtype,int16_t value)837 snl_add_msg_attr_s16(struct snl_writer *nw, int attrtype, int16_t value)
838 {
839 return (snl_add_msg_attr(nw, attrtype, sizeof(int16_t), &value));
840 }
841
842 static inline bool
snl_add_msg_attr_s32(struct snl_writer * nw,int attrtype,int32_t value)843 snl_add_msg_attr_s32(struct snl_writer *nw, int attrtype, int32_t value)
844 {
845 return (snl_add_msg_attr(nw, attrtype, sizeof(int32_t), &value));
846 }
847
848 static inline bool
snl_add_msg_attr_s64(struct snl_writer * nw,int attrtype,int64_t value)849 snl_add_msg_attr_s64(struct snl_writer *nw, int attrtype, int64_t value)
850 {
851 return (snl_add_msg_attr(nw, attrtype, sizeof(int64_t), &value));
852 }
853
854 static inline bool
snl_add_msg_attr_flag(struct snl_writer * nw,int attrtype)855 snl_add_msg_attr_flag(struct snl_writer *nw, int attrtype)
856 {
857 return (snl_add_msg_attr(nw, attrtype, 0, NULL));
858 }
859
860 static inline bool
snl_add_msg_attr_string(struct snl_writer * nw,int attrtype,const char * str)861 snl_add_msg_attr_string(struct snl_writer *nw, int attrtype, const char *str)
862 {
863 return (snl_add_msg_attr(nw, attrtype, strlen(str) + 1, str));
864 }
865
866
867 static inline int
snl_get_msg_offset(const struct snl_writer * nw)868 snl_get_msg_offset(const struct snl_writer *nw)
869 {
870 return (nw->offset - ((char *)nw->hdr - nw->base));
871 }
872
873 static inline void *
_snl_restore_msg_offset(const struct snl_writer * nw,int off)874 _snl_restore_msg_offset(const struct snl_writer *nw, int off)
875 {
876 return ((void *)((char *)nw->hdr + off));
877 }
878 #define snl_restore_msg_offset(_ns, _off, _t) ((_t *)_snl_restore_msg_offset(_ns, _off))
879
880 static inline int
snl_add_msg_attr_nested(struct snl_writer * nw,int attrtype)881 snl_add_msg_attr_nested(struct snl_writer *nw, int attrtype)
882 {
883 int off = snl_get_msg_offset(nw);
884 struct nlattr *nla = snl_reserve_msg_data(nw, sizeof(struct nlattr), struct nlattr);
885 if (__predict_false(nla == NULL))
886 return (0);
887 nla->nla_type = attrtype;
888 return (off);
889 }
890
891 static inline void
snl_end_attr_nested(const struct snl_writer * nw,int off)892 snl_end_attr_nested(const struct snl_writer *nw, int off)
893 {
894 if (!nw->error) {
895 struct nlattr *nla = snl_restore_msg_offset(nw, off, struct nlattr);
896 nla->nla_len = NETLINK_ALIGN(snl_get_msg_offset(nw) - off);
897 }
898 }
899
900 static inline struct nlmsghdr *
snl_create_msg_request(struct snl_writer * nw,int nlmsg_type)901 snl_create_msg_request(struct snl_writer *nw, int nlmsg_type)
902 {
903 assert(nw->hdr == NULL);
904
905 struct nlmsghdr *hdr = snl_reserve_msg_object(nw, struct nlmsghdr);
906 hdr->nlmsg_type = nlmsg_type;
907 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
908 nw->hdr = hdr;
909
910 return (hdr);
911 }
912
913 static void
snl_abort_msg(struct snl_writer * nw)914 snl_abort_msg(struct snl_writer *nw)
915 {
916 if (nw->hdr != NULL) {
917 int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
918
919 nw->offset -= offset;
920 nw->hdr = NULL;
921 }
922 }
923
924 static inline struct nlmsghdr *
snl_finalize_msg(struct snl_writer * nw)925 snl_finalize_msg(struct snl_writer *nw)
926 {
927 if (nw->error)
928 snl_abort_msg(nw);
929 if (nw->hdr != NULL) {
930 struct nlmsghdr *hdr = nw->hdr;
931
932 int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
933 hdr->nlmsg_len = offset;
934 hdr->nlmsg_seq = snl_get_seq(nw->ss);
935 nw->hdr = NULL;
936
937 return (hdr);
938 }
939 return (NULL);
940 }
941
942 static inline bool
snl_send_msgs(struct snl_writer * nw)943 snl_send_msgs(struct snl_writer *nw)
944 {
945 int offset = nw->offset;
946
947 assert(nw->hdr == NULL);
948 nw->offset = 0;
949
950 return (snl_send(nw->ss, nw->base, offset));
951 }
952
953 static const struct snl_hdr_parser *snl_all_core_parsers[] = {
954 &snl_errmsg_parser, &snl_donemsg_parser,
955 };
956
957 #endif
958