2018-03-08 07:11:56 +00:00
|
|
|
/*
|
|
|
|
* virarptable.c Linux ARP table handling
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Chen Hanxiao
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2018-03-15 12:27:55 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <linux/rtnetlink.h>
|
|
|
|
#endif
|
2018-03-08 07:11:56 +00:00
|
|
|
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virarptable.h"
|
2021-01-06 11:56:11 +00:00
|
|
|
#include "virerror.h"
|
2018-03-08 07:11:56 +00:00
|
|
|
#include "virfile.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virnetlink.h"
|
|
|
|
#include "virsocketaddr.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
VIR_LOG_INIT("util.arptable");
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
|
|
|
# define NDA_RTA(r) \
|
|
|
|
((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
|
|
|
|
|
2018-03-17 10:24:08 +00:00
|
|
|
|
2018-03-08 07:11:56 +00:00
|
|
|
static int
|
|
|
|
parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
|
|
|
|
{
|
|
|
|
memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
|
2018-03-15 14:08:58 +00:00
|
|
|
VIR_WARNINGS_NO_CAST_ALIGN
|
|
|
|
for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
|
|
|
|
VIR_WARNINGS_RESET
|
2018-03-08 07:11:56 +00:00
|
|
|
if ((rta->rta_type <= max) && (!tb[rta->rta_type]))
|
|
|
|
tb[rta->rta_type] = rta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
VIR_WARN("malformed netlink message: Deficit %d, rta_len=%d",
|
|
|
|
len, rta->rta_len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-17 10:24:08 +00:00
|
|
|
|
|
|
|
virArpTablePtr
|
|
|
|
virArpTableGet(void)
|
2018-03-08 07:11:56 +00:00
|
|
|
{
|
|
|
|
int num = 0;
|
|
|
|
int msglen;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree void *nlData = NULL;
|
2018-03-08 07:11:56 +00:00
|
|
|
virArpTablePtr table = NULL;
|
|
|
|
struct nlmsghdr* nh;
|
|
|
|
struct rtattr * tb[NDA_MAX+1];
|
|
|
|
|
|
|
|
msglen = virNetlinkGetNeighbor(&nlData, 0, 0);
|
|
|
|
if (msglen < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2020-10-05 17:12:37 +00:00
|
|
|
table = g_new0(virArpTable, 1);
|
2018-03-08 07:11:56 +00:00
|
|
|
|
|
|
|
nh = (struct nlmsghdr*)nlData;
|
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
VIR_WARNINGS_NO_CAST_ALIGN
|
|
|
|
for (; NLMSG_OK(nh, msglen); nh = NLMSG_NEXT(nh, msglen)) {
|
|
|
|
VIR_WARNINGS_RESET
|
2018-03-08 07:11:56 +00:00
|
|
|
struct ndmsg *r = NLMSG_DATA(nh);
|
|
|
|
int len = nh->nlmsg_len;
|
|
|
|
void *addr;
|
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
if ((len -= NLMSG_LENGTH(sizeof(*nh))) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("wrong nlmsg len"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
if (r->ndm_family && (r->ndm_family != AF_INET))
|
|
|
|
continue;
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
/* catch stale and reachalbe arp entry only */
|
|
|
|
if (r->ndm_state &&
|
|
|
|
(!(r->ndm_state == NUD_STALE || r->ndm_state == NUD_REACHABLE)))
|
|
|
|
continue;
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
if (nh->nlmsg_type == NLMSG_DONE)
|
2018-07-13 17:55:04 +00:00
|
|
|
return table;
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
VIR_WARNINGS_NO_CAST_ALIGN
|
|
|
|
parse_rtattr(tb, NDA_MAX, NDA_RTA(r),
|
|
|
|
nh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
|
|
|
|
VIR_WARNINGS_RESET
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
if (tb[NDA_DST] == NULL || tb[NDA_LLADDR] == NULL)
|
|
|
|
continue;
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
if (tb[NDA_DST]) {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *ipstr = NULL;
|
2018-03-15 14:08:58 +00:00
|
|
|
virSocketAddr virAddr;
|
|
|
|
if (VIR_REALLOC_N(table->t, num + 1) < 0)
|
|
|
|
goto cleanup;
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
table->n = num + 1;
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
addr = RTA_DATA(tb[NDA_DST]);
|
|
|
|
bzero(&virAddr, sizeof(virAddr));
|
|
|
|
virAddr.len = sizeof(virAddr.data.inet4);
|
|
|
|
virAddr.data.inet4.sin_family = AF_INET;
|
|
|
|
virAddr.data.inet4.sin_addr = *(struct in_addr *)addr;
|
|
|
|
ipstr = virSocketAddrFormat(&virAddr);
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
table->t[num].ipaddr = g_strdup(ipstr);
|
2018-03-15 14:08:58 +00:00
|
|
|
}
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
if (tb[NDA_LLADDR]) {
|
|
|
|
virMacAddr macaddr;
|
|
|
|
char ifmac[VIR_MAC_STRING_BUFLEN];
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
addr = RTA_DATA(tb[NDA_LLADDR]);
|
|
|
|
memcpy(macaddr.addr, addr, VIR_MAC_BUFLEN);
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
virMacAddrFormat(&macaddr, ifmac);
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
table->t[num].mac = g_strdup(ifmac);
|
2018-03-08 07:11:56 +00:00
|
|
|
|
2018-03-15 14:08:58 +00:00
|
|
|
num++;
|
|
|
|
}
|
2018-03-08 07:11:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 07:54:24 +00:00
|
|
|
return table;
|
|
|
|
|
2018-03-08 07:11:56 +00:00
|
|
|
cleanup:
|
2018-03-17 10:24:08 +00:00
|
|
|
virArpTableFree(table);
|
2018-03-08 07:11:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-03-17 10:24:08 +00:00
|
|
|
virArpTablePtr
|
|
|
|
virArpTableGet(void)
|
2018-03-08 07:11:56 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_NO_SUPPORT, "%s",
|
|
|
|
_("get arp table not implemented on this platform"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
|
|
|
void
|
|
|
|
virArpTableFree(virArpTablePtr table)
|
|
|
|
{
|
|
|
|
size_t i;
|
2018-03-17 10:24:08 +00:00
|
|
|
|
|
|
|
if (!table)
|
|
|
|
return;
|
|
|
|
|
2018-03-08 07:11:56 +00:00
|
|
|
for (i = 0; i < table->n; i++) {
|
|
|
|
VIR_FREE(table->t[i].ipaddr);
|
|
|
|
VIR_FREE(table->t[i].mac);
|
|
|
|
}
|
|
|
|
VIR_FREE(table->t);
|
|
|
|
VIR_FREE(table);
|
|
|
|
}
|