msg.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup nl
  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 <linux/socket.h>
  165. static size_t default_msg_size;
  166. static void __init init_msg_size(void)
  167. {
  168. default_msg_size = getpagesize();
  169. }
  170. /**
  171. * @name Size Calculations
  172. * @{
  173. */
  174. /**
  175. * length of netlink message not including padding
  176. * @arg payload length of message payload
  177. */
  178. int nlmsg_msg_size(int payload)
  179. {
  180. return NLMSG_HDRLEN + payload;
  181. }
  182. /**
  183. * length of netlink message including padding
  184. * @arg payload length of message payload
  185. */
  186. int nlmsg_total_size(int payload)
  187. {
  188. return NLMSG_ALIGN(nlmsg_msg_size(payload));
  189. }
  190. /**
  191. * length of padding at the message's tail
  192. * @arg payload length of message payload
  193. */
  194. int nlmsg_padlen(int payload)
  195. {
  196. return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
  197. }
  198. /** @} */
  199. /**
  200. * @name Payload Access
  201. * @{
  202. */
  203. /**
  204. * head of message payload
  205. * @arg nlh netlink messsage header
  206. */
  207. void *nlmsg_data(const struct nlmsghdr *nlh)
  208. {
  209. return (unsigned char *) nlh + NLMSG_HDRLEN;
  210. }
  211. void *nlmsg_tail(const struct nlmsghdr *nlh)
  212. {
  213. return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
  214. }
  215. /**
  216. * length of message payload
  217. * @arg nlh netlink message header
  218. */
  219. int nlmsg_len(const struct nlmsghdr *nlh)
  220. {
  221. return nlh->nlmsg_len - NLMSG_HDRLEN;
  222. }
  223. /** @} */
  224. /**
  225. * @name Attribute Access
  226. * @{
  227. */
  228. /**
  229. * head of attributes data
  230. * @arg nlh netlink message header
  231. * @arg hdrlen length of family specific header
  232. */
  233. struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
  234. {
  235. unsigned char *data = nlmsg_data(nlh);
  236. return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
  237. }
  238. /**
  239. * length of attributes data
  240. * @arg nlh netlink message header
  241. * @arg hdrlen length of family specific header
  242. */
  243. int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
  244. {
  245. return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
  246. }
  247. /** @} */
  248. /**
  249. * @name Message Parsing
  250. * @{
  251. */
  252. int nlmsg_valid_hdr(const struct nlmsghdr *nlh, int hdrlen)
  253. {
  254. if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
  255. return 0;
  256. return 1;
  257. }
  258. /**
  259. * check if the netlink message fits into the remaining bytes
  260. * @arg nlh netlink message header
  261. * @arg remaining number of bytes remaining in message stream
  262. */
  263. int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
  264. {
  265. return (remaining >= (int)sizeof(struct nlmsghdr) &&
  266. nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
  267. nlh->nlmsg_len <= remaining);
  268. }
  269. /**
  270. * next netlink message in message stream
  271. * @arg nlh netlink message header
  272. * @arg remaining number of bytes remaining in message stream
  273. *
  274. * @returns the next netlink message in the message stream and
  275. * decrements remaining by the size of the current message.
  276. */
  277. struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
  278. {
  279. int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
  280. *remaining -= totlen;
  281. return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
  282. }
  283. /**
  284. * parse attributes of a netlink message
  285. * @arg nlh netlink message header
  286. * @arg hdrlen length of family specific header
  287. * @arg tb destination array with maxtype+1 elements
  288. * @arg maxtype maximum attribute type to be expected
  289. * @arg policy validation policy
  290. *
  291. * See nla_parse()
  292. */
  293. int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
  294. int maxtype, struct nla_policy *policy)
  295. {
  296. if (!nlmsg_valid_hdr(nlh, hdrlen))
  297. return nl_errno(EINVAL);
  298. return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
  299. nlmsg_attrlen(nlh, hdrlen), policy);
  300. }
  301. /**
  302. * nlmsg_find_attr - find a specific attribute in a netlink message
  303. * @arg nlh netlink message header
  304. * @arg hdrlen length of familiy specific header
  305. * @arg attrtype type of attribute to look for
  306. *
  307. * Returns the first attribute which matches the specified type.
  308. */
  309. struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
  310. {
  311. return nla_find(nlmsg_attrdata(nlh, hdrlen),
  312. nlmsg_attrlen(nlh, hdrlen), attrtype);
  313. }
  314. /**
  315. * nlmsg_validate - validate a netlink message including attributes
  316. * @arg nlh netlinket message header
  317. * @arg hdrlen length of familiy specific header
  318. * @arg maxtype maximum attribute type to be expected
  319. * @arg policy validation policy
  320. */
  321. int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
  322. struct nla_policy *policy)
  323. {
  324. if (!nlmsg_valid_hdr(nlh, hdrlen))
  325. return nl_errno(EINVAL);
  326. return nla_validate(nlmsg_attrdata(nlh, hdrlen),
  327. nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
  328. }
  329. /** @} */
  330. /**
  331. * @name Message Building/Access
  332. * @{
  333. */
  334. static struct nl_msg *__nlmsg_alloc(size_t len)
  335. {
  336. struct nl_msg *nm;
  337. nm = calloc(1, sizeof(*nm));
  338. if (!nm)
  339. goto errout;
  340. nm->nm_nlh = calloc(1, len);
  341. if (!nm->nm_nlh)
  342. goto errout;
  343. nm->nm_protocol = -1;
  344. nm->nm_size = len;
  345. nm->nm_nlh->nlmsg_len = nlmsg_total_size(0);
  346. NL_DBG(2, "msg %p: Allocated new message, maxlen=%zu\n", nm, len);
  347. return nm;
  348. errout:
  349. free(nm);
  350. nl_errno(ENOMEM);
  351. return NULL;
  352. }
  353. /**
  354. * Allocate a new netlink message with the default maximum payload size.
  355. *
  356. * Allocates a new netlink message without any further payload. The
  357. * maximum payload size defaults to PAGESIZE or as otherwise specified
  358. * with nlmsg_set_default_size().
  359. *
  360. * @return Newly allocated netlink message or NULL.
  361. */
  362. struct nl_msg *nlmsg_alloc(void)
  363. {
  364. return __nlmsg_alloc(default_msg_size);
  365. }
  366. /**
  367. * Allocate a new netlink message with maximum payload size specified.
  368. */
  369. struct nl_msg *nlmsg_alloc_size(size_t max)
  370. {
  371. return __nlmsg_alloc(max);
  372. }
  373. /**
  374. * Allocate a new netlink message and inherit netlink message header
  375. * @arg hdr Netlink message header template
  376. *
  377. * Allocates a new netlink message and inherits the original message
  378. * header. If \a hdr is not NULL it will be used as a template for
  379. * the netlink message header, otherwise the header is left blank.
  380. *
  381. * @return Newly allocated netlink message or NULL
  382. */
  383. struct nl_msg *nlmsg_inherit(struct nlmsghdr *hdr)
  384. {
  385. struct nl_msg *nm;
  386. nm = nlmsg_alloc();
  387. if (nm && hdr) {
  388. struct nlmsghdr *new = nm->nm_nlh;
  389. new->nlmsg_type = hdr->nlmsg_type;
  390. new->nlmsg_flags = hdr->nlmsg_flags;
  391. new->nlmsg_seq = hdr->nlmsg_seq;
  392. new->nlmsg_pid = hdr->nlmsg_pid;
  393. }
  394. return nm;
  395. }
  396. /**
  397. * Allocate a new netlink message
  398. * @arg nlmsgtype Netlink message type
  399. * @arg flags Message flags.
  400. *
  401. * @return Newly allocated netlink message or NULL.
  402. */
  403. struct nl_msg *nlmsg_alloc_simple(int nlmsgtype, int flags)
  404. {
  405. struct nl_msg *msg;
  406. struct nlmsghdr nlh = {
  407. .nlmsg_type = nlmsgtype,
  408. .nlmsg_flags = flags,
  409. };
  410. msg = nlmsg_inherit(&nlh);
  411. if (msg)
  412. NL_DBG(2, "msg %p: Allocated new simple message\n", msg);
  413. return msg;
  414. }
  415. /**
  416. * Set the default maximum message payload size for allocated messages
  417. * @arg max Size of payload in bytes.
  418. */
  419. void nlmsg_set_default_size(size_t max)
  420. {
  421. if (max < nlmsg_total_size(0))
  422. max = nlmsg_total_size(0);
  423. default_msg_size = max;
  424. }
  425. /**
  426. * Convert a netlink message received from a netlink socket to a nl_msg
  427. * @arg hdr Netlink message received from netlink socket.
  428. *
  429. * Allocates a new netlink message and copies all of the data pointed to
  430. * by \a hdr into the new message object.
  431. *
  432. * @return Newly allocated netlink message or NULL.
  433. */
  434. struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr)
  435. {
  436. struct nl_msg *nm;
  437. nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
  438. if (!nm)
  439. goto errout;
  440. memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
  441. return nm;
  442. errout:
  443. nlmsg_free(nm);
  444. return NULL;
  445. }
  446. /**
  447. * Reserve room for additional data in a netlink message
  448. * @arg n netlink message
  449. * @arg len length of additional data to reserve room for
  450. * @arg pad number of bytes to align data to
  451. *
  452. * Reserves room for additional data at the tail of the an
  453. * existing netlink message. Eventual padding required will
  454. * be zeroed out.
  455. *
  456. * @return Pointer to start of additional data tailroom or NULL.
  457. */
  458. void *nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
  459. {
  460. void *buf = n->nm_nlh;
  461. size_t nlmsg_len = n->nm_nlh->nlmsg_len;
  462. size_t tlen;
  463. tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
  464. if ((tlen + nlmsg_len) > n->nm_size) {
  465. nl_errno(ENOBUFS);
  466. return NULL;
  467. }
  468. buf += nlmsg_len;
  469. n->nm_nlh->nlmsg_len += tlen;
  470. if (tlen > len)
  471. memset(buf + len, 0, tlen - len);
  472. NL_DBG(2, "msg %p: Reserved %zu bytes, pad=%d, nlmsg_len=%d\n",
  473. n, len, pad, n->nm_nlh->nlmsg_len);
  474. return buf;
  475. }
  476. /**
  477. * Append data to tail of a netlink message
  478. * @arg n netlink message
  479. * @arg data data to add
  480. * @arg len length of data
  481. * @arg pad Number of bytes to align data to.
  482. *
  483. * Extends the netlink message as needed and appends the data of given
  484. * length to the message.
  485. *
  486. * @return 0 on success or a negative error code
  487. */
  488. int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
  489. {
  490. void *tmp;
  491. tmp = nlmsg_reserve(n, len, pad);
  492. if (tmp == NULL)
  493. return nl_errno(ENOMEM);
  494. memcpy(tmp, data, len);
  495. NL_DBG(2, "msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
  496. return 0;
  497. }
  498. /**
  499. * Expand maximum payload size of a netlink message
  500. * @arg n Netlink message.
  501. * @arg newlen New maximum payload size.
  502. *
  503. * Reallocates the payload section of a netlink message and increases
  504. * the maximum payload size of the message.
  505. *
  506. * @note Any pointers pointing to old payload block will be stale and
  507. * need to be refetched. Therfore, do not expand while constructing
  508. * nested attributes or while reserved data blocks are held.
  509. *
  510. * @return 0 on success or a negative error code.
  511. */
  512. int nlmsg_expand(struct nl_msg *n, size_t newlen)
  513. {
  514. void *tmp;
  515. if (newlen <= n->nm_size)
  516. return nl_errno(EINVAL);
  517. tmp = realloc(n->nm_nlh, newlen);
  518. if (tmp == NULL)
  519. return nl_errno(ENOMEM);
  520. n->nm_nlh = tmp;
  521. n->nm_size = newlen;
  522. return 0;
  523. }
  524. /**
  525. * Add a netlink message header to a netlink message
  526. * @arg n netlink message
  527. * @arg pid netlink process id or NL_AUTO_PID
  528. * @arg seq sequence number of message or NL_AUTO_SEQ
  529. * @arg type message type
  530. * @arg payload length of message payload
  531. * @arg flags message flags
  532. *
  533. * Adds or overwrites the netlink message header in an existing message
  534. * object. If \a payload is greater-than zero additional room will be
  535. * reserved, f.e. for family specific headers. It can be accesed via
  536. * nlmsg_data().
  537. *
  538. * @return A pointer to the netlink message header or NULL.
  539. */
  540. struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
  541. int type, int payload, int flags)
  542. {
  543. struct nlmsghdr *nlh;
  544. if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
  545. BUG();
  546. nlh = (struct nlmsghdr *) n->nm_nlh;
  547. nlh->nlmsg_type = type;
  548. nlh->nlmsg_flags = flags;
  549. nlh->nlmsg_pid = pid;
  550. nlh->nlmsg_seq = seq;
  551. NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
  552. "seq=%d\n", n, type, flags, pid, seq);
  553. if (payload > 0 &&
  554. nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
  555. return NULL;
  556. return nlh;
  557. }
  558. /**
  559. * Return actual netlink message
  560. * @arg n netlink message
  561. *
  562. * Returns the actual netlink message casted to the type of the netlink
  563. * message header.
  564. *
  565. * @return A pointer to the netlink message.
  566. */
  567. struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
  568. {
  569. return n->nm_nlh;
  570. }
  571. /**
  572. * Free a netlink message
  573. * @arg n netlink message
  574. *
  575. * Destroys a netlink message and frees up all used memory.
  576. *
  577. * @pre The message must be unused.
  578. */
  579. void nlmsg_free(struct nl_msg *n)
  580. {
  581. if (!n)
  582. return;
  583. free(n->nm_nlh);
  584. free(n);
  585. NL_DBG(2, "msg %p: Freed\n", n);
  586. }
  587. /** @} */
  588. /**
  589. * @name Attributes
  590. * @{
  591. */
  592. void nlmsg_set_proto(struct nl_msg *msg, int protocol)
  593. {
  594. msg->nm_protocol = protocol;
  595. }
  596. int nlmsg_get_proto(struct nl_msg *msg)
  597. {
  598. return msg->nm_protocol;
  599. }
  600. size_t nlmsg_get_max_size(struct nl_msg *msg)
  601. {
  602. return msg->nm_size;
  603. }
  604. void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
  605. {
  606. memcpy(&msg->nm_src, addr, sizeof(*addr));
  607. }
  608. struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg)
  609. {
  610. return &msg->nm_src;
  611. }
  612. void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr)
  613. {
  614. memcpy(&msg->nm_dst, addr, sizeof(*addr));
  615. }
  616. struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg)
  617. {
  618. return &msg->nm_dst;
  619. }
  620. void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
  621. {
  622. memcpy(&msg->nm_creds, creds, sizeof(*creds));
  623. msg->nm_flags |= NL_MSG_CRED_PRESENT;
  624. }
  625. struct ucred *nlmsg_get_creds(struct nl_msg *msg)
  626. {
  627. if (msg->nm_flags & NL_MSG_CRED_PRESENT)
  628. return &msg->nm_creds;
  629. return NULL;
  630. }
  631. /** @} */
  632. /**
  633. * @name Netlink Message Type Translations
  634. * @{
  635. */
  636. static struct trans_tbl nl_msgtypes[] = {
  637. __ADD(NLMSG_NOOP,NOOP)
  638. __ADD(NLMSG_ERROR,ERROR)
  639. __ADD(NLMSG_DONE,DONE)
  640. __ADD(NLMSG_OVERRUN,OVERRUN)
  641. };
  642. char *nl_nlmsgtype2str(int type, char *buf, size_t size)
  643. {
  644. return __type2str(type, buf, size, nl_msgtypes,
  645. ARRAY_SIZE(nl_msgtypes));
  646. }
  647. int nl_str2nlmsgtype(const char *name)
  648. {
  649. return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
  650. }
  651. /** @} */
  652. /**
  653. * @name Netlink Message Flags Translations
  654. * @{
  655. */
  656. char *nl_nlmsg_flags2str(int flags, char *buf, size_t len)
  657. {
  658. memset(buf, 0, len);
  659. #define PRINT_FLAG(f) \
  660. if (flags & NLM_F_##f) { \
  661. flags &= ~NLM_F_##f; \
  662. strncat(buf, #f, len - strlen(buf) - 1); \
  663. if (flags) \
  664. strncat(buf, ",", len - strlen(buf) - 1); \
  665. }
  666. PRINT_FLAG(REQUEST);
  667. PRINT_FLAG(MULTI);
  668. PRINT_FLAG(ACK);
  669. PRINT_FLAG(ECHO);
  670. PRINT_FLAG(ROOT);
  671. PRINT_FLAG(MATCH);
  672. PRINT_FLAG(ATOMIC);
  673. PRINT_FLAG(REPLACE);
  674. PRINT_FLAG(EXCL);
  675. PRINT_FLAG(CREATE);
  676. PRINT_FLAG(APPEND);
  677. if (flags) {
  678. char s[32];
  679. snprintf(s, sizeof(s), "0x%x", flags);
  680. strncat(buf, s, len - strlen(buf) - 1);
  681. }
  682. #undef PRINT_FLAG
  683. return buf;
  684. }
  685. /** @} */
  686. /**
  687. * @name Direct Parsing
  688. * @{
  689. */
  690. /** @cond SKIP */
  691. struct dp_xdata {
  692. void (*cb)(struct nl_object *, void *);
  693. void *arg;
  694. };
  695. /** @endcond */
  696. static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
  697. {
  698. struct dp_xdata *x = p->pp_arg;
  699. x->cb(obj, x->arg);
  700. return 0;
  701. }
  702. int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
  703. void *arg)
  704. {
  705. struct nl_cache_ops *ops;
  706. struct nl_parser_param p = {
  707. .pp_cb = parse_cb
  708. };
  709. struct dp_xdata x = {
  710. .cb = cb,
  711. .arg = arg,
  712. };
  713. ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
  714. nlmsg_hdr(msg)->nlmsg_type);
  715. if (ops == NULL)
  716. return nl_error(ENOENT, "Unknown message type %d",
  717. nlmsg_hdr(msg)->nlmsg_type);
  718. p.pp_arg = &x;
  719. return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
  720. }
  721. /** @} */
  722. /**
  723. * @name Dumping
  724. * @{
  725. */
  726. static void prefix_line(FILE *ofd, int prefix)
  727. {
  728. int i;
  729. for (i = 0; i < prefix; i++)
  730. fprintf(ofd, " ");
  731. }
  732. static inline void dump_hex(FILE *ofd, char *start, int len, int prefix)
  733. {
  734. int i, a, c, limit;
  735. char ascii[21] = {0};
  736. limit = 18 - (prefix * 2);
  737. prefix_line(ofd, prefix);
  738. fprintf(ofd, " ");
  739. for (i = 0, a = 0, c = 0; i < len; i++) {
  740. int v = *(uint8_t *) (start + i);
  741. fprintf(ofd, "%02x ", v);
  742. ascii[a++] = isprint(v) ? v : '.';
  743. if (c == limit-1) {
  744. fprintf(ofd, "%s\n", ascii);
  745. if (i < (len - 1)) {
  746. prefix_line(ofd, prefix);
  747. fprintf(ofd, " ");
  748. }
  749. a = c = 0;
  750. memset(ascii, 0, sizeof(ascii));
  751. } else
  752. c++;
  753. }
  754. if (c != 0) {
  755. for (i = 0; i < (limit - c); i++)
  756. fprintf(ofd, " ");
  757. fprintf(ofd, "%s\n", ascii);
  758. }
  759. }
  760. static void print_hdr(FILE *ofd, struct nl_msg *msg)
  761. {
  762. struct nlmsghdr *nlh = nlmsg_hdr(msg);
  763. struct nl_cache_ops *ops;
  764. struct nl_msgtype *mt;
  765. char buf[128];
  766. fprintf(ofd, " .nlmsg_len = %d\n", nlh->nlmsg_len);
  767. ops = nl_cache_ops_associate(nlmsg_get_proto(msg), nlh->nlmsg_type);
  768. if (ops) {
  769. mt = nl_msgtype_lookup(ops, nlh->nlmsg_type);
  770. if (!mt)
  771. BUG();
  772. snprintf(buf, sizeof(buf), "%s::%s", ops->co_name, mt->mt_name);
  773. } else
  774. nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf));
  775. fprintf(ofd, " .nlmsg_type = %d <%s>\n", nlh->nlmsg_type, buf);
  776. fprintf(ofd, " .nlmsg_flags = %d <%s>\n", nlh->nlmsg_flags,
  777. nl_nlmsg_flags2str(nlh->nlmsg_flags, buf, sizeof(buf)));
  778. fprintf(ofd, " .nlmsg_seq = %d\n", nlh->nlmsg_seq);
  779. fprintf(ofd, " .nlmsg_pid = %d\n", nlh->nlmsg_pid);
  780. }
  781. static void dump_attrs(FILE *ofd, struct nlattr *attrs, int attrlen,
  782. int prefix)
  783. {
  784. int rem;
  785. struct nlattr *nla;
  786. nla_for_each_attr(nla, attrs, attrlen, rem) {
  787. int padlen, alen = nla_len(nla);
  788. prefix_line(ofd, prefix);
  789. fprintf(ofd, " [ATTR %02d%s] %d octets\n", nla_type(nla),
  790. nla->nla_type & NLA_F_NESTED ? " NESTED" : "",
  791. alen);
  792. if (nla->nla_type & NLA_F_NESTED)
  793. dump_attrs(ofd, nla_data(nla), alen, prefix+1);
  794. else
  795. dump_hex(ofd, nla_data(nla), alen, prefix);
  796. padlen = nla_padlen(alen);
  797. if (padlen > 0) {
  798. prefix_line(ofd, prefix);
  799. fprintf(ofd, " [PADDING] %d octets\n",
  800. padlen);
  801. dump_hex(ofd, nla_data(nla) + alen,
  802. padlen, prefix);
  803. }
  804. }
  805. if (rem) {
  806. prefix_line(ofd, prefix);
  807. fprintf(ofd, " [LEFTOVER] %d octets\n", rem);
  808. }
  809. }
  810. /**
  811. * Dump message in human readable format to file descriptor
  812. * @arg msg Message to print
  813. * @arg ofd File descriptor.
  814. */
  815. void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
  816. {
  817. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  818. fprintf(ofd,
  819. "-------------------------- BEGIN NETLINK MESSAGE "
  820. "---------------------------\n");
  821. fprintf(ofd, " [HEADER] %Zu octets\n", sizeof(struct nlmsghdr));
  822. print_hdr(ofd, msg);
  823. if (hdr->nlmsg_type == NLMSG_ERROR &&
  824. hdr->nlmsg_len >= nlmsg_msg_size(sizeof(struct nlmsgerr))) {
  825. struct nl_msg *errmsg;
  826. struct nlmsgerr *err = nlmsg_data(hdr);
  827. char buf[256];
  828. fprintf(ofd, " [ERRORMSG] %Zu octets\n", sizeof(*err));
  829. fprintf(ofd, " .error = %d \"%s\"\n", err->error,
  830. strerror_r(-err->error, buf, sizeof(buf)));
  831. fprintf(ofd, " [ORIGINAL MESSAGE] %Zu octets\n", sizeof(*hdr));
  832. errmsg = nlmsg_inherit(&err->msg);
  833. print_hdr(ofd, errmsg);
  834. nlmsg_free(errmsg);
  835. } else if (nlmsg_len(hdr) > 0) {
  836. struct nl_cache_ops *ops;
  837. int payloadlen = nlmsg_len(hdr);
  838. int attrlen = 0;
  839. ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
  840. hdr->nlmsg_type);
  841. if (ops) {
  842. attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
  843. payloadlen -= attrlen;
  844. }
  845. fprintf(ofd, " [PAYLOAD] %d octets\n", payloadlen);
  846. dump_hex(ofd, nlmsg_data(hdr), payloadlen, 0);
  847. if (attrlen) {
  848. struct nlattr *attrs;
  849. int attrlen;
  850. attrs = nlmsg_attrdata(hdr, ops->co_hdrsize);
  851. attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
  852. dump_attrs(ofd, attrs, attrlen, 0);
  853. }
  854. }
  855. fprintf(ofd,
  856. "--------------------------- END NETLINK MESSAGE "
  857. "---------------------------\n");
  858. }
  859. /** @} */
  860. /** @} */