This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new f44e1b92180 net: Report a failed address resolution as EHOSTUNREACH.
f44e1b92180 is described below

commit f44e1b9218055d198d28df1f660ecdf94509bf93
Author: raiden00pl <[email protected]>
AuthorDate: Sat Oct 24 19:10:17 2026 +0200

    net: Report a failed address resolution as EHOSTUNREACH.
    
    The callers of arp_send() and icmpv6_neighbor() turned every error into
    ENETUNREACH, so a host on the local network that never answers ARP or
    Neighbor Solicitation was reported as having no route.  Return
    EHOSTUNREACH from both functions when the resolution fails and
    ENETUNREACH when no device or router reaches the address, and pass the
    error on to the socket calls.
    
    POSIX connect(): "[ENETUNREACH] No route to the network is present."
    "[EHOSTUNREACH] The destination host cannot be reached (probably
    because the host is down or a remote router cannot reach it)."
    https://pubs.opengroup.org/onlinepubs/9799919799/functions/connect.html
    
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: raiden00pl <[email protected]>
---
 net/arp/arp.h                   |  6 ++++--
 net/arp/arp_send.c              | 28 +++++++++++++++++++++++-----
 net/icmp/icmp_sendmsg.c         |  1 -
 net/icmpv6/icmpv6.h             |  5 +++--
 net/icmpv6/icmpv6_neighbor.c    | 18 +++++++++++++-----
 net/icmpv6/icmpv6_sendmsg.c     |  1 -
 net/tcp/tcp_conn.c              |  2 +-
 net/tcp/tcp_send_buffered.c     |  1 -
 net/tcp/tcp_send_unbuffered.c   |  1 -
 net/tcp/tcp_sendfile.c          |  2 +-
 net/udp/udp_sendto_buffered.c   |  2 +-
 net/udp/udp_sendto_unbuffered.c |  2 +-
 12 files changed, 47 insertions(+), 22 deletions(-)

diff --git a/net/arp/arp.h b/net/arp/arp.h
index d1fac3c98e9..d36de6d691b 100644
--- a/net/arp/arp.h
+++ b/net/arp/arp.h
@@ -271,8 +271,10 @@ void arp_out(FAR struct net_driver_s *dev);
  *   Zero (OK) is returned on success and the IP address mapping can now be
  *   found in the ARP table.  On error a negated errno value is returned:
  *
- *     -ETIMEDOUT:    The number or retry counts has been exceed.
- *     -EHOSTUNREACH: Could not find a route to the host
+ *     -ENETUNREACH:  No device or router reaches the address, or the
+ *                    device went down
+ *     -EHOSTUNREACH: The address resolution failed
+ *     -ENOMEM:       No callback available
  *
  * Assumptions:
  *   This function is called from the normal tasking context.
diff --git a/net/arp/arp_send.c b/net/arp/arp_send.c
index 1ca996e9669..b776385448c 100644
--- a/net/arp/arp_send.c
+++ b/net/arp/arp_send.c
@@ -167,8 +167,10 @@ static uint32_t arp_send_eventhandler(FAR struct 
net_driver_s *dev,
  *   Zero (OK) is returned on success and the IP address mapping can now be
  *   found in the ARP table.  On error a negated errno value is returned:
  *
- *     -ETIMEDOUT:    The number or retry counts has been exceed.
- *     -EHOSTUNREACH: Could not find a route to the host
+ *     -ENETUNREACH:  No device or router reaches the address, or the
+ *                    device went down
+ *     -EHOSTUNREACH: The address resolution failed
+ *     -ENOMEM:       No callback available
  *
  * Assumptions:
  *   This function is called from the normal tasking context.
@@ -216,7 +218,7 @@ int arp_send(in_addr_t ipaddr)
   if (!dev)
     {
       nerr("ERROR: Unreachable: %08lx\n", (unsigned long)ipaddr);
-      return -EHOSTUNREACH;
+      return -ENETUNREACH;
     }
 
   /* ARP support is only built if the Ethernet link layer is supported.
@@ -256,7 +258,7 @@ int arp_send(in_addr_t ipaddr)
       ipaddr = dripaddr;
       if (ipaddr == INADDR_ANY)
         {
-          return -EHOSTUNREACH;
+          return -ENETUNREACH;
         }
     }
 
@@ -312,7 +314,7 @@ int arp_send(in_addr_t ipaddr)
 
       netdev_lock(dev);
       ret = arp_find(ipaddr, NULL, dev);
-      if (ret >= 0 || ret == -ENETUNREACH)
+      if (ret >= 0)
         {
           /* We have it! Break out with ret value */
 
@@ -320,6 +322,15 @@ int arp_send(in_addr_t ipaddr)
           break;
         }
 
