libnl  3.2.29
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  tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
415 
416  if ((tlen + nlmsg_len) > n->nm_size)
417  return NULL;
418 
419  buf += nlmsg_len;
420  n->nm_nlh->nlmsg_len += tlen;
421 
422  if (tlen > len)
423  memset(buf + len, 0, tlen - len);
424 
425  NL_DBG(2, "msg %p: Reserved %zu (%zu) bytes, pad=%d, nlmsg_len=%d\n",
426  n, tlen, len, pad, n->nm_nlh->nlmsg_len);
427 
428  return buf;
429 }
430 
431 /**
432  * Append data to tail of a netlink message
433  * @arg n netlink message
434  * @arg data data to add
435  * @arg len length of data
436  * @arg pad Number of bytes to align data to.
437  *
438  * Extends the netlink message as needed and appends the data of given
439  * length to the message.
440  *
441  * @return 0 on success or a negative error code
442  */
443 int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
444 {
445  void *tmp;
446 
447  tmp = nlmsg_reserve(n, len, pad);
448  if (tmp == NULL)
449  return -NLE_NOMEM;
450 
451  memcpy(tmp, data, len);
452  NL_DBG(2, "msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
453 
454  return 0;
455 }
456 
457 /**
458  * Expand maximum payload size of a netlink message
459  * @arg n Netlink message.
460  * @arg newlen New maximum payload size.
461  *
462  * Reallocates the payload section of a netlink message and increases
463  * the maximum payload size of the message.
464  *
465  * @note Any pointers pointing to old payload block will be stale and
466  * need to be refetched. Therfore, do not expand while constructing
467  * nested attributes or while reserved data blocks are held.
468  *
469  * @return 0 on success or a negative error code.
470  */
471 int nlmsg_expand(struct nl_msg *n, size_t newlen)
472 {
473  void *tmp;
474 
475  if (newlen <= n->nm_size)
476  return -NLE_INVAL;
477 
478  tmp = realloc(n->nm_nlh, newlen);
479  if (tmp == NULL)
480  return -NLE_NOMEM;
481 
482  n->nm_nlh = tmp;
483  n->nm_size = newlen;
484 
485  return 0;
486 }
487 
488 /**
489  * Add a netlink message header to a netlink message
490  * @arg n netlink message
491  * @arg pid netlink process id or NL_AUTO_PID
492  * @arg seq sequence number of message or NL_AUTO_SEQ
493  * @arg type message type
494  * @arg payload length of message payload
495  * @arg flags message flags
496  *
497  * Adds or overwrites the netlink message header in an existing message
498  * object. If \a payload is greater-than zero additional room will be
499  * reserved, f.e. for family specific headers. It can be accesed via
500  * nlmsg_data().
501  *
502  * @return A pointer to the netlink message header or NULL.
503  */
504 struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
505  int type, int payload, int flags)
506 {
507  struct nlmsghdr *nlh;
508 
509  if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
510  BUG();
511 
512  nlh = (struct nlmsghdr *) n->nm_nlh;
513  nlh->nlmsg_type = type;
514  nlh->nlmsg_flags = flags;
515  nlh->nlmsg_pid = pid;
516  nlh->nlmsg_seq = seq;
517 
518  NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
519  "seq=%d\n", n, type, flags, pid, seq);
520 
521  if (payload > 0 &&
522  nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
523  return NULL;
524 
525  return nlh;
526 }
527 
528 /**
529  * Return actual netlink message
530  * @arg n netlink message
531  *
532  * Returns the actual netlink message casted to the type of the netlink
533  * message header.
534  *
535  * @return A pointer to the netlink message.
536  */
537 struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
538 {
539  return n->nm_nlh;
540 }
541 
542 /**
543  * Acquire a reference on a netlink message
544  * @arg msg message to acquire reference from
545  */
546 void nlmsg_get(struct nl_msg *msg)
547 {
548  msg->nm_refcnt++;
549  NL_DBG(4, "New reference to message %p, total %d\n",
550  msg, msg->nm_refcnt);
551 }
552 
553 /**
554  * Release a reference from an netlink message
555  * @arg msg message to release reference from
556  *
557  * Frees memory after the last reference has been released.
558  */
559 void nlmsg_free(struct nl_msg *msg)
560 {
561  if (!msg)
562  return;
563 
564  msg->nm_refcnt--;
565  NL_DBG(4, "Returned message reference %p, %d remaining\n",
566  msg, msg->nm_refcnt);
567 
568  if (msg->nm_refcnt < 0)
569  BUG();
570 
571  if (msg->nm_refcnt <= 0) {
572  free(msg->nm_nlh);
573  NL_DBG(2, "msg %p: Freed\n", msg);
574  free(msg);
575  }
576 }
577 
578 /** @} */
579 
580 /**
581  * @name Attributes
582  * @{
583  */
584 
585 void nlmsg_set_proto(struct nl_msg *msg, int protocol)
586 {
587  msg->nm_protocol = protocol;
588 }
589 
590 int nlmsg_get_proto(struct nl_msg *msg)
591 {
592  return msg->nm_protocol;
593 }
594 
595 size_t nlmsg_get_max_size(struct nl_msg *msg)
596 {
597  return msg->nm_size;
598 }
599 
600 void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
601 {
602  memcpy(&msg->nm_src, addr, sizeof(*addr));
603 }
604 
605 struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg)
606 {
607  return &msg->nm_src;
608 }
609 
610 void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr)
611 {
612  memcpy(&msg->nm_dst, addr, sizeof(*addr));
613 }
614 
615 struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg)
616 {
617  return &msg->nm_dst;
618 }
619 
620 void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
621 {
622  memcpy(&msg->nm_creds, creds, sizeof(*creds));
623  msg->nm_flags |= NL_MSG_CRED_PRESENT;
624 }
625 
626 struct ucred *nlmsg_get_creds(struct nl_msg *msg)
627 {
628  if (msg->nm_flags & NL_MSG_CRED_PRESENT)
629  return &msg->nm_creds;
630  return NULL;
631 }
632 
633 /** @} */
634 
635 /**
636  * @name Netlink Message Type Translations
637  * @{
638  */
639 
640 static const struct trans_tbl nl_msgtypes[] = {
641  __ADD(NLMSG_NOOP,NOOP),
642  __ADD(NLMSG_ERROR,ERROR),
643  __ADD(NLMSG_DONE,DONE),
644  __ADD(NLMSG_OVERRUN,OVERRUN),
645 };
646 
647 char *nl_nlmsgtype2str(int type, char *buf, size_t size)
648 {
649  return __type2str(type, buf, size, nl_msgtypes,
650  ARRAY_SIZE(nl_msgtypes));
651 }
652 
653 int nl_str2nlmsgtype(const char *name)
654 {
655  return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
656 }
657 
658 /** @} */
659 
660 /**
661  * @name Netlink Message Flags Translations
662  * @{
663  */
664 
665 char *nl_nlmsg_flags2str(int flags, char *buf, size_t len)
666 {
667  memset(buf, 0, len);
668 
669 #define PRINT_FLAG(f) \
670  if (flags & NLM_F_##f) { \
671  flags &= ~NLM_F_##f; \
672  strncat(buf, #f, len - strlen(buf) - 1); \
673  if (flags) \
674  strncat(buf, ",", len - strlen(buf) - 1); \
675  }
676 
677  PRINT_FLAG(REQUEST);
678  PRINT_FLAG(MULTI);
679  PRINT_FLAG(ACK);
680  PRINT_FLAG(ECHO);
681  PRINT_FLAG(ROOT);
682  PRINT_FLAG(MATCH);
683  PRINT_FLAG(ATOMIC);
684  PRINT_FLAG(REPLACE);
685  PRINT_FLAG(EXCL);
686  PRINT_FLAG(CREATE);
687  PRINT_FLAG(APPEND);
688 
689  if (flags) {
690  char s[32];
691  snprintf(s, sizeof(s), "0x%x", flags);
692  strncat(buf, s, len - strlen(buf) - 1);
693  }
694 #undef PRINT_FLAG
695 
696  return buf;
697 }
698 
699 /** @} */
700 
701 /**
702  * @name Direct Parsing
703  * @{
704  */
705 
706 /** @cond SKIP */
707 struct dp_xdata {
708  void (*cb)(struct nl_object *, void *);
709  void *arg;
710 };
711 /** @endcond */
712 
713 static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
714 {
715  struct dp_xdata *x = p->pp_arg;
716 
717  x->cb(obj, x->arg);
718  return 0;
719 }
720 
721 int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
722  void *arg)
723 {
724  struct nl_cache_ops *ops;
725  struct nl_parser_param p = {
726  .pp_cb = parse_cb
727  };
728  struct dp_xdata x = {
729  .cb = cb,
730  .arg = arg,
731  };
732  int err;
733 
734  ops = nl_cache_ops_associate_safe(nlmsg_get_proto(msg),
735  nlmsg_hdr(msg)->nlmsg_type);
736  if (ops == NULL)
737  return -NLE_MSGTYPE_NOSUPPORT;
738  p.pp_arg = &x;
739 
740  err = nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
741  nl_cache_ops_put(ops);
742 
743  return err;
744 }
745 
746 /** @} */
747 
748 /**
749  * @name Dumping
750  * @{
751  */
752 
753 static void prefix_line(FILE *ofd, int prefix)
754 {
755  int i;
756 
757  for (i = 0; i < prefix; i++)
758  fprintf(ofd, " ");
759 }
760 
761 static inline void dump_hex(FILE *ofd, char *start, int len, int prefix)
762 {
763  int i, a, c, limit;
764  char ascii[21] = {0};
765 
766  limit = 16 - (prefix * 2);
767  prefix_line(ofd, prefix);
768  fprintf(ofd, " ");
769 
770  for (i = 0, a = 0, c = 0; i < len; i++) {
771  int v = *(uint8_t *) (start + i);
772 
773  fprintf(ofd, "%02x ", v);
774  ascii[a++] = isprint(v) ? v : '.';
775 
776  if (++c >= limit) {
777  fprintf(ofd, "%s\n", ascii);
778  if (i < (len - 1)) {
779  prefix_line(ofd, prefix);
780  fprintf(ofd, " ");
781  }
782  a = c = 0;
783  memset(ascii, 0, sizeof(ascii));
784  }
785  }
786 
787  if (c != 0) {
788  for (i = 0; i < (limit - c); i++)
789  fprintf(ofd, " ");
790  fprintf(ofd, "%s\n", ascii);
791  }
792 }
793 
794 static void print_hdr(FILE *ofd, struct nl_msg *msg)
795 {
796  struct nlmsghdr *nlh = nlmsg_hdr(msg);
797  struct nl_cache_ops *ops;
798  struct nl_msgtype *mt;
799  char buf[128];
800 
801  fprintf(ofd, " .nlmsg_len = %d\n", nlh->nlmsg_len);
802 
803  ops = nl_cache_ops_associate_safe(nlmsg_get_proto(msg), nlh->nlmsg_type);
804  if (ops) {
805  mt = nl_msgtype_lookup(ops, nlh->nlmsg_type);
806  if (!mt)
807  BUG();
808 
809  snprintf(buf, sizeof(buf), "%s::%s", ops->co_name, mt->mt_name);
810  nl_cache_ops_put(ops);
811  } else
812  nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf));
813 
814  fprintf(ofd, " .type = %d <%s>\n", nlh->nlmsg_type, buf);
815  fprintf(ofd, " .flags = %d <%s>\n", nlh->nlmsg_flags,
816  nl_nlmsg_flags2str(nlh->nlmsg_flags, buf, sizeof(buf)));
817  fprintf(ofd, " .seq = %d\n", nlh->nlmsg_seq);
818  fprintf(ofd, " .port = %d\n", nlh->nlmsg_pid);
819 
820 }
821 
822 static void print_genl_hdr(FILE *ofd, void *start)
823 {
824  struct genlmsghdr *ghdr = start;
825 
826  fprintf(ofd, " [GENERIC NETLINK HEADER] %zu octets\n", GENL_HDRLEN);
827  fprintf(ofd, " .cmd = %u\n", ghdr->cmd);
828  fprintf(ofd, " .version = %u\n", ghdr->version);
829  fprintf(ofd, " .unused = %#x\n", ghdr->reserved);
830 }
831 
832 static void *print_genl_msg(struct nl_msg *msg, FILE *ofd, struct nlmsghdr *hdr,
833  struct nl_cache_ops *ops, int *payloadlen)
834 {
835  void *data = nlmsg_data(hdr);
836 
837  if (*payloadlen < GENL_HDRLEN)
838  return data;
839 
840  print_genl_hdr(ofd, data);
841 
842  *payloadlen -= GENL_HDRLEN;
843  data += GENL_HDRLEN;
844 
845  if (ops) {
846  int hdrsize = ops->co_hdrsize - GENL_HDRLEN;
847 
848  if (hdrsize > 0) {
849  if (*payloadlen < hdrsize)
850  return data;
851 
852  fprintf(ofd, " [HEADER] %d octets\n", hdrsize);
853  dump_hex(ofd, data, hdrsize, 0);
854 
855  *payloadlen -= hdrsize;
856  data += hdrsize;
857  }
858  }
859 
860  return data;
861 }
862 
863 static void dump_attr(FILE *ofd, struct nlattr *attr, int prefix)
864 {
865  int len = nla_len(attr);
866 
867  dump_hex(ofd, nla_data(attr), len, prefix);
868 }
869 
870 static void dump_attrs(FILE *ofd, struct nlattr *attrs, int attrlen,
871  int prefix)
872 {
873  int rem;
874  struct nlattr *nla;
875 
876  nla_for_each_attr(nla, attrs, attrlen, rem) {
877  int padlen, alen = nla_len(nla);
878 
879  prefix_line(ofd, prefix);
880 
881  if (nla->nla_type == 0)
882  fprintf(ofd, " [ATTR PADDING] %d octets\n", alen);
883  else
884  fprintf(ofd, " [ATTR %02d%s] %d octets\n", nla_type(nla),
885  nla_is_nested(nla) ? " NESTED" : "",
886  alen);
887 
888  if (nla_is_nested(nla))
889  dump_attrs(ofd, nla_data(nla), alen, prefix+1);
890  else
891  dump_attr(ofd, nla, prefix);
892 
893  padlen = nla_padlen(alen);
894  if (padlen > 0) {
895  prefix_line(ofd, prefix);
896  fprintf(ofd, " [PADDING] %d octets\n",
897  padlen);
898  dump_hex(ofd, nla_data(nla) + alen,
899  padlen, prefix);
900  }
901  }
902 
903  if (rem) {
904  prefix_line(ofd, prefix);
905  fprintf(ofd, " [LEFTOVER] %d octets\n", rem);
906  }
907 }
908 
909 static void dump_error_msg(struct nl_msg *msg, FILE *ofd)
910 {
911  struct nlmsghdr *hdr = nlmsg_hdr(msg);
912  struct nlmsgerr *err = nlmsg_data(hdr);
913 
914  fprintf(ofd, " [ERRORMSG] %zu octets\n", sizeof(*err));
915 
916  if (nlmsg_len(hdr) >= sizeof(*err)) {
917  struct nl_msg *errmsg;
918 
919  fprintf(ofd, " .error = %d \"%s\"\n", err->error,
920  nl_strerror_l(-err->error));
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: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:559
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:67
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:1004
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:504
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: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:537
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:471
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:546
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:443
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: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