libnl  3.2.11
nl.c
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-2012 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @defgroup core Core Library (libnl)
14  *
15  * Socket handling, connection management, sending and receiving of data,
16  * message construction and parsing, object caching system, ...
17  *
18  * This is the API reference of the core library. It is not meant as a guide
19  * but as a reference. Please refer to the core library guide for detailed
20  * documentation on the library architecture and examples:
21  *
22  * * @ref_asciidoc{core,_,Netlink Core Library Development Guide}
23  *
24  *
25  * @{
26  */
27 
28 #include <netlink-local.h>
29 #include <netlink/netlink.h>
30 #include <netlink/utils.h>
31 #include <netlink/handlers.h>
32 #include <netlink/msg.h>
33 #include <netlink/attr.h>
34 
35 /**
36  * @defgroup core_types Data Types
37  *
38  * Core library data types
39  * @{
40  * @}
41  *
42  * @defgroup send_recv Send & Receive Data
43  *
44  * Connection management, sending & receiving of data
45  *
46  * Related sections in the development guide:
47  * - @core_doc{core_send_recv, Sending & Receiving}
48  * - @core_doc{core_sockets, Sockets}
49  *
50  * @{
51  *
52  * Header
53  * ------
54  * ~~~~{.c}
55  * #include <netlink/netlink.h>
56  * ~~~~
57  */
58 
59 /**
60  * @name Connection Management
61  * @{
62  */
63 
64 /**
65  * Create and connect netlink socket.
66  * @arg sk Netlink socket.
67  * @arg protocol Netlink protocol to use.
68  *
69  * Creates a netlink socket using the specified protocol, binds the socket
70  * and issues a connection attempt.
71  *
72  * @note SOCK_CLOEXEC is set on the socket if available.
73  *
74  * @return 0 on success or a negative error code.
75  */
76 int nl_connect(struct nl_sock *sk, int protocol)
77 {
78  int err, flags = 0;
79  socklen_t addrlen;
80 
81 #ifdef SOCK_CLOEXEC
82  flags |= SOCK_CLOEXEC;
83 #endif
84 
85  sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
86  if (sk->s_fd < 0) {
87  err = -nl_syserr2nlerr(errno);
88  goto errout;
89  }
90 
91  if (!(sk->s_flags & NL_SOCK_BUFSIZE_SET)) {
92  err = nl_socket_set_buffer_size(sk, 0, 0);
93  if (err < 0)
94  goto errout;
95  }
96 
97  err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
98  sizeof(sk->s_local));
99  if (err < 0) {
100  err = -nl_syserr2nlerr(errno);
101  goto errout;
102  }
103 
104  addrlen = sizeof(sk->s_local);
105  err = getsockname(sk->s_fd, (struct sockaddr *) &sk->s_local,
106  &addrlen);
107  if (err < 0) {
108  err = -nl_syserr2nlerr(errno);
109  goto errout;
110  }
111 
112  if (addrlen != sizeof(sk->s_local)) {
113  err = -NLE_NOADDR;
114  goto errout;
115  }
116 
117  if (sk->s_local.nl_family != AF_NETLINK) {
118  err = -NLE_AF_NOSUPPORT;
119  goto errout;
120  }
121 
122  sk->s_proto = protocol;
123 
124  return 0;
125 errout:
126  close(sk->s_fd);
127  sk->s_fd = -1;
128 
129  return err;
130 }
131 
132 /**
133  * Close/Disconnect netlink socket.
134  * @arg sk Netlink socket.
135  */
136 void nl_close(struct nl_sock *sk)
137 {
138  if (sk->s_fd >= 0) {
139  close(sk->s_fd);
140  sk->s_fd = -1;
141  }
142 
143  sk->s_proto = 0;
144 }
145 
146 /** @} */
147 
148 /**
149  * @name Send
150  * @{
151  */
152 
153 /**
154  * Send raw data over netlink socket.
155  * @arg sk Netlink socket.
156  * @arg buf Data buffer.
157  * @arg size Size of data buffer.
158  * @return Number of characters written on success or a negative error code.
159  */
160 int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
161 {
162  int ret;
163 
164  ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *)
165  &sk->s_peer, sizeof(sk->s_peer));
166  if (ret < 0)
167  return -nl_syserr2nlerr(errno);
168 
169  return ret;
170 }
171 
172 /**
173  * Send netlink message with control over sendmsg() message header.
174  * @arg sk Netlink socket.
175  * @arg msg Netlink message to be sent.
176  * @arg hdr Sendmsg() message header.
177  * @return Number of characters sent on sucess or a negative error code.
178  */
179 int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
180 {
181  struct nl_cb *cb;
182  int ret;
183 
184  nlmsg_set_src(msg, &sk->s_local);
185 
186  cb = sk->s_cb;
187  if (cb->cb_set[NL_CB_MSG_OUT])
188  if ((ret = nl_cb_call(cb, NL_CB_MSG_OUT, msg)) != NL_OK)
189  return ret;
190 
191  ret = sendmsg(sk->s_fd, hdr, 0);
192  if (ret < 0)
193  return -nl_syserr2nlerr(errno);
194 
195  NL_DBG(4, "sent %d bytes\n", ret);
196  return ret;
197 }
198 
199 
200 /**
201  * Send netlink message.
202  * @arg sk Netlink socket.
203  * @arg msg Netlink message to be sent.
204  * @arg iov iovec to be sent.
205  * @arg iovlen number of struct iovec to be sent.
206  * @see nl_sendmsg()
207  * @return Number of characters sent on success or a negative error code.
208  */
209 int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
210 {
211  struct sockaddr_nl *dst;
212  struct ucred *creds;
213  struct msghdr hdr = {
214  .msg_name = (void *) &sk->s_peer,
215  .msg_namelen = sizeof(struct sockaddr_nl),
216  .msg_iov = iov,
217  .msg_iovlen = iovlen,
218  };
219 
220  /* Overwrite destination if specified in the message itself, defaults
221  * to the peer address of the socket.
222  */
223  dst = nlmsg_get_dst(msg);
224  if (dst->nl_family == AF_NETLINK)
225  hdr.msg_name = dst;
226 
227  /* Add credentials if present. */
228  creds = nlmsg_get_creds(msg);
229  if (creds != NULL) {
230  char buf[CMSG_SPACE(sizeof(struct ucred))];
231  struct cmsghdr *cmsg;
232 
233  hdr.msg_control = buf;
234  hdr.msg_controllen = sizeof(buf);
235 
236  cmsg = CMSG_FIRSTHDR(&hdr);
237  cmsg->cmsg_level = SOL_SOCKET;
238  cmsg->cmsg_type = SCM_CREDENTIALS;
239  cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
240  memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
241  }
242 
243  return nl_sendmsg(sk, msg, &hdr);
244 }
245 
246 
247 
248 /**
249 * Send netlink message.
250 * @arg sk Netlink socket.
251 * @arg msg Netlink message to be sent.
252 * @see nl_sendmsg()
253 * @return Number of characters sent on success or a negative error code.
254 */
255 int nl_send(struct nl_sock *sk, struct nl_msg *msg)
256 {
257  struct iovec iov = {
258  .iov_base = (void *) nlmsg_hdr(msg),
259  .iov_len = nlmsg_hdr(msg)->nlmsg_len,
260  };
261 
262  return nl_send_iovec(sk, msg, &iov, 1);
263 }
264 
265 void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
266 {
267  struct nlmsghdr *nlh;
268 
269  nlh = nlmsg_hdr(msg);
270  if (nlh->nlmsg_pid == 0)
271  nlh->nlmsg_pid = sk->s_local.nl_pid;
272 
273  if (nlh->nlmsg_seq == 0)
274  nlh->nlmsg_seq = sk->s_seq_next++;
275 
276  if (msg->nm_protocol == -1)
277  msg->nm_protocol = sk->s_proto;
278 
279  nlh->nlmsg_flags |= NLM_F_REQUEST;
280 
281  if (!(sk->s_flags & NL_NO_AUTO_ACK))
282  nlh->nlmsg_flags |= NLM_F_ACK;
283 }
284 
285 void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
286 {
287  nl_complete_msg(sk, msg);
288 }
289 
290 /**
291  * Automatically complete and send a netlink message
292  * @arg sk Netlink socket.
293  * @arg msg Netlink message to be sent.
294  *
295  * This function takes a netlink message and passes it on to
296  * nl_auto_complete() for completion.
297  *
298  * Checks the netlink message \c nlh for completness and extends it
299  * as required before sending it out. Checked fields include pid,
300  * sequence nr, and flags.
301  *
302  * @see nl_send()
303  * @return Number of characters sent or a negative error code.
304  */
305 int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
306 {
307  struct nl_cb *cb = sk->s_cb;
308 
309  nl_complete_msg(sk, msg);
310 
311  if (cb->cb_send_ow)
312  return cb->cb_send_ow(sk, msg);
313  else
314  return nl_send(sk, msg);
315 }
316 
317 int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
318 {
319  return nl_send_auto(sk, msg);
320 }
321 
322 /**
323  * Send netlink message and wait for response (sync request-response)
324  * @arg sk Netlink socket
325  * @arg msg Netlink message to be sent
326  *
327  * This function takes a netlink message and sends it using nl_send_auto().
328  * It will then wait for the response (ACK or error message) to be
329  * received. Threfore this function will block until the operation has
330  * been completed.
331  *
332  * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
333  * this function to return immediately after sending. In this case,
334  * it is the responsibility of the caller to handle any eventual
335  * error messages returned.
336  *
337  * @see nl_send_auto().
338  *
339  * @return 0 on success or a negative error code.
340  */
341 int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
342 {
343  int err;
344 
345  err = nl_send_auto(sk, msg);
346  nlmsg_free(msg);
347  if (err < 0)
348  return err;
349 
350  return wait_for_ack(sk);
351 }
352 
353 /**
354  * Send simple netlink message using nl_send_auto_complete()
355  * @arg sk Netlink socket.
356  * @arg type Netlink message type.
357  * @arg flags Netlink message flags.
358  * @arg buf Data buffer.
359  * @arg size Size of data buffer.
360  *
361  * Builds a netlink message with the specified type and flags and
362  * appends the specified data as payload to the message.
363  *
364  * @see nl_send_auto_complete()
365  * @return Number of characters sent on success or a negative error code.
366  */
367 int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf,
368  size_t size)
369 {
370  int err;
371  struct nl_msg *msg;
372 
373  msg = nlmsg_alloc_simple(type, flags);
374  if (!msg)
375  return -NLE_NOMEM;
376 
377  if (buf && size) {
378  err = nlmsg_append(msg, buf, size, NLMSG_ALIGNTO);
379  if (err < 0)
380  goto errout;
381  }
382 
383 
384  err = nl_send_auto_complete(sk, msg);
385 errout:
386  nlmsg_free(msg);
387 
388  return err;
389 }
390 
391 /** @} */
392 
393 /**
394  * @name Receive
395  * @{
396  */
397 
398 /**
399  * Receive data from netlink socket
400  * @arg sk Netlink socket.
401  * @arg nla Destination pointer for peer's netlink address.
402  * @arg buf Destination pointer for message content.
403  * @arg creds Destination pointer for credentials.
404  *
405  * Receives a netlink message, allocates a buffer in \c *buf and
406  * stores the message content. The peer's netlink address is stored
407  * in \c *nla. The caller is responsible for freeing the buffer allocated
408  * in \c *buf if a positive value is returned. Interrupted system calls
409  * are handled by repeating the read. The input buffer size is determined
410  * by peeking before the actual read is done.
411  *
412  * A non-blocking sockets causes the function to return immediately with
413  * a return value of 0 if no data is available.
414  *
415  * @return Number of octets read, 0 on EOF or a negative error code.
416  */
417 int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
418  unsigned char **buf, struct ucred **creds)
419 {
420  ssize_t n;
421  int flags = 0;
422  static int page_size = 0;
423  struct iovec iov;
424  struct msghdr msg = {
425  .msg_name = (void *) nla,
426  .msg_namelen = sizeof(struct sockaddr_nl),
427  .msg_iov = &iov,
428  .msg_iovlen = 1,
429  .msg_control = NULL,
430  .msg_controllen = 0,
431  .msg_flags = 0,
432  };
433  struct cmsghdr *cmsg;
434 
435  memset(nla, 0, sizeof(*nla));
436 
437  if (sk->s_flags & NL_MSG_PEEK)
438  flags |= MSG_PEEK | MSG_TRUNC;
439 
440  if (page_size == 0)
441  page_size = getpagesize();
442 
443  iov.iov_len = page_size;
444  iov.iov_base = *buf = malloc(iov.iov_len);
445 
446  if (sk->s_flags & NL_SOCK_PASSCRED) {
447  msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
448  msg.msg_control = calloc(1, msg.msg_controllen);
449  }
450 retry:
451 
452  n = recvmsg(sk->s_fd, &msg, flags);
453  if (!n)
454  goto abort;
455  else if (n < 0) {
456  if (errno == EINTR) {
457  NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
458  goto retry;
459  } else if (errno == EAGAIN) {
460  NL_DBG(3, "recvmsg() returned EAGAIN, aborting\n");
461  goto abort;
462  } else {
463  free(msg.msg_control);
464  free(*buf);
465  return -nl_syserr2nlerr(errno);
466  }
467  }
468 
469  if (msg.msg_flags & MSG_CTRUNC) {
470  msg.msg_controllen *= 2;
471  msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
472  goto retry;
473  } else if (iov.iov_len < n || msg.msg_flags & MSG_TRUNC) {
474  /* Provided buffer is not long enough, enlarge it
475  * to size of n (which should be total length of the message)
476  * and try again. */
477  iov.iov_len = n;
478  iov.iov_base = *buf = realloc(*buf, iov.iov_len);
479  flags = 0;
480  goto retry;
481  } else if (flags != 0) {
482  /* Buffer is big enough, do the actual reading */
483  flags = 0;
484  goto retry;
485  }
486 
487  if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
488  free(msg.msg_control);
489  free(*buf);
490  return -NLE_NOADDR;
491  }
492 
493  for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
494  if (cmsg->cmsg_level == SOL_SOCKET &&
495  cmsg->cmsg_type == SCM_CREDENTIALS) {
496  if (creds) {
497  *creds = calloc(1, sizeof(struct ucred));
498  memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
499  }
500  break;
501  }
502  }
503 
504  free(msg.msg_control);
505  return n;
506 
507 abort:
508  free(msg.msg_control);
509  free(*buf);
510  return 0;
511 }
512 
513 /** @cond SKIP */
514 #define NL_CB_CALL(cb, type, msg) \
515 do { \
516  err = nl_cb_call(cb, type, msg); \
517  switch (err) { \
518  case NL_OK: \
519  err = 0; \
520  break; \
521  case NL_SKIP: \
522  goto skip; \
523  case NL_STOP: \
524  goto stop; \
525  default: \
526  goto out; \
527  } \
528 } while (0)
529 /** @endcond */
530 
531 static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
532 {
533  int n, err = 0, multipart = 0, interrupted = 0, nrecv = 0;
534  unsigned char *buf = NULL;
535  struct nlmsghdr *hdr;
536  struct sockaddr_nl nla = {0};
537  struct nl_msg *msg = NULL;
538  struct ucred *creds = NULL;
539 
540 continue_reading:
541  NL_DBG(3, "Attempting to read from %p\n", sk);
542  if (cb->cb_recv_ow)
543  n = cb->cb_recv_ow(sk, &nla, &buf, &creds);
544  else
545  n = nl_recv(sk, &nla, &buf, &creds);
546 
547  if (n <= 0)
548  return n;
549 
550  NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n);
551 
552  hdr = (struct nlmsghdr *) buf;
553  while (nlmsg_ok(hdr, n)) {
554  NL_DBG(3, "recvmsgs(%p): Processing valid message...\n", sk);
555 
556  nlmsg_free(msg);
557  msg = nlmsg_convert(hdr);
558  if (!msg) {
559  err = -NLE_NOMEM;
560  goto out;
561  }
562 
563  nlmsg_set_proto(msg, sk->s_proto);
564  nlmsg_set_src(msg, &nla);
565  if (creds)
566  nlmsg_set_creds(msg, creds);
567 
568  nrecv++;
569 
570  /* Raw callback is the first, it gives the most control
571  * to the user and he can do his very own parsing. */
572  if (cb->cb_set[NL_CB_MSG_IN])
573  NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
574 
575  /* Sequence number checking. The check may be done by
576  * the user, otherwise a very simple check is applied
577  * enforcing strict ordering */
578  if (cb->cb_set[NL_CB_SEQ_CHECK]) {
579  NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
580 
581  /* Only do sequence checking if auto-ack mode is enabled */
582  } else if (!(sk->s_flags & NL_NO_AUTO_ACK)) {
583  if (hdr->nlmsg_seq != sk->s_seq_expect) {
584  if (cb->cb_set[NL_CB_INVALID])
585  NL_CB_CALL(cb, NL_CB_INVALID, msg);
586  else {
587  err = -NLE_SEQ_MISMATCH;
588  goto out;
589  }
590  }
591  }
592 
593  if (hdr->nlmsg_type == NLMSG_DONE ||
594  hdr->nlmsg_type == NLMSG_ERROR ||
595  hdr->nlmsg_type == NLMSG_NOOP ||
596  hdr->nlmsg_type == NLMSG_OVERRUN) {
597  /* We can't check for !NLM_F_MULTI since some netlink
598  * users in the kernel are broken. */
599  sk->s_seq_expect++;
600  NL_DBG(3, "recvmsgs(%p): Increased expected " \
601  "sequence number to %d\n",
602  sk, sk->s_seq_expect);
603  }
604 
605  if (hdr->nlmsg_flags & NLM_F_MULTI)
606  multipart = 1;
607 
608  if (hdr->nlmsg_flags & NLM_F_DUMP_INTR) {
609  if (cb->cb_set[NL_CB_DUMP_INTR])
610  NL_CB_CALL(cb, NL_CB_DUMP_INTR, msg);
611  else {
612  /*
613  * We have to continue reading to clear
614  * all messages until a NLMSG_DONE is
615  * received and report the inconsistency.
616  */
617  interrupted = 1;
618  }
619  }
620 
621  /* Other side wishes to see an ack for this message */
622  if (hdr->nlmsg_flags & NLM_F_ACK) {
623  if (cb->cb_set[NL_CB_SEND_ACK])
624  NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
625  else {
626  /* FIXME: implement */
627  }
628  }
629 
630  /* messages terminates a multpart message, this is
631  * usually the end of a message and therefore we slip
632  * out of the loop by default. the user may overrule
633  * this action by skipping this packet. */
634  if (hdr->nlmsg_type == NLMSG_DONE) {
635  multipart = 0;
636  if (cb->cb_set[NL_CB_FINISH])
637  NL_CB_CALL(cb, NL_CB_FINISH, msg);
638  }
639 
640  /* Message to be ignored, the default action is to
641  * skip this message if no callback is specified. The
642  * user may overrule this action by returning
643  * NL_PROCEED. */
644  else if (hdr->nlmsg_type == NLMSG_NOOP) {
645  if (cb->cb_set[NL_CB_SKIPPED])
646  NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
647  else
648  goto skip;
649  }
650 
651  /* Data got lost, report back to user. The default action is to
652  * quit parsing. The user may overrule this action by retuning
653  * NL_SKIP or NL_PROCEED (dangerous) */
654  else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
655  if (cb->cb_set[NL_CB_OVERRUN])
656  NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
657  else {
658  err = -NLE_MSG_OVERFLOW;
659  goto out;
660  }
661  }
662 
663  /* Message carries a nlmsgerr */
664  else if (hdr->nlmsg_type == NLMSG_ERROR) {
665  struct nlmsgerr *e = nlmsg_data(hdr);
666 
667  if (hdr->nlmsg_len < nlmsg_size(sizeof(*e))) {
668  /* Truncated error message, the default action
669  * is to stop parsing. The user may overrule
670  * this action by returning NL_SKIP or
671  * NL_PROCEED (dangerous) */
672  if (cb->cb_set[NL_CB_INVALID])
673  NL_CB_CALL(cb, NL_CB_INVALID, msg);
674  else {
675  err = -NLE_MSG_TRUNC;
676  goto out;
677  }
678  } else if (e->error) {
679  /* Error message reported back from kernel. */
680  if (cb->cb_err) {
681  err = cb->cb_err(&nla, e,
682  cb->cb_err_arg);
683  if (err < 0)
684  goto out;
685  else if (err == NL_SKIP)
686  goto skip;
687  else if (err == NL_STOP) {
688  err = -nl_syserr2nlerr(e->error);
689  goto out;
690  }
691  } else {
692  err = -nl_syserr2nlerr(e->error);
693  goto out;
694  }
695  } else if (cb->cb_set[NL_CB_ACK])
696  NL_CB_CALL(cb, NL_CB_ACK, msg);
697  } else {
698  /* Valid message (not checking for MULTIPART bit to
699  * get along with broken kernels. NL_SKIP has no
700  * effect on this. */
701  if (cb->cb_set[NL_CB_VALID])
702  NL_CB_CALL(cb, NL_CB_VALID, msg);
703  }
704 skip:
705  err = 0;
706  hdr = nlmsg_next(hdr, &n);
707  }
708 
709  nlmsg_free(msg);
710  free(buf);
711  free(creds);
712  buf = NULL;
713  msg = NULL;
714  creds = NULL;
715 
716  if (multipart) {
717  /* Multipart message not yet complete, continue reading */
718  goto continue_reading;
719  }
720 stop:
721  err = 0;
722 out:
723  nlmsg_free(msg);
724  free(buf);
725  free(creds);
726 
727  if (interrupted)
728  err = -NLE_DUMP_INTR;
729 
730  if (!err)
731  err = nrecv;
732 
733  return err;
734 }
735 
736 /**
737  * Receive a set of messages from a netlink socket and report parsed messages
738  * @arg sk Netlink socket.
739  * @arg cb set of callbacks to control behaviour.
740  *
741  * This function is identical to nl_recvmsgs() to the point that it will
742  * return the number of parsed messages instead of 0 on success.
743  *
744  * @see nl_recvmsgs()
745  *
746  * @return Number of received messages or a negative error code from nl_recv().
747  */
748 int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
749 {
750  if (cb->cb_recvmsgs_ow)
751  return cb->cb_recvmsgs_ow(sk, cb);
752  else
753  return recvmsgs(sk, cb);
754 }
755 
756 /**
757  * Receive a set of messages from a netlink socket.
758  * @arg sk Netlink socket.
759  * @arg cb set of callbacks to control behaviour.
760  *
761  * Repeatedly calls nl_recv() or the respective replacement if provided
762  * by the application (see nl_cb_overwrite_recv()) and parses the
763  * received data as netlink messages. Stops reading if one of the
764  * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
765  *
766  * A non-blocking sockets causes the function to return immediately if
767  * no data is available.
768  *
769  * @see nl_recvmsgs_report()
770  *
771  * @return 0 on success or a negative error code from nl_recv().
772  */
773 int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
774 {
775  int err;
776 
777  if ((err = nl_recvmsgs_report(sk, cb)) > 0)
778  err = 0;
779 
780  return err;
781 }
782 
783 /**
784  * Receive a set of message from a netlink socket using handlers in nl_sock.
785  * @arg sk Netlink socket.
786  *
787  * Calls nl_recvmsgs() with the handlers configured in the netlink socket.
788  */
789 int nl_recvmsgs_default(struct nl_sock *sk)
790 {
791  return nl_recvmsgs(sk, sk->s_cb);
792 
793 }
794 
795 static int ack_wait_handler(struct nl_msg *msg, void *arg)
796 {
797  return NL_STOP;
798 }
799 
800 /**
801  * Wait for ACK.
802  * @arg sk Netlink socket.
803  * @pre The netlink socket must be in blocking state.
804  *
805  * Waits until an ACK is received for the latest not yet acknowledged
806  * netlink message.
807  */
808 int nl_wait_for_ack(struct nl_sock *sk)
809 {
810  int err;
811  struct nl_cb *cb;
812 
813  cb = nl_cb_clone(sk->s_cb);
814  if (cb == NULL)
815  return -NLE_NOMEM;
816 
817  nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
818  err = nl_recvmsgs(sk, cb);
819  nl_cb_put(cb);
820 
821  return err;
822 }
823 
824 /** @cond SKIP */
825 struct pickup_param
826 {
827  int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
828  struct nlmsghdr *, struct nl_parser_param *);
829  struct nl_object *result;
830 };
831 
832 static int __store_answer(struct nl_object *obj, struct nl_parser_param *p)
833 {
834  struct pickup_param *pp = p->pp_arg;
835  /*
836  * the parser will put() the object at the end, expecting the cache
837  * to take the reference.
838  */
839  nl_object_get(obj);
840  pp->result = obj;
841 
842  return 0;
843 }
844 
845 static int __pickup_answer(struct nl_msg *msg, void *arg)
846 {
847  struct pickup_param *pp = arg;
848  struct nl_parser_param parse_arg = {
849  .pp_cb = __store_answer,
850  .pp_arg = pp,
851  };
852 
853  return pp->parser(NULL, &msg->nm_src, msg->nm_nlh, &parse_arg);
854 }
855 
856 /** @endcond */
857 
858 /**
859  * Pickup netlink answer, parse is and return object
860  * @arg sk Netlink socket
861  * @arg parser Parser function to parse answer
862  * @arg result Result pointer to return parsed object
863  *
864  * @return 0 on success or a negative error code.
865  */
866 int nl_pickup(struct nl_sock *sk,
867  int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
868  struct nlmsghdr *, struct nl_parser_param *),
869  struct nl_object **result)
870 {
871  struct nl_cb *cb;
872  int err;
873  struct pickup_param pp = {
874  .parser = parser,
875  };
876 
877  cb = nl_cb_clone(sk->s_cb);
878  if (cb == NULL)
879  return -NLE_NOMEM;
880 
881  nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __pickup_answer, &pp);
882 
883  err = nl_recvmsgs(sk, cb);
884  if (err < 0)
885  goto errout;
886 
887  *result = pp.result;
888 errout:
889  nl_cb_put(cb);
890 
891  return err;
892 }
893 
894 /** @} */
895 
896 /** @} */
897 
898 /** @} */