Issues in network restart after adding route entries

I am using openWrt23.05 in my virtual machine(Intel 86 based) and made few changes in route entries for interface eth0 and eth1 via uci set and ip route command. After this if I do "/etc/init.d/network restart", my routing entries got removed wheras "/etc/init.d/network restart" is not deleting the routing entries.

uci add network route
uci set network.@route[-1].target="$target"
uci set network.@route[-1].gateway="$gateway"
uci set network.@route[-1].interface="$interface"         
ip route add "$targetIP" via "$gateway" dev "$interface" proto static
uci commit network


root@myhost:# netstat -r -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.17.1    0.0.0.0         UG        0 0          0 eth1
10.8.0.0        10.240.215.1    255.255.255.255 UGH       0 0          0 eth0
10.9.0.0        192.168.17.1    255.255.255.255 UGH       0 0          0 eth1
10.60.74.0      10.240.215.1    255.255.255.255 UGH       0 0          0 eth0
10.60.75.0      192.168.17.1    255.255.255.255 UGH       0 0          0 eth1
10.62.0.0       10.240.215.1    255.255.255.255 UGH       0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 br-lan

root@myhost:# /etc/init.d/network reload
root@myhost:# netstat -r -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.17.1    0.0.0.0         UG        0 0          0 eth1
10.8.0.0        10.240.215.1    255.255.255.255 UGH       0 0          0 eth0
10.9.0.0        192.168.17.1    255.255.255.255 UGH       0 0          0 eth1
10.60.74.0      10.240.215.1    255.255.255.255 UGH       0 0          0 eth0
10.60.75.0      192.168.17.1    255.255.255.255 UGH       0 0          0 eth1
10.62.0.0       10.240.215.1    255.255.255.255 UGH       0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 br-lan


root@myhost:# /etc/init.d/network restart
root@myhost:# netstat -r -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.17.1    0.0.0.0         UG        0 0          0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 br-lan
192.168.17.0    0.0.0.0         255.255.255.0   U         0 0          0 eth1
root@myhost:# 

root@myhost:# /etc/init.d/network   --help
Syntax: /etc/init.d/network [command]

Available commands:
        start           Start the service
        stop            Stop the service
        restart         Restart the service
        reload          Reload configuration files (or restart if service does not implement reload)
        enable          Enable service autostart
        disable         Disable service autostart
        enabled         Check if service is started on boot
        running         Check if service is running
        status          Service status
        trace           Start with syscall trace
        info            Dump procd service info

/etc/init.d/network restart is stopping and restarting the network interfaces without considering configuration changes.

/etc/init.d/network reload is picking up the configuration changes made with UCI.

Do we have any restriction on usage of these reload and restart command in network procd service. Or Did I miss anything while adding the route entries. Can someone please clarify ?

With this command you add the routes manually.
Unlike the restart action, reload does not flush the routing tables.

That's all you need, but you must use the logical (not the physical) interface names.

Thanks @pavelgl. If I use the physical interfaces will network restart retain the route entries ? Also need to know how we can add logical name to an interface via uci ? If I want to retain route entries after network restart, what should I do ?

If you use uci when creating static routes, you should use lan, wan (and so on) depending on the names defined in the interface sections. Using the physical interface names (br-lan, eth0, eth1) breaks the configuration.

Create the routes in /etc/config/network using the correct interface names and they will remain persistent.

@pavelgl Now I used logical interface names(lan/wan) to update the route entry via uci. I can see those entries in /etc/config/network file as well. But When I tried both network restart and reload I can't see those routing entry listed under netstat -r -n.

uci add network route
uci set network.@route[-1].target="$target"
uci set network.@route[-1].gateway="$gateway"
uci set network.@route[-1].interface="$logical_interface"
...
config route
        option target '10.62.0.0/16'
        option gateway '10.240.215.1'
        option interface 'lan'

config route
        option target '172.30.169.0/24'
        option gateway '192.168.17.1'
        option interface 'lan'

config route
        option target '172.26.39.0/24'
        option gateway '192.168.17.1'
        option interface 'lan'
