1
0

nl.c 18 KB

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