Opened 6 years ago

Closed 3 years ago

#11227 closed enhancement (not_a_bug)

Enhance dnsmasq static DHCP lease config to support host-specific lease times incl. infinite lease

Reported by: hnyman Owned by: developers
Priority: normal Milestone: Barrier Breaker 14.07
Component: base system Version: Trunk
Keywords: dnsmasq DHCP lease infinite Cc: hannu.nyman@…


A forum discussion got me interested in dnsmasq DHCP config. A user wanted infinite lease times for a device, and the given answer was to change the default lease time.

Based on dnsmasq config examples, it looks to me that dnsmasq itself already supports device-specific lease times for static leases:

# Supply parameters for specified hosts using DHCP. There are lots
# of valid alternatives, so we will give examples of each. ...
# Always give the host with Ethernet address 11:22:33:44:55:66
# the name fred and IP address and lease time 45 minutes
# Give the machine which says its name is "bert" IP address
# and an infinite lease

As far as I see, the required task for supporting device-specific lease times in Openwrt is to define a new leasetime parameter for static leases in dnsmasq init script and to add support for that in LuCI.

Static leases are handled in dhcp_host_add in dnsmasq init script:

LuCI Network / DHCP and DNS screen might have the config of each static lease to contain also a box for optionally entering a non-standard lease time. The optional lease time box might also a be a drop-down with "standard", 1h, 6h, 1d, 7d, 31d and "infinite" as choices.

Attachments (1)

dhcp_static_leasetime.patch (579 bytes) - added by OXINARF 6 years ago.
Patch from hnyman

Download all attachments as: .zip

Change History (7)

comment:1 Changed 6 years ago by hnyman <hannu.nyman@…>

I wrote a short proof-of-concept for the dnsmasq part.
The following change to dnsmasq.init (/etc/init.d/dnsmasq in a live system) is enough to have dnsmasq to adjust leasetimes per host. If no device-specific value is set, it leaves the option empty.

root@OpenWrt:/etc/init.d# diff /rom/etc/init.d/dnsmasq /etc/init.d/dnsmasq
--- /rom/etc/init.d/dnsmasq
+++ /etc/init.d/dnsmasq
@@ -231,7 +231,8 @@
        for m in $mac; do append macs "$m" ","; done
        [ -n "$macs" ] || return 0

-       xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${ip:+,$ip}${name:+,$name}"
+       config_get leaseperiod "$cfg" leaseperiod
+       xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${ip:+,$ip}${name:+,$name}${leaseperiod:+,$leaseperiod}"

 dhcp_mac_add() {

I edited my /etc/config/dhcp for two hosts:

config 'host'
        option 'name' 'HNHD57JG'
        option 'mac' '70:aa:bb:4b:29:90'
        option 'ip' ''
        option 'leaseperiod' 'infinite'

config 'host'
        option 'name' 'LS-WSGL195'
        option 'mac' '00:aa:bb:4c:51:95'
        option 'ip' ''
        option 'leaseperiod' '33h'

config 'host'
        option 'name' 'NetgearRangeExtender'
        option 'mac' 'a0:aa:bb:97:c6:3c'
        option 'ip' ''

This led to expected results in /var/etc/dnsmasq (the new runtime config file for dnsmasq):


The hosts did get proper leasetimes just ok. (In practice, "infinite" led to a 40 day lease time on my Windows PC.)

LuCI does not play well with infinite, as it shows that as expired in DHCP status.

comment:2 Changed 6 years ago by hnyman <hannu.nyman@…>

Also the LuCI part seems rather simple. Below is the changes I made to a live system, where /usr/lib/lua/luci/model/cbi/admin_network/dhcp.lua corresponds to in source code.

I added the new option and then also some explanations for the user:

--- /rom/usr/lib/lua/luci/model/cbi/admin_network/dhcp.lua
+++ /usr/lib/lua/luci/model/cbi/admin_network/dhcp.lua
@@ -224,7 +224,9 @@
                "only hosts with a corresponding lease are served.") .. "<br />" ..
        translate("Use the <em>Add</em> Button to add a new lease entry. The <em>MAC-Address</em> " ..
                "indentifies the host, the <em>IPv4-Address</em> specifies to the fixed address to " ..
-               "use and the <em>Hostname</em> is assigned as symbolic name to the requesting host."))
+               "use and the <em>Hostname</em> is assigned as symbolic name to the requesting host. " ..
+               "The optional <em>Lease time</em> can be used to set a non-standard host-specific " ..
+               "lease time, e.g. 12h, 3d or infinite."))

 s.addremove = true
 s.anonymous = true
@@ -239,6 +241,9 @@

 ip = s:option(Value, "ip", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
 ip.datatype = "ip4addr"
+time = s:option(Value, "leaseperiod", translate("Lease time"))
+time.rmempty  = true
        ip:value(entry["IP address"])

Changed 6 years ago by OXINARF

Patch from hnyman

comment:3 Changed 6 years ago by OXINARF

I attached the patch from hnyman with only one difference: the new option is called leasetime

I will also submit the patch to Luci with the changes proposed by hnyman (plus detection of infinite leasetime).

comment:4 Changed 4 years ago by jow

  • Milestone changed from Attitude Adjustment 12.09 to Barrier Breaker 14.07

Milestone Attitude Adjustment 12.09 deleted

comment:5 Changed 3 years ago by anonymous

It would be awesome if this feature could be merged upstream!

comment:6 Changed 3 years ago by jogo

  • Resolution set to not_a_bug
  • Status changed from new to closed

The patch needs to be submitted to the ML according to SubmittingPatches.

Add Comment

Modify Ticket

as closed .
The resolution will be deleted. Next status will be 'reopened'.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.