xref: /freebsd/sys/netlink/netlink_snl.h (revision 51015e6d0f570239b0c2088dc6cf2b018928375d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 *
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
93 lb_free(struct linear_buffer *lb)
94 {
95 	free(lb);
96 }
97 
98 static inline char *
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
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 *
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
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
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
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
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 uint32_t
262 snl_get_seq(struct snl_state *ss)
263 {
264 	return (++ss->seq);
265 }
266 
267 static inline struct nlmsghdr *
268 snl_read_message(struct snl_state *ss)
269 {
270 	if (ss->off == ss->datalen) {
271 		struct sockaddr_nl nladdr;
272 		struct iovec iov = {
273 			.iov_base = ss->buf,
274 			.iov_len = ss->bufsize,
275 		};
276 		struct msghdr msg = {
277 			.msg_name = &nladdr,
278 			.msg_namelen = sizeof(nladdr),
279 			.msg_iov = &iov,
280 			.msg_iovlen = 1,
281 		};
282 		ss->off = 0;
283 		ss->datalen = 0;
284 		for (;;) {
285 			ssize_t datalen = recvmsg(ss->fd, &msg, 0);
286 			if (datalen > 0) {
287 				ss->datalen = datalen;
288 				break;
289 			} else if (errno != EINTR)
290 				return (NULL);
291 		}
292 	}
293 	struct nlmsghdr *hdr = (struct nlmsghdr *)(void *)&ss->buf[ss->off];
294 	ss->off += NLMSG_ALIGN(hdr->nlmsg_len);
295 	return (hdr);
296 }
297 
298 static inline struct nlmsghdr *
299 snl_read_reply(struct snl_state *ss, uint32_t nlmsg_seq)
300 {
301 	while (true) {
302 		struct nlmsghdr *hdr = snl_read_message(ss);
303 		if (hdr == NULL)
304 			break;
305 		if (hdr->nlmsg_seq == nlmsg_seq)
306 			return (hdr);
307 	}
308 
309 	return (NULL);
310 }
311 
312 static inline struct nlmsghdr *
313 snl_get_reply(struct snl_state *ss, struct nlmsghdr *hdr)
314 {
315 	uint32_t nlmsg_seq = hdr->nlmsg_seq;
316 
317 	if (snl_send(ss, hdr, hdr->nlmsg_len))
318 		return (snl_read_reply(ss, nlmsg_seq));
319 	return (NULL);
320 }
321 
322 /*
323  * Checks that attributes are sorted by attribute type.
324  */
325 static inline void
326 snl_verify_parsers(const struct snl_hdr_parser **parser, int count)
327 {
328 	for (int i = 0; i < count; i++) {
329 		const struct snl_hdr_parser *p = parser[i];
330 		int attr_type = 0;
331 		for (int j = 0; j < p->np_size; j++) {
332 			assert(p->np[j].type > attr_type);
333 			attr_type = p->np[j].type;
334 		}
335 	}
336 }
337 #define	SNL_VERIFY_PARSERS(_p)	snl_verify_parsers((_p), NL_ARRAY_LEN(_p))
338 
339 static const struct snl_attr_parser *
340 find_parser(const struct snl_attr_parser *ps, int pslen, int key)
341 {
342 	int left_i = 0, right_i = pslen - 1;
343 
344 	if (key < ps[0].type || key > ps[pslen - 1].type)
345 		return (NULL);
346 
347 	while (left_i + 1 < right_i) {
348 		int mid_i = (left_i + right_i) / 2;
349 		if (key < ps[mid_i].type)
350 			right_i = mid_i;
351 		else if (key > ps[mid_i].type)
352 			left_i = mid_i + 1;
353 		else
354 			return (&ps[mid_i]);
355 	}
356 	if (ps[left_i].type == key)
357 		return (&ps[left_i]);
358 	else if (ps[right_i].type == key)
359 		return (&ps[right_i]);
360 	return (NULL);
361 }
362 
363 static inline bool
364 snl_parse_attrs_raw(struct snl_state *ss, struct nlattr *nla_head, int len,
365     const struct snl_attr_parser *ps, int pslen, void *target)
366 {
367 	struct nlattr *nla;
368 
369 	NLA_FOREACH(nla, nla_head, len) {
370 		if (nla->nla_len < sizeof(struct nlattr))
371 			return (false);
372 		int nla_type = nla->nla_type & NLA_TYPE_MASK;
373 		const struct snl_attr_parser *s = find_parser(ps, pslen, nla_type);
374 		if (s != NULL) {
375 			void *ptr = (void *)((char *)target + s->off);
376 			if (!s->cb(ss, nla, s->arg, ptr))
377 				return (false);
378 		}
379 	}
380 	return (true);
381 }
382 
383 static inline bool
384 snl_parse_attrs(struct snl_state *ss, struct nlmsghdr *hdr, int hdrlen,
385     const struct snl_attr_parser *ps, int pslen, void *target)
386 {
387 	int off = NLMSG_HDRLEN + NETLINK_ALIGN(hdrlen);
388 	int len = hdr->nlmsg_len - off;
389 	struct nlattr *nla_head = (struct nlattr *)(void *)((char *)hdr + off);
390 
391 	return (snl_parse_attrs_raw(ss, nla_head, len, ps, pslen, target));
392 }
393 
394 static inline bool
395 snl_parse_header(struct snl_state *ss, void *hdr, int len,
396     const struct snl_hdr_parser *parser, void *target)
397 {
398 	/* Extract fields first (if any) */
399 	for (int i = 0; i < parser->fp_size; i++) {
400 		const struct snl_field_parser *fp = &parser->fp[i];
401 		void *src = (char *)hdr + fp->off_in;
402 		void *dst = (char *)target + fp->off_out;
403 
404 		fp->cb(ss, src, dst);
405 	}
406 
407 	struct nlattr *nla_head = (struct nlattr *)(void *)((char *)hdr + parser->hdr_off);
408 	bool result = snl_parse_attrs_raw(ss, nla_head, len - parser->hdr_off,
409 	    parser->np, parser->np_size, target);
410 
411 	return (result);
412 }
413 
414 static inline bool
415 snl_parse_nlmsg(struct snl_state *ss, struct nlmsghdr *hdr,
416     const struct snl_hdr_parser *parser, void *target)
417 {
418 	return (snl_parse_header(ss, hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser, target));
419 }
420 
421 static inline bool
422 snl_attr_get_flag(struct snl_state *ss __unused, struct nlattr *nla, void *target)
423 {
424 	if (NLA_DATA_LEN(nla) == 0) {
425 		*((uint8_t *)target) = 1;
426 		return (true);
427 	}
428 	return (false);
429 }
430 
431 static inline bool
432 snl_attr_get_uint8(struct snl_state *ss __unused, struct nlattr *nla,
433     const void *arg __unused, void *target)
434 {
435 	if (NLA_DATA_LEN(nla) == sizeof(uint8_t)) {
436 		*((uint8_t *)target) = *((const uint8_t *)NLA_DATA_CONST(nla));
437 		return (true);
438 	}
439 	return (false);
440 }
441 
442 static inline bool
443 snl_attr_get_uint16(struct snl_state *ss __unused, struct nlattr *nla,
444     const void *arg __unused, void *target)
445 {
446 	if (NLA_DATA_LEN(nla) == sizeof(uint16_t)) {
447 		*((uint16_t *)target) = *((const uint16_t *)NLA_DATA_CONST(nla));
448 		return (true);
449 	}
450 	return (false);
451 }
452 
453 static inline bool
454 snl_attr_get_uint32(struct snl_state *ss __unused, struct nlattr *nla,
455     const void *arg __unused, void *target)
456 {
457 	if (NLA_DATA_LEN(nla) == sizeof(uint32_t)) {
458 		*((uint32_t *)target) = *((const uint32_t *)NLA_DATA_CONST(nla));
459 		return (true);
460 	}
461 	return (false);
462 }
463 
464 static inline bool
465 snl_attr_get_uint64(struct snl_state *ss __unused, struct nlattr *nla,
466     const void *arg __unused, void *target)
467 {
468 	if (NLA_DATA_LEN(nla) == sizeof(uint64_t)) {
469 		memcpy(target, NLA_DATA_CONST(nla), sizeof(uint64_t));
470 		return (true);
471 	}
472 	return (false);
473 }
474 
475 static inline bool
476 snl_attr_get_string(struct snl_state *ss __unused, struct nlattr *nla,
477     const void *arg __unused, void *target)
478 {
479 	size_t maxlen = NLA_DATA_LEN(nla);
480 
481 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
482 		*((char **)target) = (char *)NLA_DATA(nla);
483 		return (true);
484 	}
485 	return (false);
486 }
487 
488 static inline bool
489 snl_attr_get_stringn(struct snl_state *ss, struct nlattr *nla,
490     const void *arg __unused, void *target)
491 {
492 	int maxlen = NLA_DATA_LEN(nla);
493 
494 	char *buf = snl_allocz(ss, maxlen + 1);
495 	if (buf == NULL)
496 		return (false);
497 	buf[maxlen] = '\0';
498 	memcpy(buf, NLA_DATA(nla), maxlen);
499 
500 	*((char **)target) = buf;
501 	return (true);
502 }
503 
504 static inline bool
505 snl_attr_get_nested(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
506 {
507 	const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
508 
509 	/* Assumes target points to the beginning of the structure */
510 	return (snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, target));
511 }
512 
513 static inline bool
514 snl_attr_get_nla(struct snl_state *ss __unused, struct nlattr *nla,
515     const void *arg __unused, void *target)
516 {
517 	*((struct nlattr **)target) = nla;
518 	return (true);
519 }
520 
521 static inline bool
522 snl_attr_copy_struct(struct snl_state *ss, struct nlattr *nla,
523     const void *arg __unused, void *target)
524 {
525 	void *ptr = snl_allocz(ss, NLA_DATA_LEN(nla));
526 
527 	if (ptr != NULL) {
528 		memcpy(ptr, NLA_DATA(nla), NLA_DATA_LEN(nla));
529 		*((void **)target) = ptr;
530 		return (true);
531 	}
532 	return (false);
533 }
534 
535 static inline void
536 snl_field_get_uint8(struct snl_state *ss __unused, void *src, void *target)
537 {
538 	*((uint8_t *)target) = *((uint8_t *)src);
539 }
540 
541 static inline void
542 snl_field_get_uint16(struct snl_state *ss __unused, void *src, void *target)
543 {
544 	*((uint16_t *)target) = *((uint16_t *)src);
545 }
546 
547 static inline void
548 snl_field_get_uint32(struct snl_state *ss __unused, void *src, void *target)
549 {
550 	*((uint32_t *)target) = *((uint32_t *)src);
551 }
552 
553 struct snl_errmsg_data {
554 	uint32_t	nlmsg_seq;
555 	int		error;
556 	char		*error_str;
557 	uint32_t	error_offs;
558 	struct nlattr	*cookie;
559 };
560 #define	_IN(_field)	offsetof(struct nlmsgerr, _field)
561 #define	_OUT(_field)	offsetof(struct snl_errmsg_data, _field)
562 static const struct snl_attr_parser nla_p_errmsg[] = {
563 	{ .type = NLMSGERR_ATTR_MSG, .off = _OUT(error_str), .cb = snl_attr_get_string },
564 	{ .type = NLMSGERR_ATTR_OFFS, .off = _OUT(error_offs), .cb = snl_attr_get_uint32 },
565 	{ .type = NLMSGERR_ATTR_COOKIE, .off = _OUT(cookie), .cb = snl_attr_get_nla },
566 };
567 
568 static const struct snl_field_parser nlf_p_errmsg[] = {
569 	{ .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
570 	{ .off_in = _IN(msg.nlmsg_seq), .off_out = _OUT(nlmsg_seq), .cb = snl_field_get_uint32 },
571 };
572 #undef _IN
573 #undef _OUT
574 SNL_DECLARE_PARSER(snl_errmsg_parser, struct nlmsgerr, nlf_p_errmsg, nla_p_errmsg);
575 
576 static inline bool
577 snl_check_return(struct snl_state *ss, struct nlmsghdr *hdr, struct snl_errmsg_data *e)
578 {
579 	if (hdr != NULL && hdr->nlmsg_type == NLMSG_ERROR)
580 		return (snl_parse_nlmsg(ss, hdr, &snl_errmsg_parser, e));
581 	return (false);
582 }
583 
584 /* writer logic */
585 struct snl_writer {
586 	char			*base;
587 	uint32_t		offset;
588 	uint32_t		size;
589 	struct nlmsghdr		*hdr;
590 	struct snl_state	*ss;
591 	bool			error;
592 };
593 
594 static inline void
595 snl_init_writer(struct snl_state *ss, struct snl_writer *nw)
596 {
597 	nw->size = SNL_WRITER_BUFFER_SIZE;
598 	nw->base = snl_allocz(ss, nw->size);
599 	if (nw->base == NULL) {
600 		nw->error = true;
601 		nw->size = 0;
602 	}
603 
604 	nw->offset = 0;
605 	nw->hdr = NULL;
606 	nw->error = false;
607 	nw->ss = ss;
608 }
609 
610 static inline bool
611 snl_realloc_msg_buffer(struct snl_writer *nw, size_t sz)
612 {
613 	uint32_t new_size = nw->size * 2;
614 
615 	while (new_size < nw->size + sz)
616 		new_size *= 2;
617 
618 	if (nw->error)
619 		return (false);
620 
621 	void *new_base = snl_allocz(nw->ss, new_size);
622 	if (new_base == NULL) {
623 		nw->error = true;
624 		return (false);
625 	}
626 
627 	memcpy(new_base, nw->base, nw->offset);
628 	if (nw->hdr != NULL) {
629 		int hdr_off = (char *)(nw->hdr) - nw->base;
630 		nw->hdr = (struct nlmsghdr *)(void *)((char *)new_base + hdr_off);
631 	}
632 	nw->base = new_base;
633 
634 	return (true);
635 }
636 
637 static inline void *
638 snl_reserve_msg_data_raw(struct snl_writer *nw, size_t sz)
639 {
640 	sz = NETLINK_ALIGN(sz);
641 
642         if (__predict_false(nw->offset + sz > nw->size)) {
643 		if (!snl_realloc_msg_buffer(nw, sz))
644 			return (NULL);
645         }
646 
647         void *data_ptr = &nw->base[nw->offset];
648         nw->offset += sz;
649 
650         return (data_ptr);
651 }
652 #define snl_reserve_msg_object(_ns, _t)	((_t *)snl_reserve_msg_data_raw(_ns, sizeof(_t)))
653 #define snl_reserve_msg_data(_ns, _sz, _t)	((_t *)snl_reserve_msg_data_raw(_ns, _sz))
654 
655 static inline void *
656 _snl_reserve_msg_attr(struct snl_writer *nw, uint16_t nla_type, uint16_t sz)
657 {
658 	sz += sizeof(struct nlattr);
659 
660 	struct nlattr *nla = snl_reserve_msg_data(nw, sz, struct nlattr);
661 	if (__predict_false(nla == NULL))
662 		return (NULL);
663 	nla->nla_type = nla_type;
664 	nla->nla_len = sz;
665 
666 	return ((void *)(nla + 1));
667 }
668 #define	snl_reserve_msg_attr(_ns, _at, _t)	((_t *)_snl_reserve_msg_attr(_ns, _at, sizeof(_t)))
669 
670 static inline bool
671 snl_add_msg_attr(struct snl_writer *nw, int attr_type, int attr_len, const void *data)
672 {
673 	int required_len = NLA_ALIGN(attr_len + sizeof(struct nlattr));
674 
675         if (__predict_false(nw->offset + required_len > nw->size)) {
676 		if (!snl_realloc_msg_buffer(nw, required_len))
677 			return (false);
678 	}
679 
680         struct nlattr *nla = (struct nlattr *)(void *)(&nw->base[nw->offset]);
681 
682         nla->nla_len = attr_len + sizeof(struct nlattr);
683         nla->nla_type = attr_type;
684         if (attr_len > 0) {
685 		if ((attr_len % 4) != 0) {
686 			/* clear padding bytes */
687 			bzero((char *)nla + required_len - 4, 4);
688 		}
689                 memcpy((nla + 1), data, attr_len);
690 	}
691         nw->offset += required_len;
692         return (true);
693 }
694 
695 static inline bool
696 snl_add_msg_attr_raw(struct snl_writer *nw, const struct nlattr *nla_src)
697 {
698 	int attr_len = nla_src->nla_len - sizeof(struct nlattr);
699 
700 	assert(attr_len >= 0);
701 
702 	return (snl_add_msg_attr(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1)));
703 }
704 
705 static inline bool
706 snl_add_msg_attr_u8(struct snl_writer *nw, int attrtype, uint8_t value)
707 {
708 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint8_t), &value));
709 }
710 
711 static inline bool
712 snl_add_msg_attr_u16(struct snl_writer *nw, int attrtype, uint16_t value)
713 {
714 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint16_t), &value));
715 }
716 
717 static inline bool
718 snl_add_msg_attr_u32(struct snl_writer *nw, int attrtype, uint32_t value)
719 {
720 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint32_t), &value));
721 }
722 
723 static inline bool
724 snl_add_msg_attr_u64(struct snl_writer *nw, int attrtype, uint64_t value)
725 {
726 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint64_t), &value));
727 }
728 
729 static inline bool
730 snl_add_msg_attr_s8(struct snl_writer *nw, int attrtype, int8_t value)
731 {
732 	return (snl_add_msg_attr(nw, attrtype, sizeof(int8_t), &value));
733 }
734 
735 static inline bool
736 snl_add_msg_attr_s16(struct snl_writer *nw, int attrtype, int16_t value)
737 {
738 	return (snl_add_msg_attr(nw, attrtype, sizeof(int16_t), &value));
739 }
740 
741 static inline bool
742 snl_add_msg_attr_s32(struct snl_writer *nw, int attrtype, int32_t value)
743 {
744 	return (snl_add_msg_attr(nw, attrtype, sizeof(int32_t), &value));
745 }
746 
747 static inline bool
748 snl_add_msg_attr_s64(struct snl_writer *nw, int attrtype, int64_t value)
749 {
750 	return (snl_add_msg_attr(nw, attrtype, sizeof(int64_t), &value));
751 }
752 
753 static inline bool
754 snl_add_msg_attr_flag(struct snl_writer *nw, int attrtype)
755 {
756 	return (snl_add_msg_attr(nw, attrtype, 0, NULL));
757 }
758 
759 static inline bool
760 snl_add_msg_attr_string(struct snl_writer *nw, int attrtype, const char *str)
761 {
762 	return (snl_add_msg_attr(nw, attrtype, strlen(str) + 1, str));
763 }
764 
765 
766 static inline int
767 snl_get_msg_offset(const struct snl_writer *nw)
768 {
769         return (nw->offset - ((char *)nw->hdr - nw->base));
770 }
771 
772 static inline void *
773 _snl_restore_msg_offset(const struct snl_writer *nw, int off)
774 {
775 	return ((void *)((char *)nw->hdr + off));
776 }
777 #define	snl_restore_msg_offset(_ns, _off, _t)	((_t *)_snl_restore_msg_offset(_ns, _off))
778 
779 static inline int
780 snl_add_msg_attr_nested(struct snl_writer *nw, int attrtype)
781 {
782 	int off = snl_get_msg_offset(nw);
783 	struct nlattr *nla = snl_reserve_msg_data(nw, sizeof(struct nlattr), struct nlattr);
784 	if (__predict_false(nla == NULL))
785 		return (0);
786 	nla->nla_type = attrtype;
787 	return (off);
788 }
789 
790 static inline void
791 snl_end_attr_nested(const struct snl_writer *nw, int off)
792 {
793 	if (!nw->error) {
794 		struct nlattr *nla = snl_restore_msg_offset(nw, off, struct nlattr);
795 		nla->nla_len = NETLINK_ALIGN(snl_get_msg_offset(nw) - off);
796 	}
797 }
798 
799 static inline struct nlmsghdr *
800 snl_create_msg_request(struct snl_writer *nw, int nlmsg_type)
801 {
802 	assert(nw->hdr == NULL);
803 
804 	struct nlmsghdr *hdr = snl_reserve_msg_object(nw, struct nlmsghdr);
805 	hdr->nlmsg_type = nlmsg_type;
806 	hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
807 	nw->hdr = hdr;
808 
809 	return (hdr);
810 }
811 
812 static void
813 snl_abort_msg(struct snl_writer *nw)
814 {
815 	if (nw->hdr != NULL) {
816 		int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
817 
818 		nw->offset -= offset;
819 		nw->hdr = NULL;
820 	}
821 }
822 
823 static inline struct nlmsghdr *
824 snl_finalize_msg(struct snl_writer *nw)
825 {
826 	if (nw->error)
827 		snl_abort_msg(nw);
828 	if (nw->hdr != NULL) {
829 		struct nlmsghdr *hdr = nw->hdr;
830 
831 		int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
832 		hdr->nlmsg_len = offset;
833 		hdr->nlmsg_seq = snl_get_seq(nw->ss);
834 		nw->hdr = NULL;
835 
836 		return (hdr);
837 	}
838 	return (NULL);
839 }
840 
841 static inline bool
842 snl_send_msgs(struct snl_writer *nw)
843 {
844 	int offset = nw->offset;
845 
846 	assert(nw->hdr == NULL);
847 	nw->offset = 0;
848 
849 	return (snl_send(nw->ss, nw->base, offset));
850 }
851 
852 #endif
853