Squashed commit of the following:

net/netlink/netlink_route.c:  Add Netlink socket NETLINK_ROUTE support for getting a snopshot of the Neighbor table.

    net/neighbor/neighbor_snapshot.c:  Add neighbor_snapshot() that will eventually be used by the Netlink sockets.  Also fixed naming violation 'struct neighbor_entry' -> 'struct neighbor_entry_s'.

    include/nuttx/net/neighbor.h:  Expose format of the IPv6 neighbor table for use with Netlink sockets.
This commit is contained in:
Gregory Nutt 2019-11-08 11:15:16 -06:00
parent 2213904cfd
commit dbbabcd33c
13 changed files with 359 additions and 54 deletions

View File

@ -0,0 +1,121 @@
/****************************************************************************
* include/nuttx/net/neighbor.h
* Definitions for use with IPv6 Neighor Table
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory nutt <gnutt@nuttx.org>
*
* Includes some definitions that a compatible with the LGPL GNU C Library
* header file of the same name.
*
* 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_NET_NEIGHBOR_H
#define __INCLUDE_NUTTX_NET_NEIGHBOR_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <net/ethernet.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/netdev.h>
#ifdef CONFIG_NET_IPv6
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_NET_IPv6_NCONF_ENTRIES
# define CONFIG_NET_IPv6_NCONF_ENTRIES 8
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/* Describes the link layer address */
struct neighbor_addr_s
{
uint8_t na_lltype; /* See enum net_lltype_e */
uint8_t na_llsize; /* Link layer header size */
union
{
#ifdef CONFIG_NET_ETHERNET
struct ether_addr na_ethernet;
#endif
#ifdef CONFIG_NET_6LOWPAN
struct netdev_maxaddr_s na_sixlowpan;
#endif
} u;
};
/* This structure describes on entry in the neighbor table. This is intended
* for internal use within the Neighbor implementation.
*
* An unused entry can be detected by all zero entries, particularly the
* ne_ipaddr (the IPv6 unspecified address) which should never be zero for a
* real neighbor.
*/
struct neighbor_entry_s
{
net_ipv6addr_t ne_ipaddr; /* IPv6 address of the Neighbor */
struct neighbor_addr_s ne_addr; /* Link layer address of the Neighbor */
clock_t ne_time; /* For aging, units of tick */
};
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_NET_LOOPBACK */
#endif /* __INCLUDE_NUTTX_NET_NEIGHBOR_H */

View File

@ -437,7 +437,7 @@ unsigned int arp_snapshot(FAR struct arp_entry_s *snapshot,
}
}
/* Not found */
/* Return the number of entries copied into the user buffer */
return ncopied;
}

View File

@ -1,7 +1,7 @@
############################################################################
# net/neighbor/Make.defs
#
# Copyright (C) 2014 Gregory Nutt. All rights reserved.
# Copyright (C) 2014, 2019 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
@ -54,6 +54,10 @@ ifeq ($(CONFIG_DEBUG_NET_INFO),y)
NET_CSRCS += neighbor_dumpentry.c
endif
ifeq ($(CONFIG_NETLINK_ROUTE),y)
NET_CSRCS += neighbor_snapshot.c
endif
# Include utility build support
DEPPATH += --dep-path neighbor

View File

