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
|
|
|
*
|
2017-04-15 14:00:42 +02:00
|
|
|
* Copyright (C) 2007-2009, 2014-2015, 2017 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
|
|
|
|
|
2009-12-15 16:57:25 +01:00
|
|
|
#include <stdbool.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
|
|
|
|
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
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: netdev_finddevice_ipv4addr
|
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:
|
2014-11-23 18:00:22 +01:00
|
|
|
* ripaddr - Remote address of a connection to use in the lookup
|
2013-10-05 21:16:18 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to driver on success; null on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
static FAR struct net_driver_s *netdev_finddevice_ipv4addr(in_addr_t ripaddr)
|
2007-11-22 15:42:52 +01:00
|
|
|
{
|
2014-11-21 21:14:39 +01:00
|
|
|
FAR struct net_driver_s *dev;
|
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
|
|
|
|
2014-03-22 16:24:25 +01:00
|
|
|
if ((dev->d_flags & IFF_UP) != 0)
|
2013-10-05 21:16:18 +02:00
|
|
|
{
|
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
|
|
|
|
2015-01-16 20:01:08 +01:00
|
|
|
if (net_ipv4addr_maskcmp(dev->d_ipaddr, ripaddr,
|
|
|
|
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
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2014-03-22 16:24:25 +01:00
|
|
|
return dev;
|
2014-03-21 00:27:59 +01:00
|
|
|
}
|
2013-10-05 21:16:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 00:27:59 +01:00
|
|
|
/* No device with the matching address found */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2013-10-05 21:16:18 +02:00
|
|
|
return NULL;
|
2007-11-22 15:42:52 +01:00
|
|
|
}
|
2015-01-16 15:51:18 +01:00
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: netdev_finddevice_ipv6addr
|
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:
|
2015-01-16 15:51:18 +01:00
|
|
|
* ripaddr - Remote address of a connection to use in the lookup
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to driver on success; null on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
static FAR struct net_driver_s *
|
|
|
|
netdev_finddevice_ipv6addr(const net_ipv6addr_t ripaddr)
|
|
|
|
{
|
|
|
|
FAR struct net_driver_s *dev;
|
|
|
|
|
|
|
|
/* Examine each registered network device */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-01-16 15:51:18 +01:00
|
|
|
for (dev = g_netdevices; dev; dev = dev->flink)
|
|
|
|
{
|
|
|
|
/* Is the interface in the "up" state? */
|
|
|
|
|
|
|
|
if ((dev->d_flags & IFF_UP) != 0)
|
|
|
|
{
|
|
|
|
/* Yes.. check for an address match (under the netmask) */
|
|
|
|
|
2015-01-16 20:01:08 +01:00
|
|
|
if (net_ipv6addr_maskcmp(dev->d_ipv6addr, ripaddr,
|
|
|
|
dev->d_ipv6netmask))
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
|
|
|
/* Its a match */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-16 15:51:18 +01:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No device with the matching address found */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-16 15:51:18 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
2007-11-22 15:42:52 +01:00
|
|
|
|
2007-09-16 19:46:25 +02:00
|
|
|
/****************************************************************************
|
2015-01-19 23:02:56 +01:00
|
|
|
* Public Functions
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: netdev_findby_ipv4addr
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2013-10-05 21:16:18 +02:00
|
|
|
* Find a previously registered network device by matching an arbitrary
|
2015-01-16 15:51:18 +01:00
|
|
|
* IPv4 address.
|
2007-09-16 19:46:25 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-08-08 22:24:12 +02:00
|
|
|
* lipaddr - Local, bound address of a connection.
|
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:
|
|
|
|
* Pointer to driver on success; null on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
FAR struct net_driver_s *netdev_findby_ipv4addr(in_addr_t lipaddr,
|
|
|
|
in_addr_t ripaddr)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2014-06-28 00:48:12 +02:00
|
|
|
struct net_driver_s *dev;
|
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 */
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
return netdev_finddevice_ipv4addr(lipaddr);
|
2014-11-23 18:00:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 17:20:37 +02:00
|
|
|
/* Check if the address maps to a locally available network */
|
2014-11-23 18:00:22 +01:00
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
dev = netdev_finddevice_ipv4addr(ripaddr);
|
2013-10-05 21:16:18 +02:00
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
return dev;
|
|
|
|
}
|
2007-11-22 15:42:52 +01:00
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
/* No.. The address lies on an external network */
|
|
|
|
|
|
|
|
#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
|
2013-10-05 21:16:18 +02:00
|
|
|
* address of a router that can forward packets to the external network.
|
|
|
|
*/
|
|
|
|
|
2015-01-16 19:30:18 +01:00
|
|
|
ret = net_ipv4_router(ripaddr, &router);
|
2015-01-16 15:51:18 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Success... try to find the network device associated with the local
|
|
|
|
* router address
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev = netdev_finddevice_ipv4addr(router);
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_ROUTE */
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: netdev_findby_ipv6addr
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a previously registered network device by matching an arbitrary
|
|
|
|
* IPv6 address.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-08-08 22:24:12 +02:00
|
|
|
* lipaddr - Local, bound address of a connection.
|
2015-01-16 15:51:18 +01:00
|
|
|
* ripaddr - Remote address of a connection to use in the lookup
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to driver on success; null on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-01-16 15:51:18 +01:00
|
|
|
FAR struct net_driver_s *netdev_findby_ipv6addr(const net_ipv6addr_t lipaddr,
|
|
|
|
const net_ipv6addr_t ripaddr)
|
|
|
|
{
|
|
|
|
struct net_driver_s *dev;
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
return netdev_finddevice_ipv6addr(lipaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 17:20:37 +02:00
|
|
|
/* Check if the address maps to a locally available network */
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
dev = netdev_finddevice_ipv6addr(ripaddr);
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. The address lies on an external network */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ROUTE
|
|
|
|
/* If we have a routing table, then perhaps we can find the local
|
|
|
|
* address of a router that can forward packets to the external network.
|
|
|
|
*/
|
|
|
|
|
2015-01-16 19:30:18 +01:00
|
|
|
ret = net_ipv6_router(ripaddr, router);
|
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
|
|
|
|
*/
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
dev = netdev_finddevice_ipv6addr(router);
|
2013-10-05 21:16:18 +02:00
|
|
|
if (dev)
|
2007-09-16 19:46:25 +02:00
|
|
|
{
|
2013-10-05 21:16:18 +02:00
|
|
|
return dev;
|
2007-09-16 19:46:25 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-05 21:16:18 +02:00
|
|
|
#endif /* CONFIG_NET_ROUTE */
|
|
|
|
|
|
|
|
/* 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 */
|
2007-09-16 19:46:25 +02:00
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
|