nl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * lib/nl.c Core Netlink 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. * @defgroup nl Core Netlink API
  13. * @brief
  14. *
  15. * @par Receiving Semantics
  16. * @code
  17. * nl_recvmsgs_default(socket)
  18. * |
  19. * | cb = nl_socket_get_cb(socket)
  20. * v
  21. * nl_recvmsgs(socket, cb)
  22. * | [Application provides nl_recvmsgs() replacement]
  23. * |- - - - - - - - - - - - - - - v
  24. * | cb->cb_recvmsgs_ow()
  25. * |
  26. * | [Application provides nl_recv() replacement]
  27. * +-------------->|- - - - - - - - - - - - - - - v
  28. * | nl_recv() cb->cb_recv_ow()
  29. * | +----------->|<- - - - - - - - - - - - - - -+
  30. * | | v
  31. * | | Parse Message
  32. * | | |- - - - - - - - - - - - - - - v
  33. * | | | NL_CB_MSG_IN()
  34. * | | |<- - - - - - - - - - - - - - -+
  35. * | | |
  36. * | | |- - - - - - - - - - - - - - - v
  37. * | | Sequence Check NL_CB_SEQ_CHECK()
  38. * | | |<- - - - - - - - - - - - - - -+
  39. * | | |
  40. * | | |- - - - - - - - - - - - - - - v [ NLM_F_ACK is set ]
  41. * | | | NL_CB_SEND_ACK()
  42. * | | |<- - - - - - - - - - - - - - -+
  43. * | | |
  44. * | | +-----+------+--------------+----------------+--------------+
  45. * | | v v v v v
  46. * | | Valid Message ACK NOOP Message End of Multipart Error Message
  47. * | | | | | | |
  48. * | | v v v v v
  49. * | |NL_CB_VALID() NL_CB_ACK() NL_CB_SKIPPED() NL_CB_FINISH() cb->cb_err()
  50. * | | | | | | |
  51. * | | +------------+--------------+----------------+ v
  52. * | | | (FAILURE)
  53. * | | | [Callback returned NL_SKIP]
  54. * | | [More messages to be parsed] |<-----------
  55. * | +----------------------------------|
  56. * | |
  57. * | [Multipart message] |
  58. * +-------------------------------------| [Callback returned NL_STOP]
  59. * |<-----------
  60. * v
  61. * (SUCCESS)
  62. *
  63. * At any time:
  64. * Message Format Error
  65. * |- - - - - - - - - - - - v
  66. * v NL_CB_INVALID()
  67. * (FAILURE)
  68. *
  69. * Message Overrun (Kernel Lost Data)
  70. * |- - - - - - - - - - - - v
  71. * v NL_CB_OVERRUN()
  72. * (FAILURE)
  73. *
  74. * Callback returned negative error code
  75. * (FAILURE)
  76. * @endcode
  77. *
  78. * @par Sending Semantics
  79. * @code
  80. * nl_send_auto_complete()
  81. * |
  82. * | Automatically fill in PID and/or sequence number
  83. * |
  84. * | [Application provides nl_send() replacement]
  85. * |- - - - - - - - - - - - - - - - - - - - v
  86. * v cb->cb_send_ow()
  87. * nl_send()
  88. * | Add destination address and credentials
  89. * v
  90. * nl_sendmsg()
  91. * | Set source address
  92. * |
  93. * |- - - - - - - - - - - - - - - - - - - - v
  94. * | NL_CB_MSG_OUT()
  95. * |<- - - - - - - - - - - - - - - - - - - -+
  96. * v
  97. * sendmsg()
  98. * @endcode
  99. *
  100. * @par 1) Connecting the socket
  101. * @code
  102. * // Bind and connect the socket to a protocol, NETLINK_ROUTE in this example.
  103. * nl_connect(handle, NETLINK_ROUTE);
  104. * @endcode
  105. *
  106. * @par 2) Sending data
  107. * @code
  108. * // The most rudimentary method is to use nl_sendto() simply pushing
  109. * // a piece of data to the other netlink peer. This method is not
  110. * // recommended.
  111. * const char buf[] = { 0x01, 0x02, 0x03, 0x04 };
  112. * nl_sendto(handle, buf, sizeof(buf));
  113. *
  114. * // A more comfortable interface is nl_send() taking a pointer to
  115. * // a netlink message.
  116. * struct nl_msg *msg = my_msg_builder();
  117. * nl_send(handle, nlmsg_hdr(msg));
  118. *
  119. * // nl_sendmsg() provides additional control over the sendmsg() message
  120. * // header in order to allow more specific addressing of multiple peers etc.
  121. * struct msghdr hdr = { ... };
  122. * nl_sendmsg(handle, nlmsg_hdr(msg), &hdr);
  123. *
  124. * // You're probably too lazy to fill out the netlink pid, sequence number
  125. * // and message flags all the time. nl_send_auto_complete() automatically
  126. * // extends your message header as needed with an appropriate sequence
  127. * // number, the netlink pid stored in the netlink handle and the message
  128. * // flags NLM_F_REQUEST and NLM_F_ACK
  129. * nl_send_auto_complete(handle, nlmsg_hdr(msg));
  130. *
  131. * // Simple protocols don't require the complex message construction interface
  132. * // and may favour nl_send_simple() to easly send a bunch of payload
  133. * // encapsulated in a netlink message header.
  134. * nl_send_simple(handle, MY_MSG_TYPE, 0, buf, sizeof(buf));
  135. * @endcode
  136. *
  137. * @par 3) Receiving data
  138. * @code
  139. * // nl_recv() receives a single message allocating a buffer for the message
  140. * // content and gives back the pointer to you.
  141. * struct sockaddr_nl peer;
  142. * unsigned char *msg;
  143. * nl_recv(handle, &peer, &msg);
  144. *
  145. * // nl_recvmsgs() receives a bunch of messages until the callback system
  146. * // orders it to state, usually after receving a compolete multi part
  147. * // message series.
  148. * nl_recvmsgs(handle, my_callback_configuration);
  149. *
  150. * // nl_recvmsgs_default() acts just like nl_recvmsg() but uses the callback
  151. * // configuration stored in the handle.
  152. * nl_recvmsgs_default(handle);
  153. *
  154. * // In case you want to wait for the ACK to be recieved that you requested
  155. * // with your latest message, you can call nl_wait_for_ack()
  156. * nl_wait_for_ack(handle);
  157. * @endcode
  158. *
  159. * @par 4) Closing
  160. * @code
  161. * // Close the socket first to release kernel memory
  162. * nl_close(handle);
  163. * @endcode
  164. *
  165. * @{
  166. */
  167. #include <netlink-local.h>
  168. #include <netlink/netlink.h>
  169. #include <netlink/utils.h>
  170. #include <netlink/handlers.h>
  171. #include <netlink/msg.h>
  172. #include <netlink/attr.h>
  173. /**
  174. * @name Connection Management
  175. * @{
  176. */
  177. /**
  178. * Create and connect netlink socket.
  179. * @arg handle Netlink handle.
  180. * @arg protocol Netlink protocol to use.
  181. *
  182. * Creates a netlink socket using the specified protocol, binds the socket
  183. * and issues a connection attempt.
  184. *
  185. * @return 0 on success or a negative error code.
  186. */
  187. int nl_connect(struct nl_handle *handle, int protocol)
  188. {
  189. int err;
  190. socklen_t addrlen;
  191. handle->h_fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  192. if (handle->h_fd < 0) {
  193. err = nl_error(1, "socket(AF_NETLINK, ...) failed");
  194. goto errout;
  195. }
  196. if (!(handle->h_flags & NL_SOCK_BUFSIZE_SET)) {
  197. err = nl_set_buffer_size(handle, 0, 0);
  198. if (err < 0)
  199. goto errout;
  200. }
  201. err = bind(handle->h_fd, (struct sockaddr*) &handle->h_local,
  202. sizeof(handle->h_local));
  203. if (err < 0) {
  204. err = nl_error(1, "bind() failed");
  205. goto errout;
  206. }
  207. addrlen = sizeof(handle->h_local);
  208. err = getsockname(handle->h_fd, (struct sockaddr *) &handle->h_local,
  209. &addrlen);
  210. if (err < 0) {
  211. err = nl_error(1, "getsockname failed");
  212. goto errout;
  213. }
  214. if (addrlen != sizeof(handle->h_local)) {
  215. err = nl_error(EADDRNOTAVAIL, "Invalid address length");
  216. goto errout;
  217. }
  218. if (handle->h_local.nl_family != AF_NETLINK) {
  219. err = nl_error(EPFNOSUPPORT, "Address format not supported");
  220. goto errout;
  221. }
  222. handle->h_proto = protocol;
  223. return 0;
  224. errout:
  225. close(handle->h_fd);
  226. handle->h_fd = -1;
  227. return err;
  228. }
  229. /**
  230. * Close/Disconnect netlink socket.
  231. * @arg handle Netlink handle
  232. */
  233. void nl_close(struct nl_handle *handle)
  234. {
  235. if (handle->h_fd >= 0) {
  236. close(handle->h_fd);
  237. handle->h_fd = -1;
  238. }
  239. handle->h_proto = 0;
  240. }
  241. /** @} */
  242. /**
  243. * @name Send
  244. * @{
  245. */
  246. /**
  247. * Send raw data over netlink socket.
  248. * @arg handle Netlink handle.
  249. * @arg buf Data buffer.
  250. * @arg size Size of data buffer.
  251. * @return Number of characters written on success or a negative error code.
  252. */
  253. int nl_sendto(struct nl_handle *handle, void *buf, size_t size)
  254. {
  255. int ret;
  256. ret = sendto(handle->h_fd, buf, size, 0, (struct sockaddr *)
  257. &handle->h_peer, sizeof(handle->h_peer));
  258. if (ret < 0)
  259. return nl_errno(errno);
  260. return ret;
  261. }
  262. /**
  263. * Send netlink message with control over sendmsg() message header.
  264. * @arg handle Netlink handle.
  265. * @arg msg Netlink message to be sent.
  266. * @arg hdr Sendmsg() message header.
  267. * @return Number of characters sent on sucess or a negative error code.
  268. */
  269. int nl_sendmsg(struct nl_handle *handle, struct nl_msg *msg, struct msghdr *hdr)
  270. {
  271. struct nl_cb *cb;
  272. int ret;
  273. struct iovec iov = {
  274. .iov_base = (void *) nlmsg_hdr(msg),
  275. .iov_len = nlmsg_hdr(msg)->nlmsg_len,
  276. };
  277. hdr->msg_iov = &iov;
  278. hdr->msg_iovlen = 1;
  279. nlmsg_set_src(msg, &handle->h_local);
  280. cb = handle->h_cb;
  281. if (cb->cb_set[NL_CB_MSG_OUT])
  282. if (nl_cb_call(cb, NL_CB_MSG_OUT, msg) != NL_OK)
  283. return 0;
  284. ret = sendmsg(handle->h_fd, hdr, 0);
  285. if (ret < 0)
  286. return nl_errno(errno);
  287. return ret;
  288. }
  289. /**
  290. * Send netlink message.
  291. * @arg handle Netlink handle
  292. * @arg msg Netlink message to be sent.
  293. * @see nl_sendmsg()
  294. * @return Number of characters sent on success or a negative error code.
  295. */
  296. int nl_send(struct nl_handle *handle, struct nl_msg *msg)
  297. {
  298. struct sockaddr_nl *dst;
  299. struct ucred *creds;
  300. struct msghdr hdr = {
  301. .msg_name = (void *) &handle->h_peer,
  302. .msg_namelen = sizeof(struct sockaddr_nl),
  303. };
  304. /* Overwrite destination if specified in the message itself, defaults
  305. * to the peer address of the handle.
  306. */
  307. dst = nlmsg_get_dst(msg);
  308. if (dst->nl_family == AF_NETLINK)
  309. hdr.msg_name = dst;
  310. /* Add credentials if present. */
  311. creds = nlmsg_get_creds(msg);
  312. if (creds != NULL) {
  313. char buf[CMSG_SPACE(sizeof(struct ucred))];
  314. struct cmsghdr *cmsg;
  315. hdr.msg_control = buf;
  316. hdr.msg_controllen = sizeof(buf);
  317. cmsg = CMSG_FIRSTHDR(&hdr);
  318. cmsg->cmsg_level = SOL_SOCKET;
  319. cmsg->cmsg_type = SCM_CREDENTIALS;
  320. cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
  321. memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
  322. }
  323. return nl_sendmsg(handle, msg, &hdr);
  324. }
  325. /**
  326. * Send netlink message and check & extend header values as needed.
  327. * @arg handle Netlink handle.
  328. * @arg msg Netlink message to be sent.
  329. *
  330. * Checks the netlink message \c nlh for completness and extends it
  331. * as required before sending it out. Checked fields include pid,
  332. * sequence nr, and flags.
  333. *
  334. * @see nl_send()
  335. * @return Number of characters sent or a negative error code.
  336. */
  337. int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
  338. {
  339. struct nlmsghdr *nlh;
  340. struct nl_cb *cb = handle->h_cb;
  341. nlh = nlmsg_hdr(msg);
  342. if (nlh->nlmsg_pid == 0)
  343. nlh->nlmsg_pid = handle->h_local.nl_pid;
  344. if (nlh->nlmsg_seq == 0)
  345. nlh->nlmsg_seq = handle->h_seq_next++;
  346. if (msg->nm_protocol == -1)
  347. msg->nm_protocol = handle->h_proto;
  348. nlh->nlmsg_flags |= (NLM_F_REQUEST | NLM_F_ACK);
  349. if (cb->cb_send_ow)
  350. return cb->cb_send_ow(handle, msg);
  351. else
  352. return nl_send(handle, msg);
  353. }
  354. /**
  355. * Send simple netlink message using nl_send_auto_complete()
  356. * @arg handle Netlink handle.
  357. * @arg type Netlink message type.
  358. * @arg flags Netlink message flags.
  359. * @arg buf Data buffer.
  360. * @arg size Size of data buffer.
  361. *
  362. * Builds a netlink message with the specified type and flags and
  363. * appends the specified data as payload to the message.
  364. *
  365. * @see nl_send_auto_complete()
  366. * @return Number of characters sent on success or a negative error code.
  367. */
  368. int nl_send_simple(struct nl_handle *handle, int type, int flags, void *buf,
  369. size_t size)
  370. {
  371. int err;
  372. struct nl_msg *msg;
  373. msg = nlmsg_alloc_simple(type, flags);
  374. if (!msg)
  375. return nl_errno(ENOMEM);
  376. if (buf && size) {
  377. err = nlmsg_append(msg, buf, size, NLMSG_ALIGNTO);
  378. if (err < 0)
  379. goto errout;
  380. }
  381. err = nl_send_auto_complete(handle, msg);
  382. errout:
  383. nlmsg_free(msg);
  384. return err;
  385. }
  386. /** @} */
  387. /**
  388. * @name Receive
  389. * @{
  390. */
  391. /**
  392. * Receive data from netlink socket
  393. * @arg handle Netlink handle.
  394. * @arg nla Destination pointer for peer's netlink address.
  395. * @arg buf Destination pointer for message content.
  396. * @arg creds Destination pointer for credentials.
  397. *
  398. * Receives a netlink message, allocates a buffer in \c *buf and
  399. * stores the message content. The peer's netlink address is stored
  400. * in \c *nla. The caller is responsible for freeing the buffer allocated
  401. * in \c *buf if a positive value is returned. Interruped system calls
  402. * are handled by repeating the read. The input buffer size is determined
  403. * by peeking before the actual read is done.
  404. *
  405. * A non-blocking sockets causes the function to return immediately with
  406. * a return value of 0 if no data is available.
  407. *
  408. * @return Number of octets read, 0 on EOF or a negative error code.
  409. */
  410. int nl_recv(struct nl_handle *handle, struct sockaddr_nl *nla,
  411. unsigned char **buf, struct ucred **creds)
  412. {
  413. int n;
  414. int flags = 0;
  415. static int page_size = 0;
  416. struct iovec iov;
  417. struct msghdr msg = {
  418. .msg_name = (void *) nla,
  419. .msg_namelen = sizeof(struct sockaddr_nl),
  420. .msg_iov = &iov,
  421. .msg_iovlen = 1,
  422. .msg_control = NULL,
  423. .msg_controllen = 0,
  424. .msg_flags = 0,
  425. };
  426. struct cmsghdr *cmsg;
  427. if (handle->h_flags & NL_MSG_PEEK)
  428. flags |= MSG_PEEK | MSG_TRUNC;
  429. if (page_size == 0)
  430. page_size = getpagesize() * 4;
  431. iov.iov_len = page_size;
  432. iov.iov_base = *buf = calloc(1, iov.iov_len);
  433. if (handle->h_flags & NL_SOCK_PASSCRED) {
  434. msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
  435. msg.msg_control = calloc(1, msg.msg_controllen);
  436. }
  437. retry:
  438. n = recvmsg(handle->h_fd, &msg, flags);
  439. if (!n)
  440. goto abort;
  441. else if (n < 0) {
  442. if (errno == EINTR) {
  443. NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
  444. goto retry;
  445. } else if (errno == EAGAIN) {
  446. NL_DBG(3, "recvmsg() returned EAGAIN, aborting\n");
  447. goto abort;
  448. } else {
  449. free(msg.msg_control);
  450. free(*buf);
  451. return nl_error(errno, "recvmsg failed");
  452. }
  453. }
  454. if (msg.msg_flags & MSG_CTRUNC) {
  455. msg.msg_controllen *= 2;
  456. msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
  457. goto retry;
  458. } else if (iov.iov_len < n || msg.msg_flags & MSG_TRUNC) {
  459. /* Provided buffer is not long enough, enlarge it
  460. * to size of n (which should be total length of the message)
  461. * and try again. */
  462. iov.iov_len = n;
  463. iov.iov_base = *buf = realloc(*buf, iov.iov_len);
  464. flags = 0;
  465. goto retry;
  466. } else if (flags != 0) {
  467. /* Buffer is big enough, do the actual reading */
  468. flags = 0;
  469. goto retry;
  470. }
  471. if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
  472. free(msg.msg_control);
  473. free(*buf);
  474. return nl_error(EADDRNOTAVAIL, "socket address size mismatch");
  475. }
  476. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  477. if (cmsg->cmsg_level == SOL_SOCKET &&
  478. cmsg->cmsg_type == SCM_CREDENTIALS) {
  479. *creds = calloc(1, sizeof(struct ucred));
  480. memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  481. break;
  482. }
  483. }
  484. free(msg.msg_control);
  485. return n;
  486. abort:
  487. free(msg.msg_control);
  488. free(*buf);
  489. return 0;
  490. }
  491. #define NL_CB_CALL(cb, type, msg) \
  492. do { \
  493. err = nl_cb_call(cb, type, msg); \
  494. switch (err) { \
  495. case NL_OK: \
  496. err = 0; \
  497. break; \
  498. case NL_SKIP: \
  499. goto skip; \
  500. case NL_STOP: \
  501. goto stop; \
  502. default: \
  503. goto out; \
  504. } \
  505. } while (0)
  506. static int recvmsgs(struct nl_handle *handle, struct nl_cb *cb)
  507. {
  508. int n, err = 0, multipart = 0;
  509. unsigned char *buf = NULL;
  510. struct nlmsghdr *hdr;
  511. struct sockaddr_nl nla = {0};
  512. struct nl_msg *msg = NULL;
  513. struct ucred *creds = NULL;
  514. continue_reading:
  515. NL_DBG(3, "Attempting to read from %p\n", handle);
  516. if (cb->cb_recv_ow)
  517. n = cb->cb_recv_ow(handle, &nla, &buf, &creds);
  518. else
  519. n = nl_recv(handle, &nla, &buf, &creds);
  520. if (n <= 0)
  521. return n;
  522. NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", handle, n);
  523. hdr = (struct nlmsghdr *) buf;
  524. while (nlmsg_ok(hdr, n)) {
  525. NL_DBG(3, "recgmsgs(%p): Processing valid message...\n",
  526. handle);
  527. nlmsg_free(msg);
  528. msg = nlmsg_convert(hdr);
  529. if (!msg) {
  530. err = nl_errno(ENOMEM);
  531. goto out;
  532. }
  533. nlmsg_set_proto(msg, handle->h_proto);
  534. nlmsg_set_src(msg, &nla);
  535. if (creds)
  536. nlmsg_set_creds(msg, creds);
  537. /* Raw callback is the first, it gives the most control
  538. * to the user and he can do his very own parsing. */
  539. if (cb->cb_set[NL_CB_MSG_IN])
  540. NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
  541. /* Sequence number checking. The check may be done by
  542. * the user, otherwise a very simple check is applied
  543. * enforcing strict ordering */
  544. if (cb->cb_set[NL_CB_SEQ_CHECK])
  545. NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
  546. else if (hdr->nlmsg_seq != handle->h_seq_expect) {
  547. if (cb->cb_set[NL_CB_INVALID])
  548. NL_CB_CALL(cb, NL_CB_INVALID, msg);
  549. else {
  550. err = nl_error(EINVAL,
  551. "Sequence number mismatch");
  552. goto out;
  553. }
  554. }
  555. if (hdr->nlmsg_type == NLMSG_DONE ||
  556. hdr->nlmsg_type == NLMSG_ERROR ||
  557. hdr->nlmsg_type == NLMSG_NOOP ||
  558. hdr->nlmsg_type == NLMSG_OVERRUN) {
  559. /* We can't check for !NLM_F_MULTI since some netlink
  560. * users in the kernel are broken. */
  561. handle->h_seq_expect++;
  562. NL_DBG(3, "recvmsgs(%p): Increased expected " \
  563. "sequence number to %d\n",
  564. handle, handle->h_seq_expect);
  565. }
  566. if (hdr->nlmsg_flags & NLM_F_MULTI)
  567. multipart = 1;
  568. /* Other side wishes to see an ack for this message */
  569. if (hdr->nlmsg_flags & NLM_F_ACK) {
  570. if (cb->cb_set[NL_CB_SEND_ACK])
  571. NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
  572. else {
  573. /* FIXME: implement */
  574. }
  575. }
  576. /* messages terminates a multpart message, this is
  577. * usually the end of a message and therefore we slip
  578. * out of the loop by default. the user may overrule
  579. * this action by skipping this packet. */
  580. if (hdr->nlmsg_type == NLMSG_DONE) {
  581. multipart = 0;
  582. if (cb->cb_set[NL_CB_FINISH])
  583. NL_CB_CALL(cb, NL_CB_FINISH, msg);
  584. }
  585. /* Message to be ignored, the default action is to
  586. * skip this message if no callback is specified. The
  587. * user may overrule this action by returning
  588. * NL_PROCEED. */
  589. else if (hdr->nlmsg_type == NLMSG_NOOP) {
  590. if (cb->cb_set[NL_CB_SKIPPED])
  591. NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
  592. else
  593. goto skip;
  594. }
  595. /* Data got lost, report back to user. The default action is to
  596. * quit parsing. The user may overrule this action by retuning
  597. * NL_SKIP or NL_PROCEED (dangerous) */
  598. else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
  599. if (cb->cb_set[NL_CB_OVERRUN])
  600. NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
  601. else {
  602. err = nl_error(EOVERFLOW, "Overrun");
  603. goto out;
  604. }
  605. }
  606. /* Message carries a nlmsgerr */
  607. else if (hdr->nlmsg_type == NLMSG_ERROR) {
  608. struct nlmsgerr *e = nlmsg_data(hdr);
  609. if (hdr->nlmsg_len < nlmsg_msg_size(sizeof(*e))) {
  610. /* Truncated error message, the default action
  611. * is to stop parsing. The user may overrule
  612. * this action by returning NL_SKIP or
  613. * NL_PROCEED (dangerous) */
  614. if (cb->cb_set[NL_CB_INVALID])
  615. NL_CB_CALL(cb, NL_CB_INVALID, msg);
  616. else {
  617. err = nl_error(EINVAL,
  618. "Truncated error message");
  619. goto out;
  620. }
  621. } else if (e->error) {
  622. /* Error message reported back from kernel. */
  623. if (cb->cb_err) {
  624. err = cb->cb_err(&nla, e,
  625. cb->cb_err_arg);
  626. if (err < 0)
  627. goto out;
  628. else if (err == NL_SKIP)
  629. goto skip;
  630. else if (err == NL_STOP) {
  631. err = nl_error(-e->error,
  632. "Netlink Error");
  633. goto out;
  634. }
  635. } else {
  636. err = nl_error(-e->error,
  637. "Netlink Error");
  638. goto out;
  639. }
  640. } else if (cb->cb_set[NL_CB_ACK])
  641. NL_CB_CALL(cb, NL_CB_ACK, msg);
  642. } else {
  643. /* Valid message (not checking for MULTIPART bit to
  644. * get along with broken kernels. NL_SKIP has no
  645. * effect on this. */
  646. if (cb->cb_set[NL_CB_VALID])
  647. NL_CB_CALL(cb, NL_CB_VALID, msg);
  648. }
  649. skip:
  650. err = 0;
  651. hdr = nlmsg_next(hdr, &n);
  652. }
  653. nlmsg_free(msg);
  654. free(buf);
  655. free(creds);
  656. buf = NULL;
  657. msg = NULL;
  658. creds = NULL;
  659. if (multipart) {
  660. /* Multipart message not yet complete, continue reading */
  661. goto continue_reading;
  662. }
  663. stop:
  664. err = 0;
  665. out:
  666. nlmsg_free(msg);
  667. free(buf);
  668. free(creds);
  669. return err;
  670. }
  671. /**
  672. * Receive a set of messages from a netlink socket.
  673. * @arg handle netlink handle
  674. * @arg cb set of callbacks to control behaviour.
  675. *
  676. * Repeatedly calls nl_recv() or the respective replacement if provided
  677. * by the application (see nl_cb_overwrite_recv()) and parses the
  678. * received data as netlink messages. Stops reading if one of the
  679. * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
  680. *
  681. * A non-blocking sockets causes the function to return immediately if
  682. * no data is available.
  683. *
  684. * @return 0 on success or a negative error code from nl_recv().
  685. */
  686. int nl_recvmsgs(struct nl_handle *handle, struct nl_cb *cb)
  687. {
  688. if (cb->cb_recvmsgs_ow)
  689. return cb->cb_recvmsgs_ow(handle, cb);
  690. else
  691. return recvmsgs(handle, cb);
  692. }
  693. /**
  694. * Receive a set of message from a netlink socket using handlers in nl_handle.
  695. * @arg handle netlink handle
  696. *
  697. * Calls nl_recvmsgs() with the handlers configured in the netlink handle.
  698. */
  699. int nl_recvmsgs_default(struct nl_handle *handle)
  700. {
  701. return nl_recvmsgs(handle, handle->h_cb);
  702. }
  703. static int ack_wait_handler(struct nl_msg *msg, void *arg)
  704. {
  705. return NL_STOP;
  706. }
  707. /**
  708. * Wait for ACK.
  709. * @arg handle netlink handle
  710. * @pre The netlink socket must be in blocking state.
  711. *
  712. * Waits until an ACK is received for the latest not yet acknowledged
  713. * netlink message.
  714. */
  715. int nl_wait_for_ack(struct nl_handle *handle)
  716. {
  717. int err;
  718. struct nl_cb *cb;
  719. cb = nl_cb_clone(handle->h_cb);
  720. if (cb == NULL)
  721. return nl_get_errno();
  722. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
  723. err = nl_recvmsgs(handle, cb);
  724. nl_cb_put(cb);
  725. return err;
  726. }
  727. /** @} */
  728. /** @} */