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