2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2014-06-27 17:56:45 +02:00
|
|
|
* net/netdev/netdev_findbyaddr.c
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01: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
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01: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.
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-15 16:57:25 +01:00
|
|
|
#include <stdbool.h>
|
2023-12-29 09:12:11 +01:00
|
|
|
#include <stdint.h>
|
2007-09-17 00:12:04 +02:00
|
|
|
#include <string.h>
|
2007-09-16 19:46:25 +02:00
|
|
|
#include <errno.h>
|
2007-11-22 15:42:52 +01:00
|
|
|
#include <debug.h>
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2015-05-29 23:16:11 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2007-09-16 19:46:25 +02:00
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
#include "arp/arp.h"
|
|
|
|
#include "neighbor/neighbor.h"
|
2015-05-31 16:34:03 +02:00
|
|
|
#include "utils/utils.h"
|
2014-11-23 18:00:22 +01:00
|
|
|
#include "devif/devif.h"
|
2017-08-07 19:50:50 +02:00
|
|
|
#include "inet/inet.h"
|
2014-06-26 21:02:08 +02:00
|
|
|
#include "route/route.h"
|
2015-05-31 16:34:03 +02:00
|
|
|
#include "netdev/netdev.h"
|
2007-09-16 19:46:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2023-12-29 09:12:11 +01:00
|
|
|
* Private Functions
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
/****************************************************************************
|
2023-12-29 09:12:11 +01:00
|
|
|
* Name: netdev_prefixlen_findby_lipv4addr
|
2013-10-05 21:16:18 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a previously registered network device by matching a local address
|
2014-03-21 00:27:59 +01:00
|
|
|
* with the subnet served by the device. Only "up" devices are considered
|
|
|
|
* (since a "down" device has no meaningful address).
|
2013-10-05 21:16:18 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-10-29 23:25:57 +01:00
|
|
|
* lipaddr - Local, IPv4 address assigned to the network device. Or any
|
|
|
|
* IPv4 address on the sub-net served by the network device.
|
2023-12-29 09:12:11 +01:00
|
|
|
* prefixlen - The length of matching prefix. Range: -1(no match) ~ 32
|
2013-10-05 21:16:18 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-10-29 23:25:57 +01:00
|
|
|
* Pointer to driver on success; null on failure
|
2013-10-05 21:16:18 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2023-12-29 09:12:11 +01:00
|
|
|
static FAR struct net_driver_s *
|
|
|
|
netdev_prefixlen_findby_lipv4addr(in_addr_t lipaddr, FAR int8_t *prefixlen)
|
2007-11-22 15:42:52 +01:00
|
|
|
{
|
2014-11-21 21:14:39 +01:00
|
|
|
FAR struct net_driver_s *dev;
|
2023-12-29 09:12:11 +01:00
|
|
|
FAR struct net_driver_s *bestdev = NULL;
|
|
|
|
int8_t bestpref = -1;
|
|
|
|
#ifdef CONFIG_ROUTE_LONGEST_MATCH
|
|
|
|
int8_t len;
|
|
|
|
#endif
|
2014-03-21 00:27:59 +01:00
|
|
|
|
|
|
|
/* Examine each registered network device */
|
2013-10-05 21:16:18 +02:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2013-10-05 21:16:18 +02:00
|
|
|
for (dev = g_netdevices; dev; dev = dev->flink)
|
|
|
|
{
|
2014-03-22 16:24:25 +01:00
|
|
|
/* Is the interface in the "up" state? */
|
2014-03-21 00:27:59 +01:00
|
|
|
|
2021-01-20 08:50:33 +01:00
|
|
|
if ((dev->d_flags & IFF_UP) != 0 &&
|
|
|
|
!net_ipv4addr_cmp(dev->d_ipaddr, INADDR_ANY))
|
2013-10-05 21:16:18 +02:00
|
|
|
{
|
2023-12-29 09:12:11 +01:00
|
|
|
#ifndef CONFIG_ROUTE_LONGEST_MATCH
|
2014-03-22 16:24:25 +01:00
|
|
|
/* Yes.. check for an address match (under the netmask) */
|
2014-03-21 00:27:59 +01:00
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
if (net_ipv4addr_maskcmp(dev->d_ipaddr, lipaddr,
|
2015-01-16 20:01:08 +01:00
|
|
|
dev->d_netmask))
|
2014-03-21 00:27:59 +01:00
|
|
|
{
|
2014-03-22 16:24:25 +01:00
|
|
|
/* Its a match */
|
2014-03-21 00:27:59 +01:00
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
bestdev = dev;
|
|
|
|
bestpref = 32; /* Regard as best (exact) match */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Longest prefix flow: First, check for an exact address match */
|
|
|
|
|
|
|
|
if (net_ipv4addr_cmp(dev->d_ipaddr, lipaddr))
|
|
|
|
{
|
|
|
|
/* It's an exact match */
|
|
|
|
|
|
|
|
bestdev = dev;
|
|
|
|
bestpref = 32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then, check for an address match (under the netmask) */
|
|
|
|
|
|
|
|
if (net_ipv4addr_maskcmp(dev->d_ipaddr, lipaddr,
|
|
|
|
dev->d_netmask))
|
|
|
|
{
|
|
|
|
len = (int8_t)net_ipv4_mask2pref(dev->d_netmask);
|
|
|
|
|
|
|
|
/* Regard current device as better if:
|
|
|
|
* 1. It has longer prefix length
|
|
|
|
* 2. It has the same prefix length but it has target address
|
|
|
|
* in the ARP cache (We don't have other information
|
|
|
|
* for the precedence of networks)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (len > bestpref
|
|
|
|
#ifdef CONFIG_NET_ARP
|
|
|
|
|| (len == bestpref && arp_find(lipaddr, NULL, dev) == OK)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
bestdev = dev;
|
|
|
|
bestpref = len;
|
|
|
|
}
|
2014-03-21 00:27:59 +01:00
|
|
|
}
|
2023-12-29 09:12:11 +01:00
|
|
|
#endif /* CONFIG_ROUTE_LONGEST_MATCH */
|
2013-10-05 21:16:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2023-12-29 09:12:11 +01:00
|
|
|
*prefixlen = bestpref;
|
|
|
|
return bestdev;
|
2007-11-22 15:42:52 +01:00
|
|
|
}
|
2015-01-16 15:51:18 +01:00
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-12-29 09:12:11 +01:00
|
|
|
* Name: netdev_prefixlen_findby_lipv6addr
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a previously registered network device by matching a local address
|
|
|
|
* with the subnet served by the device. Only "up" devices are considered
|
|
|
|
* (since a "down" device has no meaningful address).
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-10-29 23:25:57 +01:00
|
|
|
* lipaddr - Local, IPv6 address assigned to the network device. Or any
|
|
|
|
* IPv6 address on the sub-net served by the network device.
|
2023-12-29 09:12:11 +01:00
|
|
|
* prefixlen - The length of matching prefix. Range: -1(no match) ~ 128
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-10-29 23:25:57 +01:00
|
|
|
* Pointer to driver on success; null on failure
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2023-12-29 09:12:11 +01:00
|
|
|
static FAR struct net_driver_s *
|
|
|
|
netdev_prefixlen_findby_lipv6addr(const net_ipv6addr_t lipaddr,
|
|
|
|
FAR int16_t *prefixlen)
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
|
|
|
FAR struct net_driver_s *dev;
|
2023-12-29 09:12:11 +01:00
|
|
|
FAR struct net_driver_s *bestdev = NULL;
|
|
|
|
int16_t bestpref = -1;
|
|
|
|
#ifdef CONFIG_ROUTE_LONGEST_MATCH
|
|
|
|
FAR struct netdev_ifaddr6_s *ifaddr6;
|
|
|
|
FAR struct neighbor_entry_s *ne;
|
|
|
|
FAR struct net_driver_s *hint;
|
|
|
|
int16_t len;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROUTE_LONGEST_MATCH
|
|
|
|
/* Find a hint from neighbor table in case same prefix length exists on
|
|
|
|
* multiple devices.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ne = neighbor_findentry(lipaddr);
|
|
|
|
hint = ne ? ne->ne_dev : NULL;
|
|
|
|
#endif
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
/* Examine each registered network device */
|
|
|
|
|
|
|
|
for (dev = g_netdevices; dev; dev = dev->flink)
|
|
|
|
{
|
|
|
|
/* Is the interface in the "up" state? */
|
|
|
|
|
2023-08-25 11:37:11 +02:00
|
|
|
if ((dev->d_flags & IFF_UP) != 0 && NETDEV_HAS_V6ADDR(dev))
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
2023-12-29 09:12:11 +01:00
|
|
|
#ifndef CONFIG_ROUTE_LONGEST_MATCH
|
2015-01-16 15:51:18 +01:00
|
|
|
/* Yes.. check for an address match (under the netmask) */
|
|
|
|
|
2023-08-25 11:37:11 +02:00
|
|
|
if (NETDEV_V6ADDR_ONLINK(dev, lipaddr))
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
|
|
|
/* Its a match */
|
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
bestdev = dev;
|
|
|
|
bestpref = 128; /* Regard as best (exact) match */
|
|
|
|
break;
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
2023-12-29 09:12:11 +01:00
|
|
|
#else
|
|
|
|
/* Longest prefix flow: First, check for an exact address match */
|
|
|
|
|
|
|
|
if (NETDEV_IS_MY_V6ADDR(dev, lipaddr))
|
|
|
|
{
|
|
|
|
/* It's an exact match */
|
|
|
|
|
|
|
|
bestdev = dev;
|
|
|
|
bestpref = 128;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then, check for an address match (under the netmask) */
|
|
|
|
|
|
|
|
if ((ifaddr6 = netdev_ipv6_lookup(dev, lipaddr, true)) != NULL)
|
|
|
|
{
|
|
|
|
len = (int16_t)net_ipv6_mask2pref(ifaddr6->mask);
|
|
|
|
|
|
|
|
/* Regard current device as better if:
|
|
|
|
* 1. It has longer prefix length
|
|
|
|
* 2. It has the same prefix length but it has target address
|
|
|
|
* in the neighbor cache (We don't have other information
|
|
|
|
* for the precedence of networks)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (len > bestpref || (len == bestpref && hint == dev))
|
|
|
|
{
|
|
|
|
bestdev = dev;
|
|
|
|
bestpref = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_ROUTE_LONGEST_MATCH */
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2023-12-29 09:12:11 +01:00
|
|
|
*prefixlen = bestpref;
|
|
|
|
return bestdev;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netdev_findby_lipv4addr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a previously registered network device by matching a local address
|
|
|
|
* with the subnet served by the device. Only "up" devices are considered
|
|
|
|
* (since a "down" device has no meaningful address).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lipaddr - Local, IPv4 address assigned to the network device. Or any
|
|
|
|
* IPv4 address on the sub-net served by the network device.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to driver on success; null on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
FAR struct net_driver_s *netdev_findby_lipv4addr(in_addr_t lipaddr)
|
|
|
|
{
|
|
|
|
int8_t prefixlen;
|
|
|
|
return netdev_prefixlen_findby_lipv4addr(lipaddr, &prefixlen);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netdev_findby_lipv6addr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a previously registered network device by matching a local address
|
|
|
|
* with the subnet served by the device. Only "up" devices are considered
|
|
|
|
* (since a "down" device has no meaningful address).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lipaddr - Local, IPv6 address assigned to the network device. Or any
|
|
|
|
* IPv6 address on the sub-net served by the network device.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to driver on success; null on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
FAR struct net_driver_s *netdev_findby_lipv6addr(
|
|
|
|
const net_ipv6addr_t lipaddr)
|
|
|
|
{
|
|
|
|
int16_t prefixlen;
|
|
|
|
return netdev_prefixlen_findby_lipv6addr(lipaddr, &prefixlen);
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
2007-11-22 15:42:52 +01:00
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2018-10-29 19:20:44 +01:00
|
|
|
* Name: netdev_findby_ripv4addr
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-10-29 13:50:47 +01:00
|
|
|
* Find a previously registered network device by matching the remote
|
|
|
|
* IPv4 address that can be reached by the device.
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-10-29 23:25:57 +01:00
|
|
|
* lipaddr - Local, bound address of a connection (used only if ripaddr is
|
|
|
|
* the broadcast address).
|
2014-11-23 18:00:22 +01:00
|
|
|
* ripaddr - Remote address of a connection to use in the lookup
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-10-29 23:25:57 +01:00
|
|
|
* Pointer to driver on success; null on failure
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2018-10-29 19:20:44 +01:00
|
|
|
FAR struct net_driver_s *netdev_findby_ripv4addr(in_addr_t lipaddr,
|
|
|
|
in_addr_t ripaddr)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2023-12-29 09:12:11 +01:00
|
|
|
FAR struct net_driver_s *dev;
|
|
|
|
int8_t prefixlen;
|
2013-10-05 21:16:18 +02:00
|
|
|
#ifdef CONFIG_NET_ROUTE
|
2015-01-16 19:30:18 +01:00
|
|
|
in_addr_t router;
|
2013-10-05 21:16:18 +02:00
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
2014-11-23 18:00:22 +01:00
|
|
|
/* First, check if this is the broadcast IP address */
|
|
|
|
|
2015-05-29 23:16:11 +02:00
|
|
|
if (net_ipv4addr_cmp(ripaddr, INADDR_BROADCAST))
|
2014-11-23 18:00:22 +01:00
|
|
|
{
|
|
|
|
/* Yes.. Check the local, bound address. Is it INADDR_ANY? */
|
2013-10-05 21:16:18 +02:00
|
|
|
|
2015-05-29 23:16:11 +02:00
|
|
|
if (net_ipv4addr_cmp(lipaddr, INADDR_ANY))
|
2014-11-23 18:00:22 +01:00
|
|
|
{
|
|
|
|
/* Yes.. In this case, I think we are supposed to send the
|
2017-08-09 17:20:37 +02:00
|
|
|
* broadcast packet out ALL locally available networks. I am not
|
|
|
|
* sure of that and, in any event, there is nothing we can do
|
2014-11-23 18:00:22 +01:00
|
|
|
* about that here.
|
|
|
|
*/
|
|
|
|
|
2018-08-26 22:40:47 +02:00
|
|
|
return netdev_default();
|
2014-11-23 18:00:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Return the device associated with the local address */
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
return netdev_findby_lipv4addr(lipaddr);
|
2014-11-23 18:00:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
/* Check if the address maps to a locally available network
|
|
|
|
* Note: If longest prefix match is not enabled, prefixlen will be 32 if
|
|
|
|
* matched and it will disable further routing lookup.
|
|
|
|
*/
|
2007-11-22 15:42:52 +01:00
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
dev = netdev_prefixlen_findby_lipv4addr(ripaddr, &prefixlen);
|
2013-10-05 21:16:18 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ROUTE
|
2014-11-17 22:34:50 +01:00
|
|
|
/* If we have a routing table, then perhaps we can find the local
|
2023-12-29 09:12:11 +01:00
|
|
|
* address of a router that can forward packets to the external network
|
|
|
|
* with longer prefix.
|
2013-10-05 21:16:18 +02:00
|
|
|
*/
|
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
ret = net_ipv4_router(ripaddr, &router, prefixlen);
|
2015-01-16 15:51:18 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Success... try to find the network device associated with the local
|
|
|
|
* router address
|
|
|
|
*/
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_lipv4addr(router);
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_ROUTE */
|
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
/* Return the device we found. */
|
|
|
|
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
/* The above lookup will fail if the packet is being sent out of our
|
2018-08-26 22:40:47 +02:00
|
|
|
* out subnet to a router and there is no routing information. Let's
|
|
|
|
* try the default network device.
|
2015-01-16 15:51:18 +01:00
|
|
|
*/
|
|
|
|
|
2018-08-26 22:40:47 +02:00
|
|
|
return netdev_default();
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-10-29 19:20:44 +01:00
|
|
|
* Name: netdev_findby_ripv6addr
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2018-10-29 13:50:47 +01:00
|
|
|
* Find a previously registered network device by matching the remote
|
|
|
|
* IPv6 address that can be reached by the device.
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-10-29 23:25:57 +01:00
|
|
|
* lipaddr - Local, bound address of a connection (used only if ripaddr is
|
|
|
|
* a multicast address).
|
2015-01-16 15:51:18 +01:00
|
|
|
* ripaddr - Remote address of a connection to use in the lookup
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-10-29 23:25:57 +01:00
|
|
|
* Pointer to driver on success; null on failure
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2021-02-19 15:01:46 +01:00
|
|
|
FAR struct net_driver_s *netdev_findby_ripv6addr(
|
|
|
|
const net_ipv6addr_t lipaddr,
|
|
|
|
const net_ipv6addr_t ripaddr)
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
2023-12-29 09:12:11 +01:00
|
|
|
FAR struct net_driver_s *dev;
|
|
|
|
int16_t prefixlen;
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NET_ROUTE
|
2015-01-16 19:30:18 +01:00
|
|
|
net_ipv6addr_t router;
|
2015-01-16 15:51:18 +01:00
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
2018-06-23 18:13:38 +02:00
|
|
|
/* First, check if this is the multicast IP address */
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2018-06-23 18:13:38 +02:00
|
|
|
if (net_is_addr_mcast(ripaddr))
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
2018-06-23 20:53:27 +02:00
|
|
|
/* Yes.. Check the local, bound address. Is it the IPv6 unspecified
|
|
|
|
* address?
|
|
|
|
*/
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2018-06-23 20:53:27 +02:00
|
|
|
if (net_ipv6addr_cmp(lipaddr, g_ipv6_unspecaddr))
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
|
|
|
/* Yes.. In this case, I think we are supposed to send the
|
2017-08-09 17:20:37 +02:00
|
|
|
* broadcast packet out ALL locally available networks. I am not
|
|
|
|
* sure of that and, in any event, there is nothing we can do
|
2015-01-16 15:51:18 +01:00
|
|
|
* about that here.
|
|
|
|
*/
|
|
|
|
|
2018-08-26 22:40:47 +02:00
|
|
|
return netdev_default();
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Return the device associated with the local address */
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
return netdev_findby_lipv6addr(lipaddr);
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
/* Check if the address maps to a locally available network
|
|
|
|
* Note: If longest prefix match is not enabled, prefixlen will be 128 if
|
|
|
|
* matched and it will disable further routing lookup.
|
|
|
|
*/
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
dev = netdev_prefixlen_findby_lipv6addr(ripaddr, &prefixlen);
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ROUTE
|
|
|
|
/* If we have a routing table, then perhaps we can find the local
|
2023-12-29 09:12:11 +01:00
|
|
|
* address of a router that can forward packets to the external network
|
|
|
|
* with longer prefix.
|
2015-01-16 15:51:18 +01:00
|
|
|
*/
|
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
ret = net_ipv6_router(ripaddr, router, prefixlen);
|
2013-10-05 21:16:18 +02:00
|
|
|
if (ret >= 0)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2013-10-05 21:16:18 +02:00
|
|
|
/* Success... try to find the network device associated with the local
|
|
|
|
* router address
|
|
|
|
*/
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_lipv6addr(router);
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
2013-10-05 21:16:18 +02:00
|
|
|
#endif /* CONFIG_NET_ROUTE */
|
|
|
|
|
2023-12-29 09:12:11 +01:00
|
|
|
/* Return the device we found. */
|
|
|
|
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
/* The above lookup will fail if the packet is being sent out of our
|
2018-08-26 22:40:47 +02:00
|
|
|
* out subnet to a router and there is no routing information. Let's
|
|
|
|
* try the default network device.
|
2013-10-05 21:16:18 +02:00
|
|
|
*/
|
|
|
|
|
2018-08-26 22:40:47 +02:00
|
|
|
return netdev_default();
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
2015-01-16 15:51:18 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|