netutils/netlib/netlib_getnbtab.c: Add support for retrieving a snapshot of the IPv6 neighbor table using Netlink sockets.
This commit is contained in:
parent
caecdd6079
commit
2507cc15e8
@ -297,6 +297,12 @@ int netlib_ipv6adaptor(FAR const struct in6_addr *destipaddr,
|
||||
|
||||
uint8_t netlib_ipv6netmask2prefix(FAR const uint16_t *mask);
|
||||
void netlib_prefix2ipv6netmask(uint8_t preflen, FAR struct in6_addr *netmask);
|
||||
#ifdef CONFIG_NETLINK_ROUTE
|
||||
struct neighbor_entry_s;
|
||||
ssize_t netlib_get_nbtable(FAR struct neighbor_entry_s *nbtab,
|
||||
unsigned int nentries);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NETDEV_WIRELESS_IOCTL
|
||||
|
@ -1,7 +1,8 @@
|
||||
############################################################################
|
||||
# apps/netutils/netlib/Makefile
|
||||
#
|
||||
# Copyright (C) 2011-2012, 2014-2015, 2017 Gregory Nutt. All rights reserved.
|
||||
# Copyright (C) 2011-2012, 2014-2015, 2017, 2019 Gregory Nutt. All
|
||||
# rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
@ -41,6 +42,7 @@ CSRCS = netlib_ipv4addrconv.c netlib_ethaddrconv.c netlib_parsehttpurl.c
|
||||
CSRCS += netlib_setifstatus.c netlib_getifstatus.c
|
||||
|
||||
# Generic URL parsing support
|
||||
|
||||
ifeq ($(CONFIG_NETUTILS_NETLIB_GENERICURLPARSER),y)
|
||||
CSRCS += netlib_parseurl.c
|
||||
endif
|
||||
@ -54,6 +56,9 @@ CSRCS += netlib_getdripv4addr.c netlib_getipv4netmask.c
|
||||
CSRCS += netlib_ipv4adaptor.c
|
||||
ifeq ($(CONFIG_NET_ARP),y)
|
||||
CSRCS += netlib_getarp.c netlib_setarp.c netlib_delarp.c
|
||||
ifeq ($(CONFIG_NETLINK_ROUTE),y)
|
||||
CSRCS += netlib_getarptab.c
|
||||
endif
|
||||
endif
|
||||
ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
|
||||
CSRCS += netlib_setipv4dnsaddr.c
|
||||
@ -74,6 +79,9 @@ endif
|
||||
ifeq ($(CONFIG_NET_ROUTE),y)
|
||||
CSRCS += netlib_ipv6route.c netlib_ipv6router.c
|
||||
endif
|
||||
ifeq ($(CONFIG_NETLINK_ROUTE),y)
|
||||
CSRCS += netlib_getnbtab.c
|
||||
endif
|
||||
endif
|
||||
|
||||
# These require TCP support
|
||||
@ -84,14 +92,6 @@ CSRCS += netlib_server.c netlib_listenon.c
|
||||
endif
|
||||
endif
|
||||
|
||||
# These require Netlink support
|
||||
|
||||
ifeq ($(CONFIG_NETLINK_ROUTE),y)
|
||||
ifeq ($(CONFIG_NET_ARP),y)
|
||||
CSRCS += netlib_getarptab.c
|
||||
endif
|
||||
endif
|
||||
|
||||
# These require wireless IOCTL support */
|
||||
|
||||
ifeq ($(CONFIG_NETDEV_WIRELESS_IOCTL),y)
|
||||
|
@ -96,7 +96,8 @@ struct netlib_recvfrom_response_s
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab, unsigned int nentries)
|
||||
ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
|
||||
unsigned int nentries)
|
||||
{
|
||||
FAR struct netlib_recvfrom_response_s *resp;
|
||||
struct netlib_sendto_request_s req;
|
||||
|
228
netutils/netlib/netlib_getnbtab.c
Normal file
228
netutils/netlib/netlib_getnbtab.c
Normal file
@ -0,0 +1,228 @@
|
||||
/****************************************************************************
|
||||
* netutils/netlib/netlib_getntab.c
|
||||
*
|
||||
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netpacket/netlink.h>
|
||||
|
||||
#include <nuttx/net/neighbor.h>
|
||||
|
||||
#include "netutils/netlib.h"
|
||||
|
||||
#if defined(CONFIG_NET_IPv6) && defined(CONFIG_NETLINK_ROUTE)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
struct netlib_sendto_request_s
|
||||
{
|
||||
struct nlmsghdr hdr;
|
||||
struct ndmsg msg;
|
||||
};
|
||||
|
||||
struct netlib_recvfrom_response_s
|
||||
{
|
||||
struct nlmsghdr hdr;
|
||||
struct ndmsg msg;
|
||||
struct rtattr attr;
|
||||
uint8_t data[1];
|
||||
};
|
||||
|
||||
#define SIZEOF_NETLIB_RECVFROM_RESPONSE_S(n) \
|
||||
(sizeof(struct netlib_recvfrom_response_s) + (n) - 1)
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: netlib_get_nbtable
|
||||
*
|
||||
* Description:
|
||||
* Attempt to read the entire Neighbor table into a buffer.
|
||||
*
|
||||
* Parameters:
|
||||
* nbtab - The location to store the copy of the Neighbor table
|
||||
* nentries - The size of the provided 'nbtab' in number of entries each
|
||||
* of size sizeof(struct neighbor_entry_s)
|
||||
*
|
||||
* Return:
|
||||
* The number of Neighbor table entries read is returned on success; a
|
||||
* negated errno value is returned on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t netlib_get_nbtable(FAR struct neighbor_entry_s *nbtab,
|
||||
unsigned int nentries)
|
||||
{
|
||||
FAR struct netlib_recvfrom_response_s *resp;
|
||||
struct netlib_sendto_request_s req;
|
||||
struct sockaddr_nl addr;
|
||||
static unsigned int seqno = 0;
|
||||
unsigned int thiseq;
|
||||
unsigned int allocsize;
|
||||
ssize_t nsent;
|
||||
ssize_t nrecvd;
|
||||
ssize_t paysize;
|
||||
ssize_t maxsize;
|
||||
int fd;
|
||||
int ret;
|
||||
|
||||
/* Pre-allocate a buffer to hold the response */
|
||||
|
||||
maxsize = CONFIG_NET_IPv6_NCONF_ENTRIES *
|
||||
sizeof(struct neighbor_entry_s);
|
||||
allocsize = SIZEOF_NETLIB_RECVFROM_RESPONSE_S(maxsize);
|
||||
resp = (FAR struct netlib_recvfrom_response_s *)malloc(allocsize);
|
||||
if (resp == NULL)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Failed to allocate response buffer\n");
|
||||
ret = -ENOMEM;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* Create a NetLink socket with NETLINK_ROUTE protocol */
|
||||
|
||||
fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
|
||||
if (fd < 0)
|
||||
{
|
||||
int errcode = errno;
|
||||
fprintf(stderr, "ERROR: socket() failed: %d\n", errcode);
|
||||
ret = -errcode;
|
||||
goto errout_with_resp;
|
||||
}
|
||||
|
||||
/* Bind the socket so that we can use send() and receive() */
|
||||
|
||||
addr.nl_family = AF_NETLINK;
|
||||
addr.nl_pad = 0;
|
||||
addr.nl_pid = getpid();
|
||||
addr.nl_groups = RTM_GETNEIGH;
|
||||
|
||||
ret = bind(fd, (FAR const struct sockaddr *)&addr,
|
||||
sizeof( struct sockaddr_nl));
|
||||
if (fd < 0)
|
||||
{
|
||||
int errcode = errno;
|
||||
fprintf(stderr, "ERROR: bind() failed: %d\n", errcode);
|
||||
ret = -errcode;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Initialize the request */
|
||||
|
||||
thiseq = ++seqno;
|
||||
|
||||
memset(&req, 0, sizeof(req));
|
||||
req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
|
||||
req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT | NLM_F_REQUEST;
|
||||
req.hdr.nlmsg_seq = thiseq;
|
||||
req.hdr.nlmsg_type = RTM_GETNEIGH;
|
||||
req.msg.ndm_family = AF_INET6;
|
||||
|
||||
nsent = send(fd, &req, req.hdr.nlmsg_len, 0);
|
||||
if (nsent < 0)
|
||||
{
|
||||
int errcode = errno;
|
||||
fprintf(stderr, "ERROR: send() failed: %d\n", errcode);
|
||||
ret = -errcode;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Read the response */
|
||||
|
||||
nrecvd = recv(fd, resp, allocsize, 0);
|
||||
if (nrecvd < 0)
|
||||
{
|
||||
int errcode = errno;
|
||||
fprintf(stderr, "ERROR: recv() failed: %d\n", errcode);
|
||||
ret = -errcode;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Verify the data and transfer the Neighbor table data to the caller */
|
||||
|
||||
if (resp->hdr.nlmsg_len < sizeof(struct nlmsghdr) ||
|
||||
resp->hdr.nlmsg_len > nrecvd)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Bad message\n");
|
||||
ret = -EIO;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* The sequence number in the response should match the sequence
|
||||
* number in the request (since we created the socket, this should
|
||||
* always be true).
|
||||
*/
|
||||
|
||||
if (resp->hdr.nlmsg_seq != thiseq)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Bad sequence number in response\n");
|
||||
ret = -EIO;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Copy the Neighbor table data to the caller's buffer */
|
||||
|
||||
paysize = resp->attr.rta_len;
|
||||
if (paysize > maxsize)
|
||||
{
|
||||
paysize = maxsize;
|
||||
}
|
||||
|
||||
memcpy(nbtab, resp->data, paysize);
|
||||
ret = paysize / sizeof(struct neighbor_entry_s);
|
||||
|
||||
errout_with_socket:
|
||||
close(fd);
|
||||
|
||||
errout_with_resp:
|
||||
free(resp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET_IPv6 && CONFIG_NETLINK_ROUTE */
|
Loading…
x
Reference in New Issue
Block a user