+      if (ret == -ENETUNREACH)
+        {
+          /* The resolution of the address failed a short time ago */
+
+          netdev_unlock(dev);
+          ret = -EHOSTUNREACH;
+          break;
+        }
+
       /* Set up the ARP response wait BEFORE we send the ARP request */
 
       arp_wait_setup(ipaddr, &notify);
@@ -434,6 +445,13 @@ timeout:
            ip4_addr3(ipaddr), ip4_addr4(ipaddr));
     }
 
+  /* No reply to any of the requests: the address resolution failed */
+
+  if (ret == -ETIMEDOUT)
+    {
+      ret = -EHOSTUNREACH;
+    }
+
   nxsem_destroy(&state.snd_sem);
   arp_callback_free(dev, state.snd_cb);
   return ret;
diff --git a/net/icmp/icmp_sendmsg.c b/net/icmp/icmp_sendmsg.c
index 39b77a83491..73db924eb11 100644
--- a/net/icmp/icmp_sendmsg.c
+++ b/net/icmp/icmp_sendmsg.c
@@ -386,7 +386,6 @@ ssize_t icmp_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
   if (ret < 0)
     {
       nerr("ERROR: Not reachable\n");
-      ret = -ENETUNREACH;
       goto errout;
     }
 #endif
diff --git a/net/icmpv6/icmpv6.h b/net/icmpv6/icmpv6.h
index b08cb0791c9..babaf170073 100644
--- a/net/icmpv6/icmpv6.h
+++ b/net/icmpv6/icmpv6.h
@@ -234,8 +234,9 @@ void icmpv6_input(FAR struct net_driver_s *dev, unsigned 
int iplen);
  *   found in the Neighbor Table.
  *   On error a negated errno value is returned:
  *
- *     -ETIMEDOUT:    The number or retry counts has been exceed.
- *     -EHOSTUNREACH: Could not find a route to the host
+ *     -ENETUNREACH:  No device or router reaches the address,
+ *     -EHOSTUNREACH: The address resolution failed
+ *     -ENOMEM:       No callback available
  *
  * Assumptions:
  *   This function is called from the normal tasking context.
