2015-01-20 19:31:56 +01:00
|
|
|
/****************************************************************************
|
2018-11-08 01:21:21 +01:00
|
|
|
* net/neighbor/neighbor_lookup.c
|
2015-01-20 19:31:56 +01:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-09-18 00:12:48 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-01-20 19:31:56 +01:00
|
|
|
*
|
2021-09-18 00:12:48 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-20 19:31:56 +01:00
|
|
|
*
|
2021-09-18 00:12:48 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-01-20 19:31:56 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2018-11-09 21:14:15 +01:00
|
|
|
#include <errno.h>
|
2015-01-20 19:31:56 +01:00
|
|
|
#include <debug.h>
|
2018-11-09 21:14:15 +01:00
|
|
|
#include <string.h>
|
2015-01-20 19:31:56 +01:00
|
|
|
|
|
|
|
#include <nuttx/net/ip.h>
|
2019-11-08 18:15:16 +01:00
|
|
|
#include <nuttx/net/neighbor.h>
|
2015-01-20 19:31:56 +01:00
|
|
|
|
2018-11-09 21:14:15 +01:00
|
|
|
#include "netdev/netdev.h"
|
2015-01-20 19:31:56 +01:00
|
|
|
#include "neighbor/neighbor.h"
|
|
|
|
|
2018-11-09 21:14:15 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-08-25 11:37:11 +02:00
|
|
|
if (!NETDEV_IS_MY_V6ADDR(dev, info->ni_ipaddr))
|
2018-11-09 21:14:15 +01:00
|
|
|
{
|
|
|
|
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;
|
2018-11-09 23:05:11 +01:00
|
|
|
info->ni_laddr->na_llsize = netdev_lladdrsize(dev);
|
2018-11-09 21:14:15 +01:00
|
|
|
memcpy(&info->ni_laddr->u, &dev->d_mac, info->ni_laddr->na_llsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return success in any event */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-01-20 19:31:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: neighbor_lookup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find an entry in the Neighbor Table and return its link layer address.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ipaddr - The IPv6 address to use in the lookup;
|
2018-11-09 21:14:15 +01:00
|
|
|
* laddr - Location to return the corresponding link layer address.
|
|
|
|
* This address may be NULL. In that case, this function may be
|
2021-02-19 15:01:46 +01:00
|
|
|
* used simply to determine if the link layer address is
|
|
|
|
* available.
|
2015-01-20 19:31:56 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-11-09 21:14:15 +01:00
|
|
|
* Zero (OK) if the link layer address is returned. A negated errno value
|
|
|
|
* is returned on any error.
|
2015-01-20 19:31:56 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-09 21:14:15 +01:00
|
|
|
int neighbor_lookup(FAR const net_ipv6addr_t ipaddr,
|
|
|
|
FAR struct neighbor_addr_s *laddr)
|
2015-01-20 19:31:56 +01:00
|
|
|
{
|
2019-11-08 18:15:16 +01:00
|
|
|
FAR struct neighbor_entry_s *neighbor;
|
2018-11-09 21:14:15 +01:00
|
|
|
struct neighbor_table_info_s info;
|
|
|
|
|
|
|
|
/* Check if the IPv6 address is already in the neighbor table. */
|
2015-01-20 19:31:56 +01:00
|
|
|
|
|
|
|
neighbor = neighbor_findentry(ipaddr);
|
|
|
|
if (neighbor != NULL)
|
|
|
|
{
|
2018-11-09 21:14:15 +01:00
|
|
|
/* 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;
|
2015-01-20 19:31:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-09 21:14:15 +01:00
|
|
|
/* 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;
|
2015-01-20 19:31:56 +01:00
|
|
|
}
|