2007-09-14 23:28:01 +00:00
|
|
|
/*
|
|
|
|
* lib/nl.c Core Netlink Interface
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
2012-05-10 10:03:59 +00:00
|
|
|
* Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-05-10 10:03:59 +00:00
|
|
|
* @defgroup core Core Library (libnl)
|
|
|
|
*
|
|
|
|
* Socket handling, connection management, sending and receiving of data,
|
|
|
|
* message construction and parsing, object caching system, ...
|
|
|
|
*
|
|
|
|
* This is the API reference of the core library. It is not meant as a guide
|
|
|
|
* but as a reference. Please refer to the core library guide for detailed
|
|
|
|
* documentation on the library architecture and examples:
|
|
|
|
*
|
|
|
|
* * @ref_asciidoc{core,_,Netlink Core Library Development Guide}
|
|
|
|
*
|
|
|
|
*
|
2007-09-14 23:28:01 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-01-24 12:51:24 +00:00
|
|
|
#include <netlink-private/netlink.h>
|
2014-04-09 10:08:52 +00:00
|
|
|
#include <netlink-private/socket.h>
|
2007-09-14 23:28:01 +00:00
|
|
|
#include <netlink/netlink.h>
|
|
|
|
#include <netlink/utils.h>
|
|
|
|
#include <netlink/handlers.h>
|
|
|
|
#include <netlink/msg.h>
|
|
|
|
#include <netlink/attr.h>
|
2015-08-13 11:34:13 +00:00
|
|
|
#include <linux/socket.h>
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2012-05-10 10:03:59 +00:00
|
|
|
/**
|
|
|
|
* @defgroup core_types Data Types
|
|
|
|
*
|
|
|
|
* Core library data types
|
|
|
|
* @{
|
|
|
|
* @}
|
|
|
|
*
|
|
|
|
* @defgroup send_recv Send & Receive Data
|
|
|
|
*
|
|
|
|
* Connection management, sending & receiving of data
|
|
|
|
*
|
|
|
|
* Related sections in the development guide:
|
|
|
|
* - @core_doc{core_send_recv, Sending & Receiving}
|
|
|
|
* - @core_doc{core_sockets, Sockets}
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* Header
|
|
|
|
* ------
|
|
|
|
* ~~~~{.c}
|
|
|
|
* #include <netlink/netlink.h>
|
|
|
|
* ~~~~
|
|
|
|
*/
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/**
|
|
|
|
* @name Connection Management
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-11-08 12:41:54 +00:00
|
|
|
* Create file descriptor and bind socket.
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg protocol Netlink protocol to use (required)
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2012-11-08 12:41:54 +00:00
|
|
|
* Creates a new Netlink socket using `socket()` and binds the socket to the
|
|
|
|
* protocol and local port specified in the `sk` socket object. Fails if
|
|
|
|
* the socket is already connected.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2012-11-08 12:41:54 +00:00
|
|
|
* @note If available, the `close-on-exec` (`SOCK_CLOEXEC`) feature is enabled
|
|
|
|
* automatically on the new file descriptor. This causes the socket to
|
|
|
|
* be closed automatically if any of the `exec` family functions succeed.
|
|
|
|
* This is essential for multi threaded programs.
|
2012-08-29 22:33:40 +00:00
|
|
|
*
|
2014-04-09 10:08:52 +00:00
|
|
|
* @note The local port (`nl_socket_get_local_port()`) is unspecified after
|
|
|
|
* creating a new socket. It only gets determined when accessing the
|
|
|
|
* port the first time or during `nl_connect()`. When nl_connect()
|
|
|
|
* fails during `bind()` due to `ADDRINUSE`, it will retry with
|
|
|
|
* different ports if the port is unspecified. Unless you want to enforce
|
|
|
|
* the use of a specific local port, don't access the local port (or
|
|
|
|
* reset it to `unspecified` by calling `nl_socket_set_local_port(sk, 0)`).
|
|
|
|
* This capability is indicated by
|
|
|
|
* `%NL_CAPABILITY_NL_CONNECT_RETRY_GENERATE_PORT_ON_ADDRINUSE`.
|
|
|
|
*
|
2015-03-05 09:39:43 +00:00
|
|
|
* @note nl_connect() creates and sets the file descriptor. You can setup the file
|
|
|
|
* descriptor yourself by creating and binding it, and then calling
|
|
|
|
* nl_socket_set_fd(). The result will be the same.
|
|
|
|
*
|
2012-11-08 12:41:54 +00:00
|
|
|
* @see nl_socket_alloc()
|
|
|
|
* @see nl_close()
|
2015-03-05 09:39:43 +00:00
|
|
|
* @see nl_socket_set_fd()
|
2011-06-15 15:37:28 +00:00
|
|
|
*
|
2007-09-14 23:28:01 +00:00
|
|
|
* @return 0 on success or a negative error code.
|
2012-11-08 12:41:54 +00:00
|
|
|
*
|
|
|
|
* @retval -NLE_BAD_SOCK Socket is already connected
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_connect(struct nl_sock *sk, int protocol)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2011-06-15 15:37:28 +00:00
|
|
|
int err, flags = 0;
|
2014-04-09 10:08:52 +00:00
|
|
|
int errsv;
|
2007-09-14 23:28:01 +00:00
|
|
|
socklen_t addrlen;
|
2015-03-05 09:50:04 +00:00
|
|
|
struct sockaddr_nl local = { 0 };
|
2015-01-25 17:11:53 +00:00
|
|
|
char buf[64];
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
int try_bind = 1;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2011-06-15 15:37:28 +00:00
|
|
|
#ifdef SOCK_CLOEXEC
|
|
|
|
flags |= SOCK_CLOEXEC;
|
|
|
|
#endif
|
|
|
|
|
2015-03-05 09:39:43 +00:00
|
|
|
if (sk->s_fd != -1)
|
|
|
|
return -NLE_BAD_SOCK;
|
2012-08-29 22:33:40 +00:00
|
|
|
|
2011-06-15 15:37:28 +00:00
|
|
|
sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd < 0) {
|
2014-04-09 10:08:52 +00:00
|
|
|
errsv = errno;
|
2015-01-25 17:11:53 +00:00
|
|
|
NL_DBG(4, "nl_connect(%p): socket() failed with %d (%s)\n", sk, errsv,
|
|
|
|
strerror_r(errsv, buf, sizeof(buf)));
|
2014-04-09 10:08:52 +00:00
|
|
|
err = -nl_syserr2nlerr(errsv);
|
2007-12-17 15:26:49 +00:00
|
|
|
goto errout;
|
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2015-03-05 07:46:31 +00:00
|
|
|
err = nl_socket_set_buffer_size(sk, 0, 0);
|
|
|
|
if (err < 0)
|
|
|
|
goto errout;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2014-04-09 10:08:52 +00:00
|
|
|
if (_nl_socket_is_local_port_unspecified (sk)) {
|
|
|
|
uint32_t port;
|
|
|
|
uint32_t used_ports[32] = { 0 };
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
int ntries = 0;
|
2014-04-09 10:08:52 +00:00
|
|
|
|
|
|
|
while (1) {
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
if (ntries++ > 5) {
|
|
|
|
/* try only a few times. We hit this only if many ports are already in
|
|
|
|
* use but allocated *outside* libnl/generate_local_port(). */
|
2015-08-24 15:57:16 +00:00
|
|
|
_nl_socket_set_local_port_no_release (sk, 0);
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-24 15:57:16 +00:00
|
|
|
port = _nl_socket_set_local_port_no_release(sk, 1);
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
if (port == 0)
|
|
|
|
break;
|
2014-04-09 10:08:52 +00:00
|
|
|
|
|
|
|
err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
|
|
|
|
sizeof(sk->s_local));
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
if (err == 0) {
|
|
|
|
try_bind = 0;
|
2014-04-09 10:08:52 +00:00
|
|
|
break;
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
}
|
2014-04-09 10:08:52 +00:00
|
|
|
|
|
|
|
errsv = errno;
|
|
|
|
if (errsv == EADDRINUSE) {
|
|
|
|
NL_DBG(4, "nl_connect(%p): local port %u already in use. Retry.\n", sk, (unsigned) port);
|
|
|
|
_nl_socket_used_ports_set(used_ports, port);
|
|
|
|
} else {
|
2015-01-25 17:11:53 +00:00
|
|
|
NL_DBG(4, "nl_connect(%p): bind() for port %u failed with %d (%s)\n",
|
|
|
|
sk, (unsigned) port, errsv, strerror_r(errsv, buf, sizeof(buf)));
|
2014-04-09 10:08:52 +00:00
|
|
|
_nl_socket_used_ports_release_all(used_ports);
|
|
|
|
err = -nl_syserr2nlerr(errsv);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_nl_socket_used_ports_release_all(used_ports);
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
}
|
|
|
|
if (try_bind) {
|
2014-04-09 10:08:52 +00:00
|
|
|
err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
|
|
|
|
sizeof(sk->s_local));
|
|
|
|
if (err != 0) {
|
|
|
|
errsv = errno;
|
2015-01-25 17:11:53 +00:00
|
|
|
NL_DBG(4, "nl_connect(%p): bind() failed with %d (%s)\n",
|
|
|
|
sk, errsv, strerror_r(errsv, buf, sizeof(buf)));
|
2014-04-09 10:08:52 +00:00
|
|
|
err = -nl_syserr2nlerr(errsv);
|
|
|
|
goto errout;
|
|
|
|
}
|
2007-12-17 15:26:49 +00:00
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2015-03-05 09:50:04 +00:00
|
|
|
addrlen = sizeof(local);
|
|
|
|
err = getsockname(sk->s_fd, (struct sockaddr *) &local,
|
2007-09-14 23:28:01 +00:00
|
|
|
&addrlen);
|
2007-12-17 15:26:49 +00:00
|
|
|
if (err < 0) {
|
2015-01-25 17:11:53 +00:00
|
|
|
NL_DBG(4, "nl_connect(%p): getsockname() failed with %d (%s)\n",
|
|
|
|
sk, errno, strerror_r(errno, buf, sizeof(buf)));
|
2008-05-14 15:49:44 +00:00
|
|
|
err = -nl_syserr2nlerr(errno);
|
2007-12-17 15:26:49 +00:00
|
|
|
goto errout;
|
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2015-03-05 09:50:04 +00:00
|
|
|
if (addrlen != sizeof(local)) {
|
2008-05-14 15:49:44 +00:00
|
|
|
err = -NLE_NOADDR;
|
2007-12-17 15:26:49 +00:00
|
|
|
goto errout;
|
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2015-03-05 09:50:04 +00:00
|
|
|
if (local.nl_family != AF_NETLINK) {
|
2008-05-14 15:49:44 +00:00
|
|
|
err = -NLE_AF_NOSUPPORT;
|
2007-12-17 15:26:49 +00:00
|
|
|
goto errout;
|
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2015-03-05 09:50:04 +00:00
|
|
|
if (sk->s_local.nl_pid != local.nl_pid) {
|
socket: add fallback for nl_connect() by trying to bind to unspecified local port
libnl allows the user to explicitly set the local port before connecting
the socket. A more convenient way is to leave the local port unspecified
and let libnl generate a port id.
As it is, generate_local_port() would try at most 1024 ports, that
means if a user tries to connect more sockets, the automatism will
fail.
Kernel also supports choosing the local port itself (via netlink_autobind()).
So, this could be fixed by always leaving the port unspecified and let
kernel decide on the port. For that we could entirely drop generate_local_port().
There are however problems with that:
- it is unclear why generate_local_port() was even introduced in the
first place instead of always relying kernel. This code already
appeared in libnl-1, so maybe there was a good reason for it or
it is necessary on some kernel versions.
- The deprecated libnl-1 library also uses a form of generate_local_port().
Its first guess would always be getpid(), but the problem is that
it would not retry on EADDRINUSE. Currently libnl-3 generates ports in
a different sequence and will not generate a conflicting port (until it
already exhausted 1016 other ports).
Hence, currently if your application uses libnl1 and libnl3
together, the automatism might just work without conflicts
(commit 1f734a8f892abcd3f81637df4a089155aca1b66a).
Accidently, kernel/netlink_autobind() also first tries the process
id as port. That means, if we change libnl-3 to leave the decision
to kernel, and
- the application connects sockets both via libnl-1 and libnl-3
- and the libnl-3 socket happens to connect first
then the libnl-1 socket would fail to connect without retrying
another port.
- Removing generate_local_port() entirely changes behavior in the
following case:
sk = nl_socket_alloc();
/* accessing local port before connecting the socket used to
* freeze the local port to the generated value. */
port = nl_socket_get_local_port(sk);
nl_connect(sk, NETLINK_...);
Maybe the issues are minor and it would simplify the code just to get
rid of the cruft. But instead fix the issue without changing behavior.
Just keep trying with generate_local_port() first, before fallback to
kernel.
Reported-by: Julien Courtat <julien.courtat@6wind.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
http://lists.infradead.org/pipermail/libnl/2015-June/001889.html
2015-07-10 12:58:51 +00:00
|
|
|
/* The port id is different. That can happen if the port id was zero
|
|
|
|
* and kernel assigned a local port. */
|
2015-03-05 09:50:04 +00:00
|
|
|
nl_socket_set_local_port (sk, local.nl_pid);
|
|
|
|
}
|
|
|
|
sk->s_local = local;
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_proto = protocol;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-12-17 15:26:49 +00:00
|
|
|
errout:
|
2015-03-05 09:50:04 +00:00
|
|
|
if (sk->s_fd != -1) {
|
|
|
|
close(sk->s_fd);
|
|
|
|
sk->s_fd = -1;
|
|
|
|
}
|
2007-12-17 15:26:49 +00:00
|
|
|
|
|
|
|
return err;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-08 12:41:54 +00:00
|
|
|
* Close Netlink socket
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
*
|
|
|
|
* Closes the Netlink socket using `close()`.
|
|
|
|
*
|
|
|
|
* @note The socket is closed automatically if a `struct nl_sock` object is
|
|
|
|
* freed using `nl_socket_free()`.
|
|
|
|
*
|
|
|
|
* @see nl_connect()
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
void nl_close(struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd >= 0) {
|
|
|
|
close(sk->s_fd);
|
|
|
|
sk->s_fd = -1;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_proto = 0;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Send
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-11-08 00:29:12 +00:00
|
|
|
* Transmit raw data over Netlink socket.
|
2012-11-06 16:48:28 +00:00
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg buf Buffer carrying data to send (required)
|
|
|
|
* @arg size Size of buffer (required)
|
|
|
|
*
|
2012-11-08 00:29:12 +00:00
|
|
|
* Transmits "raw" data over the specified Netlink socket. Unlike the other
|
2012-11-06 16:48:28 +00:00
|
|
|
* transmit functions it does not modify the data in any way. It directly
|
|
|
|
* passes the buffer \c buf of \c size to sendto().
|
|
|
|
*
|
|
|
|
* The message is addressed to the peer as specified in the socket by either
|
|
|
|
* the nl_socket_set_peer_port() or nl_socket_set_peer_groups() function.
|
|
|
|
*
|
|
|
|
* @note Because there is no indication on the message boundaries of the data
|
|
|
|
* being sent, the \c NL_CB_MSG_OUT callback handler will not be invoked
|
|
|
|
* for data that is being sent using this function.
|
|
|
|
*
|
|
|
|
* @see nl_socket_set_peer_port()
|
|
|
|
* @see nl_socket_set_peer_groups()
|
|
|
|
* @see nl_sendmsg()
|
|
|
|
*
|
|
|
|
* @return Number of bytes sent or a negative error code.
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-11-06 16:48:28 +00:00
|
|
|
if (!buf)
|
|
|
|
return -NLE_INVAL;
|
|
|
|
|
|
|
|
if (sk->s_fd < 0)
|
|
|
|
return -NLE_BAD_SOCK;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *)
|
|
|
|
&sk->s_peer, sizeof(sk->s_peer));
|
2015-01-25 17:11:53 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
char errbuf[64];
|
|
|
|
|
|
|
|
NL_DBG(4, "nl_sendto(%p): sendto() failed with %d (%s)\n",
|
|
|
|
sk, errno, strerror_r(errno, errbuf, sizeof(errbuf)));
|
2008-05-14 15:49:44 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2015-01-25 17:11:53 +00:00
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-08 00:29:12 +00:00
|
|
|
* Transmit Netlink message using sendmsg()
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg msg Netlink message to be sent (required)
|
|
|
|
* @arg hdr sendmsg() message header (required)
|
|
|
|
*
|
|
|
|
* Transmits the message specified in \c hdr over the Netlink socket using the
|
|
|
|
* sendmsg() system call.
|
|
|
|
*
|
|
|
|
* @attention
|
|
|
|
* The `msg` argument will *not* be used to derive the message payload that
|
|
|
|
* is being sent out. The `msg` argument is *only* passed on to the
|
|
|
|
* `NL_CB_MSG_OUT` callback. The caller is responsible to initialize the
|
|
|
|
* `hdr` struct properly and have it point to the message payload and
|
|
|
|
* socket address.
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
* This function uses `nlmsg_set_src()` to modify the `msg` argument prior to
|
|
|
|
* invoking the `NL_CB_MSG_OUT` callback to provide the local port number.
|
|
|
|
*
|
|
|
|
* @callback This function triggers the `NL_CB_MSG_OUT` callback.
|
|
|
|
*
|
|
|
|
* @attention
|
|
|
|
* Think twice before using this function. It provides a low level access to
|
|
|
|
* the Netlink socket. Among other limitations, it does not add credentials
|
|
|
|
* even if enabled or respect the destination address specified in the `msg`
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* @see nl_socket_set_local_port()
|
|
|
|
* @see nl_send_auto()
|
|
|
|
* @see nl_send_iovec()
|
|
|
|
*
|
|
|
|
* @return Number of bytes sent on success or a negative error code.
|
|
|
|
*
|
|
|
|
* @lowlevel
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
struct nl_cb *cb;
|
|
|
|
int ret;
|
|
|
|
|
2012-11-08 00:29:12 +00:00
|
|
|
if (sk->s_fd < 0)
|
|
|
|
return -NLE_BAD_SOCK;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
nlmsg_set_src(msg, &sk->s_local);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
cb = sk->s_cb;
|
2007-09-14 23:28:01 +00:00
|
|
|
if (cb->cb_set[NL_CB_MSG_OUT])
|
2010-11-18 13:16:33 +00:00
|
|
|
if ((ret = nl_cb_call(cb, NL_CB_MSG_OUT, msg)) != NL_OK)
|
|
|
|
return ret;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
ret = sendmsg(sk->s_fd, hdr, 0);
|
2015-01-25 17:11:53 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
char errbuf[64];
|
|
|
|
|
|
|
|
NL_DBG(4, "nl_sendmsg(%p): sendmsg() failed with %d (%s)\n",
|
|
|
|
sk, errno, strerror_r(errno, errbuf, sizeof(errbuf)));
|
2008-05-14 15:49:44 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2015-01-25 17:11:53 +00:00
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2010-02-24 20:29:13 +00:00
|
|
|
NL_DBG(4, "sent %d bytes\n", ret);
|
2007-09-14 23:28:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-11-08 00:29:12 +00:00
|
|
|
* Transmit Netlink message (taking IO vector)
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg msg Netlink message to be sent (required)
|
|
|
|
* @arg iov IO vector to be sent (required)
|
|
|
|
* @arg iovlen Number of struct iovec to be sent (required)
|
|
|
|
*
|
|
|
|
* This function is identical to nl_send() except that instead of taking a
|
|
|
|
* `struct nl_msg` object it takes an IO vector. Please see the description
|
|
|
|
* of `nl_send()`.
|
|
|
|
*
|
|
|
|
* @callback This function triggers the `NL_CB_MSG_OUT` callback.
|
|
|
|
*
|
|
|
|
* @see nl_send()
|
|
|
|
*
|
|
|
|
* @return Number of bytes sent on success or a negative error code.
|
|
|
|
*
|
|
|
|
* @lowlevel
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2010-07-03 13:14:13 +00:00
|
|
|
int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_nl *dst;
|
|
|
|
struct ucred *creds;
|
|
|
|
struct msghdr hdr = {
|
2008-05-15 11:26:32 +00:00
|
|
|
.msg_name = (void *) &sk->s_peer,
|
2007-09-14 23:28:01 +00:00
|
|
|
.msg_namelen = sizeof(struct sockaddr_nl),
|
2010-02-24 20:29:13 +00:00
|
|
|
.msg_iov = iov,
|
|
|
|
.msg_iovlen = iovlen,
|
2007-09-14 23:28:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Overwrite destination if specified in the message itself, defaults
|
2008-05-15 11:26:32 +00:00
|
|
|
* to the peer address of the socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
|
|
|
dst = nlmsg_get_dst(msg);
|
|
|
|
if (dst->nl_family == AF_NETLINK)
|
|
|
|
hdr.msg_name = dst;
|
|
|
|
|
|
|
|
/* Add credentials if present. */
|
|
|
|
creds = nlmsg_get_creds(msg);
|
|
|
|
if (creds != NULL) {
|
|
|
|
char buf[CMSG_SPACE(sizeof(struct ucred))];
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
|
|
|
hdr.msg_control = buf;
|
|
|
|
hdr.msg_controllen = sizeof(buf);
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&hdr);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_CREDENTIALS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
|
|
|
|
memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
|
|
|
|
}
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
return nl_sendmsg(sk, msg, &hdr);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-08 00:29:12 +00:00
|
|
|
* Transmit Netlink message
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg msg Netlink message (required)
|
|
|
|
*
|
|
|
|
* Transmits the Netlink message `msg` over the Netlink socket using the
|
|
|
|
* `sendmsg()` system call. This function is based on `nl_send_iovec()` but
|
|
|
|
* takes care of initializing a `struct iovec` based on the `msg` object.
|
|
|
|
*
|
|
|
|
* The message is addressed to the peer as specified in the socket by either
|
|
|
|
* the nl_socket_set_peer_port() or nl_socket_set_peer_groups() function.
|
|
|
|
* The peer address can be overwritten by specifying an address in the `msg`
|
|
|
|
* object using nlmsg_set_dst().
|
|
|
|
*
|
|
|
|
* If present in the `msg`, credentials set by the nlmsg_set_creds() function
|
|
|
|
* are added to the control buffer of the message.
|
|
|
|
*
|
2012-11-08 12:19:07 +00:00
|
|
|
* @par Overwriting Capability:
|
|
|
|
* Calls to this function can be overwritten by providing an alternative using
|
|
|
|
* the nl_cb_overwrite_send() function.
|
|
|
|
*
|
2012-11-08 00:29:12 +00:00
|
|
|
* @callback This function triggers the `NL_CB_MSG_OUT` callback.
|
|
|
|
*
|
|
|
|
* @attention
|
|
|
|
* Unlike `nl_send_auto()`, this function does *not* finalize the message in
|
|
|
|
* terms of automatically adding needed flags or filling out port numbers.
|
|
|
|
*
|
|
|
|
* @see nl_send_auto()
|
|
|
|
* @see nl_send_iovec()
|
|
|
|
* @see nl_socket_set_peer_port()
|
|
|
|
* @see nl_socket_set_peer_groups()
|
|
|
|
* @see nlmsg_set_dst()
|
|
|
|
* @see nlmsg_set_creds()
|
2012-11-08 12:19:07 +00:00
|
|
|
* @see nl_cb_overwrite_send()
|
2012-11-08 00:29:12 +00:00
|
|
|
*
|
|
|
|
* @return Number of bytes sent on success or a negative error code.
|
2010-02-24 20:29:13 +00:00
|
|
|
*/
|
|
|
|
int nl_send(struct nl_sock *sk, struct nl_msg *msg)
|
|
|
|
{
|
2012-11-08 12:19:07 +00:00
|
|
|
struct nl_cb *cb = sk->s_cb;
|
|
|
|
|
|
|
|
if (cb->cb_send_ow)
|
|
|
|
return cb->cb_send_ow(sk, msg);
|
|
|
|
else {
|
|
|
|
struct iovec iov = {
|
|
|
|
.iov_base = (void *) nlmsg_hdr(msg),
|
|
|
|
.iov_len = nlmsg_hdr(msg)->nlmsg_len,
|
|
|
|
};
|
2010-02-24 20:29:13 +00:00
|
|
|
|
2012-11-08 12:19:07 +00:00
|
|
|
return nl_send_iovec(sk, msg, &iov, 1);
|
|
|
|
}
|
2010-02-24 20:29:13 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 00:29:12 +00:00
|
|
|
/**
|
|
|
|
* Finalize Netlink message
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg msg Netlink message (required)
|
|
|
|
*
|
|
|
|
* This function finalizes a Netlink message by completing the message with
|
|
|
|
* desirable flags and values depending on the socket configuration.
|
|
|
|
*
|
|
|
|
* - If not yet filled out, the source address of the message (`nlmsg_pid`)
|
|
|
|
* will be set to the local port number of the socket.
|
|
|
|
* - If not yet specified, the next available sequence number is assigned
|
|
|
|
* to the message (`nlmsg_seq`).
|
|
|
|
* - If not yet specified, the protocol field of the message will be set to
|
|
|
|
* the protocol field of the socket.
|
|
|
|
* - The `NLM_F_REQUEST` Netlink message flag will be set.
|
|
|
|
* - The `NLM_F_ACK` flag will be set if Auto-ACK mode is enabled on the
|
|
|
|
* socket.
|
|
|
|
*/
|
2010-11-18 13:13:49 +00:00
|
|
|
void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
|
|
|
|
nlh = nlmsg_hdr(msg);
|
2012-08-30 12:40:11 +00:00
|
|
|
if (nlh->nlmsg_pid == NL_AUTO_PORT)
|
2014-04-09 10:08:52 +00:00
|
|
|
nlh->nlmsg_pid = nl_socket_get_local_port(sk);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2012-08-30 12:40:11 +00:00
|
|
|
if (nlh->nlmsg_seq == NL_AUTO_SEQ)
|
2008-05-15 11:26:32 +00:00
|
|
|
nlh->nlmsg_seq = sk->s_seq_next++;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
if (msg->nm_protocol == -1)
|
2008-05-15 11:26:32 +00:00
|
|
|
msg->nm_protocol = sk->s_proto;
|
2010-02-24 20:29:13 +00:00
|
|
|
|
2008-10-20 11:02:38 +00:00
|
|
|
nlh->nlmsg_flags |= NLM_F_REQUEST;
|
|
|
|
|
|
|
|
if (!(sk->s_flags & NL_NO_AUTO_ACK))
|
|
|
|
nlh->nlmsg_flags |= NLM_F_ACK;
|
2010-02-24 20:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-08 00:29:12 +00:00
|
|
|
* Finalize and transmit Netlink message
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg msg Netlink message (required)
|
2010-02-24 20:29:13 +00:00
|
|
|
*
|
2012-11-08 00:29:12 +00:00
|
|
|
* Finalizes the message by passing it to `nl_complete_msg()` and transmits it
|
|
|
|
* by passing it to `nl_send()`.
|
2010-11-18 13:13:49 +00:00
|
|
|
*
|
2012-11-08 00:29:12 +00:00
|
|
|
* @callback This function triggers the `NL_CB_MSG_OUT` callback.
|
2010-02-24 20:29:13 +00:00
|
|
|
*
|
2012-11-08 00:29:12 +00:00
|
|
|
* @see nl_complete_msg()
|
2010-02-24 20:29:13 +00:00
|
|
|
* @see nl_send()
|
2012-11-08 00:29:12 +00:00
|
|
|
*
|
|
|
|
* @return Number of bytes sent or a negative error code.
|
2010-02-24 20:29:13 +00:00
|
|
|
*/
|
2010-11-18 13:13:49 +00:00
|
|
|
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
|
2010-02-24 20:29:13 +00:00
|
|
|
{
|
2010-11-18 13:13:49 +00:00
|
|
|
nl_complete_msg(sk, msg);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2012-11-08 12:19:07 +00:00
|
|
|
return nl_send(sk, msg);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 17:11:52 +00:00
|
|
|
/**
|
2012-11-08 00:29:12 +00:00
|
|
|
* Finalize and transmit Netlink message and wait for ACK or error message
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg msg Netlink message (required)
|
|
|
|
*
|
|
|
|
* Passes the `msg` to `nl_send_auto()` to finalize and transmit it. Frees the
|
|
|
|
* message and waits (sleeps) for the ACK or error message to be received.
|
2011-03-25 17:11:52 +00:00
|
|
|
*
|
2012-11-08 00:29:12 +00:00
|
|
|
* @attention
|
|
|
|
* Disabling Auto-ACK (nl_socket_disable_auto_ack()) will cause this function
|
|
|
|
* to return immediately after transmitting the message. However, the peer may
|
|
|
|
* still be returning an error message in response to the request. It is the
|
|
|
|
* responsibility of the caller to handle such messages.
|
2011-03-25 17:11:52 +00:00
|
|
|
*
|
2012-11-08 00:29:12 +00:00
|
|
|
* @callback This function triggers the `NL_CB_MSG_OUT` callback.
|
|
|
|
*
|
|
|
|
* @attention
|
|
|
|
* This function frees the `msg` object after transmitting it by calling
|
|
|
|
* `nlmsg_free()`.
|
2011-03-25 17:11:52 +00:00
|
|
|
*
|
|
|
|
* @see nl_send_auto().
|
2012-11-08 00:29:12 +00:00
|
|
|
* @see nl_wait_for_ack()
|
2011-03-25 17:11:52 +00:00
|
|
|
*
|
|
|
|
* @return 0 on success or a negative error code.
|
|
|
|
*/
|
|
|
|
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = nl_send_auto(sk, msg);
|
|
|
|
nlmsg_free(msg);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return wait_for_ack(sk);
|
|
|
|
}
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/**
|
2012-11-08 20:17:32 +00:00
|
|
|
* Construct and transmit a Netlink message
|
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg type Netlink message type (required)
|
|
|
|
* @arg flags Netlink message flags (optional)
|
|
|
|
* @arg buf Data buffer (optional)
|
|
|
|
* @arg size Size of data buffer (optional)
|
|
|
|
*
|
|
|
|
* Allocates a new Netlink message based on `type` and `flags`. If `buf`
|
|
|
|
* points to payload of length `size` that payload will be appended to the
|
|
|
|
* message.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2012-11-08 20:17:32 +00:00
|
|
|
* Sends out the message using `nl_send_auto()` and frees the message
|
|
|
|
* afterwards.
|
|
|
|
*
|
|
|
|
* @see nl_send_auto()
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
|
|
|
* @return Number of characters sent on success or a negative error code.
|
2012-11-08 20:17:32 +00:00
|
|
|
* @retval -NLE_NOMEM Unable to allocate Netlink message
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf,
|
2007-09-14 23:28:01 +00:00
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct nl_msg *msg;
|
|
|
|
|
|
|
|
msg = nlmsg_alloc_simple(type, flags);
|
|
|
|
if (!msg)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -NLE_NOMEM;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-01-14 15:17:56 +00:00
|
|
|
if (buf && size) {
|
|
|
|
err = nlmsg_append(msg, buf, size, NLMSG_ALIGNTO);
|
|
|
|
if (err < 0)
|
|
|
|
goto errout;
|
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2012-11-08 20:17:32 +00:00
|
|
|
err = nl_send_auto(sk, msg);
|
2008-01-14 15:17:56 +00:00
|
|
|
errout:
|
2007-09-14 23:28:01 +00:00
|
|
|
nlmsg_free(msg);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Receive
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive data from netlink socket
|
2012-11-06 13:21:18 +00:00
|
|
|
* @arg sk Netlink socket (required)
|
|
|
|
* @arg nla Netlink socket structure to hold address of peer (required)
|
|
|
|
* @arg buf Destination pointer for message content (required)
|
|
|
|
* @arg creds Destination pointer for credentials (optional)
|
|
|
|
*
|
|
|
|
* Receives data from a connected netlink socket using recvmsg() and returns
|
|
|
|
* the number of bytes read. The read data is stored in a newly allocated
|
|
|
|
* buffer that is assigned to \c *buf. The peer's netlink address will be
|
|
|
|
* stored in \c *nla.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2012-11-06 13:21:18 +00:00
|
|
|
* This function blocks until data is available to be read unless the socket
|
|
|
|
* has been put into non-blocking mode using nl_socket_set_nonblocking() in
|
|
|
|
* which case this function will return immediately with a return value of 0.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2012-11-05 23:19:28 +00:00
|
|
|
* The buffer size used when reading from the netlink socket and thus limiting
|
|
|
|
* the maximum size of a netlink message that can be read defaults to the size
|
|
|
|
* of a memory page (getpagesize()). The buffer size can be modified on a per
|
|
|
|
* socket level using the function nl_socket_set_msg_buf_size().
|
|
|
|
*
|
2012-11-06 13:21:18 +00:00
|
|
|
* If message peeking is enabled using nl_socket_enable_msg_peek() the size of
|
|
|
|
* the message to be read will be determined using the MSG_PEEK flag prior to
|
|
|
|
* performing the actual read. This leads to an additional recvmsg() call for
|
|
|
|
* every read operation which has performance implications and is not
|
|
|
|
* recommended for high throughput protocols.
|
|
|
|
*
|
|
|
|
* An eventual interruption of the recvmsg() system call is automatically
|
|
|
|
* handled by retrying the operation.
|
|
|
|
*
|
|
|
|
* If receiving of credentials has been enabled using the function
|
|
|
|
* nl_socket_set_passcred(), this function will allocate a new struct ucred
|
|
|
|
* filled with the received credentials and assign it to \c *creds. The caller
|
|
|
|
* is responsible for freeing the buffer.
|
|
|
|
*
|
|
|
|
* @note The caller is responsible to free the returned data buffer and if
|
|
|
|
* enabled, the credentials buffer.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2012-11-06 13:21:18 +00:00
|
|
|
* @see nl_socket_set_nonblocking()
|
2012-11-05 23:19:28 +00:00
|
|
|
* @see nl_socket_set_msg_buf_size()
|
2012-11-06 13:21:18 +00:00
|
|
|
* @see nl_socket_enable_msg_peek()
|
|
|
|
* @see nl_socket_set_passcred()
|
2012-11-05 23:19:28 +00:00
|
|
|
*
|
2012-11-06 13:21:18 +00:00
|
|
|
* @return Number of bytes read, 0 on EOF, 0 on no data event (non-blocking
|
|
|
|
* mode), or a negative error code.
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
|
2007-09-14 23:28:01 +00:00
|
|
|
unsigned char **buf, struct ucred **creds)
|
|
|
|
{
|
2012-06-08 14:15:06 +00:00
|
|
|
ssize_t n;
|
2007-09-14 23:28:01 +00:00
|
|
|
int flags = 0;
|
|
|
|
static int page_size = 0;
|
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg = {
|
|
|
|
.msg_name = (void *) nla,
|
|
|
|
.msg_namelen = sizeof(struct sockaddr_nl),
|
|
|
|
.msg_iov = &iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
};
|
2012-10-19 16:58:58 +00:00
|
|
|
struct ucred* tmpcreds = NULL;
|
|
|
|
int retval = 0;
|
2010-11-22 13:19:40 +00:00
|
|
|
|
2012-11-06 13:24:09 +00:00
|
|
|
if (!buf || !nla)
|
2012-11-06 13:22:50 +00:00
|
|
|
return -NLE_INVAL;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_flags & NL_MSG_PEEK)
|
2012-05-28 12:05:27 +00:00
|
|
|
flags |= MSG_PEEK | MSG_TRUNC;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
if (page_size == 0)
|
2013-05-08 11:52:27 +00:00
|
|
|
page_size = getpagesize() * 4;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2012-10-09 14:15:08 +00:00
|
|
|
iov.iov_len = sk->s_bufsize ? : page_size;
|
2012-10-19 16:58:58 +00:00
|
|
|
iov.iov_base = malloc(iov.iov_len);
|
|
|
|
|
|
|
|
if (!iov.iov_base) {
|
2012-11-06 13:27:34 +00:00
|
|
|
retval = -NLE_NOMEM;
|
|
|
|
goto abort;
|
2012-10-19 16:58:58 +00:00
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2012-10-19 17:23:10 +00:00
|
|
|
if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
|
2007-09-14 23:28:01 +00:00
|
|
|
msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
|
2012-10-19 17:06:23 +00:00
|
|
|
msg.msg_control = malloc(msg.msg_controllen);
|
2012-10-19 16:58:58 +00:00
|
|
|
if (!msg.msg_control) {
|
|
|
|
retval = -NLE_NOMEM;
|
|
|
|
goto abort;
|
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
retry:
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
n = recvmsg(sk->s_fd, &msg, flags);
|
2012-10-19 16:58:58 +00:00
|
|
|
if (!n) {
|
|
|
|
retval = 0;
|
2007-09-14 23:28:01 +00:00
|
|
|
goto abort;
|
2012-10-19 16:58:58 +00:00
|
|
|
}
|
2012-09-09 20:25:07 +00:00
|
|
|
if (n < 0) {
|
2015-01-25 17:11:53 +00:00
|
|
|
char errbuf[64];
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
if (errno == EINTR) {
|
|
|
|
NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
|
|
|
|
goto retry;
|
2012-09-09 20:25:07 +00:00
|
|
|
}
|
2015-01-25 17:11:53 +00:00
|
|
|
|
|
|
|
NL_DBG(4, "nl_sendmsg(%p): nl_recv() failed with %d (%s)\n",
|
|
|
|
sk, errno, strerror_r(errno, errbuf, sizeof(errbuf)));
|
2012-10-19 16:58:58 +00:00
|
|
|
retval = -nl_syserr2nlerr(errno);
|
|
|
|
goto abort;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2012-05-28 12:05:27 +00:00
|
|
|
if (msg.msg_flags & MSG_CTRUNC) {
|
2012-10-19 16:58:58 +00:00
|
|
|
void *tmp;
|
2015-09-14 15:59:58 +00:00
|
|
|
|
|
|
|
if (msg.msg_controllen == 0) {
|
|
|
|
retval = -NLE_MSG_TRUNC;
|
|
|
|
NL_DBG(4, "recvmsg(%p): Received unexpected control data", sk);
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
|
2012-05-28 12:05:27 +00:00
|
|
|
msg.msg_controllen *= 2;
|
2012-10-19 16:58:58 +00:00
|
|
|
tmp = realloc(msg.msg_control, msg.msg_controllen);
|
|
|
|
if (!tmp) {
|
2012-11-06 13:27:34 +00:00
|
|
|
retval = -NLE_NOMEM;
|
|
|
|
goto abort;
|
2012-10-19 16:58:58 +00:00
|
|
|
}
|
|
|
|
msg.msg_control = tmp;
|
2012-05-28 12:05:27 +00:00
|
|
|
goto retry;
|
2012-09-09 20:25:07 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:06:23 +00:00
|
|
|
if (iov.iov_len < n || (msg.msg_flags & MSG_TRUNC)) {
|
2012-10-19 16:58:58 +00:00
|
|
|
void *tmp;
|
2015-06-04 12:43:58 +00:00
|
|
|
|
|
|
|
/* respond with error to an incomplete message */
|
|
|
|
if (!(sk->s_flags & NL_MSG_PEEK)) {
|
|
|
|
retval = -NLE_MSG_TRUNC;
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/* Provided buffer is not long enough, enlarge it
|
2012-05-28 12:05:27 +00:00
|
|
|
* to size of n (which should be total length of the message)
|
2007-09-14 23:28:01 +00:00
|
|
|
* and try again. */
|
2012-05-28 12:05:27 +00:00
|
|
|
iov.iov_len = n;
|
2012-10-19 16:58:58 +00:00
|
|
|
tmp = realloc(iov.iov_base, iov.iov_len);
|
|
|
|
if (!tmp) {
|
2012-11-06 13:27:34 +00:00
|
|
|
retval = -NLE_NOMEM;
|
|
|
|
goto abort;
|
2012-10-19 16:58:58 +00:00
|
|
|
}
|
|
|
|
iov.iov_base = tmp;
|
2012-05-28 12:05:27 +00:00
|
|
|
flags = 0;
|
2007-09-14 23:28:01 +00:00
|
|
|
goto retry;
|
2012-09-09 20:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags != 0) {
|
2007-09-14 23:28:01 +00:00
|
|
|
/* Buffer is big enough, do the actual reading */
|
|
|
|
flags = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
|
2012-10-19 16:58:58 +00:00
|
|
|
retval = -NLE_NOADDR;
|
|
|
|
goto abort;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:23:10 +00:00
|
|
|
if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
|
|
|
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
|
|
|
if (cmsg->cmsg_level != SOL_SOCKET)
|
|
|
|
continue;
|
|
|
|
if (cmsg->cmsg_type != SCM_CREDENTIALS)
|
|
|
|
continue;
|
|
|
|
tmpcreds = malloc(sizeof(*tmpcreds));
|
|
|
|
if (!tmpcreds) {
|
|
|
|
retval = -NLE_NOMEM;
|
|
|
|
goto abort;
|
2010-11-22 13:18:59 +00:00
|
|
|
}
|
2012-10-19 17:23:10 +00:00
|
|
|
memcpy(tmpcreds, CMSG_DATA(cmsg), sizeof(*tmpcreds));
|
2007-09-14 23:28:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 16:58:58 +00:00
|
|
|
retval = n;
|
2007-09-14 23:28:01 +00:00
|
|
|
abort:
|
|
|
|
free(msg.msg_control);
|
2012-10-19 16:58:58 +00:00
|
|
|
|
|
|
|
if (retval <= 0) {
|
2012-11-06 13:27:34 +00:00
|
|
|
free(iov.iov_base);
|
|
|
|
iov.iov_base = NULL;
|
|
|
|
free(tmpcreds);
|
|
|
|
tmpcreds = NULL;
|
2012-11-05 23:21:35 +00:00
|
|
|
} else
|
|
|
|
*buf = iov.iov_base;
|
2012-10-19 16:58:58 +00:00
|
|
|
|
|
|
|
if (creds)
|
2012-11-06 13:27:34 +00:00
|
|
|
*creds = tmpcreds;
|
2012-10-19 16:58:58 +00:00
|
|
|
|
|
|
|
return retval;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2012-05-10 10:03:59 +00:00
|
|
|
/** @cond SKIP */
|
2007-09-14 23:28:01 +00:00
|
|
|
#define NL_CB_CALL(cb, type, msg) \
|
|
|
|
do { \
|
|
|
|
err = nl_cb_call(cb, type, msg); \
|
|
|
|
switch (err) { \
|
|
|
|
case NL_OK: \
|
|
|
|
err = 0; \
|
|
|
|
break; \
|
|
|
|
case NL_SKIP: \
|
|
|
|
goto skip; \
|
|
|
|
case NL_STOP: \
|
|
|
|
goto stop; \
|
|
|
|
default: \
|
|
|
|
goto out; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2012-05-10 10:03:59 +00:00
|
|
|
/** @endcond */
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2012-04-21 10:46:01 +00:00
|
|
|
int n, err = 0, multipart = 0, interrupted = 0, nrecv = 0;
|
2008-10-14 17:26:44 +00:00
|
|
|
unsigned char *buf = NULL;
|
2007-09-14 23:28:01 +00:00
|
|
|
struct nlmsghdr *hdr;
|
2012-09-09 19:33:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
nla is passed on to not only to nl_recv() but may also be passed
|
|
|
|
to a function pointer provided by the caller which may or may not
|
|
|
|
initialize the variable. Thomas Graf.
|
|
|
|
*/
|
2007-09-14 23:28:01 +00:00
|
|
|
struct sockaddr_nl nla = {0};
|
|
|
|
struct nl_msg *msg = NULL;
|
|
|
|
struct ucred *creds = NULL;
|
|
|
|
|
|
|
|
continue_reading:
|
2008-05-15 11:26:32 +00:00
|
|
|
NL_DBG(3, "Attempting to read from %p\n", sk);
|
2007-09-14 23:28:01 +00:00
|
|
|
if (cb->cb_recv_ow)
|
2008-05-15 11:26:32 +00:00
|
|
|
n = cb->cb_recv_ow(sk, &nla, &buf, &creds);
|
2007-09-14 23:28:01 +00:00
|
|
|
else
|
2008-05-15 11:26:32 +00:00
|
|
|
n = nl_recv(sk, &nla, &buf, &creds);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
if (n <= 0)
|
|
|
|
return n;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
hdr = (struct nlmsghdr *) buf;
|
|
|
|
while (nlmsg_ok(hdr, n)) {
|
2011-10-28 10:30:31 +00:00
|
|
|
NL_DBG(3, "recvmsgs(%p): Processing valid message...\n", sk);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-10-14 17:26:44 +00:00
|
|
|
nlmsg_free(msg);
|
2007-09-14 23:28:01 +00:00
|
|
|
msg = nlmsg_convert(hdr);
|
|
|
|
if (!msg) {
|
2008-05-14 15:49:44 +00:00
|
|
|
err = -NLE_NOMEM;
|
2007-09-14 23:28:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
nlmsg_set_proto(msg, sk->s_proto);
|
2007-09-14 23:28:01 +00:00
|
|
|
nlmsg_set_src(msg, &nla);
|
|
|
|
if (creds)
|
|
|
|
nlmsg_set_creds(msg, creds);
|
|
|
|
|
2012-04-21 10:46:01 +00:00
|
|
|
nrecv++;
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/* Raw callback is the first, it gives the most control
|
|
|
|
* to the user and he can do his very own parsing. */
|
|
|
|
if (cb->cb_set[NL_CB_MSG_IN])
|
|
|
|
NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
|
|
|
|
|
|
|
|
/* Sequence number checking. The check may be done by
|
|
|
|
* the user, otherwise a very simple check is applied
|
|
|
|
* enforcing strict ordering */
|
2010-11-18 11:51:20 +00:00
|
|
|
if (cb->cb_set[NL_CB_SEQ_CHECK]) {
|
2007-09-14 23:28:01 +00:00
|
|
|
NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
|
2010-11-18 11:51:20 +00:00
|
|
|
|
|
|
|
/* Only do sequence checking if auto-ack mode is enabled */
|
|
|
|
} else if (!(sk->s_flags & NL_NO_AUTO_ACK)) {
|
|
|
|
if (hdr->nlmsg_seq != sk->s_seq_expect) {
|
|
|
|
if (cb->cb_set[NL_CB_INVALID])
|
|
|
|
NL_CB_CALL(cb, NL_CB_INVALID, msg);
|
|
|
|
else {
|
|
|
|
err = -NLE_SEQ_MISMATCH;
|
|
|
|
goto out;
|
|
|
|
}
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr->nlmsg_type == NLMSG_DONE ||
|
|
|
|
hdr->nlmsg_type == NLMSG_ERROR ||
|
|
|
|
hdr->nlmsg_type == NLMSG_NOOP ||
|
|
|
|
hdr->nlmsg_type == NLMSG_OVERRUN) {
|
|
|
|
/* We can't check for !NLM_F_MULTI since some netlink
|
|
|
|
* users in the kernel are broken. */
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_seq_expect++;
|
2007-09-14 23:28:01 +00:00
|
|
|
NL_DBG(3, "recvmsgs(%p): Increased expected " \
|
|
|
|
"sequence number to %d\n",
|
2008-05-15 11:26:32 +00:00
|
|
|
sk, sk->s_seq_expect);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr->nlmsg_flags & NLM_F_MULTI)
|
|
|
|
multipart = 1;
|
2011-07-14 08:51:49 +00:00
|
|
|
|
|
|
|
if (hdr->nlmsg_flags & NLM_F_DUMP_INTR) {
|
|
|
|
if (cb->cb_set[NL_CB_DUMP_INTR])
|
|
|
|
NL_CB_CALL(cb, NL_CB_DUMP_INTR, msg);
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* We have to continue reading to clear
|
|
|
|
* all messages until a NLMSG_DONE is
|
|
|
|
* received and report the inconsistency.
|
|
|
|
*/
|
|
|
|
interrupted = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-11-26 17:13:36 +00:00
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/* Other side wishes to see an ack for this message */
|
|
|
|
if (hdr->nlmsg_flags & NLM_F_ACK) {
|
|
|
|
if (cb->cb_set[NL_CB_SEND_ACK])
|
|
|
|
NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
|
|
|
|
else {
|
|
|
|
/* FIXME: implement */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 16:23:54 +00:00
|
|
|
/* messages terminates a multipart message, this is
|
2007-09-14 23:28:01 +00:00
|
|
|
* usually the end of a message and therefore we slip
|
|
|
|
* out of the loop by default. the user may overrule
|
|
|
|
* this action by skipping this packet. */
|
|
|
|
if (hdr->nlmsg_type == NLMSG_DONE) {
|
|
|
|
multipart = 0;
|
|
|
|
if (cb->cb_set[NL_CB_FINISH])
|
|
|
|
NL_CB_CALL(cb, NL_CB_FINISH, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Message to be ignored, the default action is to
|
|
|
|
* skip this message if no callback is specified. The
|
|
|
|
* user may overrule this action by returning
|
|
|
|
* NL_PROCEED. */
|
|
|
|
else if (hdr->nlmsg_type == NLMSG_NOOP) {
|
|
|
|
if (cb->cb_set[NL_CB_SKIPPED])
|
|
|
|
NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
|
|
|
|
else
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Data got lost, report back to user. The default action is to
|
|
|
|
* quit parsing. The user may overrule this action by retuning
|
|
|
|
* NL_SKIP or NL_PROCEED (dangerous) */
|
|
|
|
else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
|
|
|
|
if (cb->cb_set[NL_CB_OVERRUN])
|
|
|
|
NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
|
|
|
|
else {
|
2008-05-14 15:49:44 +00:00
|
|
|
err = -NLE_MSG_OVERFLOW;
|
2007-09-14 23:28:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Message carries a nlmsgerr */
|
|
|
|
else if (hdr->nlmsg_type == NLMSG_ERROR) {
|
|
|
|
struct nlmsgerr *e = nlmsg_data(hdr);
|
|
|
|
|
2010-11-22 12:13:04 +00:00
|
|
|
if (hdr->nlmsg_len < nlmsg_size(sizeof(*e))) {
|
2007-09-14 23:28:01 +00:00
|
|
|
/* Truncated error message, the default action
|
|
|
|
* is to stop parsing. The user may overrule
|
|
|
|
* this action by returning NL_SKIP or
|
|
|
|
* NL_PROCEED (dangerous) */
|
|
|
|
if (cb->cb_set[NL_CB_INVALID])
|
|
|
|
NL_CB_CALL(cb, NL_CB_INVALID, msg);
|
|
|
|
else {
|
2008-05-14 15:49:44 +00:00
|
|
|
err = -NLE_MSG_TRUNC;
|
2007-09-14 23:28:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else if (e->error) {
|
2015-01-25 17:11:53 +00:00
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
NL_DBG(4, "recvmsgs(%p): RTNETLINK responded with %d (%s)\n",
|
|
|
|
sk, -e->error, strerror_r(-e->error, buf, sizeof(buf)));
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/* Error message reported back from kernel. */
|
|
|
|
if (cb->cb_err) {
|
|
|
|
err = cb->cb_err(&nla, e,
|
|
|
|
cb->cb_err_arg);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
else if (err == NL_SKIP)
|
|
|
|
goto skip;
|
|
|
|
else if (err == NL_STOP) {
|
2008-05-14 15:49:44 +00:00
|
|
|
err = -nl_syserr2nlerr(e->error);
|
2007-09-14 23:28:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
2008-05-14 15:49:44 +00:00
|
|
|
err = -nl_syserr2nlerr(e->error);
|
2007-09-14 23:28:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else if (cb->cb_set[NL_CB_ACK])
|
|
|
|
NL_CB_CALL(cb, NL_CB_ACK, msg);
|
|
|
|
} else {
|
|
|
|
/* Valid message (not checking for MULTIPART bit to
|
|
|
|
* get along with broken kernels. NL_SKIP has no
|
|
|
|
* effect on this. */
|
|
|
|
if (cb->cb_set[NL_CB_VALID])
|
|
|
|
NL_CB_CALL(cb, NL_CB_VALID, msg);
|
|
|
|
}
|
|
|
|
skip:
|
|
|
|
err = 0;
|
|
|
|
hdr = nlmsg_next(hdr, &n);
|
|
|
|
}
|
2014-11-26 17:13:36 +00:00
|
|
|
|
2008-10-14 17:26:44 +00:00
|
|
|
nlmsg_free(msg);
|
2007-09-14 23:28:01 +00:00
|
|
|
free(buf);
|
|
|
|
free(creds);
|
|
|
|
buf = NULL;
|
|
|
|
msg = NULL;
|
|
|
|
creds = NULL;
|
|
|
|
|
|
|
|
if (multipart) {
|
|
|
|
/* Multipart message not yet complete, continue reading */
|
|
|
|
goto continue_reading;
|
|
|
|
}
|
|
|
|
stop:
|
|
|
|
err = 0;
|
|
|
|
out:
|
|
|
|
nlmsg_free(msg);
|
|
|
|
free(buf);
|
|
|
|
free(creds);
|
|
|
|
|
2011-07-14 08:51:49 +00:00
|
|
|
if (interrupted)
|
|
|
|
err = -NLE_DUMP_INTR;
|
|
|
|
|
2012-04-21 10:46:01 +00:00
|
|
|
if (!err)
|
|
|
|
err = nrecv;
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-05-08 20:48:00 +00:00
|
|
|
/**
|
|
|
|
* Receive a set of messages from a netlink socket and report parsed messages
|
|
|
|
* @arg sk Netlink socket.
|
|
|
|
* @arg cb set of callbacks to control behaviour.
|
|
|
|
*
|
|
|
|
* This function is identical to nl_recvmsgs() to the point that it will
|
|
|
|
* return the number of parsed messages instead of 0 on success.
|
|
|
|
*
|
|
|
|
* @see nl_recvmsgs()
|
|
|
|
*
|
|
|
|
* @return Number of received messages or a negative error code from nl_recv().
|
|
|
|
*/
|
|
|
|
int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
|
|
|
|
{
|
|
|
|
if (cb->cb_recvmsgs_ow)
|
|
|
|
return cb->cb_recvmsgs_ow(sk, cb);
|
|
|
|
else
|
|
|
|
return recvmsgs(sk, cb);
|
|
|
|
}
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/**
|
|
|
|
* Receive a set of messages from a netlink socket.
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg cb set of callbacks to control behaviour.
|
|
|
|
*
|
|
|
|
* Repeatedly calls nl_recv() or the respective replacement if provided
|
|
|
|
* by the application (see nl_cb_overwrite_recv()) and parses the
|
|
|
|
* received data as netlink messages. Stops reading if one of the
|
|
|
|
* callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
|
|
|
|
*
|
|
|
|
* A non-blocking sockets causes the function to return immediately if
|
|
|
|
* no data is available.
|
|
|
|
*
|
2012-05-08 20:48:00 +00:00
|
|
|
* @see nl_recvmsgs_report()
|
|
|
|
*
|
|
|
|
* @return 0 on success or a negative error code from nl_recv().
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2012-05-08 20:48:00 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = nl_recvmsgs_report(sk, cb)) > 0)
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
return err;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-15 11:26:32 +00:00
|
|
|
* Receive a set of message from a netlink socket using handlers in nl_sock.
|
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2008-05-15 11:26:32 +00:00
|
|
|
* Calls nl_recvmsgs() with the handlers configured in the netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_recvmsgs_default(struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
return nl_recvmsgs(sk, sk->s_cb);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ack_wait_handler(struct nl_msg *msg, void *arg)
|
|
|
|
{
|
|
|
|
return NL_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for ACK.
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
* @pre The netlink socket must be in blocking state.
|
|
|
|
*
|
|
|
|
* Waits until an ACK is received for the latest not yet acknowledged
|
|
|
|
* netlink message.
|
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_wait_for_ack(struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct nl_cb *cb;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
cb = nl_cb_clone(sk->s_cb);
|
2007-09-14 23:28:01 +00:00
|
|
|
if (cb == NULL)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -NLE_NOMEM;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
|
2008-05-15 11:26:32 +00:00
|
|
|
err = nl_recvmsgs(sk, cb);
|
2007-09-14 23:28:01 +00:00
|
|
|
nl_cb_put(cb);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-04-11 10:34:01 +00:00
|
|
|
/** @cond SKIP */
|
|
|
|
struct pickup_param
|
|
|
|
{
|
|
|
|
int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
|
|
|
|
struct nlmsghdr *, struct nl_parser_param *);
|
|
|
|
struct nl_object *result;
|
2014-11-26 17:13:37 +00:00
|
|
|
int *syserror;
|
2011-04-11 10:34:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __store_answer(struct nl_object *obj, struct nl_parser_param *p)
|
|
|
|
{
|
|
|
|
struct pickup_param *pp = p->pp_arg;
|
|
|
|
/*
|
|
|
|
* the parser will put() the object at the end, expecting the cache
|
|
|
|
* to take the reference.
|
|
|
|
*/
|
|
|
|
nl_object_get(obj);
|
|
|
|
pp->result = obj;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __pickup_answer(struct nl_msg *msg, void *arg)
|
|
|
|
{
|
|
|
|
struct pickup_param *pp = arg;
|
|
|
|
struct nl_parser_param parse_arg = {
|
|
|
|
.pp_cb = __store_answer,
|
|
|
|
.pp_arg = pp,
|
|
|
|
};
|
|
|
|
|
|
|
|
return pp->parser(NULL, &msg->nm_src, msg->nm_nlh, &parse_arg);
|
|
|
|
}
|
|
|
|
|
2014-11-26 17:13:37 +00:00
|
|
|
static int __pickup_answer_syserr(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
|
|
|
|
{
|
|
|
|
*(((struct pickup_param *) arg)->syserror) = nlerr->error;
|
|
|
|
|
|
|
|
return -nl_syserr2nlerr(nlerr->error);
|
|
|
|
}
|
|
|
|
|
2011-04-11 10:34:01 +00:00
|
|
|
/** @endcond */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pickup netlink answer, parse is and return object
|
2014-11-26 17:13:36 +00:00
|
|
|
* @arg sk Netlink socket
|
|
|
|
* @arg parser Parser function to parse answer
|
|
|
|
* @arg result Result pointer to return parsed object
|
2011-04-11 10:34:01 +00:00
|
|
|
*
|
|
|
|
* @return 0 on success or a negative error code.
|
|
|
|
*/
|
|
|
|
int nl_pickup(struct nl_sock *sk,
|
2014-11-26 17:13:36 +00:00
|
|
|
int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
|
|
|
|
struct nlmsghdr *, struct nl_parser_param *),
|
|
|
|
struct nl_object **result)
|
2014-11-26 17:13:37 +00:00
|
|
|
{
|
|
|
|
return nl_pickup_keep_syserr(sk, parser, result, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pickup netlink answer, parse is and return object with preserving system error
|
|
|
|
* @arg sk Netlink socket
|
|
|
|
* @arg parser Parser function to parse answer
|
|
|
|
* @arg result Result pointer to return parsed object
|
|
|
|
* @arg syserr Result pointer for the system error in case of failure
|
|
|
|
*
|
|
|
|
* @return 0 on success or a negative error code.
|
|
|
|
*/
|
|
|
|
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)
|
2011-04-11 10:34:01 +00:00
|
|
|
{
|
|
|
|
struct nl_cb *cb;
|
|
|
|
int err;
|
|
|
|
struct pickup_param pp = {
|
2014-11-26 17:13:36 +00:00
|
|
|
.parser = parser,
|
2011-04-11 10:34:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cb = nl_cb_clone(sk->s_cb);
|
|
|
|
if (cb == NULL)
|
|
|
|
return -NLE_NOMEM;
|
|
|
|
|
|
|
|
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __pickup_answer, &pp);
|
2014-11-26 17:13:37 +00:00
|
|
|
if (syserror) {
|
|
|
|
*syserror = 0;
|
|
|
|
pp.syserror = syserror;
|
|
|
|
nl_cb_err(cb, NL_CB_CUSTOM, __pickup_answer_syserr, &pp);
|
|
|
|
}
|
2011-04-11 10:34:01 +00:00
|
|
|
|
|
|
|
err = nl_recvmsgs(sk, cb);
|
|
|
|
if (err < 0)
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
*result = pp.result;
|
|
|
|
errout:
|
|
|
|
nl_cb_put(cb);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-11-08 00:29:12 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Deprecated
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Please use nl_complete_msg()
|
|
|
|
*/
|
|
|
|
void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
|
|
|
|
{
|
|
|
|
nl_complete_msg(sk, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Please use nl_send_auto()
|
|
|
|
*/
|
|
|
|
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
|
|
|
|
{
|
|
|
|
return nl_send_auto(sk, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/** @} */
|
2012-05-10 10:03:59 +00:00
|
|
|
|
|
|
|
/** @} */
|