[Dnsmasq-discuss] [PATCH] Allow specifying a netmask in dhcp-range for relays.

Nicolas Cavallari nicolas.cavallari at green-communications.fr
Mon Mar 12 09:26:50 GMT 2018

When using a DHCP relay, the netmask in dhcp-range is matched against
giaddr (or the link selection suboption) to determine on which range the
address should be allocated.

However, in some cases, you want to ignore this mechanism, because your
relay, clients and your DHCP server are not in the same subnet, the
relay do not have addresses on their client interfaces and/or you have
more complicated matching in place (e.g.  other relay agent information

The link selection suboption would be a good candidate but this requires
a duplicate policy/configuration in every relay that matches what the
DHCP server would select.

Specifying as a netmask is accepted by the parser but is
rejected by a condition in rfc2131.c, that only checks if
netmask.s_addr = 0, which can happen if the guessing failed, but can
also happen if was explicitly specified.

This fixes the condition so that netmask.s_addr = 0 is acceptable
if it was specified in the configuration.  i.e.  if the
dhcp context (the considered dhcp range) has the CONTEXT_NETMASK

The netmask to be sent to the client can be set with
dhcp-option=option:netmask, anyway.
Yuki Nisiwaki's message reminded me of this patch that I use but forgot

 src/rfc2131.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/rfc2131.c b/src/rfc2131.c
index c08a8ab..6b2df35 100644
--- a/src/rfc2131.c
+++ b/src/rfc2131.c
@@ -319,7 +319,7 @@ size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
 	    /* This section fills in context mainly when a client which is on a remote (relayed)
 	       network renews a lease without using the relay, after dnsmasq has restarted. */
-	    if (netmask.s_addr != 0  && 
+	    if ((netmask.s_addr != 0 || context_tmp->flags & CONTEXT_NETMASK) &&
 		is_same_net(addr, context_tmp->start, netmask) &&
 		is_same_net(addr, context_tmp->end, netmask))

More information about the Dnsmasq-discuss mailing list