genl.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * lib/genl/genl.c Generic Netlink
  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-2012 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @defgroup genl Generic Netlink Library (libnl-genl)
  13. *
  14. * @{
  15. */
  16. #include <netlink-private/genl.h>
  17. #include <netlink/netlink.h>
  18. #include <netlink/genl/genl.h>
  19. #include <netlink/utils.h>
  20. /**
  21. * @name Generic Netlink Socket
  22. * @{
  23. */
  24. /**
  25. * Connect a Generic Netlink socket
  26. * @arg sk Unconnected Netlink socket
  27. *
  28. * This function expects a struct nl_socket object previously allocated via
  29. * nl_socket_alloc(). It calls nl_connect() to create the local socket file
  30. * descriptor and binds the socket to the \c NETLINK_GENERIC Netlink protocol.
  31. *
  32. * Using this function is equivalent to:
  33. * @code
  34. * nl_connect(sk, NETLINK_GENERIC);
  35. * @endcode
  36. *
  37. * @see nl_connect()
  38. *
  39. * @return 0 on success or a negative error code.
  40. */
  41. int genl_connect(struct nl_sock *sk)
  42. {
  43. return nl_connect(sk, NETLINK_GENERIC);
  44. }
  45. /** @} */
  46. /**
  47. * @name Sending Data
  48. * @{
  49. */
  50. /**
  51. * Send a Generic Netlink message consisting only of a header
  52. * @arg sk Generic Netlink socket
  53. * @arg family Numeric family identifier
  54. * @arg cmd Numeric command identifier
  55. * @arg version Interface version
  56. * @arg flags Additional Netlink message flags (optional)
  57. *
  58. * This function is a shortcut for sending a Generic Netlink message without
  59. * any message payload. The message will only consist of the Netlink and
  60. * Generic Netlink headers. The header is constructed based on the specified
  61. * parameters and passed on to nl_send_simple() to send it on the specified
  62. * socket.
  63. *
  64. * @par Example:
  65. * @code
  66. * #include <netlink/genl/genl.h>
  67. * #include <linux/genetlink.h>
  68. *
  69. * err = genl_send_simple(sk, GENL_ID_CTRL, CTRL_CMD_GETFAMILY, CTRL_VERSION,
  70. * NLM_F_DUMP);
  71. * @endcode
  72. *
  73. * @see nl_send_simple()
  74. *
  75. * @return 0 on success or a negative error code.
  76. */
  77. int genl_send_simple(struct nl_sock *sk, int family, int cmd,
  78. int version, int flags)
  79. {
  80. struct genlmsghdr hdr = {
  81. .cmd = cmd,
  82. .version = version,
  83. };
  84. return nl_send_simple(sk, family, flags, &hdr, sizeof(hdr));
  85. }
  86. /** @} */
  87. /**
  88. * @name Message Parsing
  89. * @{
  90. */
  91. /**
  92. * Validate Generic Netlink message headers
  93. * @arg nlh Pointer to Netlink message header
  94. * @arg hdrlen Length of user header
  95. *
  96. * Verifies the integrity of the Netlink and Generic Netlink headers by
  97. * enforcing the following requirements:
  98. * - Valid Netlink message header (nlmsg_valid_hdr())
  99. * - Presence of a complete Generic Netlink header
  100. * - At least \c hdrlen bytes of payload included after the generic
  101. * netlink header.
  102. *
  103. * @return A positive integer (true) if the headers are valid or
  104. * 0 (false) if not.
  105. */
  106. int genlmsg_valid_hdr(struct nlmsghdr *nlh, int hdrlen)
  107. {
  108. struct genlmsghdr *ghdr;
  109. if (!nlmsg_valid_hdr(nlh, GENL_HDRLEN))
  110. return 0;
  111. ghdr = nlmsg_data(nlh);
  112. if (genlmsg_len(ghdr) < NLMSG_ALIGN(hdrlen))
  113. return 0;
  114. return 1;
  115. }
  116. /**
  117. * Validate Generic Netlink message including attributes
  118. * @arg nlh Pointer to Netlink message header
  119. * @arg hdrlen Length of user header
  120. * @arg maxtype Maximum attribtue id expected
  121. * @arg policy Attribute validation policy
  122. *
  123. * Verifies the validity of the Netlink and Generic Netlink headers using
  124. * genlmsg_valid_hdr() and calls nla_validate() on the message payload to
  125. * verify the integrity of eventual attributes.
  126. *
  127. * @note You may call genlmsg_parse() directly to perform validation and
  128. * parsing in a single step.
  129. *
  130. * @see genlmsg_valid_hdr()
  131. * @see nla_validate()
  132. * @see genlmsg_parse()
  133. *
  134. * @return 0 on success or a negative error code.
  135. */
  136. int genlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
  137. struct nla_policy *policy)
  138. {
  139. struct genlmsghdr *ghdr;
  140. if (!genlmsg_valid_hdr(nlh, hdrlen))
  141. return -NLE_MSG_TOOSHORT;
  142. ghdr = nlmsg_data(nlh);
  143. return nla_validate(genlmsg_attrdata(ghdr, hdrlen),
  144. genlmsg_attrlen(ghdr, hdrlen), maxtype, policy);
  145. }
  146. /**
  147. * Parse Generic Netlink message including attributes
  148. * @arg nlh Pointer to Netlink message header
  149. * @arg hdrlen Length of user header
  150. * @arg tb Array to store parsed attributes
  151. * @arg maxtype Maximum attribute id expected
  152. * @arg policy Attribute validation policy
  153. *
  154. * Verifies the validity of the Netlink and Generic Netlink headers using
  155. * genlmsg_valid_hdr() and calls nla_parse() on the message payload to
  156. * parse eventual attributes.
  157. *
  158. * @par Example:
  159. * @code
  160. * struct nlattr *attrs[MY_TYPE_MAX+1];
  161. *
  162. * if ((err = genlsmg_parse(nlmsg_nlh(msg), sizeof(struct my_hdr), attrs,
  163. * MY_TYPE_MAX, attr_policy)) < 0)
  164. * // ERROR
  165. * @endcode
  166. *
  167. * @see genlmsg_valid_hdr()
  168. * @see genlmsg_validate()
  169. * @see nla_parse()
  170. *
  171. * @return 0 on success or a negative error code.
  172. */
  173. int genlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
  174. int maxtype, struct nla_policy *policy)
  175. {
  176. struct genlmsghdr *ghdr;
  177. if (!genlmsg_valid_hdr(nlh, hdrlen))
  178. return -NLE_MSG_TOOSHORT;
  179. ghdr = nlmsg_data(nlh);
  180. return nla_parse(tb, maxtype, genlmsg_attrdata(ghdr, hdrlen),
  181. genlmsg_attrlen(ghdr, hdrlen), policy);
  182. }
  183. /**
  184. * Return pointer to Generic Netlink header
  185. * @arg nlh Netlink message header
  186. *
  187. * @return Pointer to Generic Netlink message header
  188. */
  189. struct genlmsghdr *genlmsg_hdr(struct nlmsghdr *nlh)
  190. {
  191. return nlmsg_data(nlh);
  192. }
  193. /**
  194. * Return length of message payload including user header
  195. * @arg gnlh Generic Netlink message header
  196. *
  197. * @see genlmsg_data()
  198. *
  199. * @return Length of user payload including an eventual user header in
  200. * number of bytes.
  201. */
  202. int genlmsg_len(const struct genlmsghdr *gnlh)
  203. {
  204. const struct nlmsghdr *nlh;
  205. nlh = (const struct nlmsghdr *)((const unsigned char *) gnlh - NLMSG_HDRLEN);
  206. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  207. }
  208. /**
  209. * Return pointer to user header
  210. * @arg gnlh Generic Netlink message header
  211. *
  212. * Calculates the pointer to the user header based on the pointer to
  213. * the Generic Netlink message header.
  214. *
  215. * @return Pointer to the user header
  216. */
  217. void *genlmsg_user_hdr(const struct genlmsghdr *gnlh)
  218. {
  219. return genlmsg_data(gnlh);
  220. }
  221. /**
  222. * Return pointer to user data
  223. * @arg gnlh Generic netlink message header
  224. * @arg hdrlen Length of user header
  225. *
  226. * Calculates the pointer to the user data based on the pointer to
  227. * the Generic Netlink message header.
  228. *
  229. * @see genlmsg_user_datalen()
  230. *
  231. * @return Pointer to the user data
  232. */
  233. void *genlmsg_user_data(const struct genlmsghdr *gnlh, const int hdrlen)
  234. {
  235. return genlmsg_user_hdr(gnlh) + NLMSG_ALIGN(hdrlen);
  236. }
  237. /**
  238. * Return length of user data
  239. * @arg gnlh Generic Netlink message header
  240. * @arg hdrlen Length of user header
  241. *
  242. * @see genlmsg_user_data()
  243. *
  244. * @return Length of user data in bytes
  245. */
  246. int genlmsg_user_datalen(const struct genlmsghdr *gnlh, const int hdrlen)
  247. {
  248. return genlmsg_len(gnlh) - NLMSG_ALIGN(hdrlen);
  249. }
  250. /**
  251. * Return pointer to message attributes
  252. * @arg gnlh Generic Netlink message header
  253. * @arg hdrlen Length of user header
  254. *
  255. * @see genlmsg_attrlen()
  256. *
  257. * @return Pointer to the start of the message's attributes section.
  258. */
  259. struct nlattr *genlmsg_attrdata(const struct genlmsghdr *gnlh, int hdrlen)
  260. {
  261. return genlmsg_user_data(gnlh, hdrlen);
  262. }
  263. /**
  264. * Return length of message attributes
  265. * @arg gnlh Generic Netlink message header
  266. * @arg hdrlen Length of user header
  267. *
  268. * @see genlmsg_attrdata()
  269. *
  270. * @return Length of the message section containing attributes in number
  271. * of bytes.
  272. */
  273. int genlmsg_attrlen(const struct genlmsghdr *gnlh, int hdrlen)
  274. {
  275. return genlmsg_len(gnlh) - NLMSG_ALIGN(hdrlen);
  276. }
  277. /** @} */
  278. /**
  279. * @name Message Construction
  280. * @{
  281. */
  282. /**
  283. * Add Generic Netlink headers to Netlink message
  284. * @arg msg Netlink message object
  285. * @arg port Netlink port or NL_AUTO_PORT
  286. * @arg seq Sequence number of message or NL_AUTO_SEQ
  287. * @arg family Numeric family identifier
  288. * @arg hdrlen Length of user header
  289. * @arg flags Additional Netlink message flags (optional)
  290. * @arg cmd Numeric command identifier
  291. * @arg version Interface version
  292. *
  293. * Calls nlmsg_put() on the specified message object to reserve space for
  294. * the Netlink header, the Generic Netlink header, and a user header of
  295. * specified length. Fills out the header fields with the specified
  296. * parameters.
  297. *
  298. * @par Example:
  299. * @code
  300. * struct nl_msg *msg;
  301. * struct my_hdr *user_hdr;
  302. *
  303. * if (!(msg = nlmsg_alloc()))
  304. * // ERROR
  305. *
  306. * user_hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, family_id,
  307. * sizeof(struct my_hdr), 0, MY_CMD_FOO, 0);
  308. * if (!user_hdr)
  309. * // ERROR
  310. * @endcode
  311. *
  312. * @see nlmsg_put()
  313. *
  314. * Returns Pointer to user header or NULL if an error occurred.
  315. */
  316. void *genlmsg_put(struct nl_msg *msg, uint32_t port, uint32_t seq, int family,
  317. int hdrlen, int flags, uint8_t cmd, uint8_t version)
  318. {
  319. struct nlmsghdr *nlh;
  320. struct genlmsghdr hdr = {
  321. .cmd = cmd,
  322. .version = version,
  323. };
  324. nlh = nlmsg_put(msg, port, seq, family, GENL_HDRLEN + hdrlen, flags);
  325. if (nlh == NULL)
  326. return NULL;
  327. memcpy(nlmsg_data(nlh), &hdr, sizeof(hdr));
  328. NL_DBG(2, "msg %p: Added generic netlink header cmd=%d version=%d\n",
  329. msg, cmd, version);
  330. return nlmsg_data(nlh) + GENL_HDRLEN;
  331. }
  332. /** @} */
  333. /**
  334. * @name Deprecated
  335. * @{
  336. */
  337. /**
  338. * Return pointer to message payload
  339. * @arg gnlh Generic Netlink message header
  340. *
  341. * @deprecated This function has been deprecated due to inability to specify
  342. * the length of the user header. Use genlmsg_user_hdr()
  343. * respectively genlmsg_user_data().
  344. *
  345. * @return Pointer to payload section
  346. */
  347. void *genlmsg_data(const struct genlmsghdr *gnlh)
  348. {
  349. return ((unsigned char *) gnlh + GENL_HDRLEN);
  350. }
  351. /** @} */
  352. /** @} */