DHCP option for static route - is there a smart way to specify the default gateway?

I would like to push static routes for internal networks to all DHCP clients using dnsmasq.
You can see 2 such static routes examples for and below, where <router-ip-in-subnet> would need to be replaced by the IP of the default router in the subnet where the DHCP clients reside.


If there are n sbnets, then for n subnets needs to be given the correct gateway ip. This is somewhat cumbersome.
For other DHCP options dnsmasq will replace with the IP of the DHCP server, which is often what you want, e.g. because the same machine is also hosting the DNS server (see dnsmasq) and is also the correct gateway for the default route.


Is there a similar trick for the static routes option to specify the gateway automatically without actually having to manually pick the right IP address?

As far as I know, dnsmasq's "dhcp-option" always translates "" to the router's IP address.

On the other hand... I wonder why you need to do that, isn't the router the default gateway?

For this option is not replaced by dnsmasq, it is written vanilla into the response. I checked with Wireshark.

The default route is not good enough if a host runs OpenVPN, which will provide a default route into the tunnel. Sure the static route could be written into the ovpn config, but that would mean to touch a lot of places. Not a fun activity.

I consider it more elegant to push the static routes using DHCP.

Did you try using the numerical value of the classless-static-route option (121)? It works for other options, perhaps it will work here, too.

By the way, how did you configure OpenVPN to use dnsmasq as a DHCP server? I've always seen it configured so OpenVPN does all the work.

I have not tried the byte format. I wonder how this would help: I would imagine that dnsmasq will not parse and replace anything in that format. Or does it? Any examples?

OpenVpn is not using dnsmasq in this case. It just adds a default route. Only link local routes are there normally, and would take precedence for link lokal access. But if you provide static routes for the other lokal networks these will take precedence over the default route that is added by openvpn. Makes sense?

I have seen you need TAP to use an external DHCP server, and my clients all use Android; thanks anyway.

So I have tested this and found that some of the formats below were parsing well and others did not. In those that did parse, in no format did dnsmasq replace with the router IP! (verified using Wireshark)

Semantically all of the formats should be equivalent.

For the sake of documentation and to spare some users these experiments...

The following parse fine:


The following do NOT parse: