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