2011-06-22 18:17:36 +00:00
|
|
|
/*
|
2016-01-11 06:59:00 +00:00
|
|
|
* Copyright (C) 2010-2016 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2010-2012, 2016 IBM Corporation
|
2011-06-22 18:17:36 +00:00
|
|
|
*
|
|
|
|
* 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; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2011-06-22 18:17:36 +00:00
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* netlink: http://lovezutto.googlepages.com/netlink.pdf
|
|
|
|
* iproute2 package
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-02-03 14:13:19 +00:00
|
|
|
#include "virnetlink.h"
|
2016-06-13 11:59:12 +00:00
|
|
|
#include "virnetdev.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 15:49:48 +00:00
|
|
|
#include "virthread.h"
|
2012-02-22 13:17:13 +00:00
|
|
|
#include "virmacaddr.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2020-01-14 17:38:59 +00:00
|
|
|
#include "virsocket.h"
|
2011-06-22 18:17:36 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NET
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.netlink");
|
|
|
|
|
2013-05-13 13:43:20 +00:00
|
|
|
#define NETLINK_ACK_TIMEOUT_S (2*1000)
|
2011-06-22 18:17:36 +00:00
|
|
|
|
2020-09-28 21:29:41 +00:00
|
|
|
#if defined(WITH_LIBNL)
|
2020-12-16 06:01:04 +00:00
|
|
|
|
|
|
|
# include <linux/veth.h>
|
|
|
|
|
2021-01-11 02:23:35 +00:00
|
|
|
# define NETLINK_MSG_NEST_START(msg, container, attrtype) \
|
|
|
|
do { \
|
|
|
|
container = nla_nest_start(msg, attrtype); \
|
|
|
|
if (!container) { \
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
|
|
|
|
_("allocated netlink buffer is too small")); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
# define NETLINK_MSG_NEST_END(msg, container) \
|
|
|
|
do { nla_nest_end(msg, container); } while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we need to use an intermediary pointer to @data as compilers may sometimes
|
|
|
|
* complain about @data not being a pointer type:
|
|
|
|
* error: the address of 'foo' will always evaluate as 'true' [-Werror=address]
|
|
|
|
*/
|
|
|
|
# define NETLINK_MSG_PUT(msg, attrtype, datalen, data) \
|
|
|
|
do { \
|
|
|
|
const void *dataptr = data; \
|
|
|
|
if (dataptr && nla_put(msg, attrtype, datalen, dataptr) < 0) { \
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
|
|
|
|
_("allocated netlink buffer is too small")); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2021-01-11 02:23:36 +00:00
|
|
|
# define NETLINK_MSG_APPEND(msg, datalen, dataptr) \
|
|
|
|
do { \
|
|
|
|
if (nlmsg_append(msg, dataptr, datalen, NLMSG_ALIGNTO) < 0) { \
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
|
|
|
|
_("allocated netlink buffer is too small")); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2021-01-11 02:23:35 +00:00
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
/* State for a single netlink event handle */
|
|
|
|
struct virNetlinkEventHandle {
|
|
|
|
int watch;
|
|
|
|
virNetlinkEventHandleCallback handleCB;
|
|
|
|
virNetlinkEventRemoveCallback removeCB;
|
|
|
|
void *opaque;
|
2012-07-17 12:07:59 +00:00
|
|
|
virMacAddr macaddr;
|
2012-02-22 13:17:13 +00:00
|
|
|
int deleted;
|
|
|
|
};
|
|
|
|
|
2019-10-09 12:36:32 +00:00
|
|
|
# define virNetlinkAlloc nl_socket_alloc
|
|
|
|
# define virNetlinkSetBufferSize nl_socket_set_buffer_size
|
|
|
|
# define virNetlinkFree nl_socket_free
|
2012-05-03 16:10:50 +00:00
|
|
|
typedef struct nl_sock virNetlinkHandle;
|
|
|
|
|
2019-10-15 12:47:50 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetlinkHandle, virNetlinkFree);
|
2018-07-28 18:01:37 +00:00
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
typedef struct _virNetlinkEventSrvPrivate virNetlinkEventSrvPrivate;
|
|
|
|
typedef virNetlinkEventSrvPrivate *virNetlinkEventSrvPrivatePtr;
|
|
|
|
struct _virNetlinkEventSrvPrivate {
|
2020-01-24 20:30:04 +00:00
|
|
|
/* Server */
|
2012-02-22 13:17:13 +00:00
|
|
|
virMutex lock;
|
|
|
|
int eventwatch;
|
|
|
|
int netlinkfd;
|
2012-05-03 16:10:50 +00:00
|
|
|
virNetlinkHandle *netlinknh;
|
2020-01-24 20:30:04 +00:00
|
|
|
/* Events */
|
2012-02-22 13:17:13 +00:00
|
|
|
int handled;
|
|
|
|
size_t handlesCount;
|
|
|
|
size_t handlesAlloc;
|
|
|
|
struct virNetlinkEventHandle *handles;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum virNetlinkDeleteMode {
|
|
|
|
VIR_NETLINK_HANDLE_VALID,
|
|
|
|
VIR_NETLINK_HANDLE_DELETED,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Unique ID for the next netlink watch to be registered */
|
|
|
|
static int nextWatch = 1;
|
|
|
|
|
|
|
|
/* Allocate extra slots for virEventPollHandle/virEventPollTimeout
|
|
|
|
records in this multiple */
|
|
|
|
# define NETLINK_EVENT_ALLOC_EXTENT 10
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
/* Linux kernel supports up to MAX_LINKS (32 at the time) individual
|
|
|
|
* netlink protocols. */
|
|
|
|
static virNetlinkEventSrvPrivatePtr server[MAX_LINKS] = {NULL};
|
2014-10-28 18:38:04 +00:00
|
|
|
static virNetlinkHandle *placeholder_nlhandle;
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
/* Function definitions */
|
|
|
|
|
2021-02-24 08:51:19 +00:00
|
|
|
struct nl_msg *
|
|
|
|
virNetlinkMsgNew(int nlmsgtype,
|
|
|
|
int nlmsgflags)
|
|
|
|
{
|
|
|
|
struct nl_msg *ret;
|
|
|
|
|
|
|
|
if (!(ret = nlmsg_alloc_simple(nlmsgtype, nlmsgflags)))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-03 14:39:04 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkStartup:
|
|
|
|
*
|
|
|
|
* Perform any initialization that needs to take place before the
|
|
|
|
* program starts up worker threads. This is currently used to assure
|
|
|
|
* that an nl_handle is allocated prior to any attempts to bind a
|
|
|
|
* netlink socket. For a discussion of why this is necessary, please
|
|
|
|
* see the following email message:
|
|
|
|
*
|
|
|
|
* https://www.redhat.com/archives/libvir-list/2012-May/msg00202.html
|
|
|
|
*
|
|
|
|
* The short version is that, without this placeholder allocation of
|
|
|
|
* an nl_handle that is never used, it is possible for nl_connect() in
|
|
|
|
* one thread to collide with a direct bind() of a netlink socket in
|
|
|
|
* another thread, leading to failure of the operation (which could
|
|
|
|
* lead to failure of libvirtd to start). Since getaddrinfo() (used by
|
|
|
|
* libvirtd in virSocketAddrParse, which is called quite frequently
|
|
|
|
* during startup) directly calls bind() on a netlink socket, this is
|
|
|
|
* actually a very common occurrence (15-20% failure rate on some
|
|
|
|
* hardware).
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virNetlinkStartup(void)
|
|
|
|
{
|
|
|
|
if (placeholder_nlhandle)
|
|
|
|
return 0;
|
2012-06-15 12:48:34 +00:00
|
|
|
VIR_DEBUG("Running global netlink initialization");
|
2012-05-09 09:57:44 +00:00
|
|
|
placeholder_nlhandle = virNetlinkAlloc();
|
2012-05-03 14:39:04 +00:00
|
|
|
if (!placeholder_nlhandle) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot allocate placeholder nlhandle for netlink"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkShutdown:
|
|
|
|
*
|
|
|
|
* Undo any initialization done by virNetlinkStartup. This currently
|
|
|
|
* destroys the placeholder nl_handle.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virNetlinkShutdown(void)
|
|
|
|
{
|
|
|
|
if (placeholder_nlhandle) {
|
2012-05-09 09:57:44 +00:00
|
|
|
virNetlinkFree(placeholder_nlhandle);
|
2012-05-03 14:39:04 +00:00
|
|
|
placeholder_nlhandle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 06:59:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetLinkCreateSocket:
|
|
|
|
*
|
|
|
|
* @protocol: which protocol to connect to (e.g. NETLINK_ROUTE,
|
|
|
|
*
|
|
|
|
* Create a netlink socket, set its buffer size, and turn on message
|
|
|
|
* peeking (so the buffer size can be dynamically increased if
|
|
|
|
* needed).
|
|
|
|
*
|
|
|
|
* Returns a handle to the new netlink socket, or 0 if there was a failure.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static virNetlinkHandle *
|
|
|
|
virNetlinkCreateSocket(int protocol)
|
|
|
|
{
|
|
|
|
virNetlinkHandle *nlhandle = NULL;
|
|
|
|
|
|
|
|
if (!(nlhandle = virNetlinkAlloc())) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot allocate nlhandle for netlink"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (nl_connect(nlhandle, protocol) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot connect to netlink socket "
|
|
|
|
"with protocol %d"), protocol);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virNetlinkSetBufferSize(nlhandle, 131702, 0) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot set netlink socket buffer "
|
|
|
|
"size to 128k"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
nl_socket_enable_msg_peek(nlhandle);
|
|
|
|
|
|
|
|
return nlhandle;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (nlhandle) {
|
|
|
|
nl_close(nlhandle);
|
|
|
|
virNetlinkFree(nlhandle);
|
|
|
|
}
|
2020-01-06 21:57:45 +00:00
|
|
|
return NULL;
|
2016-01-11 06:59:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 11:16:32 +00:00
|
|
|
static virNetlinkHandle *
|
|
|
|
virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t src_pid,
|
|
|
|
struct sockaddr_nl nladdr,
|
2012-08-22 04:10:23 +00:00
|
|
|
unsigned int protocol, unsigned int groups)
|
2011-06-22 18:17:36 +00:00
|
|
|
{
|
|
|
|
ssize_t nbytes;
|
|
|
|
int fd;
|
|
|
|
int n;
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkHandle *nlhandle = NULL;
|
2017-03-03 11:16:32 +00:00
|
|
|
struct pollfd fds[1];
|
|
|
|
struct nlmsghdr *nlmsg = nlmsg_hdr(nl_msg);
|
2012-08-22 04:10:23 +00:00
|
|
|
|
|
|
|
if (protocol >= MAX_LINKS) {
|
|
|
|
virReportSystemError(EINVAL,
|
|
|
|
_("invalid protocol argument: %d"), protocol);
|
2017-03-03 11:16:32 +00:00
|
|
|
goto error;
|
2012-08-22 04:10:23 +00:00
|
|
|
}
|
2011-06-22 18:17:36 +00:00
|
|
|
|
2016-01-11 06:59:00 +00:00
|
|
|
if (!(nlhandle = virNetlinkCreateSocket(protocol)))
|
2017-03-03 11:16:32 +00:00
|
|
|
goto error;
|
2012-08-22 04:10:23 +00:00
|
|
|
|
|
|
|
fd = nl_socket_get_fd(nlhandle);
|
|
|
|
if (fd < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s", _("cannot get netlink socket fd"));
|
2017-03-03 11:16:32 +00:00
|
|
|
goto error;
|
2012-08-22 04:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (groups && nl_socket_add_membership(nlhandle, groups) < 0) {
|
2011-06-22 18:17:36 +00:00
|
|
|
virReportSystemError(errno,
|
2012-08-22 04:10:23 +00:00
|
|
|
"%s", _("cannot add netlink membership"));
|
2017-03-03 11:16:32 +00:00
|
|
|
goto error;
|
2011-06-22 18:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nlmsg_set_dst(nl_msg, &nladdr);
|
|
|
|
|
2012-05-04 16:58:36 +00:00
|
|
|
nlmsg->nlmsg_pid = src_pid ? src_pid : getpid();
|
2011-06-22 18:17:36 +00:00
|
|
|
|
|
|
|
nbytes = nl_send_auto_complete(nlhandle, nl_msg);
|
|
|
|
if (nbytes < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s", _("cannot send to netlink socket"));
|
2017-03-03 11:16:32 +00:00
|
|
|
goto error;
|
2011-06-22 18:17:36 +00:00
|
|
|
}
|
|
|
|
|
2013-05-13 13:43:20 +00:00
|
|
|
memset(fds, 0, sizeof(fds));
|
2017-03-03 11:16:32 +00:00
|
|
|
|
2013-05-13 13:43:20 +00:00
|
|
|
fds[0].fd = fd;
|
|
|
|
fds[0].events = POLLIN;
|
2011-06-22 18:17:36 +00:00
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
n = poll(fds, G_N_ELEMENTS(fds), NETLINK_ACK_TIMEOUT_S);
|
2011-06-22 18:17:36 +00:00
|
|
|
if (n <= 0) {
|
|
|
|
if (n < 0)
|
|
|
|
virReportSystemError(errno, "%s",
|
2013-05-13 13:43:20 +00:00
|
|
|
_("error in poll call"));
|
2011-06-22 18:17:36 +00:00
|
|
|
if (n == 0)
|
|
|
|
virReportSystemError(ETIMEDOUT, "%s",
|
|
|
|
_("no valid netlink response was received"));
|
|
|
|
}
|
|
|
|
|
2017-03-03 11:16:32 +00:00
|
|
|
return nlhandle;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virNetlinkFree(nlhandle);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkCommand:
|
2018-02-06 11:38:17 +00:00
|
|
|
* @nl_msg: pointer to netlink message
|
|
|
|
* @resp: pointer to pointer where response buffer will be allocated
|
2017-03-03 11:16:32 +00:00
|
|
|
* @respbuflen: pointer to integer holding the size of the response buffer
|
2018-02-06 11:38:17 +00:00
|
|
|
* on return of the function.
|
|
|
|
* @src_pid: the pid of the process to send a message
|
|
|
|
* @dst_pid: the pid of the process to talk to, i.e., pid = 0 for kernel
|
|
|
|
* @protocol: netlink protocol
|
|
|
|
* @groups: the group identifier
|
2017-03-03 11:16:32 +00:00
|
|
|
*
|
|
|
|
* Send the given message to the netlink layer and receive response.
|
|
|
|
* Returns 0 on success, -1 on error. In case of error, no response
|
|
|
|
* buffer will be returned.
|
|
|
|
*/
|
|
|
|
int virNetlinkCommand(struct nl_msg *nl_msg,
|
|
|
|
struct nlmsghdr **resp, unsigned int *respbuflen,
|
|
|
|
uint32_t src_pid, uint32_t dst_pid,
|
|
|
|
unsigned int protocol, unsigned int groups)
|
|
|
|
{
|
|
|
|
struct sockaddr_nl nladdr = {
|
|
|
|
.nl_family = AF_NETLINK,
|
|
|
|
.nl_pid = dst_pid,
|
|
|
|
.nl_groups = 0,
|
|
|
|
};
|
|
|
|
struct pollfd fds[1];
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree struct nlmsghdr *temp_resp = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virNetlinkHandle) nlhandle = NULL;
|
2017-03-03 11:16:32 +00:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
memset(fds, 0, sizeof(fds));
|
|
|
|
|
|
|
|
if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
|
|
|
|
protocol, groups)))
|
2018-08-09 04:12:11 +00:00
|
|
|
return -1;
|
2017-03-03 11:16:32 +00:00
|
|
|
|
2018-08-09 04:12:11 +00:00
|
|
|
len = nl_recv(nlhandle, &nladdr, (unsigned char **)&temp_resp, NULL);
|
util: refactor virNetlinkCommand to fix several bugs / style problems
Inspired by a simpler patch from "Wangrui (K) <moon.wangrui@huawei.com>".
A submitted patch pointed out that virNetlinkCommand() was doing an
improper typecast of the return value from nl_recv() (int to
unsigned), causing it to miss error returns, and that even after
remedying that problem, virNetlinkCommand() was calling VIR_FREE() on
the pointer returned from nl_recv() (*resp) even if nl_recv() had
returned an error, and that in this case the pointer was verifiably
invalid, as it was pointing to memory that had been allocated by
libnl, but then freed prior to returning the error.
While reviewing this patch, I noticed several other problems with this
seemingly simple function (at least one of them as serious as the
problem being reported/fixed by the aforementioned patch), and decided
they all deserved to be fixed. Here is the list:
1) The return value from nl_recv() must be assigned to an int (rather
than unsigned int) in order to detect failure.
2) When nl_recv() returns an error or 0, the contents of *resp is
invalid, and should be simply set to 0, *not* VIR_FREE()'d.
3) When nl_recv() returns 0, errno is not set, so the logged error
message should not reference errno (it *is* an error though).
4) The first error return from virNetlinkCommand returns -EINVAL,
incorrectly implying that the caller can expect the return value to
be of the "-errno" variety, which is not true in any other case.
5) The 2nd error return returns directly with garbage in *resp. While
the caller should never use *resp in this case, it's still good
practice to set it to NULL.
6) For the next 5 (!!) error conditions, *resp will contain garbage,
and virNetlinkCommand() will goto it's cleanup code which will
VIR_FREE(*resp), almost surely leading to a segfault.
In addition to fixing these 6 problems, this patch also makes the
following two changes to make the function conform more closely to the
style of other libvirt code:
1) Change the handling of return code from "named rc and defaulted to
0, but changed to -1 on error" to the more common "named ret and
defaulted to -1, but changed to 0 on success".
2) Rename the "error" label to "cleanup", since the code that follows
is executed in success cases as well as failure.
2014-05-13 11:34:43 +00:00
|
|
|
if (len == 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("nl_recv failed - returned 0 bytes"));
|
2018-08-09 04:12:11 +00:00
|
|
|
return -1;
|
2011-06-22 18:17:36 +00:00
|
|
|
}
|
util: refactor virNetlinkCommand to fix several bugs / style problems
Inspired by a simpler patch from "Wangrui (K) <moon.wangrui@huawei.com>".
A submitted patch pointed out that virNetlinkCommand() was doing an
improper typecast of the return value from nl_recv() (int to
unsigned), causing it to miss error returns, and that even after
remedying that problem, virNetlinkCommand() was calling VIR_FREE() on
the pointer returned from nl_recv() (*resp) even if nl_recv() had
returned an error, and that in this case the pointer was verifiably
invalid, as it was pointing to memory that had been allocated by
libnl, but then freed prior to returning the error.
While reviewing this patch, I noticed several other problems with this
seemingly simple function (at least one of them as serious as the
problem being reported/fixed by the aforementioned patch), and decided
they all deserved to be fixed. Here is the list:
1) The return value from nl_recv() must be assigned to an int (rather
than unsigned int) in order to detect failure.
2) When nl_recv() returns an error or 0, the contents of *resp is
invalid, and should be simply set to 0, *not* VIR_FREE()'d.
3) When nl_recv() returns 0, errno is not set, so the logged error
message should not reference errno (it *is* an error though).
4) The first error return from virNetlinkCommand returns -EINVAL,
incorrectly implying that the caller can expect the return value to
be of the "-errno" variety, which is not true in any other case.
5) The 2nd error return returns directly with garbage in *resp. While
the caller should never use *resp in this case, it's still good
practice to set it to NULL.
6) For the next 5 (!!) error conditions, *resp will contain garbage,
and virNetlinkCommand() will goto it's cleanup code which will
VIR_FREE(*resp), almost surely leading to a segfault.
In addition to fixing these 6 problems, this patch also makes the
following two changes to make the function conform more closely to the
style of other libvirt code:
1) Change the handling of return code from "named rc and defaulted to
0, but changed to -1 on error" to the more common "named ret and
defaulted to -1, but changed to 0 on success".
2) Rename the "error" label to "cleanup", since the code that follows
is executed in success cases as well as failure.
2014-05-13 11:34:43 +00:00
|
|
|
if (len < 0) {
|
|
|
|
virReportSystemError(errno, "%s", _("nl_recv failed"));
|
2018-08-09 04:12:11 +00:00
|
|
|
return -1;
|
util: refactor virNetlinkCommand to fix several bugs / style problems
Inspired by a simpler patch from "Wangrui (K) <moon.wangrui@huawei.com>".
A submitted patch pointed out that virNetlinkCommand() was doing an
improper typecast of the return value from nl_recv() (int to
unsigned), causing it to miss error returns, and that even after
remedying that problem, virNetlinkCommand() was calling VIR_FREE() on
the pointer returned from nl_recv() (*resp) even if nl_recv() had
returned an error, and that in this case the pointer was verifiably
invalid, as it was pointing to memory that had been allocated by
libnl, but then freed prior to returning the error.
While reviewing this patch, I noticed several other problems with this
seemingly simple function (at least one of them as serious as the
problem being reported/fixed by the aforementioned patch), and decided
they all deserved to be fixed. Here is the list:
1) The return value from nl_recv() must be assigned to an int (rather
than unsigned int) in order to detect failure.
2) When nl_recv() returns an error or 0, the contents of *resp is
invalid, and should be simply set to 0, *not* VIR_FREE()'d.
3) When nl_recv() returns 0, errno is not set, so the logged error
message should not reference errno (it *is* an error though).
4) The first error return from virNetlinkCommand returns -EINVAL,
incorrectly implying that the caller can expect the return value to
be of the "-errno" variety, which is not true in any other case.
5) The 2nd error return returns directly with garbage in *resp. While
the caller should never use *resp in this case, it's still good
practice to set it to NULL.
6) For the next 5 (!!) error conditions, *resp will contain garbage,
and virNetlinkCommand() will goto it's cleanup code which will
VIR_FREE(*resp), almost surely leading to a segfault.
In addition to fixing these 6 problems, this patch also makes the
following two changes to make the function conform more closely to the
style of other libvirt code:
1) Change the handling of return code from "named rc and defaulted to
0, but changed to -1 on error" to the more common "named ret and
defaulted to -1, but changed to 0 on success".
2) Rename the "error" label to "cleanup", since the code that follows
is executed in success cases as well as failure.
2014-05-13 11:34:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*resp = g_steal_pointer(&temp_resp);
|
util: refactor virNetlinkCommand to fix several bugs / style problems
Inspired by a simpler patch from "Wangrui (K) <moon.wangrui@huawei.com>".
A submitted patch pointed out that virNetlinkCommand() was doing an
improper typecast of the return value from nl_recv() (int to
unsigned), causing it to miss error returns, and that even after
remedying that problem, virNetlinkCommand() was calling VIR_FREE() on
the pointer returned from nl_recv() (*resp) even if nl_recv() had
returned an error, and that in this case the pointer was verifiably
invalid, as it was pointing to memory that had been allocated by
libnl, but then freed prior to returning the error.
While reviewing this patch, I noticed several other problems with this
seemingly simple function (at least one of them as serious as the
problem being reported/fixed by the aforementioned patch), and decided
they all deserved to be fixed. Here is the list:
1) The return value from nl_recv() must be assigned to an int (rather
than unsigned int) in order to detect failure.
2) When nl_recv() returns an error or 0, the contents of *resp is
invalid, and should be simply set to 0, *not* VIR_FREE()'d.
3) When nl_recv() returns 0, errno is not set, so the logged error
message should not reference errno (it *is* an error though).
4) The first error return from virNetlinkCommand returns -EINVAL,
incorrectly implying that the caller can expect the return value to
be of the "-errno" variety, which is not true in any other case.
5) The 2nd error return returns directly with garbage in *resp. While
the caller should never use *resp in this case, it's still good
practice to set it to NULL.
6) For the next 5 (!!) error conditions, *resp will contain garbage,
and virNetlinkCommand() will goto it's cleanup code which will
VIR_FREE(*resp), almost surely leading to a segfault.
In addition to fixing these 6 problems, this patch also makes the
following two changes to make the function conform more closely to the
style of other libvirt code:
1) Change the handling of return code from "named rc and defaulted to
0, but changed to -1 on error" to the more common "named ret and
defaulted to -1, but changed to 0 on success".
2) Rename the "error" label to "cleanup", since the code that follows
is executed in success cases as well as failure.
2014-05-13 11:34:43 +00:00
|
|
|
*respbuflen = len;
|
2018-08-09 04:12:11 +00:00
|
|
|
return 0;
|
2011-06-22 18:17:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkTalk:
|
|
|
|
* @ifname: name of the link
|
|
|
|
* @nl_msg: pointer to netlink message
|
|
|
|
* @src_pid: pid used for nl_pid of the local end of the netlink message
|
|
|
|
* (0 == "use getpid()")
|
|
|
|
* @dst_pid: pid of destination nl_pid if the kernel
|
|
|
|
* is not the target of the netlink message but it is to be
|
|
|
|
* sent to another process (0 if sending to the kernel)
|
|
|
|
* @resp: pointer to pointer where response buffer will be allocated
|
|
|
|
* @resp_len: pointer to integer holding the size of the response buffer
|
|
|
|
* on return of the function
|
|
|
|
* @error: pointer to store netlink error (-errno)
|
|
|
|
* @fallback: pointer to an alternate function that will be called in case
|
|
|
|
* netlink fails with EOPNOTSUPP (any other error will simply be
|
|
|
|
* treated as an error)
|
|
|
|
*
|
|
|
|
* Simple wrapper around virNetlinkCommand(). The returned netlink message
|
|
|
|
* is allocated at @resp. Please note that according to netlink(7) man page,
|
|
|
|
* reply with type of NLMSG_ERROR and @error == 0 is an acknowledgment and
|
|
|
|
* thus not an error.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
|
|
|
* -1 otherwise (error reported if @error == NULL)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virNetlinkTalk(const char *ifname,
|
|
|
|
virNetlinkMsg *nl_msg,
|
|
|
|
uint32_t src_pid,
|
|
|
|
uint32_t dst_pid,
|
|
|
|
struct nlmsghdr **resp,
|
|
|
|
unsigned int *resp_len,
|
|
|
|
int *error,
|
|
|
|
virNetlinkTalkFallback fallback)
|
|
|
|
{
|
|
|
|
if (virNetlinkCommand(nl_msg, resp, resp_len,
|
|
|
|
src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (*resp_len < NLMSG_LENGTH(0) || *resp == NULL)
|
|
|
|
goto malformed_resp;
|
|
|
|
|
|
|
|
if ((*resp)->nlmsg_type == NLMSG_ERROR) {
|
|
|
|
struct nlmsgerr *err;
|
|
|
|
|
|
|
|
err = (struct nlmsgerr *) NLMSG_DATA(*resp);
|
|
|
|
|
|
|
|
if ((*resp)->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
|
|
|
|
goto malformed_resp;
|
|
|
|
|
|
|
|
if (-err->error == EOPNOTSUPP && fallback)
|
|
|
|
return fallback(ifname);
|
|
|
|
|
|
|
|
if (err->error < 0) {
|
|
|
|
if (error)
|
|
|
|
*error = err->error;
|
|
|
|
else
|
|
|
|
virReportSystemError(-err->error, "%s", _("netlink error"));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* According to netlink(7) man page NLMSG_ERROR packet with error
|
|
|
|
* field set to 0 is an acknowledgment packet and thus not an error. */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
malformed_resp:
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed netlink response message"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-03 11:22:50 +00:00
|
|
|
int
|
|
|
|
virNetlinkDumpCommand(struct nl_msg *nl_msg,
|
|
|
|
virNetlinkDumpCallback callback,
|
|
|
|
uint32_t src_pid, uint32_t dst_pid,
|
|
|
|
unsigned int protocol, unsigned int groups,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
bool end = false;
|
|
|
|
int len = 0;
|
|
|
|
struct nlmsghdr *msg = NULL;
|
|
|
|
|
|
|
|
struct sockaddr_nl nladdr = {
|
|
|
|
.nl_family = AF_NETLINK,
|
|
|
|
.nl_pid = dst_pid,
|
|
|
|
.nl_groups = 0,
|
|
|
|
};
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virNetlinkHandle) nlhandle = NULL;
|
2017-03-03 11:22:50 +00:00
|
|
|
|
|
|
|
if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
|
|
|
|
protocol, groups)))
|
2018-08-09 04:12:11 +00:00
|
|
|
return -1;
|
2017-03-03 11:22:50 +00:00
|
|
|
|
|
|
|
while (!end) {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree struct nlmsghdr *resp = NULL;
|
2018-08-09 04:12:11 +00:00
|
|
|
|
2017-03-03 11:22:50 +00:00
|
|
|
len = nl_recv(nlhandle, &nladdr, (unsigned char **)&resp, NULL);
|
2017-04-04 10:51:47 +00:00
|
|
|
VIR_WARNINGS_NO_CAST_ALIGN
|
2017-03-03 11:22:50 +00:00
|
|
|
for (msg = resp; NLMSG_OK(msg, len); msg = NLMSG_NEXT(msg, len)) {
|
2017-04-04 10:51:47 +00:00
|
|
|
VIR_WARNINGS_RESET
|
2017-03-03 11:22:50 +00:00
|
|
|
if (msg->nlmsg_type == NLMSG_DONE)
|
|
|
|
end = true;
|
|
|
|
|
|
|
|
if (virNetlinkGetErrorCode(msg, len) < 0)
|
2018-08-09 04:12:11 +00:00
|
|
|
return -1;
|
2017-03-03 11:22:50 +00:00
|
|
|
|
|
|
|
if (callback(msg, opaque) < 0)
|
2018-08-09 04:12:11 +00:00
|
|
|
return -1;
|
2017-03-03 11:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 04:12:11 +00:00
|
|
|
return 0;
|
2017-03-03 11:22:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
|
2016-06-13 11:59:12 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkDumpLink:
|
|
|
|
*
|
|
|
|
* @ifname: The name of the interface; only use if ifindex <= 0
|
|
|
|
* @ifindex: The interface index; may be <= 0 if ifname is given
|
2018-02-06 11:38:17 +00:00
|
|
|
* @nlData: Gets a pointer to the raw data from netlink.
|
2016-06-13 11:59:12 +00:00
|
|
|
MUST BE FREED BY CALLER!
|
2018-02-06 11:38:17 +00:00
|
|
|
* @tb: Pointer to a pointer of netlink attributes that will contain
|
2016-06-13 11:59:12 +00:00
|
|
|
* the results
|
|
|
|
* @src_pid: pid used for nl_pid of the local end of the netlink message
|
|
|
|
* (0 == "use getpid()")
|
|
|
|
* @dst_pid: pid of destination nl_pid if the kernel
|
|
|
|
* is not the target of the netlink message but it is to be
|
|
|
|
* sent to another process (0 if sending to the kernel)
|
|
|
|
*
|
|
|
|
* Get information from netlink about an interface given its name or index.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on fatal error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virNetlinkDumpLink(const char *ifname, int ifindex,
|
|
|
|
void **nlData, struct nlattr **tb,
|
|
|
|
uint32_t src_pid, uint32_t dst_pid)
|
|
|
|
{
|
|
|
|
struct ifinfomsg ifinfo = {
|
|
|
|
.ifi_family = AF_UNSPEC,
|
|
|
|
.ifi_index = ifindex
|
|
|
|
};
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virNetlinkMsg) nl_msg = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree struct nlmsghdr *resp = NULL;
|
2021-01-11 02:23:37 +00:00
|
|
|
unsigned int resp_len = 0;
|
|
|
|
int error = 0;
|
2016-06-13 11:59:12 +00:00
|
|
|
|
|
|
|
if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ifinfo.ifi_index = ifindex;
|
|
|
|
|
2021-02-24 08:51:19 +00:00
|
|
|
nl_msg = virNetlinkMsgNew(RTM_GETLINK, NLM_F_REQUEST);
|
2016-06-13 11:59:12 +00:00
|
|
|
|
2021-01-11 02:23:36 +00:00
|
|
|
NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo);
|
2016-06-13 11:59:12 +00:00
|
|
|
|
2021-01-11 02:23:35 +00:00
|
|
|
if (ifname)
|
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname);
|
2016-06-13 11:59:12 +00:00
|
|
|
|
|
|
|
# ifdef RTEXT_FILTER_VF
|
|
|
|
/* if this filter exists in the kernel's netlink implementation,
|
|
|
|
* we need to set it, otherwise the response message will not
|
|
|
|
* contain the IFLA_VFINFO_LIST that we're looking for.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
uint32_t ifla_ext_mask = RTEXT_FILTER_VF;
|
|
|
|
|
2021-01-11 02:23:35 +00:00
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_EXT_MASK,
|
|
|
|
sizeof(ifla_ext_mask), &ifla_ext_mask);
|
2016-06-13 11:59:12 +00:00
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
if (virNetlinkTalk(ifname, nl_msg, src_pid, dst_pid,
|
|
|
|
&resp, &resp_len, &error, NULL) < 0) {
|
|
|
|
virReportSystemError(-error,
|
|
|
|
_("error dumping %s (%d) interface"),
|
|
|
|
ifname, ifindex);
|
2018-08-09 04:12:12 +00:00
|
|
|
return -1;
|
2021-01-11 02:23:37 +00:00
|
|
|
}
|
2016-06-13 11:59:12 +00:00
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
if ((resp->nlmsg_type != NLMSG_ERROR &&
|
|
|
|
resp->nlmsg_type != GENL_ID_CTRL &&
|
|
|
|
resp->nlmsg_type != NLMSG_DONE) ||
|
|
|
|
nlmsg_parse(resp, sizeof(struct ifinfomsg), tb, IFLA_MAX, NULL) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed netlink response message"));
|
|
|
|
return -1;
|
2016-06-13 11:59:12 +00:00
|
|
|
}
|
2018-08-09 04:12:11 +00:00
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*nlData = g_steal_pointer(&resp);
|
2018-08-09 04:12:12 +00:00
|
|
|
return 0;
|
2016-06-13 11:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-07 07:17:24 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkNewLink:
|
|
|
|
*
|
|
|
|
* @ifname: name of the link
|
|
|
|
* @type: the type of the device, i.e. "bridge", "macvtap", "macvlan"
|
|
|
|
* @extra_args: the extra args for creating the netlink interface
|
|
|
|
* @error: netlink error code
|
|
|
|
*
|
|
|
|
* A generic wrapper to create a network link.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error. Additionally, if the @error is
|
|
|
|
* non-zero, then a failure occurred during virNetlinkCommand, but
|
|
|
|
* no error message is generated leaving it up to the caller to handle
|
|
|
|
* the condition.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virNetlinkNewLink(const char *ifname,
|
|
|
|
const char *type,
|
|
|
|
virNetlinkNewLinkDataPtr extra_args,
|
|
|
|
int *error)
|
|
|
|
{
|
|
|
|
struct nlattr *linkinfo = NULL;
|
|
|
|
struct nlattr *infodata = NULL;
|
|
|
|
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virNetlinkMsg) nl_msg = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree struct nlmsghdr *resp = NULL;
|
2021-01-11 02:23:37 +00:00
|
|
|
unsigned int resp_len = 0;
|
2018-09-07 07:17:24 +00:00
|
|
|
|
|
|
|
*error = 0;
|
|
|
|
|
|
|
|
VIR_DEBUG("Creating %s interface '%s'", type, ifname);
|
|
|
|
|
|
|
|
if (!ifname || !type) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
|
|
|
_("both interface name and type must not be NULL"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-02-24 08:51:19 +00:00
|
|
|
nl_msg = virNetlinkMsgNew(RTM_NEWLINK,
|
|
|
|
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
|
2018-09-07 07:17:24 +00:00
|
|
|
|
2021-01-11 02:23:36 +00:00
|
|
|
NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo);
|
2018-09-07 07:17:24 +00:00
|
|
|
|
2018-09-07 07:17:25 +00:00
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname);
|
2018-09-07 07:17:24 +00:00
|
|
|
|
2018-09-07 07:17:25 +00:00
|
|
|
NETLINK_MSG_NEST_START(nl_msg, linkinfo, IFLA_LINKINFO);
|
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_INFO_KIND, (strlen(type) + 1), type);
|
2018-09-07 07:17:24 +00:00
|
|
|
|
|
|
|
if ((STREQ(type, "macvtap") || STREQ(type, "macvlan")) &&
|
2018-09-19 08:38:14 +00:00
|
|
|
extra_args &&
|
|
|
|
extra_args->macvlan_mode &&
|
|
|
|
*extra_args->macvlan_mode > 0) {
|
2018-09-07 07:17:25 +00:00
|
|
|
NETLINK_MSG_NEST_START(nl_msg, infodata, IFLA_INFO_DATA);
|
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_MACVLAN_MODE,
|
|
|
|
sizeof(uint32_t), extra_args->macvlan_mode);
|
|
|
|
NETLINK_MSG_NEST_END(nl_msg, infodata);
|
2018-09-07 07:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 06:01:04 +00:00
|
|
|
if (STREQ(type, "veth") && extra_args && extra_args->veth_peer) {
|
|
|
|
struct nlattr *infoveth = NULL;
|
|
|
|
|
|
|
|
NETLINK_MSG_NEST_START(nl_msg, infodata, IFLA_INFO_DATA);
|
|
|
|
NETLINK_MSG_NEST_START(nl_msg, infoveth, VETH_INFO_PEER);
|
|
|
|
nlmsg_reserve(nl_msg, sizeof(struct ifinfomsg), 0);
|
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME,
|
|
|
|
(strlen(extra_args->veth_peer) + 1),
|
|
|
|
extra_args->veth_peer);
|
|
|
|
NETLINK_MSG_NEST_END(nl_msg, infoveth);
|
|
|
|
NETLINK_MSG_NEST_END(nl_msg, infodata);
|
|
|
|
}
|
|
|
|
|
2018-09-07 07:17:25 +00:00
|
|
|
NETLINK_MSG_NEST_END(nl_msg, linkinfo);
|
2018-09-07 07:17:24 +00:00
|
|
|
|
|
|
|
if (extra_args) {
|
2019-10-16 18:06:54 +00:00
|
|
|
if (extra_args->ifindex) {
|
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_LINK,
|
2018-09-07 07:17:25 +00:00
|
|
|
sizeof(uint32_t), extra_args->ifindex);
|
2019-10-16 18:06:54 +00:00
|
|
|
}
|
|
|
|
if (extra_args->mac) {
|
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_ADDRESS,
|
|
|
|
VIR_MAC_BUFLEN, extra_args->mac);
|
|
|
|
}
|
2018-09-07 07:17:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
if (virNetlinkTalk(ifname, nl_msg, 0, 0,
|
|
|
|
&resp, &resp_len, error, NULL) < 0)
|
2018-09-07 07:17:24 +00:00
|
|
|
return -1;
|
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
if (resp->nlmsg_type != NLMSG_ERROR &&
|
|
|
|
resp->nlmsg_type != NLMSG_DONE) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed netlink response message"));
|
|
|
|
return -1;
|
2018-09-07 07:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-17 18:27:21 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkDelLink:
|
|
|
|
*
|
2018-02-06 11:38:17 +00:00
|
|
|
* @ifname: Name of the link
|
util: fallback to ioctl(SIOCBRDELBR) if netlink RTM_DELLINK fails
commit 09778e09 switched from using ioctl(SIOCBRDELBR) for bridge
device deletion to using a netlink RTM_DELLINK message, which is the
more modern way to delete a bridge (and also doesn't require the
bridge to be ~IFF_UP to succeed). However, although older kernels
(e.g. 2.6.32, in RHEL6/CentOS6) support deleting *some* link types
with RTM_NEWLINK, they don't support deleting bridges, and there is no
compile-time way to figure this out.
This patch moves the body of the SIOCBRDELBR version of
virNetDevBridgeDelete() into a static function, calls the new function
from the original, and also calls the new function from the
RTM_DELLINK version if the RTM_DELLINK message generates an EOPNOTSUPP
error. Since RTM_DELLINK is done from the subordinate function
virNetlinkDelLink, which is also called for other purposes (deleting a
macvtap interface), a function pointer called "fallback" has been
added to the arglist of virNetlinkDelLink() - if that arg != NULL, the
provided function will be called when (and only when) RTM_DELLINK
fails with EOPNOTSUPP.
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1252780 (part 2)
2015-08-26 03:19:03 +00:00
|
|
|
* @fallback: pointer to an alternate function that will
|
|
|
|
* be called to perform the delete if RTM_DELLINK fails
|
|
|
|
* with EOPNOTSUPP (any other error will simply be treated
|
|
|
|
* as an error).
|
2015-03-17 18:27:21 +00:00
|
|
|
*
|
|
|
|
* delete a network "link" (aka interface aka device) with the given
|
|
|
|
* name. This works for many different types of network devices,
|
|
|
|
* including macvtap and bridges.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on fatal error.
|
|
|
|
*/
|
|
|
|
int
|
2021-01-11 02:23:37 +00:00
|
|
|
virNetlinkDelLink(const char *ifname, virNetlinkTalkFallback fallback)
|
2015-03-17 18:27:21 +00:00
|
|
|
{
|
|
|
|
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virNetlinkMsg) nl_msg = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree struct nlmsghdr *resp = NULL;
|
2021-01-11 02:23:37 +00:00
|
|
|
unsigned int resp_len = 0;
|
|
|
|
int error = 0;
|
2015-03-17 18:27:21 +00:00
|
|
|
|
2021-02-24 08:51:19 +00:00
|
|
|
nl_msg = virNetlinkMsgNew(RTM_DELLINK, NLM_F_REQUEST);
|
2015-03-17 18:27:21 +00:00
|
|
|
|
2021-01-11 02:23:36 +00:00
|
|
|
NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo);
|
2015-03-17 18:27:21 +00:00
|
|
|
|
2021-01-11 02:23:35 +00:00
|
|
|
NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname);
|
2015-03-17 18:27:21 +00:00
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
if (virNetlinkTalk(ifname, nl_msg, 0, 0,
|
|
|
|
&resp, &resp_len, &error, fallback) < 0) {
|
|
|
|
virReportSystemError(-error,
|
|
|
|
_("error destroying network device %s"),
|
|
|
|
ifname);
|
2018-08-09 04:12:12 +00:00
|
|
|
return -1;
|
2015-03-17 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
if (resp->nlmsg_type != NLMSG_ERROR &&
|
|
|
|
resp->nlmsg_type != NLMSG_DONE) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed netlink response message"));
|
|
|
|
return -1;
|
2015-03-17 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 04:12:12 +00:00
|
|
|
return 0;
|
2015-03-17 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 07:11:55 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkGetNeighbor:
|
|
|
|
*
|
|
|
|
* @nlData: Gets a pointer to the raw data from netlink.
|
|
|
|
MUST BE FREED BY CALLER!
|
|
|
|
* @src_pid: pid used for nl_pid of the local end of the netlink message
|
|
|
|
* (0 == "use getpid()")
|
|
|
|
* @dst_pid: pid of destination nl_pid if the kernel
|
|
|
|
* is not the target of the netlink message but it is to be
|
|
|
|
* sent to another process (0 if sending to the kernel)
|
|
|
|
*
|
|
|
|
* Get neighbor table entry from netlink.
|
|
|
|
*
|
2021-01-11 02:23:37 +00:00
|
|
|
* Returns length of the raw data from netlink on success, -1 on fatal error.
|
2018-03-08 07:11:55 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
|
|
|
|
{
|
|
|
|
struct ndmsg ndinfo = {
|
|
|
|
.ndm_family = AF_UNSPEC,
|
|
|
|
};
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virNetlinkMsg) nl_msg = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree struct nlmsghdr *resp = NULL;
|
2021-01-11 02:23:37 +00:00
|
|
|
unsigned int resp_len = 0;
|
|
|
|
int error = 0;
|
2018-03-08 07:11:55 +00:00
|
|
|
|
2021-02-24 08:51:19 +00:00
|
|
|
nl_msg = virNetlinkMsgNew(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
|
2018-03-08 07:11:55 +00:00
|
|
|
|
2021-01-11 02:23:36 +00:00
|
|
|
NETLINK_MSG_APPEND(nl_msg, sizeof(ndinfo), &ndinfo);
|
2018-03-08 07:11:55 +00:00
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
if (virNetlinkTalk(NULL, nl_msg, src_pid, dst_pid,
|
|
|
|
&resp, &resp_len, &error, NULL) < 0) {
|
|
|
|
virReportSystemError(-error, "%s", _("error dumping neighbor table"));
|
2018-08-09 04:12:12 +00:00
|
|
|
return -1;
|
2021-01-11 02:23:37 +00:00
|
|
|
}
|
2018-03-08 07:11:55 +00:00
|
|
|
|
2021-01-11 02:23:37 +00:00
|
|
|
if (resp->nlmsg_type != NLMSG_ERROR &&
|
|
|
|
resp->nlmsg_type != RTM_NEWNEIGH) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed netlink response message"));
|
|
|
|
return -1;
|
2018-03-08 07:11:55 +00:00
|
|
|
}
|
2018-08-09 04:12:11 +00:00
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*nlData = g_steal_pointer(&resp);
|
2021-01-11 02:23:37 +00:00
|
|
|
return resp_len;
|
2018-03-08 07:11:55 +00:00
|
|
|
}
|
2015-03-17 18:27:21 +00:00
|
|
|
|
2015-01-05 22:08:08 +00:00
|
|
|
int
|
|
|
|
virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen)
|
2014-08-05 17:15:11 +00:00
|
|
|
{
|
|
|
|
struct nlmsgerr *err;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
|
|
|
|
goto malformed_resp;
|
|
|
|
|
|
|
|
switch (resp->nlmsg_type) {
|
|
|
|
case NLMSG_ERROR:
|
|
|
|
err = (struct nlmsgerr *)NLMSG_DATA(resp);
|
|
|
|
if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
|
|
|
|
goto malformed_resp;
|
|
|
|
|
|
|
|
switch (err->error) {
|
|
|
|
case 0: /* ACK */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
result = err->error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NLMSG_DONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-10-20 15:44:19 +00:00
|
|
|
/* We allow multipart messages. */
|
|
|
|
if (!(resp->nlmsg_flags & NLM_F_MULTI))
|
|
|
|
goto malformed_resp;
|
2014-08-05 17:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
malformed_resp:
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed netlink response message"));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-01-05 22:08:08 +00:00
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
static void
|
|
|
|
virNetlinkEventServerLock(virNetlinkEventSrvPrivatePtr driver)
|
|
|
|
{
|
|
|
|
virMutexLock(&driver->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virNetlinkEventServerUnlock(virNetlinkEventSrvPrivatePtr driver)
|
|
|
|
{
|
|
|
|
virMutexUnlock(&driver->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkEventRemoveClientPrimitive:
|
|
|
|
*
|
2018-02-06 11:38:17 +00:00
|
|
|
* @i: index of the client to remove from the table
|
2012-08-22 04:10:23 +00:00
|
|
|
* @protocol: netlink protocol
|
2012-02-22 13:17:13 +00:00
|
|
|
*
|
|
|
|
* This static function does the low level removal of a client from
|
|
|
|
* the table once its index is known, including calling the remove
|
|
|
|
* callback (which usually will free resources required by the
|
|
|
|
* handler). The event server lock *must* be locked before calling
|
|
|
|
* this function.
|
|
|
|
*
|
|
|
|
* assumes success, returns nothing.
|
|
|
|
*/
|
2012-08-22 04:10:23 +00:00
|
|
|
static int
|
|
|
|
virNetlinkEventRemoveClientPrimitive(size_t i, unsigned int protocol)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
2020-07-28 17:50:28 +00:00
|
|
|
virNetlinkEventRemoveCallback removeCB;
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
if (protocol >= MAX_LINKS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-07-28 17:50:28 +00:00
|
|
|
removeCB = server[protocol]->handles[i].removeCB;
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
if (removeCB) {
|
2012-08-22 04:10:23 +00:00
|
|
|
(removeCB)(server[protocol]->handles[i].watch,
|
|
|
|
&server[protocol]->handles[i].macaddr,
|
|
|
|
server[protocol]->handles[i].opaque);
|
2012-02-22 13:17:13 +00:00
|
|
|
}
|
2012-08-22 04:10:23 +00:00
|
|
|
server[protocol]->handles[i].deleted = VIR_NETLINK_HANDLE_DELETED;
|
|
|
|
return 0;
|
2012-02-22 13:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virNetlinkEventCallback(int watch,
|
2019-10-14 12:45:33 +00:00
|
|
|
int fd G_GNUC_UNUSED,
|
|
|
|
int events G_GNUC_UNUSED,
|
2012-02-22 13:17:13 +00:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virNetlinkEventSrvPrivatePtr srv = opaque;
|
|
|
|
struct sockaddr_nl peer;
|
|
|
|
struct ucred *creds = NULL;
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
|
|
|
int length;
|
2012-02-22 13:17:13 +00:00
|
|
|
bool handled = false;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree struct nlmsghdr *msg = NULL;
|
2012-02-22 13:17:13 +00:00
|
|
|
|
2013-04-03 13:09:19 +00:00
|
|
|
length = nl_recv(srv->netlinknh, &peer,
|
|
|
|
(unsigned char **)&msg, &creds);
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return;
|
|
|
|
if (length < 0) {
|
2012-04-26 18:35:26 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s", _("nl_recv returned with error"));
|
2012-02-22 13:17:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
virNetlinkEventServerLock(srv);
|
|
|
|
|
|
|
|
VIR_DEBUG("dispatching to max %d clients, called from event watch %d",
|
2018-09-19 08:38:14 +00:00
|
|
|
(int)srv->handlesCount, watch);
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
for (i = 0; i < srv->handlesCount; i++) {
|
|
|
|
if (srv->handles[i].deleted != VIR_NETLINK_HANDLE_VALID)
|
|
|
|
continue;
|
|
|
|
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
VIR_DEBUG("dispatching client %zu.", i);
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
(srv->handles[i].handleCB)(msg, length, &peer, &handled,
|
|
|
|
srv->handles[i].opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled)
|
|
|
|
VIR_DEBUG("event not handled.");
|
2018-08-09 04:12:11 +00:00
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
virNetlinkEventServerUnlock(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkEventServiceStop:
|
|
|
|
*
|
|
|
|
* stop the monitor to receive netlink messages for libvirtd.
|
|
|
|
* This removes the netlink socket fd from the event handler.
|
|
|
|
*
|
2012-08-22 04:10:23 +00:00
|
|
|
* @protocol: netlink protocol
|
|
|
|
*
|
2012-02-29 20:21:27 +00:00
|
|
|
* Returns -1 if the monitor cannot be unregistered, 0 upon success
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
|
|
|
int
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkEventServiceStop(unsigned int protocol)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
2020-07-28 17:50:28 +00:00
|
|
|
virNetlinkEventSrvPrivatePtr srv;
|
|
|
|
size_t i;
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
if (protocol >= MAX_LINKS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-07-28 17:50:28 +00:00
|
|
|
srv = server[protocol];
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
VIR_INFO("stopping netlink event service");
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
if (!server[protocol])
|
2012-02-22 13:17:13 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
virNetlinkEventServerLock(srv);
|
|
|
|
nl_close(srv->netlinknh);
|
2012-05-03 16:10:50 +00:00
|
|
|
virNetlinkFree(srv->netlinknh);
|
2012-02-22 13:17:13 +00:00
|
|
|
virEventRemoveHandle(srv->eventwatch);
|
|
|
|
|
|
|
|
/* free any remaining clients on the list */
|
|
|
|
for (i = 0; i < srv->handlesCount; i++) {
|
|
|
|
if (srv->handles[i].deleted == VIR_NETLINK_HANDLE_VALID)
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkEventRemoveClientPrimitive(i, protocol);
|
2012-02-22 13:17:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
server[protocol] = NULL;
|
2017-05-18 04:06:42 +00:00
|
|
|
VIR_FREE(srv->handles);
|
2012-02-22 13:17:13 +00:00
|
|
|
virNetlinkEventServerUnlock(srv);
|
|
|
|
|
|
|
|
virMutexDestroy(&srv->lock);
|
|
|
|
VIR_FREE(srv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 04:10:24 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkEventServiceStopAll:
|
|
|
|
*
|
|
|
|
* Stop all the monitors to receive netlink messages for libvirtd.
|
|
|
|
*
|
|
|
|
* Returns -1 if any monitor cannot be unregistered, 0 upon success
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virNetlinkEventServiceStopAll(void)
|
|
|
|
{
|
2017-05-18 04:06:41 +00:00
|
|
|
size_t i;
|
2012-08-22 04:10:24 +00:00
|
|
|
|
|
|
|
VIR_INFO("stopping all netlink event services");
|
|
|
|
|
2017-05-18 04:06:41 +00:00
|
|
|
for (i = 0; i < MAX_LINKS; i++)
|
|
|
|
virNetlinkEventServiceStop(i);
|
2012-08-22 04:10:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkEventServiceIsRunning:
|
|
|
|
*
|
2012-02-29 20:21:27 +00:00
|
|
|
* Returns if the netlink event service is running.
|
2012-02-22 13:17:13 +00:00
|
|
|
*
|
2012-08-22 04:10:23 +00:00
|
|
|
* @protocol: netlink protocol
|
|
|
|
*
|
2012-02-29 20:21:27 +00:00
|
|
|
* Returns 'true' if the service is running, 'false' if stopped.
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
|
|
|
bool
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkEventServiceIsRunning(unsigned int protocol)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
2012-08-22 04:10:23 +00:00
|
|
|
if (protocol >= MAX_LINKS) {
|
|
|
|
virReportSystemError(EINVAL,
|
|
|
|
_("invalid protocol argument: %d"), protocol);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return server[protocol] != NULL;
|
2012-02-22 13:17:13 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 17:19:51 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkEventServiceLocalPid:
|
|
|
|
*
|
2012-08-22 04:10:23 +00:00
|
|
|
* @protocol: netlink protocol
|
|
|
|
*
|
2012-05-04 17:19:51 +00:00
|
|
|
* Returns the nl_pid value that was used to bind() the netlink socket
|
|
|
|
* used by the netlink event service, or -1 on error (netlink
|
|
|
|
* guarantees that this value will always be > 0).
|
|
|
|
*/
|
2012-08-22 04:10:23 +00:00
|
|
|
int virNetlinkEventServiceLocalPid(unsigned int protocol)
|
2012-05-04 17:19:51 +00:00
|
|
|
{
|
2012-08-22 04:10:23 +00:00
|
|
|
if (protocol >= MAX_LINKS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!(server[protocol] && server[protocol]->netlinknh)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("netlink event service not running"));
|
2012-05-04 17:19:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-08-22 04:10:23 +00:00
|
|
|
return (int)nl_socket_get_local_port(server[protocol]->netlinknh);
|
2012-05-04 17:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
/**
|
|
|
|
* virNetlinkEventServiceStart:
|
|
|
|
*
|
|
|
|
* start a monitor to receive netlink messages for libvirtd.
|
|
|
|
* This registers a netlink socket with the event interface.
|
|
|
|
*
|
2012-08-22 04:10:23 +00:00
|
|
|
* @protocol: netlink protocol
|
2018-02-06 11:38:17 +00:00
|
|
|
* @groups: broadcast groups to join in
|
|
|
|
*
|
2012-02-29 20:21:27 +00:00
|
|
|
* Returns -1 if the monitor cannot be registered, 0 upon success
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
|
|
|
int
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
|
|
|
virNetlinkEventSrvPrivatePtr srv;
|
|
|
|
int fd;
|
|
|
|
int ret = -1;
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
if (protocol >= MAX_LINKS) {
|
|
|
|
virReportSystemError(EINVAL,
|
|
|
|
_("invalid protocol argument: %d"), protocol);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server[protocol])
|
2012-02-22 13:17:13 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
VIR_INFO("starting netlink event service with protocol %d", protocol);
|
2012-02-22 13:17:13 +00:00
|
|
|
|
2020-10-05 17:12:37 +00:00
|
|
|
srv = g_new0(virNetlinkEventSrvPrivate, 1);
|
2012-02-22 13:17:13 +00:00
|
|
|
|
2012-05-02 14:51:38 +00:00
|
|
|
if (virMutexInit(&srv->lock) < 0) {
|
|
|
|
VIR_FREE(srv);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
virNetlinkEventServerLock(srv);
|
|
|
|
|
|
|
|
/* Allocate a new socket and get fd */
|
2016-01-11 06:59:00 +00:00
|
|
|
if (!(srv->netlinknh = virNetlinkCreateSocket(protocol)))
|
2012-02-22 13:17:13 +00:00
|
|
|
goto error_locked;
|
|
|
|
|
|
|
|
fd = nl_socket_get_fd(srv->netlinknh);
|
|
|
|
if (fd < 0) {
|
2012-04-26 18:35:26 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s", _("cannot get netlink socket fd"));
|
2012-02-22 13:17:13 +00:00
|
|
|
goto error_server;
|
|
|
|
}
|
|
|
|
|
2012-08-22 04:10:23 +00:00
|
|
|
if (groups && nl_socket_add_membership(srv->netlinknh, groups) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s", _("cannot add netlink membership"));
|
|
|
|
goto error_server;
|
|
|
|
}
|
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
if (nl_socket_set_nonblocking(srv->netlinknh)) {
|
2012-04-26 18:35:26 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot set netlink socket nonblocking"));
|
2012-02-22 13:17:13 +00:00
|
|
|
goto error_server;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((srv->eventwatch = virEventAddHandle(fd,
|
|
|
|
VIR_EVENT_HANDLE_READABLE,
|
|
|
|
virNetlinkEventCallback,
|
|
|
|
srv, NULL)) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Failed to add netlink event handle watch"));
|
2012-02-22 13:17:13 +00:00
|
|
|
goto error_server;
|
|
|
|
}
|
|
|
|
|
|
|
|
srv->netlinkfd = fd;
|
|
|
|
VIR_DEBUG("netlink event listener on fd: %i running", fd);
|
|
|
|
|
|
|
|
ret = 0;
|
2012-08-22 04:10:23 +00:00
|
|
|
server[protocol] = srv;
|
2012-02-22 13:17:13 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error_server:
|
2012-02-22 13:17:13 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
nl_close(srv->netlinknh);
|
2012-05-03 16:10:50 +00:00
|
|
|
virNetlinkFree(srv->netlinknh);
|
2012-02-22 13:17:13 +00:00
|
|
|
}
|
2014-03-25 06:53:22 +00:00
|
|
|
error_locked:
|
2012-02-22 13:17:13 +00:00
|
|
|
virNetlinkEventServerUnlock(srv);
|
|
|
|
if (ret < 0) {
|
|
|
|
virMutexDestroy(&srv->lock);
|
|
|
|
VIR_FREE(srv);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkEventAddClient:
|
|
|
|
*
|
|
|
|
* @handleCB: callback to invoke when an event occurs
|
|
|
|
* @removeCB: callback to invoke when removing a client
|
2018-02-06 11:38:17 +00:00
|
|
|
* @opaque: user data to pass to callback
|
|
|
|
* @macaddr: macaddr to store with the data. Used to identify callers.
|
|
|
|
* May be null.
|
2012-08-22 04:10:23 +00:00
|
|
|
* @protocol: netlink protocol
|
2012-02-22 13:17:13 +00:00
|
|
|
*
|
|
|
|
* register a callback for handling of netlink messages. The
|
|
|
|
* registered function receives the entire netlink message and
|
|
|
|
* may choose to act upon it.
|
|
|
|
*
|
2012-02-29 20:21:27 +00:00
|
|
|
* Returns -1 if the file handle cannot be registered, number of
|
|
|
|
* monitor upon success.
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB,
|
|
|
|
virNetlinkEventRemoveCallback removeCB,
|
2013-10-05 19:41:44 +00:00
|
|
|
void *opaque, const virMacAddr *macaddr,
|
2012-08-22 04:10:23 +00:00
|
|
|
unsigned int protocol)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
|
|
|
int r, ret = -1;
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkEventSrvPrivatePtr srv = NULL;
|
|
|
|
|
|
|
|
if (protocol >= MAX_LINKS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
srv = server[protocol];
|
2012-02-22 13:17:13 +00:00
|
|
|
|
2012-03-27 12:38:33 +00:00
|
|
|
if (handleCB == NULL) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Invalid NULL callback provided"));
|
2012-02-22 13:17:13 +00:00
|
|
|
return -1;
|
2012-03-27 12:38:33 +00:00
|
|
|
}
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
virNetlinkEventServerLock(srv);
|
|
|
|
|
|
|
|
VIR_DEBUG("adding client: %d.", nextWatch);
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
/* first try to re-use deleted free slots */
|
|
|
|
for (i = 0; i < srv->handlesCount; i++) {
|
|
|
|
if (srv->handles[i].deleted == VIR_NETLINK_HANDLE_DELETED) {
|
|
|
|
r = i;
|
|
|
|
goto addentry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Resize the eventLoop array if needed */
|
|
|
|
if (srv->handlesCount == srv->handlesAlloc) {
|
|
|
|
VIR_DEBUG("Used %zu handle slots, adding at least %d more",
|
|
|
|
srv->handlesAlloc, NETLINK_EVENT_ALLOC_EXTENT);
|
2021-03-19 23:37:01 +00:00
|
|
|
VIR_RESIZE_N(srv->handles, srv->handlesAlloc,
|
|
|
|
srv->handlesCount, NETLINK_EVENT_ALLOC_EXTENT);
|
2012-02-22 13:17:13 +00:00
|
|
|
}
|
|
|
|
r = srv->handlesCount++;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
addentry:
|
2012-02-22 13:17:13 +00:00
|
|
|
srv->handles[r].watch = nextWatch;
|
|
|
|
srv->handles[r].handleCB = handleCB;
|
|
|
|
srv->handles[r].removeCB = removeCB;
|
|
|
|
srv->handles[r].opaque = opaque;
|
|
|
|
srv->handles[r].deleted = VIR_NETLINK_HANDLE_VALID;
|
|
|
|
if (macaddr)
|
2012-07-17 12:07:59 +00:00
|
|
|
virMacAddrSet(&srv->handles[r].macaddr, macaddr);
|
2012-02-22 13:17:13 +00:00
|
|
|
else
|
2012-07-17 12:07:59 +00:00
|
|
|
virMacAddrSetRaw(&srv->handles[r].macaddr,
|
2013-11-19 23:00:32 +00:00
|
|
|
(unsigned char[VIR_MAC_BUFLEN]){0, 0, 0, 0, 0, 0});
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("added client to loop slot: %d. with macaddr ptr=%p", r, macaddr);
|
|
|
|
|
|
|
|
ret = nextWatch++;
|
2021-03-19 23:37:01 +00:00
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
virNetlinkEventServerUnlock(srv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkEventRemoveClient:
|
|
|
|
*
|
2018-02-06 11:38:17 +00:00
|
|
|
* @watch: watch whose handle to remove
|
|
|
|
* @macaddr: macaddr whose handle to remove
|
2012-08-22 04:10:23 +00:00
|
|
|
* @protocol: netlink protocol
|
2012-02-22 13:17:13 +00:00
|
|
|
*
|
|
|
|
* Unregister a callback from a netlink monitor.
|
|
|
|
* The handler function referenced will no longer receive netlink messages.
|
|
|
|
* Either watch or macaddr may be used, the other should be null.
|
|
|
|
*
|
2012-02-29 20:21:27 +00:00
|
|
|
* Returns -1 if the file handle was not registered, 0 upon success
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-10-05 19:41:44 +00:00
|
|
|
virNetlinkEventRemoveClient(int watch, const virMacAddr *macaddr,
|
2012-08-22 04:10:23 +00:00
|
|
|
unsigned int protocol)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-02-22 13:17:13 +00:00
|
|
|
int ret = -1;
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkEventSrvPrivatePtr srv = NULL;
|
|
|
|
|
|
|
|
if (protocol >= MAX_LINKS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
srv = server[protocol];
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("removing client watch=%d, mac=%p.", watch, macaddr);
|
|
|
|
|
|
|
|
if (watch <= 0 && !macaddr) {
|
|
|
|
VIR_WARN("Ignoring invalid netlink client id: %d", watch);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virNetlinkEventServerLock(srv);
|
|
|
|
|
|
|
|
for (i = 0; i < srv->handlesCount; i++) {
|
|
|
|
if (srv->handles[i].deleted != VIR_NETLINK_HANDLE_VALID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((watch && srv->handles[i].watch == watch) ||
|
|
|
|
(!watch &&
|
2012-07-17 12:07:59 +00:00
|
|
|
virMacAddrCmp(macaddr, &srv->handles[i].macaddr) == 0)) {
|
2012-02-22 13:17:13 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("removed client: %d by %s.",
|
|
|
|
srv->handles[i].watch, watch ? "index" : "mac");
|
2012-08-22 04:10:23 +00:00
|
|
|
virNetlinkEventRemoveClientPrimitive(i, protocol);
|
2012-02-22 13:17:13 +00:00
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VIR_DEBUG("no client found to remove.");
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-02-22 13:17:13 +00:00
|
|
|
virNetlinkEventServerUnlock(srv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-22 18:17:36 +00:00
|
|
|
#else
|
|
|
|
|
2021-03-09 10:18:53 +00:00
|
|
|
# if defined(__linux__)
|
2012-03-06 17:21:21 +00:00
|
|
|
static const char *unsupported = N_("libnl was not available at build time");
|
|
|
|
# else
|
|
|
|
static const char *unsupported = N_("not supported on non-linux platforms");
|
|
|
|
# endif
|
|
|
|
|
2012-05-03 14:39:04 +00:00
|
|
|
int
|
|
|
|
virNetlinkStartup(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
virNetlinkShutdown(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
int virNetlinkCommand(struct nl_msg *nl_msg G_GNUC_UNUSED,
|
|
|
|
struct nlmsghdr **resp G_GNUC_UNUSED,
|
|
|
|
unsigned int *respbuflen G_GNUC_UNUSED,
|
|
|
|
uint32_t src_pid G_GNUC_UNUSED,
|
|
|
|
uint32_t dst_pid G_GNUC_UNUSED,
|
|
|
|
unsigned int protocol G_GNUC_UNUSED,
|
|
|
|
unsigned int groups G_GNUC_UNUSED)
|
2011-06-22 18:17:36 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-06-22 18:17:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-03 11:22:50 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virNetlinkDumpCommand(struct nl_msg *nl_msg G_GNUC_UNUSED,
|
|
|
|
virNetlinkDumpCallback callback G_GNUC_UNUSED,
|
|
|
|
uint32_t src_pid G_GNUC_UNUSED,
|
|
|
|
uint32_t dst_pid G_GNUC_UNUSED,
|
|
|
|
unsigned int protocol G_GNUC_UNUSED,
|
|
|
|
unsigned int groups G_GNUC_UNUSED,
|
|
|
|
void *opaque G_GNUC_UNUSED)
|
2017-03-03 11:22:50 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-17 18:27:21 +00:00
|
|
|
|
2016-06-13 11:59:12 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virNetlinkDumpLink(const char *ifname G_GNUC_UNUSED,
|
|
|
|
int ifindex G_GNUC_UNUSED,
|
|
|
|
void **nlData G_GNUC_UNUSED,
|
|
|
|
struct nlattr **tb G_GNUC_UNUSED,
|
|
|
|
uint32_t src_pid G_GNUC_UNUSED,
|
|
|
|
uint32_t dst_pid G_GNUC_UNUSED)
|
2016-06-13 11:59:12 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Unable to dump link info on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-17 18:27:21 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virNetlinkDelLink(const char *ifname G_GNUC_UNUSED,
|
2021-01-11 02:23:37 +00:00
|
|
|
virNetlinkTalkFallback fallback G_GNUC_UNUSED)
|
2015-03-17 18:27:21 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-16 07:44:20 +00:00
|
|
|
|
2018-09-07 07:17:24 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virNetlinkNewLink(const char *ifname G_GNUC_UNUSED,
|
|
|
|
const char *type G_GNUC_UNUSED,
|
|
|
|
virNetlinkNewLinkDataPtr extra_args G_GNUC_UNUSED,
|
|
|
|
int *error G_GNUC_UNUSED)
|
2018-09-07 07:17:24 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-16 07:44:20 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virNetlinkGetNeighbor(void **nlData G_GNUC_UNUSED,
|
|
|
|
uint32_t src_pid G_GNUC_UNUSED,
|
|
|
|
uint32_t dst_pid G_GNUC_UNUSED)
|
2018-03-16 07:44:20 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
/**
|
2012-02-29 20:21:27 +00:00
|
|
|
* stopNetlinkEventServer: stop the monitor to receive netlink
|
|
|
|
* messages for libvirtd
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
2019-10-14 12:45:33 +00:00
|
|
|
int virNetlinkEventServiceStop(unsigned int protocol G_GNUC_UNUSED)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
2012-05-03 15:52:17 +00:00
|
|
|
VIR_DEBUG("%s", _(unsupported));
|
2012-02-22 13:17:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 04:10:24 +00:00
|
|
|
/**
|
|
|
|
* stopNetlinkEventServerAll: stop all the monitors to receive netlink
|
|
|
|
* messages for libvirtd
|
|
|
|
*/
|
|
|
|
int virNetlinkEventServiceStopAll(void)
|
|
|
|
{
|
|
|
|
VIR_DEBUG("%s", _(unsupported));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
/**
|
2012-02-29 20:21:27 +00:00
|
|
|
* startNetlinkEventServer: start a monitor to receive netlink
|
|
|
|
* messages for libvirtd
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
2019-10-14 12:45:33 +00:00
|
|
|
int virNetlinkEventServiceStart(unsigned int protocol G_GNUC_UNUSED,
|
|
|
|
unsigned int groups G_GNUC_UNUSED)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
2012-05-03 15:52:17 +00:00
|
|
|
VIR_DEBUG("%s", _(unsupported));
|
2012-02-22 13:17:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-29 20:21:27 +00:00
|
|
|
* virNetlinkEventServiceIsRunning: returns if the netlink event
|
|
|
|
* service is running.
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
2019-10-14 12:45:33 +00:00
|
|
|
bool virNetlinkEventServiceIsRunning(unsigned int protocol G_GNUC_UNUSED)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2020-05-05 06:05:18 +00:00
|
|
|
return false;
|
2012-02-22 13:17:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
int virNetlinkEventServiceLocalPid(unsigned int protocol G_GNUC_UNUSED)
|
2012-05-24 12:14:57 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2012-05-24 12:14:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-22 13:17:13 +00:00
|
|
|
/**
|
2012-02-29 20:21:27 +00:00
|
|
|
* virNetlinkEventAddClient: register a callback for handling of
|
|
|
|
* netlink messages
|
2012-02-22 13:17:13 +00:00
|
|
|
*/
|
2019-10-14 12:45:33 +00:00
|
|
|
int virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB G_GNUC_UNUSED,
|
|
|
|
virNetlinkEventRemoveCallback removeCB G_GNUC_UNUSED,
|
|
|
|
void *opaque G_GNUC_UNUSED,
|
|
|
|
const virMacAddr *macaddr G_GNUC_UNUSED,
|
|
|
|
unsigned int protocol G_GNUC_UNUSED)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2012-02-22 13:17:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetlinkEventRemoveClient: unregister a callback from a netlink monitor
|
|
|
|
*/
|
2019-10-14 12:45:33 +00:00
|
|
|
int virNetlinkEventRemoveClient(int watch G_GNUC_UNUSED,
|
|
|
|
const virMacAddr *macaddr G_GNUC_UNUSED,
|
|
|
|
unsigned int protocol G_GNUC_UNUSED)
|
2012-02-22 13:17:13 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2012-02-22 13:17:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-05 22:08:08 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virNetlinkGetErrorCode(struct nlmsghdr *resp G_GNUC_UNUSED,
|
|
|
|
unsigned int recvbuflen G_GNUC_UNUSED)
|
2015-01-05 22:08:08 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-09-28 21:29:41 +00:00
|
|
|
#endif /* WITH_LIBNL */
|