PageRenderTime 21ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/package/libs/libnl-tiny/src/nl.c

https://gitlab.com/YoungCereal/openwrt-bpi-r1
C | 720 lines | 392 code | 91 blank | 237 comment | 116 complexity | 6bed20efcff7a14f154aa4a1090117f2 MD5 | raw file
  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. socklen_t addrlen;
  106. sk->s_fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  107. if (sk->s_fd < 0) {
  108. err = -nl_syserr2nlerr(errno);
  109. goto errout;
  110. }
  111. if (!(sk->s_flags & NL_SOCK_BUFSIZE_SET)) {
  112. err = nl_socket_set_buffer_size(sk, 0, 0);
  113. if (err < 0)
  114. goto errout;
  115. }
  116. err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
  117. sizeof(sk->s_local));
  118. if (err < 0) {
  119. err = -nl_syserr2nlerr(errno);
  120. goto errout;
  121. }
  122. addrlen = sizeof(sk->s_local);
  123. err = getsockname(sk->s_fd, (struct sockaddr *) &sk->s_local,
  124. &addrlen);
  125. if (err < 0) {
  126. err = -nl_syserr2nlerr(errno);
  127. goto errout;
  128. }
  129. if (addrlen != sizeof(sk->s_local)) {
  130. err = -NLE_NOADDR;
  131. goto errout;
  132. }
  133. if (sk->s_local.nl_family != AF_NETLINK) {
  134. err = -NLE_AF_NOSUPPORT;
  135. goto errout;
  136. }
  137. sk->s_proto = protocol;
  138. return 0;
  139. errout:
  140. close(sk->s_fd);
  141. sk->s_fd = -1;
  142. return err;
  143. }
  144. /**
  145. * Close/Disconnect netlink socket.
  146. * @arg sk Netlink socket.
  147. */
  148. void nl_close(struct nl_sock *sk)
  149. {
  150. if (sk->s_fd >= 0) {
  151. close(sk->s_fd);
  152. sk->s_fd = -1;
  153. }
  154. sk->s_proto = 0;
  155. }
  156. /** @} */
  157. /**
  158. * @name Send
  159. * @{
  160. */
  161. /**
  162. * Send raw data over netlink socket.
  163. * @arg sk Netlink socket.
  164. * @arg buf Data buffer.
  165. * @arg size Size of data buffer.
  166. * @return Number of characters written on success or a negative error code.
  167. */
  168. int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
  169. {
  170. int ret;
  171. ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *)
  172. &sk->s_peer, sizeof(sk->s_peer));
  173. if (ret < 0)
  174. return -nl_syserr2nlerr(errno);
  175. return ret;
  176. }
  177. /**
  178. * Send netlink message with control over sendmsg() message header.
  179. * @arg sk Netlink socket.
  180. * @arg msg Netlink message to be sent.
  181. * @arg hdr Sendmsg() message header.
  182. * @return Number of characters sent on sucess or a negative error code.
  183. */
  184. int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
  185. {
  186. struct nl_cb *cb;
  187. int ret;
  188. struct iovec iov = {
  189. .iov_base = (void *) nlmsg_hdr(msg),
  190. .iov_len = nlmsg_hdr(msg)->nlmsg_len,
  191. };
  192. hdr->msg_iov = &iov;
  193. hdr->msg_iovlen = 1;
  194. nlmsg_set_src(msg, &sk->s_local);
  195. cb = sk->s_cb;
  196. if (cb->cb_set[NL_CB_MSG_OUT])
  197. if (nl_cb_call(cb, NL_CB_MSG_OUT, msg) != NL_OK)
  198. return 0;
  199. ret = sendmsg(sk->s_fd, hdr, 0);
  200. if (ret < 0)
  201. return -nl_syserr2nlerr(errno);
  202. return ret;
  203. }
  204. /**
  205. * Send netlink message.
  206. * @arg sk Netlink socket.
  207. * @arg msg Netlink message to be sent.
  208. * @see nl_sendmsg()
  209. * @return Number of characters sent on success or a negative error code.
  210. */
  211. int nl_send(struct nl_sock *sk, struct nl_msg *msg)
  212. {
  213. struct sockaddr_nl *dst;
  214. struct ucred *creds;
  215. struct msghdr hdr = {
  216. .msg_name = (void *) &sk->s_peer,
  217. .msg_namelen = sizeof(struct sockaddr_nl),
  218. };
  219. /* Overwrite destination if specified in the message itself, defaults
  220. * to the peer address of the socket.
  221. */
  222. dst = nlmsg_get_dst(msg);
  223. if (dst->nl_family == AF_NETLINK)
  224. hdr.msg_name = dst;
  225. /* Add credentials if present. */
  226. creds = nlmsg_get_creds(msg);
  227. if (creds != NULL) {
  228. char buf[CMSG_SPACE(sizeof(struct ucred))];
  229. struct cmsghdr *cmsg;
  230. hdr.msg_control = buf;
  231. hdr.msg_controllen = sizeof(buf);
  232. cmsg = CMSG_FIRSTHDR(&hdr);
  233. cmsg->cmsg_level = SOL_SOCKET;
  234. cmsg->cmsg_type = SCM_CREDENTIALS;
  235. cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
  236. memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
  237. }
  238. return nl_sendmsg(sk, msg, &hdr);
  239. }
  240. /**
  241. * Send netlink message and check & extend header values as needed.
  242. * @arg sk Netlink socket.
  243. * @arg msg Netlink message to be sent.
  244. *
  245. * Checks the netlink message \c nlh for completness and extends it
  246. * as required before sending it out. Checked fields include pid,
  247. * sequence nr, and flags.
  248. *
  249. * @see nl_send()
  250. * @return Number of characters sent or a negative error code.
  251. */
  252. int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
  253. {
  254. struct nlmsghdr *nlh;
  255. struct nl_cb *cb = sk->s_cb;
  256. nlh = nlmsg_hdr(msg);
  257. if (nlh->nlmsg_pid == 0)
  258. nlh->nlmsg_pid = sk->s_local.nl_pid;
  259. if (nlh->nlmsg_seq == 0)
  260. nlh->nlmsg_seq = sk->s_seq_next++;
  261. if (msg->nm_protocol == -1)
  262. msg->nm_protocol = sk->s_proto;
  263. nlh->nlmsg_flags |= NLM_F_REQUEST;
  264. if (!(sk->s_flags & NL_NO_AUTO_ACK))
  265. nlh->nlmsg_flags |= NLM_F_ACK;
  266. if (cb->cb_send_ow)
  267. return cb->cb_send_ow(sk, msg);
  268. else
  269. return nl_send(sk, msg);
  270. }
  271. /**
  272. * Send simple netlink message using nl_send_auto_complete()
  273. * @arg sk Netlink socket.
  274. * @arg type Netlink message type.
  275. * @arg flags Netlink message flags.
  276. * @arg buf Data buffer.
  277. * @arg size Size of data buffer.
  278. *
  279. * Builds a netlink message with the specified type and flags and
  280. * appends the specified data as payload to the message.
  281. *
  282. * @see nl_send_auto_complete()
  283. * @return Number of characters sent on success or a negative error code.
  284. */
  285. int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf,
  286. size_t size)
  287. {
  288. int err;
  289. struct nl_msg *msg;
  290. msg = nlmsg_alloc_simple(type, flags);
  291. if (!msg)
  292. return -NLE_NOMEM;
  293. if (buf && size) {
  294. err = nlmsg_append(msg, buf, size, NLMSG_ALIGNTO);
  295. if (err < 0)
  296. goto errout;
  297. }
  298. err = nl_send_auto_complete(sk, msg);
  299. errout:
  300. nlmsg_free(msg);
  301. return err;
  302. }
  303. /** @} */
  304. /**
  305. * @name Receive
  306. * @{
  307. */
  308. /**
  309. * Receive data from netlink socket
  310. * @arg sk Netlink socket.
  311. * @arg nla Destination pointer for peer's netlink address.
  312. * @arg buf Destination pointer for message content.
  313. * @arg creds Destination pointer for credentials.
  314. *
  315. * Receives a netlink message, allocates a buffer in \c *buf and
  316. * stores the message content. The peer's netlink address is stored
  317. * in \c *nla. The caller is responsible for freeing the buffer allocated
  318. * in \c *buf if a positive value is returned. Interruped system calls
  319. * are handled by repeating the read. The input buffer size is determined
  320. * by peeking before the actual read is done.
  321. *
  322. * A non-blocking sockets causes the function to return immediately with
  323. * a return value of 0 if no data is available.
  324. *
  325. * @return Number of octets read, 0 on EOF or a negative error code.
  326. */
  327. int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
  328. unsigned char **buf, struct ucred **creds)
  329. {
  330. int n;
  331. int flags = 0;
  332. static int page_size = 0;
  333. struct iovec iov;
  334. struct msghdr msg = {
  335. .msg_name = (void *) nla,
  336. .msg_namelen = sizeof(struct sockaddr_nl),
  337. .msg_iov = &iov,
  338. .msg_iovlen = 1,
  339. .msg_control = NULL,
  340. .msg_controllen = 0,
  341. .msg_flags = 0,
  342. };
  343. struct cmsghdr *cmsg;
  344. if (sk->s_flags & NL_MSG_PEEK)
  345. flags |= MSG_PEEK;
  346. if (page_size == 0)
  347. page_size = getpagesize() * 4;
  348. iov.iov_len = page_size;
  349. iov.iov_base = *buf = malloc(iov.iov_len);
  350. if (sk->s_flags & NL_SOCK_PASSCRED) {
  351. msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
  352. msg.msg_control = calloc(1, msg.msg_controllen);
  353. }
  354. retry:
  355. n = recvmsg(sk->s_fd, &msg, flags);
  356. if (!n)
  357. goto abort;
  358. else if (n < 0) {
  359. if (errno == EINTR) {
  360. NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
  361. goto retry;
  362. } else if (errno == EAGAIN) {
  363. NL_DBG(3, "recvmsg() returned EAGAIN, aborting\n");
  364. goto abort;
  365. } else {
  366. free(msg.msg_control);
  367. free(*buf);
  368. return -nl_syserr2nlerr(errno);
  369. }
  370. }
  371. if (iov.iov_len < n ||
  372. msg.msg_flags & MSG_TRUNC) {
  373. /* Provided buffer is not long enough, enlarge it
  374. * and try again. */
  375. iov.iov_len *= 2;
  376. iov.iov_base = *buf = realloc(*buf, iov.iov_len);
  377. goto retry;
  378. } else if (msg.msg_flags & MSG_CTRUNC) {
  379. msg.msg_controllen *= 2;
  380. msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
  381. goto retry;
  382. } else if (flags != 0) {
  383. /* Buffer is big enough, do the actual reading */
  384. flags = 0;
  385. goto retry;
  386. }
  387. if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
  388. free(msg.msg_control);
  389. free(*buf);
  390. return -NLE_NOADDR;
  391. }
  392. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  393. if (cmsg->cmsg_level == SOL_SOCKET &&
  394. cmsg->cmsg_type == SCM_CREDENTIALS) {
  395. *creds = calloc(1, sizeof(struct ucred));
  396. memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  397. break;
  398. }
  399. }
  400. free(msg.msg_control);
  401. return n;
  402. abort:
  403. free(msg.msg_control);
  404. free(*buf);
  405. return 0;
  406. }
  407. #define NL_CB_CALL(cb, type, msg) \
  408. do { \
  409. err = nl_cb_call(cb, type, msg); \
  410. switch (err) { \
  411. case NL_OK: \
  412. err = 0; \
  413. break; \
  414. case NL_SKIP: \
  415. goto skip; \
  416. case NL_STOP: \
  417. goto stop; \
  418. default: \
  419. goto out; \
  420. } \
  421. } while (0)
  422. static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
  423. {
  424. int n, err = 0, multipart = 0;
  425. unsigned char *buf = NULL;
  426. struct nlmsghdr *hdr;
  427. struct sockaddr_nl nla = {0};
  428. struct nl_msg *msg = NULL;
  429. struct ucred *creds = NULL;
  430. continue_reading:
  431. NL_DBG(3, "Attempting to read from %p\n", sk);
  432. if (cb->cb_recv_ow)
  433. n = cb->cb_recv_ow(sk, &nla, &buf, &creds);
  434. else
  435. n = nl_recv(sk, &nla, &buf, &creds);
  436. if (n <= 0)
  437. return n;
  438. NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n);
  439. hdr = (struct nlmsghdr *) buf;
  440. while (nlmsg_ok(hdr, n)) {
  441. NL_DBG(3, "recgmsgs(%p): Processing valid message...\n", sk);
  442. nlmsg_free(msg);
  443. msg = nlmsg_convert(hdr);
  444. if (!msg) {
  445. err = -NLE_NOMEM;
  446. goto out;
  447. }
  448. nlmsg_set_proto(msg, sk->s_proto);
  449. nlmsg_set_src(msg, &nla);
  450. if (creds)
  451. nlmsg_set_creds(msg, creds);
  452. /* Raw callback is the first, it gives the most control
  453. * to the user and he can do his very own parsing. */
  454. if (cb->cb_set[NL_CB_MSG_IN])
  455. NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
  456. /* Sequence number checking. The check may be done by
  457. * the user, otherwise a very simple check is applied
  458. * enforcing strict ordering */
  459. if (cb->cb_set[NL_CB_SEQ_CHECK])
  460. NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
  461. else if (hdr->nlmsg_seq != sk->s_seq_expect) {
  462. if (cb->cb_set[NL_CB_INVALID])
  463. NL_CB_CALL(cb, NL_CB_INVALID, msg);
  464. else {
  465. err = -NLE_SEQ_MISMATCH;
  466. goto out;
  467. }
  468. }
  469. if (hdr->nlmsg_type == NLMSG_DONE ||
  470. hdr->nlmsg_type == NLMSG_ERROR ||
  471. hdr->nlmsg_type == NLMSG_NOOP ||
  472. hdr->nlmsg_type == NLMSG_OVERRUN) {
  473. /* We can't check for !NLM_F_MULTI since some netlink
  474. * users in the kernel are broken. */
  475. sk->s_seq_expect++;
  476. NL_DBG(3, "recvmsgs(%p): Increased expected " \
  477. "sequence number to %d\n",
  478. sk, sk->s_seq_expect);
  479. }
  480. if (hdr->nlmsg_flags & NLM_F_MULTI)
  481. multipart = 1;
  482. /* Other side wishes to see an ack for this message */
  483. if (hdr->nlmsg_flags & NLM_F_ACK) {
  484. if (cb->cb_set[NL_CB_SEND_ACK])
  485. NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
  486. else {
  487. /* FIXME: implement */
  488. }
  489. }
  490. /* messages terminates a multpart message, this is
  491. * usually the end of a message and therefore we slip
  492. * out of the loop by default. the user may overrule
  493. * this action by skipping this packet. */
  494. if (hdr->nlmsg_type == NLMSG_DONE) {
  495. multipart = 0;
  496. if (cb->cb_set[NL_CB_FINISH])
  497. NL_CB_CALL(cb, NL_CB_FINISH, msg);
  498. }
  499. /* Message to be ignored, the default action is to
  500. * skip this message if no callback is specified. The
  501. * user may overrule this action by returning
  502. * NL_PROCEED. */
  503. else if (hdr->nlmsg_type == NLMSG_NOOP) {
  504. if (cb->cb_set[NL_CB_SKIPPED])
  505. NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
  506. else
  507. goto skip;
  508. }
  509. /* Data got lost, report back to user. The default action is to
  510. * quit parsing. The user may overrule this action by retuning
  511. * NL_SKIP or NL_PROCEED (dangerous) */
  512. else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
  513. if (cb->cb_set[NL_CB_OVERRUN])
  514. NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
  515. else {
  516. err = -NLE_MSG_OVERFLOW;
  517. goto out;
  518. }
  519. }
  520. /* Message carries a nlmsgerr */
  521. else if (hdr->nlmsg_type == NLMSG_ERROR) {
  522. struct nlmsgerr *e = nlmsg_data(hdr);
  523. if (hdr->nlmsg_len < nlmsg_msg_size(sizeof(*e))) {
  524. /* Truncated error message, the default action
  525. * is to stop parsing. The user may overrule
  526. * this action by returning NL_SKIP or
  527. * NL_PROCEED (dangerous) */
  528. if (cb->cb_set[NL_CB_INVALID])
  529. NL_CB_CALL(cb, NL_CB_INVALID, msg);
  530. else {
  531. err = -NLE_MSG_TRUNC;
  532. goto out;
  533. }
  534. } else if (e->error) {
  535. /* Error message reported back from kernel. */
  536. if (cb->cb_err) {
  537. err = cb->cb_err(&nla, e,
  538. cb->cb_err_arg);
  539. if (err < 0)
  540. goto out;
  541. else if (err == NL_SKIP)
  542. goto skip;
  543. else if (err == NL_STOP) {
  544. err = -nl_syserr2nlerr(e->error);
  545. goto out;
  546. }
  547. } else {
  548. err = -nl_syserr2nlerr(e->error);
  549. goto out;
  550. }
  551. } else if (cb->cb_set[NL_CB_ACK])
  552. NL_CB_CALL(cb, NL_CB_ACK, msg);
  553. } else {
  554. /* Valid message (not checking for MULTIPART bit to
  555. * get along with broken kernels. NL_SKIP has no
  556. * effect on this. */
  557. if (cb->cb_set[NL_CB_VALID])
  558. NL_CB_CALL(cb, NL_CB_VALID, msg);
  559. }
  560. skip:
  561. err = 0;
  562. hdr = nlmsg_next(hdr, &n);
  563. }
  564. nlmsg_free(msg);
  565. free(buf);
  566. free(creds);
  567. buf = NULL;
  568. msg = NULL;
  569. creds = NULL;
  570. if (multipart) {
  571. /* Multipart message not yet complete, continue reading */
  572. goto continue_reading;
  573. }
  574. stop:
  575. err = 0;
  576. out:
  577. nlmsg_free(msg);
  578. free(buf);
  579. free(creds);
  580. return err;
  581. }
  582. /**
  583. * Receive a set of messages from a netlink socket.
  584. * @arg sk Netlink socket.
  585. * @arg cb set of callbacks to control behaviour.
  586. *
  587. * Repeatedly calls nl_recv() or the respective replacement if provided
  588. * by the application (see nl_cb_overwrite_recv()) and parses the
  589. * received data as netlink messages. Stops reading if one of the
  590. * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
  591. *
  592. * A non-blocking sockets causes the function to return immediately if
  593. * no data is available.
  594. *
  595. * @return 0 on success or a negative error code from nl_recv().
  596. */
  597. int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
  598. {
  599. if (cb->cb_recvmsgs_ow)
  600. return cb->cb_recvmsgs_ow(sk, cb);
  601. else
  602. return recvmsgs(sk, cb);
  603. }
  604. static int ack_wait_handler(struct nl_msg *msg, void *arg)
  605. {
  606. return NL_STOP;
  607. }
  608. /**
  609. * Wait for ACK.
  610. * @arg sk Netlink socket.
  611. * @pre The netlink socket must be in blocking state.
  612. *
  613. * Waits until an ACK is received for the latest not yet acknowledged
  614. * netlink message.
  615. */
  616. int nl_wait_for_ack(struct nl_sock *sk)
  617. {
  618. int err;
  619. struct nl_cb *cb;
  620. cb = nl_cb_clone(sk->s_cb);
  621. if (cb == NULL)
  622. return -NLE_NOMEM;
  623. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
  624. err = nl_recvmsgs(sk, cb);
  625. nl_cb_put(cb);
  626. return err;
  627. }
  628. /** @} */
  629. /** @} */