From fd1405d9761e2b3153ca5a53da9bb05ac588ea99 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 26 Aug 2018 14:40:47 -0600 Subject: [PATCH] netdev_findby_ipv[4|6]addr return netdev_default() as last resort but don't return loopback device if another network device is in the UP state. --- net/netdev/netdev_default.c | 17 +++++++++++------ net/netdev/netdev_findbyaddr.c | 32 ++++++++------------------------ 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/net/netdev/netdev_default.c b/net/netdev/netdev_default.c index f177d0dfb8d..55d8a2bf1ee 100644 --- a/net/netdev/netdev_default.c +++ b/net/netdev/netdev_default.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/netdev/netdev_default.c * - * Copyright (C) 2014-2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2014-2015, 2018 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -72,6 +72,7 @@ FAR struct net_driver_s *netdev_default(void) { + FAR struct net_driver_s *ret = NULL; FAR struct net_driver_s *dev; /* Examine each registered network device */ @@ -83,17 +84,21 @@ FAR struct net_driver_s *netdev_default(void) if ((dev->d_flags & IFF_UP) != 0) { - /* Return a reference to the first device we find that is in the UP - * state. + /* Return a reference to the first device that we find in the UP + * state (but not the loopback device unless it is the only + * device). */ - net_unlock(); - return dev; + ret = dev; + if (dev->d_lltype != NET_LL_LOOPBACK) + { + break; + } } } net_unlock(); - return NULL; + return ret; } #endif /* CONFIG_NET */ diff --git a/net/netdev/netdev_findbyaddr.c b/net/netdev/netdev_findbyaddr.c index 4851e2f9856..366764b6458 100644 --- a/net/netdev/netdev_findbyaddr.c +++ b/net/netdev/netdev_findbyaddr.c @@ -203,13 +203,9 @@ FAR struct net_driver_s *netdev_findby_ipv4addr(in_addr_t lipaddr, * broadcast packet out ALL locally available networks. I am not * sure of that and, in any event, there is nothing we can do * about that here. - * - * REVISIT: For now, arbitrarily return the first network - * interface in the list of network devices. The broadcast - * will be sent on that device only. */ - return g_netdevices; + return netdev_default(); } else { @@ -250,15 +246,11 @@ FAR struct net_driver_s *netdev_findby_ipv4addr(in_addr_t lipaddr, #endif /* CONFIG_NET_ROUTE */ /* The above lookup will fail if the packet is being sent out of our - * out subnet to a router and there is no routing information. + * out subnet to a router and there is no routing information. Let's + * try the default network device. */ - /* If we will did not find the network device, then we might as well fail - * because we are not configured properly to determine the route to the - * destination. - */ - - return dev; + return netdev_default(); } #endif /* CONFIG_NET_IPv4 */ @@ -302,13 +294,9 @@ FAR struct net_driver_s *netdev_findby_ipv6addr(const net_ipv6addr_t lipaddr, * broadcast packet out ALL locally available networks. I am not * sure of that and, in any event, there is nothing we can do * about that here. - * - * REVISIT: For now, arbitrarily return the first network - * interface in the list of network devices. The broadcast - * will be sent on that device only. */ - return g_netdevices; + return netdev_default(); } else { @@ -349,15 +337,11 @@ FAR struct net_driver_s *netdev_findby_ipv6addr(const net_ipv6addr_t lipaddr, #endif /* CONFIG_NET_ROUTE */ /* The above lookup will fail if the packet is being sent out of our - * out subnet to a router and there is no routing information. + * out subnet to a router and there is no routing information. Let's + * try the default network device. */ - /* If we will did not find the network device, then we might as well fail - * because we are not configured properly to determine the route to the - * destination. - */ - - return dev; + return netdev_default(); } #endif /* CONFIG_NET_IPv6 */