Configuring multiple VLANs with different default gateways, v23.05 (DSA)

For future reference, the OpenWRT method for adding multiple default gateways involves using the config route and config rule options in /etc/config/network. This creates a separate routing table for a particular interface with its own isolated default gateway. It's worth mentioning that this doesn't require any additional packages.

An example using the interface vlan8_interface (VLAN 8) is shown below, as well as a configuration using LuCI:

config device
	option type '8021q'
	option ifname 'eth0'
	option vid '8'
	option name 'eth0.8_device'

config interface 'vlan8_interface'
	option proto 'static'
	option type 'bridge'
	option device 'eth0.8_device'
	option ipaddr '192.168.8.232'
	option netmask '255.255.255.0'
	option gateway '192.168.8.254'
	option ip4table 'vlan8_table'

config route
	option target '0.0.0.0/0'
	option table 'vlan8_table'
	option gateway '192.168.8.254'
	option interface 'vlan8_interface'

config rule
	option priority '30000'
	option in 'vlan8_interface'
	option lookup 'vlan8_table'

The same can also be accomplished using LuCI by configuring the following settings:

  1. Network > Interfaces > vlan8_interface > Advanced Settings > Override IPv4 routing table:

  1. Network > Routing > Static IPv4 Routes:

  1. Network > Routing > IPv4 Rules

Both methods result in the following routing rules:

# ip rule show
0:	from all lookup local 
10000:	from 192.168.8.232 lookup vlan8_table 
20000:	from all to 192.168.8.232/24 lookup vlan8_table 
30000:	from all iif eth0.8_device lookup vlan8_table 
32766:	from all lookup main 
32767:	from all lookup default 
90039:	from all iif lo lookup vlan8_table
3 Likes