libnl  3.4.0
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
625  * -NLA_AGAIN (versions before 3.2.22 returned instead 0, in which case you
626  * should check first clear errno and then check for errno EAGAIN).
627  *
628  * The buffer size used when reading from the netlink socket and thus limiting
629  * the maximum size of a netlink message that can be read defaults to the size
630  * of a memory page (getpagesize()). The buffer size can be modified on a per
631  * socket level using the function nl_socket_set_msg_buf_size().
632  *
633  * If message peeking is enabled using nl_socket_enable_msg_peek() the size of
634  * the message to be read will be determined using the MSG_PEEK flag prior to
635  * performing the actual read. This leads to an additional recvmsg() call for
636  * every read operation which has performance implications and is not
637  * recommended for high throughput protocols.
638  *
639  * An eventual interruption of the recvmsg() system call is automatically
640  * handled by retrying the operation.
641  *
642  * If receiving of credentials has been enabled using the function
643  * nl_socket_set_passcred(), this function will allocate a new struct ucred
644  * filled with the received credentials and assign it to \c *creds. The caller
645  * is responsible for freeing the buffer.
646  *
647  * @note The caller is responsible to free the returned data buffer and if
648  * enabled, the credentials buffer.
649  *
650  * @see nl_socket_set_nonblocking()
651  * @see nl_socket_set_msg_buf_size()
652  * @see nl_socket_enable_msg_peek()
653  * @see nl_socket_set_passcred()
654  *
655  * @return Number of bytes read, 0 on EOF, 0 on no data event (non-blocking
656  * mode), or a negative error code.
657  */
658 int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
659  unsigned char **buf, struct ucred **creds)
660 {
661  ssize_t n;
662  int flags = 0;
663  static int page_size = 0;
664  struct iovec iov;
665  struct msghdr msg = {
666  .msg_name = (void *) nla,
667  .msg_namelen = sizeof(struct sockaddr_nl),
668  .msg_iov = &iov,
669  .msg_iovlen = 1,
670  };
671  struct ucred* tmpcreds = NULL;
672  int retval = 0;
673 
674  if (!buf || !nla)
675  return -NLE_INVAL;
676 
677  if ( (sk->s_flags & NL_MSG_PEEK)
678  || (!(sk->s_flags & NL_MSG_PEEK_EXPLICIT) && sk->s_bufsize == 0))
679  flags |= MSG_PEEK | MSG_TRUNC;
680 
681  if (page_size == 0)
682  page_size = getpagesize() * 4;
683 
684  iov.iov_len = sk->s_bufsize ? : page_size;
685  iov.iov_base = malloc(iov.iov_len);
686 
687  if (!iov.iov_base) {
688  retval = -NLE_NOMEM;
689  goto abort;
690  }
691 
692  if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
693  msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
694  msg.msg_control = malloc(msg.msg_controllen);
695  if (!msg.msg_control) {
696  retval = -NLE_NOMEM;
697  goto abort;
698  }
699  }
700 retry:
701 
702  n = recvmsg(sk->s_fd, &msg, flags);
703  if (!n) {
704  retval = 0;
705  goto abort;
706  }
707  if (n < 0) {
708  if (errno == EINTR) {
709  NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
710  goto retry;
711  }
712 
713  NL_DBG(4, "nl_sendmsg(%p): nl_recv() failed with %d (%s)\n",
714  sk, errno, nl_strerror_l(errno));
715  retval = -nl_syserr2nlerr(errno);
716  goto abort;
717  }
718 
719  if (msg.msg_flags & MSG_CTRUNC) {
720  void *tmp;
721 
722  if (msg.msg_controllen == 0) {
723  retval = -NLE_MSG_TRUNC;
724  NL_DBG(4, "recvmsg(%p): Received unexpected control data", sk);
725  goto abort;
726  }
727 
728  msg.msg_controllen *= 2;
729  tmp = realloc(msg.msg_control, msg.msg_controllen);
730  if (!tmp) {
731  retval = -NLE_NOMEM;
732  goto abort;
733  }
734  msg.msg_control = tmp;
735  goto retry;
736  }
737 
738  if (iov.iov_len < n || (msg.msg_flags & MSG_TRUNC)) {
739  void *tmp;
740 
741  /* respond with error to an incomplete message */
742  if (flags == 0) {
743  retval = -NLE_MSG_TRUNC;
744  goto abort;
745  }
746 
747  /* Provided buffer is not long enough, enlarge it
748  * to size of n (which should be total length of the message)
749  * and try again. */
750  iov.iov_len = n;
751  tmp = realloc(iov.iov_base, iov.iov_len);
752  if (!tmp) {
753  retval = -NLE_NOMEM;
754  goto abort;
755  }
756  iov.iov_base = tmp;
757  flags = 0;
758  goto retry;
759  }
760 
761  if (flags != 0) {
762  /* Buffer is big enough, do the actual reading */
763  flags = 0;
764  goto retry;
765  }
766 
767  if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
768  retval = -NLE_NOADDR;
769  goto abort;
770  }
771 
772  if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
773  struct cmsghdr *cmsg;
774 
775  for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
776  if (cmsg->cmsg_level != SOL_SOCKET)
777  continue;
778  if (cmsg->cmsg_type != SCM_CREDENTIALS)
779  continue;
780  tmpcreds = malloc(sizeof(*tmpcreds));
781  if (!tmpcreds) {
782  retval = -NLE_NOMEM;
783  goto abort;
784  }
785  memcpy(tmpcreds, CMSG_DATA(cmsg), sizeof(*tmpcreds));
786  break;
787  }
788  }
789 
790  retval = n;
791 abort:
792  free(msg.msg_control);
793 
794  if (retval <= 0) {
795  free(iov.iov_base);
796  iov.iov_base = NULL;
797  free(tmpcreds);
798  tmpcreds = NULL;
799  } else
800  *buf = iov.iov_base;
801 
802  if (creds)
803  *creds = tmpcreds;
804 
805  return retval;
806 }
807 
808 /** @cond SKIP */
809 #define NL_CB_CALL(cb, type, msg) \
810 do { \
811  err = nl_cb_call(cb, type, msg); \
812  switch (err) { \
813  case NL_OK: \
814  err = 0; \
815  break; \
816  case NL_SKIP: \
817  goto skip; \
818  case NL_STOP: \
819  goto stop; \
820  default: \
821  goto out; \
822  } \
823 } while (0)
824 /** @endcond */
825 
826 static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
827 {
828  int n, err = 0, multipart = 0, interrupted = 0, nrecv = 0;
829  unsigned char *buf = NULL;
830  struct nlmsghdr *hdr;
831 
832  /*
833  nla is passed on to not only to nl_recv() but may also be passed
834  to a function pointer provided by the caller which may or may not
835  initialize the variable. Thomas Graf.
836  */
837  struct sockaddr_nl nla = {0};
838  struct nl_msg *msg = NULL;
839  struct ucred *creds = NULL;
840 
841 continue_reading:
842  NL_DBG(3, "Attempting to read from %p\n", sk);
843  if (cb->cb_recv_ow)
844  n = cb->cb_recv_ow(sk, &nla, &buf, &creds);
845  else
846  n = nl_recv(sk, &nla, &buf, &creds);
847 
848  if (n <= 0)
849  return n;
850 
851  NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n);
852 
853  hdr = (struct nlmsghdr *) buf;
854  while (nlmsg_ok(hdr, n)) {
855  NL_DBG(3, "recvmsgs(%p): Processing valid message...\n", sk);
856 
857  nlmsg_free(msg);
858  msg = nlmsg_convert(hdr);
859  if (!msg) {
860  err = -NLE_NOMEM;
861  goto out;
862  }
863 
864  nlmsg_set_proto(msg, sk->s_proto);
865  nlmsg_set_src(msg, &nla);
866  if (creds)
867  nlmsg_set_creds(msg, creds);
868 
869  nrecv++;
870 
871  /* Raw callback is the first, it gives the most control
872  * to the user and he can do his very own parsing. */
873  if (cb->cb_set[NL_CB_MSG_IN])
874  NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
875 
876  /* Sequence number checking. The check may be done by
877  * the user, otherwise a very simple check is applied
878  * enforcing strict ordering */
879  if (cb->cb_set[NL_CB_SEQ_CHECK]) {
880  NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
881 
882  /* Only do sequence checking if auto-ack mode is enabled */
883  } else if (!(sk->s_flags & NL_NO_AUTO_ACK)) {
884  if (hdr->nlmsg_seq != sk->s_seq_expect) {
885  if (cb->cb_set[NL_CB_INVALID])
886  NL_CB_CALL(cb, NL_CB_INVALID, msg);
887  else {
888  err = -NLE_SEQ_MISMATCH;
889  goto out;
890  }
891  }
892  }
893 
894  if (hdr->nlmsg_type == NLMSG_DONE ||
895  hdr->nlmsg_type == NLMSG_ERROR ||
896  hdr->nlmsg_type == NLMSG_NOOP ||
897  hdr->nlmsg_type == NLMSG_OVERRUN) {
898  /* We can't check for !NLM_F_MULTI since some netlink
899  * users in the kernel are broken. */
900  sk->s_seq_expect++;
901  NL_DBG(3, "recvmsgs(%p): Increased expected " \
902  "sequence number to %d\n",
903  sk, sk->s_seq_expect);
904  }
905 
906  if (hdr->nlmsg_flags & NLM_F_MULTI)
907  multipart = 1;
908 
909  if (hdr->nlmsg_flags & NLM_F_DUMP_INTR) {
910  if (cb->cb_set[NL_CB_DUMP_INTR])
911  NL_CB_CALL(cb, NL_CB_DUMP_INTR, msg);
912  else {
913  /*
914  * We have to continue reading to clear
915  * all messages until a NLMSG_DONE is
916  * received and report the inconsistency.
917  */
918  interrupted = 1;
919  }
920  }
921 
922  /* Other side wishes to see an ack for this message */
923  if (hdr->nlmsg_flags & NLM_F_ACK) {
924  if (cb->cb_set[NL_CB_SEND_ACK])
925  NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
926  else {
927  /* FIXME: implement */
928  }
929  }
930 
931  /* messages terminates a multipart message, this is
932  * usually the end of a message and therefore we slip
933  * out of the loop by default. the user may overrule
934  * this action by skipping this packet. */
935  if (hdr->nlmsg_type == NLMSG_DONE) {
936  multipart = 0;
937  if (cb->cb_set[NL_CB_FINISH])
938  NL_CB_CALL(cb, NL_CB_FINISH, msg);
939  }
940 
941  /* Message to be ignored, the default action is to
942  * skip this message if no callback is specified. The
943  * user may overrule this action by returning
944  * NL_PROCEED. */
945  else if (hdr->nlmsg_type == NLMSG_NOOP) {
946  if (cb->cb_set[NL_CB_SKIPPED])
947  NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
948  else
949  goto skip;
950  }
951 
952  /* Data got lost, report back to user. The default action is to
953  * quit parsing. The user may overrule this action by retuning
954  * NL_SKIP or NL_PROCEED (dangerous) */
955  else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
956  if (cb->cb_set[NL_CB_OVERRUN])
957  NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
958  else {
959  err = -NLE_MSG_OVERFLOW;
960  goto out;
961  }
962  }
963 
964  /* Message carries a nlmsgerr */
965  else if (hdr->nlmsg_type == NLMSG_ERROR) {
966  struct nlmsgerr *e = nlmsg_data(hdr);
967 
968  if (hdr->nlmsg_len < nlmsg_size(sizeof(*e))) {
969  /* Truncated error message, the default action
970  * is to stop parsing. The user may overrule
971  * this action by returning NL_SKIP or
972  * NL_PROCEED (dangerous) */
973  if (cb->cb_set[NL_CB_INVALID])
974  NL_CB_CALL(cb, NL_CB_INVALID, msg);
975  else {
976  err = -NLE_MSG_TRUNC;
977  goto out;
978  }
979  } else if (e->error) {
980  NL_DBG(4, "recvmsgs(%p): RTNETLINK responded with %d (%s)\n",
981  sk, -e->error, nl_strerror_l(-e->error));
982 
983  /* Error message reported back from kernel. */
984  if (cb->cb_err) {
985  err = cb->cb_err(&nla, e,
986  cb->cb_err_arg);
987  if (err < 0)
988  goto out;
989  else if (err == NL_SKIP)
990  goto skip;
991  else if (err == NL_STOP) {
992  err = -nl_syserr2nlerr(e->error);
993  goto out;
994  }
995  } else {
996  err = -nl_syserr2nlerr(e->error);
997  goto out;
998  }
999  } else if (cb->cb_set[NL_CB_ACK])
1000  NL_CB_CALL(cb, NL_CB_ACK, msg);
1001  } else {
1002  /* Valid message (not checking for MULTIPART bit to
1003  * get along with broken kernels. NL_SKIP has no
1004  * effect on this. */
1005  if (cb->cb_set[NL_CB_VALID])
1006  NL_CB_CALL(cb, NL_CB_VALID, msg);
1007  }
1008 skip:
1009  err = 0;
1010  hdr = nlmsg_next(hdr, &n);
1011  }
1012 
1013  nlmsg_free(msg);
1014  free(buf);
1015  free(creds);
1016  buf = NULL;
1017  msg = NULL;
1018  creds = NULL;
1019 
1020  if (multipart) {
1021  /* Multipart message not yet complete, continue reading */
1022  goto continue_reading;
1023  }
1024 stop:
1025  err = 0;
1026 out:
1027  nlmsg_free(msg);
1028  free(buf);
1029  free(creds);
1030 
1031  if (interrupted)
1032  err = -NLE_DUMP_INTR;
1033 
1034  if (!err)
1035  err = nrecv;
1036 
1037  return err;
1038 }
1039 
1040 /**
1041  * Receive a set of messages from a netlink socket and report parsed messages
1042  * @arg sk Netlink socket.
1043  * @arg cb set of callbacks to control behaviour.
1044  *
1045  * This function is identical to nl_recvmsgs() to the point that it will
1046  * return the number of parsed messages instead of 0 on success.
1047  *
1048  * @see nl_recvmsgs()
1049  *
1050  * @return Number of received messages or a negative error code from nl_recv().
1051  */
1052 int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
1053 {
1054  if (cb->cb_recvmsgs_ow)
1055  return cb->cb_recvmsgs_ow(sk, cb);
1056  else
1057  return recvmsgs(sk, cb);
1058 }
1059 
1060 /**
1061  * Receive a set of messages from a netlink socket.
1062  * @arg sk Netlink socket.
1063  * @arg cb set of callbacks to control behaviour.
1064  *
1065  * Repeatedly calls nl_recv() or the respective replacement if provided
1066  * by the application (see nl_cb_overwrite_recv()) and parses the
1067  * received data as netlink messages. Stops reading if one of the
1068  * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
1069  *
1070  * A non-blocking sockets causes the function to return immediately if
1071  * no data is available.
1072  *
1073  * @see nl_recvmsgs_report()
1074  *
1075  * @return 0 on success or a negative error code from nl_recv().
1076  */
1077 int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
1078 {
1079  int err;
1080 
1081  if ((err = nl_recvmsgs_report(sk, cb)) > 0)
1082  err = 0;
1083 
1084  return err;
1085 }
1086 
1087 /**
1088  * Receive a set of message from a netlink socket using handlers in nl_sock.
1089  * @arg sk Netlink socket.
1090  *
1091  * Calls nl_recvmsgs() with the handlers configured in the netlink socket.
1092  */
1093 int nl_recvmsgs_default(struct nl_sock *sk)
1094 {
1095  return nl_recvmsgs(sk, sk->s_cb);
1096 
1097 }
1098 
1099 static int ack_wait_handler(struct nl_msg *msg, void *arg)
1100 {
1101  return NL_STOP;
1102 }
1103 
1104 /**
1105  * Wait for ACK.
1106  * @arg sk Netlink socket.
1107  * @pre The netlink socket must be in blocking state.
1108  *
1109  * Waits until an ACK is received for the latest not yet acknowledged
1110  * netlink message.
1111  */
1112 int nl_wait_for_ack(struct nl_sock *sk)
1113 {
1114  int err;
1115  struct nl_cb *cb;
1116 
1117  cb = nl_cb_clone(sk->s_cb);
1118  if (cb == NULL)
1119  return -NLE_NOMEM;
1120 
1121  nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
1122  err = nl_recvmsgs(sk, cb);
1123  nl_cb_put(cb);
1124 
1125  return err;
1126 }
1127 
1128 /** @cond SKIP */
1129 struct pickup_param
1130 {
1131  int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1132  struct nlmsghdr *, struct nl_parser_param *);
1133  struct nl_object *result;
1134  int *syserror;
1135 };
1136 
1137 static int __store_answer(struct nl_object *obj, struct nl_parser_param *p)
1138 {
1139  struct pickup_param *pp = p->pp_arg;
1140  /*
1141  * the parser will put() the object at the end, expecting the cache
1142  * to take the reference.
1143  */
1144  nl_object_get(obj);
1145  pp->result = obj;
1146 
1147  return 0;
1148 }
1149 
1150 static int __pickup_answer(struct nl_msg *msg, void *arg)
1151 {
1152  struct pickup_param *pp = arg;
1153  struct nl_parser_param parse_arg = {
1154  .pp_cb = __store_answer,
1155  .pp_arg = pp,
1156  };
1157 
1158  return pp->parser(NULL, &msg->nm_src, msg->nm_nlh, &parse_arg);
1159 }
1160 
1161 static int __pickup_answer_syserr(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
1162 {
1163  *(((struct pickup_param *) arg)->syserror) = nlerr->error;
1164 
1165  return -nl_syserr2nlerr(nlerr->error);
1166 }
1167 
1168 /** @endcond */
1169 
1170 /**
1171  * Pickup netlink answer, parse is and return object
1172  * @arg sk Netlink socket
1173  * @arg parser Parser function to parse answer
1174  * @arg result Result pointer to return parsed object
1175  *
1176  * @return 0 on success or a negative error code.
1177  */
1178 int nl_pickup(struct nl_sock *sk,
1179  int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1180  struct nlmsghdr *, struct nl_parser_param *),
1181  struct nl_object **result)
1182 {
1183  return nl_pickup_keep_syserr(sk, parser, result, NULL);
1184 }
1185 
1186 /**
1187  * Pickup netlink answer, parse is and return object with preserving system error
1188  * @arg sk Netlink socket
1189  * @arg parser Parser function to parse answer
1190  * @arg result Result pointer to return parsed object
1191  * @arg syserr Result pointer for the system error in case of failure
1192  *
1193  * @return 0 on success or a negative error code.
1194  */
1195 int nl_pickup_keep_syserr(struct nl_sock *sk,
1196  int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1197  struct nlmsghdr *, struct nl_parser_param *),
1198  struct nl_object **result,
1199  int *syserror)
1200 {
1201  struct nl_cb *cb;
1202  int err;
1203  struct pickup_param pp = {
1204  .parser = parser,
1205  };
1206 
1207  cb = nl_cb_clone(sk->s_cb);
1208  if (cb == NULL)
1209  return -NLE_NOMEM;
1210 
1211  nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __pickup_answer, &pp);
1212  if (syserror) {
1213  *syserror = 0;
1214  pp.syserror = syserror;
1215  nl_cb_err(cb, NL_CB_CUSTOM, __pickup_answer_syserr, &pp);
1216  }
1217 
1218  err = nl_recvmsgs(sk, cb);
1219  if (err < 0)
1220  goto errout;
1221 
1222  *result = pp.result;
1223 errout:
1224  nl_cb_put(cb);
1225 
1226  return err;
1227 }
1228 
1229 /** @} */
1230 
1231 /**
1232  * @name Deprecated
1233  * @{
1234  */
1235 
1236 /**
1237  * @deprecated Please use nl_complete_msg()
1238  */
1239 void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
1240 {
1241  nl_complete_msg(sk, msg);
1242 }
1243 
1244 /**
1245  * @deprecated Please use nl_send_auto()
1246  */
1247 int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
1248 {
1249  return nl_send_auto(sk, msg);
1250 }
1251 
1252 
1253 /** @} */
1254 
1255 /** @} */
1256 
1257 /** @} */
Report received that data was lost.
Definition: handlers.h:99
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1247
Called for every message sent out except for nl_sendto()
Definition: handlers.h:107
Message is an acknowledge.
Definition: handlers.h:103
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition: msg.c:562
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:113
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:35
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:83
#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:46
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:101
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:1178
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:1195
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:68
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:105
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:1077
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:109
Flag NLM_F_DUMP_INTR is set in message.
Definition: handlers.h:115
Skip this message.
Definition: handlers.h:66
Last message in a series of multi part messages received.
Definition: handlers.h:97
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition: msg.c:540
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:1093
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:95
Called instead of internal sequence number checking.
Definition: handlers.h:111
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:446
int nl_wait_for_ack(struct nl_sock *sk)
Wait for ACK.
Definition: nl.c:1112
Proceed with wathever would come next.
Definition: handlers.h:64
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:1239
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:1052
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:658
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