xref: /freebsd/sys/netlink/netlink_snl.h (revision ff92493a4f6504c49a6c84ec65053f493ff5d708)
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 = (struct nlattr *)(_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 *
lb_init(uint32_t size)81 lb_init(uint32_t size)
82 {
83 	struct linear_buffer *lb = (struct linear_buffer *)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
lb_free(struct linear_buffer * lb)94 lb_free(struct linear_buffer *lb)
95 {
96 	free(lb);
97 }
98 
99 static inline char *
lb_allocz(struct linear_buffer * lb,int len)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 	char *data = (lb->base + lb->offset);
106 	lb->offset += len;
107 	return (data);
108 }
109 
110 static inline void
lb_clear(struct linear_buffer * lb)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 *
snl_allocz(struct snl_state * ss,int len)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
snl_clear_lb(struct snl_state * ss)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
snl_free(struct snl_state * ss)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
snl_init(struct snl_state * ss,int netlink_family)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 = (char *)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
snl_send(struct snl_state * ss,void * data,int sz)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
snl_send_message(struct snl_state * ss,struct nlmsghdr * hdr)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
snl_get_seq(struct snl_state * ss)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 *
snl_read_message_dbg(struct snl_state * ss,struct snl_msg_info * cinfo)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 *
snl_read_message(struct snl_state * ss)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 *
snl_read_reply(struct snl_state * ss,uint32_t nlmsg_seq)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
snl_verify_parsers(const struct snl_hdr_parser ** parser,int count)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 *
find_parser(const struct snl_attr_parser * ps,int pslen,int key)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
snl_parse_attrs_raw(struct snl_state * ss,struct nlattr * nla_head,int len,const struct snl_attr_parser * ps,int pslen,void * target)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
snl_parse_attrs(struct snl_state * ss,struct nlmsghdr * hdr,int hdrlen,const struct snl_attr_parser * ps,int pslen,void * target)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
snl_parse_fields(struct snl_state * ss,struct nlmsghdr * hdr,int hdrlen __unused,const struct snl_field_parser * ps,int pslen,void * target)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
snl_parse_header(struct snl_state * ss,void * hdr,int len,const struct snl_hdr_parser * parser,void * target)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, (struct nlmsghdr *)hdr, parser->in_hdr_size,
499 	    parser->fp, parser->fp_size, target);
500 
501 	nla_head = (struct nlattr *)(void *)((char *)hdr + parser->in_hdr_size);
502 	bool result = snl_parse_attrs_raw(ss, nla_head, len - parser->in_hdr_size,
503 	    parser->np, parser->np_size, target);
504 
505 	if (result && parser->cb_post != NULL)
506 		result = parser->cb_post(ss, target);
507 
508 	return (result);
509 }
510 
511 static inline bool
snl_parse_nlmsg(struct snl_state * ss,struct nlmsghdr * hdr,const struct snl_hdr_parser * parser,void * target)512 snl_parse_nlmsg(struct snl_state *ss, struct nlmsghdr *hdr,
513     const struct snl_hdr_parser *parser, void *target)
514 {
515 	return (snl_parse_header(ss, hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser, target));
516 }
517 
518 static inline bool
snl_attr_get_flag(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)519 snl_attr_get_flag(struct snl_state *ss __unused, struct nlattr *nla, const void *arg __unused,
520     void *target)
521 {
522 	if (NLA_DATA_LEN(nla) == 0) {
523 		*((uint8_t *)target) = 1;
524 		return (true);
525 	}
526 	return (false);
527 }
528 
529 static inline bool
snl_attr_get_bytes(struct snl_state * ss __unused,struct nlattr * nla,const void * arg,void * target)530 snl_attr_get_bytes(struct snl_state *ss __unused, struct nlattr *nla, const void *arg,
531     void *target)
532 {
533 	if ((size_t)NLA_DATA_LEN(nla) != (size_t)arg)
534 		return (false);
535 
536 	memcpy(target, NLA_DATA_CONST(nla), (size_t)arg);
537 
538 	return (true);
539 }
540 
541 static inline bool
snl_attr_get_bool(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)542 snl_attr_get_bool(struct snl_state *ss __unused, struct nlattr *nla,
543     const void *arg __unused, void *target)
544 {
545 	if (NLA_DATA_LEN(nla) == sizeof(bool)) {
546 		*((bool *)target) = *((const bool *)NLA_DATA_CONST(nla));
547 		return (true);
548 	}
549 	return (false);
550 }
551 
552 static inline bool
snl_attr_get_uint8(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)553 snl_attr_get_uint8(struct snl_state *ss __unused, struct nlattr *nla,
554     const void *arg __unused, void *target)
555 {
556 	if (NLA_DATA_LEN(nla) == sizeof(uint8_t)) {
557 		*((uint8_t *)target) = *((const uint8_t *)NLA_DATA_CONST(nla));
558 		return (true);
559 	}
560 	return (false);
561 }
562 
563 static inline bool
snl_attr_get_uint16(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)564 snl_attr_get_uint16(struct snl_state *ss __unused, struct nlattr *nla,
565     const void *arg __unused, void *target)
566 {
567 	if (NLA_DATA_LEN(nla) == sizeof(uint16_t)) {
568 		*((uint16_t *)target) = *((const uint16_t *)NLA_DATA_CONST(nla));
569 		return (true);
570 	}
571 	return (false);
572 }
573 
574 static inline bool
snl_attr_get_uint32(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)575 snl_attr_get_uint32(struct snl_state *ss __unused, struct nlattr *nla,
576     const void *arg __unused, void *target)
577 {
578 	if (NLA_DATA_LEN(nla) == sizeof(uint32_t)) {
579 		*((uint32_t *)target) = *((const uint32_t *)NLA_DATA_CONST(nla));
580 		return (true);
581 	}
582 	return (false);
583 }
584 
585 static inline bool
snl_attr_get_uint64(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)586 snl_attr_get_uint64(struct snl_state *ss __unused, struct nlattr *nla,
587     const void *arg __unused, void *target)
588 {
589 	if (NLA_DATA_LEN(nla) == sizeof(uint64_t)) {
590 		memcpy(target, NLA_DATA_CONST(nla), sizeof(uint64_t));
591 		return (true);
592 	}
593 	return (false);
594 }
595 
596 static inline bool
snl_attr_get_int8(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)597 snl_attr_get_int8(struct snl_state *ss, struct nlattr *nla, const void *arg,
598     void *target)
599 {
600 	return (snl_attr_get_uint8(ss, nla, arg, target));
601 }
602 
603 static inline bool
snl_attr_get_int16(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)604 snl_attr_get_int16(struct snl_state *ss, struct nlattr *nla, const void *arg,
605     void *target)
606 {
607 	return (snl_attr_get_uint16(ss, nla, arg, target));
608 }
609 
610 static inline bool
snl_attr_get_int32(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)611 snl_attr_get_int32(struct snl_state *ss, struct nlattr *nla, const void *arg,
612     void *target)
613 {
614 	return (snl_attr_get_uint32(ss, nla, arg, target));
615 }
616 
617 static inline bool
snl_attr_get_int64(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)618 snl_attr_get_int64(struct snl_state *ss, struct nlattr *nla, const void *arg,
619     void *target)
620 {
621 	return (snl_attr_get_uint64(ss, nla, arg, target));
622 }
623 
624 static inline bool
snl_attr_get_string(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)625 snl_attr_get_string(struct snl_state *ss __unused, struct nlattr *nla,
626     const void *arg __unused, void *target)
627 {
628 	size_t maxlen = NLA_DATA_LEN(nla);
629 
630 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
631 		*((char **)target) = (char *)NLA_DATA(nla);
632 		return (true);
633 	}
634 	return (false);
635 }
636 
637 static inline bool
snl_attr_get_stringn(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)638 snl_attr_get_stringn(struct snl_state *ss, struct nlattr *nla,
639     const void *arg __unused, void *target)
640 {
641 	int maxlen = NLA_DATA_LEN(nla);
642 
643 	char *buf = (char *)snl_allocz(ss, maxlen + 1);
644 	if (buf == NULL)
645 		return (false);
646 	buf[maxlen] = '\0';
647 	memcpy(buf, NLA_DATA(nla), maxlen);
648 
649 	*((char **)target) = buf;
650 	return (true);
651 }
652 
653 static inline bool
snl_attr_copy_string(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)654 snl_attr_copy_string(struct snl_state *ss, struct nlattr *nla,
655     const void *arg, void *target)
656 {
657 	char *tmp;
658 
659 	if (snl_attr_get_string(ss, nla, NULL, &tmp)) {
660 		strlcpy((char *)target, tmp, (size_t)arg);
661 		return (true);
662 	}
663 	return (false);
664 }
665 
666 static inline bool
snl_attr_dup_string(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)667 snl_attr_dup_string(struct snl_state *ss __unused, struct nlattr *nla,
668     const void *arg __unused, void *target)
669 {
670 	size_t maxlen = NLA_DATA_LEN(nla);
671 
672 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
673 		char *buf = (char *)snl_allocz(ss, maxlen);
674 		if (buf == NULL)
675 			return (false);
676 		memcpy(buf, NLA_DATA(nla), maxlen);
677 		*((char **)target) = buf;
678 		return (true);
679 	}
680 	return (false);
681 }
682 
683 static inline bool
snl_attr_get_nested(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)684 snl_attr_get_nested(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
685 {
686 	const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
687 
688 	/* Assumes target points to the beginning of the structure */
689 	return (snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, target));
690 }
691 
692 struct snl_parray {
693 	uint32_t count;
694 	void **items;
695 };
696 
697 static inline bool
snl_attr_get_parray_sz(struct snl_state * ss,struct nlattr * container_nla,uint32_t start_size,const void * arg,void * target)698 snl_attr_get_parray_sz(struct snl_state *ss, struct nlattr *container_nla,
699     uint32_t start_size, const void *arg, void *target)
700 {
701 	const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
702 	struct snl_parray *array = (struct snl_parray *)target;
703 	struct nlattr *nla;
704 	uint32_t count = 0, size = start_size;
705 
706 	if (p->out_size == 0)
707 		return (false);
708 
709 	array->items = (void **)snl_allocz(ss, size * sizeof(void *));
710 	if (array->items == NULL)
711 		return (false);
712 
713 	/*
714 	 * If the provided parser is an attribute parser, assume that each
715 	 *  nla in the container nla is the container nla itself and parse
716 	 *  the contents of this nla.
717 	 * Otherwise, run the parser on raw data, assuming the header of this
718 	 * data has u16 field with total size in the beginning.
719 	 */
720 	uint32_t data_off = 0;
721 
722 	if (p->in_hdr_size == 0)
723 		data_off = sizeof(struct nlattr);
724 
725 	NLA_FOREACH(nla, NLA_DATA(container_nla), NLA_DATA_LEN(container_nla)) {
726 		void *item = snl_allocz(ss, p->out_size);
727 
728 		if (item == NULL)
729 			return (false);
730 
731 		void *data = (char *)(void *)nla + data_off;
732 		int data_len = nla->nla_len - data_off;
733 
734 		if (!(snl_parse_header(ss, data, data_len, p, item)))
735 			return (false);
736 
737 		if (count == size) {
738 			uint32_t new_size = size * 2;
739 			void **new_array = (void **)snl_allocz(ss, new_size *sizeof(void *));
740 
741 			memcpy(new_array, array->items, size * sizeof(void *));
742 			array->items = new_array;
743 			size = new_size;
744 		}
745 		array->items[count++] = item;
746 	}
747 	array->count = count;
748 
749 	return (true);
750 }
751 
752 /*
753  * Parses and stores the unknown-size array.
754  * Assumes each array item is a container and the NLAs in the container are parsable
755  *  by the parser provided in @arg.
756  * Assumes @target is struct snl_parray
757  */
758 static inline bool
snl_attr_get_parray(struct snl_state * ss,struct nlattr * nla,const void * arg,void * target)759 snl_attr_get_parray(struct snl_state *ss, struct nlattr *nla, const void *arg, void *target)
760 {
761 	return (snl_attr_get_parray_sz(ss, nla, 8, arg, target));
762 }
763 
764 static inline bool
snl_attr_get_nla(struct snl_state * ss __unused,struct nlattr * nla,const void * arg __unused,void * target)765 snl_attr_get_nla(struct snl_state *ss __unused, struct nlattr *nla,
766     const void *arg __unused, void *target)
767 {
768 	*((struct nlattr **)target) = nla;
769 	return (true);
770 }
771 
772 static inline bool
snl_attr_dup_nla(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)773 snl_attr_dup_nla(struct snl_state *ss, struct nlattr *nla,
774     const void *arg __unused, void *target)
775 {
776 	void *ptr = snl_allocz(ss, nla->nla_len);
777 
778 	if (ptr != NULL) {
779 		memcpy(ptr, nla, nla->nla_len);
780 		*((void **)target) = ptr;
781 		return (true);
782 	}
783 	return (false);
784 }
785 
786 static inline bool
snl_attr_copy_struct(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)787 snl_attr_copy_struct(struct snl_state *ss, struct nlattr *nla,
788     const void *arg __unused, void *target)
789 {
790 	void *ptr = snl_allocz(ss, NLA_DATA_LEN(nla));
791 
792 	if (ptr != NULL) {
793 		memcpy(ptr, NLA_DATA(nla), NLA_DATA_LEN(nla));
794 		*((void **)target) = ptr;
795 		return (true);
796 	}
797 	return (false);
798 }
799 
800 static inline bool
snl_attr_dup_struct(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * target)801 snl_attr_dup_struct(struct snl_state *ss, struct nlattr *nla,
802     const void *arg __unused, void *target)
803 {
804 	void *ptr = snl_allocz(ss, NLA_DATA_LEN(nla));
805 
806 	if (ptr != NULL) {
807 		memcpy(ptr, NLA_DATA(nla), NLA_DATA_LEN(nla));
808 		*((void **)target) = ptr;
809 		return (true);
810 	}
811 	return (false);
812 }
813 
814 struct snl_attr_bit {
815 	uint32_t	bit_index;
816 	char		*bit_name;
817 	int		bit_value;
818 };
819 
820 struct snl_attr_bits {
821 	uint32_t num_bits;
822 	struct snl_attr_bit **bits;
823 };
824 
825 #define	_OUT(_field)	offsetof(struct snl_attr_bit, _field)
826 static const struct snl_attr_parser _nla_p_bit[] = {
827 	{ .type = NLA_BITSET_BIT_INDEX, .off = _OUT(bit_index), .cb = snl_attr_get_uint32 },
828 	{ .type = NLA_BITSET_BIT_NAME, .off = _OUT(bit_name), .cb = snl_attr_dup_string },
829 	{ .type = NLA_BITSET_BIT_VALUE, .off = _OUT(bit_value), .cb = snl_attr_get_flag },
830 };
831 #undef _OUT
832 SNL_DECLARE_ATTR_PARSER_EXT(_nla_bit_parser, sizeof(struct snl_attr_bit), _nla_p_bit, NULL);
833 
834 struct snl_attr_bitset {
835 	uint32_t		nla_bitset_size;
836 	uint32_t		*nla_bitset_mask;
837 	uint32_t		*nla_bitset_value;
838 	struct snl_attr_bits	bits;
839 };
840 
841 #define	_OUT(_field)	offsetof(struct snl_attr_bitset, _field)
842 static const struct snl_attr_parser _nla_p_bitset[] = {
843 	{ .type = NLA_BITSET_SIZE, .off = _OUT(nla_bitset_size), .cb = snl_attr_get_uint32 },
844 	{ .type = NLA_BITSET_BITS, .off = _OUT(bits), .cb = snl_attr_get_parray, .arg = &_nla_bit_parser },
845 	{ .type = NLA_BITSET_VALUE, .off = _OUT(nla_bitset_mask), .cb = snl_attr_dup_nla },
846 	{ .type = NLA_BITSET_MASK, .off = _OUT(nla_bitset_value), .cb = snl_attr_dup_nla },
847 };
848 
849 static inline bool
_cb_p_bitset(struct snl_state * ss __unused,void * _target)850 _cb_p_bitset(struct snl_state *ss __unused, void *_target)
851 {
852 	struct snl_attr_bitset *target = (struct snl_attr_bitset *)_target;
853 
854 	uint32_t sz_bytes = _roundup2(target->nla_bitset_size, 32) / 8;
855 
856 	if (target->nla_bitset_mask != NULL) {
857 		struct nlattr *nla = (struct nlattr *)target->nla_bitset_mask;
858 		uint32_t data_len = NLA_DATA_LEN(nla);
859 
860 		if (data_len != sz_bytes || _roundup2(data_len, 4) != data_len)
861 			return (false);
862 		target->nla_bitset_mask = (uint32_t *)NLA_DATA(nla);
863 	}
864 
865 	if (target->nla_bitset_value != NULL) {
866 		struct nlattr *nla = (struct nlattr *)target->nla_bitset_value;
867 		uint32_t data_len = NLA_DATA_LEN(nla);
868 
869 		if (data_len != sz_bytes || _roundup2(data_len, 4) != data_len)
870 			return (false);
871 		target->nla_bitset_value = (uint32_t *)NLA_DATA(nla);
872 	}
873 	return (true);
874 }
875 #undef _OUT
876 SNL_DECLARE_ATTR_PARSER_EXT(_nla_bitset_parser,
877 		sizeof(struct snl_attr_bitset),
878 		_nla_p_bitset, _cb_p_bitset);
879 
880 /*
881  * Parses the compact bitset representation.
882  */
883 static inline bool
snl_attr_get_bitset_c(struct snl_state * ss,struct nlattr * nla,const void * arg __unused,void * _target)884 snl_attr_get_bitset_c(struct snl_state *ss, struct nlattr *nla,
885     const void *arg __unused, void *_target)
886 {
887 	const struct snl_hdr_parser *p = &_nla_bitset_parser;
888 	struct snl_attr_bitset *target = (struct snl_attr_bitset *)_target;
889 
890 	/* Assumes target points to the beginning of the structure */
891 	if (!snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, _target))
892 		return (false);
893 	if (target->nla_bitset_mask == NULL || target->nla_bitset_value == NULL)
894 		return (false);
895 	return (true);
896 }
897 
898 static inline void
snl_field_get_uint8(struct snl_state * ss __unused,void * src,void * target)899 snl_field_get_uint8(struct snl_state *ss __unused, void *src, void *target)
900 {
901 	*((uint8_t *)target) = *((uint8_t *)src);
902 }
903 
904 static inline void
snl_field_get_uint16(struct snl_state * ss __unused,void * src,void * target)905 snl_field_get_uint16(struct snl_state *ss __unused, void *src, void *target)
906 {
907 	*((uint16_t *)target) = *((uint16_t *)src);
908 }
909 
910 static inline void
snl_field_get_uint32(struct snl_state * ss __unused,void * src,void * target)911 snl_field_get_uint32(struct snl_state *ss __unused, void *src, void *target)
912 {
913 	*((uint32_t *)target) = *((uint32_t *)src);
914 }
915 
916 static inline void
snl_field_get_ptr(struct snl_state * ss __unused,void * src,void * target)917 snl_field_get_ptr(struct snl_state *ss __unused, void *src, void *target)
918 {
919 	*((void **)target) = src;
920 }
921 
922 struct snl_errmsg_data {
923 	struct nlmsghdr	*orig_hdr;
924 	int		error;
925 	uint32_t	error_offs;
926 	char		*error_str;
927 	struct nlattr	*cookie;
928 };
929 
930 #define	_IN(_field)	offsetof(struct nlmsgerr, _field)
931 #define	_OUT(_field)	offsetof(struct snl_errmsg_data, _field)
932 static const struct snl_attr_parser nla_p_errmsg[] = {
933 	{ .type = NLMSGERR_ATTR_MSG, .off = _OUT(error_str), .cb = snl_attr_get_string },
934 	{ .type = NLMSGERR_ATTR_OFFS, .off = _OUT(error_offs), .cb = snl_attr_get_uint32 },
935 	{ .type = NLMSGERR_ATTR_COOKIE, .off = _OUT(cookie), .cb = snl_attr_get_nla },
936 };
937 
938 static const struct snl_field_parser nlf_p_errmsg[] = {
939 	{ .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
940 	{ .off_in = _IN(msg), .off_out = _OUT(orig_hdr), .cb = snl_field_get_ptr },
941 };
942 #undef _IN
943 #undef _OUT
944 SNL_DECLARE_PARSER(snl_errmsg_parser, struct nlmsgerr, nlf_p_errmsg, nla_p_errmsg);
945 
946 #define	_IN(_field)	offsetof(struct nlmsgerr, _field)
947 #define	_OUT(_field)	offsetof(struct snl_errmsg_data, _field)
948 static const struct snl_field_parser nlf_p_donemsg[] = {
949 	{ .off_in = _IN(error), .off_out = _OUT(error), .cb = snl_field_get_uint32 },
950 };
951 #undef _IN
952 #undef _OUT
953 SNL_DECLARE_FIELD_PARSER(snl_donemsg_parser, struct nlmsgerr, nlf_p_donemsg);
954 
955 static inline bool
snl_parse_errmsg(struct snl_state * ss,struct nlmsghdr * hdr,struct snl_errmsg_data * e)956 snl_parse_errmsg(struct snl_state *ss, struct nlmsghdr *hdr, struct snl_errmsg_data *e)
957 {
958 	if ((hdr->nlmsg_flags & NLM_F_CAPPED) != 0)
959 		return (snl_parse_nlmsg(ss, hdr, &snl_errmsg_parser, e));
960 
961 	const struct snl_hdr_parser *ps = &snl_errmsg_parser;
962 	struct nlmsgerr *errmsg = (struct nlmsgerr *)(hdr + 1);
963 	int hdrlen = sizeof(int) + NLMSG_ALIGN(errmsg->msg.nlmsg_len);
964 	struct nlattr *attr_head = (struct nlattr *)(void *)((char *)errmsg + hdrlen);
965 	int attr_len = hdr->nlmsg_len - sizeof(struct nlmsghdr) - hdrlen;
966 
967 	snl_parse_fields(ss, (struct nlmsghdr *)errmsg, hdrlen, ps->fp, ps->fp_size, e);
968 	return (snl_parse_attrs_raw(ss, attr_head, attr_len, ps->np, ps->np_size, e));
969 }
970 
971 static inline bool
snl_read_reply_code(struct snl_state * ss,uint32_t nlmsg_seq,struct snl_errmsg_data * e)972 snl_read_reply_code(struct snl_state *ss, uint32_t nlmsg_seq, struct snl_errmsg_data *e)
973 {
974 	struct nlmsghdr *hdr = snl_read_reply(ss, nlmsg_seq);
975 
976 	if (hdr == NULL) {
977 		e->error = EINVAL;
978 	} else if (hdr->nlmsg_type == NLMSG_ERROR) {
979 		if (!snl_parse_errmsg(ss, hdr, e))
980 			e->error = EINVAL;
981 		return (e->error == 0);
982 	}
983 
984 	return (false);
985 }
986 
987 #define	_OUT(_field)	offsetof(struct snl_msg_info, _field)
988 static const struct snl_attr_parser _nla_p_cinfo[] = {
989 	{ .type = NLMSGINFO_ATTR_PROCESS_ID, .off = _OUT(process_id), .cb = snl_attr_get_uint32 },
990 	{ .type = NLMSGINFO_ATTR_PORT_ID, .off = _OUT(port_id), .cb = snl_attr_get_uint32 },
991 	{ .type = NLMSGINFO_ATTR_SEQ_ID, .off = _OUT(seq_id), .cb = snl_attr_get_uint32 },
992 };
993 #undef _OUT
994 SNL_DECLARE_ATTR_PARSER(snl_msg_info_parser, _nla_p_cinfo);
995 
996 static inline bool
parse_cmsg(struct snl_state * ss,const struct msghdr * msg,struct snl_msg_info * attrs)997 parse_cmsg(struct snl_state *ss, const struct msghdr *msg, struct snl_msg_info *attrs)
998 {
999 	for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
1000 	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
1001 		if (cmsg->cmsg_level != SOL_NETLINK || cmsg->cmsg_type != NETLINK_MSG_INFO)
1002 			continue;
1003 
1004 		void *data = CMSG_DATA(cmsg);
1005 		int len = cmsg->cmsg_len - ((char *)data - (char *)cmsg);
1006 		const struct snl_hdr_parser *ps = &snl_msg_info_parser;
1007 
1008 		return (snl_parse_attrs_raw(ss, (struct nlattr *)data, len, ps->np, ps->np_size, attrs));
1009 	}
1010 
1011 	return (false);
1012 }
1013 
1014 /*
1015  * Assumes e is zeroed
1016  */
1017 static inline struct nlmsghdr *
snl_read_reply_multi(struct snl_state * ss,uint32_t nlmsg_seq,struct snl_errmsg_data * e)1018 snl_read_reply_multi(struct snl_state *ss, uint32_t nlmsg_seq, struct snl_errmsg_data *e)
1019 {
1020 	struct nlmsghdr *hdr = snl_read_reply(ss, nlmsg_seq);
1021 
1022 	if (hdr == NULL) {
1023 		e->error = EINVAL;
1024 	} else if (hdr->nlmsg_type == NLMSG_ERROR) {
1025 		if (!snl_parse_errmsg(ss, hdr, e))
1026 			e->error = EINVAL;
1027 	} else if (hdr->nlmsg_type == NLMSG_DONE) {
1028 		snl_parse_nlmsg(ss, hdr, &snl_donemsg_parser, e);
1029 	} else
1030 		return (hdr);
1031 
1032 	return (NULL);
1033 }
1034 
1035 
1036 /* writer logic */
1037 struct snl_writer {
1038 	char			*base;
1039 	uint32_t		offset;
1040 	uint32_t		size;
1041 	struct nlmsghdr		*hdr;
1042 	struct snl_state	*ss;
1043 	bool			error;
1044 };
1045 
1046 static inline void
snl_init_writer(struct snl_state * ss,struct snl_writer * nw)1047 snl_init_writer(struct snl_state *ss, struct snl_writer *nw)
1048 {
1049 	nw->size = SNL_WRITER_BUFFER_SIZE;
1050 	nw->base = (char *)snl_allocz(ss, nw->size);
1051 	if (nw->base == NULL) {
1052 		nw->error = true;
1053 		nw->size = 0;
1054 	}
1055 
1056 	nw->offset = 0;
1057 	nw->hdr = NULL;
1058 	nw->error = false;
1059 	nw->ss = ss;
1060 }
1061 
1062 static inline bool
snl_realloc_msg_buffer(struct snl_writer * nw,size_t sz)1063 snl_realloc_msg_buffer(struct snl_writer *nw, size_t sz)
1064 {
1065 	uint32_t new_size = nw->size * 2;
1066 
1067 	while (new_size < nw->size + sz)
1068 		new_size *= 2;
1069 
1070 	if (nw->error)
1071 		return (false);
1072 
1073 	if (snl_allocz(nw->ss, new_size) == NULL) {
1074 		nw->error = true;
1075 		return (false);
1076 	}
1077 	nw->size = new_size;
1078 
1079 	void *new_base = nw->ss->lb->base;
1080 	if (new_base != nw->base) {
1081 		memcpy(new_base, nw->base, nw->offset);
1082 		if (nw->hdr != NULL) {
1083 			int hdr_off = (char *)(nw->hdr) - nw->base;
1084 
1085 			nw->hdr = (struct nlmsghdr *)
1086 			    (void *)((char *)new_base + hdr_off);
1087 		}
1088 		nw->base = (char *)new_base;
1089 	}
1090 
1091 	return (true);
1092 }
1093 
1094 static inline void *
snl_reserve_msg_data_raw(struct snl_writer * nw,size_t sz)1095 snl_reserve_msg_data_raw(struct snl_writer *nw, size_t sz)
1096 {
1097 	sz = NETLINK_ALIGN(sz);
1098 
1099         if (__predict_false(nw->offset + sz > nw->size)) {
1100 		if (!snl_realloc_msg_buffer(nw, sz))
1101 			return (NULL);
1102         }
1103 
1104         void *data_ptr = &nw->base[nw->offset];
1105         nw->offset += sz;
1106 
1107         return (data_ptr);
1108 }
1109 #define snl_reserve_msg_object(_ns, _t)	((_t *)snl_reserve_msg_data_raw(_ns, sizeof(_t)))
1110 #define snl_reserve_msg_data(_ns, _sz, _t)	((_t *)snl_reserve_msg_data_raw(_ns, _sz))
1111 
1112 static inline void *
_snl_reserve_msg_attr(struct snl_writer * nw,uint16_t nla_type,uint16_t sz)1113 _snl_reserve_msg_attr(struct snl_writer *nw, uint16_t nla_type, uint16_t sz)
1114 {
1115 	sz += sizeof(struct nlattr);
1116 
1117 	struct nlattr *nla = snl_reserve_msg_data(nw, sz, struct nlattr);
1118 	if (__predict_false(nla == NULL))
1119 		return (NULL);
1120 	nla->nla_type = nla_type;
1121 	nla->nla_len = sz;
1122 
1123 	return ((void *)(nla + 1));
1124 }
1125 #define	snl_reserve_msg_attr(_ns, _at, _t)	((_t *)_snl_reserve_msg_attr(_ns, _at, sizeof(_t)))
1126 
1127 static inline bool
snl_add_msg_attr(struct snl_writer * nw,int attr_type,int attr_len,const void * data)1128 snl_add_msg_attr(struct snl_writer *nw, int attr_type, int attr_len, const void *data)
1129 {
1130 	int required_len = NLA_ALIGN(attr_len + sizeof(struct nlattr));
1131 
1132         if (__predict_false(nw->offset + required_len > nw->size)) {
1133 		if (!snl_realloc_msg_buffer(nw, required_len))
1134 			return (false);
1135 	}
1136 
1137         struct nlattr *nla = (struct nlattr *)(void *)(&nw->base[nw->offset]);
1138 
1139         nla->nla_len = attr_len + sizeof(struct nlattr);
1140         nla->nla_type = attr_type;
1141         if (attr_len > 0) {
1142 		if ((attr_len % 4) != 0) {
1143 			/* clear padding bytes */
1144 			bzero((char *)nla + required_len - 4, 4);
1145 		}
1146                 memcpy((nla + 1), data, attr_len);
1147 	}
1148         nw->offset += required_len;
1149         return (true);
1150 }
1151 
1152 static inline bool
snl_add_msg_attr_raw(struct snl_writer * nw,const struct nlattr * nla_src)1153 snl_add_msg_attr_raw(struct snl_writer *nw, const struct nlattr *nla_src)
1154 {
1155 	int attr_len = nla_src->nla_len - sizeof(struct nlattr);
1156 
1157 	assert(attr_len >= 0);
1158 
1159 	return (snl_add_msg_attr(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1)));
1160 }
1161 
1162 static inline bool
snl_add_msg_attr_bool(struct snl_writer * nw,int attrtype,bool value)1163 snl_add_msg_attr_bool(struct snl_writer *nw, int attrtype, bool value)
1164 {
1165 	return (snl_add_msg_attr(nw, attrtype, sizeof(bool), &value));
1166 }
1167 
1168 static inline bool
snl_add_msg_attr_u8(struct snl_writer * nw,int attrtype,uint8_t value)1169 snl_add_msg_attr_u8(struct snl_writer *nw, int attrtype, uint8_t value)
1170 {
1171 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint8_t), &value));
1172 }
1173 
1174 static inline bool
snl_add_msg_attr_u16(struct snl_writer * nw,int attrtype,uint16_t value)1175 snl_add_msg_attr_u16(struct snl_writer *nw, int attrtype, uint16_t value)
1176 {
1177 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint16_t), &value));
1178 }
1179 
1180 static inline bool
snl_add_msg_attr_u32(struct snl_writer * nw,int attrtype,uint32_t value)1181 snl_add_msg_attr_u32(struct snl_writer *nw, int attrtype, uint32_t value)
1182 {
1183 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint32_t), &value));
1184 }
1185 
1186 static inline bool
snl_add_msg_attr_u64(struct snl_writer * nw,int attrtype,uint64_t value)1187 snl_add_msg_attr_u64(struct snl_writer *nw, int attrtype, uint64_t value)
1188 {
1189 	return (snl_add_msg_attr(nw, attrtype, sizeof(uint64_t), &value));
1190 }
1191 
1192 static inline bool
snl_add_msg_attr_s8(struct snl_writer * nw,int attrtype,int8_t value)1193 snl_add_msg_attr_s8(struct snl_writer *nw, int attrtype, int8_t value)
1194 {
1195 	return (snl_add_msg_attr(nw, attrtype, sizeof(int8_t), &value));
1196 }
1197 
1198 static inline bool
snl_add_msg_attr_s16(struct snl_writer * nw,int attrtype,int16_t value)1199 snl_add_msg_attr_s16(struct snl_writer *nw, int attrtype, int16_t value)
1200 {
1201 	return (snl_add_msg_attr(nw, attrtype, sizeof(int16_t), &value));
1202 }
1203 
1204 static inline bool
snl_add_msg_attr_s32(struct snl_writer * nw,int attrtype,int32_t value)1205 snl_add_msg_attr_s32(struct snl_writer *nw, int attrtype, int32_t value)
1206 {
1207 	return (snl_add_msg_attr(nw, attrtype, sizeof(int32_t), &value));
1208 }
1209 
1210 static inline bool
snl_add_msg_attr_s64(struct snl_writer * nw,int attrtype,int64_t value)1211 snl_add_msg_attr_s64(struct snl_writer *nw, int attrtype, int64_t value)
1212 {
1213 	return (snl_add_msg_attr(nw, attrtype, sizeof(int64_t), &value));
1214 }
1215 
1216 static inline bool
snl_add_msg_attr_flag(struct snl_writer * nw,int attrtype)1217 snl_add_msg_attr_flag(struct snl_writer *nw, int attrtype)
1218 {
1219 	return (snl_add_msg_attr(nw, attrtype, 0, NULL));
1220 }
1221 
1222 static inline bool
snl_add_msg_attr_string(struct snl_writer * nw,int attrtype,const char * str)1223 snl_add_msg_attr_string(struct snl_writer *nw, int attrtype, const char *str)
1224 {
1225 	return (snl_add_msg_attr(nw, attrtype, strlen(str) + 1, str));
1226 }
1227 
1228 
1229 static inline int
snl_get_msg_offset(const struct snl_writer * nw)1230 snl_get_msg_offset(const struct snl_writer *nw)
1231 {
1232         return (nw->offset - ((char *)nw->hdr - nw->base));
1233 }
1234 
1235 static inline void *
_snl_restore_msg_offset(const struct snl_writer * nw,int off)1236 _snl_restore_msg_offset(const struct snl_writer *nw, int off)
1237 {
1238 	return ((void *)((char *)nw->hdr + off));
1239 }
1240 #define	snl_restore_msg_offset(_ns, _off, _t)	((_t *)_snl_restore_msg_offset(_ns, _off))
1241 
1242 static inline int
snl_add_msg_attr_nested(struct snl_writer * nw,int attrtype)1243 snl_add_msg_attr_nested(struct snl_writer *nw, int attrtype)
1244 {
1245 	int off = snl_get_msg_offset(nw);
1246 	struct nlattr *nla = snl_reserve_msg_data(nw, sizeof(struct nlattr), struct nlattr);
1247 	if (__predict_false(nla == NULL))
1248 		return (0);
1249 	nla->nla_type = attrtype;
1250 	return (off);
1251 }
1252 
1253 static inline void
snl_end_attr_nested(const struct snl_writer * nw,int off)1254 snl_end_attr_nested(const struct snl_writer *nw, int off)
1255 {
1256 	if (!nw->error) {
1257 		struct nlattr *nla = snl_restore_msg_offset(nw, off, struct nlattr);
1258 		nla->nla_len = NETLINK_ALIGN(snl_get_msg_offset(nw) - off);
1259 	}
1260 }
1261 
1262 static inline struct nlmsghdr *
snl_create_msg_request(struct snl_writer * nw,int nlmsg_type)1263 snl_create_msg_request(struct snl_writer *nw, int nlmsg_type)
1264 {
1265 	assert(nw->hdr == NULL);
1266 
1267 	struct nlmsghdr *hdr = snl_reserve_msg_object(nw, struct nlmsghdr);
1268 	hdr->nlmsg_type = nlmsg_type;
1269 	hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1270 	nw->hdr = hdr;
1271 
1272 	return (hdr);
1273 }
1274 
1275 static void
snl_abort_msg(struct snl_writer * nw)1276 snl_abort_msg(struct snl_writer *nw)
1277 {
1278 	if (nw->hdr != NULL) {
1279 		int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
1280 
1281 		nw->offset -= offset;
1282 		nw->hdr = NULL;
1283 	}
1284 }
1285 
1286 static inline struct nlmsghdr *
snl_finalize_msg(struct snl_writer * nw)1287 snl_finalize_msg(struct snl_writer *nw)
1288 {
1289 	if (nw->error)
1290 		snl_abort_msg(nw);
1291 	if (nw->hdr != NULL) {
1292 		struct nlmsghdr *hdr = nw->hdr;
1293 
1294 		int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
1295 		hdr->nlmsg_len = offset;
1296 		hdr->nlmsg_seq = snl_get_seq(nw->ss);
1297 		nw->hdr = NULL;
1298 
1299 		return (hdr);
1300 	}
1301 	return (NULL);
1302 }
1303 
1304 static inline bool
snl_send_msgs(struct snl_writer * nw)1305 snl_send_msgs(struct snl_writer *nw)
1306 {
1307 	int offset = nw->offset;
1308 
1309 	assert(nw->hdr == NULL);
1310 	nw->offset = 0;
1311 
1312 	return (snl_send(nw->ss, nw->base, offset));
1313 }
1314 
1315 static const struct snl_hdr_parser *snl_all_core_parsers[] = {
1316 	&snl_errmsg_parser, &snl_donemsg_parser,
1317 	&_nla_bit_parser, &_nla_bitset_parser,
1318 };
1319 
1320 #endif
1321