libnl  3.2.11
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-2008 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink-local.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 };
186 
187 static int validate_nla(struct nlattr *nla, int maxtype,
188  struct nla_policy *policy)
189 {
190  struct nla_policy *pt;
191  unsigned int minlen = 0;
192  int type = nla_type(nla);
193 
194  if (type <= 0 || type > maxtype)
195  return 0;
196 
197  pt = &policy[type];
198 
199  if (pt->type > NLA_TYPE_MAX)
200  BUG();
201 
202  if (pt->minlen)
203  minlen = pt->minlen;
204  else if (pt->type != NLA_UNSPEC)
205  minlen = nla_attr_minlen[pt->type];
206 
207  if (pt->type == NLA_FLAG && nla_len(nla) > 0)
208  return -NLE_RANGE;
209 
210  if (nla_len(nla) < minlen)
211  return -NLE_RANGE;
212 
213  if (pt->maxlen && nla_len(nla) > pt->maxlen)
214  return -NLE_RANGE;
215 
216  if (pt->type == NLA_STRING) {
217  char *data = nla_data(nla);
218  if (data[nla_len(nla) - 1] != '\0')
219  return -NLE_INVAL;
220  }
221 
222  return 0;
223 }
224 
225 
226 /**
227  * Create attribute index based on a stream of attributes.
228  * @arg tb Index array to be filled (maxtype+1 elements).
229  * @arg maxtype Maximum attribute type expected and accepted.
230  * @arg head Head of attribute stream.
231  * @arg len Length of attribute stream.
232  * @arg policy Attribute validation policy.
233  *
234  * Iterates over the stream of attributes and stores a pointer to each
235  * attribute in the index array using the attribute type as index to
236  * the array. Attribute with a type greater than the maximum type
237  * specified will be silently ignored in order to maintain backwards
238  * compatibility. If \a policy is not NULL, the attribute will be
239  * validated using the specified policy.
240  *
241  * @see nla_validate
242  * @return 0 on success or a negative error code.
243  */
244 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
245  struct nla_policy *policy)
246 {
247  struct nlattr *nla;
248  int rem, err;
249 
250  memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
251 
252  nla_for_each_attr(nla, head, len, rem) {
253  int type = nla_type(nla);
254 
255  if (type == 0) {
256  fprintf(stderr, "Illegal nla->nla_type == 0\n");
257  continue;
258  }
259 
260  if (type <= maxtype) {
261  if (policy) {
262  err = validate_nla(nla, maxtype, policy);
263  if (err < 0)
264  goto errout;
265  }
266 
267  tb[type] = nla;
268  }
269  }
270 
271  if (rem > 0)
272  fprintf(stderr, "netlink: %d bytes leftover after parsing "
273  "attributes.\n", rem);
274 
275  err = 0;
276 errout:
277  return err;
278 }
279 
280 /**
281  * Validate a stream of attributes.
282  * @arg head Head of attributes stream.
283  * @arg len Length of attributes stream.
284  * @arg maxtype Maximum attribute type expected and accepted.
285  * @arg policy Validation policy.
286  *
287  * Iterates over the stream of attributes and validates each attribute
288  * one by one using the specified policy. Attributes with a type greater
289  * than the maximum type specified will be silently ignored in order to
290  * maintain backwards compatibility.
291  *
292  * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
293  *
294  * @return 0 on success or a negative error code.
295  */
296 int nla_validate(struct nlattr *head, int len, int maxtype,
297  struct nla_policy *policy)
298 {
299  struct nlattr *nla;
300  int rem, err;
301 
302  nla_for_each_attr(nla, head, len, rem) {
303  err = validate_nla(nla, maxtype, policy);
304  if (err < 0)
305  goto errout;
306  }
307 
308  err = 0;
309 errout:
310  return err;
311 }
312 
313 /**
314  * Find a single attribute in a stream of attributes.
315  * @arg head Head of attributes stream.
316  * @arg len Length of attributes stream.
317  * @arg attrtype Attribute type to look for.
318  *
319  * Iterates over the stream of attributes and compares each type with
320  * the type specified. Returns the first attribute which matches the
321  * type.
322  *
323  * @return Pointer to attribute found or NULL.
324  */
325 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
326 {
327  struct nlattr *nla;
328  int rem;
329 
330  nla_for_each_attr(nla, head, len, rem)
331  if (nla_type(nla) == attrtype)
332  return nla;
333 
334  return NULL;
335 }
336 
337 /** @} */
338 
339 /**
340  * @name Helper Functions
341  * @{
342  */
343 
344 /**
345  * Copy attribute payload to another memory area.
346  * @arg dest Pointer to destination memory area.
347  * @arg src Attribute
348  * @arg count Number of bytes to copy at most.
349  *
350  * Note: The number of bytes copied is limited by the length of
351  * the attribute payload.
352  *
353  * @return The number of bytes copied to dest.
354  */
355 int nla_memcpy(void *dest, struct nlattr *src, int count)
356 {
357  int minlen;
358 
359  if (!src)
360  return 0;
361 
362  minlen = min_t(int, count, nla_len(src));
363  memcpy(dest, nla_data(src), minlen);
364 
365  return minlen;
366 }
367 
368 /**
369  * Copy string attribute payload to a buffer.
370  * @arg dst Pointer to destination buffer.
371  * @arg nla Attribute of type NLA_STRING.
372  * @arg dstsize Size of destination buffer in bytes.
373  *
374  * Copies at most dstsize - 1 bytes to the destination buffer.
375  * The result is always a valid NUL terminated string. Unlike
376  * strlcpy the destination buffer is always padded out.
377  *
378  * @return The length of string attribute without the terminating NUL.
379  */
380 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
381 {
382  size_t srclen = nla_len(nla);
383  char *src = nla_data(nla);
384 
385  if (srclen > 0 && src[srclen - 1] == '\0')
386  srclen--;
387 
388  if (dstsize > 0) {
389  size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
390 
391  memset(dst, 0, dstsize);
392  memcpy(dst, src, len);
393  }
394 
395  return srclen;
396 }
397 
398 /**
399  * Compare attribute payload with memory area.
400  * @arg nla Attribute.
401  * @arg data Memory area to compare to.
402  * @arg size Number of bytes to compare.
403  *
404  * @see memcmp(3)
405  * @return An integer less than, equal to, or greater than zero.
406  */
407 int nla_memcmp(const struct nlattr *nla, const void *data, size_t size)
408 {
409  int d = nla_len(nla) - size;
410 
411  if (d == 0)
412  d = memcmp(nla_data(nla), data, size);
413 
414  return d;
415 }
416 
417 /**
418  * Compare string attribute payload with string
419  * @arg nla Attribute of type NLA_STRING.
420  * @arg str NUL terminated string.
421  *
422  * @see strcmp(3)
423  * @return An integer less than, equal to, or greater than zero.
424  */
425 int nla_strcmp(const struct nlattr *nla, const char *str)
426 {
427  int len = strlen(str) + 1;
428  int d = nla_len(nla) - len;
429 
430  if (d == 0)
431  d = memcmp(nla_data(nla), str, len);
432 
433  return d;
434 }
435 
436 /** @} */
437 
438 /**
439  * @name Unspecific Attribute
440  * @{
441  */
442 
443 /**
444  * Reserve space for a attribute.
445  * @arg msg Netlink Message.
446  * @arg attrtype Attribute Type.
447  * @arg attrlen Length of payload.
448  *
449  * Reserves room for a attribute in the specified netlink message and
450  * fills in the attribute header (type, length). Returns NULL if there
451  * is unsuficient space for the attribute.
452  *
453  * Any padding between payload and the start of the next attribute is
454  * zeroed out.
455  *
456  * @return Pointer to start of attribute or NULL on failure.
457  */
458 struct nlattr *nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
459 {
460  struct nlattr *nla;
461  int tlen;
462 
463  tlen = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) + nla_total_size(attrlen);
464 
465  if ((tlen + msg->nm_nlh->nlmsg_len) > msg->nm_size)
466  return NULL;
467 
468  nla = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
469  nla->nla_type = attrtype;
470  nla->nla_len = nla_attr_size(attrlen);
471 
472  if (attrlen)
473  memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
474  msg->nm_nlh->nlmsg_len = tlen;
475 
476  NL_DBG(2, "msg %p: attr <%p> %d: Reserved %d (%d) bytes at offset +%td "
477  "nlmsg_len=%d\n", msg, nla, nla->nla_type,
478  nla_total_size(attrlen), attrlen,
479  (void *) nla - nlmsg_data(msg->nm_nlh),
480  msg->nm_nlh->nlmsg_len);
481 
482  return nla;
483 }
484 
485 /**
486  * Add a unspecific attribute to netlink message.
487  * @arg msg Netlink message.
488  * @arg attrtype Attribute type.
489  * @arg datalen Length of data to be used as payload.
490  * @arg data Pointer to data to be used as attribute payload.
491  *
492  * Reserves room for a unspecific attribute and copies the provided data
493  * into the message as payload of the attribute. Returns an error if there
494  * is insufficient space for the attribute.
495  *
496  * @see nla_reserve
497  * @return 0 on success or a negative error code.
498  */
499 int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
500 {
501  struct nlattr *nla;
502 
503  nla = nla_reserve(msg, attrtype, datalen);
504  if (!nla)
505  return -NLE_NOMEM;
506 
507  if (datalen > 0) {
508  memcpy(nla_data(nla), data, datalen);
509  NL_DBG(2, "msg %p: attr <%p> %d: Wrote %d bytes at offset +%td\n",
510  msg, nla, nla->nla_type, datalen,
511  (void *) nla - nlmsg_data(msg->nm_nlh));
512  }
513 
514  return 0;
515 }
516 
517 /**
518  * Add abstract data as unspecific attribute to netlink message.
519  * @arg msg Netlink message.
520  * @arg attrtype Attribute type.
521  * @arg data Abstract data object.
522  *
523  * Equivalent to nla_put() except that the length of the payload is
524  * derived from the abstract data object.
525  *
526  * @see nla_put
527  * @return 0 on success or a negative error code.
528  */
529 int nla_put_data(struct nl_msg *msg, int attrtype, struct nl_data *data)
530 {
531  return nla_put(msg, attrtype, nl_data_get_size(data),
532  nl_data_get(data));
533 }
534 
535 /**
536  * Add abstract address as unspecific attribute to netlink message.
537  * @arg msg Netlink message.
538  * @arg attrtype Attribute type.
539  * @arg addr Abstract address object.
540  *
541  * @see nla_put
542  * @return 0 on success or a negative error code.
543  */
544 int nla_put_addr(struct nl_msg *msg, int attrtype, struct nl_addr *addr)
545 {
546  return nla_put(msg, attrtype, nl_addr_get_len(addr),
548 }
549 
550 /** @} */
551 
552 /**
553  * @name Integer Attributes
554  */
555 
556 /**
557  * Add 8 bit integer attribute to netlink message.
558  * @arg msg Netlink message.
559  * @arg attrtype Attribute type.
560  * @arg value Numeric value to store as payload.
561  *
562  * @see nla_put
563  * @return 0 on success or a negative error code.
564  */
565 int nla_put_u8(struct nl_msg *msg, int attrtype, uint8_t value)
566 {
567  return nla_put(msg, attrtype, sizeof(uint8_t), &value);
568 }
569 
570 /**
571  * Return value of 8 bit integer attribute.
572  * @arg nla 8 bit integer attribute
573  *
574  * @return Payload as 8 bit integer.
575  */
576 uint8_t nla_get_u8(struct nlattr *nla)
577 {
578  return *(uint8_t *) nla_data(nla);
579 }
580 
581 /**
582  * Add 16 bit integer attribute to netlink message.
583  * @arg msg Netlink message.
584  * @arg attrtype Attribute type.
585  * @arg value Numeric value to store as payload.
586  *
587  * @see nla_put
588  * @return 0 on success or a negative error code.
589  */
590 int nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value)
591 {
592  return nla_put(msg, attrtype, sizeof(uint16_t), &value);
593 }
594 
595 /**
596  * Return payload of 16 bit integer attribute.
597  * @arg nla 16 bit integer attribute
598  *
599  * @return Payload as 16 bit integer.
600  */
601 uint16_t nla_get_u16(struct nlattr *nla)
602 {
603  return *(uint16_t *) nla_data(nla);
604 }
605 
606 /**
607  * Add 32 bit integer attribute to netlink message.
608  * @arg msg Netlink message.
609  * @arg attrtype Attribute type.
610  * @arg value Numeric value to store as payload.
611  *
612  * @see nla_put
613  * @return 0 on success or a negative error code.
614  */
615 int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
616 {
617  return nla_put(msg, attrtype, sizeof(uint32_t), &value);
618 }
619 
620 /**
621  * Return payload of 32 bit integer attribute.
622  * @arg nla 32 bit integer attribute.
623  *
624  * @return Payload as 32 bit integer.
625  */
626 uint32_t nla_get_u32(struct nlattr *nla)
627 {
628  return *(uint32_t *) nla_data(nla);
629 }
630 
631 /**
632  * Add 64 bit integer attribute to netlink message.
633  * @arg msg Netlink message.
634  * @arg attrtype Attribute type.
635  * @arg value Numeric value to store as payload.
636  *
637  * @see nla_put
638  * @return 0 on success or a negative error code.
639  */
640 int nla_put_u64(struct nl_msg *msg, int attrtype, uint64_t value)
641 {
642  return nla_put(msg, attrtype, sizeof(uint64_t), &value);
643 }
644 
645 /**
646  * Return payload of u64 attribute
647  * @arg nla u64 netlink attribute
648  *
649  * @return Payload as 64 bit integer.
650  */
651 uint64_t nla_get_u64(struct nlattr *nla)
652 {
653  uint64_t tmp;
654 
655  nla_memcpy(&tmp, nla, sizeof(tmp));
656 
657  return tmp;
658 }
659 
660 /** @} */
661 
662 /**
663  * @name String Attribute
664  */
665 
666 /**
667  * Add string attribute to netlink message.
668  * @arg msg Netlink message.
669  * @arg attrtype Attribute type.
670  * @arg str NUL terminated string.
671  *
672  * @see nla_put
673  * @return 0 on success or a negative error code.
674  */
675 int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
676 {
677  return nla_put(msg, attrtype, strlen(str) + 1, str);
678 }
679 
680 /**
681  * Return payload of string attribute.
682  * @arg nla String attribute.
683  *
684  * @return Pointer to attribute payload.
685  */
686 char *nla_get_string(struct nlattr *nla)
687 {
688  return (char *) nla_data(nla);
689 }
690 
691 char *nla_strdup(struct nlattr *nla)
692 {
693  return strdup(nla_get_string(nla));
694 }
695 
696 /** @} */
697 
698 /**
699  * @name Flag Attribute
700  */
701 
702 /**
703  * Add flag netlink attribute to netlink message.
704  * @arg msg Netlink message.
705  * @arg attrtype Attribute type.
706  *
707  * @see nla_put
708  * @return 0 on success or a negative error code.
709  */
710 int nla_put_flag(struct nl_msg *msg, int attrtype)
711 {
712  return nla_put(msg, attrtype, 0, NULL);
713 }
714 
715 /**
716  * Return true if flag attribute is set.
717  * @arg nla Flag netlink attribute.
718  *
719  * @return True if flag is set, otherwise false.
720  */
721 int nla_get_flag(struct nlattr *nla)
722 {
723  return !!nla;
724 }
725 
726 /** @} */
727 
728 /**
729  * @name Microseconds Attribute
730  */
731 
732 /**
733  * Add a msecs netlink attribute to a netlink message
734  * @arg n netlink message
735  * @arg attrtype attribute type
736  * @arg msecs number of msecs
737  */
738 int nla_put_msecs(struct nl_msg *n, int attrtype, unsigned long msecs)
739 {
740  return nla_put_u64(n, attrtype, msecs);
741 }
742 
743 /**
744  * Return payload of msecs attribute
745  * @arg nla msecs netlink attribute
746  *
747  * @return the number of milliseconds.
748  */
749 unsigned long nla_get_msecs(struct nlattr *nla)
750 {
751  return nla_get_u64(nla);
752 }
753 
754 /** @} */
755 
756 /**
757  * @name Nested Attribute
758  */
759 
760 /**
761  * Add nested attributes to netlink message.
762  * @arg msg Netlink message.
763  * @arg attrtype Attribute type.
764  * @arg nested Message containing attributes to be nested.
765  *
766  * Takes the attributes found in the \a nested message and appends them
767  * to the message \a msg nested in a container of the type \a attrtype.
768  * The \a nested message may not have a family specific header.
769  *
770  * @see nla_put
771  * @return 0 on success or a negative error code.
772  */
773 int nla_put_nested(struct nl_msg *msg, int attrtype, struct nl_msg *nested)
774 {
775  NL_DBG(2, "msg %p: attr <> %d: adding msg %p as nested attribute\n",
776  msg, attrtype, nested);
777 
778  return nla_put(msg, attrtype, nlmsg_datalen(nested->nm_nlh),
779  nlmsg_data(nested->nm_nlh));
780 }
781 
782 
783 /**
784  * Start a new level of nested attributes.
785  * @arg msg Netlink message.
786  * @arg attrtype Attribute type of container.
787  *
788  * @return Pointer to container attribute.
789  */
790 struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
791 {
792  struct nlattr *start = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
793 
794  if (nla_put(msg, attrtype, 0, NULL) < 0)
795  return NULL;
796 
797  NL_DBG(2, "msg %p: attr <%p> %d: starting nesting\n",
798  msg, start, start->nla_type);
799 
800  return start;
801 }
802 
803 /**
804  * Finalize nesting of attributes.
805  * @arg msg Netlink message.
806  * @arg start Container attribute as returned from nla_nest_start().
807  *
808  * Corrects the container attribute header to include the appeneded attributes.
809  *
810  * @return 0
811  */
812 int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
813 {
814  size_t pad, len;
815 
816  len = (void *) nlmsg_tail(msg->nm_nlh) - (void *) start;
817 
818  if (len == NLA_HDRLEN) {
819  /*
820  * Kernel can't handle empty nested attributes, trim the
821  * attribute header again
822  */
823  msg->nm_nlh->nlmsg_len -= NLA_HDRLEN;
824  memset(nlmsg_tail(msg->nm_nlh), 0, NLA_HDRLEN);
825 
826  return 0;
827  }
828 
829  start->nla_len = len;
830 
831  pad = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) - msg->nm_nlh->nlmsg_len;
832  if (pad > 0) {
833  /*
834  * Data inside attribute does not end at a alignment boundry.
835  * Pad accordingly and accoun for the additional space in
836  * the message. nlmsg_reserve() may never fail in this situation,
837  * the allocate message buffer must be a multiple of NLMSG_ALIGNTO.
838  */
839  if (!nlmsg_reserve(msg, pad, 0))
840  BUG();
841 
842  NL_DBG(2, "msg %p: attr <%p> %d: added %zu bytes of padding\n",
843  msg, start, start->nla_type, pad);
844  }
845 
846  NL_DBG(2, "msg %p: attr <%p> %d: closing nesting, len=%u\n",
847  msg, start, start->nla_type, start->nla_len);
848 
849  return 0;
850 }
851 
852 /**
853  * Create attribute index based on nested attribute
854  * @arg tb Index array to be filled (maxtype+1 elements).
855  * @arg maxtype Maximum attribute type expected and accepted.
856  * @arg nla Nested Attribute.
857  * @arg policy Attribute validation policy.
858  *
859  * Feeds the stream of attributes nested into the specified attribute
860  * to nla_parse().
861  *
862  * @see nla_parse
863  * @return 0 on success or a negative error code.
864  */
865 int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
866  struct nla_policy *policy)
867 {
868  return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
869 }
870 
871 /** @} */
872 
873 /** @} */