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

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

commit 189d5b72fd9c9ecbf6c88119a26d15fdb6cb88af
Author: zhangkai25 <[email protected]>
AuthorDate: Thu Jul 23 20:24:22 2026 +0800

    net/arp: Preserve static ARP entries on carrier loss.
    
    arp_cleanup() flushed every ARP entry of a device, including the
    permanent (ATF_PERM) ones.  netdev_carrier_off() calls it directly,
    so a PHY link-down event such as a cable unplug or a renegotiation
    also dropped the manually configured static entries.  Linux keeps
    them in this case: carrier loss goes through neigh_carrier_down()
    with skip_perm set, and only an administrative down flushes
    everything.
    
    Add a skip_perm parameter to arp_cleanup():
    
      - netdev_carrier_off() passes true, so static entries survive a
        carrier loss and only the dynamic ones are flushed;
      - netdev_ifdown() passes false, so an administrative down flushes
        every entry, like neigh_ifdown() does.
    
    No network driver has to change, because the behaviour follows from
    the two entry points.  The arp_cleanup() call in netdev_ifdown() is
    idempotent with respect to any cleanup that a driver d_ifdown() has
    already done through netdev_carrier_off().
    
    Signed-off-by: zhangkai25 <[email protected]>
---
 net/arp/arp.h               |  9 ++++++---
 net/arp/arp_table.c         | 15 ++++++++++++++-
 net/netdev/netdev_carrier.c |  8 +++++++-
 net/netdev/netdev_ioctl.c   |  8 ++++++++
 4 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/net/arp/arp.h b/net/arp/arp.h
index d36de6d691b..aa0b9ab6a2f 100644
--- a/net/arp/arp.h
+++ b/net/arp/arp.h
@@ -458,14 +458,17 @@ int arp_delete(in_addr_t ipaddr, FAR struct net_driver_s 
*dev);
  *   Clear the ARP table on the network device
  *
  * Input Parameters:
- *   dev  - The device driver structure
+ *   dev       - The device driver structure
+ *   skip_perm - When true, preserve permanent (static, ATF_PERM) entries
+ *               and only flush dynamic ones (carrier-loss semantics).  When
+ *               false, flush every entry on the device.
  *
  * Assumptions
  *   The network is locked to assure exclusive access to the ARP table.
  *
  ****************************************************************************/
 
-void arp_cleanup(FAR struct net_driver_s *dev);
+void arp_cleanup(FAR struct net_driver_s *dev, bool skip_perm);
 
 /****************************************************************************
  * Name: arp_update
@@ -656,7 +659,7 @@ int arp_queue_iob(FAR struct net_driver_s *dev, in_addr_t 
ipaddr,
 #  define arp_notify(i)
 #  define arp_find(i,e,d) (-ENOSYS)
 #  define arp_delete(i,d) (-ENOSYS)
-#  define arp_cleanup(d)
+#  define arp_cleanup(d,s)
 #  define arp_update(d,i,m,f);
 #  define arp_hdr_update(d,i,m);
 #  define arp_snapshot(s,n) (0)
diff --git a/net/arp/arp_table.c b/net/arp/arp_table.c
index e26dbd0ac50..eec837bca5a 100644
--- a/net/arp/arp_table.c
+++ b/net/arp/arp_table.c
@@ -602,13 +602,16 @@ int arp_delete(in_addr_t ipaddr, FAR struct net_driver_s 
*dev)
  *
  * Input Parameters:
  *   dev  - The device driver structure
+ *   skip_perm - When true, preserve permanent (static, ATF_PERM) entries
+ *               and only flush dynamic ones (carrier-loss semantics).  When
+ *               false, flush every entry on the device.
  *
  * Assumptions
  *   The network is locked to assure exclusive access to the ARP table.
  *
  ****************************************************************************/
 
-void arp_cleanup(FAR struct net_driver_s *dev)
+void arp_cleanup(FAR struct net_driver_s *dev, bool skip_perm)
 {
   int i;
 
@@ -616,6 +619,16 @@ void arp_cleanup(FAR struct net_driver_s *dev)
     {
       if (dev == g_arptable[i].at_dev)
         {
+          /* On a carrier-loss cleanup (skip_perm == true) preserve static
+           * (permanent) entries.  Only an administrative down (skip_perm
+           * == false) flushes them too.
+           */
+
+          if (skip_perm && (g_arptable[i].at_flags & ATF_PERM) != 0)
+            {
+              continue;
+            }
+
 #ifdef CONFIG_NET_ARP_SEND_QUEUE
           work_cancel_sync(LPWORK, &g_arptable[i].at_work);
           iob_free_queue(&g_arptable[i].at_queue);
diff --git a/net/netdev/netdev_carrier.c b/net/netdev/netdev_carrier.c
index cbc526d5766..6d1f20039ae 100644
--- a/net/netdev/netdev_carrier.c
+++ b/net/netdev/netdev_carrier.c
@@ -95,6 +95,12 @@ void netdev_carrier_off(FAR struct net_driver_s *dev)
       /* Notify clients that the network has been taken down */
 
       devif_dev_event(dev, NETDEV_DOWN);
-      arp_cleanup(dev);
+
+      /* Carrier loss is a physical-link event, not an administrative
+       * down.  Preserve permanent (static) ARP entries and flush only
+       * the dynamic ones.
+       */
+
+      arp_cleanup(dev, true);
     }
 }
diff --git a/net/netdev/netdev_ioctl.c b/net/netdev/netdev_ioctl.c
index 6f907f17206..45aae457923 100644
--- a/net/netdev/netdev_ioctl.c
+++ b/net/netdev/netdev_ioctl.c
@@ -2191,6 +2191,14 @@ int netdev_ifdown(FAR struct net_driver_s *dev)
 
               devif_dev_event(dev, NETDEV_DOWN);
 
+              /* Administrative down: flush the entire ARP table for this
+               * device, including permanent (static) entries.  This differs
+               * from the carrier-loss path (netdev_carrier_off), which keeps
+               * static entries.
+               */
+
+              arp_cleanup(dev, false);
+
 #ifdef CONFIG_NETDOWN_NOTIFIER
               /* Provide signal notifications to threads that want to be
                * notified of the network down state via signal.

Reply via email to