opnsense / core

OPNsense GUI, API and systems backend
https://opnsense.org/
BSD 2-Clause "Simplified" License
3.36k stars 754 forks source link

DHCP server dynamic IP allocation with Host Name and Description fields from Static info #2946

Closed Northguy closed 5 years ago

Northguy commented 5 years ago

I configured OPNsense as DHCP server and would like to identify my devices in the DHCP leases table a bit more easily, based on a description. Most devices only show generic manufacturer info under the MAC address, but do not provide a Hostname or a more verbose description.

I tried creating a static mapping for the MAC address but am not necessarily looking for a static IP lease. Therefore I did not fill in the IP address in the edit box. According to the help it says "If no IPv4 address is given, one will be dynamically allocated from the pool", but if I follow this way, the MAC address is shown as ' static' in the leases table without an IP. i.e.: I have created a static lease without a visible dynamic IP?

Is this a bug, or is there another way to create an easily identifiable indicator for the MAC addresses that have obtained a dynamic lease?

Issue seems (not entirely) related to https://github.com/opnsense/core/issues/1465

zz0rk commented 5 years ago

I am in the same boat and would also appreciate some feedback from an expert. I've set up several static DHCP mappings and they show as 'active' when in use under Leases; but no IP address is being populated for each entry row.

ja-finn commented 5 years ago

I was looking for the same. In my old Asus router I could just type in a new hostname and I'm guessing it tied it to the MAC. It's really nice to have for devices that you can't set a hostname on (ie IoT).

Further it seems that setting a static entry without defining an IP address leaves you unable to edit the entry afterwards. No edit button or thrascan to remove it.

OPNsense 19.1.1

screenshot from 2019-02-08 09-54-46

teklabania commented 5 years ago

Here's some more formal BUG report for the issue.

Brief description:

What I want to achieve is to translate MAC address to hostname without caring about IP address assigned to the host.

OPNSense: 19.1.6

Steps to reproduce

Results

Expected results

Screenshot:

The picture shows the current state and the arrows show where the IP leases should go to. 2019-04-24_21-21

Additional info:

https://forum.opnsense.org/index.php?topic=12369.0 https://forum.netgate.com/topic/125306/dhcp-static-mapped-hostname-to-dns-resolver-without-specifying-ip

AdSchellevis commented 5 years ago

@teklabania it can't do what you think it does, but might require better explanation in the help text (ideas are welcome).

When the marker is set, it will write out all dns entries which come with a valid ip address.

https://github.com/opnsense/core/blob/31a81c51a6d76b44439acf4fdcb186075cab0e79/src/etc/inc/plugins.inc.d/unbound.inc#L642-L665

Since leases change over time, this specific feature would require unbound to restart after every given lease, to load the file again (which for obvious reasons doesn't happen).

The duplication of entries as shown in the last screenshot seem to happen on my end as well, which should hopefully be easy to fix. I'll prepare a fix and close the issue, feel free to suggest changes in a new ticket to better explain the unbound registration feature.

It might be an option to check DHCP Registration, which has a daemon active to watch the leases file, although I'm not exactly sure if/how static leases end up in /var/dhcpd/var/db/dhcpd.leases.

AdSchellevis commented 5 years ago

In theory it would be possible to add logic in https://github.com/opnsense/core/blob/master/src/opnsense/scripts/dns/unbound_dhcpd.py to use the configured hostname, but you easily end up in a race condition there, since a change doesn't trigger a new lease.

It looks like client-hostname in /var/dhcpd/var/db/dhcpd.leases is always set to the response received from the client. I can't spend the amount of time needed to add a feature to support this, it works out of the box when using static leases. The gui glitch should be fixed by https://github.com/opnsense/core/commit/839ec3567ab25811b3504b9afcc4e168efbe9713.

Northguy commented 5 years ago

Hi @AdSchellevis . I see you closed the issue I originally logged. I was not available to react due to spring holiday at the time you closed the issue and afterward forgot about it, so please forgive me resurrecting this thread.

I did not log a formal template for this issue, so it is good that @teklabania did put in the effort. My case was actually more simple than @teklabania wrote, but had almost the same goal. Although I do not care about the Unbound part of his problem description.

Brief description: What I want to achieve is to have recognisable devices in the DHCP leases table, while i do not actually care that a static IP is assigned (or resolved by Unbound DNS). In order to reach this goal, I add a hostname and description while not providing a static IP in the static leases table (as is described by the help text)

OPNSense: 19.1.7

Steps to reproduce ~go to Services -> Unbound DNS -> General~ ~set "Register DHCP static mappings" to true~ go to Services -> DHCPv4 -> Interface scroll down to section "DHCP Static Mappings for this interface." click "+" to add new Static DHCP Mapping Under "Static DHCP Mapping" provide: MAC address Hostname click Save go to Services -> DHCPv4 -> Leases Results IP from the pool is not visibly leased to MAC address ~No~ hostname is assigned to ~IP~ MAC address DNS query does not resolve hostname provided under "Static DHCP Mapping"

Expected results IP from the pool is leased to MAC address Hostname and description is assigned to MAC / IP address ~DNS query resolves hostname provided under "Static DHCP Mapping"~

As Unbound is left out of this equation, this would not result in a race condition?

Screenshot: image

It is unclear which 'glitch' you refer to with 839ec35, but applying the mentioned patch did not result in desired outcome as indicated with the screenshot.

AdSchellevis commented 5 years ago

@Northguy the gui glitch this https://github.com/opnsense/core/commit/839ec3567ab25811b3504b9afcc4e168efbe9713 fixes is the duplication of the mac addresses as mentioned in the screenshot, descriptions should be visible as configured when set in a static entry without an address (at least on my end).

Northguy commented 5 years ago

OK, applied opnsense-patch 839ec3567ab25811b3504b9afcc4e168efbe9713 which indeed combines information. Only issue I see is that Interface name now has gone missing....

image

AdSchellevis commented 5 years ago

@Northguy can you create new issue for the missing interface in this case? I'll try to look at it when I have a bit of time available.

teklabania commented 5 years ago

@AdSchellevis, thank you for taking care of this issue with duplicated mac addresses. It's a pitty we cannot get the feature of getting hostname being assigned to MAC and being resolved by DNS. So putting additional info under hostname form could be good idea.

Currently under: Services -> DHCPv4 -> Interface -> DHCP Static Mappings for this interface -> "+" we have:

Hostname: Name of the host, without domain part.

Maybe something like this would do:

Hostname: Name of the host, without domain part. If no IPv4 address is given above, hostname will not be resolved by Unbound DNS.

AdSchellevis commented 5 years ago

@teklabania sounds like a good idea indeed to better explain the feature, would this be something you might be willing to create a pull request for?

Northguy commented 5 years ago

@Northguy can you create new issue for the missing interface in this case? I'll try to look at it when I have a bit of time available.

Done. See #3487

Northguy commented 5 years ago

@teklabania sounds like a good idea indeed to better explain the feature, would this be something you might be willing to create a pull request for?

@AdSchellevis see #3488