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>
|
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
|
|
|
|
|
|
|
/****************************************************************************
|
2018-10-29 13:50:47 +01:00
|
|
|
* Public Functions
|
2007-09-16 19:46:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-05 21:16:18 +02:00
|
|
|
/****************************************************************************
|
2018-10-29 19:20:44 +01:00
|
|
|
* Name: netdev_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.
|
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
|
2018-10-29 19:20:44 +01:00
|
|
|
FAR struct net_driver_s *netdev_findby_lipv4addr(in_addr_t lipaddr)
|
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
|
|
|
|
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
|
|
|
|
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 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-10-29 19:20:44 +01:00
|
|
|
* Name: netdev_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.
|
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
|
2021-02-19 15:01:46 +01:00
|
|
|
FAR struct net_driver_s *netdev_findby_lipv6addr(
|
|
|
|
const net_ipv6addr_t lipaddr)
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
|
|
|
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) */
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
if (net_ipv6addr_maskcmp(dev->d_ipv6addr, lipaddr,
|
2015-01-16 20:01:08 +01:00
|
|
|
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
|
|
|
/****************************************************************************
|
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
|
|
|
{
|
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 */
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
return netdev_findby_lipv4addr(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
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_lipv4addr(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
|
|
|
|
*/
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_lipv4addr(router);
|
2015-01-16 15:51:18 +01:00
|
|
|
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 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
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
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
return netdev_findby_lipv6addr(lipaddr);
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_lipv6addr(ripaddr);
|
2015-01-16 15:51:18 +01:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_lipv6addr(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 */
|