libnl 3.9.0
msg.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup core
8 * @defgroup msg Message Construction & Parsing
9 * Netlink Message Construction/Parsing Interface
10 *
11 * Related sections in the development guide:
12 * - @core_doc{_message_parsing_amp_construction,Message Parsing & Construction}
13 *
14 * @{
15 *
16 * Header
17 * ------
18 * ~~~~{.c}
19 * #include <netlink/msg.h>
20 * ~~~~
21 */
22
23#include "nl-default.h"
24
25#include <ctype.h>
26
27#include <linux/socket.h>
28
29#include <netlink/netlink.h>
30#include <netlink/utils.h>
31#include <netlink/cache.h>
32#include <netlink/attr.h>
33
34#include "nl-core.h"
35#include "nl-priv-dynamic-core/nl-core.h"
36#include "nl-priv-dynamic-core/cache-api.h"
37#include "nl-aux-core/nl-core.h"
38
39static size_t default_msg_size;
40
41static void _nl_init init_msg_size(void)
42{
43 default_msg_size = getpagesize();
44}
45
46/**
47 * @name Size Calculations
48 * @{
49 */
50
51/**
52 * Calculates size of netlink message based on payload length.
53 * @arg payload Length of payload
54 *
55 * @return size of netlink message without padding.
56 */
57int nlmsg_size(int payload)
58{
59 return NLMSG_HDRLEN + payload;
60}
61
62static int nlmsg_msg_size(int payload)
63{
64 return nlmsg_size(payload);
65}
66
67/**
68 * Calculates size of netlink message including padding based on payload length
69 * @arg payload Length of payload
70 *
71 * This function is idential to nlmsg_size() + nlmsg_padlen().
72 *
73 * @return Size of netlink message including padding.
74 */
75int nlmsg_total_size(int payload)
76{
77 return NLMSG_ALIGN(nlmsg_msg_size(payload));
78}
79
80/**
81 * Size of padding that needs to be added at end of message
82 * @arg payload Length of payload
83 *
84 * Calculates the number of bytes of padding which is required to be added to
85 * the end of the message to ensure that the next netlink message header begins
86 * properly aligned to NLMSG_ALIGNTO.
87 *
88 * @return Number of bytes of padding needed.
89 */
90int nlmsg_padlen(int payload)
91{
92 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
93}
94
95/** @} */
96
97/**
98 * @name Access to Message Payload
99 * @{
100 */
101
102/**
103 * Return pointer to message payload
104 * @arg nlh Netlink message header
105 *
106 * @return Pointer to start of message payload.
107 */
108void *nlmsg_data(const struct nlmsghdr *nlh)
109{
110 return (unsigned char *) nlh + NLMSG_HDRLEN;
111}
112
113void *nlmsg_tail(const struct nlmsghdr *nlh)
114{
115 return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
116}
117
118/**
119 * Return length of message payload
120 * @arg nlh Netlink message header
121 *
122 * @return Length of message payload in bytes.
123 */
124int nlmsg_datalen(const struct nlmsghdr *nlh)
125{
126 return nlh->nlmsg_len - NLMSG_HDRLEN;
127}
128
129static int nlmsg_len(const struct nlmsghdr *nlh)
130{
131 return nlmsg_datalen(nlh);
132}
133
134/** @} */
135
136/**
137 * @name Attribute Access
138 * @{
139 */
140
141/**
142 * head of attributes data
143 * @arg nlh netlink message header
144 * @arg hdrlen length of family specific header
145 */
146struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
147{
148 unsigned char *data = nlmsg_data(nlh);
149 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
150}
151
152/**
153 * length of attributes data
154 * @arg nlh netlink message header
155 * @arg hdrlen length of family specific header
156 */
157int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
158{
159 return _NL_MAX(nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen), 0u);
160}
161
162/** @} */
163
164/**
165 * @name Message Parsing
166 * @{
167 */
168
169int nlmsg_valid_hdr(const struct nlmsghdr *nlh, int hdrlen)
170{
171 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
172 return 0;
173
174 return 1;
175}
176
177/**
178 * check if the netlink message fits into the remaining bytes
179 * @arg nlh netlink message header
180 * @arg remaining number of bytes remaining in message stream
181 */
182int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
183{
184 return (remaining >= (int)sizeof(struct nlmsghdr) &&
185 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
186 nlh->nlmsg_len <= remaining);
187}
188
189/**
190 * next netlink message in message stream
191 * @arg nlh netlink message header
192 * @arg remaining number of bytes remaining in message stream
193 *
194 * @returns the next netlink message in the message stream and
195 * decrements remaining by the size of the current message.
196 */
197struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
198{
199 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
200
201 *remaining -= totlen;
202
203 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
204}
205
206/**
207 * parse attributes of a netlink message
208 * @arg nlh netlink message header
209 * @arg hdrlen length of family specific header
210 * @arg tb destination array with maxtype+1 elements
211 * @arg maxtype maximum attribute type to be expected
212 * @arg policy validation policy
213 *
214 * See nla_parse()
215 */
216int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
217 int maxtype, const struct nla_policy *policy)
218{
219 if (!nlmsg_valid_hdr(nlh, hdrlen))
220 return -NLE_MSG_TOOSHORT;
221
222 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
223 nlmsg_attrlen(nlh, hdrlen), policy);
224}
225
226/**
227 * nlmsg_find_attr - find a specific attribute in a netlink message
228 * @arg nlh netlink message header
229 * @arg hdrlen length of familiy specific header
230 * @arg attrtype type of attribute to look for
231 *
232 * Returns the first attribute which matches the specified type.
233 */
234struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
235{
236 return nla_find(nlmsg_attrdata(nlh, hdrlen),
237 nlmsg_attrlen(nlh, hdrlen), attrtype);
238}
239
240/**
241 * nlmsg_validate - validate a netlink message including attributes
242 * @arg nlh netlinket message header
243 * @arg hdrlen length of familiy specific header
244 * @arg maxtype maximum attribute type to be expected
245 * @arg policy validation policy
246 */
247int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
248 const struct nla_policy *policy)
249{
250 if (!nlmsg_valid_hdr(nlh, hdrlen))
251 return -NLE_MSG_TOOSHORT;
252
253 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
254 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
255}
256
257/** @} */
258
259/**
260 * @name Message Building/Access
261 * @{
262 */
263
264static struct nl_msg *__nlmsg_alloc(size_t len)
265{
266 struct nl_msg *nm;
267
268 if (len < sizeof(struct nlmsghdr))
269 len = sizeof(struct nlmsghdr);
270
271 nm = calloc(1, sizeof(*nm));
272 if (!nm)
273 goto errout;
274
275 nm->nm_refcnt = 1;
276
277 nm->nm_nlh = calloc(1, len);
278 if (!nm->nm_nlh)
279 goto errout;
280
281 nm->nm_protocol = -1;
282 nm->nm_size = len;
283 nm->nm_nlh->nlmsg_len = nlmsg_total_size(0);
284
285 NL_DBG(2, "msg %p: Allocated new message, maxlen=%zu\n", nm, len);
286
287 return nm;
288errout:
289 free(nm);
290 return NULL;
291}
292
293/**
294 * Allocate a new netlink message with the default maximum payload size.
295 *
296 * Allocates a new netlink message without any further payload. The
297 * maximum payload size defaults to PAGESIZE or as otherwise specified
298 * with nlmsg_set_default_size().
299 *
300 * @return Newly allocated netlink message or NULL.
301 */
302struct nl_msg *nlmsg_alloc(void)
303{
304 return __nlmsg_alloc(default_msg_size);
305}
306
307/**
308 * Allocate a new netlink message with maximum payload size specified.
309 */
310struct nl_msg *nlmsg_alloc_size(size_t max)
311{
312 return __nlmsg_alloc(max);
313}
314
315/**
316 * Allocate a new netlink message and inherit netlink message header
317 * @arg hdr Netlink message header template
318 *
319 * Allocates a new netlink message and inherits the original message
320 * header. If \a hdr is not NULL it will be used as a template for
321 * the netlink message header, otherwise the header is left blank.
322 *
323 * @return Newly allocated netlink message or NULL
324 */
325struct nl_msg *nlmsg_inherit(struct nlmsghdr *hdr)
326{
327 struct nl_msg *nm;
328
329 nm = nlmsg_alloc();
330 if (nm && hdr) {
331 struct nlmsghdr *new = nm->nm_nlh;
332
333 new->nlmsg_type = hdr->nlmsg_type;
334 new->nlmsg_flags = hdr->nlmsg_flags;
335 new->nlmsg_seq = hdr->nlmsg_seq;
336 new->nlmsg_pid = hdr->nlmsg_pid;
337 }
338
339 return nm;
340}
341
342/**
343 * Allocate a new netlink message
344 * @arg nlmsgtype Netlink message type
345 * @arg flags Message flags.
346 *
347 * @return Newly allocated netlink message or NULL.
348 */
349struct nl_msg *nlmsg_alloc_simple(int nlmsgtype, int flags)
350{
351 struct nl_msg *msg;
352 struct nlmsghdr nlh = {
353 .nlmsg_type = nlmsgtype,
354 .nlmsg_flags = flags,
355 .nlmsg_seq = NL_AUTO_SEQ,
356 .nlmsg_pid = NL_AUTO_PID,
357 };
358
359 msg = nlmsg_inherit(&nlh);
360 if (msg)
361 NL_DBG(2, "msg %p: Allocated new simple message\n", msg);
362
363 return msg;
364}
365
366/**
367 * Set the default maximum message payload size for allocated messages
368 * @arg max Size of payload in bytes.
369 */
371{
372 if (max < nlmsg_total_size(0))
373 max = nlmsg_total_size(0);
374
375 default_msg_size = max;
376}
377
378/**
379 * Convert a netlink message received from a netlink socket to a nl_msg
380 * @arg hdr Netlink message received from netlink socket.
381 *
382 * Allocates a new netlink message and copies all of the data pointed to
383 * by \a hdr into the new message object.
384 *
385 * @return Newly allocated netlink message or NULL.
386 */
387struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr)
388{
389 struct nl_msg *nm;
390
391 nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
392 if (!nm)
393 return NULL;
394
395 memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
396
397 return nm;
398}
399
400/**
401 * Reserve room for additional data in a netlink message
402 * @arg n netlink message
403 * @arg len length of additional data to reserve room for
404 * @arg pad number of bytes to align data to
405 *
406 * Reserves room for additional data at the tail of the an
407 * existing netlink message. Eventual padding required will
408 * be zeroed out.
409 *
410 * @return Pointer to start of additional data tailroom or NULL.
411 */
412void *nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
413{
414 char *buf = (char *) n->nm_nlh;
415 size_t nlmsg_len = n->nm_nlh->nlmsg_len;
416 size_t tlen;
417
418 if (len > n->nm_size)
419 return NULL;
420
421 tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
422
423 if ((tlen + nlmsg_len) > n->nm_size)
424 return NULL;
425
426 buf += nlmsg_len;
427 n->nm_nlh->nlmsg_len += tlen;
428
429 if (tlen > len)
430 memset(buf + len, 0, tlen - len);
431
432 NL_DBG(2, "msg %p: Reserved %zu (%zu) bytes, pad=%d, nlmsg_len=%d\n",
433 n, tlen, len, pad, n->nm_nlh->nlmsg_len);
434
435 return buf;
436}
437
438/**
439 * Append data to tail of a netlink message
440 * @arg n netlink message
441 * @arg data data to add
442 * @arg len length of data
443 * @arg pad Number of bytes to align data to.
444 *
445 * Extends the netlink message as needed and appends the data of given
446 * length to the message.
447 *
448 * @return 0 on success or a negative error code
449 */
450int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
451{
452 void *tmp;
453
454 tmp = nlmsg_reserve(n, len, pad);
455 if (tmp == NULL)
456 return -NLE_NOMEM;
457
458 memcpy(tmp, data, len);
459 NL_DBG(2, "msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
460
461 return 0;
462}
463
464/**
465 * Expand maximum payload size of a netlink message
466 * @arg n Netlink message.
467 * @arg newlen New maximum payload size.
468 *
469 * Reallocates the payload section of a netlink message and increases
470 * the maximum payload size of the message.
471 *
472 * @note Any pointers pointing to old payload block will be stale and
473 * need to be refetched. Therfore, do not expand while constructing
474 * nested attributes or while reserved data blocks are held.
475 *
476 * @return 0 on success or a negative error code.
477 */
478int nlmsg_expand(struct nl_msg *n, size_t newlen)
479{
480 void *tmp;
481
482 if (newlen <= n->nm_size)
483 return -NLE_INVAL;
484
485 tmp = realloc(n->nm_nlh, newlen);
486 if (tmp == NULL)
487 return -NLE_NOMEM;
488
489 n->nm_nlh = tmp;
490 n->nm_size = newlen;
491
492 return 0;
493}
494
495/**
496 * Add a netlink message header to a netlink message
497 * @arg n netlink message
498 * @arg pid netlink process id or NL_AUTO_PID
499 * @arg seq sequence number of message or NL_AUTO_SEQ
500 * @arg type message type
501 * @arg payload length of message payload
502 * @arg flags message flags
503 *
504 * Adds or overwrites the netlink message header in an existing message
505 * object. If \a payload is greater-than zero additional room will be
506 * reserved, f.e. for family specific headers. It can be accesed via
507 * nlmsg_data().
508 *
509 * @return A pointer to the netlink message header or NULL.
510 */
511struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
512 int type, int payload, int flags)
513{
514 struct nlmsghdr *nlh;
515
516 if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
517 BUG();
518
519 nlh = (struct nlmsghdr *) n->nm_nlh;
520 nlh->nlmsg_type = type;
521 nlh->nlmsg_flags = flags;
522 nlh->nlmsg_pid = pid;
523 nlh->nlmsg_seq = seq;
524
525 NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
526 "seq=%d\n", n, type, flags, pid, seq);
527
528 if (payload > 0 &&
529 nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
530 return NULL;
531
532 return nlh;
533}
534
535/**
536 * Return actual netlink message
537 * @arg n netlink message
538 *
539 * Returns the actual netlink message casted to the type of the netlink
540 * message header.
541 *
542 * @return A pointer to the netlink message.
543 */
544struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
545{
546 return n->nm_nlh;
547}
548
549/**
550 * Acquire a reference on a netlink message
551 * @arg msg message to acquire reference from
552 */
553void nlmsg_get(struct nl_msg *msg)
554{
555 msg->nm_refcnt++;
556 NL_DBG(4, "New reference to message %p, total %d\n",
557 msg, msg->nm_refcnt);
558}
559
560/**
561 * Release a reference from an netlink message
562 * @arg msg message to release reference from
563 *
564 * Frees memory after the last reference has been released.
565 */
566void nlmsg_free(struct nl_msg *msg)
567{
568 if (!msg)
569 return;
570
571 msg->nm_refcnt--;
572 NL_DBG(4, "Returned message reference %p, %d remaining\n",
573 msg, msg->nm_refcnt);
574
575 if (msg->nm_refcnt < 0)
576 BUG();
577
578 if (msg->nm_refcnt <= 0) {
579 free(msg->nm_nlh);
580 NL_DBG(2, "msg %p: Freed\n", msg);
581 free(msg);
582 }
583}
584
585/** @} */
586
587/**
588 * @name Attributes
589 * @{
590 */
591
592void nlmsg_set_proto(struct nl_msg *msg, int protocol)
593{
594 msg->nm_protocol = protocol;
595}
596
597int nlmsg_get_proto(struct nl_msg *msg)
598{
599 return msg->nm_protocol;
600}
601
602size_t nlmsg_get_max_size(struct nl_msg *msg)
603{
604 return msg->nm_size;
605}
606
607void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
608{
609 memcpy(&msg->nm_src, addr, sizeof(*addr));
610}
611
612struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg)
613{
614 return &msg->nm_src;
615}
616
617void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr)
618{
619 memcpy(&msg->nm_dst, addr, sizeof(*addr));
620}
621
622struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg)
623{
624 return &msg->nm_dst;
625}
626
627void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
628{
629 memcpy(&msg->nm_creds, creds, sizeof(*creds));
630 msg->nm_flags |= NL_MSG_CRED_PRESENT;
631}
632
633struct ucred *nlmsg_get_creds(struct nl_msg *msg)
634{
635 if (msg->nm_flags & NL_MSG_CRED_PRESENT)
636 return &msg->nm_creds;
637 return NULL;
638}
639
640/** @} */
641
642/**
643 * @name Netlink Message Type Translations
644 * @{
645 */
646
647static const struct trans_tbl nl_msgtypes[] = {
648 __ADD(NLMSG_NOOP,NOOP),
649 __ADD(NLMSG_ERROR,ERROR),
650 __ADD(NLMSG_DONE,DONE),
651 __ADD(NLMSG_OVERRUN,OVERRUN),
652};
653
654char *nl_nlmsgtype2str(int type, char *buf, size_t size)
655{
656 return __type2str(type, buf, size, nl_msgtypes,
657 ARRAY_SIZE(nl_msgtypes));
658}
659
660int nl_str2nlmsgtype(const char *name)
661{
662 return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
663}
664
665/** @} */
666
667/**
668 * @name Netlink Message Flags Translations
669 * @{
670 */
671
672char *nl_nlmsg_flags2str(int flags, char *buf, size_t len)
673{
674 memset(buf, 0, len);
675
676#define PRINT_FLAG(f) \
677 if (flags & NLM_F_##f) { \
678 flags &= ~NLM_F_##f; \
679 strncat(buf, #f, len - strlen(buf) - 1); \
680 if (flags) \
681 strncat(buf, ",", len - strlen(buf) - 1); \
682 }
683
684 PRINT_FLAG(REQUEST);
685 PRINT_FLAG(MULTI);
686 PRINT_FLAG(ACK);
687 PRINT_FLAG(ECHO);
688 PRINT_FLAG(ROOT);
689 PRINT_FLAG(MATCH);
690 PRINT_FLAG(ATOMIC);
691 PRINT_FLAG(REPLACE);
692 PRINT_FLAG(EXCL);
693 PRINT_FLAG(CREATE);
694 PRINT_FLAG(APPEND);
695
696 if (flags) {
697 char s[32];
698 snprintf(s, sizeof(s), "0x%x", flags);
699 strncat(buf, s, len - strlen(buf) - 1);
700 }
701#undef PRINT_FLAG
702
703 return buf;
704}
705
706/** @} */
707
708/**
709 * @name Direct Parsing
710 * @{
711 */
712
713/** @cond SKIP */
714struct dp_xdata {
715 void (*cb)(struct nl_object *, void *);
716 void *arg;
717};
718/** @endcond */
719
720static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
721{
722 struct dp_xdata *x = p->pp_arg;
723
724 x->cb(obj, x->arg);
725 return 0;
726}
727
728int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
729 void *arg)
730{
731 struct nl_cache_ops *ops;
732 struct nl_parser_param p = {
733 .pp_cb = parse_cb
734 };
735 struct dp_xdata x = {
736 .cb = cb,
737 .arg = arg,
738 };
739 int err;
740
741 ops = nl_cache_ops_associate_safe(nlmsg_get_proto(msg),
742 nlmsg_hdr(msg)->nlmsg_type);
743 if (ops == NULL)
744 return -NLE_MSGTYPE_NOSUPPORT;
745 p.pp_arg = &x;
746
747 err = nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
748 nl_cache_ops_put(ops);
749
750 return err;
751}
752
753/** @} */
754
755/**
756 * @name Dumping
757 * @{
758 */
759
760static void prefix_line(FILE *ofd, int prefix)
761{
762 int i;
763
764 for (i = 0; i < prefix; i++)
765 fprintf(ofd, " ");
766}
767
768static inline void dump_hex(FILE *ofd, char *start, int len, int prefix)
769{
770 int i, a, c, limit;
771 char ascii[21] = {0};
772
773 limit = 16 - (prefix * 2);
774 prefix_line(ofd, prefix);
775 fprintf(ofd, " ");
776
777 for (i = 0, a = 0, c = 0; i < len; i++) {
778 int v = *(uint8_t *) (start + i);
779
780 fprintf(ofd, "%02x ", v);
781 ascii[a++] = isprint(v) ? v : '.';
782
783 if (++c >= limit) {
784 fprintf(ofd, "%s\n", ascii);
785 if (i < (len - 1)) {
786 prefix_line(ofd, prefix);
787 fprintf(ofd, " ");
788 }
789 a = c = 0;
790 memset(ascii, 0, sizeof(ascii));
791 }
792 }
793
794 if (c != 0) {
795 for (i = 0; i < (limit - c); i++)
796 fprintf(ofd, " ");
797 fprintf(ofd, "%s\n", ascii);
798 }
799}
800
801static void print_hdr(FILE *ofd, struct nl_msg *msg)
802{
803 struct nlmsghdr *nlh = nlmsg_hdr(msg);
804 struct nl_cache_ops *ops;
805 struct nl_msgtype *mt;
806 char buf[128];
807
808 fprintf(ofd, " .nlmsg_len = %d\n", nlh->nlmsg_len);
809
810 ops = nl_cache_ops_associate_safe(nlmsg_get_proto(msg), nlh->nlmsg_type);
811 if (ops) {
812 mt = nl_msgtype_lookup(ops, nlh->nlmsg_type);
813 if (!mt)
814 BUG();
815
816 snprintf(buf, sizeof(buf), "%s::%s", ops->co_name, mt->mt_name);
817 nl_cache_ops_put(ops);
818 } else
819 nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf));
820
821 fprintf(ofd, " .type = %d <%s>\n", nlh->nlmsg_type, buf);
822 fprintf(ofd, " .flags = %d <%s>\n", nlh->nlmsg_flags,
823 nl_nlmsg_flags2str(nlh->nlmsg_flags, buf, sizeof(buf)));
824 fprintf(ofd, " .seq = %d\n", nlh->nlmsg_seq);
825 fprintf(ofd, " .port = %d\n", nlh->nlmsg_pid);
826
827}
828
829static void print_genl_hdr(FILE *ofd, void *start)
830{
831 struct genlmsghdr *ghdr = start;
832
833 fprintf(ofd, " [GENERIC NETLINK HEADER] %zu octets\n", GENL_HDRLEN);
834 fprintf(ofd, " .cmd = %u\n", ghdr->cmd);
835 fprintf(ofd, " .version = %u\n", ghdr->version);
836 fprintf(ofd, " .unused = %#x\n", ghdr->reserved);
837}
838
839static void *print_genl_msg(struct nl_msg *msg, FILE *ofd, struct nlmsghdr *hdr,
840 struct nl_cache_ops *ops, int *payloadlen)
841{
842 char *data = nlmsg_data(hdr);
843
844 if (*payloadlen < GENL_HDRLEN)
845 return data;
846
847 print_genl_hdr(ofd, data);
848
849 *payloadlen -= GENL_HDRLEN;
850 data += GENL_HDRLEN;
851
852 if (ops) {
853 int hdrsize = ops->co_hdrsize - GENL_HDRLEN;
854
855 if (hdrsize > 0) {
856 if (*payloadlen < hdrsize)
857 return data;
858
859 fprintf(ofd, " [HEADER] %d octets\n", hdrsize);
860 dump_hex(ofd, data, hdrsize, 0);
861
862 *payloadlen -= hdrsize;
863 data += hdrsize;
864 }
865 }
866
867 return data;
868}
869
870static void dump_attr(FILE *ofd, struct nlattr *attr, int prefix)
871{
872 int len = nla_len(attr);
873
874 dump_hex(ofd, nla_data(attr), len, prefix);
875}
876
877static void dump_attrs(FILE *ofd, struct nlattr *attrs, int attrlen,
878 int prefix)
879{
880 int rem;
881 struct nlattr *nla;
882
883 nla_for_each_attr(nla, attrs, attrlen, rem) {
884 int padlen, alen = nla_len(nla);
885
886 prefix_line(ofd, prefix);
887
888 if (nla->nla_type == 0)
889 fprintf(ofd, " [ATTR PADDING] %d octets\n", alen);
890 else
891 fprintf(ofd, " [ATTR %02d%s] %d octets\n", nla_type(nla),
892 nla_is_nested(nla) ? " NESTED" : "",
893 alen);
894
895 if (nla_is_nested(nla))
896 dump_attrs(ofd, nla_data(nla), alen, prefix+1);
897 else
898 dump_attr(ofd, nla, prefix);
899
900 padlen = nla_padlen(alen);
901 if (padlen > 0) {
902 prefix_line(ofd, prefix);
903 fprintf(ofd, " [PADDING] %d octets\n",
904 padlen);
905 dump_hex(ofd, (char *) nla_data(nla) + alen,
906 padlen, prefix);
907 }
908 }
909
910 if (rem) {
911 prefix_line(ofd, prefix);
912 fprintf(ofd, " [LEFTOVER] %d octets\n", rem);
913 }
914}
915
916static void dump_error_msg(struct nl_msg *msg, FILE *ofd)
917{
918 struct nlmsghdr *hdr = nlmsg_hdr(msg);
919 struct nlmsgerr *err = nlmsg_data(hdr);
920
921 fprintf(ofd, " [ERRORMSG] %zu octets\n", sizeof(*err));
922
923 if (nlmsg_len(hdr) >= sizeof(*err)) {
924 struct nl_msg *errmsg;
925
926 fprintf(ofd, " .error = %d \"%s\"\n", err->error,
927 nl_strerror_l(-err->error));
928 fprintf(ofd, " [ORIGINAL MESSAGE] %zu octets\n", sizeof(*hdr));
929
930 errmsg = nlmsg_inherit(&err->msg);
931 print_hdr(ofd, errmsg);
932 nlmsg_free(errmsg);
933 }
934}
935
936static void print_msg(struct nl_msg *msg, FILE *ofd, struct nlmsghdr *hdr)
937{
938 struct nl_cache_ops *ops;
939 int payloadlen = nlmsg_len(hdr);
940 int attrlen = 0;
941 void *data;
942
943 data = nlmsg_data(hdr);
944 ops = nl_cache_ops_associate_safe(nlmsg_get_proto(msg),
945 hdr->nlmsg_type);
946 if (ops) {
947 attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
948 payloadlen -= attrlen;
949 }
950
951 if (msg->nm_protocol == NETLINK_GENERIC)
952 data = print_genl_msg(msg, ofd, hdr, ops, &payloadlen);
953
954 if (payloadlen) {
955 fprintf(ofd, " [PAYLOAD] %d octets\n", payloadlen);
956 dump_hex(ofd, data, payloadlen, 0);
957 }
958
959 if (attrlen) {
960 struct nlattr *attrs;
961 int attrlen;
962
963 attrs = nlmsg_attrdata(hdr, ops->co_hdrsize);
964 attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
965 dump_attrs(ofd, attrs, attrlen, 0);
966 }
967
968 if (ops)
969 nl_cache_ops_put(ops);
970}
971
972/**
973 * Dump message in human readable format to file descriptor
974 * @arg msg Message to print
975 * @arg ofd File descriptor.
976 */
977void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
978{
979 struct nlmsghdr *hdr = nlmsg_hdr(msg);
980
981 fprintf(ofd,
982 "-------------------------- BEGIN NETLINK MESSAGE ---------------------------\n");
983
984 fprintf(ofd, " [NETLINK HEADER] %zu octets\n", sizeof(struct nlmsghdr));
985 print_hdr(ofd, msg);
986
987 if (hdr->nlmsg_type == NLMSG_ERROR)
988 dump_error_msg(msg, ofd);
989 else if (nlmsg_len(hdr) > 0)
990 print_msg(msg, ofd, hdr);
991
992 fprintf(ofd,
993 "--------------------------- END NETLINK MESSAGE ---------------------------\n");
994}
995
996/** @} */
997
998/** @} */
int nla_validate(const struct nlattr *head, int len, int maxtype, const struct nla_policy *policy)
Validate a stream of attributes.
Definition attr.c:292
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
Definition attr.c:241
int nla_type(const struct nlattr *nla)
Return type of the attribute.
Definition attr.c:108
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition attr.c:119
int nla_is_nested(const struct nlattr *attr)
Return true if attribute has NLA_F_NESTED flag set.
Definition attr.c:1045
#define nla_for_each_attr(pos, head, len, rem)
Iterate over a stream of attributes.
Definition attr.h:312
int nla_len(const struct nlattr *nla)
Return length of the payload .
Definition attr.c:130
struct nlattr * nla_find(const struct nlattr *head, int len, int attrtype)
Find a single attribute in a stream of attributes.
Definition attr.c:321
int nla_padlen(int payload)
Return length of padding at the tail of the attribute.
Definition attr.c:90
struct nl_msgtype * nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
Lookup message type cache association.
Definition cache_mngt.c:189
struct nl_cache_ops * nl_cache_ops_associate_safe(int protocol, int msgtype)
Associate protocol and message type to cache operations.
Definition cache_mngt.c:164
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
Definition cache_mngt.c:65
int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
check if the netlink message fits into the remaining bytes
Definition msg.c:182
void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
Dump message in human readable format to file descriptor.
Definition msg.c:977
int nlmsg_total_size(int payload)
Calculates size of netlink message including padding based on payload length.
Definition msg.c:75
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition msg.c:544
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition msg.c:349
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition msg.c:108
void nlmsg_get(struct nl_msg *msg)
Acquire a reference on a netlink message.
Definition msg.c:553
struct nlmsghdr * nlmsg_next(struct nlmsghdr *nlh, int *remaining)
next netlink message in message stream
Definition msg.c:197
int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, const struct nla_policy *policy)
nlmsg_validate - validate a netlink message including attributes
Definition msg.c:247
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
Definition msg.c:412
struct nl_msg * nlmsg_convert(struct nlmsghdr *hdr)
Convert a netlink message received from a netlink socket to a nl_msg.
Definition msg.c:387
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:566
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
Definition msg.c:216
struct nl_msg * nlmsg_alloc_size(size_t max)
Allocate a new netlink message with maximum payload size specified.
Definition msg.c:310
int nlmsg_expand(struct nl_msg *n, size_t newlen)
Expand maximum payload size of a netlink message.
Definition msg.c:478
struct nlattr * nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
nlmsg_find_attr - find a specific attribute in a netlink message
Definition msg.c:234
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition msg.c:302
int nlmsg_datalen(const struct nlmsghdr *nlh)
Return length of message payload.
Definition msg.c:124
struct nlmsghdr * nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq, int type, int payload, int flags)
Add a netlink message header to a netlink message.
Definition msg.c:511
int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
length of attributes data
Definition msg.c:157
void nlmsg_set_default_size(size_t max)
Set the default maximum message payload size for allocated messages.
Definition msg.c:370
#define NL_AUTO_SEQ
May be used to refer to a sequence number which should be automatically set just before sending the m...
Definition msg.h:40
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition msg.c:450
int nlmsg_padlen(int payload)
Size of padding that needs to be added at end of message.
Definition msg.c:90
struct nlattr * nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
head of attributes data
Definition msg.c:146
int nlmsg_size(int payload)
Calculates size of netlink message based on payload length.
Definition msg.c:57
struct nl_msg * nlmsg_inherit(struct nlmsghdr *hdr)
Allocate a new netlink message and inherit netlink message header.
Definition msg.c:325
Attribute validation policy.
Definition attr.h:63