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