Skip to content

Commit

Permalink
Support custom DNS configuration on auto
Browse files Browse the repository at this point in the history
So far, when the network setting is set to auto, we don't allow a
custom DNS configuration. However, it is a perfectly valid use case to
provide custom DNS configuration while using automatic IP configuration
(e.g. DHCP). Especially when the DHCP server doesn't provide a DNS
server. In this case, systemd-resolved (at least on Home Assistant OS)
falls back to Cloudflare. This might not what the user wants. Customze
the DNS setting allows to set a custom fallback DNS server or override
a potential non-working DHCP provided one.
  • Loading branch information
agners committed Aug 20, 2024
1 parent 8ab396d commit c1747ab
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
10 changes: 8 additions & 2 deletions supervisor/api/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ATTR_METHOD,
ATTR_MODE,
ATTR_NAMESERVERS,
ATTR_NAMESERVERS_AUTO,
ATTR_PARENT,
ATTR_PRIMARY,
ATTR_PSK,
Expand Down Expand Up @@ -90,6 +91,7 @@ def ipconfig_struct(config: IpConfig) -> dict[str, Any]:
return {
ATTR_METHOD: config.method,
ATTR_ADDRESS: [address.with_prefixlen for address in config.address],
ATTR_NAMESERVERS_AUTO: config.nameservers_auto,
ATTR_NAMESERVERS: [str(address) for address in config.nameservers],
ATTR_GATEWAY: str(config.gateway) if config.gateway else None,
ATTR_READY: config.ready,
Expand Down Expand Up @@ -200,15 +202,19 @@ async def interface_update(self, request: web.Request) -> None:
if key == ATTR_IPV4:
interface.ipv4 = replace(
interface.ipv4
or IpConfig(InterfaceMethod.STATIC, [], None, [], None),
or IpConfig(InterfaceMethod.STATIC, [], None, False, [], None),
**config,
)
if interface.ipv4.method == InterfaceMethod.AUTO:
interface.ipv4.nameservers_auto = ATTR_NAMESERVERS not in config
elif key == ATTR_IPV6:
interface.ipv6 = replace(
interface.ipv6
or IpConfig(InterfaceMethod.STATIC, [], None, [], None),
or IpConfig(InterfaceMethod.STATIC, [], None, False, [], None),
**config,
)
if interface.ipv6.method == InterfaceMethod.AUTO:
interface.ipv6.nameservers_auto = ATTR_NAMESERVERS not in config
elif key == ATTR_WIFI:
interface.wifi = replace(
interface.wifi
Expand Down
1 change: 1 addition & 0 deletions supervisor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
ATTR_MULTICAST = "multicast"
ATTR_NAME = "name"
ATTR_NAMESERVERS = "nameservers"
ATTR_NAMESERVERS_AUTO = "nameservers_auto"
ATTR_NETWORK = "network"
ATTR_NETWORK_DESCRIPTION = "network_description"
ATTR_NETWORK_RX = "network_rx"
Expand Down
1 change: 1 addition & 0 deletions supervisor/dbus/network/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class IpProperties:
"""IP properties object for Network Manager."""

method: str | None
dns: list[str] | None


@dataclass(slots=True)
Expand Down
6 changes: 4 additions & 2 deletions supervisor/dbus/network/setting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dbus_fast import Variant
from dbus_fast.aio.message_bus import MessageBus

from ....const import ATTR_METHOD, ATTR_MODE, ATTR_PSK, ATTR_SSID
from ....const import ATTR_DNS, ATTR_METHOD, ATTR_MODE, ATTR_PSK, ATTR_SSID
from ...const import DBUS_NAME_NM
from ...interface import DBusInterface
from ...utils import dbus_connected
Expand Down Expand Up @@ -75,7 +75,7 @@ def _merge_settings_attribute(
class NetworkSetting(DBusInterface):
"""Network connection setting object for Network Manager.
https://developer.gnome.org/NetworkManager/stable/gdbus-org.freedesktop.NetworkManager.Settings.Connection.html
https://networkmanager.dev/docs/api/1.48.0/gdbus-org.freedesktop.NetworkManager.Settings.Connection.html
"""

bus_name: str = DBUS_NAME_NM
Expand Down Expand Up @@ -229,11 +229,13 @@ async def reload(self):
if CONF_ATTR_IPV4 in data:
self._ipv4 = IpProperties(
data[CONF_ATTR_IPV4].get(ATTR_METHOD),
data[CONF_ATTR_IPV4].get(ATTR_DNS),
)

if CONF_ATTR_IPV6 in data:
self._ipv6 = IpProperties(
data[CONF_ATTR_IPV6].get(ATTR_METHOD),
data[CONF_ATTR_IPV6].get(ATTR_DNS),
)

if CONF_ATTR_MATCH in data:
Expand Down
30 changes: 20 additions & 10 deletions supervisor/dbus/network/setting/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ def get_connection_from_interface(
ipv4["method"] = Variant("s", "disabled")
else:
ipv4["method"] = Variant("s", "manual")
ipv4["dns"] = Variant(
"au",
[
socket.htonl(int(ip_address))
for ip_address in interface.ipv4.nameservers
],
)

adressdata = []
for address in interface.ipv4.address:
Expand All @@ -96,6 +89,18 @@ def get_connection_from_interface(
ipv4["address-data"] = Variant("aa{sv}", adressdata)
ipv4["gateway"] = Variant("s", str(interface.ipv4.gateway))

if (
interface.ipv4.method == InterfaceMethod.AUTO
and not interface.ipv4.nameservers_auto
) or interface.ipv4.method == InterfaceMethod.STATIC:
ipv4["dns"] = Variant(
"au",
[
socket.htonl(int(ip_address))
for ip_address in interface.ipv4.nameservers
],
)

conn[CONF_ATTR_IPV4] = ipv4

ipv6 = {}
Expand All @@ -105,9 +110,6 @@ def get_connection_from_interface(
ipv6["method"] = Variant("s", "link-local")
else:
ipv6["method"] = Variant("s", "manual")
ipv6["dns"] = Variant(
"aay", [ip_address.packed for ip_address in interface.ipv6.nameservers]
)

adressdata = []
for address in interface.ipv6.address:
Expand All @@ -121,6 +123,14 @@ def get_connection_from_interface(
ipv6["address-data"] = Variant("aa{sv}", adressdata)
ipv6["gateway"] = Variant("s", str(interface.ipv6.gateway))

if (
interface.ipv6.method == InterfaceMethod.AUTO
and not interface.ipv6.nameservers_auto
) or interface.ipv6.method == InterfaceMethod.STATIC:
ipv6["dns"] = Variant(
"aay", [ip_address.packed for ip_address in interface.ipv6.nameservers]
)

conn[CONF_ATTR_IPV6] = ipv6

if interface.type == InterfaceType.ETHERNET:
Expand Down
52 changes: 40 additions & 12 deletions supervisor/host/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..dbus.network.connection import NetworkConnection
from ..dbus.network.interface import NetworkInterface
from .const import AuthMethod, InterfaceMethod, InterfaceType, WifiMode
import socket


@dataclass(slots=True)
Expand All @@ -32,6 +33,7 @@ class IpConfig:
method: InterfaceMethod
address: list[IPv4Interface | IPv6Interface]
gateway: IPv4Address | IPv6Address | None
nameservers_auto: bool
nameservers: list[IPv4Address | IPv6Address]
ready: bool | None

Expand Down Expand Up @@ -102,6 +104,30 @@ def from_dbus_interface(inet: NetworkInterface) -> "Interface":
bool(inet.connection)
and ConnectionStateFlags.IP6_READY in inet.connection.state_flags
)

# Use Nameserver from configuration if present (means manual DNS override)
if inet.settings.ipv4.dns:
ipv4_nameservers_auto = False
ipv4_nameservers = [
IPv4Address(socket.ntohl(ip)) for ip in inet.settings.ipv4.dns
]
elif inet.connection.ipv4.nameservers:
ipv4_nameservers_auto = True
ipv4_nameservers = inet.connection.ipv4.nameservers
else:
ipv4_nameservers_auto = True
ipv4_nameservers = []

if inet.settings.ipv6.dns:
ipv6_nameservers_auto = False
ipv6_nameservers = [IPv6Address(bytes(ip)) for ip in inet.settings.ipv6.dns]
elif inet.connection.ipv6.nameservers:
ipv6_nameservers_auto = True
ipv6_nameservers = inet.connection.ipv6.nameservers
else:
ipv6_nameservers_auto = True
ipv6_nameservers = []

return Interface(
inet.name,
inet.hw_address,
Expand All @@ -111,24 +137,26 @@ def from_dbus_interface(inet: NetworkInterface) -> "Interface":
inet.primary,
Interface._map_nm_type(inet.type),
IpConfig(
ipv4_method,
inet.connection.ipv4.address if inet.connection.ipv4.address else [],
inet.connection.ipv4.gateway,
inet.connection.ipv4.nameservers
if inet.connection.ipv4.nameservers
method=ipv4_method,
address=inet.connection.ipv4.address
if inet.connection.ipv4.address
else [],
ipv4_ready,
gateway=inet.connection.ipv4.gateway,
nameservers_auto=ipv4_nameservers_auto,
nameservers=ipv4_nameservers,
ready=ipv4_ready,
)
if inet.connection and inet.connection.ipv4
else IpConfig(ipv4_method, [], None, [], ipv4_ready),

Check failure on line 150 in supervisor/host/configuration.py

View workflow job for this annotation

GitHub Actions / Check pylint

E1120: No value for argument 'ready' in constructor call (no-value-for-parameter)
IpConfig(
ipv6_method,
inet.connection.ipv6.address if inet.connection.ipv6.address else [],
inet.connection.ipv6.gateway,
inet.connection.ipv6.nameservers
if inet.connection.ipv6.nameservers
method=ipv6_method,
address=inet.connection.ipv6.address
if inet.connection.ipv6.address
else [],
ipv6_ready,
gateway=inet.connection.ipv6.gateway,
nameservers_auto=ipv6_nameservers_auto,
nameservers=ipv6_nameservers,
ready=ipv6_ready,
)
if inet.connection and inet.connection.ipv6
else IpConfig(ipv6_method, [], None, [], ipv6_ready),

Check failure on line 162 in supervisor/host/configuration.py

View workflow job for this annotation

GitHub Actions / Check pylint

E1120: No value for argument 'ready' in constructor call (no-value-for-parameter)
Expand Down

0 comments on commit c1747ab

Please sign in to comment.