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 */
51 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
57 * Attribute validation policy.
59 * See \ref attr_datatypes for more details.
62 /** Type of attribute or NLA_UNSPEC */
65 /** Minimal length of payload required */
68 /** Maximal length of payload allowed */
72 /* Attribute parsing */
73 extern int nla_ok(const struct nlattr *, int);
74 extern struct nlattr * nla_next(const struct nlattr *, int *);
75 extern int nla_parse(struct nlattr **, int, struct nlattr *,
76 int, struct nla_policy *);
77 extern int nla_validate(struct nlattr *, int, int,
79 extern struct nlattr * nla_find(struct nlattr *, int, int);
81 /* Unspecific attribute */
82 extern struct nlattr * nla_reserve(struct nl_msg *, int, int);
83 extern int nla_put(struct nl_msg *, int, int, const void *);
86 * nlmsg_find_attr - find a specific attribute in a netlink message
87 * @arg nlh netlink message header
88 * @arg hdrlen length of familiy specific header
89 * @arg attrtype type of attribute to look for
91 * Returns the first attribute which matches the specified type.
93 static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
95 return nla_find(nlmsg_attrdata(nlh, hdrlen),
96 nlmsg_attrlen(nlh, hdrlen), attrtype);
101 * Return size of attribute whithout padding.
102 * @arg payload Payload length of attribute.
105 * <-------- nla_attr_size(payload) --------->
106 * +------------------+- - -+- - - - - - - - - +- - -+
107 * | Attribute Header | Pad | Payload | Pad |
108 * +------------------+- - -+- - - - - - - - - +- - -+
111 * @return Size of attribute in bytes without padding.
113 static inline int nla_attr_size(int payload)
115 return NLA_HDRLEN + payload;
119 * Return size of attribute including padding.
120 * @arg payload Payload length of attribute.
123 * <----------- nla_total_size(payload) ----------->
124 * +------------------+- - -+- - - - - - - - - +- - -+
125 * | Attribute Header | Pad | Payload | Pad |
126 * +------------------+- - -+- - - - - - - - - +- - -+
129 * @return Size of attribute in bytes.
131 static inline int nla_total_size(int payload)
133 return NLA_ALIGN(nla_attr_size(payload));
137 * Return length of padding at the tail of the attribute.
138 * @arg payload Payload length of attribute.
141 * +------------------+- - -+- - - - - - - - - +- - -+
142 * | Attribute Header | Pad | Payload | Pad |
143 * +------------------+- - -+- - - - - - - - - +- - -+
147 * @return Length of padding in bytes.
149 static inline int nla_padlen(int payload)
151 return nla_total_size(payload) - nla_attr_size(payload);
155 * Return type of the attribute.
156 * @arg nla Attribute.
158 * @return Type of attribute.
160 static inline int nla_type(const struct nlattr *nla)
162 return nla->nla_type & NLA_TYPE_MASK;
166 * Return pointer to the payload section.
167 * @arg nla Attribute.
169 * @return Pointer to start of payload section.
171 static inline void *nla_data(const struct nlattr *nla)
173 return (char *) nla + NLA_HDRLEN;
177 * Return length of the payload .
180 * @return Length of payload in bytes.
182 static inline int nla_len(const struct nlattr *nla)
184 return nla->nla_len - NLA_HDRLEN;
188 * Copy attribute payload to another memory area.
189 * @arg dest Pointer to destination memory area.
191 * @arg count Number of bytes to copy at most.
193 * Note: The number of bytes copied is limited by the length of
194 * the attribute payload.
196 * @return The number of bytes copied to dest.
198 static inline int nla_memcpy(void *dest, struct nlattr *src, int count)
205 minlen = min_t(int, count, nla_len(src));
206 memcpy(dest, nla_data(src), minlen);
213 * Add abstract data as unspecific attribute to netlink message.
214 * @arg msg Netlink message.
215 * @arg attrtype Attribute type.
216 * @arg data Abstract data object.
218 * Equivalent to nla_put() except that the length of the payload is
219 * derived from the abstract data object.
222 * @return 0 on success or a negative error code.
224 static inline int nla_put_data(struct nl_msg *msg, int attrtype, struct nl_data *data)
226 return nla_put(msg, attrtype, nl_data_get_size(data),
231 * Add abstract address as unspecific attribute to netlink message.
232 * @arg msg Netlink message.
233 * @arg attrtype Attribute type.
234 * @arg addr Abstract address object.
237 * @return 0 on success or a negative error code.
239 static inline int nla_put_addr(struct nl_msg *msg, int attrtype, struct nl_addr *addr)
241 return nla_put(msg, attrtype, nl_addr_get_len(addr),
242 nl_addr_get_binary_addr(addr));
248 * @name Integer Attributes
252 * Add 8 bit integer attribute to netlink message.
253 * @arg msg Netlink message.
254 * @arg attrtype Attribute type.
255 * @arg value Numeric value to store as payload.
258 * @return 0 on success or a negative error code.
260 static inline int nla_put_u8(struct nl_msg *msg, int attrtype, uint8_t value)
262 return nla_put(msg, attrtype, sizeof(uint8_t), &value);
266 * Return value of 8 bit integer attribute.
267 * @arg nla 8 bit integer attribute
269 * @return Payload as 8 bit integer.
271 static inline uint8_t nla_get_u8(struct nlattr *nla)
273 return *(uint8_t *) nla_data(nla);
277 * Add 16 bit integer attribute to netlink message.
278 * @arg msg Netlink message.
279 * @arg attrtype Attribute type.
280 * @arg value Numeric value to store as payload.
283 * @return 0 on success or a negative error code.
285 static inline int nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value)
287 return nla_put(msg, attrtype, sizeof(uint16_t), &value);
291 * Return payload of 16 bit integer attribute.
292 * @arg nla 16 bit integer attribute
294 * @return Payload as 16 bit integer.
296 static inline uint16_t nla_get_u16(struct nlattr *nla)
298 return *(uint16_t *) nla_data(nla);
302 * Add 32 bit integer attribute to netlink message.
303 * @arg msg Netlink message.
304 * @arg attrtype Attribute type.
305 * @arg value Numeric value to store as payload.
308 * @return 0 on success or a negative error code.
310 static inline int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
312 return nla_put(msg, attrtype, sizeof(uint32_t), &value);
316 * Return payload of 32 bit integer attribute.
317 * @arg nla 32 bit integer attribute.
319 * @return Payload as 32 bit integer.
321 static inline uint32_t nla_get_u32(struct nlattr *nla)
323 return *(uint32_t *) nla_data(nla);
327 * Add 64 bit integer attribute to netlink message.
328 * @arg msg Netlink message.
329 * @arg attrtype Attribute type.
330 * @arg value Numeric value to store as payload.
333 * @return 0 on success or a negative error code.
335 static inline int nla_put_u64(struct nl_msg *msg, int attrtype, uint64_t value)
337 return nla_put(msg, attrtype, sizeof(uint64_t), &value);
341 * Return payload of u64 attribute
342 * @arg nla u64 netlink attribute
344 * @return Payload as 64 bit integer.
346 static inline uint64_t nla_get_u64(struct nlattr *nla)
350 nla_memcpy(&tmp, nla, sizeof(tmp));
356 * Add string attribute to netlink message.
357 * @arg msg Netlink message.
358 * @arg attrtype Attribute type.
359 * @arg str NUL terminated string.
362 * @return 0 on success or a negative error code.
364 static inline int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
366 return nla_put(msg, attrtype, strlen(str) + 1, str);
370 * Return payload of string attribute.
371 * @arg nla String attribute.
373 * @return Pointer to attribute payload.
375 static inline char *nla_get_string(struct nlattr *nla)
377 return (char *) nla_data(nla);
380 static inline char *nla_strdup(struct nlattr *nla)
382 return strdup(nla_get_string(nla));
388 * @name Flag Attribute
392 * Add flag netlink attribute to netlink message.
393 * @arg msg Netlink message.
394 * @arg attrtype Attribute type.
397 * @return 0 on success or a negative error code.
399 static inline int nla_put_flag(struct nl_msg *msg, int attrtype)
401 return nla_put(msg, attrtype, 0, NULL);
405 * Return true if flag attribute is set.
406 * @arg nla Flag netlink attribute.
408 * @return True if flag is set, otherwise false.
410 static inline int nla_get_flag(struct nlattr *nla)
418 * @name Microseconds Attribute
422 * Add a msecs netlink attribute to a netlink message
423 * @arg n netlink message
424 * @arg attrtype attribute type
425 * @arg msecs number of msecs
427 static inline int nla_put_msecs(struct nl_msg *n, int attrtype, unsigned long msecs)
429 return nla_put_u64(n, attrtype, msecs);
433 * Return payload of msecs attribute
434 * @arg nla msecs netlink attribute
436 * @return the number of milliseconds.
438 static inline unsigned long nla_get_msecs(struct nlattr *nla)
440 return nla_get_u64(nla);
444 * Add nested attributes to netlink message.
445 * @arg msg Netlink message.
446 * @arg attrtype Attribute type.
447 * @arg nested Message containing attributes to be nested.
449 * Takes the attributes found in the \a nested message and appends them
450 * to the message \a msg nested in a container of the type \a attrtype.
451 * The \a nested message may not have a family specific header.
454 * @return 0 on success or a negative error code.
456 static inline int nla_put_nested(struct nl_msg *msg, int attrtype, struct nl_msg *nested)
458 return nla_put(msg, attrtype, nlmsg_len(nested->nm_nlh),
459 nlmsg_data(nested->nm_nlh));
463 * Start a new level of nested attributes.
464 * @arg msg Netlink message.
465 * @arg attrtype Attribute type of container.
467 * @return Pointer to container attribute.
469 static inline struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
471 struct nlattr *start = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
473 if (nla_put(msg, attrtype, 0, NULL) < 0)
480 * Finalize nesting of attributes.
481 * @arg msg Netlink message.
482 * @arg start Container attribute as returned from nla_nest_start().
484 * Corrects the container attribute header to include the appeneded attributes.
488 static inline int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
490 start->nla_len = (unsigned char *) nlmsg_tail(msg->nm_nlh) -
491 (unsigned char *) start;
496 * Create attribute index based on nested attribute
497 * @arg tb Index array to be filled (maxtype+1 elements).
498 * @arg maxtype Maximum attribute type expected and accepted.
499 * @arg nla Nested Attribute.
500 * @arg policy Attribute validation policy.
502 * Feeds the stream of attributes nested into the specified attribute
506 * @return 0 on success or a negative error code.
508 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
509 struct nla_policy *policy)
511 return nla_parse(tb, maxtype, (struct nlattr *)nla_data(nla), nla_len(nla), policy);
515 * Compare attribute payload with memory area.
516 * @arg nla Attribute.
517 * @arg data Memory area to compare to.
518 * @arg size Number of bytes to compare.
521 * @return An integer less than, equal to, or greater than zero.
523 static inline int nla_memcmp(const struct nlattr *nla, const void *data, size_t size)
525 int d = nla_len(nla) - size;
528 d = memcmp(nla_data(nla), data, size);
534 * Compare string attribute payload with string
535 * @arg nla Attribute of type NLA_STRING.
536 * @arg str NUL terminated string.
539 * @return An integer less than, equal to, or greater than zero.
541 static inline int nla_strcmp(const struct nlattr *nla, const char *str)
543 int len = strlen(str) + 1;
544 int d = nla_len(nla) - len;
547 d = memcmp(nla_data(nla), str, len);
553 * Copy string attribute payload to a buffer.
554 * @arg dst Pointer to destination buffer.
555 * @arg nla Attribute of type NLA_STRING.
556 * @arg dstsize Size of destination buffer in bytes.
558 * Copies at most dstsize - 1 bytes to the destination buffer.
559 * The result is always a valid NUL terminated string. Unlike
560 * strlcpy the destination buffer is always padded out.
562 * @return The length of string attribute without the terminating NUL.
564 static inline size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
566 size_t srclen = (size_t)nla_len(nla);
567 char *src = (char*)nla_data(nla);
569 if (srclen > 0 && src[srclen - 1] == '\0')
573 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
575 memset(dst, 0, dstsize);
576 memcpy(dst, src, len);
584 * @name Attribute Construction (Exception Based)
590 * Add unspecific attribute to netlink message.
591 * @arg msg Netlink message.
592 * @arg attrtype Attribute type.
593 * @arg attrlen Length of attribute payload.
594 * @arg data Head of attribute payload.
596 #define NLA_PUT(msg, attrtype, attrlen, data) \
598 if (nla_put(msg, attrtype, attrlen, data) < 0) \
599 goto nla_put_failure; \
604 * Add atomic type attribute to netlink message.
605 * @arg msg Netlink message.
606 * @arg type Atomic type.
607 * @arg attrtype Attribute type.
608 * @arg value Head of attribute payload.
610 #define NLA_PUT_TYPE(msg, type, attrtype, value) \
612 type __tmp = value; \
613 NLA_PUT(msg, attrtype, sizeof(type), &__tmp); \
617 * Add 8 bit integer attribute to netlink message.
618 * @arg msg Netlink message.
619 * @arg attrtype Attribute type.
620 * @arg value Numeric value.
622 #define NLA_PUT_U8(msg, attrtype, value) \
623 NLA_PUT_TYPE(msg, uint8_t, attrtype, value)
626 * Add 16 bit integer attribute to netlink message.
627 * @arg msg Netlink message.
628 * @arg attrtype Attribute type.
629 * @arg value Numeric value.
631 #define NLA_PUT_U16(msg, attrtype, value) \
632 NLA_PUT_TYPE(msg, uint16_t, attrtype, value)
635 * Add 32 bit integer attribute to netlink message.
636 * @arg msg Netlink message.
637 * @arg attrtype Attribute type.
638 * @arg value Numeric value.
640 #define NLA_PUT_U32(msg, attrtype, value) \
641 NLA_PUT_TYPE(msg, uint32_t, attrtype, value)
644 * Add 64 bit integer attribute to netlink message.
645 * @arg msg Netlink message.
646 * @arg attrtype Attribute type.
647 * @arg value Numeric value.
649 #define NLA_PUT_U64(msg, attrtype, value) \
650 NLA_PUT_TYPE(msg, uint64_t, attrtype, value)
653 * Add string attribute to netlink message.
654 * @arg msg Netlink message.
655 * @arg attrtype Attribute type.
656 * @arg value NUL terminated character string.
658 #define NLA_PUT_STRING(msg, attrtype, value) \
659 NLA_PUT(msg, attrtype, strlen(value) + 1, value)
662 * Add flag attribute to netlink message.
663 * @arg msg Netlink message.
664 * @arg attrtype Attribute type.
666 #define NLA_PUT_FLAG(msg, attrtype) \
667 NLA_PUT(msg, attrtype, 0, NULL)
670 * Add msecs attribute to netlink message.
671 * @arg msg Netlink message.
672 * @arg attrtype Attribute type.
673 * @arg msecs Numeric value in micro seconds.
675 #define NLA_PUT_MSECS(msg, attrtype, msecs) \
676 NLA_PUT_U64(msg, attrtype, msecs)
679 * Add address attribute to netlink message.
680 * @arg msg Netlink message.
681 * @arg attrtype Attribute type.
682 * @arg addr Abstract address object.
684 #define NLA_PUT_ADDR(msg, attrtype, addr) \
685 NLA_PUT(msg, attrtype, nl_addr_get_len(addr), \
686 nl_addr_get_binary_addr(addr))
697 * Iterate over a stream of attributes
698 * @arg pos loop counter, set to current attribute
699 * @arg head head of attribute stream
700 * @arg len length of attribute stream
701 * @arg rem initialized to len, holds bytes currently remaining in stream
703 #define nla_for_each_attr(pos, head, len, rem) \
704 for (pos = head, rem = len; \
706 pos = nla_next(pos, &(rem)))
710 * Iterate over a stream of nested attributes
711 * @arg pos loop counter, set to current attribute
712 * @arg nla attribute containing the nested attributes
713 * @arg rem initialized to len, holds bytes currently remaining in stream
715 #define nla_for_each_nested(pos, nla, rem) \
716 for (pos = (struct nlattr *)nla_data(nla), rem = nla_len(nla); \
718 pos = nla_next(pos, &(rem)))