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