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