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 3590ac44bc938fc26a863131aff6d12c140593ae Author: zhanghongyu <[email protected]> AuthorDate: Tue Nov 24 14:59:28 2026 +0800 drivers/net/bridge: refuse a device that is already a port. bridge_addif() only looked for the device in the port table of the bridge it was adding to, so adding a device that was already a port of another bridge succeeded. netdev_lower_bridge_set() then overwrote the port the device pointed to: the first bridge kept an entry for a device it no longer received from, and both bridges took the MAC address of the device. Deleting the second bridge detached the device for good, and it stopped communicating until it was unregistered. nsh> brctl addbr br0 nsh> brctl addif br0 eth0 nsh> brctl addbr br1 nsh> brctl addif br1 eth0 <- succeeded, must be EBUSY nsh> brctl delbr br1 nsh> ifconfig br0 10.0.0.2 netmask 255.255.255.0 nsh> brctl addif br0 eth1 nsh> ifup br0 ... ping of 10.0.0.2 over eth0: 0/3, over eth1: 3/3 Look the device up over all bridges instead, with the new netdev_lower_bridge_get(), and report -EBUSY as Linux does. Assisted-by: Kiro:claude-opus-4 Signed-off-by: zhanghongyu <[email protected]> --- drivers/net/bridge.c | 10 ++++++++++ drivers/net/netdev_upperhalf.c | 25 +++++++++++++++++++++++++ include/nuttx/net/netdev_lowerhalf.h | 15 +++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/drivers/net/bridge.c b/drivers/net/bridge.c index b5d04f13ea1..809d654e75f 100644 --- a/drivers/net/bridge.c +++ b/drivers/net/bridge.c @@ -1096,6 +1096,16 @@ static int bridge_addif(FAR const struct ifreq *req) return -EINVAL; } + /* A device can only be a port of one bridge. Checked over all bridges, + * because attaching it here would overwrite the port it already points + * to, leaving the other bridge with a port it no longer receives from. + */ + + if (netdev_lower_bridge_get(dev) != NULL) + { + return -EBUSY; + } + nxmutex_lock(&br->br_lock); for (i = 0; i < CONFIG_NET_BRIDGE_MAX_PORTS; i++) diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c index 16f0c45810f..081806f02dd 100644 --- a/drivers/net/netdev_upperhalf.c +++ b/drivers/net/netdev_upperhalf.c @@ -2063,6 +2063,31 @@ void netdev_lower_bridge_set(FAR struct netdev_lowerhalf_s *dev, upper->bridge = port; netdev_unlock(&dev->netdev); } + +/**************************************************************************** + * Name: netdev_lower_bridge_get + * + * Description: + * Return the bridge port of the device, or NULL when the device is not a + * port of any bridge. + * + * Input Parameters: + * dev - The lower half device driver structure + * + ****************************************************************************/ + +FAR struct bridge_port_s * +netdev_lower_bridge_get(FAR struct netdev_lowerhalf_s *dev) +{ + FAR struct netdev_upperhalf_s *upper = dev->netdev.d_private; + FAR struct bridge_port_s *port; + + netdev_lock(&dev->netdev); + port = upper->bridge; + netdev_unlock(&dev->netdev); + + return port; +} #endif /**************************************************************************** diff --git a/include/nuttx/net/netdev_lowerhalf.h b/include/nuttx/net/netdev_lowerhalf.h index 9bbc241cb62..c942f765e8a 100644 --- a/include/nuttx/net/netdev_lowerhalf.h +++ b/include/nuttx/net/netdev_lowerhalf.h @@ -478,6 +478,21 @@ void netdev_lower_input(FAR struct netdev_lowerhalf_s *dev, struct bridge_port_s; void netdev_lower_bridge_set(FAR struct netdev_lowerhalf_s *dev, FAR struct bridge_port_s *port); + +/**************************************************************************** + * Name: netdev_lower_bridge_get + * + * Description: + * Return the bridge port of the device, or NULL when the device is not a + * port of any bridge. + * + * Input Parameters: + * dev - The lower half device driver structure + * + ****************************************************************************/ + +FAR struct bridge_port_s * +netdev_lower_bridge_get(FAR struct netdev_lowerhalf_s *dev); #endif /****************************************************************************
