28fac87c41322d4cc179d862992f3d5b6885981a
2 * netlink/attr.h Netlink Attributes
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
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
12 #ifndef NETLINK_ATTR_H_
13 #define NETLINK_ATTR_H_
15 #include <netlink/netlink.h>
16 #include <netlink/object.h>
17 #include <netlink/addr.h>
18 #include <netlink/data.h>
19 #include <netlink/msg.h>
28 * @name Basic Attribute Data Types
34 * Basic attribute data types
36 * See \ref attr_datatypes for more details.
39 NLA_UNSPEC
, /**< Unspecified type, binary data chunk */
40 NLA_U8
, /**< 8 bit integer */
41 NLA_U16
, /**< 16 bit integer */
42 NLA_U32
, /**< 32 bit integer */
43 NLA_U64
, /**< 64 bit integer */
44 NLA_STRING
, /**< NUL terminated character string */
45 NLA_FLAG
, /**< Flag */
46 NLA_MSECS
, /**< Micro seconds (64bit) */
47 NLA_NESTED
, /**< Nested attributes */
58 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
64 * Attribute validation policy.
66 * See \ref attr_datatypes for more details.
69 /** Type of attribute or NLA_UNSPEC */
72 /** Minimal length of payload required */
75 /** Maximal length of payload allowed */
79 /* Attribute parsing */
80 extern int nla_ok(const struct nlattr
*, int);
81 extern struct nlattr
* nla_next(const struct nlattr
*, int *);
82 extern int nla_parse(struct nlattr
**, int, struct nlattr
*,
83 int, struct nla_policy
*);
84 extern int nla_validate(struct nlattr
*, int, int,
86 extern struct nlattr
* nla_find(struct nlattr
*, int, int);
88 /* Unspecific attribute */
89 extern struct nlattr
* nla_reserve(struct nl_msg
*, int, int);
90 extern int nla_put(struct nl_msg
*, int, int, const void *);
93 * nlmsg_find_attr - find a specific attribute in a netlink message
94 * @arg nlh netlink message header
95 * @arg hdrlen length of familiy specific header
96 * @arg attrtype type of attribute to look for
98 * Returns the first attribute which matches the specified type.
100 static inline struct nlattr
*nlmsg_find_attr(struct nlmsghdr
*nlh
, int hdrlen
, int attrtype
)
102 return nla_find(nlmsg_attrdata(nlh
, hdrlen
),
103 nlmsg_attrlen(nlh
, hdrlen
), attrtype
);
108 * Return size of attribute whithout padding.
109 * @arg payload Payload length of attribute.
112 * <-------- nla_attr_size(payload) --------->
113 * +------------------+- - -+- - - - - - - - - +- - -+
114 * | Attribute Header | Pad | Payload | Pad |
115 * +------------------+- - -+- - - - - - - - - +- - -+
118 * @return Size of attribute in bytes without padding.
120 static inline int nla_attr_size(int payload
)
122 return NLA_HDRLEN
+ payload
;
126 * Return size of attribute including padding.
127 * @arg payload Payload length of attribute.
130 * <----------- nla_total_size(payload) ----------->
131 * +------------------+- - -+- - - - - - - - - +- - -+
132 * | Attribute Header | Pad | Payload | Pad |
133 * +------------------+- - -+- - - - - - - - - +- - -+
136 * @return Size of attribute in bytes.
138 static inline int nla_total_size(int payload
)
140 return NLA_ALIGN(nla_attr_size(payload
));
144 * Return length of padding at the tail of the attribute.
145 * @arg payload Payload length of attribute.
148 * +------------------+- - -+- - - - - - - - - +- - -+
149 * | Attribute Header | Pad | Payload | Pad |
150 * +------------------+- - -+- - - - - - - - - +- - -+
154 * @return Length of padding in bytes.
156 static inline int nla_padlen(int payload
)
158 return nla_total_size(payload
) - nla_attr_size(payload
);
162 * Return type of the attribute.
163 * @arg nla Attribute.
165 * @return Type of attribute.
167 static inline int nla_type(const struct nlattr
*nla
)
169 return nla
->nla_type
& NLA_TYPE_MASK
;
173 * Return pointer to the payload section.
174 * @arg nla Attribute.
176 * @return Pointer to start of payload section.
178 static inline void *nla_data(const struct nlattr
*nla
)
180 return (char *) nla
+ NLA_HDRLEN
;
184 * Return length of the payload .
187 * @return Length of payload in bytes.
189 static inline int nla_len(const struct nlattr
*nla
)
191 return nla
->nla_len
- NLA_HDRLEN
;
195 * Copy attribute payload to another memory area.
196 * @arg dest Pointer to destination memory area.
198 * @arg count Number of bytes to copy at most.
200 * Note: The number of bytes copied is limited by the length of
201 * the attribute payload.
203 * @return The number of bytes copied to dest.
205 static inline int nla_memcpy(void *dest
, struct nlattr
*src
, int count
)
212 minlen
= min_t(int, count
, nla_len(src
));
213 memcpy(dest
, nla_data(src
), minlen
);
220 * Add abstract data as unspecific attribute to netlink message.
221 * @arg msg Netlink message.
222 * @arg attrtype Attribute type.
223 * @arg data Abstract data object.
225 * Equivalent to nla_put() except that the length of the payload is
226 * derived from the abstract data object.
229 * @return 0 on success or a negative error code.
231 static inline int nla_put_data(struct nl_msg
*msg
, int attrtype
, struct nl_data
*data
)
233 return nla_put(msg
, attrtype
, nl_data_get_size(data
),
238 * Add abstract address as unspecific attribute to netlink message.
239 * @arg msg Netlink message.
240 * @arg attrtype Attribute type.
241 * @arg addr Abstract address object.
244 * @return 0 on success or a negative error code.
246 static inline int nla_put_addr(struct nl_msg
*msg
, int attrtype
, struct nl_addr
*addr
)
248 return nla_put(msg
, attrtype
, nl_addr_get_len(addr
),
249 nl_addr_get_binary_addr(addr
));
255 * @name Integer Attributes
259 * Add 8 bit signed integer attribute to netlink message.
260 * @arg msg Netlink message.
261 * @arg attrtype Attribute type.
262 * @arg value Numeric value to store as payload.
265 * @return 0 on success or a negative error code.
267 static inline int nla_put_s8(struct nl_msg
*msg
, int attrtype
, int8_t value
)
269 return nla_put(msg
, attrtype
, sizeof(int8_t), &value
);
273 * Return value of 8 bit signed integer attribute.
274 * @arg nla 8 bit integer attribute
276 * @return Payload as 8 bit integer.
278 static inline int8_t nla_get_s8(struct nlattr
*nla
)
280 return *(int8_t *) nla_data(nla
);
284 * Add 8 bit integer attribute to netlink message.
285 * @arg msg Netlink message.
286 * @arg attrtype Attribute type.
287 * @arg value Numeric value to store as payload.
290 * @return 0 on success or a negative error code.
292 static inline int nla_put_u8(struct nl_msg
*msg
, int attrtype
, uint8_t value
)
294 return nla_put(msg
, attrtype
, sizeof(uint8_t), &value
);
298 * Return value of 8 bit integer attribute.
299 * @arg nla 8 bit integer attribute
301 * @return Payload as 8 bit integer.
303 static inline uint8_t nla_get_u8(struct nlattr
*nla
)
305 return *(uint8_t *) nla_data(nla
);
309 * Add 16 bit signed integer attribute to netlink message.
310 * @arg msg Netlink message.
311 * @arg attrtype Attribute type.
312 * @arg value Numeric value to store as payload.
315 * @return 0 on success or a negative error code.
317 static inline int nla_put_s16(struct nl_msg
*msg
, int attrtype
, int16_t value
)
319 return nla_put(msg
, attrtype
, sizeof(int16_t), &value
);
323 * Return payload of 16 bit signed integer attribute.
324 * @arg nla 16 bit integer attribute
326 * @return Payload as 16 bit integer.
328 static inline int16_t nla_get_s16(struct nlattr
*nla
)
330 return *(int16_t *) nla_data(nla
);
334 * Add 16 bit integer attribute to netlink message.
335 * @arg msg Netlink message.
336 * @arg attrtype Attribute type.
337 * @arg value Numeric value to store as payload.
340 * @return 0 on success or a negative error code.
342 static inline int nla_put_u16(struct nl_msg
*msg
, int attrtype
, uint16_t value
)
344 return nla_put(msg
, attrtype
, sizeof(uint16_t), &value
);
348 * Return payload of 16 bit integer attribute.
349 * @arg nla 16 bit integer attribute
351 * @return Payload as 16 bit integer.
353 static inline uint16_t nla_get_u16(struct nlattr
*nla
)
355 return *(uint16_t *) nla_data(nla
);
359 * Add 32 bit signed integer attribute to netlink message.
360 * @arg msg Netlink message.
361 * @arg attrtype Attribute type.
362 * @arg value Numeric value to store as payload.
365 * @return 0 on success or a negative error code.
367 static inline int nla_put_s32(struct nl_msg
*msg
, int attrtype
, int32_t value
)
369 return nla_put(msg
, attrtype
, sizeof(int32_t), &value
);
373 * Return payload of 32 bit signed integer attribute.
374 * @arg nla 32 bit integer attribute.
376 * @return Payload as 32 bit integer.
378 static inline int32_t nla_get_s32(struct nlattr
*nla
)
380 return *(int32_t *) nla_data(nla
);
384 * Add 32 bit integer attribute to netlink message.
385 * @arg msg Netlink message.
386 * @arg attrtype Attribute type.
387 * @arg value Numeric value to store as payload.
390 * @return 0 on success or a negative error code.
392 static inline int nla_put_u32(struct nl_msg
*msg
, int attrtype
, uint32_t value
)
394 return nla_put(msg
, attrtype
, sizeof(uint32_t), &value
);
398 * Return payload of 32 bit integer attribute.
399 * @arg nla 32 bit integer attribute.
401 * @return Payload as 32 bit integer.
403 static inline uint32_t nla_get_u32(struct nlattr
*nla
)
405 return *(uint32_t *) nla_data(nla
);
409 * Add 64 bit signed integer attribute to netlink message.
410 * @arg msg Netlink message.
411 * @arg attrtype Attribute type.
412 * @arg value Numeric value to store as payload.
415 * @return 0 on success or a negative error code.
417 static inline int nla_put_s64(struct nl_msg
*msg
, int attrtype
, int64_t value
)
419 return nla_put(msg
, attrtype
, sizeof(int64_t), &value
);
423 * Return payload of s64 attribute
424 * @arg nla s64 netlink attribute
426 * @return Payload as 64 bit integer.
428 static inline int64_t nla_get_s64(struct nlattr
*nla
)
432 nla_memcpy(&tmp
, nla
, sizeof(tmp
));
438 * Add 64 bit integer attribute to netlink message.
439 * @arg msg Netlink message.
440 * @arg attrtype Attribute type.
441 * @arg value Numeric value to store as payload.
444 * @return 0 on success or a negative error code.
446 static inline int nla_put_u64(struct nl_msg
*msg
, int attrtype
, uint64_t value
)
448 return nla_put(msg
, attrtype
, sizeof(uint64_t), &value
);
452 * Return payload of u64 attribute
453 * @arg nla u64 netlink attribute
455 * @return Payload as 64 bit integer.
457 static inline uint64_t nla_get_u64(struct nlattr
*nla
)
461 nla_memcpy(&tmp
, nla
, sizeof(tmp
));
467 * Add string attribute to netlink message.
468 * @arg msg Netlink message.
469 * @arg attrtype Attribute type.
470 * @arg str NUL terminated string.
473 * @return 0 on success or a negative error code.
475 static inline int nla_put_string(struct nl_msg
*msg
, int attrtype
, const char *str
)
477 return nla_put(msg
, attrtype
, strlen(str
) + 1, str
);
481 * Return payload of string attribute.
482 * @arg nla String attribute.
484 * @return Pointer to attribute payload.
486 static inline char *nla_get_string(struct nlattr
*nla
)
488 return (char *) nla_data(nla
);
491 static inline char *nla_strdup(struct nlattr
*nla
)
493 return strdup(nla_get_string(nla
));
499 * @name Flag Attribute
503 * Add flag netlink attribute to netlink message.
504 * @arg msg Netlink message.
505 * @arg attrtype Attribute type.
508 * @return 0 on success or a negative error code.
510 static inline int nla_put_flag(struct nl_msg
*msg
, int attrtype
)
512 return nla_put(msg
, attrtype
, 0, NULL
);
516 * Return true if flag attribute is set.
517 * @arg nla Flag netlink attribute.
519 * @return True if flag is set, otherwise false.
521 static inline int nla_get_flag(struct nlattr
*nla
)
529 * @name Microseconds Attribute
533 * Add a msecs netlink attribute to a netlink message
534 * @arg n netlink message
535 * @arg attrtype attribute type
536 * @arg msecs number of msecs
538 static inline int nla_put_msecs(struct nl_msg
*n
, int attrtype
, unsigned long msecs
)
540 return nla_put_u64(n
, attrtype
, msecs
);
544 * Return payload of msecs attribute
545 * @arg nla msecs netlink attribute
547 * @return the number of milliseconds.
549 static inline unsigned long nla_get_msecs(struct nlattr
*nla
)
551 return nla_get_u64(nla
);
555 * Add nested attributes to netlink message.
556 * @arg msg Netlink message.
557 * @arg attrtype Attribute type.
558 * @arg nested Message containing attributes to be nested.
560 * Takes the attributes found in the \a nested message and appends them
561 * to the message \a msg nested in a container of the type \a attrtype.
562 * The \a nested message may not have a family specific header.
565 * @return 0 on success or a negative error code.
567 static inline int nla_put_nested(struct nl_msg
*msg
, int attrtype
, struct nl_msg
*nested
)
569 return nla_put(msg
, attrtype
, nlmsg_len(nested
->nm_nlh
),
570 nlmsg_data(nested
->nm_nlh
));
574 * Start a new level of nested attributes.
575 * @arg msg Netlink message.
576 * @arg attrtype Attribute type of container.
578 * @return Pointer to container attribute.
580 static inline struct nlattr
*nla_nest_start(struct nl_msg
*msg
, int attrtype
)
582 struct nlattr
*start
= (struct nlattr
*) nlmsg_tail(msg
->nm_nlh
);
584 if (nla_put(msg
, attrtype
| NLA_F_NESTED
, 0, NULL
) < 0)
591 * Finalize nesting of attributes.
592 * @arg msg Netlink message.
593 * @arg start Container attribute as returned from nla_nest_start().
595 * Corrects the container attribute header to include the appeneded attributes.
599 static inline int nla_nest_end(struct nl_msg
*msg
, struct nlattr
*start
)
601 start
->nla_len
= (unsigned char *) nlmsg_tail(msg
->nm_nlh
) -
602 (unsigned char *) start
;
607 * Create attribute index based on nested attribute
608 * @arg tb Index array to be filled (maxtype+1 elements).
609 * @arg maxtype Maximum attribute type expected and accepted.
610 * @arg nla Nested Attribute.
611 * @arg policy Attribute validation policy.
613 * Feeds the stream of attributes nested into the specified attribute
617 * @return 0 on success or a negative error code.
619 static inline int nla_parse_nested(struct nlattr
*tb
[], int maxtype
, struct nlattr
*nla
,
620 struct nla_policy
*policy
)
622 return nla_parse(tb
, maxtype
, (struct nlattr
*)nla_data(nla
), nla_len(nla
), policy
);
626 * Compare attribute payload with memory area.
627 * @arg nla Attribute.
628 * @arg data Memory area to compare to.
629 * @arg size Number of bytes to compare.
632 * @return An integer less than, equal to, or greater than zero.
634 static inline int nla_memcmp(const struct nlattr
*nla
, const void *data
, size_t size
)
636 int d
= nla_len(nla
) - size
;
639 d
= memcmp(nla_data(nla
), data
, size
);
645 * Compare string attribute payload with string
646 * @arg nla Attribute of type NLA_STRING.
647 * @arg str NUL terminated string.
650 * @return An integer less than, equal to, or greater than zero.
652 static inline int nla_strcmp(const struct nlattr
*nla
, const char *str
)
654 int len
= strlen(str
) + 1;
655 int d
= nla_len(nla
) - len
;
658 d
= memcmp(nla_data(nla
), str
, len
);
664 * Copy string attribute payload to a buffer.
665 * @arg dst Pointer to destination buffer.
666 * @arg nla Attribute of type NLA_STRING.
667 * @arg dstsize Size of destination buffer in bytes.
669 * Copies at most dstsize - 1 bytes to the destination buffer.
670 * The result is always a valid NUL terminated string. Unlike
671 * strlcpy the destination buffer is always padded out.
673 * @return The length of string attribute without the terminating NUL.
675 static inline size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
, size_t dstsize
)
677 size_t srclen
= (size_t)nla_len(nla
);
678 char *src
= (char*)nla_data(nla
);
680 if (srclen
> 0 && src
[srclen
- 1] == '\0')
684 size_t len
= (srclen
>= dstsize
) ? dstsize
- 1 : srclen
;
686 memset(dst
, 0, dstsize
);
687 memcpy(dst
, src
, len
);
695 * @name Attribute Construction (Exception Based)
701 * Add unspecific attribute to netlink message.
702 * @arg msg Netlink message.
703 * @arg attrtype Attribute type.
704 * @arg attrlen Length of attribute payload.
705 * @arg data Head of attribute payload.
707 #define NLA_PUT(msg, attrtype, attrlen, data) \
709 if (nla_put(msg, attrtype, attrlen, data) < 0) \
710 goto nla_put_failure; \
715 * Add atomic type attribute to netlink message.
716 * @arg msg Netlink message.
717 * @arg type Atomic type.
718 * @arg attrtype Attribute type.
719 * @arg value Head of attribute payload.
721 #define NLA_PUT_TYPE(msg, type, attrtype, value) \
723 type __tmp = value; \
724 NLA_PUT(msg, attrtype, sizeof(type), &__tmp); \
728 * Add 8 bit signed integer attribute to netlink message.
729 * @arg msg Netlink message.
730 * @arg attrtype Attribute type.
731 * @arg value Numeric value.
733 #define NLA_PUT_S8(msg, attrtype, value) \
734 NLA_PUT_TYPE(msg, int8_t, attrtype, value)
737 * Add 8 bit signed integer attribute to netlink message.
738 * @arg msg Netlink message.
739 * @arg attrtype Attribute type.
740 * @arg value Numeric value.
742 #define NLA_PUT_S8(msg, attrtype, value) \
743 NLA_PUT_TYPE(msg, int8_t, attrtype, value)
746 * Add 8 bit integer attribute to netlink message.
747 * @arg msg Netlink message.
748 * @arg attrtype Attribute type.
749 * @arg value Numeric value.
751 #define NLA_PUT_U8(msg, attrtype, value) \
752 NLA_PUT_TYPE(msg, uint8_t, attrtype, value)
755 * Add 16 bit signed integer attribute to netlink message.
756 * @arg msg Netlink message.
757 * @arg attrtype Attribute type.
758 * @arg value Numeric value.
760 #define NLA_PUT_S16(msg, attrtype, value) \
761 NLA_PUT_TYPE(msg, int16_t, attrtype, value)
764 * Add 16 bit integer attribute to netlink message.
765 * @arg msg Netlink message.
766 * @arg attrtype Attribute type.
767 * @arg value Numeric value.
769 #define NLA_PUT_U16(msg, attrtype, value) \
770 NLA_PUT_TYPE(msg, uint16_t, attrtype, value)
773 * Add 32 bit signed integer attribute to netlink message.
774 * @arg msg Netlink message.
775 * @arg attrtype Attribute type.
776 * @arg value Numeric value.
778 #define NLA_PUT_S32(msg, attrtype, value) \
779 NLA_PUT_TYPE(msg, int32_t, attrtype, value)
782 * Add 32 bit integer attribute to netlink message.
783 * @arg msg Netlink message.
784 * @arg attrtype Attribute type.
785 * @arg value Numeric value.
787 #define NLA_PUT_U32(msg, attrtype, value) \
788 NLA_PUT_TYPE(msg, uint32_t, attrtype, value)
791 * Add 64 bit signed integer attribute to netlink message.
792 * @arg msg Netlink message.
793 * @arg attrtype Attribute type.
794 * @arg value Numeric value.
796 #define NLA_PUT_S64(msg, attrtype, value) \
797 NLA_PUT_TYPE(msg, int64_t, attrtype, value)
800 * Add 64 bit integer attribute to netlink message.
801 * @arg msg Netlink message.
802 * @arg attrtype Attribute type.
803 * @arg value Numeric value.
805 #define NLA_PUT_U64(msg, attrtype, value) \
806 NLA_PUT_TYPE(msg, uint64_t, attrtype, value)
809 * Add string attribute to netlink message.
810 * @arg msg Netlink message.
811 * @arg attrtype Attribute type.
812 * @arg value NUL terminated character string.
814 #define NLA_PUT_STRING(msg, attrtype, value) \
815 NLA_PUT(msg, attrtype, strlen(value) + 1, value)
818 * Add flag attribute to netlink message.
819 * @arg msg Netlink message.
820 * @arg attrtype Attribute type.
822 #define NLA_PUT_FLAG(msg, attrtype) \
823 NLA_PUT(msg, attrtype, 0, NULL)
826 * Add msecs attribute to netlink message.
827 * @arg msg Netlink message.
828 * @arg attrtype Attribute type.
829 * @arg msecs Numeric value in micro seconds.
831 #define NLA_PUT_MSECS(msg, attrtype, msecs) \
832 NLA_PUT_U64(msg, attrtype, msecs)
835 * Add address attribute to netlink message.
836 * @arg msg Netlink message.
837 * @arg attrtype Attribute type.
838 * @arg addr Abstract address object.
840 #define NLA_PUT_ADDR(msg, attrtype, addr) \
841 NLA_PUT(msg, attrtype, nl_addr_get_len(addr), \
842 nl_addr_get_binary_addr(addr))
853 * Iterate over a stream of attributes
854 * @arg pos loop counter, set to current attribute
855 * @arg head head of attribute stream
856 * @arg len length of attribute stream
857 * @arg rem initialized to len, holds bytes currently remaining in stream
859 #define nla_for_each_attr(pos, head, len, rem) \
860 for (pos = head, rem = len; \
862 pos = nla_next(pos, &(rem)))
866 * Iterate over a stream of nested attributes
867 * @arg pos loop counter, set to current attribute
868 * @arg nla attribute containing the nested attributes
869 * @arg rem initialized to len, holds bytes currently remaining in stream
871 #define nla_for_each_nested(pos, nla, rem) \
872 for (pos = (struct nlattr *)nla_data(nla), rem = nla_len(nla); \
874 pos = nla_next(pos, &(rem)))