Differences between dhcp.leases and hosts/dhcp

Hi All,

Using OpenWrt 21.02.1 and have 3 different DNS/DHCP servers configured for three networks with also individual lease and resolve files configured
e.g.

        option leasefile '/tmp/dhcp.leases.guest'
        option resolvfile '/tmp/resolv.conf.d/resolv.conf.guest'

Generally all works just was surprised to see dhcp.leases and hosts/dhcp files acting differently.

/tmp/dhcp.leases.* act as expected they only list the leases of the respective interface. But /tmp/hosts/dhcp.* files show all leases (hosts) across all interfaces (as you can see on the size of them.

root@router:/# ls -lah /tmp/dhcp.leases.*
-rw-r--r--    1 root     root          75 Jan  8 08:06 /tmp/dhcp.leases.guest
-rw-r--r--    1 root     root         723 Jan  8 13:28 /tmp/dhcp.leases.lan
-rw-r--r--    1 root     root         564 Jan  8 13:17 /tmp/dhcp.leases.media
root@router:/# ls -lah /tmp/hosts/dhcp.*
-rw-r--r--    1 root     root        1.0K Jan  8 13:14 /tmp/hosts/dhcp.guest_dns
-rw-r--r--    1 root     root        1.0K Jan  8 13:14 /tmp/hosts/dhcp.lan_dns
-rw-r--r--    1 root     root        1.0K Jan  8 13:15 /tmp/hosts/dhcp.media_dns

Questions I have:

  1. Is this behavior normal/expected that the /tmp/hosts/ files include all hosts and not only the ones from the interface?
  2. If you want to actually on one specific interface to have all hosts resolutions available (while keeping the other interfaces restricted to their names) what would be the suggest approach? Have them separated as above and then have a cronjob that combines the three lease files together for one interface?

Actually more testing revealed it seems not to work as expected. It seems all names (if you use the FQN) can be resolved from each DNS server on each interface. That would indicate that they use /tmp/hosts/ to resolve. So question how to ensure that hosts files only receive the hosts from the local interface (DHCP Leases).

EDIT:
Which I could already have figured out from the log file

Jan  8 11:53:11 router dnsmasq[31253]: read /tmp/hosts/dhcp.guest_dns - 33 addresses
Jan  8 11:53:11 router dnsmasq[31252]: read /tmp/hosts/dhcp.lan_dns - 34 addresses
Jan  8 11:53:11 router dnsmasq[31254]: read /tmp/hosts/dhcp.lan_dns - 34 addresses
Jan  8 11:53:11 router dnsmasq[31253]: read /tmp/hosts/dhcp.media_dns - 34 addresses
Jan  8 11:53:11 router dnsmasq[31254]: read /tmp/hosts/dhcp.guest_dns - 33 addresses
Jan  8 11:53:11 router dnsmasq[31252]: read /tmp/hosts/dhcp.guest_dns - 33 addresses

So question remains how do I influence how the hosts files are generated and used by the DNS servers?

hi,

are you using config host / config domain? you can add option instance <dnsmasq instance name> to bind to dnsmasq instance. this requires "named" config dnsmasq sections.

e.g.

config dnsmasq 'landns'
  option ...

config dnsmasq 'guestdns'
  option ...

config host
  option mac 'xx:xx:xx:xx:xx:xx'
  option name 'client01'
  option instance 'landns'

config host
  option mac 'xx:xx:xx:xx:xx:xx'
  option name 'client02'
  option instance 'guestdns'

this setup will attach host config to respective dnsmasq instance and makes them available only for the respective dns / dhcp domain (controlled by the dnsmasq instance).

note: maybe using option dhcphostsfile is a more comfortable way to setup hosts imho. From luci you cannot bind host config to an instance, you need to edit /etc/config/dhcp anyhow. but if you put your host config into a dhcp hosts file or directory you have the benefit that it can be edited and then reloaded instead of full restart.

2 Likes

@grrr2
Many thanks for your feedback. I had already the individual dnsmasq instances configured. The missing part was the option instance in each of the config host entries.
How about config domain entries, seems they don't support the `option instance' or? Most likely the dhcphostfile approach would fit for them.

best to check /etc/init.d/dnsmasq:

        config_foreach filter_dnsmasq boot dhcp_boot_add "$cfg"
        config_foreach filter_dnsmasq mac dhcp_mac_add "$cfg"
        config_foreach filter_dnsmasq tag dhcp_tag_add "$cfg"
        config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add "$cfg"
        config_foreach filter_dnsmasq userclass dhcp_userclass_add "$cfg"
        config_foreach filter_dnsmasq circuitid dhcp_circuitid_add "$cfg"
        config_foreach filter_dnsmasq remoteid dhcp_remoteid_add "$cfg"
        config_foreach filter_dnsmasq subscrid dhcp_subscrid_add "$cfg"
        config_foreach filter_dnsmasq match dhcp_match_add "$cfg"
        config_foreach filter_dnsmasq domain dhcp_domain_add "$cfg"
        config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"

filter_dnsmasq() {
        local cfg="$1" func="$2" match_cfg="$3" found_cfg

        # use entry when no instance entry set, or if it matches
        config_get found_cfg "$cfg" "instance"
        if [ -z "$found_cfg" ] || [ "$found_cfg" = "$match_cfg" ]; then
                $func $cfg
        fi
}

meaning for example all these config elements are option instance aware

1 Like

Thanks, good reminder. I was just checking the documentation and didn't saw it.

This is now how I expected it

Jan  8 15:32:43 router dnsmasq[9816]: read /tmp/hosts/dhcp.guest_dns - 6 addresses
Jan  8 15:32:43 router dnsmasq[9816]: read /tmp/hosts/dhcp.media_dns - 17 addresses
Jan  8 15:32:43 router dnsmasq[9814]: read /tmp/hosts/dhcp.lan_dns - 36 addresses

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.