msg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * lib/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-2008 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup core
  13. * @defgroup msg Messages
  14. * Netlink Message Construction/Parsing Interface
  15. *
  16. * The following information is partly extracted from RFC3549
  17. * (ftp://ftp.rfc-editor.org/in-notes/rfc3549.txt)
  18. *
  19. * @par Message Format
  20. * Netlink messages consist of a byte stream with one or multiple
  21. * Netlink headers and an associated payload. If the payload is too big
  22. * to fit into a single message it, can be split over multiple Netlink
  23. * messages, collectively called a multipart message. For multipart
  24. * messages, the first and all following headers have the \c NLM_F_MULTI
  25. * Netlink header flag set, except for the last header which has the
  26. * Netlink header type \c NLMSG_DONE.
  27. *
  28. * @par
  29. * The Netlink message header (\link nlmsghdr struct nlmsghdr\endlink) is shown below.
  30. * @code
  31. * 0 1 2 3
  32. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  33. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  34. * | Length |
  35. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  36. * | Type | Flags |
  37. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  38. * | Sequence Number |
  39. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  40. * | Process ID (PID) |
  41. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  42. * @endcode
  43. *
  44. * @par
  45. * The netlink message header and payload must be aligned properly:
  46. * @code
  47. * <------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
  48. * +----------------------------+- - -+- - - - - - - - - - -+- - -+
  49. * | Header | Pad | Payload | Pad |
  50. * | struct nlmsghdr | | | |
  51. * +----------------------------+- - -+- - - - - - - - - - -+- - -+
  52. * @endcode
  53. * @par
  54. * Message Format:
  55. * @code
  56. * <--- nlmsg_total_size(payload) --->
  57. * <-- nlmsg_msg_size(payload) ->
  58. * +----------+- - -+-------------+- - -+-------- - -
  59. * | nlmsghdr | Pad | Payload | Pad | nlmsghdr
  60. * +----------+- - -+-------------+- - -+-------- - -
  61. * nlmsg_data(nlh)---^ ^
  62. * nlmsg_next(nlh)-----------------------+
  63. * @endcode
  64. * @par
  65. * The payload may consist of arbitary data but may have strict
  66. * alignment and formatting rules depening on the specific netlink
  67. * families.
  68. * @par
  69. * @code
  70. * <---------------------- nlmsg_len(nlh) --------------------->
  71. * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
  72. * +----------------------+- - -+--------------------------------+
  73. * | Family Header | Pad | Attributes |
  74. * +----------------------+- - -+--------------------------------+
  75. * nlmsg_attrdata(nlh, hdrlen)---^
  76. * @endcode
  77. * @par The ACK Netlink Message
  78. * This message is actually used to denote both an ACK and a NACK.
  79. * Typically, the direction is from FEC to CPC (in response to an ACK
  80. * request message). However, the CPC should be able to send ACKs back
  81. * to FEC when requested.
  82. * @code
  83. * 0 1 2 3
  84. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  85. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  86. * | Netlink message header |
  87. * | type = NLMSG_ERROR |
  88. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  89. * | Error code |
  90. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  91. * | OLD Netlink message header |
  92. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  93. * @endcode
  94. *
  95. * @par Example
  96. * @code
  97. * // Various methods exist to create/allocate a new netlink
  98. * // message.
  99. * //
  100. * // nlmsg_alloc() will allocate an empty netlink message with
  101. * // a maximum payload size which defaults to the page size of
  102. * // the system. This default size can be modified using the
  103. * // function nlmsg_set_default_size().
  104. * struct nl_msg *msg = nlmsg_alloc();
  105. *
  106. * // Very often, the message type and message flags are known
  107. * // at allocation time while the other fields are auto generated:
  108. * struct nl_msg *msg = nlmsg_alloc_simple(MY_TYPE, MY_FLAGS);
  109. *
  110. * // Alternatively an existing netlink message header can be used
  111. * // to inherit the header values:
  112. * struct nlmsghdr hdr = {
  113. * .nlmsg_type = MY_TYPE,
  114. * .nlmsg_flags = MY_FLAGS,
  115. * };
  116. * struct nl_msg *msg = nlmsg_inherit(&hdr);
  117. *
  118. * // Last but not least, netlink messages received from netlink sockets
  119. * // can be converted into nl_msg objects using nlmsg_convert(). This
  120. * // will create a message with a maximum payload size which equals the
  121. * // length of the existing netlink message, therefore no more data can
  122. * // be appened without calling nlmsg_expand() first.
  123. * struct nl_msg *msg = nlmsg_convert(nlh_from_nl_sock);
  124. *
  125. * // Payload may be added to the message via nlmsg_append(). The fourth
  126. * // parameter specifies the number of alignment bytes the data should
  127. * // be padding with at the end. Common values are 0 to disable it or
  128. * // NLMSG_ALIGNTO to ensure proper netlink message padding.
  129. * nlmsg_append(msg, &mydata, sizeof(mydata), 0);
  130. *
  131. * // Sometimes it may be necessary to reserve room for data but defer
  132. * // the actual copying to a later point, nlmsg_reserve() can be used
  133. * // for this purpose:
  134. * void *data = nlmsg_reserve(msg, sizeof(mydata), NLMSG_ALIGNTO);
  135. *
  136. * // Attributes may be added using the attributes interface.
  137. *
  138. * // After successful use of the message, the memory must be freed
  139. * // using nlmsg_free()
  140. * nlmsg_free(msg);
  141. * @endcode
  142. *
  143. * @par 4) Parsing messages
  144. * @code
  145. * int n;
  146. * unsigned char *buf;
  147. * struct nlmsghdr *hdr;
  148. *
  149. * n = nl_recv(handle, NULL, &buf);
  150. *
  151. * hdr = (struct nlmsghdr *) buf;
  152. * while (nlmsg_ok(hdr, n)) {
  153. * // Process message here...
  154. * hdr = nlmsg_next(hdr, &n);
  155. * }
  156. * @endcode
  157. * @{
  158. */
  159. #include <netlink-local.h>
  160. #include <netlink/netlink.h>
  161. #include <netlink/utils.h>
  162. #include <netlink/cache.h>
  163. #include <netlink/attr.h>
  164. #include <netlink/msg.h>
  165. #include <linux/socket.h>
  166. static size_t default_msg_size = 4096;
  167. /**
  168. * @name Attribute Access
  169. * @{
  170. */
  171. //** @} */
  172. /**
  173. * @name Message Parsing
  174. * @{
  175. */
  176. /**
  177. * check if the netlink message fits into the remaining bytes
  178. * @arg nlh netlink message header
  179. * @arg remaining number of bytes remaining in message stream
  180. */
  181. int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  182. {
  183. size_t r = remaining;
  184. return (r >= sizeof(struct nlmsghdr) &&
  185. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  186. nlh->nlmsg_len <= r);
  187. }
  188. /**
  189. * next netlink message in message stream
  190. * @arg nlh netlink message header
  191. * @arg remaining number of bytes remaining in message stream
  192. *
  193. * @returns the next netlink message in the message stream and
  194. * decrements remaining by the size of the current message.
  195. */
  196. struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
  197. {
  198. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  199. *remaining -= totlen;
  200. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  201. }
  202. /**
  203. * parse attributes of a netlink message
  204. * @arg nlh netlink message header
  205. * @arg hdrlen length of family specific header
  206. * @arg tb destination array with maxtype+1 elements
  207. * @arg maxtype maximum attribute type to be expected
  208. * @arg policy validation policy
  209. *
  210. * See nla_parse()
  211. */
  212. int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
  213. int maxtype, struct nla_policy *policy)
  214. {
  215. if (!nlmsg_valid_hdr(nlh, hdrlen))
  216. return -NLE_MSG_TOOSHORT;
  217. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  218. nlmsg_attrlen(nlh, hdrlen), policy);
  219. }
  220. /**
  221. * nlmsg_validate - validate a netlink message including attributes
  222. * @arg nlh netlinket message header
  223. * @arg hdrlen length of familiy specific header
  224. * @arg maxtype maximum attribute type to be expected
  225. * @arg policy validation policy
  226. */
  227. int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
  228. struct nla_policy *policy)
  229. {
  230. if (!nlmsg_valid_hdr(nlh, hdrlen))
  231. return -NLE_MSG_TOOSHORT;
  232. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  233. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  234. }
  235. /** @} */
  236. /**
  237. * @name Message Building/Access
  238. * @{
  239. */
  240. static struct nl_msg *__nlmsg_alloc(size_t len)
  241. {
  242. struct nl_msg *nm;
  243. nm = calloc(1, sizeof(*nm));
  244. if (!nm)
  245. goto errout;
  246. nm->nm_refcnt = 1;
  247. nm->nm_nlh = malloc(len);
  248. if (!nm->nm_nlh)
  249. goto errout;
  250. memset(nm->nm_nlh, 0, sizeof(struct nlmsghdr));
  251. nm->nm_protocol = -1;
  252. nm->nm_size = len;
  253. nm->nm_nlh->nlmsg_len = nlmsg_total_size(0);
  254. NL_DBG(2, "msg %p: Allocated new message, maxlen=%zu\n", nm, len);
  255. return nm;
  256. errout:
  257. free(nm);
  258. return NULL;
  259. }
  260. /**
  261. * Allocate a new netlink message with the default maximum payload size.
  262. *
  263. * Allocates a new netlink message without any further payload. The
  264. * maximum payload size defaults to PAGESIZE or as otherwise specified
  265. * with nlmsg_set_default_size().
  266. *
  267. * @return Newly allocated netlink message or NULL.
  268. */
  269. struct nl_msg *nlmsg_alloc(void)
  270. {
  271. return __nlmsg_alloc(default_msg_size);
  272. }
  273. /**
  274. * Allocate a new netlink message with maximum payload size specified.
  275. */
  276. struct nl_msg *nlmsg_alloc_size(size_t max)
  277. {
  278. return __nlmsg_alloc(max);
  279. }
  280. /**
  281. * Allocate a new netlink message and inherit netlink message header
  282. * @arg hdr Netlink message header template
  283. *
  284. * Allocates a new netlink message and inherits the original message
  285. * header. If \a hdr is not NULL it will be used as a template for
  286. * the netlink message header, otherwise the header is left blank.
  287. *
  288. * @return Newly allocated netlink message or NULL
  289. */
  290. struct nl_msg *nlmsg_inherit(struct nlmsghdr *hdr)
  291. {
  292. struct nl_msg *nm;
  293. nm = nlmsg_alloc();
  294. if (nm && hdr) {
  295. struct nlmsghdr *new = nm->nm_nlh;
  296. new->nlmsg_type = hdr->nlmsg_type;
  297. new->nlmsg_flags = hdr->nlmsg_flags;
  298. new->nlmsg_seq = hdr->nlmsg_seq;
  299. new->nlmsg_pid = hdr->nlmsg_pid;
  300. }
  301. return nm;
  302. }
  303. /**
  304. * Allocate a new netlink message
  305. * @arg nlmsgtype Netlink message type
  306. * @arg flags Message flags.
  307. *
  308. * @return Newly allocated netlink message or NULL.
  309. */
  310. struct nl_msg *nlmsg_alloc_simple(int nlmsgtype, int flags)
  311. {
  312. struct nl_msg *msg;
  313. struct nlmsghdr nlh = {
  314. .nlmsg_type = nlmsgtype,
  315. .nlmsg_flags = flags,
  316. };
  317. msg = nlmsg_inherit(&nlh);
  318. if (msg)
  319. NL_DBG(2, "msg %p: Allocated new simple message\n", msg);
  320. return msg;
  321. }
  322. /**
  323. * Set the default maximum message payload size for allocated messages
  324. * @arg max Size of payload in bytes.
  325. */
  326. void nlmsg_set_default_size(size_t max)
  327. {
  328. if (max < (size_t) nlmsg_total_size(0))
  329. max = nlmsg_total_size(0);
  330. default_msg_size = max;
  331. }
  332. /**
  333. * Convert a netlink message received from a netlink socket to a nl_msg
  334. * @arg hdr Netlink message received from netlink socket.
  335. *
  336. * Allocates a new netlink message and copies all of the data pointed to
  337. * by \a hdr into the new message object.
  338. *
  339. * @return Newly allocated netlink message or NULL.
  340. */
  341. struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr)
  342. {
  343. struct nl_msg *nm;
  344. nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
  345. if (!nm)
  346. goto errout;
  347. memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
  348. return nm;
  349. errout:
  350. nlmsg_free(nm);
  351. return NULL;
  352. }
  353. /**
  354. * Reserve room for additional data in a netlink message
  355. * @arg n netlink message
  356. * @arg len length of additional data to reserve room for
  357. * @arg pad number of bytes to align data to
  358. *
  359. * Reserves room for additional data at the tail of the an
  360. * existing netlink message. Eventual padding required will
  361. * be zeroed out.
  362. *
  363. * @return Pointer to start of additional data tailroom or NULL.
  364. */
  365. void *nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
  366. {
  367. void *buf = n->nm_nlh;
  368. size_t nlmsg_len = n->nm_nlh->nlmsg_len;
  369. size_t tlen;
  370. tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
  371. if ((tlen + nlmsg_len) > n->nm_size)
  372. return NULL;
  373. buf += nlmsg_len;
  374. n->nm_nlh->nlmsg_len += tlen;
  375. if (tlen > len)
  376. memset(buf + len, 0, tlen - len);
  377. NL_DBG(2, "msg %p: Reserved %zu bytes, pad=%d, nlmsg_len=%d\n",
  378. n, len, pad, n->nm_nlh->nlmsg_len);
  379. return buf;
  380. }
  381. /**
  382. * Append data to tail of a netlink message
  383. * @arg n netlink message
  384. * @arg data data to add
  385. * @arg len length of data
  386. * @arg pad Number of bytes to align data to.
  387. *
  388. * Extends the netlink message as needed and appends the data of given
  389. * length to the message.
  390. *
  391. * @return 0 on success or a negative error code
  392. */
  393. int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
  394. {
  395. void *tmp;
  396. tmp = nlmsg_reserve(n, len, pad);
  397. if (tmp == NULL)
  398. return -NLE_NOMEM;
  399. memcpy(tmp, data, len);
  400. NL_DBG(2, "msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
  401. return 0;
  402. }
  403. /**
  404. * Add a netlink message header to a netlink message
  405. * @arg n netlink message
  406. * @arg pid netlink process id or NL_AUTO_PID
  407. * @arg seq sequence number of message or NL_AUTO_SEQ
  408. * @arg type message type
  409. * @arg payload length of message payload
  410. * @arg flags message flags
  411. *
  412. * Adds or overwrites the netlink message header in an existing message
  413. * object. If \a payload is greater-than zero additional room will be
  414. * reserved, f.e. for family specific headers. It can be accesed via
  415. * nlmsg_data().
  416. *
  417. * @return A pointer to the netlink message header or NULL.
  418. */
  419. struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
  420. int type, int payload, int flags)
  421. {
  422. struct nlmsghdr *nlh;
  423. if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
  424. BUG();
  425. nlh = (struct nlmsghdr *) n->nm_nlh;
  426. nlh->nlmsg_type = type;
  427. nlh->nlmsg_flags = flags;
  428. nlh->nlmsg_pid = pid;
  429. nlh->nlmsg_seq = seq;
  430. NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
  431. "seq=%d\n", n, type, flags, pid, seq);
  432. if (payload > 0 &&
  433. nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
  434. return NULL;
  435. return nlh;
  436. }
  437. /**
  438. * Release a reference from an netlink message
  439. * @arg msg message to release reference from
  440. *
  441. * Frees memory after the last reference has been released.
  442. */
  443. void nlmsg_free(struct nl_msg *msg)
  444. {
  445. if (!msg)
  446. return;
  447. msg->nm_refcnt--;
  448. NL_DBG(4, "Returned message reference %p, %d remaining\n",
  449. msg, msg->nm_refcnt);
  450. if (msg->nm_refcnt < 0)
  451. BUG();
  452. if (msg->nm_refcnt <= 0) {
  453. free(msg->nm_nlh);
  454. free(msg);
  455. NL_DBG(2, "msg %p: Freed\n", msg);
  456. }
  457. }
  458. /** @} */
  459. /**
  460. * @name Direct Parsing
  461. * @{
  462. */
  463. /** @cond SKIP */
  464. struct dp_xdata {
  465. void (*cb)(struct nl_object *, void *);
  466. void *arg;
  467. };
  468. /** @endcond */
  469. static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
  470. {
  471. struct dp_xdata *x = p->pp_arg;
  472. x->cb(obj, x->arg);
  473. return 0;
  474. }
  475. int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
  476. void *arg)
  477. {
  478. struct nl_cache_ops *ops;
  479. struct nl_parser_param p = {
  480. .pp_cb = parse_cb
  481. };
  482. struct dp_xdata x = {
  483. .cb = cb,
  484. .arg = arg,
  485. };
  486. ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
  487. nlmsg_hdr(msg)->nlmsg_type);
  488. if (ops == NULL)
  489. return -NLE_MSGTYPE_NOSUPPORT;
  490. p.pp_arg = &x;
  491. return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
  492. }
  493. /** @} */