libnl  3.3.0
attr.c
1 /*
2  * lib/attr.c Netlink Attributes
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-2013 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink-private/netlink.h>
13 #include <netlink/netlink.h>
14 #include <netlink/utils.h>
15 #include <netlink/addr.h>
16 #include <netlink/attr.h>
17 #include <netlink/msg.h>
18 #include <linux/socket.h>
19 
20 /**
21  * @ingroup msg
22  * @defgroup attr Attributes
23  * Netlink Attributes Construction/Parsing Interface
24  *
25  * Related sections in the development guide:
26  * - @core_doc{core_attr,Netlink Attributes}
27  *
28  * @{
29  *
30  * Header
31  * ------
32  * ~~~~{.c}
33  * #include <netlink/attr.h>
34  * ~~~~
35  */
36 
37 /**
38  * @name Attribute Size Calculation
39  * @{
40  */
41 
42 /**
43  * Return size of attribute whithout padding.
44  * @arg payload Payload length of attribute.
45  *
46  * @code
47  * <-------- nla_attr_size(payload) --------->
48  * +------------------+- - -+- - - - - - - - - +- - -+
49  * | Attribute Header | Pad | Payload | Pad |
50  * +------------------+- - -+- - - - - - - - - +- - -+
51  * @endcode
52  *
53  * @return Size of attribute in bytes without padding.
54  */
55 int nla_attr_size(int payload)
56 {
57  return NLA_HDRLEN + payload;
58 }
59 
60 /**
61  * Return size of attribute including padding.
62  * @arg payload Payload length of attribute.
63  *
64  * @code
65  * <----------- nla_total_size(payload) ----------->
66  * +------------------+- - -+- - - - - - - - - +- - -+
67  * | Attribute Header | Pad | Payload | Pad |
68  * +------------------+- - -+- - - - - - - - - +- - -+
69  * @endcode
70  *
71  * @return Size of attribute in bytes.
72  */
73 int nla_total_size(int payload)
74 {
75  return NLA_ALIGN(nla_attr_size(payload));
76 }
77 
78 /**
79  * Return length of padding at the tail of the attribute.
80  * @arg payload Payload length of attribute.
81  *
82  * @code
83  * +------------------+- - -+- - - - - - - - - +- - -+
84  * | Attribute Header | Pad | Payload | Pad |
85  * +------------------+- - -+- - - - - - - - - +- - -+
86  * <--->
87  * @endcode
88  *
89  * @return Length of padding in bytes.
90  */
91 int nla_padlen(int payload)
92 {
93  return nla_total_size(payload) - nla_attr_size(payload);
94 }
95 
96 /** @} */
97 
98 /**
99  * @name Parsing Attributes
100  * @{
101  */
102 
103 /**
104  * Return type of the attribute.
105  * @arg nla Attribute.
106  *
107  * @return Type of attribute.
108  */
109 int nla_type(const struct nlattr *nla)
110 {
111  return nla->nla_type & NLA_TYPE_MASK;
112 }
113 
114 /**
115  * Return pointer to the payload section.
116  * @arg nla Attribute.
117  *
118  * @return Pointer to start of payload section.
119  */
120 void *nla_data(const struct nlattr *nla)
121 {
122  return (char *) nla + NLA_HDRLEN;
123 }
124 
125 /**
126  * Return length of the payload .
127  * @arg nla Attribute
128  *
129  * @return Length of payload in bytes.
130  */
131 int nla_len(const struct nlattr *nla)
132 {
133  return nla->nla_len - NLA_HDRLEN;
134 }
135 
136 /**
137  * Check if the attribute header and payload can be accessed safely.
138  * @arg nla Attribute of any kind.
139  * @arg remaining Number of bytes remaining in attribute stream.
140  *
141  * Verifies that the header and payload do not exceed the number of
142  * bytes left in the attribute stream. This function must be called
143  * before access the attribute header or payload when iterating over
144  * the attribute stream using nla_next().
145  *
146  * @return True if the attribute can be accessed safely, false otherwise.
147  */
148 int nla_ok(const struct nlattr *nla, int remaining)
149 {
150  return remaining >= sizeof(*nla) &&
151  nla->nla_len >= sizeof(*nla) &&
152  nla->nla_len <= remaining;
153 }
154 
155 /**
156  * Return next attribute in a stream of attributes.
157  * @arg nla Attribute of any kind.
158  * @arg remaining Variable to count remaining bytes in stream.
159  *
160  * Calculates the offset to the next attribute based on the attribute
161  * given. The attribute provided is assumed to be accessible, the
162  * caller is responsible to use nla_ok() beforehand. The offset (length
163  * of specified attribute including padding) is then subtracted from
164  * the remaining bytes variable and a pointer to the next attribute is
165  * returned.
166  *
167  * nla_next() can be called as long as remainig is >0.
168  *
169  * @return Pointer to next attribute.
170  */
171 struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
172 {
173  int totlen = NLA_ALIGN(nla->nla_len);
174 
175  *remaining -= totlen;
176  return (struct nlattr *) ((char *) nla + totlen);
177 }
178 
179 static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = {
180  [NLA_U8] = sizeof(uint8_t),
181  [NLA_U16] = sizeof(uint16_t),
182  [NLA_U32] = sizeof(uint32_t),
183  [NLA_U64] = sizeof(uint64_t),
184  [NLA_STRING] = 1,
185  [NLA_FLAG] = 0,
186 };
187 
188 static int validate_nla(const struct nlattr *nla, int maxtype,
189  const struct nla_policy *policy)
190 {
191  const struct nla_policy *pt;
192  unsigned int minlen = 0;
193  int type = nla_type(nla);
194 
195  if (type < 0 || type > maxtype)
196  return 0;
197 
198  pt = &policy[type];
199 
200  if (pt->type > NLA_TYPE_MAX)
201  BUG();
202 
203  if (pt->minlen)
204  minlen = pt->minlen;
205  else if (pt->type != NLA_UNSPEC)
206  minlen = nla_attr_minlen[pt->type];
207 
208  if (nla_len(nla) < minlen)
209  return -NLE_RANGE;
210 
211  if (pt->maxlen && nla_len(nla) > pt->maxlen)
212  return -NLE_RANGE;
213 
214  if (pt->type == NLA_STRING) {
215  const char *data = nla_data(nla);
216  if (data[nla_len(nla) - 1] != '\0')
217  return -NLE_INVAL;
218  }
219 
220  return 0;
221 }
222 
223 
224 /**
225  * Create attribute index based on a stream of attributes.
226  * @arg tb Index array to be filled (maxtype+1 elements).
227  * @arg maxtype Maximum attribute type expected and accepted.
228  * @arg head Head of attribute stream.
229  * @arg len Length of attribute stream.
230  * @arg policy Attribute validation policy.
231  *
232  * Iterates over the stream of attributes and stores a pointer to each
233  * attribute in the index array using the attribute type as index to
234  * the array. Attribute with a type greater than the maximum type
235  * specified will be silently ignored in order to maintain backwards
236  * compatibility. If \a policy is not NULL, the attribute will be
237  * validated using the specified policy.
238  *
239  * @see nla_validate
240  * @return 0 on success or a negative error code.
241  */
242 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
243  struct nla_policy *policy)
244 {
245  struct nlattr *nla;
246  int rem, err;
247 
248  memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
249 
250  nla_for_each_attr(nla, head, len, rem) {
251  int type = nla_type(nla);
252 
253  if (type > maxtype)
254  continue;
255 
256  if (policy) {
257  err = validate_nla(nla, maxtype, policy);
258  if (err < 0)
259  goto errout;
260  }
261 
262  if (tb[type])
263  NL_DBG(1, "Attribute of type %#x found multiple times in message, "
264  "previous attribute is being ignored.\n", type);
265 
266  tb[type] = nla;
267  }
268 
269  if (rem > 0)
270  NL_DBG(1, "netlink: %d bytes leftover after parsing "
271  "attributes.\n", rem);
272 
273  err = 0;
274 errout:
275  return err;
276 }
277 
278 /**
279  * Validate a stream of attributes.
280  * @arg head Head of attributes stream.
281  * @arg len Length of attributes stream.
282  * @arg maxtype Maximum attribute type expected and accepted.
283  * @arg policy Validation policy.
284  *
285  * Iterates over the stream of attributes and validates each attribute
286  * one by one using the specified policy. Attributes with a type greater
287  * than the maximum type specified will be silently ignored in order to
288  * maintain backwards compatibility.
289  *
290  * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
291  *
292  * @return 0 on success or a negative error code.
293  */
294 int nla_validate(const struct nlattr *head, int len, int maxtype,
295  const struct nla_policy *policy)
296 {
297  const struct nlattr *nla;
298  int rem, err;
299 
300  nla_for_each_attr(nla, head, len, rem) {
301  err = validate_nla(nla, maxtype, policy);
302  if (err < 0)
303  goto errout;
304  }
305 
306  err = 0;
307 errout:
308  return err;
309 }
310 
311 /**
312  * Find a single attribute in a stream of attributes.
313  * @arg head Head of attributes stream.
314  * @arg len Length of attributes stream.
315  * @arg attrtype Attribute type to look for.
316  *
317  * Iterates over the stream of attributes and compares each type with
318  * the type specified. Returns the first attribute which matches the
319  * type.
320  *
321  * @return Pointer to attribute found or NULL.
322  */
323 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
324 {
325  const struct nlattr *nla;
326  int rem;
327 
328  nla_for_each_attr(nla, head, len, rem)
329  if (nla_type(nla) == attrtype)
330  return (struct nlattr*)nla;
331 
332  return NULL;
333 }
334 
335 /** @} */
336 
337 /**
338  * @name Helper Functions
339  * @{
340  */
341 
342 /**
343  * Copy attribute payload to another memory area.
344  * @arg dest Pointer to destination memory area.
345  * @arg src Attribute
346  * @arg count Number of bytes to copy at most.
347  *
348  * Note: The number of bytes copied is limited by the length of
349  * the attribute payload.
350  *
351  * @return The number of bytes copied to dest.
352  */
353 int nla_memcpy(void *dest, const struct nlattr *src, int count)
354 {
355  int minlen;
356 
357  if (!src)
358  return 0;
359 
360  minlen = min_t(int, count, nla_len(src));
361  memcpy(dest, nla_data(src), minlen);
362 
363  return minlen;
364 }
365 
366 /**
367  * Copy string attribute payload to a buffer.
368  * @arg dst Pointer to destination buffer.
369  * @arg nla Attribute of type NLA_STRING.
370  * @arg dstsize Size of destination buffer in bytes.
371  *
372  * Copies at most dstsize - 1 bytes to the destination buffer.
373  * The result is always a valid NUL terminated string. Unlike
374  * strlcpy the destination buffer is always padded out.
375  *
376  * @return The length of string attribute without the terminating NUL.
377  */
378 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
379 {
380  size_t srclen = nla_len(nla);
381  const char *src = nla_data(nla);
382 
383  if (srclen > 0 && src[srclen - 1] == '\0')
384  srclen--;
385 
386  if (dstsize > 0) {
387  size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
388 
389  memset(dst, 0, dstsize);
390  memcpy(dst, src, len);
391  }
392 
393  return srclen;
394 }
395 
396 /**
397  * Compare attribute payload with memory area.
398  * @arg nla Attribute.
399  * @arg data Memory area to compare to.
400  * @arg size Number of bytes to compare.
401  *
402  * @see memcmp(3)
403  * @return An integer less than, equal to, or greater than zero.
404  */
405 int nla_memcmp(const struct nlattr *nla, const void *data, size_t size)
406 {
407  int d = nla_len(nla) - size;
408 
409  if (d == 0)
410  d = memcmp(nla_data(nla), data, size);
411 
412  return d;
413 }
414 
415 /**
416  * Compare string attribute payload with string
417  * @arg nla Attribute of type NLA_STRING.
418  * @arg str NUL terminated string.
419  *
420  * @see strcmp(3)
421  * @return An integer less than, equal to, or greater than zero.
422  */
423 int nla_strcmp(const struct nlattr *nla, const char *str)
424 {
425  int len = strlen(str) + 1;
426  int d = nla_len(nla) - len;
427 
428  if (d == 0)
429  d = memcmp(nla_data(nla), str, len);
430 
431  return d;
432 }
433 
434 /** @} */
435 
436 /**
437  * @name Unspecific Attribute
438  * @{
439  */
440 
441 /**
442  * Reserve space for a attribute.
443  * @arg msg Netlink Message.
444  * @arg attrtype Attribute Type.
445  * @arg attrlen Length of payload.
446  *
447  * Reserves room for a attribute in the specified netlink message and
448  * fills in the attribute header (type, length). Returns NULL if there
449  * is unsuficient space for the attribute.
450  *
451  * Any padding between payload and the start of the next attribute is
452  * zeroed out.
453  *
454  * @return Pointer to start of attribute or NULL on failure.
455  */
456 struct nlattr *nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
457 {
458  struct nlattr *nla;
459  int tlen;
460 
461  if (attrlen < 0)
462  return NULL;
463 
464  tlen = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) + nla_total_size(attrlen);
465 
466  if (tlen > msg->nm_size)
467  return NULL;
468 
469  nla = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
470  nla->nla_type = attrtype;
471  nla->nla_len = nla_attr_size(attrlen);
472 
473  if (attrlen)
474  memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
475  msg->nm_nlh->nlmsg_len = tlen;
476 
477  NL_DBG(2, "msg %p: attr <%p> %d: Reserved %d (%d) bytes at offset +%td "
478  "nlmsg_len=%d\n", msg, nla, nla->nla_type,
479  nla_total_size(attrlen), attrlen,
480  (void *) nla - nlmsg_data(msg->nm_nlh),
481  msg->nm_nlh->nlmsg_len);
482 
483  return nla;
484 }
485 
486 /**
487  * Add a unspecific attribute to netlink message.
488  * @arg msg Netlink message.
489  * @arg attrtype Attribute type.
490  * @arg datalen Length of data to be used as payload.
491  * @arg data Pointer to data to be used as attribute payload.
492  *
493  * Reserves room for a unspecific attribute and copies the provided data
494  * into the message as payload of the attribute. Returns an error if there
495  * is insufficient space for the attribute.
496  *
497  * @see nla_reserve
498  * @return 0 on success or a negative error code.
499  */
500 int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
501 {
502  struct nlattr *nla;
503 
504  nla = nla_reserve(msg, attrtype, datalen);
505  if (!nla) {
506  if (datalen < 0)
507  return -NLE_INVAL;
508 
509  return -NLE_NOMEM;
510  }
511 
512  if (datalen > 0) {
513  memcpy(nla_data(nla), data, datalen);
514  NL_DBG(2, "msg %p: attr <%p> %d: Wrote %d bytes at offset +%td\n",
515  msg, nla, nla->nla_type, datalen,
516  (void *) nla - nlmsg_data(msg->nm_nlh));
517  }
518 
519  return 0;
520 }
521 
522 /**
523  * Add abstract data as unspecific attribute to netlink message.
524  * @arg msg Netlink message.
525  * @arg attrtype Attribute type.
526  * @arg data Abstract data object.
527  *
528  * Equivalent to nla_put() except that the length of the payload is
529  * derived from the abstract data object.
530  *
531  * @see nla_put
532  * @return 0 on success or a negative error code.
533  */
534 int nla_put_data(struct nl_msg *msg, int attrtype, const struct nl_data *data)
535 {
536  return nla_put(msg, attrtype, nl_data_get_size(data),
537  nl_data_get(data));
538 }
539 
540 /**
541  * Add abstract address as unspecific attribute to netlink message.
542  * @arg msg Netlink message.
543  * @arg attrtype Attribute type.
544  * @arg addr Abstract address object.
545  *
546  * @see nla_put
547  * @return 0 on success or a negative error code.
548  */
549 int nla_put_addr(struct nl_msg *msg, int attrtype, struct nl_addr *addr)
550 {
551  return nla_put(msg, attrtype, nl_addr_get_len(addr),
553 }
554 
555 /** @} */
556 
557 /**
558  * @name Integer Attributes
559  */
560 
561 /**
562  * Add 8 bit signed integer attribute to netlink message.
563  * @arg msg Netlink message.
564  * @arg attrtype Attribute type.
565  * @arg value Numeric value to store as payload.
566  *
567  * @see nla_put
568  * @return 0 on success or a negative error code.
569  */
570 int nla_put_s8(struct nl_msg *msg, int attrtype, int8_t value)
571 {
572  return nla_put(msg, attrtype, sizeof(int8_t), &value);
573 }
574 
575 /**
576  * Return value of 8 bit signed integer attribute.
577  * @arg nla 8 bit integer attribute
578  *
579  * @return Payload as 8 bit integer.
580  */
581 int8_t nla_get_s8(const struct nlattr *nla)
582 {
583  return *(const int8_t *) nla_data(nla);
584 }
585 
586 /**
587  * Add 8 bit integer attribute to netlink message.
588  * @arg msg Netlink message.
589  * @arg attrtype Attribute type.
590  * @arg value Numeric value to store as payload.
591  *
592  * @see nla_put
593  * @return 0 on success or a negative error code.
594  */
595 int nla_put_u8(struct nl_msg *msg, int attrtype, uint8_t value)
596 {
597  return nla_put(msg, attrtype, sizeof(uint8_t), &value);
598 }
599 
600 /**
601  * Return value of 8 bit integer attribute.
602  * @arg nla 8 bit integer attribute
603  *
604  * @return Payload as 8 bit integer.
605  */
606 uint8_t nla_get_u8(const struct nlattr *nla)
607 {
608  return *(const uint8_t *) nla_data(nla);
609 }
610 
611 /**
612  * Add 16 bit signed integer attribute to netlink message.
613  * @arg msg Netlink message.
614  * @arg attrtype Attribute type.
615  * @arg value Numeric value to store as payload.
616  *
617  * @see nla_put
618  * @return 0 on success or a negative error code.
619  */
620 int nla_put_s16(struct nl_msg *msg, int attrtype, int16_t value)
621 {
622  return nla_put(msg, attrtype, sizeof(int16_t), &value);
623 }
624 
625 /**
626  * Return payload of 16 bit signed integer attribute.
627  * @arg nla 16 bit integer attribute
628  *
629  * @return Payload as 16 bit integer.
630  */
631 int16_t nla_get_s16(const struct nlattr *nla)
632 {
633  return *(const int16_t *) nla_data(nla);
634 }
635 
636 /**
637  * Add 16 bit integer attribute to netlink message.
638  * @arg msg Netlink message.
639  * @arg attrtype Attribute type.
640  * @arg value Numeric value to store as payload.
641  *
642  * @see nla_put
643  * @return 0 on success or a negative error code.
644  */
645 int nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value)
646 {
647  return nla_put(msg, attrtype, sizeof(uint16_t), &value);
648 }
649 
650 /**
651  * Return payload of 16 bit integer attribute.
652  * @arg nla 16 bit integer attribute
653  *
654  * @return Payload as 16 bit integer.
655  */
656 uint16_t nla_get_u16(const struct nlattr *nla)
657 {
658  return *(const uint16_t *) nla_data(nla);
659 }
660 
661 /**
662  * Add 32 bit signed integer attribute to netlink message.
663  * @arg msg Netlink message.
664  * @arg attrtype Attribute type.
665  * @arg value Numeric value to store as payload.
666  *
667  * @see nla_put
668  * @return 0 on success or a negative error code.
669  */
670 int nla_put_s32(struct nl_msg *msg, int attrtype, int32_t value)
671 {
672  return nla_put(msg, attrtype, sizeof(int32_t), &value);
673 }
674 
675 /**
676  * Return payload of 32 bit signed integer attribute.
677  * @arg nla 32 bit integer attribute.
678  *
679  * @return Payload as 32 bit integer.
680  */
681 int32_t nla_get_s32(const struct nlattr *nla)
682 {
683  return *(const int32_t *) nla_data(nla);
684 }
685 
686 /**
687  * Add 32 bit integer attribute to netlink message.
688  * @arg msg Netlink message.
689  * @arg attrtype Attribute type.
690  * @arg value Numeric value to store as payload.
691  *
692  * @see nla_put
693  * @return 0 on success or a negative error code.
694  */
695 int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
696 {
697  return nla_put(msg, attrtype, sizeof(uint32_t), &value);
698 }
699 
700 /**
701  * Return payload of 32 bit integer attribute.
702  * @arg nla 32 bit integer attribute.
703  *
704  * @return Payload as 32 bit integer.
705  */
706 uint32_t nla_get_u32(const struct nlattr *nla)
707 {
708  return *(const uint32_t *) nla_data(nla);
709 }
710 
711 /**
712  * Add 64 bit signed integer attribute to netlink message.
713  * @arg msg Netlink message.
714  * @arg attrtype Attribute type.
715  * @arg value Numeric value to store as payload.
716  *
717  * @see nla_put
718  * @return 0 on success or a negative error code.
719  */
720 int nla_put_s64(struct nl_msg *msg, int attrtype, int64_t value)
721 {
722  return nla_put(msg, attrtype, sizeof(int64_t), &value);
723 }
724 
725 /**
726  * Return payload of s64 attribute
727  * @arg nla s64 netlink attribute
728  *
729  * @return Payload as 64 bit integer.
730  */
731 int64_t nla_get_s64(const struct nlattr *nla)
732 {
733  int64_t tmp = 0;
734 
735  if (nla && nla_len(nla) >= sizeof(tmp))
736  memcpy(&tmp, nla_data(nla), sizeof(tmp));
737 
738  return tmp;
739 }
740 
741 /**
742  * Add 64 bit integer attribute to netlink message.
743  * @arg msg Netlink message.
744  * @arg attrtype Attribute type.
745  * @arg value Numeric value to store as payload.
746  *
747  * @see nla_put
748  * @return 0 on success or a negative error code.
749  */
750 int nla_put_u64(struct nl_msg *msg, int attrtype, uint64_t value)
751 {
752  return nla_put(msg, attrtype, sizeof(uint64_t), &value);
753 }
754 
755 /**
756  * Return payload of u64 attribute
757  * @arg nla u64 netlink attribute
758  *
759  * @return Payload as 64 bit integer.
760  */
761 uint64_t nla_get_u64(const struct nlattr *nla)
762 {
763  uint64_t tmp = 0;
764 
765  if (nla && nla_len(nla) >= sizeof(tmp))
766  memcpy(&tmp, nla_data(nla), sizeof(tmp));
767 
768  return tmp;
769 }
770 
771 /** @} */
772 
773 /**
774  * @name String Attribute
775  */
776 
777 /**
778  * Add string attribute to netlink message.
779  * @arg msg Netlink message.
780  * @arg attrtype Attribute type.
781  * @arg str NUL terminated string.
782  *
783  * @see nla_put
784  * @return 0 on success or a negative error code.
785  */
786 int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
787 {
788  return nla_put(msg, attrtype, strlen(str) + 1, str);
789 }
790 
791 /**
792  * Return payload of string attribute.
793  * @arg nla String attribute.
794  *
795  * @return Pointer to attribute payload.
796  */
797 char *nla_get_string(const struct nlattr *nla)
798 {
799  return (char *) nla_data(nla);
800 }
801 
802 char *nla_strdup(const struct nlattr *nla)
803 {
804  return strdup(nla_get_string(nla));
805 }
806 
807 /** @} */
808 
809 /**
810  * @name Flag Attribute
811  */
812 
813 /**
814  * Add flag netlink attribute to netlink message.
815  * @arg msg Netlink message.
816  * @arg attrtype Attribute type.
817  *
818  * @see nla_put
819  * @return 0 on success or a negative error code.
820  */
821 int nla_put_flag(struct nl_msg *msg, int attrtype)
822 {
823  return nla_put(msg, attrtype, 0, NULL);
824 }
825 
826 /**
827  * Return true if flag attribute is set.
828  * @arg nla Flag netlink attribute.
829  *
830  * @return True if flag is set, otherwise false.
831  */
832 int nla_get_flag(const struct nlattr *nla)
833 {
834  return !!nla;
835 }
836 
837 /** @} */
838 
839 /**
840  * @name Microseconds Attribute
841  */
842 
843 /**
844  * Add a msecs netlink attribute to a netlink message
845  * @arg n netlink message
846  * @arg attrtype attribute type
847  * @arg msecs number of msecs
848  */
849 int nla_put_msecs(struct nl_msg *n, int attrtype, unsigned long msecs)
850 {
851  return nla_put_u64(n, attrtype, msecs);
852 }
853 
854 /**
855  * Return payload of msecs attribute
856  * @arg nla msecs netlink attribute
857  *
858  * @return the number of milliseconds.
859  */
860 unsigned long nla_get_msecs(const struct nlattr *nla)
861 {
862  return nla_get_u64(nla);
863 }
864 
865 /** @} */
866 
867 /**
868  * @name Nested Attribute
869  */
870 
871 /**
872  * Add nested attributes to netlink message.
873  * @arg msg Netlink message.
874  * @arg attrtype Attribute type.
875  * @arg nested Message containing attributes to be nested.
876  *
877  * Takes the attributes found in the \a nested message and appends them
878  * to the message \a msg nested in a container of the type \a attrtype.
879  * The \a nested message may not have a family specific header.
880  *
881  * @see nla_put
882  * @return 0 on success or a negative error code.
883  */
884 int nla_put_nested(struct nl_msg *msg, int attrtype,
885  const struct nl_msg *nested)
886 {
887  NL_DBG(2, "msg %p: attr <> %d: adding msg %p as nested attribute\n",
888  msg, attrtype, nested);
889 
890  return nla_put(msg, attrtype, nlmsg_datalen(nested->nm_nlh),
891  nlmsg_data(nested->nm_nlh));
892 }
893 
894 
895 /**
896  * Start a new level of nested attributes.
897  * @arg msg Netlink message.
898  * @arg attrtype Attribute type of container.
899  *
900  * @return Pointer to container attribute.
901  */
902 struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
903 {
904  struct nlattr *start = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
905 
906  if (nla_put(msg, attrtype, 0, NULL) < 0)
907  return NULL;
908 
909  NL_DBG(2, "msg %p: attr <%p> %d: starting nesting\n",
910  msg, start, start->nla_type);
911 
912  return start;
913 }
914 
915 /**
916  * Finalize nesting of attributes.
917  * @arg msg Netlink message.
918  * @arg start Container attribute as returned from nla_nest_start().
919  *
920  * Corrects the container attribute header to include the appeneded attributes.
921  *
922  * @return 0 on success or a negative error code.
923  */
924 int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
925 {
926  size_t pad, len;
927 
928  len = (void *) nlmsg_tail(msg->nm_nlh) - (void *) start;
929 
930  if (len == NLA_HDRLEN || len > USHRT_MAX) {
931  /*
932  * Max nlattr size exceeded or empty nested attribute, trim the
933  * attribute header again
934  */
935  nla_nest_cancel(msg, start);
936 
937  /* Return error only if nlattr size was exceeded */
938  return (len == NLA_HDRLEN) ? 0 : -NLE_ATTRSIZE;
939  }
940 
941  start->nla_len = len;
942 
943  pad = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) - msg->nm_nlh->nlmsg_len;
944  if (pad > 0) {
945  /*
946  * Data inside attribute does not end at a alignment boundry.
947  * Pad accordingly and accoun for the additional space in
948  * the message. nlmsg_reserve() may never fail in this situation,
949  * the allocate message buffer must be a multiple of NLMSG_ALIGNTO.
950  */
951  if (!nlmsg_reserve(msg, pad, 0))
952  BUG();
953 
954  NL_DBG(2, "msg %p: attr <%p> %d: added %zu bytes of padding\n",
955  msg, start, start->nla_type, pad);
956  }
957 
958  NL_DBG(2, "msg %p: attr <%p> %d: closing nesting, len=%u\n",
959  msg, start, start->nla_type, start->nla_len);
960 
961  return 0;
962 }
963 
964 /**
965  * Cancel the addition of a nested attribute
966  * @arg msg Netlink message
967  * @arg attr Nested netlink attribute
968  *
969  * Removes any partially added nested Netlink attribute from the message
970  * by resetting the message to the size before the call to nla_nest_start()
971  * and by overwriting any potentially touched message segments with 0.
972  */
973 void nla_nest_cancel(struct nl_msg *msg, const struct nlattr *attr)
974 {
975  ssize_t len;
976 
977  len = (void *) nlmsg_tail(msg->nm_nlh) - (void *) attr;
978  if (len < 0)
979  BUG();
980  else if (len > 0) {
981  msg->nm_nlh->nlmsg_len -= len;
982  memset(nlmsg_tail(msg->nm_nlh), 0, len);
983  }
984 }
985 
986 /**
987  * Create attribute index based on nested attribute
988  * @arg tb Index array to be filled (maxtype+1 elements).
989  * @arg maxtype Maximum attribute type expected and accepted.
990  * @arg nla Nested Attribute.
991  * @arg policy Attribute validation policy.
992  *
993  * Feeds the stream of attributes nested into the specified attribute
994  * to nla_parse().
995  *
996  * @see nla_parse
997  * @return 0 on success or a negative error code.
998  */
999 int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
1000  struct nla_policy *policy)
1001 {
1002  return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
1003 }
1004 
1005 /**
1006  * Return true if attribute has NLA_F_NESTED flag set
1007  * @arg attr Netlink attribute
1008  *
1009  * @return True if attribute has NLA_F_NESTED flag set, oterhwise False.
1010  */
1011 int nla_is_nested(const struct nlattr *attr)
1012 {
1013  return !!(attr->nla_type & NLA_F_NESTED);
1014 }
1015 
1016 /** @} */
1017 
1018 /** @} */
8 bit integer
Definition: attr.h:41
int nla_ok(const struct nlattr *nla, int remaining)
Check if the attribute header and payload can be accessed safely.
Definition: attr.c:148
int32_t nla_get_s32(const struct nlattr *nla)
Return payload of 32 bit signed integer attribute.
Definition: attr.c:681
int nla_padlen(int payload)
Return length of padding at the tail of the attribute.
Definition: attr.c:91
int nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value)
Add 16 bit integer attribute to netlink message.
Definition: attr.c:645
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 nla_get_flag(const struct nlattr *nla)
Return true if flag attribute is set.
Definition: attr.c:832
void * nl_data_get(const struct nl_data *data)
Get data buffer of abstract data object.
Definition: data.c:153
int16_t nla_get_s16(const struct nlattr *nla)
Return payload of 16 bit signed integer attribute.
Definition: attr.c:631
int nla_put_addr(struct nl_msg *msg, int attrtype, struct nl_addr *addr)
Add abstract address as unspecific attribute to netlink message.
Definition: attr.c:549
int nla_put_s8(struct nl_msg *msg, int attrtype, int8_t value)
Add 8 bit signed integer attribute to netlink message.
Definition: attr.c:570
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
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
Definition: attr.c:606
Unspecified type, binary data chunk.
Definition: attr.h:40
int nla_strcmp(const struct nlattr *nla, const char *str)
Compare string attribute payload with string.
Definition: attr.c:423
char * nla_get_string(const struct nlattr *nla)
Return payload of string attribute.
Definition: attr.c:797
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:706
size_t nl_data_get_size(const struct nl_data *data)
Get size of data buffer of abstract data object.
Definition: data.c:165
struct nlattr * nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
Reserve space for a attribute.
Definition: attr.c:456
int8_t nla_get_s8(const struct nlattr *nla)
Return value of 8 bit signed integer attribute.
Definition: attr.c:581
int nla_put_s32(struct nl_msg *msg, int attrtype, int32_t value)
Add 32 bit signed integer attribute to netlink message.
Definition: attr.c:670
NUL terminated character string.
Definition: attr.h:45
int nla_is_nested(const struct nlattr *attr)
Return true if attribute has NLA_F_NESTED flag set.
Definition: attr.c:1011
int nla_total_size(int payload)
Return size of attribute including padding.
Definition: attr.c:73
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:924
int nla_put_flag(struct nl_msg *msg, int attrtype)
Add flag netlink attribute to netlink message.
Definition: attr.c:821
int64_t nla_get_s64(const struct nlattr *nla)
Return payload of s64 attribute.
Definition: attr.c:731
struct nlattr * nla_next(const struct nlattr *nla, int *remaining)
Return next attribute in a stream of attributes.
Definition: attr.c:171
int nlmsg_datalen(const struct nlmsghdr *nlh)
Return length of message payload.
Definition: msg.c:122
int nla_put_data(struct nl_msg *msg, int attrtype, const struct nl_data *data)
Add abstract data as unspecific attribute to netlink message.
Definition: attr.c:534
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
Definition: attr.c:353
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition: attr.c:999
int nla_type(const struct nlattr *nla)
Return type of the attribute.
Definition: attr.c:109
16 bit integer
Definition: attr.h:42
int nla_put_msecs(struct nl_msg *n, int attrtype, unsigned long msecs)
Add a msecs netlink attribute to a netlink message.
Definition: attr.c:849
int nla_attr_size(int payload)
Return size of attribute whithout padding.
Definition: attr.c:55
int nla_put_u64(struct nl_msg *msg, int attrtype, uint64_t value)
Add 64 bit integer attribute to netlink message.
Definition: attr.c:750
int nla_put_nested(struct nl_msg *msg, int attrtype, const struct nl_msg *nested)
Add nested attributes to netlink message.
Definition: attr.c:884
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition: attr.c:120
uint16_t maxlen
Maximal length of payload allowed.
Definition: attr.h:77
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
unsigned long nla_get_msecs(const struct nlattr *nla)
Return payload of msecs attribute.
Definition: attr.c:860
uint16_t minlen
Minimal length of payload required.
Definition: attr.h:74
64 bit integer
Definition: attr.h:44
int nla_put_s16(struct nl_msg *msg, int attrtype, int16_t value)
Add 16 bit signed integer attribute to netlink message.
Definition: attr.c:620
void nla_nest_cancel(struct nl_msg *msg, const struct nlattr *attr)
Cancel the addition of a nested attribute.
Definition: attr.c:973
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:71
int nla_memcmp(const struct nlattr *nla, const void *data, size_t size)
Compare attribute payload with memory area.
Definition: attr.c:405
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
Definition: attr.c:656
int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
Add 32 bit integer attribute to netlink message.
Definition: attr.c:695
32 bit integer
Definition: attr.h:43
Flag.
Definition: attr.h:46
int nla_put_u8(struct nl_msg *msg, int attrtype, uint8_t value)
Add 8 bit integer attribute to netlink message.
Definition: attr.c:595
uint64_t nla_get_u64(const struct nlattr *nla)
Return payload of u64 attribute.
Definition: attr.c:761
int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
Add string attribute to netlink message.
Definition: attr.c:786
int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
Add a unspecific attribute to netlink message.
Definition: attr.c:500
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
Definition: addr.c:917
int nla_put_s64(struct nl_msg *msg, int attrtype, int64_t value)
Add 64 bit signed integer attribute to netlink message.
Definition: attr.c:720
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
Definition: addr.c:905
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
Definition: attr.c:378
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition: attr.c:902
#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