xref: /freebsd/sys/netlink/netlink_snl.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 <stdalign.h>
37 #include <stddef.h>
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netlink/netlink.h>
47 #include <netlink/netlink_bitset.h>
48 
49 #define _roundup2(x, y)         (((x)+((y)-1))&(~((y)-1)))
50 
51 #define NETLINK_ALIGN_SIZE      sizeof(uint32_t)
52 #define NETLINK_ALIGN(_len)     _roundup2(_len, NETLINK_ALIGN_SIZE)
53 
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)
59 
60 #define	NLA_TYPE(_nla)		((_nla)->nla_type & 0x3FFF)
61 
62 #define NLA_NEXT(_attr) (struct nlattr *)(void *)((char *)_attr + NLA_ALIGN(_attr->nla_len))
63 
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))
70 
71 #define	NL_ARRAY_LEN(_a)	(sizeof(_a) / sizeof((_a)[0]))
72 
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));
79 
80 static inline struct linear_buffer *
81 lb_init(uint32_t size)
82 {
83 	struct linear_buffer *lb = calloc(1, size);
84 
85 	if (lb != NULL) {
86 		lb->base = (char *)(lb + 1);
87 		lb->size = size - sizeof(*lb);
88 	}
89 
90 	return (lb);
91 }
92 
93 static inline void
94 lb_free(struct linear_buffer *lb)
95 {
96 	free(lb);
97 }
98 
99 static inline char *
100 lb_allocz(struct linear_buffer *lb, int len)
101 {
102 	len = roundup2(len, alignof(__max_align_t));
103 	if (lb->offset + len > lb->size)
104 		return (NULL);
105 	void *data = (void *)(lb->base + lb->offset);
106 	lb->offset += len;
107 	return (data);
108 }
109 
110 static inline void
111 lb_clear(struct linear_buffer *lb)
112 {
113 	memset(lb->base, 0, lb->offset);
114 	lb->offset = 0;
115 }
116 
117 struct snl_state {
118 	int fd;
119 	char *buf;
120 	size_t off;
121 	size_t bufsize;
122 	size_t datalen;
123 	uint32_t seq;
124 	bool init_done;
125 	struct linear_buffer *lb;
126 };
127 #define	SCRATCH_BUFFER_SIZE	1024
128 #define	SNL_WRITER_BUFFER_SIZE	256
129 
130 typedef void snl_parse_field_f(struct snl_state *ss, void *hdr, void *target);
131 struct snl_field_parser {
132 	uint16_t		off_in;
133 	uint16_t		off_out;
134 	snl_parse_field_f	*cb;
135 };
136 
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 */
143 
144 	/* Optional parser argument */
145 	union {
146 		const void		*arg;
147 		const uint32_t		 arg_u32;
148 	};
149 };
150 
151 typedef bool snl_parse_post_f(struct snl_state *ss, void *target);
152 
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 */
161 };
162 
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,						\
167 	.fp = &((_fp)[0]),						\
168 	.np = &((_np)[0]),						\
169 	.fp_size = NL_ARRAY_LEN(_fp),					\
170 	.np_size = NL_ARRAY_LEN(_np),					\
171 	.cb_post = _cb,							\
172 }
173 
174 #define	SNL_DECLARE_PARSER(_name, _t, _fp, _np)				\
175 	SNL_DECLARE_PARSER_EXT(_name, sizeof(_t), 0, _fp, _np, NULL)
176 
177 #define	SNL_DECLARE_FIELD_PARSER_EXT(_name, _sz_h_in, _sz_out, _fp, _cb) \
178 static const struct snl_hdr_parser _name = {				\
179 	.in_hdr_size = _sz_h_in,					\
180 	.out_size = _sz_out,						\
181 	.fp = &((_fp)[0]),						\
182 	.fp_size = NL_ARRAY_LEN(_fp),					\
183 	.cb_post = _cb,							\
184 }
185 
186 #define	SNL_DECLARE_FIELD_PARSER(_name, _t, _fp)			\
187 	SNL_DECLARE_FIELD_PARSER_EXT(_name, sizeof(_t), 0, _fp, NULL)
188 
189 #define	SNL_DECLARE_ATTR_PARSER_EXT(_name, _sz_out, _np, _cb)		\
190 static const struct snl_hdr_parser _name = {				\
191 	.out_size = _sz_out,						\
192 	.np = &((_np)[0]),						\
193 	.np_size = NL_ARRAY_LEN(_np),					\
194 	.cb_post = _cb,							\
195 }
196 
197 #define	SNL_DECLARE_ATTR_PARSER(_name, _np)				\
198 	SNL_DECLARE_ATTR_PARSER_EXT(_name, 0, _np, NULL)
199 
200 
201 static inline void *
202 snl_allocz(struct snl_state *ss, int len)
203 {
204 	void *data = lb_allocz(ss->lb, len);
205 
206 	if (data == NULL) {
207 		uint32_t size = ss->lb->size * 2;
208 
209 		while (size < len + sizeof(struct linear_buffer))
210 			size *= 2;
211 
212 		struct linear_buffer *lb = lb_init(size);
213 
214 		if (lb != NULL) {
215 			lb->next = ss->lb;
216 			ss->lb = lb;
217 			data = lb_allocz(ss->lb, len);
218 		}
219 	}
220 
221 	return (data);
222 }
223 
224 static inline void
225 snl_clear_lb(struct snl_state *ss)
226 {
227 	struct linear_buffer *lb = ss->lb;
228 
229 	lb_clear(lb);
230 	lb = lb->next;
231 	ss->lb->next = NULL;
232 	/* Remove all linear bufs except the largest one */
233 	while (lb != NULL) {
234 		struct linear_buffer *lb_next = lb->next;
235 		lb_free(lb);
236 		lb = lb_next;
237 	}
238 }
239 
240 static void
241 snl_free(struct snl_state *ss)
242 {
243 	if (ss->init_done) {
244 		close(ss->fd);
245 		if (ss->buf != NULL)
246 			free(ss->buf);
247 		if (ss->lb != NULL) {
248 			snl_clear_lb(ss);
249 			lb_free(ss->lb);
250 		}
251 	}
252 }
253 
254 static inline bool
255 snl_init(struct snl_state *ss, int netlink_family)
256 {
257 	memset(ss, 0, sizeof(*ss));
258 
259 	ss->fd = socket(AF_NETLINK, SOCK_RAW, netlink_family);
260 	if (ss->fd == -1)
261 		return (false);
262 	ss->init_done = true;
263 
264 	int val = 1;
265 	socklen_t optlen = sizeof(val);
266 	if (setsockopt(ss->fd, SOL_NETLINK, NETLINK_EXT_ACK, &val, optlen) == -1) {
267 		snl_free(ss);
268 		return (false);
269 	}
270 
271 	int rcvbuf;
272 	if (getsockopt(ss->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen) == -1) {
273 		snl_free(ss);
274 		return (false);
275 	}
276 
277 	ss->bufsize = rcvbuf;
278 	ss->buf = malloc(ss->bufsize);
279 	if (ss->buf == NULL) {
280 		snl_free(ss);
281 		return (false);
282 	}
283 
284 	ss->lb = lb_init(SCRATCH_BUFFER_SIZE);
285 	if (ss->lb == NULL) {
286 		snl_free(ss);
287 		return (false);
288 	}
289 
290 	return (true);
291 }
292 
293 static inline bool
294 snl_send(struct snl_state *ss, void *data, int sz)
295 {
296 	return (send(ss->fd, data, sz, 0) == sz);
297 }
298 
299 static inline bool
300 snl_send_message(struct snl_state *ss, struct nlmsghdr *hdr)
301 {
302 	ssize_t sz = NLMSG_ALIGN(hdr->nlmsg_len);
303 
304 	return (send(ss->fd, hdr, sz, 0) == sz);
305 }
306 
307 static inline uint32_t
308 snl_get_seq(struct snl_state *ss)
309 {
310 	return (++ss->seq);
311 }
312 
313 struct snl_msg_info {
314 	int		cmsg_type;
315 	int		cmsg_level;
316 	uint32_t	process_id;
317 	uint8_t		port_id;
318 	uint8_t		seq_id;
319 };
320 static inline bool parse_cmsg(struct snl_state *ss, const struct msghdr *msg,
321     struct snl_msg_info *attrs);
322 
323 static inline struct nlmsghdr *
324 snl_read_message_dbg(struct snl_state *ss, struct snl_msg_info *cinfo)
325 {
326 	memset(cinfo, 0, sizeof(*cinfo));
327 
328 	if (ss->off == ss->datalen) {
329 		struct sockaddr_nl nladdr;
330 		char cbuf[64];
331 
332 		struct iovec iov = {
333 			.iov_base = ss->buf,
334 			.iov_len = ss->bufsize,
335 		};
336 		struct msghdr msg = {
337 			.msg_name = &nladdr,
338 			.msg_namelen = sizeof(nladdr),
339 			.msg_iov = &iov,
340 			.msg_iovlen = 1,
341 			.msg_control = cbuf,
342 			.msg_controllen = sizeof(cbuf),
343 		};
344 		ss->off = 0;
345 		ss->datalen = 0;
346 		for (;;) {
347 			ssize_t datalen = recvmsg(ss->fd, &msg, 0);
348 			if (datalen > 0) {
349 				ss->datalen = datalen;
350 				parse_cmsg(ss, &msg, cinfo);
351 				break;
352 			} else if (errno != EINTR)
353 				return (NULL);
354 		}
355 	}
356 	struct nlmsghdr *hdr = (struct nlmsghdr *)(void *)&ss->buf[ss->off];
357 	ss->off += NLMSG_ALIGN(hdr->nlmsg_len);
358 	return (hdr);
359 }
360 
361 
362 static inline struct nlmsghdr *
363 snl_read_message(struct snl_state *ss)
364 {
365 	if (ss->off == ss->datalen) {
366 		struct sockaddr_nl nladdr;
367 		struct iovec iov = {
368 			.iov_base = ss->buf,
369 			.iov_len = ss->bufsize,
370 		};
371 		struct msghdr msg = {
372 			.msg_name = &nladdr,
373 			.msg_namelen = sizeof(nladdr),
374 			.msg_iov = &iov,
375 			.msg_iovlen = 1,
376 		};
377 		ss->off = 0;
378 		ss->datalen = 0;
379 		for (;;) {
380 			ssize_t datalen = recvmsg(ss->fd, &msg, 0);
381 			if (datalen > 0) {
382 				ss->datalen = datalen;
383 				break;
384 			} else if (errno != EINTR)
385 				return (NULL);
386 		}
387 	}
388 	struct nlmsghdr *hdr = (struct nlmsghdr *)(void *)&ss->buf[ss->off];
389 	ss->off += NLMSG_ALIGN(hdr->nlmsg_len);
390 	return (hdr);
391 }
392 
393 static inline struct nlmsghdr *
394 snl_read_reply(struct snl_state *ss, uint32_t nlmsg_seq)
395 {
396 	struct nlmsghdr *hdr;
397 
398 	while ((hdr = snl_read_message(ss)) != NULL) {
399 		if (hdr->nlmsg_seq == nlmsg_seq)
400 			return (hdr);
401 	}
402 
403 	return (NULL);
404 }
405 
406 /*
407  * Checks that attributes are sorted by attribute type.
408  */
409 static inline void
410 snl_verify_parsers(const struct snl_hdr_parser **parser, int count)
411 {
412 	for (int i = 0; i < count; i++) {
413 		const struct snl_hdr_parser *p = parser[i];
414 		int attr_type = 0;
415 		for (int j = 0; j < p->np_size; j++) {
416 			assert(p->np[j].type > attr_type);
417 			attr_type = p->np[j].type;
418 		}
419 	}
420 }
421 #define	SNL_VERIFY_PARSERS(_p)	snl_verify_parsers((_p), NL_ARRAY_LEN(_p))
422 
423 static const struct snl_attr_parser *
424 find_parser(const struct snl_attr_parser *ps, int pslen, int key)
425 {
426 	int left_i = 0, right_i = pslen - 1;
427 
428 	if (key < ps[0].type || key > ps[pslen - 1].type)
429 		return (NULL);
430 
431 	while (left_i + 1 < right_i) {
432 		int mid_i = (left_i + right_i) / 2;
433 		if (key < ps[mid_i].type)
434 			right_i = mid_i;
435 		else if (key > ps[mid_i].type)
436 			left_i = mid_i + 1;
437 		else
438 			return (&ps[mid_i]);
439 	}
440 	if (ps[left_i].type == key)
441 		return (&ps[left_i]);
442 	else if (ps[right_i].type == key)
443 		return (&ps[right_i]);
444 	return (NULL);
445 }
446 
447 static inline bool
448 snl_parse_attrs_raw(struct snl_state *ss, struct nlattr *nla_head, int len,
449     const struct snl_attr_parser *ps, int pslen, void *target)
450 {
451 	struct nlattr *nla;
452 
453 	NLA_FOREACH(nla, nla_head, len) {
454 		if (nla->nla_len < sizeof(struct nlattr))
455 			return (false);
456 		int nla_type = nla->nla_type & NLA_TYPE_MASK;
457 		const struct snl_attr_parser *s = find_parser(ps, pslen, nla_type);
458 		if (s != NULL) {
459 			void *ptr = (void *)((char *)target + s->off);
460 			if (!s->cb(ss, nla, s->arg, ptr))
461 				return (false);
462 		}
463 	}
464 	return (true);
465 }
466 
467 static inline bool
468 snl_parse_attrs(struct snl_state *ss, struct nlmsghdr *hdr, int hdrlen,
469     const struct snl_attr_parser *ps, int pslen, void *target)
470 {
471 	int off = NLMSG_HDRLEN + NETLINK_ALIGN(hdrlen);
472 	int len = hdr->nlmsg_len - off;
473 	struct nlattr *nla_head = (struct nlattr *)(void *)((char *)hdr + off);
474 
475 	return (snl_parse_attrs_raw(ss, nla_head, len, ps, pslen, target));
476 }
477 
478 static inline void
479 snl_parse_fields(struct snl_state *ss, struct nlmsghdr *hdr, int hdrlen __unused,
480     const struct snl_field_parser *ps, int pslen, void *target)
481 {
482 	for (int i = 0; i < pslen; i++) {
483 		const struct snl_field_parser *fp = &ps[i];
484 		void *src = (char *)hdr + fp->off_in;
485 		void *dst = (char *)target + fp->off_out;
486 
487 		fp->cb(ss, src, dst);
488 	}
489 }
490 
491 static inline bool
492 snl_parse_header(struct snl_state *ss, void *hdr, int len,
493     const struct snl_hdr_parser *parser, void *target)
494 {
495 	struct nlattr *nla_head;
496 
497 	/* Extract fields first (if any) */
498 	snl_parse_fields(ss, hdr, parser->in_hdr_size, parser->fp, parser->fp_size, target);
499 
500 	nla_head = (struct nlattr *)(void *)((char *)hdr + parser->in_hdr_size);
501 	bool result = snl_parse_attrs_raw(ss, nla_head, len - parser->in_hdr_size,
502 	    parser->np, parser->np_size, target);
503 
504 	if (result && parser->cb_post != NULL)
505 		result = parser->cb_post(ss, target);
506 
507 	return (result);
508 }
509 
510 static inline bool
511 snl_parse_nlmsg(struct snl_state *ss, struct nlmsghdr *hdr,
512     const struct snl_hdr_parser *parser, void *target)
513 {
514 	return (snl_parse_header(ss, hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser, target));
515 }
516 
517 static inline bool
518 snl_attr_get_flag(struct snl_state *ss __unused, struct nlattr *nla, const void *arg __unused,
519     void *target)
520 {
521 	if (NLA_DATA_LEN(nla) == 0) {
522 		*((uint8_t *)target) = 1;
523 		return (true);
524 	}
525 	return (false);
526 }
527 
528 static inline bool
529 snl_attr_get_bytes(struct snl_state *ss __unused, struct nlattr *nla, const void *arg,
530     void *target)
531 {
532 	if ((size_t)NLA_DATA_LEN(nla) != (size_t)arg)
533 		return (false);
534 
535 	memcpy(target, NLA_DATA_CONST(nla), (size_t)arg);
536 
537 	return (true);
538 }
539 
540 static inline bool
541 snl_attr_get_uint8(struct snl_state *ss __unused, struct nlattr *nla,
542     const void *arg __unused, void *target)
543 {
544 	if (NLA_DATA_LEN(nla) == sizeof(uint8_t)) {
545 		*((uint8_t *)target) = *((const uint8_t *)NLA_DATA_CONST(nla));
546 		return (true);
547 	}
548 	return (false);
549 }
550 
551 static inline bool
552 snl_attr_get_uint16(struct snl_state *ss __unused, struct nlattr *nla,
553     const void *arg __unused, void *target)
554 {
555 	if (NLA_DATA_LEN(nla) == sizeof(uint16_t)) {
556 		*((uint16_t *)target) = *((const uint16_t *)NLA_DATA_CONST(nla));
557 		return (true);
558 	}
559 	return (false);
560 }
561 
562 static inline bool
563 snl_attr_get_uint32(struct snl_state *ss __unused, struct nlattr *nla,
564     const void *arg __unused, void *target)
565 {
566 	if (NLA_DATA_LEN(nla) == sizeof(uint32_t)) {
567 		*((uint32_t *)target) = *((const uint32_t *)NLA_DATA_CONST(nla));
568 		return (true);
569 	}
570 	return (false);
571 }
572 
573 static inline bool
574 snl_attr_get_uint64(struct snl_state *ss __unused, struct nlattr *nla,
575     const void *arg __unused, void *target)
576 {
577 	if (NLA_DATA_LEN(nla) == sizeof(uint64_t)) {
578 		memcpy(target, NLA_DATA_CONST(nla), sizeof(uint64_t));
579 		return (true);
580 	}
581 	return (false);
582 }
583 
584 static inline bool
585 snl_attr_get_int8(struct snl_state *ss, struct nlattr *nla, const void *arg,
586     void *target)
587 {
588 	return (snl_attr_get_uint8(ss, nla, arg, target));
589 }
590 
591 static inline bool
592 snl_attr_get_int16(struct snl_state *ss, struct nlattr *nla, const void *arg,
593     void *target)
594 {
595 	return (snl_attr_get_uint16(ss, nla, arg, target));
596 }
597 
598 static inline bool
599 snl_attr_get_int32(struct snl_state *ss, struct nlattr *nla, const void *arg,
600     void *target)
601 {
602 	return (snl_attr_get_uint32(ss, nla, arg, target));
603 }
604 
605 static inline bool
606 snl_attr_get_int64(struct snl_state *ss, struct nlattr *nla, const void *arg,
607     void *target)
608 {
609 	return (snl_attr_get_uint64(ss, nla, arg, target));
610 }
611 
612 static inline bool
613 snl_attr_get_string(struct snl_state *ss __unused, struct nlattr *nla,
614     const void *arg __unused, void *target)
615 {
616 	size_t maxlen = NLA_DATA_LEN(nla);
617 
618 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
619 		*((char **)target) = (char *)NLA_DATA(nla);
620 		return (true);
621 	}
622 	return (false);
623 }
624 
625 static inline bool
626 snl_attr_get_stringn(struct snl_state *ss, struct nlattr *nla,
627     const void *arg __unused, void *target)
628 {
629 	int maxlen = NLA_DATA_LEN(nla);
630 
631 	char *buf = snl_allocz(ss, maxlen + 1);
632 	if (buf == NULL)
633 		return (false);
634 	buf[maxlen] = '\0';
635 	memcpy(buf, NLA_DATA(nla), maxlen);
636 
637 	*((char **)target) = buf;
638 	return (true);
639 }
640 
641 static inline bool
642 snl_attr_copy_string(struct snl_state *ss, struct nlattr *nla,
643     const void *arg, void *target)
644 {
645 	char *tmp;
646 
647 	if (snl_attr_get_string(ss, nla, NULL, &tmp)) {
648 		strlcpy(target, tmp, (size_t)arg);
649 		return (true);
650 	}
651 	return (false);
652 }
653 
654 static inline bool
655 snl_attr_dup_string(struct snl_state *ss __unused, struct nlattr *nla,
656     const void *arg __unused, void *target)
657 {
658 	size_t maxlen = NLA_DATA_LEN(nla);
659 
660 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
661 		char *buf = snl_allocz(ss, maxlen);
662 		if (buf == NULL)
663 			return (false);
664 		memcpy(buf, NLA_DATA(nla), maxlen);
665 		*((char **)target) = buf;
666 		return (true);
667 	}
668 	return (false);
669 }
670 
671 static inline bool
672 snl_attr_get_nested(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
673 {
674 	const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
675 
676 	/* Assumes target points to the beginning of the structure */
677 	return (snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, target));
678 }
679 
680 struct snl_parray {
681 	uint32_t count;
682 	void **items;
683 };
684 
685 static inline bool
686 snl_attr_get_parray_sz(struct snl_state *ss, struct nlattr *container_nla,
687     uint32_t start_size, const void *arg, void *target)
688 {
689 	const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
690 	struct snl_parray *array = target;
691 	struct nlattr *nla;
692 	uint32_t count = 0, size = start_size;
693 
694 	if (p->out_size == 0)
695 		return (false);
696 
697 	array->items = snl_allocz(ss, size * sizeof(void *));
698 	if (array->items == NULL)
699 		return (false);
700 
701 	/*
702 	 * If the provided parser is an attribute parser, assume that each
703 	 *  nla in the container nla is the container nla itself and parse
704 	 *  the contents of this nla.
705 	 * Otherwise, run the parser on raw data, assuming the header of this
706 	 * data has u16 field with total size in the beginning.
707 	 */
708 	uint32_t data_off = 0;
709 
710 	if (p->in_hdr_size == 0)
711 		data_off = sizeof(struct nlattr);
712 
713 	NLA_FOREACH(nla, NLA_DATA(container_nla), NLA_DATA_LEN(container_nla)) {
714 		void *item = snl_allocz(ss, p->out_size);
715 
716 		if (item == NULL)
717 			return (false);
718 
719 		void *data = (char *)(void *)nla + data_off;
720 		int data_len = nla->nla_len - data_off;
721 
722 		if (!(snl_parse_header(ss, data, data_len, p, item)))
723 			return (false);
724 
725 		if (count == size) {
726 			uint32_t new_size = size * 2;
727 			void **new_array = snl_allocz(ss, new_size *sizeof(void *));
728 
729 			memcpy(new_array, array->items, size * sizeof(void *));
730 			array->items = new_array;
731 			size = new_size;
732 		}
733 		array->items[count++] = item;
734 	}
735 	array->count = count;
736 
737 	return (true);
738 }
739 
740 /*
741  * Parses and stores the unknown-size array.
742  * Assumes each array item is a container and the NLAs in the container are parsable
743  *  by the parser provided in @arg.
744  * Assumes @target is struct snl_parray
745  */
746 static inline bool
747 snl_attr_get_parray(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
748 {
749 	return (snl_attr_get_parray_sz(ss, nla, 8, arg, target));
750 }
751 
752 static inline bool
753 snl_attr_get_nla(struct snl_state *ss __unused, struct nlattr *nla,
754     const void *arg __unused, void *target)
755 {
756 	*((struct nlattr **)target) = nla;
757 	return (true);
758 }
759 
760 static inline bool
761 snl_attr_dup_nla(struct snl_state *ss, struct nlattr *nla,
762     const void *arg __unused, void *target)
763 {
764 	void *ptr = snl_allocz(ss, nla->nla_len);
765 
766 	if (ptr != NULL) {
767 		memcpy(ptr, nla, nla->nla_len);
768 		*((void **)target) = ptr;
769 		return (true);
770 	}
771 	return (false);
772 }
773 
774 static inline bool
775 snl_attr_copy_struct(struct snl_state *ss, struct nlattr *nla,
776     const void *arg __unused, void *target)
777 {
778 	void *ptr = snl_allocz(ss, NLA_DATA_LEN(nla));
779 
780 	if (ptr != NULL) {
781 		memcpy(ptr, NLA_DATA(nla), NLA_DATA_LEN(nla));
782 		*((void **)target) = ptr;
783 		return (true);
784 	}
785 	return (false);
786 }
787 
788 static inline bool
789 snl_attr_dup_struct(struct snl_state *ss, struct nlattr *nla,
790     const void *arg __unused, void *target)
791 {
792 	void *ptr = snl_allocz(ss, NLA_DATA_LEN(nla));
793 
794 	if (ptr != NULL) {
795 		memcpy(ptr, NLA_DATA(nla), NLA_DATA_LEN(nla));
796 		*((void **)target) = ptr;
797 		return (true);
798 	}
799 	return (false);
800 }
801 
802 struct snl_attr_bit {
803 	uint32_t	bit_index;
804 	char		*bit_name;
805 	int		bit_value;
806 };
807 
808 struct snl_attr_bits {
809 	uint32_t num_bits;
810 	struct snl_attr_bit **bits;
811 };
812 
813 #define	_OUT(_field)	offsetof(struct snl_attr_bit, _field)
814 static const struct snl_attr_parser _nla_p_bit[] = {
815 	{ .type = NLA_BITSET_BIT_INDEX, .off = _OUT(bit_index), .cb = snl_attr_get_uint32 },
816 	{ .type = NLA_BITSET_BIT_NAME, .off = _OUT(bit_name), .cb = snl_attr_dup_string },
817 	{ .type = NLA_BITSET_BIT_VALUE, .off = _OUT(bit_value), .cb = snl_attr_get_flag },
818 };
819 #undef _OUT
820 SNL_DECLARE_ATTR_PARSER_EXT(_nla_bit_parser, sizeof(struct snl_attr_bit), _nla_p_bit, NULL);
821 
822 struct snl_attr_bitset {
823 	uint32_t		nla_bitset_size;
824 	uint32_t		*nla_bitset_mask;
825 	uint32_t		*nla_bitset_value;
826 	struct snl_attr_bits	bits;
827 };
828 
829 #define	_OUT(_field)	offsetof(struct snl_attr_bitset, _field)
830 static const struct snl_attr_parser _nla_p_bitset[] = {
831 	{ .type = NLA_BITSET_SIZE, .off = _OUT(nla_bitset_size), .cb = snl_attr_get_uint32 },
832 	{ .type = NLA_BITSET_BITS, .off = _OUT(bits), .cb = snl_attr_get_parray, .arg = &_nla_bit_parser },
833 	{ .type = NLA_BITSET_VALUE, .off = _OUT(nla_bitset_mask), .cb = snl_attr_dup_nla },
834 	{ .type = NLA_BITSET_MASK, .off = _OUT(nla_bitset_value), .cb = snl_attr_dup_nla },
835 };
836 
837 static inline bool
838 _cb_p_bitset(struct snl_state *ss __unused, void *_target)
839 {
840 	struct snl_attr_bitset *target = _target;
841 
842 	uint32_t sz_bytes = _roundup2(target->nla_bitset_size, 32) / 8;
843 
844 	if (target->nla_bitset_mask != NULL) {
845 		struct nlattr *nla = (struct nlattr *)target->nla_bitset_mask;
846 		uint32_t data_len = NLA_DATA_LEN(nla);
847 
848 		if (data_len != sz_bytes || _roundup2(data_len, 4) != data_len)
849 			return (false);
850 		target->nla_bitset_mask = (uint32_t *)NLA_DATA(nla);
851 	}
852 
853 	if (target->nla_bitset_value != NULL) {
854 		struct nlattr *nla = (struct nlattr *)target->nla_bitset_value;
855 		uint32_t data_len = NLA_DATA_LEN(nla);
856 
857 		if (data_len != sz_bytes || _roundup2(data_len, 4) != data_len)
858 			return (false);
859 		target->nla_bitset_value = (uint32_t *)NLA_DATA(nla);
860 	}
861 	return (true);
862 }
863 #undef _OUT
864 SNL_DECLARE_ATTR_PARSER_EXT(_nla_bitset_parser,
865 		sizeof(struct snl_attr_bitset),
866 		_nla_p_bitset, _cb_p_bitset);
867 
868 /*
869  * Parses the compact bitset representation.
870  */
871 static inline bool
872 snl_attr_get_bitset_c(struct snl_state *ss, struct nlattr *nla,
873     const void *arg __unused, void *_target)
874 {
875 	const struct snl_hdr_parser *p = &_nla_bitset_parser;
876 	struct snl_attr_bitset *target = _target;
877 
878 	/* Assumes target points to the beginning of the structure */
879 	if (!snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, _target))
880 		return (false);
881 	if (target->nla_bitset_mask == NULL || target->nla_bitset_value == NULL)
882 		return (false);
883 	return (true);
884 }
885 
886 static inline void
887 snl_field_get_uint8(struct snl_state *ss __unused, void *src, void *target)
888 {
889 	*((uint8_t *)target) = *((uint8_t *)src);
890 }
891 
892 static inline void
893 snl_field_get_uint16(struct snl_state *ss __unused, void *src, void *target)
894 {
895 	*((uint16_t *)target) = *((uint16_t *)src);
896 }
897 
898 static inline void
899 snl_field_get_uint32(struct snl_state *ss __unused, void *src, void *target)
900 {
901 	*((uint32_t *)target) = *((uint32_t *)src);
902 }
903 
904 static inline void
905 snl_field_get_ptr(struct snl_state *ss __unused, void *src, void *target)
906 {
907 	*((void **)target) = src;
908 }
909 
910 struct snl_errmsg_data {
911 	struct nlmsghdr	*orig_hdr;
912 	int		error;
913 	uint32_t	error_offs;
914 	char		*error_str;
915 	struct nlattr	*cookie;
916 };
917 
918 #define	_IN(_field)	offsetof(struct nlmsgerr, _field)
919 #define	_OUT(_field)	offsetof(struct snl_errmsg_data, _field)
920 static const struct snl_attr_parser nla_p_errmsg[] = {
921 	{ .type = NLMSGERR_ATTR_MSG, .off = _OUT(error_str), .cb = snl_attr_get_string },
922 	{ .type = NLMSGERR_ATTR_OFFS, .off = _OUT(error_offs), .cb = snl_attr_get_uint32 },
923 	{ .type = NLMSGERR_ATTR_COOKIE, .off = _OUT(cookie), .cb = snl_attr_get_nla },
924 };
925 
926 static const struct snl_field_parser nlf_p_errmsg[] = {
927 	{ .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
928 	{ .off_in = _IN(msg), .off_out = _OUT(orig_hdr), .cb = snl_field_get_ptr },
929 };
930 #undef _IN
931 #undef _OUT
932 SNL_DECLARE_PARSER(snl_errmsg_parser, struct nlmsgerr, nlf_p_errmsg, nla_p_errmsg);
933 
934 #define	_IN(_field)	offsetof(struct nlmsgerr, _field)
935 #define	_OUT(_field)	offsetof(struct snl_errmsg_data, _field)
936 static const struct snl_field_parser nlf_p_donemsg[] = {
937 	{ .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
938 };
939 #undef _IN
940 #undef _OUT
941 SNL_DECLARE_FIELD_PARSER(snl_donemsg_parser, struct nlmsgerr, nlf_p_donemsg);
942 
943 static inline bool
944 snl_parse_errmsg(struct snl_state *ss, struct nlmsghdr *hdr, struct snl_errmsg_data *e)
945 {
946 	if ((hdr->nlmsg_flags & NLM_F_CAPPED) != 0)
947 		return (snl_parse_nlmsg(ss, hdr, &snl_errmsg_parser, e));
948 
949 	const struct snl_hdr_parser *ps = &snl_errmsg_parser;
950 	struct nlmsgerr *errmsg = (struct nlmsgerr *)(hdr + 1);
951 	int hdrlen = sizeof(int) + NLMSG_ALIGN(errmsg->msg.nlmsg_len);
952 	struct nlattr *attr_head = (struct nlattr *)(void *)((char *)errmsg + hdrlen);
953 	int attr_len = hdr->nlmsg_len - sizeof(struct nlmsghdr) - hdrlen;
954 
955 	snl_parse_fields(ss, (struct nlmsghdr *)errmsg, hdrlen, ps->fp, ps->fp_size, e);
956 	return (snl_parse_attrs_raw(ss, attr_head, attr_len, ps->np, ps->np_size, e));
957 }
958 
959 static inline bool
960 snl_read_reply_code(struct snl_state *ss, uint32_t nlmsg_seq, struct snl_errmsg_data *e)
961 {
962 	struct nlmsghdr *hdr = snl_read_reply(ss, nlmsg_seq);
963 
964 	if (hdr == NULL) {
965 		e->error = EINVAL;
966 	} else if (hdr->nlmsg_type == NLMSG_ERROR) {
967 		if (!snl_parse_errmsg(ss, hdr, e))
968 			e->error = EINVAL;
969 		return (e->error == 0);
970 	}
971 
972 	return (false);
973 }
974 
975 #define	_OUT(_field)	offsetof(struct snl_msg_info, _field)
976 static const struct snl_attr_parser _nla_p_cinfo[] = {
977 	{ .type = NLMSGINFO_ATTR_PROCESS_ID, .off = _OUT(process_id), .cb = snl_attr_get_uint32 },
978 	{ .type = NLMSGINFO_ATTR_PORT_ID, .off = _OUT(port_id), .cb = snl_attr_get_uint32 },
979 	{ .type = NLMSGINFO_ATTR_SEQ_ID, .off = _OUT(seq_id), .cb = snl_attr_get_uint32 },
980 };
981 #undef _OUT
982 SNL_DECLARE_ATTR_PARSER(snl_msg_info_parser, _nla_p_cinfo);
983 
984 static inline bool
985 parse_cmsg(struct snl_state *ss, const struct msghdr *msg, struct snl_msg_info *attrs)
986 {
987 	for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
988 	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
989 		if (cmsg->cmsg_level != SOL_NETLINK || cmsg->cmsg_type != NETLINK_MSG_INFO)
990 			continue;
991 
992 		void *data = CMSG_DATA(cmsg);
993 		int len = cmsg->cmsg_len - ((char *)data - (char *)cmsg);
994 		const struct snl_hdr_parser *ps = &snl_msg_info_parser;
995 
996 		return (snl_parse_attrs_raw(ss, data, len, ps->np, ps->np_size, attrs));
997 	}
998 
999 	return (false);
1000 }
1001 
1002 /*
1003  * Assumes e is zeroed
1004  */
1005 static inline struct nlmsghdr *
1006 snl_read_reply_multi(struct snl_state *ss, uint32_t nlmsg_seq, struct snl_errmsg_data *e)
1007 {
1008 	struct nlmsghdr *hdr = snl_read_reply(ss, nlmsg_seq);
1009 
1010 	if (hdr == NULL) {
1011 		e->error = EINVAL;
1012 	} else if (hdr->nlmsg_type == NLMSG_ERROR) {
1013 		if (!snl_parse_errmsg(ss, hdr, e))
1014 			e->error = EINVAL;
1015 	} else if (hdr->nlmsg_type == NLMSG_DONE) {
1016 		snl_parse_nlmsg(ss, hdr, &snl_donemsg_parser, e);
1017 	} else
1018 		return (hdr);
1019 
1020 	return (NULL);
1021 }
1022 
1023 
1024 /* writer logic */
1025 struct snl_writer {
1026 	char			*base;
1027 	uint32_t		offset;
1028 	uint32_t		size;
1029 	struct nlmsghdr		*hdr;
1030 	struct snl_state	*ss;
1031 	bool			error;
1032 };
1033 
1034 static inline void
1035 snl_init_writer(struct snl_state *ss, struct snl_writer *nw)
1036 {
1037 	nw->size = SNL_WRITER_BUFFER_SIZE;
1038 	nw->base = snl_allocz(ss, nw->size);
1039 	if (nw->base == NULL) {
1040 		nw->error = true;
1041 		nw->size = 0;
1042 	}
1043 
1044 	nw->offset = 0;
1045 	nw->hdr = NULL;
1046 	nw->error = false;
1047 	nw->ss = ss;
1048 }
1049 
1050 static inline bool
1051 snl_realloc_msg_buffer(struct snl_writer *nw, size_t sz)
1052 {
1053 	uint32_t new_size = nw->size * 2;
1054 
1055 	while (new_size < nw->size + sz)
1056 		new_size *= 2;
1057 
1058 	if (nw->error)
1059 		return (false);
1060 
1061 	if (snl_allocz(nw->ss, new_size) == NULL) {
1062 		nw->error = true;
1063 		return (false);
1064 	}
1065 	nw->size = new_size;
1066 
1067 	void *new_base = nw->ss->lb->base;
1068 	if (new_base != nw->base) {
1069 		memcpy(new_base, nw->base, nw->offset);
1070 		if (nw->hdr != NULL) {
1071 			int hdr_off = (char *)(nw->hdr) - nw->base;
1072 
1073 			nw->hdr = (struct nlmsghdr *)
1074 			    (void *)((char *)new_base + hdr_off);
1075 		}
1076 		nw->base = new_base;
1077 	}
1078 
1079 	return (true);
1080 }
1081 
1082 static inline void *
1083 snl_reserve_msg_data_raw(struct snl_writer *nw, size_t sz)
1084 {
1085 	sz = NETLINK_ALIGN(sz);
1086 
1087         if (__predict_false(nw->offset + sz > nw->size)) {
1088 		if (!snl_realloc_msg_buffer(nw, sz))
1089 			return (NULL);
1090         }
1091 
1092         void *data_ptr = &nw->base[nw->offset];
1093         nw->offset += sz;
1094 
1095         return (data_ptr);
1096 }
1097 #define snl_reserve_msg_object(_ns, _t)	((_t *)snl_reserve_msg_data_raw(_ns, sizeof(_t)))
1098 #define snl_reserve_msg_data(_ns, _sz, _t)	((_t *)snl_reserve_msg_data_raw(_ns, _sz))
1099 
1100 static inline void *
1101 _snl_reserve_msg_attr(struct snl_writer *nw, uint16_t nla_type, uint16_t sz)
1102 {
1103 	sz += sizeof(struct nlattr);
1104 
1105 	struct nlattr *nla = snl_reserve_msg_data(nw, sz, struct nlattr);
1106 	if (__predict_false(nla == NULL))
1107 		return (NULL);
1108 	nla->nla_type = nla_type;
1109 	nla->nla_len = sz;
1110 
1111 	return ((void *)(nla + 1));
1112 }
1113 #define	snl_reserve_msg_attr(_ns, _at, _t)	((_t *)_snl_reserve_msg_attr(_ns, _at, sizeof(_t)))
1114 
1115 static inline bool
1116 snl_add_msg_attr(struct snl_writer *nw, int attr_type, int attr_len, const void *data)
1117 {
1118 	int required_len = NLA_ALIGN(attr_len + sizeof(struct nlattr));
1119 
1120         if (__predict_false(nw->offset + required_len > nw->size)) {
1121 		if (!snl_realloc_msg_buffer(nw, required_len))
1122 			return (false);
1123 	}
1124 
1125         struct nlattr *nla = (struct nlattr *)(void *)(&nw->base[nw->offset]);
1126 
1127         nla->nla_len = attr_len + sizeof(struct nlattr);
1128         nla->nla_type = attr_type;
1129         if (attr_len > 0) {
1130 		if ((attr_len % 4) != 0) {
1131 			/* clear padding bytes */
1132 			bzero((char *)nla + required_len - 4, 4);
1133 		}
1134                 memcpy((nla + 1), data, attr_len);
1135 	}
1136         nw->offset += required_len;
1137         return (true);
1138 }
1139 
1140 static inline bool
1141 snl_add_msg_attr_raw(struct snl_writer *nw, const struct nlattr *nla_src)
1142 {
1143 	int attr_len = nla_src->nla_len - sizeof(struct nlattr);
1144 
1145 	assert(attr_len >= 0);
1146 
1147 	return (snl_add_msg_attr(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1)));
1148 }
1149 
1150 static inline bool
1151 snl_add_msg_attr_bool(struct snl_writer *nw, int attrtype, bool value)
1152 {
1153 	return (snl_add_msg_attr(nw, attrtype, sizeof(bool), &value));
1154 }
1155 
1156 static inline bool
1157 snl_add_msg_attr_u8(struct snl_writer *nw, int attrtype, uint8_t value)
1158 {
1159 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint8_t), &value));
1160 }
1161 
1162 static inline bool
1163 snl_add_msg_attr_u16(struct snl_writer *nw, int attrtype, uint16_t value)
1164 {
1165 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint16_t), &value));
1166 }
1167 
1168 static inline bool
1169 snl_add_msg_attr_u32(struct snl_writer *nw, int attrtype, uint32_t value)
1170 {
1171 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint32_t), &value));
1172 }
1173 
1174 static inline bool
1175 snl_add_msg_attr_u64(struct snl_writer *nw, int attrtype, uint64_t value)
1176 {
1177 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint64_t), &value));
1178 }
1179 
1180 static inline bool
1181 snl_add_msg_attr_s8(struct snl_writer *nw, int attrtype, int8_t value)
1182 {
1183 	return (snl_add_msg_attr(nw, attrtype, sizeof(int8_t), &value));
1184 }
1185 
1186 static inline bool
1187 snl_add_msg_attr_s16(struct snl_writer *nw, int attrtype, int16_t value)
1188 {
1189 	return (snl_add_msg_attr(nw, attrtype, sizeof(int16_t), &value));
1190 }
1191 
1192 static inline bool
1193 snl_add_msg_attr_s32(struct snl_writer *nw, int attrtype, int32_t value)
1194 {
1195 	return (snl_add_msg_attr(nw, attrtype, sizeof(int32_t), &value));
1196 }
1197 
1198 static inline bool
1199 snl_add_msg_attr_s64(struct snl_writer *nw, int attrtype, int64_t value)
1200 {
1201 	return (snl_add_msg_attr(nw, attrtype, sizeof(int64_t), &value));
1202 }
1203 
1204 static inline bool
1205 snl_add_msg_attr_flag(struct snl_writer *nw, int attrtype)
1206 {
1207 	return (snl_add_msg_attr(nw, attrtype, 0, NULL));
1208 }
1209 
1210 static inline bool
1211 snl_add_msg_attr_string(struct snl_writer *nw, int attrtype, const char *str)
1212 {
1213 	return (snl_add_msg_attr(nw, attrtype, strlen(str) + 1, str));
1214 }
1215 
1216 
1217 static inline int
1218 snl_get_msg_offset(const struct snl_writer *nw)
1219 {
1220         return (nw->offset - ((char *)nw->hdr - nw->base));
1221 }
1222 
1223 static inline void *
1224 _snl_restore_msg_offset(const struct snl_writer *nw, int off)
1225 {
1226 	return ((void *)((char *)nw->hdr + off));
1227 }
1228 #define	snl_restore_msg_offset(_ns, _off, _t)	((_t *)_snl_restore_msg_offset(_ns, _off))
1229 
1230 static inline int
1231 snl_add_msg_attr_nested(struct snl_writer *nw, int attrtype)
1232 {
1233 	int off = snl_get_msg_offset(nw);
1234 	struct nlattr *nla = snl_reserve_msg_data(nw, sizeof(struct nlattr), struct nlattr);
1235 	if (__predict_false(nla == NULL))
1236 		return (0);
1237 	nla->nla_type = attrtype;
1238 	return (off);
1239 }
1240 
1241 static inline void
1242 snl_end_attr_nested(const struct snl_writer *nw, int off)
1243 {
1244 	if (!nw->error) {
1245 		struct nlattr *nla = snl_restore_msg_offset(nw, off, struct nlattr);
1246 		nla->nla_len = NETLINK_ALIGN(snl_get_msg_offset(nw) - off);
1247 	}
1248 }
1249 
1250 static inline struct nlmsghdr *
1251 snl_create_msg_request(struct snl_writer *nw, int nlmsg_type)
1252 {
1253 	assert(nw->hdr == NULL);
1254 
1255 	struct nlmsghdr *hdr = snl_reserve_msg_object(nw, struct nlmsghdr);
1256 	hdr->nlmsg_type = nlmsg_type;
1257 	hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1258 	nw->hdr = hdr;
1259 
1260 	return (hdr);
1261 }
1262 
1263 static void
1264 snl_abort_msg(struct snl_writer *nw)
1265 {
1266 	if (nw->hdr != NULL) {
1267 		int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
1268 
1269 		nw->offset -= offset;
1270 		nw->hdr = NULL;
1271 	}
1272 }
1273 
1274 static inline struct nlmsghdr *
1275 snl_finalize_msg(struct snl_writer *nw)
1276 {
1277 	if (nw->error)
1278 		snl_abort_msg(nw);
1279 	if (nw->hdr != NULL) {
1280 		struct nlmsghdr *hdr = nw->hdr;
1281 
1282 		int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
1283 		hdr->nlmsg_len = offset;
1284 		hdr->nlmsg_seq = snl_get_seq(nw->ss);
1285 		nw->hdr = NULL;
1286 
1287 		return (hdr);
1288 	}
1289 	return (NULL);
1290 }
1291 
1292 static inline bool
1293 snl_send_msgs(struct snl_writer *nw)
1294 {
1295 	int offset = nw->offset;
1296 
1297 	assert(nw->hdr == NULL);
1298 	nw->offset = 0;
1299 
1300 	return (snl_send(nw->ss, nw->base, offset));
1301 }
1302 
1303 static const struct snl_hdr_parser *snl_all_core_parsers[] = {
1304 	&snl_errmsg_parser, &snl_donemsg_parser,
1305 	&_nla_bit_parser, &_nla_bitset_parser,
1306 };
1307 
1308 #endif
1309