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