diff --git a/net/icmpv6/icmpv6_neighbor.c b/net/icmpv6/icmpv6_neighbor.c
index 3e2402ca12c..01ef93288a8 100644
--- a/net/icmpv6/icmpv6_neighbor.c
+++ b/net/icmpv6/icmpv6_neighbor.c
@@ -173,8 +173,9 @@ static uint32_t icmpv6_neighbor_eventhandler(FAR struct 
net_driver_s *dev,
  *   found in the Neighbor Table.  On error a negated errno value is
  *   returned:
  *
- *     -ETIMEDOUT:    The number or retry counts has been exceed.
- *     -EHOSTUNREACH: Could not find a route to the host
+ *     -ENETUNREACH:  No device or router reaches the address,
+ *     -EHOSTUNREACH: The address resolution failed
+ *     -ENOMEM:       No callback available
  *
  * Assumptions:
  *   This function is called from the normal tasking context.
@@ -215,7 +216,7 @@ int icmpv6_neighbor(FAR struct net_driver_s *dev,
   if (!dev)
     {
       nerr("ERROR: Unreachable: %08lx\n", (unsigned long)ipaddr);
-      ret = -EHOSTUNREACH;
+      ret = -ENETUNREACH;
       goto errout;
     }
 
@@ -261,7 +262,7 @@ int icmpv6_neighbor(FAR struct net_driver_s *dev,
 
       if (net_is_addr_unspecified(lookup))
         {
-          return -EHOSTUNREACH;
+          return -ENETUNREACH;
         }
     }
 
@@ -270,7 +271,7 @@ int icmpv6_neighbor(FAR struct net_driver_s *dev,
   if (IFF_IS_NOARP(dev->d_flags))
     {
       ninfo("ARP not supported on %s, no send!\n", dev->d_ifname);
-      return -EHOSTUNREACH;
+      return -ENETUNREACH;
     }
 
   /* Allocate resources to receive a callback.  This and the following
@@ -369,6 +370,13 @@ int icmpv6_neighbor(FAR struct net_driver_s *dev,
       state.snd_retries++;
     }
 
+  /* No reply to any of the solicitations: the address resolution failed */
+
+  if (ret == -ETIMEDOUT)
+    {
+      ret = -EHOSTUNREACH;
+    }
+
   nxsem_destroy(&state.snd_sem);
   devif_dev_callback_free(dev, state.snd_cb);
 
diff --git a/net/icmpv6/icmpv6_sendmsg.c b/net/icmpv6/icmpv6_sendmsg.c
index 72177277610..0aa5d9b87bb 100644
--- a/net/icmpv6/icmpv6_sendmsg.c
+++ b/net/icmpv6/icmpv6_sendmsg.c
@@ -361,7 +361,6 @@ ssize_t icmpv6_sendmsg(FAR struct socket *psock,
   if (ret < 0)
     {
       nerr("ERROR: Not reachable\n");
-      ret = -ENETUNREACH;
       goto errout;
     }
 #endif /* CONFIG_NET_ICMPv6_NEIGHBOR */
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index 3e97ce5662e..9e2e8ee6e0f 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -1488,7 +1488,7 @@ int tcp_connect(FAR struct tcp_conn_s *conn, FAR const 
struct sockaddr *addr)
 
   if (ret < 0)
     {
-      return -ENETUNREACH;
+      return ret;
     }
 #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
 
diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c
index 0e3edbccc86..3f9b8c3bc4e 100644
--- a/net/tcp/tcp_send_buffered.c
+++ b/net/tcp/tcp_send_buffered.c
@@ -1399,7 +1399,6 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR 
const void *buf,
   if (ret < 0)
     {
       nerr("ERROR: Not reachable\n");
-      ret = -ENETUNREACH;
       goto errout;
     }
 #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
diff --git a/net/tcp/tcp_send_unbuffered.c b/net/tcp/tcp_send_unbuffered.c
index 8fe57eb63e7..66dd05ccb73 100644
--- a/net/tcp/tcp_send_unbuffered.c
+++ b/net/tcp/tcp_send_unbuffered.c
@@ -546,7 +546,6 @@ ssize_t psock_tcp_send(FAR struct socket *psock,
   if (ret < 0)
     {
       nerr("ERROR: Not reachable\n");
-      ret = -ENETUNREACH;
       goto errout;
     }
 #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
diff --git a/net/tcp/tcp_sendfile.c b/net/tcp/tcp_sendfile.c
index 50fe745b449..dc90e3142d7 100644
--- a/net/tcp/tcp_sendfile.c
+++ b/net/tcp/tcp_sendfile.c
@@ -461,7 +461,7 @@ ssize_t tcp_sendfile(FAR struct socket *psock, FAR struct 
file *infile,
   if (ret < 0)
     {
       nerr("ERROR: Not reachable\n");
-      return -ENETUNREACH;
+      return ret;
     }
 #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
 
diff --git a/net/udp/udp_sendto_buffered.c b/net/udp/udp_sendto_buffered.c
index 6aeb751b63c..0b066890900 100644
--- a/net/udp/udp_sendto_buffered.c
+++ b/net/udp/udp_sendto_buffered.c
@@ -748,7 +748,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR 
const void *buf,
   if (ret < 0)
     {
       nerr("ERROR: Not reachable\n");
-      return -ENETUNREACH;
+      return ret;
     }
 #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
 
diff --git a/net/udp/udp_sendto_unbuffered.c b/net/udp/udp_sendto_unbuffered.c
index e2c4d4b1143..37736aa3b5e 100644
--- a/net/udp/udp_sendto_unbuffered.c
+++ b/net/udp/udp_sendto_unbuffered.c
@@ -423,7 +423,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR 
const void *buf,
   if (ret < 0)
     {
       nerr("ERROR: Peer not reachable\n");
-      return -ENETUNREACH;
+      return ret;
     }
 #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
 

Reply via email to