...
root@myhost:# netstat -r -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.17.1    0.0.0.0         UG        0 0          0 port1
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 br-lan
192.168.17.0    0.0.0.0         255.255.255.0   U         0 0          0 port1
root@myhost:#

At first: Instead of netstat please use ip route or ip r for short :wink: I think netstat is depricated since a decade or so...

Regarding your routing issue:
As I see/understand your output you are missing an interface address allocation where 10.240.215.1 is directly reachable.
To use a gateway you must be directly attached to net network.

Maybe, please post your ip addr and ip route to move things forward.

Edit: How does it come that the output on your first post is much longer then on your last one?

You are using the wrong interface.
The gateway used must be directly accessible through the specified interface.
You can use lan only if the next hop is a host on the LAN, eg 192.168.1.100.
Find the correct logical names corresponding to eth0 and eth1 listed in the published routing table in your original post and use them.

Thanks for the comments @_bernd . In the first output I used ip route add command along with uci add route, so my entries got reflected in netstat. After that I got comments from @pavelgl that I dont need to use tp route add, so I removed that and so routing entry didn't get added. Here is the o/p of ip addr command:

ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: port0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br-lan state UP group default qlen 1000
    link/ether 00:0c:29:29:a2:64 brd ff:ff:ff:ff:ff:ff
    inet 10.240.215.81/24 brd 10.240.215.255 scope global port0
       valid_lft forever preferred_lft forever
3: port1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:b2:6a:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.17.200/24 brd 192.168.17.255 scope global port1
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:feb2:6ac7/64 scope link
       valid_lft forever preferred_lft forever
7: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:29:a2:64 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global br-lan
       valid_lft forever preferred_lft forever

Thanks @pavelgl @_bernd . After adding the respective logical names to the route entry, I can see the route entry under netstat.

You can use lan only if the next hop is a host on the LAN, eg 192.168.1.100.

Just curious cant we add route entries for lan interface. I know lan is local network but just in case if I need to do routes for lan interface, cant we do that.

Is it possible to determine the logical names given the physical interface names ? Only way is to check the /etc/config/network file ??

Absolutely. You may have other routing devices on your LAN and use them to access remote networks.

You could list all available logical interfaces and their corresponding physical devices using this command:

echo -e "\nLogical => Physical"; for i in $(ubus list network.interface.* | sed 's/.*\.//'); \
do dev=$(ifstatus $i | grep \"device | awk '{ print $2 }' | sed 's/[",]//g'); echo "$i => $dev"; done
2 Likes

@Chinna I am just wondering...
Did you have looked at dynamic routing protocols and daemons like bird2 which implements i.e. ospf...

Because making routes available within a network is the very automated nature of a such dynamic routing protocol. If not and if you are interested feel free to ask.

Oh thanks @_bernd . I am not aware of that. How does this dynamic routing going to help in adding my routing entries ? Don't I need to do use UCI calls if I use dynamic routing ? Is that bird2 daemons run in Openwrt ? Please provide more info this, thanks.

Yes a dynamic routing protocol like ospf keeps track of networks assigned to an interface and maintains a state with other routers in a network to exchange the reachability information about these networks.
I'm currently at vacation but I can provide you with configuration examples end of next week.
But you can just Google for bird2 and ospf examples. There are plenty available.
Bird2 is available for Openwrt and the bird2 mailing list is also a good place to ask for guidance and help.

@pavelgl Thanks much for pointing the above script. I have used the above script for finding the logical interfaces associated with a given physical interface. However since it invokes ubus calls, my previous uci commit along with ubus call is asynchronous and am missing the logical interface added via uci call. Is there a way to find logical interfaces uysing uci call alone ?

Sorry, I don't follow you, but if you need to get the same result without using the ubus command line tool, here it is:

echo -e "\nLogical => Physical"; for i in $(uci export network | grep 'config interface' | awk '{ print $3 }' | sed "s/'//g"); \
do dev=$(ifstatus $i | grep \"device | awk '{ print $2 }' | sed 's/[",]//g'); echo "$i => $dev"; done