@ -53,50 +53,10 @@
#include <nuttx/net/ip.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/sixlowpan.h>
#include <nuttx/net/neighbor.h>
#ifdef CONFIG_NET_IPv6
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_NET_IPv6_NCONF_ENTRIES
# define CONFIG_NET_IPv6_NCONF_ENTRIES 8
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/* Describes the link layer address */
struct neighbor_addr_s
{
uint8_t na_lltype;
uint8_t na_llsize;
union
{
#ifdef CONFIG_NET_ETHERNET
struct ether_addr na_ethernet;
#endif
#ifdef CONFIG_NET_6LOWPAN
struct netdev_maxaddr_s na_sixlowpan;
#endif
} u;
};
/* This structure describes on entry in the neighbor table. This is intended
* for internal use within the Neighbor implementation.
*/
struct neighbor_entry
{
net_ipv6addr_t ne_ipaddr; /* IPv6 address of the Neighbor */
struct neighbor_addr_s ne_addr; /* Link layer address of the Neighbor */
clock_t ne_time; /* For aging, units of tick */
};
/****************************************************************************
* Public Data
****************************************************************************/
@ -105,7 +65,7 @@ struct neighbor_entry
* this table.
*/
extern struct neighbor_entry g_neighbors[CONFIG_NET_IPv6_NCONF_ENTRIES];
extern struct neighbor_entry_s g_neighbors[CONFIG_NET_IPv6_NCONF_ENTRIES];
/****************************************************************************
* Public Function Prototypes
@ -129,7 +89,7 @@ struct net_driver_s; /* Forward reference */
*
****************************************************************************/
FAR struct neighbor_entry *neighbor_findentry(const net_ipv6addr_t ipaddr);
FAR struct neighbor_entry_s *neighbor_findentry(const net_ipv6addr_t ipaddr);
/****************************************************************************
* Name: neighbor_add
@ -221,6 +181,31 @@ void neighbor_update(const net_ipv6addr_t ipaddr);
void neighbor_ethernet_out(FAR struct net_driver_s *dev);
#endif
/****************************************************************************
* Name: neighbor_snapshot
*
* Description:
* Take a snapshot of the current state of the Neighbor table.
*
* Input Parameters:
* snapshot - Location to return the Neighbor table copy
* nentries - The size of the user provided 'dest' in entries, each of
* size sizeof(struct arp_entry_s)
*
* Returned Value:
* On success, the number of entries actually copied is returned. Unused
* entries are not returned.
*
* Assumptions
* The network is locked to assure exclusive access to the ARP table
*
****************************************************************************/
#ifdef CONFIG_NETLINK_ROUTE
unsigned int neighbor_snapshot(FAR struct neighbor_entry_s *snapshot,
unsigned int nentries);
#endif
/****************************************************************************
* Name: neighbor_dumpentry
*
@ -238,7 +223,7 @@ void neighbor_ethernet_out(FAR struct net_driver_s *dev);
#ifdef CONFIG_DEBUG_NET_INFO
void neighbor_dumpentry(FAR const char *msg,
FAR struct neighbor_entry *neighbor);
FAR struct neighbor_entry_s *neighbor);
#else
# define neighbor_dumpentry(msg,neighbor)
#endif

View File

@ -52,6 +52,7 @@
#include <nuttx/net/net.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/neighbor.h>
#include "netdev/netdev.h"
#include "neighbor/neighbor.h"

View File

@ -41,6 +41,7 @@
#include <debug.h>
#include <nuttx/net/net.h>
#include <nuttx/net/neighbor.h>
#include "neighbor/neighbor.h"
@ -130,7 +131,7 @@ static void neighbor_dump_address(FAR const void *buf, unsigned int buflen)
****************************************************************************/
void neighbor_dumpentry(FAR const char *msg,
FAR struct neighbor_entry *neighbor)
FAR struct neighbor_entry_s *neighbor)
{
ninfo("%s: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
msg,

View File

@ -45,6 +45,7 @@
#include <nuttx/net/ethernet.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/neighbor.h>
#include "route/route.h"
#include "icmpv6/icmpv6.h"

View File

@ -68,13 +68,13 @@
*
****************************************************************************/
FAR struct neighbor_entry *neighbor_findentry(const net_ipv6addr_t ipaddr)
FAR struct neighbor_entry_s *neighbor_findentry(const net_ipv6addr_t ipaddr)
{
int i;
for (i = 0; i < CONFIG_NET_IPv6_NCONF_ENTRIES; ++i)
{
FAR struct neighbor_entry *neighbor = &g_neighbors[i];
FAR struct neighbor_entry_s *neighbor = &g_neighbors[i];
if (net_ipv6addr_cmp(neighbor->ne_ipaddr, ipaddr))
{

View File

@ -54,5 +54,5 @@
* this table.
*/
struct neighbor_entry g_neighbors[CONFIG_NET_IPv6_NCONF_ENTRIES];
struct neighbor_entry_s g_neighbors[CONFIG_NET_IPv6_NCONF_ENTRIES];

View File

@ -48,6 +48,7 @@
#include <string.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/neighbor.h>
#include "netdev/netdev.h"
#include "neighbor/neighbor.h"
@ -130,7 +131,7 @@ static int neighbor_match(FAR struct net_driver_s *dev, FAR void *arg)
int neighbor_lookup(FAR const net_ipv6addr_t ipaddr,
FAR struct neighbor_addr_s *laddr)
{
FAR struct neighbor_entry *neighbor;
FAR struct neighbor_entry_s *neighbor;
struct neighbor_table_info_s info;
/* Check if the IPv6 address is already in the neighbor table. */

View File

@ -0,0 +1,106 @@
/****************************************************************************
* net/neighbor/neighbor_snapshot.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 of the Institute 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 INSTITUTE 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 INSTITUTE 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 <string.h>
#include <debug.h>
#include <nuttx/net/ip.h>
#include "inet/inet.h"
#include "neighbor/neighbor.h"
#ifdef CONFIG_NETLINK_ROUTE
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: neighbor_snapshot
*
* Description:
* Take a snapshot of the current state of the Neighbor table.
*
* Input Parameters:
* snapshot - Location to return the Neighbor table copy
* nentries - The size of the user provided 'dest' in entries, each of
* size sizeof(struct arp_entry_s)
*
* Returned Value:
* On success, the number of entries actually copied is returned. Unused
* entries are not returned.
*
* Assumptions
* The network is locked to assure exclusive access to the ARP table
*
****************************************************************************/
unsigned int neighbor_snapshot(FAR struct neighbor_entry_s *snapshot,
unsigned int nentries)
{
unsigned int ncopied;
int i;
/* Copy all non-empty entries in the Neighbor table. */
for (i = 0, ncopied = 0;
nentries > ncopied && i < CONFIG_NET_IPv6_NCONF_ENTRIES;
i++)
{
FAR struct neighbor_entry_s *neighbor = &g_neighbors[i];
/* An unused entry table entry will be nullified. In particularly,
* the Neighbor IP address will be all zero (i.e., the unspecified
* IPv6 address).
*/
if (!net_ipv6addr_cmp(neighbor->ne_ipaddr, g_ipv6_unspecaddr))
{
memcpy(&snapshot[ncopied], neighbor, sizeof(struct neighbor_entry_s));
ncopied++;
}
}
/* Return the number of entries copied into the user buffer */
return ncopied;
}
#endif /* CONFIG_NETLINK_ROUTE */

View File

@ -67,7 +67,7 @@
void neighbor_update(const net_ipv6addr_t ipaddr)
{
struct neighbor_entry *neighbor;
struct neighbor_entry_s *neighbor;
neighbor = neighbor_findentry(ipaddr);
if (neighbor != NULL)

View File

@ -49,8 +49,10 @@
#include <nuttx/kmalloc.h>
#include <nuttx/net/net.h>
#include <nuttx/net/arp.h>
#include <nuttx/net/neighbor.h>
#include "arp/arp.h"
#include "neighbor/neighbor.h"
#include "netlink/netlink.h"
#ifdef CONFIG_NETLINK_ROUTE
@ -128,6 +130,7 @@ ssize_t netlink_route_sendto(FAR struct socket *psock,
req = (FAR const struct nlroute_sendto_request_s *)nlmsg;
#if defined(CONFIG_NET_ARP) || defined(CONFIG_NET_IPv6)
#ifdef CONFIG_NET_ARP
/* REVISIT: Currently, the only operation supported is retrieving the
* ARP table in its entirety.
@ -147,7 +150,7 @@ ssize_t netlink_route_sendto(FAR struct socket *psock,
* the number of valid entries in the ARP table.
*/
tabsize = CONFIG_NET_ARPTAB_SIZE * sizeof( struct arp_entry_s);
tabsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arp_entry_s);
rspsize = SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(tabsize);
allocsize = SIZEOF_NLROUTE_RECVFROM_RSPLIST_S(tabsize);
@ -182,7 +185,7 @@ ssize_t netlink_route_sendto(FAR struct socket *psock,
{
FAR struct nlroute_recvfrom_rsplist_s *newentry;
tabsize = ncopied * sizeof( struct arp_entry_s);
tabsize = ncopied * sizeof(struct arp_entry_s);
rspsize = SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(tabsize);
allocsize = SIZEOF_NLROUTE_RECVFROM_RSPLIST_S(tabsize);
@ -203,6 +206,88 @@ ssize_t netlink_route_sendto(FAR struct socket *psock,
netlink_add_response(psock, (FAR struct netlink_response_s *)entry);
return len;
}
#endif
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_ARP
else
#endif
/* REVISIT: Currently, the only operation supported is retrieving the
* Neighbor table in its entirety.
*/
if (req->hdr.nlmsg_type == RTM_GETNEIGH && req->msg.ndm_family == AF_INET6)
{
FAR struct nlroute_recvfrom_rsplist_s *entry;
unsigned int ncopied;
size_t tabsize;
size_t rspsize;
size_t allocsize;
/* Preallocate memory to hold the maximum sized Neighbor table
* REVISIT: This is probably excessively large and could cause false
* memory out conditions. A better approach would be to actually count
* the number of valid entries in the Neighbor table.
*/
tabsize = CONFIG_NET_IPv6_NCONF_ENTRIES *
sizeof(struct neighbor_entry_s);
rspsize = SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(tabsize);
allocsize = SIZEOF_NLROUTE_RECVFROM_RSPLIST_S(tabsize);
entry = (FAR struct nlroute_recvfrom_rsplist_s *)kmm_malloc(allocsize);
if (entry == NULL)
{
return -ENOMEM;
}
/* Populate the entry */
memcpy(&entry->payload.hdr, &req->hdr, sizeof(struct nlmsghdr));
entry->payload.hdr.nlmsg_len = rspsize;
memcpy(&entry->payload.msg, &req->msg, sizeof(struct ndmsg));
entry->payload.attr.rta_len = tabsize;
entry->payload.attr.rta_type = 0;
/* Lock the network so that the Neighbor table will be stable, then
* copy the Neighbor table into the allocated memory.
*/
net_lock();
ncopied = neighbor_snapshot((FAR struct neighbor_entry_s *)entry->payload.data,
CONFIG_NET_IPv6_NCONF_ENTRIES);
net_unlock();
/* Now we have the real number of valid entries in the Neighbor table
* and we can trim the allocation.
*/
if (ncopied < CONFIG_NET_IPv6_NCONF_ENTRIES)
{
FAR struct nlroute_recvfrom_rsplist_s *newentry;
tabsize = ncopied * sizeof(struct neighbor_entry_s);
rspsize = SIZEOF_NLROUTE_RECVFROM_RESPONSE_S(tabsize);
allocsize = SIZEOF_NLROUTE_RECVFROM_RSPLIST_S(tabsize);
newentry = (FAR struct nlroute_recvfrom_rsplist_s *)
kmm_realloc(entry, allocsize);
if (newentry != NULL)
{
entry = newentry;
}
entry->payload.hdr.nlmsg_len = rspsize;
entry->payload.attr.rta_len = tabsize;
}
/* Finally, add the data to the list of pending responses */
netlink_add_response(psock, (FAR struct netlink_response_s *)entry);
return len;
}
#endif
#else
UNUSED(req);