libnl  3.2.29
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-private/netlink.h>
29 #include <netlink-private/socket.h>
30 #include <netlink-private/utils.h>
31 #include <netlink/netlink.h>
32 #include <netlink/utils.h>
33 #include <netlink/handlers.h>
34 #include <netlink/msg.h>
35 #include <netlink/attr.h>
36 #include <linux/socket.h>
37 
38 /**
39  * @defgroup core_types Data Types
40  *
41  * Core library data types
42  * @{
43  * @}
44  *
45  * @defgroup send_recv Send & Receive Data
46  *
47  * Connection management, sending & receiving of data
48  *
49  * Related sections in the development guide:
50  * - @core_doc{core_send_recv, Sending & Receiving}
51  * - @core_doc{core_sockets, Sockets}
52  *
53  * @{
54  *
55  * Header
56  * ------
57  * ~~~~{.c}
58  * #include <netlink/netlink.h>
59  * ~~~~
60  */
61 
62 /**
63  * @name Connection Management
64  * @{
65  */
66 
67 /**
68  * Create file descriptor and bind socket.
69  * @arg sk Netlink socket (required)
70  * @arg protocol Netlink protocol to use (required)
71  *
72  * Creates a new Netlink socket using `socket()` and binds the socket to the
73  * protocol and local port specified in the `sk` socket object. Fails if
74  * the socket is already connected.
75  *
76  * @note If available, the `close-on-exec` (`SOCK_CLOEXEC`) feature is enabled
77  * automatically on the new file descriptor. This causes the socket to
78  * be closed automatically if any of the `exec` family functions succeed.
79  * This is essential for multi threaded programs.
80  *
81  * @note The local port (`nl_socket_get_local_port()`) is unspecified after
82  * creating a new socket. It only gets determined when accessing the
83  * port the first time or during `nl_connect()`. When nl_connect()
84  * fails during `bind()` due to `ADDRINUSE`, it will retry with
85  * different ports if the port is unspecified. Unless you want to enforce
86  * the use of a specific local port, don't access the local port (or
87  * reset it to `unspecified` by calling `nl_socket_set_local_port(sk, 0)`).
88  * This capability is indicated by
89  * `%NL_CAPABILITY_NL_CONNECT_RETRY_GENERATE_PORT_ON_ADDRINUSE`.
90  *
91  * @note nl_connect() creates and sets the file descriptor. You can setup the file
92  * descriptor yourself by creating and binding it, and then calling
93  * nl_socket_set_fd(). The result will be the same.
94  *
95  * @see nl_socket_alloc()
96  * @see nl_close()
97  * @see nl_socket_set_fd()
98  *
99  * @return 0 on success or a negative error code.
100  *
101  * @retval -NLE_BAD_SOCK Socket is already connected
102  */
103 int nl_connect(struct nl_sock *sk, int protocol)
104 {
105  int err, flags = 0;
106  int errsv;
107  socklen_t addrlen;
108  struct sockaddr_nl local = { 0 };
109  int try_bind = 1;
110 
111 #ifdef SOCK_CLOEXEC
112  flags |= SOCK_CLOEXEC;
113 #endif
114 
115  if (sk->s_fd != -1)
116  return -NLE_BAD_SOCK;
117 
118  sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
119  if (sk->s_fd < 0) {
120  errsv = errno;
121  NL_DBG(4, "nl_connect(%p): socket() failed with %d (%s)\n", sk, errsv,
122  nl_strerror_l(errsv));
123  err = -nl_syserr2nlerr(errsv);
124  goto errout;
125  }
126 
127  err = nl_socket_set_buffer_size(sk, 0, 0);
128  if (err < 0)
129  goto errout;
130 
131  if (_nl_socket_is_local_port_unspecified (sk)) {
132  uint32_t port;
133  uint32_t used_ports[32] = { 0 };
134  int ntries = 0;
135 
136  while (1) {
137  if (ntries++ > 5) {
138  /* try only a few times. We hit this only if many ports are already in
139  * use but allocated *outside* libnl/generate_local_port(). */
140  _nl_socket_set_local_port_no_release (sk, 0);
141  break;
142  }
143 
144  port = _nl_socket_set_local_port_no_release(sk, 1);
145  if (port == 0)
146  break;
147 
148  err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
149  sizeof(sk->s_local));
150  if (err == 0) {
151  try_bind = 0;
152  break;
153  }
154 
155  errsv = errno;
156  if (errsv == EADDRINUSE) {
157  NL_DBG(4, "nl_connect(%p): local port %u already in use. Retry.\n", sk, (unsigned) port);
158  _nl_socket_used_ports_set(used_ports, port);
159  } else {
160  NL_DBG(4, "nl_connect(%p): bind() for port %u failed with %d (%s)\n",
161  sk, (unsigned) port, errsv, nl_strerror_l(errsv));
162  _nl_socket_used_ports_release_all(used_ports);
163  err = -nl_syserr2nlerr(errsv);
164  goto errout;
165  }
166  }
167  _nl_socket_used_ports_release_all(used_ports);
168  }
169  if (try_bind) {
170  err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
171  sizeof(sk->s_local));
172  if (err != 0) {
173  errsv = errno;
174  NL_DBG(4, "nl_connect(%p): bind() failed with %d (%s)\n",
175  sk, errsv, nl_strerror_l(errsv));
176  err = -nl_syserr2nlerr(errsv);
177  goto errout;
178  }
179  }
180 
181  addrlen = sizeof(local);
182  err = getsockname(sk->s_fd, (struct sockaddr *) &local,
183  &addrlen);
184  if (err < 0) {
185  NL_DBG(4, "nl_connect(%p): getsockname() failed with %d (%s)\n",
186  sk, errno, nl_strerror_l(errno));
187  err = -nl_syserr2nlerr(errno);
188  goto errout;
189  }
190 
191  if (addrlen != sizeof(local)) {
192  err = -NLE_NOADDR;
193  goto errout;
194  }
195 
196  if (local.nl_family != AF_NETLINK) {
197  err = -NLE_AF_NOSUPPORT;
198  goto errout;
199  }
200 
201  if (sk->s_local.nl_pid != local.nl_pid) {
202  /* The port id is different. That can happen if the port id was zero
203  * and kernel assigned a local port. */
204  nl_socket_set_local_port (sk, local.nl_pid);
205  }
206  sk->s_local = local;
207  sk->s_proto = protocol;
208 
209  return 0;
210 errout:
211  if (sk->s_fd != -1) {
212  close(sk->s_fd);
213  sk->s_fd = -1;
214  }
215 
216  return err;
217 }
218 
219 /**
220  * Close Netlink socket
221  * @arg sk Netlink socket (required)
222  *
223  * Closes the Netlink socket using `close()`.
224  *
225  * @note The socket is closed automatically if a `struct nl_sock` object is
226  * freed using `nl_socket_free()`.
227  *
228  * @see nl_connect()
229  */
230 void nl_close(struct nl_sock *sk)
231 {
232  if (sk->s_fd >= 0) {
233  close(sk->s_fd);
234  sk->s_fd = -1;
235  }
236 
237  sk->s_proto = 0;
238 }
239 
240 /** @} */
241 
242 /**
243  * @name Send
244  * @{
245  */
246 
247 /**
248  * Transmit raw data over Netlink socket.
249  * @arg sk Netlink socket (required)
250  * @arg buf Buffer carrying data to send (required)
251  * @arg size Size of buffer (required)
252  *
253  * Transmits "raw" data over the specified Netlink socket. Unlike the other
254  * transmit functions it does not modify the data in any way. It directly
255  * passes the buffer \c buf of \c size to sendto().
256  *
257  * The message is addressed to the peer as specified in the socket by either
258  * the nl_socket_set_peer_port() or nl_socket_set_peer_groups() function.
259  *
260  * @note Because there is no indication on the message boundaries of the data
261  * being sent, the \c NL_CB_MSG_OUT callback handler will not be invoked
262  * for data that is being sent using this function.
263  *
264  * @see nl_socket_set_peer_port()
265  * @see nl_socket_set_peer_groups()
266  * @see nl_sendmsg()
267  *
268  * @return Number of bytes sent or a negative error code.
269  */
270 int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
271 {
272  int ret;
273 
274  if (!buf)
275  return -NLE_INVAL;
276 
277  if (sk->s_fd < 0)
278  return -NLE_BAD_SOCK;
279 
280  ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *)
281  &sk->s_peer, sizeof(sk->s_peer));
282  if (ret < 0) {
283  NL_DBG(4, "nl_sendto(%p): sendto() failed with %d (%s)\n",
284  sk, errno, nl_strerror_l(errno));
285  return -nl_syserr2nlerr(errno);
286  }
287 
288  return ret;
289 }
290 
291 /**
292  * Transmit Netlink message using sendmsg()
293  * @arg sk Netlink socket (required)
294  * @arg msg Netlink message to be sent (required)
295  * @arg hdr sendmsg() message header (required)
296  *
297  * Transmits the message specified in \c hdr over the Netlink socket using the
298  * sendmsg() system call.
299  *
300  * @attention
301  * The `msg` argument will *not* be used to derive the message payload that
302  * is being sent out. The `msg` argument is *only* passed on to the
303  * `NL_CB_MSG_OUT` callback. The caller is responsible to initialize the
304  * `hdr` struct properly and have it point to the message payload and
305  * socket address.
306  *
307  * @note
308  * This function uses `nlmsg_set_src()` to modify the `msg` argument prior to
309  * invoking the `NL_CB_MSG_OUT` callback to provide the local port number.
310  *
311  * @callback This function triggers the `NL_CB_MSG_OUT` callback.
312  *
313  * @attention
314  * Think twice before using this function. It provides a low level access to
315  * the Netlink socket. Among other limitations, it does not add credentials
316  * even if enabled or respect the destination address specified in the `msg`
317  * object.
318  *
319  * @see nl_socket_set_local_port()
320  * @see nl_send_auto()
321  * @see nl_send_iovec()
322  *
323  * @return Number of bytes sent on success or a negative error code.
324  *
325  * @lowlevel
326  */
327 int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
328 {
329  struct nl_cb *cb;
330  int ret;
331 
332  if (sk->s_fd < 0)
333  return -NLE_BAD_SOCK;
334 
335  nlmsg_set_src(msg, &sk->s_local);
336 
337  cb = sk->s_cb;
338  if (cb->cb_set[NL_CB_MSG_OUT])
339  if ((ret = nl_cb_call(cb, NL_CB_MSG_OUT, msg)) != NL_OK)
340  return ret;
341 
342  ret = sendmsg(sk->s_fd, hdr, 0);
343  if (ret < 0) {
344  NL_DBG(4, "nl_sendmsg(%p): sendmsg() failed with %d (%s)\n",
345  sk, errno, nl_strerror_l(errno));
346  return -nl_syserr2nlerr(errno);
347  }
348 
349  NL_DBG(4, "sent %d bytes\n", ret);
350  return ret;
351 }
352 
353 
354 /**
355  * Transmit Netlink message (taking IO vector)
356  * @arg sk Netlink socket (required)
357  * @arg msg Netlink message to be sent (required)
358  * @arg iov IO vector to be sent (required)
359  * @arg iovlen Number of struct iovec to be sent (required)
360  *
361  * This function is identical to nl_send() except that instead of taking a
362  * `struct nl_msg` object it takes an IO vector. Please see the description
363  * of `nl_send()`.
364  *
365  * @callback This function triggers the `NL_CB_MSG_OUT` callback.
366  *
367  * @see nl_send()
368  *
369  * @return Number of bytes sent on success or a negative error code.
370  *
371  * @lowlevel
372  */
373 int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
374 {
375  struct sockaddr_nl *dst;
376  struct ucred *creds;
377  struct msghdr hdr = {
378  .msg_name = (void *) &sk->s_peer,
379  .msg_namelen = sizeof(struct sockaddr_nl),
380  .msg_iov = iov,
381  .msg_iovlen = iovlen,
382  };
383  char buf[CMSG_SPACE(sizeof(struct ucred))];
384 
385  /* Overwrite destination if specified in the message itself, defaults
386  * to the peer address of the socket.
387  */
388  dst = nlmsg_get_dst(msg);
389  if (dst->nl_family == AF_NETLINK)
390  hdr.msg_name = dst;
391 
392  /* Add credentials if present. */
393  creds = nlmsg_get_creds(msg);
394  if (creds != NULL) {
395  struct cmsghdr *cmsg;
396 
397  hdr.msg_control = buf;
398  hdr.msg_controllen = sizeof(buf);
399 
400  cmsg = CMSG_FIRSTHDR(&hdr);
401  cmsg->cmsg_level = SOL_SOCKET;
402  cmsg->cmsg_type = SCM_CREDENTIALS;
403  cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
404  memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
405  }
406 
407  return nl_sendmsg(sk, msg, &hdr);
408 }
409 
410 /**
411  * Transmit Netlink message
412  * @arg sk Netlink socket (required)
413  * @arg msg Netlink message (required)
414  *
415  * Transmits the Netlink message `msg` over the Netlink socket using the
416  * `sendmsg()` system call. This function is based on `nl_send_iovec()` but
417  * takes care of initializing a `struct iovec` based on the `msg` object.
418  *
419  * The message is addressed to the peer as specified in the socket by either
420  * the nl_socket_set_peer_port() or nl_socket_set_peer_groups() function.
421  * The peer address can be overwritten by specifying an address in the `msg`
422  * object using nlmsg_set_dst().
423  *
424  * If present in the `msg`, credentials set by the nlmsg_set_creds() function
425  * are added to the control buffer of the message.
426  *
427  * @par Overwriting Capability:
428  * Calls to this function can be overwritten by providing an alternative using
429  * the nl_cb_overwrite_send() function.
430  *
431  * @callback This function triggers the `NL_CB_MSG_OUT` callback.
432  *
433  * @attention
434  * Unlike `nl_send_auto()`, this function does *not* finalize the message in
435  * terms of automatically adding needed flags or filling out port numbers.
436  *
437  * @see nl_send_auto()
438  * @see nl_send_iovec()
439  * @see nl_socket_set_peer_port()
440  * @see nl_socket_set_peer_groups()
441  * @see nlmsg_set_dst()
442  * @see nlmsg_set_creds()
443  * @see nl_cb_overwrite_send()
444  *
445  * @return Number of bytes sent on success or a negative error code.
446 */
447 int nl_send(struct nl_sock *sk, struct nl_msg *msg)
448 {
449  struct nl_cb *cb = sk->s_cb;
450 
451  if (cb->cb_send_ow)
452  return cb->cb_send_ow(sk, msg);
453  else {
454  struct iovec iov = {
455  .iov_base = (void *) nlmsg_hdr(msg),
456  .iov_len = nlmsg_hdr(msg)->nlmsg_len,
457  };
458 
459  return nl_send_iovec(sk, msg, &iov, 1);
460  }
461 }
462 
463 /**
464  * Finalize Netlink message
465  * @arg sk Netlink socket (required)
466  * @arg msg Netlink message (required)
467  *
468  * This function finalizes a Netlink message by completing the message with
469  * desirable flags and values depending on the socket configuration.
470  *
471  * - If not yet filled out, the source address of the message (`nlmsg_pid`)
472  * will be set to the local port number of the socket.
473  * - If not yet specified, the next available sequence number is assigned
474  * to the message (`nlmsg_seq`).
475  * - If not yet specified, the protocol field of the message will be set to
476  * the protocol field of the socket.
477  * - The `NLM_F_REQUEST` Netlink message flag will be set.
478  * - The `NLM_F_ACK` flag will be set if Auto-ACK mode is enabled on the
479  * socket.
480  */
481 void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
482 {
483  struct nlmsghdr *nlh;
484 
485  nlh = nlmsg_hdr(msg);
486  if (nlh->nlmsg_pid == NL_AUTO_PORT)
487  nlh->nlmsg_pid = nl_socket_get_local_port(sk);
488 
489  if (nlh->nlmsg_seq == NL_AUTO_SEQ)
490  nlh->nlmsg_seq = sk->s_seq_next++;
491 
492  if (msg->nm_protocol == -1)
493  msg->nm_protocol = sk->s_proto;
494 
495  nlh->nlmsg_flags |= NLM_F_REQUEST;
496 
497  if (!(sk->s_flags & NL_NO_AUTO_ACK))
498  nlh->nlmsg_flags |= NLM_F_ACK;
499 }
500 
501 /**
502  * Finalize and transmit Netlink message
503  * @arg sk Netlink socket (required)
504  * @arg msg Netlink message (required)
505  *
506  * Finalizes the message by passing it to `nl_complete_msg()` and transmits it
507  * by passing it to `nl_send()`.
508  *
509  * @callback This function triggers the `NL_CB_MSG_OUT` callback.
510  *
511  * @see nl_complete_msg()
512  * @see nl_send()
513  *
514  * @return Number of bytes sent or a negative error code.
515  */
516 int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
517 {
518  nl_complete_msg(sk, msg);
519 
520  return nl_send(sk, msg);
521 }
522 
523 /**
524  * Finalize and transmit Netlink message and wait for ACK or error message
525  * @arg sk Netlink socket (required)
526  * @arg msg Netlink message (required)
527  *
528  * Passes the `msg` to `nl_send_auto()` to finalize and transmit it. Frees the
529  * message and waits (sleeps) for the ACK or error message to be received.
530  *
531  * @attention
532  * Disabling Auto-ACK (nl_socket_disable_auto_ack()) will cause this function
533  * to return immediately after transmitting the message. However, the peer may
534  * still be returning an error message in response to the request. It is the
535  * responsibility of the caller to handle such messages.
536  *
537  * @callback This function triggers the `NL_CB_MSG_OUT` callback.
538  *
539  * @attention
540  * This function frees the `msg` object after transmitting it by calling
541  * `nlmsg_free()`.
542  *
543  * @see nl_send_auto().
544  * @see nl_wait_for_ack()
545  *
546  * @return 0 on success or a negative error code.
547  */
548 int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
549 {
550  int err;
551 
552  err = nl_send_auto(sk, msg);
553  nlmsg_free(msg);
554  if (err < 0)
555  return err;
556 
557  return wait_for_ack(sk);
558 }
559 
560 /**
561  * Construct and transmit a Netlink message
562  * @arg sk Netlink socket (required)
563  * @arg type Netlink message type (required)
564  * @arg flags Netlink message flags (optional)
565  * @arg buf Data buffer (optional)
566  * @arg size Size of data buffer (optional)
567  *
568  * Allocates a new Netlink message based on `type` and `flags`. If `buf`
569  * points to payload of length `size` that payload will be appended to the
570  * message.
571  *
572  * Sends out the message using `nl_send_auto()` and frees the message
573  * afterwards.
574  *
575  * @see nl_send_auto()
576  *
577  * @return Number of characters sent on success or a negative error code.
578  * @retval -NLE_NOMEM Unable to allocate Netlink message
579  */
580 int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf,
581  size_t size)
582 {
583  int err;
584  struct nl_msg *msg;
585 
586  msg = nlmsg_alloc_simple(type, flags);
587  if (!msg)
588  return -NLE_NOMEM;
589 
590  if (buf && size) {
591  err = nlmsg_append(msg, buf, size, NLMSG_ALIGNTO);
592  if (err < 0)
593  goto errout;
594  }
595 
596  err = nl_send_auto(sk, msg);
597 errout:
598  nlmsg_free(msg);
599 
600  return err;
601 }
602 
603 /** @} */
604 
605 /**
606  * @name Receive
607  * @{
608  */
609 
610 /**
611  * Receive data from netlink socket
612  * @arg sk Netlink socket (required)
613  * @arg nla Netlink socket structure to hold address of peer (required)
614  * @arg buf Destination pointer for message content (required)
615  * @arg creds Destination pointer for credentials (optional)
616  *
617  * Receives data from a connected netlink socket using recvmsg() and returns
618  * the number of bytes read. The read data is stored in a newly allocated
619  * buffer that is assigned to \c *buf. The peer's netlink address will be
620  * stored in \c *nla.
621  *
622  * This function blocks until data is available to be read unless the socket
623  * has been put into non-blocking mode using nl_socket_set_nonblocking() in
624  * which case this function will return immediately with a return value of 0.
625  *
626  * The buffer size used when reading from the netlink socket and thus limiting
627  * the maximum size of a netlink message that can be read defaults to the size
628  * of a memory page (getpagesize()). The buffer size can be modified on a per
629  * socket level using the function nl_socket_set_msg_buf_size().
630  *
631  * If message peeking is enabled using nl_socket_enable_msg_peek() the size of
632  * the message to be read will be determined using the MSG_PEEK flag prior to
633  * performing the actual read. This leads to an additional recvmsg() call for
634  * every read operation which has performance implications and is not
635  * recommended for high throughput protocols.
636  *
637  * An eventual interruption of the recvmsg() system call is automatically
638  * handled by retrying the operation.
639  *
640  * If receiving of credentials has been enabled using the function
641  * nl_socket_set_passcred(), this function will allocate a new struct ucred
642  * filled with the received credentials and assign it to \c *creds. The caller
643  * is responsible for freeing the buffer.
644  *
645  * @note The caller is responsible to free the returned data buffer and if
646  * enabled, the credentials buffer.
647  *
648  * @see nl_socket_set_nonblocking()
649  * @see nl_socket_set_msg_buf_size()
650  * @see nl_socket_enable_msg_peek()
651  * @see nl_socket_set_passcred()
652  *
653  * @return Number of bytes read, 0 on EOF, 0 on no data event (non-blocking
654  * mode), or a negative error code.
655  */
656 int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
657  unsigned char **buf, struct ucred **creds)
658 {
659  ssize_t n;
660  int flags = 0;
661  static int page_size = 0;
662  struct iovec iov;
663  struct msghdr msg = {
664  .msg_name = (void *) nla,
665  .msg_namelen = sizeof(struct sockaddr_nl),
666  .msg_iov = &iov,
667  .msg_iovlen = 1,
668  };
669  struct ucred* tmpcreds = NULL;
670  int retval = 0;
671 
672  if (!buf || !nla)
673  return -NLE_INVAL;
674 
675  if ( (sk->s_flags & NL_MSG_PEEK)
676  || (!(sk->s_flags & NL_MSG_PEEK_EXPLICIT) && sk->s_bufsize == 0))
677  flags |= MSG_PEEK | MSG_TRUNC;
678 
679  if (page_size == 0)
680  page_size = getpagesize() * 4;
681 
682  iov.iov_len = sk->s_bufsize ? : page_size;
683  iov.iov_base = malloc(iov.iov_len);
684 
685  if (!iov.iov_base) {
686  retval = -NLE_NOMEM;
687  goto abort;
688  }
689 
690  if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
691  msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
692  msg.msg_control = malloc(msg.msg_controllen);
693  if (!msg.msg_control) {
694  retval = -NLE_NOMEM;
695  goto abort;
696  }
697  }
698 retry:
699 
700  n = recvmsg(sk->s_fd, &msg, flags);
701  if (!n) {
702  retval = 0;
703  goto abort;
704  }
705  if (n < 0) {
706  if (errno == EINTR) {
707  NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
708  goto retry;
709  }
710 
711  NL_DBG(4, "nl_sendmsg(%p): nl_recv() failed with %d (%s)\n",
712  sk, errno, nl_strerror_l(errno));
713  retval = -nl_syserr2nlerr(errno);
714  goto abort;
715  }
716 
717  if (msg.msg_flags & MSG_CTRUNC) {
718  void *tmp;
719 
720  if (msg.msg_controllen == 0) {
721  retval = -NLE_MSG_TRUNC;
722  NL_DBG(4, "recvmsg(%p): Received unexpected control data", sk);
723  goto abort;
724  }
725 
726  msg.msg_controllen *= 2;
727  tmp = realloc(msg.msg_control, msg.msg_controllen);
728  if (!tmp) {
729  retval = -NLE_NOMEM;
730  goto abort;
731  }
732  msg.msg_control = tmp;
733  goto retry;
734  }
735 
736  if (iov.iov_len < n || (msg.msg_flags & MSG_TRUNC)) {
737  void *tmp;
738 
739  /* respond with error to an incomplete message */
740  if (flags == 0) {
741  retval = -NLE_MSG_TRUNC;
742  goto abort;
743  }
744 
745  /* Provided buffer is not long enough, enlarge it
746  * to size of n (which should be total length of the message)
747  * and try again. */
748  iov.iov_len = n;
749  tmp = realloc(iov.iov_base, iov.iov_len);
750  if (!tmp) {
751  retval = -NLE_NOMEM;
752  goto abort;
753  }
754  iov.iov_base = tmp;
755  flags = 0;
756  goto retry;
757  }
758 
759  if (flags != 0) {
760  /* Buffer is big enough, do the actual reading */
761  flags = 0;
762  goto retry;
763  }
764 
765  if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
766  retval = -NLE_NOADDR;
767  goto abort;
768  }
769 
770  if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
771  struct cmsghdr *cmsg;
772 
773  for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
774  if (cmsg->cmsg_level != SOL_SOCKET)
775  continue;
776  if (cmsg->cmsg_type != SCM_CREDENTIALS)
777  continue;
778  tmpcreds = malloc(sizeof(*tmpcreds));
779  if (!tmpcreds) {
780  retval = -NLE_NOMEM;
781  goto abort;
782  }
783  memcpy(tmpcreds, CMSG_DATA(cmsg), sizeof(*tmpcreds));
784  break;
785  }
786  }
787 
788  retval = n;
789 abort:
790  free(msg.msg_control);
791 
792  if (retval <= 0) {
793  free(iov.iov_base);
794  iov.iov_base = NULL;
795  free(tmpcreds);
796  tmpcreds = NULL;
797  } else
798  *buf = iov.iov_base;
799 
800  if (creds)
801  *creds = tmpcreds;
802 
803  return retval;
804 }
805 
806 /** @cond SKIP */
807 #define NL_CB_CALL(cb, type, msg) \
808 do { \
809  err = nl_cb_call(cb, type, msg); \
810  switch (err) { \
811  case NL_OK: \
812  err = 0; \
813  break; \
814  case NL_SKIP: \
815  goto skip; \
816  case NL_STOP: \
817  goto stop; \
818  default: \
819  goto out; \
820  } \
821 } while (0)
822 /** @endcond */
823 
824 static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
825 {
826  int n, err = 0, multipart = 0, interrupted = 0, nrecv = 0;
827  unsigned char *buf = NULL;
828  struct nlmsghdr *hdr;
829 
830  /*
831  nla is passed on to not only to nl_recv() but may also be passed
832  to a function pointer provided by the caller which may or may not
833  initialize the variable. Thomas Graf.
834  */
835  struct sockaddr_nl nla = {0};
836  struct nl_msg *msg = NULL;
837  struct ucred *creds = NULL;
838 
839 continue_reading:
840  NL_DBG(3, "Attempting to read from %p\n", sk);
841  if (cb->cb_recv_ow)
842  n = cb->cb_recv_ow(sk, &nla, &buf, &creds);
843  else
844  n = nl_recv(sk, &nla, &buf, &creds);
845 
846  if (n <= 0)
847  return n;
848 
849  NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n);
850 
851  hdr = (struct nlmsghdr *) buf;
852  while (nlmsg_ok(hdr, n)) {
853  NL_DBG(3, "recvmsgs(%p): Processing valid message...\n", sk);
854 
855  nlmsg_free(msg);
856  msg = nlmsg_convert(hdr);
857  if (!msg) {
858  err = -NLE_NOMEM;
859  goto out;
860  }
861 
862  nlmsg_set_proto(msg, sk->s_proto);
863  nlmsg_set_src(msg, &nla);
864  if (creds)
865  nlmsg_set_creds(msg, creds);
866 
867  nrecv++;
868 
869  /* Raw callback is the first, it gives the most control
870  * to the user and he can do his very own parsing. */
871  if (cb->cb_set[NL_CB_MSG_IN])
872  NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
873 
874  /* Sequence number checking. The check may be done by
875  * the user, otherwise a very simple check is applied
876  * enforcing strict ordering */
877  if (cb->cb_set[NL_CB_SEQ_CHECK]) {
878  NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
879 
880  /* Only do sequence checking if auto-ack mode is enabled */
881  } else if (!(sk->s_flags & NL_NO_AUTO_ACK)) {
882  if (hdr->nlmsg_seq != sk->s_seq_expect) {
883  if (cb->cb_set[NL_CB_INVALID])
884  NL_CB_CALL(cb, NL_CB_INVALID, msg);
885  else {
886  err = -NLE_SEQ_MISMATCH;
887  goto out;
888  }
889  }
890  }
891 
892  if (hdr->nlmsg_type == NLMSG_DONE ||
893  hdr->nlmsg_type == NLMSG_ERROR ||
894  hdr->nlmsg_type == NLMSG_NOOP ||
895  hdr->nlmsg_type == NLMSG_OVERRUN) {
896  /* We can't check for !NLM_F_MULTI since some netlink
897  * users in the kernel are broken. */
898  sk->s_seq_expect++;
899  NL_DBG(3, "recvmsgs(%p): Increased expected " \
900  "sequence number to %d\n",
901  sk, sk->s_seq_expect);
902  }
903 
904  if (hdr->nlmsg_flags & NLM_F_MULTI)
905  multipart = 1;
906 
907  if (hdr->nlmsg_flags & NLM_F_DUMP_INTR) {
908  if (cb->cb_set[NL_CB_DUMP_INTR])
909  NL_CB_CALL(cb, NL_CB_DUMP_INTR, msg);
910  else {
911  /*
912  * We have to continue reading to clear
913  * all messages until a NLMSG_DONE is
914  * received and report the inconsistency.
915  */
916  interrupted = 1;
917  }
918  }
919 
920  /* Other side wishes to see an ack for this message */
921  if (hdr->nlmsg_flags & NLM_F_ACK) {
922  if (cb->cb_set[NL_CB_SEND_ACK])
923  NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
924  else {
925  /* FIXME: implement */
926  }
927  }
928 
929  /* messages terminates a multipart message, this is
930  * usually the end of a message and therefore we slip
931  * out of the loop by default. the user may overrule
932  * this action by skipping this packet. */
933  if (hdr->nlmsg_type == NLMSG_DONE) {
934  multipart = 0;
935  if (cb->cb_set[NL_CB_FINISH])
936  NL_CB_CALL(cb, NL_CB_FINISH, msg);
937  }
938 
939  /* Message to be ignored, the default action is to
940  * skip this message if no callback is specified. The
941  * user may overrule this action by returning
942  * NL_PROCEED. */
943  else if (hdr->nlmsg_type == NLMSG_NOOP) {
944  if (cb->cb_set[NL_CB_SKIPPED])
945  NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
946  else
947  goto skip;
948  }
949 
950  /* Data got lost, report back to user. The default action is to
951  * quit parsing. The user may overrule this action by retuning
952  * NL_SKIP or NL_PROCEED (dangerous) */
953  else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
954  if (cb->cb_set[NL_CB_OVERRUN])
955  NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
956  else {
957  err = -NLE_MSG_OVERFLOW;
958  goto out;
959  }
960  }
961 
962  /* Message carries a nlmsgerr */
963  else if (hdr->nlmsg_type == NLMSG_ERROR) {
964  struct nlmsgerr *e = nlmsg_data(hdr);
965 
966  if (hdr->nlmsg_len < nlmsg_size(sizeof(*e))) {
967  /* Truncated error message, the default action
968  * is to stop parsing. The user may overrule
969  * this action by returning NL_SKIP or
970  * NL_PROCEED (dangerous) */
971  if (cb->cb_set[NL_CB_INVALID])
972  NL_CB_CALL(cb, NL_CB_INVALID, msg);
973  else {
974  err = -NLE_MSG_TRUNC;
975  goto out;
976  }
977  } else if (e->error) {
978  NL_DBG(4, "recvmsgs(%p): RTNETLINK responded with %d (%s)\n",
979  sk, -e->error, nl_strerror_l(-e->error));
980 
981  /* Error message reported back from kernel. */
982  if (cb->cb_err) {
983  err = cb->cb_err(&nla, e,
984  cb->cb_err_arg);
985  if (err < 0)
986  goto out;
987  else if (err == NL_SKIP)
988  goto skip;
989  else if (err == NL_STOP) {
990  err = -nl_syserr2nlerr(e->error);
991  goto out;
992  }
993  } else {
994  err = -nl_syserr2nlerr(e->error);
995  goto out;
996  }
997  } else if (cb->cb_set[NL_CB_ACK])
998  NL_CB_CALL(cb, NL_CB_ACK, msg);
999  } else {
1000  /* Valid message (not checking for MULTIPART bit to
1001  * get along with broken kernels. NL_SKIP has no
1002  * effect on this. */
1003  if (cb->cb_set[NL_CB_VALID])
1004  NL_CB_CALL(cb, NL_CB_VALID, msg);
1005  }
1006 skip:
1007  err = 0;
1008  hdr = nlmsg_next(hdr, &n);
1009  }
1010 
1011  nlmsg_free(msg);
1012  free(buf);
1013  free(creds);
1014  buf = NULL;
1015  msg = NULL;
1016  creds = NULL;
1017 
1018  if (multipart) {
1019  /* Multipart message not yet complete, continue reading */
1020  goto continue_reading;
1021  }
1022 stop:
1023  err = 0;
1024 out:
1025  nlmsg_free(msg);
1026  free(buf);
1027  free(creds);
1028 
1029  if (interrupted)
1030  err = -NLE_DUMP_INTR;
1031 
1032  if (!err)
1033  err = nrecv;
1034 
1035  return err;
1036 }
1037 
1038 /**
1039  * Receive a set of messages from a netlink socket and report parsed messages
1040  * @arg sk Netlink socket.
1041  * @arg cb set of callbacks to control behaviour.
1042  *
1043  * This function is identical to nl_recvmsgs() to the point that it will
1044  * return the number of parsed messages instead of 0 on success.
1045  *
1046  * @see nl_recvmsgs()
1047  *
1048  * @return Number of received messages or a negative error code from nl_recv().
1049  */
1050 int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
1051 {
1052  if (cb->cb_recvmsgs_ow)
1053  return cb->cb_recvmsgs_ow(sk, cb);
1054  else
1055  return recvmsgs(sk, cb);
1056 }
1057 
1058 /**
1059  * Receive a set of messages from a netlink socket.
1060  * @arg sk Netlink socket.
1061  * @arg cb set of callbacks to control behaviour.
1062  *
1063  * Repeatedly calls nl_recv() or the respective replacement if provided
1064  * by the application (see nl_cb_overwrite_recv()) and parses the
1065  * received data as netlink messages. Stops reading if one of the
1066  * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
1067  *
1068  * A non-blocking sockets causes the function to return immediately if
1069  * no data is available.
1070  *
1071  * @see nl_recvmsgs_report()
1072  *
1073  * @return 0 on success or a negative error code from nl_recv().
1074  */
1075 int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
1076 {
1077  int err;
1078 
1079  if ((err = nl_recvmsgs_report(sk, cb)) > 0)
1080  err = 0;
1081 
1082  return err;
1083 }
1084 
1085 /**
1086  * Receive a set of message from a netlink socket using handlers in nl_sock.
1087  * @arg sk Netlink socket.
1088  *
1089  * Calls nl_recvmsgs() with the handlers configured in the netlink socket.
1090  */
1091 int nl_recvmsgs_default(struct nl_sock *sk)
1092 {
1093  return nl_recvmsgs(sk, sk->s_cb);
1094 
1095 }
1096 
1097 static int ack_wait_handler(struct nl_msg *msg, void *arg)
1098 {
1099  return NL_STOP;
1100 }
1101 
1102 /**
1103  * Wait for ACK.
1104  * @arg sk Netlink socket.
1105  * @pre The netlink socket must be in blocking state.
1106  *
1107  * Waits until an ACK is received for the latest not yet acknowledged
1108  * netlink message.
1109  */
1110 int nl_wait_for_ack(struct nl_sock *sk)
1111 {
1112  int err;
1113  struct nl_cb *cb;
1114 
1115  cb = nl_cb_clone(sk->s_cb);
1116  if (cb == NULL)
1117  return -NLE_NOMEM;
1118 
1119  nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
1120  err = nl_recvmsgs(sk, cb);
1121  nl_cb_put(cb);
1122 
1123  return err;
1124 }
1125 
1126 /** @cond SKIP */
1127 struct pickup_param
1128 {
1129  int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1130  struct nlmsghdr *, struct nl_parser_param *);
1131  struct nl_object *result;
1132  int *syserror;
1133 };
1134 
1135 static int __store_answer(struct nl_object *obj, struct nl_parser_param *p)
1136 {
1137  struct pickup_param *pp = p->pp_arg;
1138  /*
1139  * the parser will put() the object at the end, expecting the cache
1140  * to take the reference.
1141  */
1142  nl_object_get(obj);
1143  pp->result = obj;
1144 
1145  return 0;
1146 }
1147 
1148 static int __pickup_answer(struct nl_msg *msg, void *arg)
1149 {
1150  struct pickup_param *pp = arg;
1151  struct nl_parser_param parse_arg = {
1152  .pp_cb = __store_answer,
1153  .pp_arg = pp,
1154  };
1155 
1156  return pp->parser(NULL, &msg->nm_src, msg->nm_nlh, &parse_arg);
1157 }
1158 
1159 static int __pickup_answer_syserr(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
1160 {
1161  *(((struct pickup_param *) arg)->syserror) = nlerr->error;
1162 
1163  return -nl_syserr2nlerr(nlerr->error);
1164 }
1165 
1166 /** @endcond */
1167 
1168 /**
1169  * Pickup netlink answer, parse is and return object
1170  * @arg sk Netlink socket
1171  * @arg parser Parser function to parse answer
1172  * @arg result Result pointer to return parsed object
1173  *
1174  * @return 0 on success or a negative error code.
1175  */
1176 int nl_pickup(struct nl_sock *sk,
1177  int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1178  struct nlmsghdr *, struct nl_parser_param *),
1179  struct nl_object **result)
1180 {
1181  return nl_pickup_keep_syserr(sk, parser, result, NULL);
1182 }
1183 
1184 /**
1185  * Pickup netlink answer, parse is and return object with preserving system error
1186  * @arg sk Netlink socket
1187  * @arg parser Parser function to parse answer
1188  * @arg result Result pointer to return parsed object
1189  * @arg syserr Result pointer for the system error in case of failure
1190  *
1191  * @return 0 on success or a negative error code.
1192  */
1193 int nl_pickup_keep_syserr(struct nl_sock *sk,
1194  int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1195  struct nlmsghdr *, struct nl_parser_param *),
1196  struct nl_object **result,
1197  int *syserror)
1198 {
1199  struct nl_cb *cb;
1200  int err;
1201  struct pickup_param pp = {
1202  .parser = parser,
1203  };
1204 
1205  cb = nl_cb_clone(sk->s_cb);
1206  if (cb == NULL)
1207  return -NLE_NOMEM;
1208 
1209  nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __pickup_answer, &pp);
1210  if (syserror) {
1211  *syserror = 0;
1212  pp.syserror = syserror;
1213  nl_cb_err(cb, NL_CB_CUSTOM, __pickup_answer_syserr, &pp);
1214  }
1215 
1216  err = nl_recvmsgs(sk, cb);
1217  if (err < 0)
1218  goto errout;
1219 
1220  *result = pp.result;
1221 errout:
1222  nl_cb_put(cb);
1223 
1224  return err;
1225 }
1226 
1227 /** @} */
1228 
1229 /**
1230  * @name Deprecated
1231  * @{
1232  */
1233 
1234 /**
1235  * @deprecated Please use nl_complete_msg()
1236  */
1237 void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
1238 {
1239  nl_complete_msg(sk, msg);
1240 }
1241 
1242 /**
1243  * @deprecated Please use nl_send_auto()
1244  */
1245 int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
1246 {
1247  return nl_send_auto(sk, msg);
1248 }
1249 
1250 
1251 /** @} */
1252 
1253 /** @} */
1254 
1255 /** @} */
Report received that data was lost.
Definition: handlers.h:96
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1245
Called for every message sent out except for nl_sendto()
Definition: handlers.h:104
Message is an acknowledge.
Definition: handlers.h:100
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition: msg.c:559
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition: msg.c:106
Sending of an acknowledge message has been requested.
Definition: handlers.h:110
void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
Finalize Netlink message.
Definition: nl.c:481
#define NL_AUTO_PORT
Will cause the netlink port to be set to the port assigned to the netlink icoket ust before sending t...
Definition: msg.h:33
int nlmsg_size(int payload)
Calculates size of netlink message based on payload length.
Definition: msg.c:55
Customized handler specified by the user.
Definition: handlers.h:80
#define NL_AUTO_SEQ
May be used to refer to a sequence number which should be automatically set just before sending the m...
Definition: msg.h:44
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:204
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message and wait for ACK or error message.
Definition: nl.c:548
Message wants to be skipped.
Definition: handlers.h:98
int nl_pickup(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result)
Pickup netlink answer, parse is and return object.
Definition: nl.c:1176
int nl_pickup_keep_syserr(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result, int *syserror)
Pickup netlink answer, parse is and return object with preserving system error.
Definition: nl.c:1193
int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
check if the netlink message fits into the remaining bytes
Definition: msg.c:180
Stop parsing altogether and discard remaining messages.
Definition: handlers.h:65
void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port)
Set local port of socket.
Definition: socket.c:403
Called for every message received.
Definition: handlers.h:102
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
Definition: handlers.c:230
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
Definition: handlers.c:293
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
Definition: nl.c:1075
int nl_connect(struct nl_sock *sk, int protocol)
Create file descriptor and bind socket.
Definition: nl.c:103
struct nlmsghdr * nlmsg_next(struct nlmsghdr *nlh, int *remaining)
next netlink message in message stream
Definition: msg.c:195
Message is malformed and invalid.
Definition: handlers.h:106
Flag NLM_F_DUMP_INTR is set in message.
Definition: handlers.h:112
Skip this message.
Definition: handlers.h:63
Last message in a series of multi part messages received.
Definition: handlers.h:94
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition: msg.c:537
int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
Transmit Netlink message (taking IO vector)
Definition: nl.c:373
int nl_recvmsgs_default(struct nl_sock *sk)
Receive a set of message from a netlink socket using handlers in nl_sock.
Definition: nl.c:1091
int nl_send(struct nl_sock *sk, struct nl_msg *msg)
Transmit Netlink message.
Definition: nl.c:447
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
Definition: nl.c:580
Message is valid.
Definition: handlers.h:92
Called instead of internal sequence number checking.
Definition: handlers.h:108
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition: msg.c:443
int nl_wait_for_ack(struct nl_sock *sk)
Wait for ACK.
Definition: nl.c:1110
Proceed with wathever would come next.
Definition: handlers.h:61
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:347
void nl_close(struct nl_sock *sk)
Close Netlink socket.
Definition: nl.c:230
void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1237
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
Set socket buffer size of netlink socket.
Definition: socket.c:812
int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket and report parsed messages.
Definition: nl.c:1050
int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
Transmit raw data over Netlink socket.
Definition: nl.c:270
struct nl_msg * nlmsg_convert(struct nlmsghdr *hdr)
Convert a netlink message received from a netlink socket to a nl_msg.
Definition: msg.c:383
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
Definition: nl.c:516
int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, unsigned char **buf, struct ucred **creds)
Receive data from netlink socket.
Definition: nl.c:656
int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
Set up an error callback.
Definition: handlers.c:343
int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
Transmit Netlink message using sendmsg()
Definition: nl.c:327