nl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. Interrupted 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 = calloc(1, iov.iov_len);
  350. if (!*buf)
  351. return -nl_syserr2nlerr(errno);
  352. if (sk->s_flags & NL_SOCK_PASSCRED) {
  353. msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
  354. msg.msg_control = calloc(1, msg.msg_controllen);
  355. }
  356. retry:
  357. n = recvmsg(sk->s_fd, &msg, flags);
  358. if (!n)
  359. goto abort;
  360. else if (n < 0) {
  361. if (errno == EINTR) {
  362. NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
  363. goto retry;
  364. } else if (errno == EAGAIN) {
  365. NL_DBG(3, "recvmsg() returned EAGAIN, aborting\n");
  366. goto abort;
  367. } else {
  368. free(msg.msg_control);
  369. free(*buf);
  370. *buf = NULL;
  371. return -nl_syserr2nlerr(errno);
  372. }
  373. }
  374. if (iov.iov_len < (size_t) n ||
  375. msg.msg_flags & MSG_TRUNC) {
  376. /* Provided buffer is not long enough, enlarge it
  377. * and try again. */
  378. iov.iov_len *= 2;
  379. iov.iov_base = *buf = realloc(*buf, iov.iov_len);
  380. goto retry;
  381. } else if (msg.msg_flags & MSG_CTRUNC) {
  382. msg.msg_controllen *= 2;
  383. msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
  384. goto retry;
  385. } else if (flags != 0) {
  386. /* Buffer is big enough, do the actual reading */
  387. flags = 0;
  388. goto retry;
  389. }
  390. if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
  391. free(msg.msg_control);
  392. free(*buf);
  393. *buf = NULL;
  394. return -NLE_NOADDR;
  395. }
  396. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  397. if (cmsg->cmsg_level == SOL_SOCKET &&
  398. cmsg->cmsg_type == SCM_CREDENTIALS) {
  399. *creds = calloc(1, sizeof(struct ucred));
  400. memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  401. break;
  402. }
  403. }
  404. free(msg.msg_control);
  405. return n;
  406. abort:
  407. free(msg.msg_control);
  408. free(*buf);
  409. *buf = NULL;
  410. return 0;
  411. }
  412. #define NL_CB_CALL(cb, type, msg) \
  413. do { \
  414. err = nl_cb_call(cb, type, msg); \
  415. switch (err) { \
  416. case NL_OK: \
  417. err = 0; \
  418. break; \
  419. case NL_SKIP: \
  420. goto skip; \
  421. case NL_STOP: \
  422. goto stop; \
  423. default: \
  424. goto out; \
  425. } \
  426. } while (0)
  427. static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
  428. {
  429. int n, err = 0, multipart = 0;
  430. unsigned char *buf = NULL;
  431. struct nlmsghdr *hdr;
  432. struct sockaddr_nl nla = {0};
  433. struct nl_msg *msg = NULL;
  434. struct ucred *creds = NULL;
  435. continue_reading:
  436. NL_DBG(3, "Attempting to read from %p\n", sk);
  437. if (cb->cb_recv_ow)
  438. n = cb->cb_recv_ow(sk, &nla, &buf, &creds);
  439. else
  440. n = nl_recv(sk, &nla, &buf, &creds);
  441. if (n <= 0)
  442. return n;
  443. /* make clang analyzer happy */
  444. assert(n > 0 && buf);
  445. NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n);
  446. hdr = (struct nlmsghdr *) buf;
  447. while (nlmsg_ok(hdr, n)) {
  448. NL_DBG(3, "recgmsgs(%p): Processing valid message...\n", sk);
  449. nlmsg_free(msg);
  450. msg = nlmsg_convert(hdr);
  451. if (!msg) {
  452. err = -NLE_NOMEM;
  453. goto out;
  454. }
  455. nlmsg_set_proto(msg, sk->s_proto);
  456. nlmsg_set_src(msg, &nla);
  457. if (creds)
  458. nlmsg_set_creds(msg, creds);
  459. /* Raw callback is the first, it gives the most control
  460. * to the user and he can do his very own parsing. */
  461. if (cb->cb_set[NL_CB_MSG_IN])
  462. NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
  463. /* Sequence number checking. The check may be done by
  464. * the user, otherwise a very simple check is applied
  465. * enforcing strict ordering */
  466. if (cb->cb_set[NL_CB_SEQ_CHECK])
  467. NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
  468. else if (hdr->nlmsg_seq != sk->s_seq_expect) {
  469. if (cb->cb_set[NL_CB_INVALID])
  470. NL_CB_CALL(cb, NL_CB_INVALID, msg);
  471. else {
  472. err = -NLE_SEQ_MISMATCH;
  473. goto out;
  474. }
  475. }
  476. if (hdr->nlmsg_type == NLMSG_DONE ||
  477. hdr->nlmsg_type == NLMSG_ERROR ||
  478. hdr->nlmsg_type == NLMSG_NOOP ||
  479. hdr->nlmsg_type == NLMSG_OVERRUN) {
  480. /* We can't check for !NLM_F_MULTI since some netlink
  481. * users in the kernel are broken. */
  482. sk->s_seq_expect++;
  483. NL_DBG(3, "recvmsgs(%p): Increased expected " \
  484. "sequence number to %d\n",
  485. sk, sk->s_seq_expect);
  486. }
  487. if (hdr->nlmsg_flags & NLM_F_MULTI)
  488. multipart = 1;
  489. /* Other side wishes to see an ack for this message */
  490. if (hdr->nlmsg_flags & NLM_F_ACK) {
  491. if (cb->cb_set[NL_CB_SEND_ACK])
  492. NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
  493. else {
  494. /* FIXME: implement */
  495. }
  496. }
  497. /* messages terminates a multpart message, this is
  498. * usually the end of a message and therefore we slip
  499. * out of the loop by default. the user may overrule
  500. * this action by skipping this packet. */
  501. if (hdr->nlmsg_type == NLMSG_DONE) {
  502. multipart = 0;
  503. if (cb->cb_set[NL_CB_FINISH])
  504. NL_CB_CALL(cb, NL_CB_FINISH, msg);
  505. }
  506. /* Message to be ignored, the default action is to
  507. * skip this message if no callback is specified. The
  508. * user may overrule this action by returning
  509. * NL_PROCEED. */
  510. else if (hdr->nlmsg_type == NLMSG_NOOP) {
  511. if (cb->cb_set[NL_CB_SKIPPED])
  512. NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
  513. else
  514. goto skip;
  515. }
  516. /* Data got lost, report back to user. The default action is to
  517. * quit parsing. The user may overrule this action by retuning
  518. * NL_SKIP or NL_PROCEED (dangerous) */
  519. else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
  520. if (cb->cb_set[NL_CB_OVERRUN])
  521. NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
  522. else {
  523. err = -NLE_MSG_OVERFLOW;
  524. goto out;
  525. }
  526. }
  527. /* Message carries a nlmsgerr */
  528. else if (hdr->nlmsg_type == NLMSG_ERROR) {
  529. struct nlmsgerr *e = nlmsg_data(hdr);
  530. if (hdr->nlmsg_len < (unsigned) nlmsg_msg_size(sizeof(*e))) {
  531. /* Truncated error message, the default action
  532. * is to stop parsing. The user may overrule
  533. * this action by returning NL_SKIP or
  534. * NL_PROCEED (dangerous) */
  535. if (cb->cb_set[NL_CB_INVALID])
  536. NL_CB_CALL(cb, NL_CB_INVALID, msg);
  537. else {
  538. err = -NLE_MSG_TRUNC;
  539. goto out;
  540. }
  541. } else if (e->error) {
  542. /* Error message reported back from kernel. */
  543. if (cb->cb_err) {
  544. err = cb->cb_err(&nla, e,
  545. cb->cb_err_arg);
  546. if (err < 0)
  547. goto out;
  548. else if (err == NL_SKIP)
  549. goto skip;
  550. else if (err == NL_STOP) {
  551. err = -nl_syserr2nlerr(e->error);
  552. goto out;
  553. }
  554. } else {
  555. err = -nl_syserr2nlerr(e->error);
  556. goto out;
  557. }
  558. } else if (cb->cb_set[NL_CB_ACK])
  559. NL_CB_CALL(cb, NL_CB_ACK, msg);
  560. } else {
  561. /* Valid message (not checking for MULTIPART bit to
  562. * get along with broken kernels. NL_SKIP has no
  563. * effect on this. */
  564. if (cb->cb_set[NL_CB_VALID])
  565. NL_CB_CALL(cb, NL_CB_VALID, msg);
  566. }
  567. skip:
  568. hdr = nlmsg_next(hdr, &n);
  569. }
  570. nlmsg_free(msg);
  571. free(buf);
  572. free(creds);
  573. buf = NULL;
  574. msg = NULL;
  575. creds = NULL;
  576. if (multipart) {
  577. /* Multipart message not yet complete, continue reading */
  578. goto continue_reading;
  579. }
  580. stop:
  581. err = 0;
  582. out:
  583. nlmsg_free(msg);
  584. free(buf);
  585. free(creds);
  586. return err;
  587. }
  588. /**
  589. * Receive a set of messages from a netlink socket.
  590. * @arg sk Netlink socket.
  591. * @arg cb set of callbacks to control behaviour.
  592. *
  593. * Repeatedly calls nl_recv() or the respective replacement if provided
  594. * by the application (see nl_cb_overwrite_recv()) and parses the
  595. * received data as netlink messages. Stops reading if one of the
  596. * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
  597. *
  598. * A non-blocking sockets causes the function to return immediately if
  599. * no data is available.
  600. *
  601. * @return 0 on success or a negative error code from nl_recv().
  602. */
  603. int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
  604. {
  605. if (cb->cb_recvmsgs_ow)
  606. return cb->cb_recvmsgs_ow(sk, cb);
  607. else
  608. return recvmsgs(sk, cb);
  609. }
  610. static int ack_wait_handler(struct nl_msg *msg, void *arg)
  611. {
  612. return NL_STOP;
  613. }
  614. /**
  615. * Wait for ACK.
  616. * @arg sk Netlink socket.
  617. * @pre The netlink socket must be in blocking state.
  618. *
  619. * Waits until an ACK is received for the latest not yet acknowledged
  620. * netlink message.
  621. */
  622. int nl_wait_for_ack(struct nl_sock *sk)
  623. {
  624. int err;
  625. struct nl_cb *cb;
  626. cb = nl_cb_clone(sk->s_cb);
  627. if (cb == NULL)
  628. return -NLE_NOMEM;
  629. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
  630. err = nl_recvmsgs(sk, cb);
  631. nl_cb_put(cb);
  632. return err;
  633. }
  634. /** @} */
  635. /** @} */