msg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 <[email protected]>
  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;
  167. static void __init init_msg_size(void)
  168. {
  169. default_msg_size = getpagesize();
  170. }
  171. /**
  172. * @name Attribute Access
  173. * @{
  174. */
  175. //** @} */
  176. /**
  177. * @name Message Parsing
  178. * @{
  179. */
  180. /**
  181. * check if the netlink message fits into the remaining bytes
  182. * @arg nlh netlink message header
  183. * @arg remaining number of bytes remaining in message stream
  184. */
  185. int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  186. {
  187. return (remaining >= sizeof(struct nlmsghdr) &&
  188. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  189. nlh->nlmsg_len <= remaining);
  190. }
  191. /**
  192. * next netlink message in message stream
  193. * @arg nlh netlink message header
  194. * @arg remaining number of bytes remaining in message stream
  195. *
  196. * @returns the next netlink message in the message stream and
  197. * decrements remaining by the size of the current message.
  198. */
  199. struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
  200. {
  201. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  202. *remaining -= totlen;
  203. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  204. }
  205. /**
  206. * parse attributes of a netlink message
  207. * @arg nlh netlink message header
  208. * @arg hdrlen length of family specific header
  209. * @arg tb destination array with maxtype+1 elements
  210. * @arg maxtype maximum attribute type to be expected
  211. * @arg policy validation policy
  212. *
  213. * See nla_parse()
  214. */
  215. int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
  216. int maxtype, struct nla_policy *policy)
  217. {
  218. if (!nlmsg_valid_hdr(nlh, hdrlen))
  219. return -NLE_MSG_TOOSHORT;
  220. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  221. nlmsg_attrlen(nlh, hdrlen), policy);
  222. }
  223. /**
  224. * nlmsg_validate - validate a netlink message including attributes
  225. * @arg nlh netlinket message header
  226. * @arg hdrlen length of familiy specific header
  227. * @arg maxtype maximum attribute type to be expected
  228. * @arg policy validation policy
  229. */
  230. int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
  231. struct nla_policy *policy)
  232. {
  233. if (!nlmsg_valid_hdr(nlh, hdrlen))
  234. return -NLE_MSG_TOOSHORT;
  235. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  236. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  237. }
  238. /** @} */
  239. /**
  240. * @name Message Building/Access
  241. * @{
  242. */
  243. static struct nl_msg *__nlmsg_alloc(size_t len)
  244. {
  245. struct nl_msg *nm;
  246. nm = calloc(1, sizeof(*nm));
  247. if (!nm)
  248. goto errout;
  249. nm->nm_refcnt = 1;
  250. nm->nm_nlh = malloc(len);
  251. if (!nm->nm_nlh)
  252. goto errout;
  253. memset(nm->nm_nlh, 0, sizeof(struct nlmsghdr));
  254. nm->nm_protocol = -1;
  255. nm->nm_size = len;
  256. nm->nm_nlh->nlmsg_len = nlmsg_total_size(0);
  257. NL_DBG(2, "msg %p: Allocated new message, maxlen=%zu\n", nm, len);
  258. return nm;
  259. errout:
  260. free(nm);
  261. return NULL;
  262. }
  263. /**
  264. * Allocate a new netlink message with the default maximum payload size.
  265. *
  266. * Allocates a new netlink message without any further payload. The
  267. * maximum payload size defaults to PAGESIZE or as otherwise specified
  268. * with nlmsg_set_default_size().
  269. *
  270. * @return Newly allocated netlink message or NULL.
  271. */
  272. struct nl_msg *nlmsg_alloc(void)
  273. {
  274. return __nlmsg_alloc(default_msg_size);
  275. }
  276. /**
  277. * Allocate a new netlink message with maximum payload size specified.
  278. */
  279. struct nl_msg *nlmsg_alloc_size(size_t max)
  280. {
  281. return __nlmsg_alloc(max);
  282. }
  283. /**
  284. * Allocate a new netlink message and inherit netlink message header
  285. * @arg hdr Netlink message header template
  286. *
  287. * Allocates a new netlink message and inherits the original message
  288. * header. If \a hdr is not NULL it will be used as a template for
  289. * the netlink message header, otherwise the header is left blank.
  290. *
  291. * @return Newly allocated netlink message or NULL
  292. */
  293. struct nl_msg *nlmsg_inherit(struct nlmsghdr *hdr)
  294. {
  295. struct nl_msg *nm;
  296. nm = nlmsg_alloc();
  297. if (nm && hdr) {
  298. struct nlmsghdr *new = nm->nm_nlh;
  299. new->nlmsg_type = hdr->nlmsg_type;
  300. new->nlmsg_flags = hdr->nlmsg_flags;
  301. new->nlmsg_seq = hdr->nlmsg_seq;
  302. new->nlmsg_pid = hdr->nlmsg_pid;
  303. }
  304. return nm;
  305. }
  306. /**
  307. * Allocate a new netlink message
  308. * @arg nlmsgtype Netlink message type
  309. * @arg flags Message flags.
  310. *
  311. * @return Newly allocated netlink message or NULL.
  312. */
  313. struct nl_msg *nlmsg_alloc_simple(int nlmsgtype, int flags)
  314. {
  315. struct nl_msg *msg;
  316. struct nlmsghdr nlh = {
  317. .nlmsg_type = nlmsgtype,
  318. .nlmsg_flags = flags,
  319. };
  320. msg = nlmsg_inherit(&nlh);
  321. if (msg)
  322. NL_DBG(2, "msg %p: Allocated new simple message\n", msg);
  323. return msg;
  324. }
  325. /**
  326. * Set the default maximum message payload size for allocated messages
  327. * @arg max Size of payload in bytes.
  328. */
  329. void nlmsg_set_default_size(size_t max)
  330. {
  331. if (max < nlmsg_total_size(0))
  332. max = nlmsg_total_size(0);
  333. default_msg_size = max;
  334. }
  335. /**
  336. * Convert a netlink message received from a netlink socket to a nl_msg
  337. * @arg hdr Netlink message received from netlink socket.
  338. *
  339. * Allocates a new netlink message and copies all of the data pointed to
  340. * by \a hdr into the new message object.
  341. *
  342. * @return Newly allocated netlink message or NULL.
  343. */
  344. struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr)
  345. {
  346. struct nl_msg *nm;
  347. nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
  348. if (!nm)
  349. goto errout;
  350. memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
  351. return nm;
  352. errout:
  353. nlmsg_free(nm);
  354. return NULL;
  355. }
  356. /**
  357. * Reserve room for additional data in a netlink message
  358. * @arg n netlink message
  359. * @arg len length of additional data to reserve room for
  360. * @arg pad number of bytes to align data to
  361. *
  362. * Reserves room for additional data at the tail of the an
  363. * existing netlink message. Eventual padding required will
  364. * be zeroed out.
  365. *
  366. * @return Pointer to start of additional data tailroom or NULL.
  367. */
  368. void *nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
  369. {
  370. void *buf = n->nm_nlh;
  371. size_t nlmsg_len = n->nm_nlh->nlmsg_len;
  372. size_t tlen;
  373. tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
  374. if ((tlen + nlmsg_len) > n->nm_size)
  375. return NULL;
  376. buf += nlmsg_len;
  377. n->nm_nlh->nlmsg_len += tlen;
  378. if (tlen > len)
  379. memset(buf + len, 0, tlen - len);
  380. NL_DBG(2, "msg %p: Reserved %zu bytes, pad=%d, nlmsg_len=%d\n",
  381. n, len, pad, n->nm_nlh->nlmsg_len);
  382. return buf;
  383. }
  384. /**
  385. * Append data to tail of a netlink message
  386. * @arg n netlink message
  387. * @arg data data to add
  388. * @arg len length of data
  389. * @arg pad Number of bytes to align data to.
  390. *
  391. * Extends the netlink message as needed and appends the data of given
  392. * length to the message.
  393. *
  394. * @return 0 on success or a negative error code
  395. */
  396. int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
  397. {
  398. void *tmp;
  399. tmp = nlmsg_reserve(n, len, pad);
  400. if (tmp == NULL)
  401. return -NLE_NOMEM;
  402. memcpy(tmp, data, len);
  403. NL_DBG(2, "msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
  404. return 0;
  405. }
  406. /**
  407. * Add a netlink message header to a netlink message
  408. * @arg n netlink message
  409. * @arg pid netlink process id or NL_AUTO_PID
  410. * @arg seq sequence number of message or NL_AUTO_SEQ
  411. * @arg type message type
  412. * @arg payload length of message payload
  413. * @arg flags message flags
  414. *
  415. * Adds or overwrites the netlink message header in an existing message
  416. * object. If \a payload is greater-than zero additional room will be
  417. * reserved, f.e. for family specific headers. It can be accesed via
  418. * nlmsg_data().
  419. *
  420. * @return A pointer to the netlink message header or NULL.
  421. */
  422. struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
  423. int type, int payload, int flags)
  424. {
  425. struct nlmsghdr *nlh;
  426. if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
  427. BUG();
  428. nlh = (struct nlmsghdr *) n->nm_nlh;
  429. nlh->nlmsg_type = type;
  430. nlh->nlmsg_flags = flags;
  431. nlh->nlmsg_pid = pid;
  432. nlh->nlmsg_seq = seq;
  433. NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
  434. "seq=%d\n", n, type, flags, pid, seq);
  435. if (payload > 0 &&
  436. nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
  437. return NULL;
  438. return nlh;
  439. }
  440. /**
  441. * Release a reference from an netlink message
  442. * @arg msg message to release reference from
  443. *
  444. * Frees memory after the last reference has been released.
  445. */
  446. void nlmsg_free(struct nl_msg *msg)
  447. {
  448. if (!msg)
  449. return;
  450. msg->nm_refcnt--;
  451. NL_DBG(4, "Returned message reference %p, %d remaining\n",
  452. msg, msg->nm_refcnt);
  453. if (msg->nm_refcnt < 0)
  454. BUG();
  455. if (msg->nm_refcnt <= 0) {
  456. free(msg->nm_nlh);
  457. free(msg);
  458. NL_DBG(2, "msg %p: Freed\n", msg);
  459. }
  460. }
  461. /** @} */
  462. /**
  463. * @name Direct Parsing
  464. * @{
  465. */
  466. /** @cond SKIP */
  467. struct dp_xdata {
  468. void (*cb)(struct nl_object *, void *);
  469. void *arg;
  470. };
  471. /** @endcond */
  472. static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
  473. {
  474. struct dp_xdata *x = p->pp_arg;
  475. x->cb(obj, x->arg);
  476. return 0;
  477. }
  478. int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
  479. void *arg)
  480. {
  481. struct nl_cache_ops *ops;
  482. struct nl_parser_param p = {
  483. .pp_cb = parse_cb
  484. };
  485. struct dp_xdata x = {
  486. .cb = cb,
  487. .arg = arg,
  488. };
  489. ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
  490. nlmsg_hdr(msg)->nlmsg_type);
  491. if (ops == NULL)
  492. return -NLE_MSGTYPE_NOSUPPORT;
  493. p.pp_arg = &x;
  494. return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
  495. }
  496. /** @} */