attr.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * netlink/attr.h 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. #ifndef NETLINK_ATTR_H_
  12. #define NETLINK_ATTR_H_
  13. #include <netlink/netlink.h>
  14. #include <netlink/object.h>
  15. #include <netlink/addr.h>
  16. #include <netlink/data.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. struct nl_msg;
  21. /**
  22. * @name Basic Attribute Data Types
  23. * @{
  24. */
  25. /**
  26. * @ingroup attr
  27. * Basic attribute data types
  28. *
  29. * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
  30. */
  31. enum {
  32. NLA_UNSPEC, /**< Unspecified type, binary data chunk */
  33. NLA_U8, /**< 8 bit integer */
  34. NLA_U16, /**< 16 bit integer */
  35. NLA_U32, /**< 32 bit integer */
  36. NLA_U64, /**< 64 bit integer */
  37. NLA_STRING, /**< NUL terminated character string */
  38. NLA_FLAG, /**< Flag */
  39. NLA_MSECS, /**< Micro seconds (64bit) */
  40. NLA_NESTED, /**< Nested attributes */
  41. __NLA_TYPE_MAX,
  42. };
  43. #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
  44. /** @} */
  45. /**
  46. * @ingroup attr
  47. * Attribute validation policy.
  48. *
  49. * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
  50. */
  51. struct nla_policy {
  52. /** Type of attribute or NLA_UNSPEC */
  53. uint16_t type;
  54. /** Minimal length of payload required */
  55. uint16_t minlen;
  56. /** Maximal length of payload allowed */
  57. uint16_t maxlen;
  58. };
  59. /* Size calculations */
  60. extern int nla_attr_size(int payload);
  61. extern int nla_total_size(int payload);
  62. extern int nla_padlen(int payload);
  63. /* Attribute parsing */
  64. extern int nla_type(const struct nlattr *);
  65. extern void * nla_data(const struct nlattr *);
  66. extern int nla_len(const struct nlattr *);
  67. extern int nla_ok(const struct nlattr *, int);
  68. extern struct nlattr * nla_next(const struct nlattr *, int *);
  69. extern int nla_parse(struct nlattr **, int, struct nlattr *,
  70. int, struct nla_policy *);
  71. extern int nla_validate(struct nlattr *, int, int,
  72. struct nla_policy *);
  73. extern struct nlattr * nla_find(struct nlattr *, int, int);
  74. /* Helper Functions */
  75. extern int nla_memcpy(void *, struct nlattr *, int);
  76. extern size_t nla_strlcpy(char *, const struct nlattr *, size_t);
  77. extern int nla_memcmp(const struct nlattr *, const void *, size_t);
  78. extern int nla_strcmp(const struct nlattr *, const char *);
  79. /* Unspecific attribute */
  80. extern struct nlattr * nla_reserve(struct nl_msg *, int, int);
  81. extern int nla_put(struct nl_msg *, int, int, const void *);
  82. extern int nla_put_data(struct nl_msg *, int, struct nl_data *);
  83. extern int nla_put_addr(struct nl_msg *, int, struct nl_addr *);
  84. /* Integer attribute */
  85. extern uint8_t nla_get_u8(struct nlattr *);
  86. extern int nla_put_u8(struct nl_msg *, int, uint8_t);
  87. extern uint16_t nla_get_u16(struct nlattr *);
  88. extern int nla_put_u16(struct nl_msg *, int, uint16_t);
  89. extern uint32_t nla_get_u32(struct nlattr *);
  90. extern int nla_put_u32(struct nl_msg *, int, uint32_t);
  91. extern uint64_t nla_get_u64(struct nlattr *);
  92. extern int nla_put_u64(struct nl_msg *, int, uint64_t);
  93. /* String attribute */
  94. extern char * nla_get_string(struct nlattr *);
  95. extern char * nla_strdup(struct nlattr *);
  96. extern int nla_put_string(struct nl_msg *, int, const char *);
  97. /* Flag attribute */
  98. extern int nla_get_flag(struct nlattr *);
  99. extern int nla_put_flag(struct nl_msg *, int);
  100. /* Msec attribute */
  101. extern unsigned long nla_get_msecs(struct nlattr *);
  102. extern int nla_put_msecs(struct nl_msg *, int, unsigned long);
  103. /* Attribute nesting */
  104. extern int nla_put_nested(struct nl_msg *, int, struct nl_msg *);
  105. extern struct nlattr * nla_nest_start(struct nl_msg *, int);
  106. extern int nla_nest_end(struct nl_msg *, struct nlattr *);
  107. extern void nla_nest_cancel(struct nl_msg *, struct nlattr *);
  108. extern int nla_parse_nested(struct nlattr **, int, struct nlattr *,
  109. struct nla_policy *);
  110. extern int nla_is_nested(struct nlattr *);
  111. /**
  112. * @name Attribute Construction (Exception Based)
  113. * @{
  114. */
  115. /**
  116. * @ingroup attr
  117. * Add unspecific attribute to netlink message.
  118. * @arg msg Netlink message.
  119. * @arg attrtype Attribute type.
  120. * @arg attrlen Length of attribute payload.
  121. * @arg data Head of attribute payload.
  122. */
  123. #define NLA_PUT(msg, attrtype, attrlen, data) \
  124. do { \
  125. if (nla_put(msg, attrtype, attrlen, data) < 0) \
  126. goto nla_put_failure; \
  127. } while(0)
  128. /**
  129. * @ingroup attr
  130. * Add atomic type attribute to netlink message.
  131. * @arg msg Netlink message.
  132. * @arg type Atomic type.
  133. * @arg attrtype Attribute type.
  134. * @arg value Head of attribute payload.
  135. */
  136. #define NLA_PUT_TYPE(msg, type, attrtype, value) \
  137. do { \
  138. type __tmp = value; \
  139. NLA_PUT(msg, attrtype, sizeof(type), &__tmp); \
  140. } while(0)
  141. /**
  142. * Add 8 bit integer attribute to netlink message.
  143. * @arg msg Netlink message.
  144. * @arg attrtype Attribute type.
  145. * @arg value Numeric value.
  146. */
  147. #define NLA_PUT_U8(msg, attrtype, value) \
  148. NLA_PUT_TYPE(msg, uint8_t, attrtype, value)
  149. /**
  150. * Add 16 bit integer attribute to netlink message.
  151. * @arg msg Netlink message.
  152. * @arg attrtype Attribute type.
  153. * @arg value Numeric value.
  154. */
  155. #define NLA_PUT_U16(msg, attrtype, value) \
  156. NLA_PUT_TYPE(msg, uint16_t, attrtype, value)
  157. /**
  158. * Add 32 bit integer attribute to netlink message.
  159. * @arg msg Netlink message.
  160. * @arg attrtype Attribute type.
  161. * @arg value Numeric value.
  162. */
  163. #define NLA_PUT_U32(msg, attrtype, value) \
  164. NLA_PUT_TYPE(msg, uint32_t, attrtype, value)
  165. /**
  166. * Add 64 bit integer attribute to netlink message.
  167. * @arg msg Netlink message.
  168. * @arg attrtype Attribute type.
  169. * @arg value Numeric value.
  170. */
  171. #define NLA_PUT_U64(msg, attrtype, value) \
  172. NLA_PUT_TYPE(msg, uint64_t, attrtype, value)
  173. /**
  174. * Add string attribute to netlink message.
  175. * @arg msg Netlink message.
  176. * @arg attrtype Attribute type.
  177. * @arg value NUL terminated character string.
  178. */
  179. #define NLA_PUT_STRING(msg, attrtype, value) \
  180. NLA_PUT(msg, attrtype, strlen(value) + 1, value)
  181. /**
  182. * Add flag attribute to netlink message.
  183. * @arg msg Netlink message.
  184. * @arg attrtype Attribute type.
  185. */
  186. #define NLA_PUT_FLAG(msg, attrtype) \
  187. NLA_PUT(msg, attrtype, 0, NULL)
  188. /**
  189. * Add msecs attribute to netlink message.
  190. * @arg msg Netlink message.
  191. * @arg attrtype Attribute type.
  192. * @arg msecs Numeric value in micro seconds.
  193. */
  194. #define NLA_PUT_MSECS(msg, attrtype, msecs) \
  195. NLA_PUT_U64(msg, attrtype, msecs)
  196. /**
  197. * Add address attribute to netlink message.
  198. * @arg msg Netlink message.
  199. * @arg attrtype Attribute type.
  200. * @arg addr Abstract address object.
  201. */
  202. #define NLA_PUT_ADDR(msg, attrtype, addr) \
  203. NLA_PUT(msg, attrtype, nl_addr_get_len(addr), \
  204. nl_addr_get_binary_addr(addr))
  205. /**
  206. * Add abstract data attribute to netlink message.
  207. * @arg msg Netlink message.
  208. * @arg attrtype Attribute type.
  209. * @arg data Abstract data object.
  210. */
  211. #define NLA_PUT_DATA(msg, attrtype, data) \
  212. NLA_PUT(msg, attrtype, nl_data_get_size(data), \
  213. nl_data_get(data))
  214. /** @} */
  215. /**
  216. * @name Iterators
  217. * @{
  218. */
  219. /**
  220. * @ingroup attr
  221. * Iterate over a stream of attributes
  222. * @arg pos loop counter, set to current attribute
  223. * @arg head head of attribute stream
  224. * @arg len length of attribute stream
  225. * @arg rem initialized to len, holds bytes currently remaining in stream
  226. */
  227. #define nla_for_each_attr(pos, head, len, rem) \
  228. for (pos = head, rem = len; \
  229. nla_ok(pos, rem); \
  230. pos = nla_next(pos, &(rem)))
  231. /**
  232. * @ingroup attr
  233. * Iterate over a stream of nested attributes
  234. * @arg pos loop counter, set to current attribute
  235. * @arg nla attribute containing the nested attributes
  236. * @arg rem initialized to len, holds bytes currently remaining in stream
  237. */
  238. #define nla_for_each_nested(pos, nla, rem) \
  239. for (pos = nla_data(nla), rem = nla_len(nla); \
  240. nla_ok(pos, rem); \
  241. pos = nla_next(pos, &(rem)))
  242. /** @} */
  243. #ifdef __cplusplus
  244. }
  245. #endif
  246. #endif