attr.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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-2006 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 Validation Policy Types
  23. * @{
  24. */
  25. /**
  26. * @ingroup attr
  27. * Standard attribute types to specify validation policy
  28. */
  29. enum {
  30. NLA_UNSPEC, /**< Unspecified type */
  31. NLA_U8, /**< 8bit integer */
  32. NLA_U16, /**< 16bit integer */
  33. NLA_U32, /**< 32bit integer */
  34. NLA_U64, /**< 64bit integer */
  35. NLA_STRING, /**< character string */
  36. NLA_FLAG, /**< flag */
  37. NLA_MSECS, /**< micro seconds (64bit) */
  38. NLA_NESTED, /**< nested attributes */
  39. __NLA_TYPE_MAX,
  40. };
  41. /**
  42. * @ingroup attr
  43. * Maximum netlink validation policy type
  44. */
  45. #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
  46. /** @} */
  47. /**
  48. * @ingroup attr
  49. * attribute validation policy
  50. *
  51. * Policies are defined as arrays of this struct, the array must
  52. * be accessible by attribute type up to the highest identifier
  53. * to be expected.
  54. *
  55. * Example:
  56. * @code
  57. * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
  58. * [ATTR_FOO] = { .type = NLA_U16 },
  59. * [ATTR_BAR] = { .type = NLA_STRING },
  60. * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) },
  61. * };
  62. * @endcode
  63. */
  64. struct nla_policy {
  65. /** Type of attribute or NLA_UNSPEC */
  66. uint16_t type;
  67. /** Minimal length of payload required to be available */
  68. uint16_t minlen;
  69. /** Maximal length of payload required to be available */
  70. uint16_t maxlen;
  71. };
  72. /* size calculations */
  73. extern int nla_attr_size(int payload);
  74. extern int nla_total_size(int payload);
  75. extern int nla_padlen(int payload);
  76. /* payload access */
  77. extern int nla_type(const struct nlattr *);
  78. extern void * nla_data(const struct nlattr *);
  79. extern int nla_len(const struct nlattr *);
  80. /* attribute parsing */
  81. extern int nla_ok(const struct nlattr *, int);
  82. extern struct nlattr * nla_next(const struct nlattr *, int *);
  83. extern int nla_parse(struct nlattr **, int, struct nlattr *,
  84. int, struct nla_policy *);
  85. extern int nla_parse_nested(struct nlattr **, int, struct nlattr *,
  86. struct nla_policy *);
  87. extern int nla_validate(struct nlattr *, int, int,
  88. struct nla_policy *);
  89. extern struct nlattr * nla_find(struct nlattr *, int, int);
  90. /* utilities */
  91. extern int nla_memcpy(void *, struct nlattr *, int);
  92. extern size_t nla_strlcpy(char *, const struct nlattr *, size_t);
  93. extern int nla_memcmp(const struct nlattr *, const void *, size_t);
  94. extern int nla_strcmp(const struct nlattr *, const char *);
  95. /* attribute construction */
  96. extern struct nlattr * nla_reserve(struct nl_msg *, int, int);
  97. extern int nla_put(struct nl_msg *, int, int, const void *);
  98. extern int nla_put_nested(struct nl_msg *, int, struct nl_msg *);
  99. extern int nla_put_u8(struct nl_msg *, int, uint8_t);
  100. extern int nla_put_u16(struct nl_msg *, int, uint16_t);
  101. extern int nla_put_u32(struct nl_msg *, int, uint32_t);
  102. extern int nla_put_u64(struct nl_msg *, int, uint64_t);
  103. extern int nla_put_string(struct nl_msg *, int, const char *);
  104. extern int nla_put_flag(struct nl_msg *, int);
  105. extern int nla_put_msecs(struct nl_msg *, int, unsigned long);
  106. extern int nla_put_data(struct nl_msg *, int, struct nl_data *);
  107. extern int nla_put_addr(struct nl_msg *, int, struct nl_addr *);
  108. /* attribute nesting */
  109. extern struct nlattr * nla_nest_start(struct nl_msg *, int);
  110. extern int nla_nest_end(struct nl_msg *, struct nlattr *);
  111. /* attribute reading */
  112. extern uint8_t nla_get_u8(struct nlattr *);
  113. extern uint16_t nla_get_u16(struct nlattr *);
  114. extern uint32_t nla_get_u32(struct nlattr *);
  115. extern uint64_t nla_get_u64(struct nlattr *);
  116. extern char * nla_get_string(struct nlattr *);
  117. extern int nla_get_flag(struct nlattr *);
  118. extern unsigned long nla_get_msecs(struct nlattr *);
  119. extern struct nl_data * nla_get_data(struct nlattr *);
  120. extern struct nl_addr * nla_get_addr(struct nlattr *, int);
  121. /**
  122. * @name Attribute Construction (Exception Based)
  123. *
  124. * All these functions jump to nla_put_failure in case of a failure
  125. * instead of returning an error code.
  126. *
  127. * @{
  128. */
  129. /**
  130. * @ingroup attr
  131. * Add a netlink attribute to a netlink message
  132. * @arg n netlink message
  133. * @arg attrtype attribute type
  134. * @arg attrlen length of attribute payload
  135. * @arg data head of attribute payload
  136. */
  137. #define NLA_PUT(n, attrtype, attrlen, data) \
  138. do { \
  139. if (nla_put(n, attrtype, attrlen, data) < 0) \
  140. goto nla_put_failure; \
  141. } while(0)
  142. /**
  143. * @ingroup attr
  144. * Add a basic netlink attribute to a netlink message
  145. * @arg n netlink message
  146. * @arg type atomic type
  147. * @arg attrtype attribute type
  148. * @arg value head of attribute payload
  149. */
  150. #define NLA_PUT_TYPE(n, type, attrtype, value) \
  151. do { \
  152. type __tmp = value; \
  153. NLA_PUT(n, attrtype, sizeof(type), &__tmp); \
  154. } while(0)
  155. /**
  156. * Add a u8 netlink attribute to a netlink message
  157. * @arg n netlink message
  158. * @arg attrtype attribute type
  159. * @arg value numeric value
  160. */
  161. #define NLA_PUT_U8(n, attrtype, value) \
  162. NLA_PUT_TYPE(n, uint8_t, attrtype, value)
  163. /**
  164. * Add a u16 netlink attribute to a netlink message
  165. * @arg n netlink message
  166. * @arg attrtype attribute type
  167. * @arg value numeric value
  168. */
  169. #define NLA_PUT_U16(n, attrtype, value) \
  170. NLA_PUT_TYPE(n, uint16_t, attrtype, value)
  171. /**
  172. * Add a u32 netlink attribute to a netlink message
  173. * @arg n netlink message
  174. * @arg attrtype attribute type
  175. * @arg value numeric value
  176. */
  177. #define NLA_PUT_U32(n, attrtype, value) \
  178. NLA_PUT_TYPE(n, uint32_t, attrtype, value)
  179. /**
  180. * Add a u64 netlink attribute to a netlink message
  181. * @arg n netlink message
  182. * @arg attrtype attribute type
  183. * @arg value numeric value
  184. */
  185. #define NLA_PUT_U64(n, attrtype, value) \
  186. NLA_PUT_TYPE(n, uint64_t, attrtype, value)
  187. /**
  188. * Add a character string netlink attribute to a netlink message
  189. * @arg n netlink message
  190. * @arg attrtype attribute type
  191. * @arg value character string
  192. */
  193. #define NLA_PUT_STRING(n, attrtype, value) \
  194. NLA_PUT(n, attrtype, strlen(value) + 1, value)
  195. /**
  196. * Add a flag netlink attribute to a netlink message
  197. * @arg n netlink message
  198. * @arg attrtype attribute type
  199. */
  200. #define NLA_PUT_FLAG(n, attrtype) \
  201. NLA_PUT(n, attrtype, 0, NULL)
  202. /**
  203. * Add a msecs netlink attribute to a netlink message
  204. * @arg n netlink message
  205. * @arg attrtype attribute type
  206. * @arg msecs numeric value in micro seconds
  207. */
  208. #define NLA_PUT_MSECS(n, attrtype, msecs) \
  209. NLA_PUT_U64(n, attrtype, msecs)
  210. /**
  211. * Add a address attribute to a netlink message
  212. * @arg n netlink message
  213. * @arg attrtype attribute type
  214. * @arg addr abstract address object
  215. */
  216. #define NLA_PUT_ADDR(n, attrtype, addr) \
  217. NLA_PUT(n, attrtype, nl_addr_get_len(addr), \
  218. nl_addr_get_binary_addr(addr))
  219. /** @} */
  220. /**
  221. * @name Iterators
  222. * @{
  223. */
  224. /**
  225. * @ingroup attr
  226. * iterate over a stream of attributes
  227. * @arg pos loop counter, set to current attribute
  228. * @arg head head of attribute stream
  229. * @arg len length of attribute stream
  230. * @arg rem initialized to len, holds bytes currently remaining in stream
  231. */
  232. #define nla_for_each_attr(pos, head, len, rem) \
  233. for (pos = head, rem = len; \
  234. nla_ok(pos, rem); \
  235. pos = nla_next(pos, &(rem)))
  236. /**
  237. * @ingroup attr
  238. * iterate over a stream of nested attributes
  239. * @arg pos loop counter, set to current attribute
  240. * @arg nla attribute containing the nested attributes
  241. * @arg rem initialized to len, holds bytes currently remaining in stream
  242. */
  243. #define nla_for_each_nested(pos, nla, rem) \
  244. for (pos = nla_data(nla), rem = nla_len(nla); \
  245. nla_ok(pos, rem); \
  246. pos = nla_next(pos, &(rem)))
  247. /** @} */
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. #endif