1
0

msg.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * netlink/msg.c Netlink Messages Interface
  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_MSG_H_
  12. #define NETLINK_MSG_H_
  13. #include <netlink/netlink.h>
  14. #include <netlink/object.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. struct nla_policy;
  19. #define NL_DONTPAD 0
  20. /**
  21. * @ingroup msg
  22. * @brief
  23. * Will cause the netlink pid to be set to the pid assigned to
  24. * the netlink handle (socket) just before sending the message off.
  25. * @note Requires the use of nl_send_auto_complete()!
  26. */
  27. #define NL_AUTO_PID 0
  28. /**
  29. * @ingroup msg
  30. * @brief
  31. * May be used to refer to a sequence number which should be
  32. * automatically set just before sending the message off.
  33. * @note Requires the use of nl_send_auto_complete()!
  34. */
  35. #define NL_AUTO_SEQ 0
  36. #define NL_MSG_CRED_PRESENT 1
  37. struct nl_msg
  38. {
  39. int nm_protocol;
  40. int nm_flags;
  41. struct sockaddr_nl nm_src;
  42. struct sockaddr_nl nm_dst;
  43. struct ucred nm_creds;
  44. struct nlmsghdr * nm_nlh;
  45. size_t nm_size;
  46. int nm_refcnt;
  47. };
  48. struct nl_msg;
  49. struct nl_tree;
  50. struct ucred;
  51. /* message parsing */
  52. extern int nlmsg_ok(const struct nlmsghdr *, int);
  53. extern struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *);
  54. extern int nlmsg_parse(struct nlmsghdr *, int, struct nlattr **,
  55. int, const struct nla_policy *);
  56. extern int nlmsg_validate(const struct nlmsghdr *, int, int,
  57. const struct nla_policy *);
  58. extern struct nl_msg * nlmsg_alloc(void);
  59. extern struct nl_msg * nlmsg_alloc_size(size_t);
  60. extern struct nl_msg * nlmsg_alloc_simple(int, int);
  61. extern void nlmsg_set_default_size(size_t);
  62. extern struct nl_msg * nlmsg_inherit(struct nlmsghdr *);
  63. extern struct nl_msg * nlmsg_convert(struct nlmsghdr *);
  64. extern void * nlmsg_reserve(struct nl_msg *, size_t, int);
  65. extern int nlmsg_append(struct nl_msg *, void *, size_t, int);
  66. extern struct nlmsghdr * nlmsg_put(struct nl_msg *, uint32_t, uint32_t,
  67. int, int, int);
  68. extern void nlmsg_free(struct nl_msg *);
  69. extern int nl_msg_parse(struct nl_msg *,
  70. void (*cb)(struct nl_object *, void *),
  71. void *);
  72. extern void nl_msg_dump(struct nl_msg *, FILE *);
  73. /**
  74. * length of netlink message not including padding
  75. * @arg payload length of message payload
  76. */
  77. static inline int nlmsg_msg_size(int payload)
  78. {
  79. return NLMSG_HDRLEN + payload;
  80. }
  81. /**
  82. * length of netlink message including padding
  83. * @arg payload length of message payload
  84. */
  85. static inline int nlmsg_total_size(int payload)
  86. {
  87. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  88. }
  89. /**
  90. * length of padding at the message's tail
  91. * @arg payload length of message payload
  92. */
  93. static inline int nlmsg_padlen(int payload)
  94. {
  95. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  96. }
  97. /**
  98. * head of message payload
  99. * @arg nlh netlink messsage header
  100. */
  101. static inline void *nlmsg_data(const struct nlmsghdr *nlh)
  102. {
  103. return (unsigned char *) nlh + NLMSG_HDRLEN;
  104. }
  105. static inline void *nlmsg_tail(const struct nlmsghdr *nlh)
  106. {
  107. return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
  108. }
  109. /**
  110. * length of message payload
  111. * @arg nlh netlink message header
  112. */
  113. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  114. {
  115. return nlh->nlmsg_len - NLMSG_HDRLEN;
  116. }
  117. /**
  118. * head of attributes data
  119. * @arg nlh netlink message header
  120. * @arg hdrlen length of family specific header
  121. */
  122. static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
  123. {
  124. unsigned char *data = (unsigned char*)nlmsg_data(nlh);
  125. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  126. }
  127. /**
  128. * length of attributes data
  129. * @arg nlh netlink message header
  130. * @arg hdrlen length of family specific header
  131. */
  132. static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  133. {
  134. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  135. }
  136. static inline int nlmsg_valid_hdr(const struct nlmsghdr *nlh, int hdrlen)
  137. {
  138. if (nlh->nlmsg_len < (uint)nlmsg_msg_size(hdrlen))
  139. return 0;
  140. return 1;
  141. }
  142. static inline void nlmsg_set_proto(struct nl_msg *msg, int protocol)
  143. {
  144. msg->nm_protocol = protocol;
  145. }
  146. static inline int nlmsg_get_proto(struct nl_msg *msg)
  147. {
  148. return msg->nm_protocol;
  149. }
  150. static inline size_t nlmsg_get_max_size(struct nl_msg *msg)
  151. {
  152. return msg->nm_size;
  153. }
  154. static inline void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
  155. {
  156. memcpy(&msg->nm_src, addr, sizeof(*addr));
  157. }
  158. static inline struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg)
  159. {
  160. return &msg->nm_src;
  161. }
  162. static inline void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr)
  163. {
  164. memcpy(&msg->nm_dst, addr, sizeof(*addr));
  165. }
  166. static inline struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg)
  167. {
  168. return &msg->nm_dst;
  169. }
  170. static inline void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
  171. {
  172. memcpy(&msg->nm_creds, creds, sizeof(*creds));
  173. msg->nm_flags |= NL_MSG_CRED_PRESENT;
  174. }
  175. static inline struct ucred *nlmsg_get_creds(struct nl_msg *msg)
  176. {
  177. if (msg->nm_flags & NL_MSG_CRED_PRESENT)
  178. return &msg->nm_creds;
  179. return NULL;
  180. }
  181. /**
  182. * Return actual netlink message
  183. * @arg n netlink message
  184. *
  185. * Returns the actual netlink message casted to the type of the netlink
  186. * message header.
  187. *
  188. * @return A pointer to the netlink message.
  189. */
  190. static inline struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
  191. {
  192. return n->nm_nlh;
  193. }
  194. /**
  195. * Acquire a reference on a netlink message
  196. * @arg msg message to acquire reference from
  197. */
  198. static inline void nlmsg_get(struct nl_msg *msg)
  199. {
  200. msg->nm_refcnt++;
  201. }
  202. /**
  203. * Expand maximum payload size of a netlink message
  204. * @arg n Netlink message.
  205. * @arg newlen New maximum payload size.
  206. *
  207. * Reallocates the payload section of a netlink message and increases
  208. * the maximum payload size of the message.
  209. *
  210. * @note Any pointers pointing to old payload block will be stale and
  211. * need to be refetched. Therfore, do not expand while constructing
  212. * nested attributes or while reserved data blocks are held.
  213. *
  214. * @return 0 on success or a negative error code.
  215. */
  216. static inline int nlmsg_expand(struct nl_msg *n, size_t newlen)
  217. {
  218. void *tmp;
  219. if (newlen <= n->nm_size)
  220. return -NLE_INVAL;
  221. tmp = realloc(n->nm_nlh, newlen);
  222. if (tmp == NULL)
  223. return -NLE_NOMEM;
  224. n->nm_nlh = (struct nlmsghdr*)tmp;
  225. n->nm_size = newlen;
  226. return 0;
  227. }
  228. /**
  229. * @name Iterators
  230. * @{
  231. */
  232. /**
  233. * @ingroup msg
  234. * Iterate over a stream of attributes in a message
  235. * @arg pos loop counter, set to current attribute
  236. * @arg nlh netlink message header
  237. * @arg hdrlen length of family header
  238. * @arg rem initialized to len, holds bytes currently remaining in stream
  239. */
  240. #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
  241. nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
  242. nlmsg_attrlen(nlh, hdrlen), rem)
  243. /**
  244. * Iterate over a stream of messages
  245. * @arg pos loop counter, set to current message
  246. * @arg head head of message stream
  247. * @arg len length of message stream
  248. * @arg rem initialized to len, holds bytes currently remaining in stream
  249. */
  250. #define nlmsg_for_each_msg(pos, head, len, rem) \
  251. for (pos = head, rem = len; \
  252. nlmsg_ok(pos, rem); \
  253. pos = nlmsg_next(pos, &(rem)))
  254. /** @} */
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #endif