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.
*/
if (neighbor_findentry(lookup) != NULL)
if (neighbor_lookup(lookup, NULL) >= 0)
{
/* 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)
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
return true;
#endif

View File

@ -159,15 +159,18 @@ void neighbor_add(FAR struct net_driver_s *dev, FAR net_ipv6addr_t ipaddr,
*
* Input Parameters:
* 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:
* A read-only reference to the link layer address in the Neighbor Table is
* returned on success. NULL is returned if there is no matching entry in
* the Neighbor Table.
* Zero (OK) if the link layer address is returned. A negated errno value
* 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);
/****************************************************************************
* 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)
{
FAR const struct neighbor_addr_s *naddr;
FAR struct eth_hdr_s *eth = ETHBUF;
FAR struct ipv6_hdr_s *ip = IPv6BUF;
struct neighbor_addr_s laddr;
net_ipv6addr_t ipaddr;
/* 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 */
naddr = neighbor_lookup(ipaddr);
if (!naddr)
if (neighbor_lookup(ipaddr, &laddr) < 0)
{
ninfo("IPv6 Neighbor solicitation for IPv6\n");
@ -236,7 +235,7 @@ void neighbor_out(FAR struct net_driver_s *dev)
/* 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 */

View File

@ -43,12 +43,68 @@
#include <nuttx/config.h>
#include <errno.h>
#include <debug.h>
#include <string.h>
#include <nuttx/net/ip.h>
#include "netdev/netdev.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
****************************************************************************/
@ -61,23 +117,57 @@
*
* Input Parameters:
* 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:
* Returns OK if the address was successfully obtain; a negated errno
* value is returned on failure.
* Zero (OK) if the link layer address is returned. A negated errno value
* 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;
struct neighbor_table_info_s info;
/* Check if the IPv6 address is already in the neighbor table. */
neighbor = neighbor_findentry(ipaddr);
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
#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;
}

View File

@ -301,9 +301,9 @@ static inline bool psock_send_addrchck(FAR struct tcp_conn_s *conn)
#endif
#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;
}

View File

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

View File

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