net/neighbor: neighbor_lookup() checks if the target IP belongs to one of the local network devices.

This commit is contained in:
Xiang Xiao 2018-11-09 14:14:15 -06:00 committed by Gregory Nutt
parent 0a673d78dc
commit 0a6e234962
9 changed files with 114 additions and 22 deletions

View file

@ -326,7 +326,7 @@ int icmpv6_neighbor(const net_ipv6addr_t ipaddr)
* issue. * issue.
*/ */
if (neighbor_findentry(lookup) != NULL) if (neighbor_lookup(lookup, NULL) >= 0)
{ {
/* We have it! Break out with success */ /* We have it! Break out with success */

View file

@ -176,7 +176,7 @@ static inline bool ipfwd_addrchk(FAR struct forward_s *fwd)
{ {
#if defined(CONFIG_NET_ICMPv6_NEIGHBOR) #if defined(CONFIG_NET_ICMPv6_NEIGHBOR)
FAR struct ipv6_hdr_s *ipv6 = (FAR struct ipv6_hdr_s *)fwd->f_iob->io_data; FAR struct ipv6_hdr_s *ipv6 = (FAR struct ipv6_hdr_s *)fwd->f_iob->io_data;
return (neighbor_findentry(ipv6->destipaddr) != NULL); return (neighbor_lookup(ipv6->destipaddr, NULL) >= 0);
#else #else
return true; return true;
#endif #endif

View file

@ -159,15 +159,18 @@ void neighbor_add(FAR struct net_driver_s *dev, FAR net_ipv6addr_t ipaddr,
* *
* Input Parameters: * Input Parameters:
* ipaddr - The IPv6 address to use in the lookup; * ipaddr - The IPv6 address to use in the lookup;
* laddr - Location to return the corresponding link layer address.
* This address may be NULL. In that case, this function may be
* used simply to determine if the link layer address is available.
* *
* Returned Value: * Returned Value:
* A read-only reference to the link layer address in the Neighbor Table is * Zero (OK) if the link layer address is returned. A negated errno value
* returned on success. NULL is returned if there is no matching entry in * is returned on any error.
* the Neighbor Table.
* *
****************************************************************************/ ****************************************************************************/
FAR const struct neighbor_addr_s *neighbor_lookup(const net_ipv6addr_t ipaddr); int neighbor_lookup(FAR const net_ipv6addr_t ipaddr,
FAR struct neighbor_addr_s *laddr);
/**************************************************************************** /****************************************************************************
* Name: neighbor_update * Name: neighbor_update

View file

@ -134,9 +134,9 @@ static const uint8_t g_multicast_ethaddr[3] =
void neighbor_out(FAR struct net_driver_s *dev) void neighbor_out(FAR struct net_driver_s *dev)
{ {
FAR const struct neighbor_addr_s *naddr;
FAR struct eth_hdr_s *eth = ETHBUF; FAR struct eth_hdr_s *eth = ETHBUF;
FAR struct ipv6_hdr_s *ip = IPv6BUF; FAR struct ipv6_hdr_s *ip = IPv6BUF;
struct neighbor_addr_s laddr;
net_ipv6addr_t ipaddr; net_ipv6addr_t ipaddr;
/* Skip sending Neighbor Solicitations when the frame to be transmitted was /* Skip sending Neighbor Solicitations when the frame to be transmitted was
@ -220,8 +220,7 @@ void neighbor_out(FAR struct net_driver_s *dev)
/* Check if we already have this destination address in the Neighbor Table */ /* Check if we already have this destination address in the Neighbor Table */
naddr = neighbor_lookup(ipaddr); if (neighbor_lookup(ipaddr, &laddr) < 0)
if (!naddr)
{ {
ninfo("IPv6 Neighbor solicitation for IPv6\n"); ninfo("IPv6 Neighbor solicitation for IPv6\n");
@ -236,7 +235,7 @@ void neighbor_out(FAR struct net_driver_s *dev)
/* Build an Ethernet header. */ /* Build an Ethernet header. */
memcpy(eth->dest, naddr->u.na_ethernet.ether_addr_octet, ETHER_ADDR_LEN); memcpy(eth->dest, laddr.u.na_ethernet.ether_addr_octet, ETHER_ADDR_LEN);
} }
/* Finish populating the Ethernet header */ /* Finish populating the Ethernet header */

View file

@ -43,12 +43,68 @@
#include <nuttx/config.h> #include <nuttx/config.h>
#include <errno.h>
#include <debug.h> #include <debug.h>
#include <string.h>
#include <nuttx/net/ip.h> #include <nuttx/net/ip.h>
#include "netdev/netdev.h"
#include "neighbor/neighbor.h" #include "neighbor/neighbor.h"
/****************************************************************************
* Private Types
****************************************************************************/
struct neighbor_table_info_s
{
net_ipv6addr_t ni_ipaddr; /* IPv6 address for lookup */
FAR struct neighbor_addr_s *ni_laddr; /* Location to return the link
* layer address */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: neighbor_match
*
* Description:
* This is a callback that checks if the network device has the
* indicated IPv6 address assigned to it.
*
****************************************************************************/
static int neighbor_match(FAR struct net_driver_s *dev, FAR void *arg)
{
FAR struct neighbor_table_info_s *info = arg;
/* Check if the network device has been assigned the IP address of the
* lookup.
*/
if (!net_ipv6addr_cmp(dev->d_ipv6addr, info->ni_ipaddr))
{
return 0;
}
/* Yes.. Return the matching link layer address if the caller of
* neighbor_lookup() provided a non-NULL location.
*/
if (info->ni_laddr != NULL)
{
info->ni_laddr->na_lltype = dev->d_lltype;
info->ni_laddr->na_llsize = netdev_dev_lladdrsize(dev);
memcpy(&info->ni_laddr->u, &dev->d_mac, info->ni_laddr->na_llsize);
}
/* Return success in any event */
return 1;
}
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -61,23 +117,57 @@
* *
* Input Parameters: * Input Parameters:
* ipaddr - The IPv6 address to use in the lookup; * ipaddr - The IPv6 address to use in the lookup;
* lladdr - The location to return the link layer address * laddr - Location to return the corresponding link layer address.
* This address may be NULL. In that case, this function may be
* used simply to determine if the link layer address is available.
* *
* Returned Value: * Returned Value:
* Returns OK if the address was successfully obtain; a negated errno * Zero (OK) if the link layer address is returned. A negated errno value
* value is returned on failure. * is returned on any error.
* *
****************************************************************************/ ****************************************************************************/
FAR const struct neighbor_addr_s *neighbor_lookup(const net_ipv6addr_t ipaddr) int neighbor_lookup(FAR const net_ipv6addr_t ipaddr,
FAR struct neighbor_addr_s *laddr)
{ {
FAR struct neighbor_entry *neighbor; FAR struct neighbor_entry *neighbor;
struct neighbor_table_info_s info;
/* Check if the IPv6 address is already in the neighbor table. */
neighbor = neighbor_findentry(ipaddr); neighbor = neighbor_findentry(ipaddr);
if (neighbor != NULL) if (neighbor != NULL)
{ {
return &neighbor->ne_addr; /* Yes.. return the link layer address if the caller has provided a
* non-NULL address in 'laddr'.
*/
if (laddr != NULL)
{
memcpy(laddr, &neighbor->ne_addr, sizeof(*laddr));
}
/* Return success in any case meaning that a valid link layer
* address mapping is available for the IPv6 address.
*/
return OK;
} }
return NULL; /* No.. check if the IPv6 address is the address assigned to a local
* network device. If so, return a mapping of that IPv6 address
* to the linker layer address assigned to the network device.
*/
net_ipv6addr_copy(info.ni_ipaddr, ipaddr);
info.ni_laddr = laddr;
if (netdev_foreach(neighbor_match, &info) != 0)
{
return OK;
}
/* Not found */
return -ENOENT;
} }

View file

@ -352,9 +352,9 @@ static inline bool psock_send_addrchck(FAR struct tcp_conn_s *conn)
#endif #endif
#if !defined(CONFIG_NET_ICMPv6_NEIGHBOR) #if !defined(CONFIG_NET_ICMPv6_NEIGHBOR)
if (neighbor_findentry(conn->u.ipv6.raddr) != NULL) if (neighbor_lookup(conn->u.ipv6.raddr, NULL) >= 0)
{ {
/* Return true if the address was found in the ARP table */ /* Return true if the address was found in the neighbor table */
return true; return true;
} }

View file

@ -301,9 +301,9 @@ static inline bool psock_send_addrchck(FAR struct tcp_conn_s *conn)
#endif #endif
#if !defined(CONFIG_NET_ICMPv6_NEIGHBOR) #if !defined(CONFIG_NET_ICMPv6_NEIGHBOR)
if (neighbor_findentry(conn->u.ipv6.raddr) != NULL) if (neighbor_lookup(conn->u.ipv6.raddr, NULL) >= 0)
{ {
/* Return true if the address was found in the ARP table */ /* Return true if the address was found in the neighbor table */
return true; return true;
} }

View file

@ -309,7 +309,7 @@ static inline bool sendfile_addrcheck(FAR struct tcp_conn_s *conn)
#endif #endif
{ {
#if !defined(CONFIG_NET_ICMPv6_NEIGHBOR) #if !defined(CONFIG_NET_ICMPv6_NEIGHBOR)
return (neighbor_findentry(conn->u.ipv6.raddr) != NULL); return (neighbor_lookup(conn->u.ipv6.raddr, NULL) >= 0);
#else #else
return true; return true;
#endif #endif

View file

@ -294,7 +294,7 @@ static inline bool sendto_addrcheck(FAR struct udp_conn_s *conn,
#endif #endif
{ {
#if !defined(CONFIG_NET_ICMPv6_NEIGHBOR) #if !defined(CONFIG_NET_ICMPv6_NEIGHBOR)
return (neighbor_findentry(conn->u.ipv6.raddr) != NULL); return (neighbor_lookup(conn->u.ipv6.raddr, NULL) >= 0);
#else #else
return true; return true;
#endif #endif