From dbbabcd33c3da3e0f6743575831e6f790031b657 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 8 Nov 2019 11:15:16 -0600 Subject: [PATCH] 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. --- include/nuttx/net/neighbor.h | 121 +++++++++++++++++++++++++++ net/arp/arp_table.c | 2 +- net/neighbor/Make.defs | 6 +- net/neighbor/neighbor.h | 73 +++++++--------- net/neighbor/neighbor_add.c | 1 + net/neighbor/neighbor_dumpentry.c | 3 +- net/neighbor/neighbor_ethernet_out.c | 1 + net/neighbor/neighbor_findentry.c | 4 +- net/neighbor/neighbor_globals.c | 2 +- net/neighbor/neighbor_lookup.c | 3 +- net/neighbor/neighbor_snapshot.c | 106 +++++++++++++++++++++++ net/neighbor/neighbor_update.c | 2 +- net/netlink/netlink_route.c | 89 +++++++++++++++++++- 13 files changed, 359 insertions(+), 54 deletions(-) create mode 100644 include/nuttx/net/neighbor.h create mode 100644 net/neighbor/neighbor_snapshot.c diff --git a/include/nuttx/net/neighbor.h b/include/nuttx/net/neighbor.h new file mode 100644 index 0000000000..e0476b8912 --- /dev/null +++ b/include/nuttx/net/neighbor.h @@ -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 + * + * 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 + +#include +#include +#include + +#include +#include + +#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 */ diff --git a/net/arp/arp_table.c b/net/arp/arp_table.c index dea0c4e91f..1cad0cfba9 100644 --- a/net/arp/arp_table.c +++ b/net/arp/arp_table.c @@ -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; } diff --git a/net/neighbor/Make.defs b/net/neighbor/Make.defs index 8b3d71a5ca..8986fd4a65 100644 --- a/net/neighbor/Make.defs +++ b/net/neighbor/Make.defs @@ -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 # # 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 diff --git a/net/neighbor/neighbor.h b/net/neighbor/neighbor.h index 64a9a2f586..64c30fc2f4 100644 --- a/net/neighbor/neighbor.h +++ b/net/neighbor/neighbor.h @@ -53,50 +53,10 @@ #include #include #include +#include #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 diff --git a/net/neighbor/neighbor_add.c b/net/neighbor/neighbor_add.c index f242ec7123..ed52927126 100644 --- a/net/neighbor/neighbor_add.c +++ b/net/neighbor/neighbor_add.c @@ -52,6 +52,7 @@ #include #include +#include #include "netdev/netdev.h" #include "neighbor/neighbor.h" diff --git a/net/neighbor/neighbor_dumpentry.c b/net/neighbor/neighbor_dumpentry.c index 01a2800ae9..1e38195a3e 100644 --- a/net/neighbor/neighbor_dumpentry.c +++ b/net/neighbor/neighbor_dumpentry.c @@ -41,6 +41,7 @@ #include #include +#include #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, diff --git a/net/neighbor/neighbor_ethernet_out.c b/net/neighbor/neighbor_ethernet_out.c index 26f0298df6..9456adfbf4 100644 --- a/net/neighbor/neighbor_ethernet_out.c +++ b/net/neighbor/neighbor_ethernet_out.c @@ -45,6 +45,7 @@ #include #include #include +#include #include "route/route.h" #include "icmpv6/icmpv6.h" diff --git a/net/neighbor/neighbor_findentry.c b/net/neighbor/neighbor_findentry.c index 7cd1e3fd76..57d9ca0a32 100644 --- a/net/neighbor/neighbor_findentry.c +++ b/net/neighbor/neighbor_findentry.c @@ -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)) { diff --git a/net/neighbor/neighbor_globals.c b/net/neighbor/neighbor_globals.c index 16434ae687..e8c0587806 100644 --- a/net/neighbor/neighbor_globals.c +++ b/net/neighbor/neighbor_globals.c @@ -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]; diff --git a/net/neighbor/neighbor_lookup.c b/net/neighbor/neighbor_lookup.c index 6330b71ac5..389582c4c5 100644 --- a/net/neighbor/neighbor_lookup.c +++ b/net/neighbor/neighbor_lookup.c @@ -48,6 +48,7 @@ #include #include +#include #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. */ diff --git a/net/neighbor/neighbor_snapshot.c b/net/neighbor/neighbor_snapshot.c new file mode 100644 index 0000000000..a53f0df54e --- /dev/null +++ b/net/neighbor/neighbor_snapshot.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * net/neighbor/neighbor_snapshot.c + * + * Copyright (C) 2019 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include + +#include + +#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 */ + diff --git a/net/neighbor/neighbor_update.c b/net/neighbor/neighbor_update.c index 6bae8b636a..26b78120e1 100644 --- a/net/neighbor/neighbor_update.c +++ b/net/neighbor/neighbor_update.c @@ -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) diff --git a/net/netlink/netlink_route.c b/net/netlink/netlink_route.c index 79bf2b13af..f27b4e397a 100644 --- a/net/netlink/netlink_route.c +++ b/net/netlink/netlink_route.c @@ -49,8 +49,10 @@ #include #include #include +#include #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);