Shutting interface down doesn't always drop br-wan ip addr

Hi,

THE ISSUE

When I unplug the ethernet cable, OpenWrt doesn't automatically detect the lost connection to drop the IP addr. There are kernel messages notifying that the link state changed but that's all.

I need to make the IP addr drop.

A PART OF THE SOLUTION

So I catch the link state change by polling it with the command

swconfig dev rt305x port 4 get link

When caught, I need to update ifconfig to drop the IP addr.

WHERE IT STARTS FUZZLING ME

To do that I set the iface down and that should drop the IP addr. It works from time to time but not always.

Here is an example:

I have this configuration for /etc/config/network:

config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config interface 'lan'
        option type 'bridge'
        option ifname 'eth0.1'
        option proto 'static'
        option netmask '255.255.255.0'
        option ipaddr '192.168.252.1'

config device 'lan_dev'
        option name 'eth0.1'
        option macaddr '1a:2b:3c:4d:5e:6f'

config interface 'wan'
        option type 'bridge'
        option ifname 'eth0.2'
        option proto 'dhcp'

config device 'wan_dev'
        option name 'eth0.2'
        option macaddr '1a:2b:3c:4d:5e:6f'

config switch
        option name 'rt305x'
        option reset '1'
        option enable_vlan '1'

config switch_vlan
        option device 'rt305x'
        option vlan '1'
        option ports '0 1 2 3 6t'

config switch_vlan
        option device 'rt305x'
        option vlan '2'
        option ports '4 6t'

And the ouput of ifconfig for br-wan is:

 br-lan    Link encap:Ethernet  HWaddr 1A:2B:3C:4D:5E:6F  
          inet addr:192.168.252.1  Bcast:192.168.252.255  Mask:255.255.255.0
          inet6 addr: fe80::182b:3cff:fe4d:5e6f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:2158 (2.1 KiB)

br-wan    Link encap:Ethernet  HWaddr 1A:2B:3C:4D:5E:6F  
          inet addr:192.168.167.12  Bcast:192.168.167.255  Mask:255.255.255.0
          inet6 addr: fe80::182b:3cff:fe4d:5e6f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:40 errors:0 dropped:0 overruns:0 frame:0
          TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3908 (3.8 KiB)  TX bytes:1937 (1.8 KiB)

eth0      Link encap:Ethernet  HWaddr 5E:D3:4B:CE:E7:D6  
          inet6 addr: fe80::5cd3:4bff:fece:e7d6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:20257 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4804 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2273842 (2.1 MiB)  TX bytes:386092 (377.0 KiB)
          Interrupt:5 

eth0.1    Link encap:Ethernet  HWaddr 1A:2B:3C:4D:5E:6F  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:2158 (2.1 KiB)

eth0.2    Link encap:Ethernet  HWaddr 1A:2B:3C:4D:5E:6F  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:40 errors:0 dropped:0 overruns:0 frame:0
          TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3908 (3.8 KiB)  TX bytes:1937 (1.8 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:196 errors:0 dropped:0 overruns:0 frame:0
          TX packets:196 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:13861 (13.5 KiB)  TX bytes:13861 (13.5 KiB)

wlan0     Link encap:Ethernet  HWaddr 1A:2B:3C:4D:5E:6F  
          inet6 addr: fe80::182b:3cff:fe4d:5e6f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:2206 (2.1 KiB)

But when I set iface eth0.2 down (and thus br-wan):

root@OpenWrt:/# ip link set eth0.2 down
[61913.989411] br-wan: port 1(eth0.2) entered disabled state

we can see in the ifconfig output that eth0.2 has disappeared but br-wan doesn't always drop the IP addr.

I need the IP addr to appear/disappear for br-wan. So removing the bridge type for wan is not an option.

Maybe start with dropping the redundant br-wan bridge.

1 Like

As specified at the end of my post, I need this redundant br-wan bridge :confused:
Because I have one script dealing with diffferent possible scenarios : wan might be on eth0.1 or eth0.2 or eth0.1 & eth0.2 or wifi. So it's easier for me to check IP addr on br-wan than tracking the iface for each scenario and checking it's IP addr.

If that is the only solution, I'll deal with it that way but that's a lot of modifications in my scripts.

. /lib/functions/network.sh

if network_get_device ifname wan; then
    echo "Device of wan is $ifname"
fi
1 Like

So simple !! :smiley:

And to get the IP addr:

. /lib/functions/network.sh

network_get_ipaddr WAN_IP wan
echo $WAN_IP

BUT STILL THE SOLUTION IS NOT COMPLETE

The function network_get_ipaddr still returns an ip addr while the interface is down. This is my test script and its output.

. /lib/functions/network.sh

if network_get_device ifname wan; then
        ip link set $ifname down
        echo =========== set $ifname DOWN
fi

if network_get_device ifname wan; then
        network_get_ipaddr WAN_IP wan
        echo $WAN_IP
fi

echo === sleep 5 seconds
sleep 5
                                      
if network_get_device ifname wan; then
        network_get_ipaddr WAN_IP wan
        echo $WAN_IP
fi
                      
ip link set $ifname up         
echo =========== set $ifname UP
                                      
if network_get_device ifname wan; then
        network_get_ipaddr WAN_IP wan
        echo $WAN_IP
fi
       
echo === sleep 5 seconds
sleep 5

if network_get_device ifname wan; then
        network_get_ipaddr WAN_IP wan
        echo $WAN_IP
fi
=========== set eth0.2 DOWN
192.168.167.12
=== sleep 5 seconds
192.168.167.12
=========== set eth0.2 UP
192.168.167.12
=== sleep 5 seconds
192.168.167.12
1 Like

The IP address of a Linux bridge is on its pseudo-interface, not on any of its members. You need to manipulate the bridge, not it’s enslaved interface[s]

1 Like

Also network.sh relies on cached information from ubus, you may want to run network_flush_cache in between.

2 Likes