From 1224c5a43ad1025d5b2d2e5abbafa3e3258d2188 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Wed, 11 Jun 2025 13:47:23 +0300 Subject: [PATCH] net: dhcpv4: Honor network interface for DNS servers Bind the network interface to the network interface we have received the DNS servers from. This is now the default. The previous behavior can be restored by disabling the CONFIG_NET_DHCPV4_DNS_SERVER_VIA_INTERFACE option. Signed-off-by: Jukka Rissanen --- subsys/net/lib/dhcpv4/Kconfig | 17 +++++++++++++++++ subsys/net/lib/dhcpv4/dhcpv4.c | 20 +++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/subsys/net/lib/dhcpv4/Kconfig b/subsys/net/lib/dhcpv4/Kconfig index 52dcfc099b5..cf7603ae65a 100644 --- a/subsys/net/lib/dhcpv4/Kconfig +++ b/subsys/net/lib/dhcpv4/Kconfig @@ -96,6 +96,23 @@ config NET_DHCPV4_OPTION_PRINT_IGNORED received and ignored. If this is not set, then we print these as unknown options. +config NET_DHCPV4_DNS_SERVER_VIA_INTERFACE + bool "Make DNS servers specific to the network interface" + depends on NET_DHCPV4_OPTION_DNS_ADDRESS + default y + help + If this is set, then if the system has multiple network interfaces + and each has DHCP enabled, then assign DNS servers received from that + network interface, to that specific interface. + If this option is not set, then any interface can be used for all + the configured DNS server addresses when doing DNS queries. + Example: We receive DNS server 192.0.2.53 DHCPv4 option from Wi-Fi + interface and DNS server 198.51.100.53 from Ethernet interface. + When this option is set, the DNS resolver will use DNS server + 192.0.2.53 when sending DNS query to the Wi-Fi interface and DNS + server 198.51.100.53 when sending DNS query to the Ethernet + interface. + endif # NET_DHCPV4 config NET_DHCPV4_SERVER diff --git a/subsys/net/lib/dhcpv4/dhcpv4.c b/subsys/net/lib/dhcpv4/dhcpv4.c index f7d7447b27d..6dedcab3cd9 100644 --- a/subsys/net/lib/dhcpv4/dhcpv4.c +++ b/subsys/net/lib/dhcpv4/dhcpv4.c @@ -1171,7 +1171,25 @@ static bool dhcpv4_parse_options(struct net_pkt *pkt, for (uint8_t i = 0; i < dns_servers_cnt; i++) { dnses[i].sin_family = AF_INET; } - status = dns_resolve_reconfigure(ctx, NULL, dns_servers); + + if (IS_ENABLED(CONFIG_NET_DHCPV4_DNS_SERVER_VIA_INTERFACE)) { + /* If we are using the interface to resolve DNS servers, + * we need to save the interface index. + */ + int ifindex = net_if_get_by_iface(iface); + int interfaces[MAX_DNS_SERVERS]; + + for (uint8_t i = 0; i < dns_servers_cnt; i++) { + interfaces[i] = ifindex; + } + + status = dns_resolve_reconfigure_with_interfaces(ctx, NULL, + dns_servers, + interfaces); + } else { + status = dns_resolve_reconfigure(ctx, NULL, dns_servers); + } + if (status < 0) { NET_DBG("options_dns, failed to set " "resolve address: %d", status);