libnl 3.5.0

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