diff --git a/NEWS b/NEWS
index 740d3099a7..9e988da586 100644
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,10 @@ USE AT YOUR OWN RISK. NOT RECOMMENDED FOR PRODUCTION USE!
* Add new ipv4.shared-dhcp-range and ipv4.shared-dhcp-lease-time,
which allows you to customize the DHCP range and lease time offered
by DHCP server in `shared` connection method.
+* DNS servers can now be specified with a URI-like syntax which supports
+ DNS over TLS name servers.
+* The initrd-generator understands the "rd.net.dns" option to configure
+ global name servers.
=============================================
NetworkManager-1.50
diff --git a/man/NetworkManager.conf.xml b/man/NetworkManager.conf.xml
index 045b7a10cf..ba7bb434dd 100644
--- a/man/NetworkManager.conf.xml
+++ b/man/NetworkManager.conf.xml
@@ -1575,8 +1575,24 @@ managed=1
servers
- A list of addresses of DNS servers to be used for the given domain.
-
+ A list of addresses of DNS servers to be used for the
+ given domain. Each server can be specified either as a
+ plain IP address or with a URI syntax. When it is
+ specified as an URI, the following forms are supported:
+
+
+
+ dns+udp://ADDRESS[:PORT] : DNS
+ dns+tls://ADDRESS[:PORT][#SERVERNAME] : DNS over TLS
+
+
+
+ ADDRESS is an IPv4 or IPv6
+ address. When it is IPv6, it must be enclosed in square
+ brackets ('[' and ']'). When it is a IPv6 link-local
+ address, the address should be followed by a percent
+ sign ('%') and an interface name.
+
diff --git a/man/nm-initrd-generator.xml b/man/nm-initrd-generator.xml
index 51015ea8a3..170cb883e3 100644
--- a/man/nm-initrd-generator.xml
+++ b/man/nm-initrd-generator.xml
@@ -157,6 +157,7 @@
+
@@ -233,6 +234,22 @@
+
+ NetworkManager supports the
+ =SERVER
+ kernel command line option to configure a global (non
+ interface-specific) DNS server. The option can be specified
+ multiple time to add more than one server. Each server can be
+ specified as a plain IP or as an URI according to the
+ description in the "global-dns-domains sections" paragraph of
+ NetworkManager.conf5. For
+ example: rd.net.dns=2001:db8::1,
+ rd.net.dns=dns+tls://192.0.2.0,
+ rd.net.dns=dns+tls://[2001:db8::2]:5353#example.org.
+
+
+
diff --git a/src/core/devices/wifi/nm-device-iwd.c b/src/core/devices/wifi/nm-device-iwd.c
index 20294105b4..a7ea14e4e4 100644
--- a/src/core/devices/wifi/nm-device-iwd.c
+++ b/src/core/devices/wifi/nm-device-iwd.c
@@ -3739,7 +3739,7 @@ nm_device_iwd_parse_netconfig(NMDeviceIwd *self, int addr_family, GVariantIter *
if (inet_pton(addr_family, str_value, &dns_bin) != 1)
goto param_error;
- nm_l3_config_data_add_nameserver_detail(l3cd, addr_family, &dns_bin, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, addr_family, &dns_bin);
nm_l3_config_data_set_dns_priority(l3cd, addr_family, NM_DNS_PRIORITY_DEFAULT_NORMAL);
}
}
diff --git a/src/core/devices/wifi/nm-wifi-utils.c b/src/core/devices/wifi/nm-wifi-utils.c
index 8a8d062f3a..332352ab06 100644
--- a/src/core/devices/wifi/nm-wifi-utils.c
+++ b/src/core/devices/wifi/nm-wifi-utils.c
@@ -1577,19 +1577,17 @@ ip_config_to_iwd_config(int addr_family, GKeyFile *file, NMSettingIPConfig *s_ip
if (num) {
nm_str_buf_reset(&strbuf);
for (i = 0; i < num; i++) {
- char sbuf[NM_INET_ADDRSTRLEN];
- NMIPAddr a;
+ char addrstr[NM_INET_ADDRSTRLEN];
- if (!nm_utils_dnsname_parse_assert(addr_family,
- nm_setting_ip_config_get_dns(s_ip, i),
- NULL,
- &a,
- NULL))
+ if (!nm_dns_uri_parse_plain(addr_family,
+ nm_setting_ip_config_get_dns(s_ip, i),
+ addrstr,
+ NULL))
continue;
if (strbuf.len > 0)
nm_str_buf_append_c(&strbuf, ' ');
- nm_str_buf_append(&strbuf, nm_inet_ntop(addr_family, &a, sbuf));
+ nm_str_buf_append(&strbuf, addrstr);
}
/* It doesn't matter whether we add the DNS under [IPv4] or [IPv6]
* except that with method=auto the list will override the
diff --git a/src/core/devices/wwan/nm-modem-broadband.c b/src/core/devices/wwan/nm-modem-broadband.c
index bc2fcd6a84..8ea16ee75d 100644
--- a/src/core/devices/wwan/nm-modem-broadband.c
+++ b/src/core/devices/wwan/nm-modem-broadband.c
@@ -1162,7 +1162,7 @@ stage3_ip_config_start(NMModem *modem, int addr_family, NMModemIPMethod ip_metho
dns = mm_bearer_ip_config_get_dns(self->_priv.ipv4_config);
for (i = 0; dns && dns[i]; i++) {
if (nm_inet_parse_bin(AF_INET, dns[i], NULL, &address_network) && address_network > 0) {
- nm_l3_config_data_add_nameserver_detail(l3cd, AF_INET, &address_network, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, AF_INET, &address_network);
_LOGI(" DNS %s", dns[i]);
}
}
@@ -1281,7 +1281,7 @@ stage3_ip_config_start(NMModem *modem, int addr_family, NMModemIPMethod ip_metho
struct in6_addr addr;
if (inet_pton(AF_INET6, dns[i], &addr)) {
- nm_l3_config_data_add_nameserver_detail(l3cd, AF_INET6, &addr, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, AF_INET6, &addr);
_LOGI(" DNS %s", dns[i]);
}
}
diff --git a/src/core/devices/wwan/nm-modem-ofono.c b/src/core/devices/wwan/nm-modem-ofono.c
index 70d024c6ee..80f966f125 100644
--- a/src/core/devices/wwan/nm-modem-ofono.c
+++ b/src/core/devices/wwan/nm-modem-ofono.c
@@ -1315,7 +1315,7 @@ handle_settings(NMModemOfono *self, GVariant *v_dict)
}
any_good = TRUE;
_LOGI("DNS: %s", array[i]);
- nm_l3_config_data_add_nameserver_detail(priv->l3cd_4, AF_INET, &address_network, NULL);
+ nm_l3_config_data_add_nameserver_addr(priv->l3cd_4, AF_INET, &address_network);
}
if (!any_good) {
_LOGW("Settings: 'DomainNameServers': none specified");
diff --git a/src/core/dhcp/nm-dhcp-nettools.c b/src/core/dhcp/nm-dhcp-nettools.c
index 32c02d7bd6..61490fb662 100644
--- a/src/core/dhcp/nm-dhcp-nettools.c
+++ b/src/core/dhcp/nm-dhcp-nettools.c
@@ -387,7 +387,7 @@ lease_parse_address_list(NDhcp4ClientLease *lease,
nm_inet4_ntop(addr, addr_str));
continue;
}
- nm_l3_config_data_add_nameserver_detail(l3cd, AF_INET, &addr, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, AF_INET, &addr);
break;
case NM_DHCP_OPTION_DHCP4_NIS_SERVERS:
nm_l3_config_data_add_nis_server(l3cd, addr);
diff --git a/src/core/dhcp/nm-dhcp-systemd.c b/src/core/dhcp/nm-dhcp-systemd.c
index 5ede0df916..e17615239a 100644
--- a/src/core/dhcp/nm-dhcp-systemd.c
+++ b/src/core/dhcp/nm-dhcp-systemd.c
@@ -157,7 +157,7 @@ lease_to_ip6_config(NMDhcpSystemd *self, sd_dhcp6_lease *lease, gint32 ts, GErro
for (i = 0; i < num; i++) {
nm_inet6_ntop(&dns[i], addr_str);
g_string_append(nm_gstring_add_space_delimiter(str), addr_str);
- nm_l3_config_data_add_nameserver_detail(l3cd, AF_INET6, &dns[i], NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, AF_INET6, &dns[i]);
}
nm_dhcp_option_add_option(options,
TRUE,
diff --git a/src/core/dhcp/nm-dhcp-utils.c b/src/core/dhcp/nm-dhcp-utils.c
index 6bac4d077b..15293fa389 100644
--- a/src/core/dhcp/nm-dhcp-utils.c
+++ b/src/core/dhcp/nm-dhcp-utils.c
@@ -499,7 +499,7 @@ nm_dhcp_utils_ip4_config_from_options(NMDedupMultiIndex *multi_idx,
for (s = dns; dns && *s; s++) {
if (inet_pton(AF_INET, *s, &tmp_addr) > 0) {
if (tmp_addr) {
- nm_l3_config_data_add_nameserver_detail(l3cd, AF_INET, &tmp_addr, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, AF_INET, &tmp_addr);
_LOG2I(LOGD_DHCP4, iface, " nameserver '%s'", *s);
}
} else
@@ -704,7 +704,7 @@ nm_dhcp_utils_ip6_config_from_options(NMDedupMultiIndex *multi_idx,
for (s = dns; dns && *s; s++) {
if (inet_pton(AF_INET6, *s, &tmp_addr) > 0) {
if (!IN6_IS_ADDR_UNSPECIFIED(&tmp_addr)) {
- nm_l3_config_data_add_nameserver_detail(l3cd, AF_INET6, &tmp_addr, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, AF_INET6, &tmp_addr);
_LOG2I(LOGD_DHCP6, iface, " nameserver '%s'", *s);
}
} else
diff --git a/src/core/dns/nm-dns-dnsmasq.c b/src/core/dns/nm-dns-dnsmasq.c
index 43cff2e453..8e3c10c98c 100644
--- a/src/core/dns/nm-dns-dnsmasq.c
+++ b/src/core/dns/nm-dns-dnsmasq.c
@@ -853,13 +853,16 @@ add_global_config(NMDnsDnsmasq *self,
const char *const *servers = nm_global_dns_domain_get_servers(domain);
const char *name = nm_global_dns_domain_get_name(domain);
- g_return_if_fail(name);
+ nm_assert(name);
for (j = 0; servers && servers[j]; j++) {
- if (!strcmp(name, "*"))
- add_dnsmasq_nameserver(self, dnsmasq_servers, servers[j], NULL);
- else
- add_dnsmasq_nameserver(self, dnsmasq_servers, servers[j], name);
+ char str[NM_INET_ADDRSTRLEN];
+
+ /* TODO: support IPv6 link-local addresses with scope id */
+ if (!nm_dns_uri_parse_plain(AF_UNSPEC, servers[j], str, NULL))
+ continue;
+
+ add_dnsmasq_nameserver(self, dnsmasq_servers, str, nm_streq(name, "*") ? NULL : name);
}
}
}
@@ -881,7 +884,7 @@ add_ip_config(NMDnsDnsmasq *self, GVariantBuilder *servers, const NMDnsConfigIPD
for (i = 0; i < num; i++) {
NMIPAddr a;
- if (!nm_utils_dnsname_parse_assert(ip_data->addr_family, strarr[i], NULL, &a, NULL))
+ if (!nm_dns_uri_parse_plain(ip_data->addr_family, strarr[i], NULL, &a))
continue;
ip_addr_to_string(ip_data->addr_family, &a, iface, ip_addr_to_string_buf);
diff --git a/src/core/dns/nm-dns-manager.c b/src/core/dns/nm-dns-manager.c
index 531e1873ac..704d030bbe 100644
--- a/src/core/dns/nm-dns-manager.c
+++ b/src/core/dns/nm-dns-manager.c
@@ -600,7 +600,7 @@ merge_one_l3cd(NMResolvConfData *rc, int addr_family, int ifindex, const NML3Con
for (i = 0; i < num_nameservers; i++) {
NMIPAddr a;
- if (!nm_utils_dnsname_parse_assert(addr_family, strarr[i], NULL, &a, NULL))
+ if (!nm_dns_uri_parse_plain(addr_family, strarr[i], NULL, &a))
continue;
if (addr_family == AF_INET)
@@ -1291,8 +1291,15 @@ merge_global_dns_config(NMResolvConfData *rc, NMGlobalDnsConfig *global_conf)
if (!servers)
return TRUE;
- for (i = 0; servers[i]; i++)
- add_string_item(rc->nameservers, servers[i], TRUE);
+ for (i = 0; servers[i]; i++) {
+ char addrstr[NM_INET_ADDRSTRLEN];
+
+ /* TODO: support IPv6 link-local addresses with scope id */
+ if (!nm_dns_uri_parse_plain(AF_UNSPEC, servers[i], addrstr, NULL))
+ continue;
+
+ add_string_item(rc->nameservers, addrstr, TRUE);
+ }
return TRUE;
}
@@ -1300,7 +1307,6 @@ merge_global_dns_config(NMResolvConfData *rc, NMGlobalDnsConfig *global_conf)
static const char *
get_nameserver_list(int addr_family, const NML3ConfigData *l3cd, NMStrBuf *tmp_strbuf)
{
- char buf[NM_INET_ADDRSTRLEN];
guint num;
guint i;
const char *const *strarr;
@@ -1309,15 +1315,9 @@ get_nameserver_list(int addr_family, const NML3ConfigData *l3cd, NMStrBuf *tmp_s
strarr = nm_l3_config_data_get_nameservers(l3cd, addr_family, &num);
for (i = 0; i < num; i++) {
- NMIPAddr a;
-
- if (!nm_utils_dnsname_parse_assert(addr_family, strarr[i], NULL, &a, NULL))
- continue;
-
- nm_inet_ntop(addr_family, &a, buf);
if (i > 0)
nm_str_buf_append_c(tmp_strbuf, ' ');
- nm_str_buf_append(tmp_strbuf, buf);
+ nm_str_buf_append(tmp_strbuf, strarr[i]);
}
nm_str_buf_maybe_expand(tmp_strbuf, 1, FALSE);
@@ -2730,7 +2730,6 @@ _get_config_variant(NMDnsManager *self)
guint num_domains;
guint num_searches;
guint i;
- char buf[NM_INET_ADDRSTRLEN];
const char *ifname;
const char *const *strarr;
@@ -2742,12 +2741,7 @@ _get_config_variant(NMDnsManager *self)
g_variant_builder_init(&strv_builder, G_VARIANT_TYPE("as"));
for (i = 0; i < num; i++) {
- NMIPAddr a;
-
- if (!nm_utils_dnsname_parse_assert(ip_data->addr_family, strarr[i], NULL, &a, NULL))
- continue;
-
- g_variant_builder_add(&strv_builder, "s", nm_inet_ntop(ip_data->addr_family, &a, buf));
+ g_variant_builder_add(&strv_builder, "s", strarr[i]);
}
g_variant_builder_add(&entry_builder,
"{sv}",
diff --git a/src/core/dns/nm-dns-systemd-resolved.c b/src/core/dns/nm-dns-systemd-resolved.c
index 49c1094de5..0701a1dcb5 100644
--- a/src/core/dns/nm-dns-systemd-resolved.c
+++ b/src/core/dns/nm-dns-systemd-resolved.c
@@ -396,13 +396,24 @@ update_add_ip_config(NMDnsSystemdResolved *self,
strarr = nm_l3_config_data_get_nameservers(ip_data->l3cd, ip_data->addr_family, &n);
for (i = 0; i < n; i++) {
- const char *server_name;
- NMIPAddr a;
+ NMDnsServer dns_server;
- if (!nm_utils_dnsname_parse_assert(ip_data->addr_family, strarr[i], NULL, &a, &server_name))
+ if (!nm_dns_uri_parse(ip_data->addr_family, strarr[i], &dns_server))
continue;
- if (server_name) {
+ if (!NM_IN_SET(dns_server.scheme,
+ NM_DNS_URI_SCHEME_TLS,
+ NM_DNS_URI_SCHEME_NONE,
+ NM_DNS_URI_SCHEME_UDP)) {
+ /* In systemd-resolved, the use of DNS-over-TLS can't be controlled
+ * for each name server; it is controlled via a per-link knob.
+ * Therefore, we pass all the addresses we know about and then let
+ * systemd-resolved decide whether to use DoT, based on the
+ * "connection.dns-over-tls" property. */
+ continue;
+ }
+
+ if (dns_server.servername) {
NM_SET_OUT(out_require_dns_ex, TRUE);
if (priv->has_set_link_dns_ex == FALSE) {
/* The caller won't care about this result anymore. We can skip setting it. */
@@ -413,15 +424,19 @@ update_add_ip_config(NMDnsSystemdResolved *self,
if (dns_ex) {
g_variant_builder_open(dns_ex, G_VARIANT_TYPE("(iayqs)"));
g_variant_builder_add(dns_ex, "i", ip_data->addr_family);
- g_variant_builder_add_value(dns_ex, nm_g_variant_new_ay((gconstpointer) &a, addr_size));
+ g_variant_builder_add_value(
+ dns_ex,
+ nm_g_variant_new_ay((gconstpointer) &dns_server.addr, addr_size));
g_variant_builder_add(dns_ex, "q", 0);
- g_variant_builder_add(dns_ex, "s", server_name ?: "");
+ g_variant_builder_add(dns_ex, "s", dns_server.servername ?: "");
g_variant_builder_close(dns_ex);
}
if (dns) {
g_variant_builder_open(dns, G_VARIANT_TYPE("(iay)"));
g_variant_builder_add(dns, "i", ip_data->addr_family);
- g_variant_builder_add_value(dns, nm_g_variant_new_ay((gconstpointer) &a, addr_size));
+ g_variant_builder_add_value(
+ dns,
+ nm_g_variant_new_ay((gconstpointer) &dns_server.addr, addr_size));
g_variant_builder_close(dns);
}
has_config = TRUE;
diff --git a/src/core/dnsmasq/nm-dnsmasq-manager.c b/src/core/dnsmasq/nm-dnsmasq-manager.c
index 3566b62480..030a8e28e1 100644
--- a/src/core/dnsmasq/nm-dnsmasq-manager.c
+++ b/src/core/dnsmasq/nm-dnsmasq-manager.c
@@ -102,7 +102,6 @@ create_dm_cmd_line(const char *iface,
char first[INET_ADDRSTRLEN];
char last[INET_ADDRSTRLEN];
char listen_address_s[INET_ADDRSTRLEN];
- char sbuf_addr[INET_ADDRSTRLEN];
gs_free char *error_desc = NULL;
const char *dm_binary;
const NMPlatformIP4Address *listen_address;
@@ -191,13 +190,13 @@ create_dm_cmd_line(const char *iface,
nm_gstring_prepare(&s);
g_string_append(s, "--dhcp-option=option:dns-server");
for (i = 0; i < n; i++) {
- in_addr_t a;
+ char addrstr[NM_INET_ADDRSTRLEN];
- if (!nm_utils_dnsname_parse_assert(AF_INET, strarr[i], NULL, &a, NULL))
+ if (!nm_dns_uri_parse_plain(AF_INET, strarr[i], addrstr, NULL))
continue;
g_string_append_c(s, ',');
- g_string_append(s, nm_inet4_ntop(a, sbuf_addr));
+ g_string_append(s, addrstr);
}
nm_strv_ptrarray_take_gstring(cmd, &s);
}
diff --git a/src/core/ndisc/nm-ndisc.c b/src/core/ndisc/nm-ndisc.c
index 8a4eb8e435..962058d66d 100644
--- a/src/core/ndisc/nm-ndisc.c
+++ b/src/core/ndisc/nm-ndisc.c
@@ -205,10 +205,7 @@ nm_ndisc_data_to_l3cd(NMDedupMultiIndex *multi_idx,
}
for (i = 0; i < rdata->dns_servers_n; i++) {
- nm_l3_config_data_add_nameserver_detail(l3cd,
- AF_INET6,
- &rdata->dns_servers[i].address,
- NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, AF_INET6, &rdata->dns_servers[i].address);
}
for (i = 0; i < rdata->dns_domains_n; i++)
@@ -1106,14 +1103,14 @@ nm_ndisc_set_config(NMNDisc *ndisc, const NML3ConfigData *l3cd)
if (l3cd)
strvarr = nm_l3_config_data_get_nameservers(l3cd, AF_INET6, &len);
for (i = 0; i < len; i++) {
- struct in6_addr a;
+ NMIPAddr a;
NMNDiscDNSServer n;
- if (!nm_utils_dnsname_parse_assert(AF_INET6, strvarr[i], NULL, &a, NULL))
+ if (!nm_dns_uri_parse_plain(AF_INET6, strvarr[i], NULL, &a))
continue;
n = (NMNDiscDNSServer) {
- .address = a,
+ .address = a.addr6,
.expiry_msec = _nm_ndisc_lifetime_to_expiry(NM_NDISC_EXPIRY_BASE_TIMESTAMP,
NM_NDISC_ROUTER_LIFETIME),
};
diff --git a/src/core/nm-config-data.c b/src/core/nm-config-data.c
index 914e66b04a..e404928517 100644
--- a/src/core/nm-config-data.c
+++ b/src/core/nm-config-data.c
@@ -1259,10 +1259,19 @@ load_global_dns(GKeyFile *keyfile, gboolean internal)
if (strv) {
nm_strv_cleanup(strv, TRUE, TRUE, TRUE);
for (i = 0, j = 0; strv[i]; i++) {
- if (nm_inet_is_valid(AF_INET, strv[i]) || nm_inet_is_valid(AF_INET6, strv[i]))
- strv[j++] = strv[i];
- else
+ gs_free char *to_free = NULL;
+
+ if (nm_dns_uri_normalize(AF_UNSPEC, strv[i], &to_free)) {
+ if (to_free) {
+ g_free(strv[i]);
+ strv[j++] = g_steal_pointer(&to_free);
+ } else {
+ strv[j++] = strv[i];
+ }
+ } else {
+ nm_log_dbg(LOGD_CORE, "invalid global name server \"%s\"", strv[i]);
g_free(strv[i]);
+ }
}
if (j == 0)
g_free(strv);
diff --git a/src/core/nm-dispatcher.c b/src/core/nm-dispatcher.c
index 4f442c685a..16fb7e96b8 100644
--- a/src/core/nm-dispatcher.c
+++ b/src/core/nm-dispatcher.c
@@ -247,6 +247,20 @@ dump_ip_to_props(const NML3ConfigData *l3cd, int addr_family, GVariantBuilder *b
}
g_variant_builder_add(builder, "{sv}", "addresses", g_variant_builder_end(&int_builder));
+ /* We used to send name servers as a entry with key "nameservers" and binary
+ * value. That no longer works because name servers can be URIs. Send the
+ * value as an array of strings.
+ * To avoid problems when the NM and NM-dispatcher version don't match (right
+ * after an upgrade or downgrade), still send the old key in the old format,
+ * and introduce a new key for the new format. */
+ g_variant_builder_init(&int_builder, G_VARIANT_TYPE("as"));
+ strarr = nm_l3_config_data_get_nameservers(l3cd, addr_family, &n);
+ for (i = 0; i < n; i++)
+ g_variant_builder_add(&int_builder, "s", strarr[i]);
+ g_variant_builder_add(builder, "{sv}", "nameservers-full", g_variant_builder_end(&int_builder));
+
+ /* Old format for nameservers. This can be removed in the future when it's
+ * expected that both NM and NM-dispatcher support the new format.*/
if (IS_IPv4)
g_variant_builder_init(&int_builder, G_VARIANT_TYPE("au"));
else
@@ -255,7 +269,7 @@ dump_ip_to_props(const NML3ConfigData *l3cd, int addr_family, GVariantBuilder *b
for (i = 0; i < n; i++) {
NMIPAddr a;
- if (!nm_utils_dnsname_parse_assert(addr_family, strarr[i], NULL, &a, NULL))
+ if (!nm_dns_uri_parse_plain(addr_family, strarr[i], NULL, &a))
continue;
if (IS_IPv4)
diff --git a/src/core/nm-ip-config.c b/src/core/nm-ip-config.c
index 8cd27ac4f7..eb0ec9aa03 100644
--- a/src/core/nm-ip-config.c
+++ b/src/core/nm-ip-config.c
@@ -449,22 +449,27 @@ get_property_ip4(GObject *object, guint prop_id, GValue *value, GParamSpec *pspe
else
g_variant_builder_init(&builder, G_VARIANT_TYPE("aa{sv}"));
for (i = 0; i < len; i++) {
- in_addr_t a;
+ NMIPAddr a;
- if (!nm_utils_dnsname_parse_assert(AF_INET, strarr[i], NULL, &a, NULL))
- continue;
-
- if (prop_id == PROP_IP4_NAMESERVERS)
+ if (prop_id == PROP_IP4_NAMESERVERS) {
+ if (!nm_dns_uri_parse_plain(AF_INET, strarr[i], NULL, &a))
+ continue;
g_variant_builder_add(&builder, "u", a);
- else {
+ } else {
GVariantBuilder nested_builder;
+ char addrstr[NM_INET_ADDRSTRLEN];
- nm_inet4_ntop(a, addr_str);
g_variant_builder_init(&nested_builder, G_VARIANT_TYPE("a{sv}"));
+ if (nm_dns_uri_parse_plain(AF_INET, strarr[i], addrstr, NULL)) {
+ g_variant_builder_add(&nested_builder,
+ "{sv}",
+ "address",
+ g_variant_new_string(addrstr));
+ }
g_variant_builder_add(&nested_builder,
"{sv}",
- "address",
- g_variant_new_string(addr_str));
+ "uri",
+ g_variant_new_string(strarr[i]));
g_variant_builder_add(&builder, "a{sv}", &nested_builder);
}
}
@@ -692,12 +697,13 @@ get_property_ip6(GObject *object, guint prop_id, GValue *value, GParamSpec *pspe
else {
g_variant_builder_init(&builder, G_VARIANT_TYPE("aay"));
for (i = 0; i < len; i++) {
- struct in6_addr a;
+ NMIPAddr a;
- if (!nm_utils_dnsname_parse_assert(AF_INET6, strarr[i], NULL, &a, NULL))
+ /* TODO: expose the full URI as well */
+ if (!nm_dns_uri_parse_plain(AF_INET6, strarr[i], NULL, &a))
continue;
- g_variant_builder_add(&builder, "@ay", nm_g_variant_new_ay_in6addr(&a));
+ g_variant_builder_add(&builder, "@ay", nm_g_variant_new_ay_in6addr(&a.addr6));
}
g_value_take_variant(value, g_variant_builder_end(&builder));
}
diff --git a/src/core/nm-l3-config-data.c b/src/core/nm-l3-config-data.c
index ea4f6a1a90..70867ff70e 100644
--- a/src/core/nm-l3-config-data.c
+++ b/src/core/nm-l3-config-data.c
@@ -1460,8 +1460,7 @@ nm_l3_config_data_add_nameserver(NML3ConfigData *self, int addr_family, const ch
if (NM_MORE_ASSERTS > 5) {
gs_free char *s_free = NULL;
- nm_assert(
- nm_streq0(nm_utils_dnsname_normalize(addr_family, nameserver, &s_free), nameserver));
+ nm_assert(nm_streq0(nm_dns_uri_normalize(addr_family, nameserver, &s_free), nameserver));
}
p_arr = &self->nameservers_x[NM_IS_IPv4(addr_family)];
@@ -1474,27 +1473,16 @@ nm_l3_config_data_add_nameserver(NML3ConfigData *self, int addr_family, const ch
}
gboolean
-nm_l3_config_data_add_nameserver_detail(NML3ConfigData *self,
- int addr_family,
- gconstpointer addr_bin,
- const char *server_name)
+nm_l3_config_data_add_nameserver_addr(NML3ConfigData *self, int addr_family, gconstpointer addr_bin)
{
- gs_free char *s_free = NULL;
- char *s;
- gsize l;
+ char addrstr[NM_INET_ADDRSTRLEN];
nm_assert(_NM_IS_L3_CONFIG_DATA(self, FALSE));
nm_assert_addr_family(addr_family);
nm_assert(addr_bin);
- l = (NM_INET_ADDRSTRLEN + 2u) + (server_name ? strlen(server_name) : 0u);
-
- s = nm_malloc_maybe_a(300, l, &s_free);
-
- if (!nm_utils_dnsname_construct(addr_family, addr_bin, server_name, s, l))
- nm_assert_not_reached();
-
- return nm_l3_config_data_add_nameserver(self, addr_family, s);
+ nm_inet_ntop(addr_family, addr_bin, addrstr);
+ return nm_l3_config_data_add_nameserver(self, addr_family, addrstr);
}
gboolean
diff --git a/src/core/nm-l3-config-data.h b/src/core/nm-l3-config-data.h
index 0fadadd51d..bcb6af6777 100644
--- a/src/core/nm-l3-config-data.h
+++ b/src/core/nm-l3-config-data.h
@@ -500,10 +500,9 @@ nm_l3_config_data_get_nameservers(const NML3ConfigData *self, int addr_family, g
gboolean
nm_l3_config_data_add_nameserver(NML3ConfigData *self, int addr_family, const char *nameserver);
-gboolean nm_l3_config_data_add_nameserver_detail(NML3ConfigData *self,
- int addr_family,
- gconstpointer addr_bin,
- const char *server_name);
+gboolean nm_l3_config_data_add_nameserver_addr(NML3ConfigData *self,
+ int addr_family,
+ gconstpointer addr_bin);
gboolean nm_l3_config_data_clear_nameservers(NML3ConfigData *self, int addr_family);
diff --git a/src/core/nm-l3cfg.c b/src/core/nm-l3cfg.c
index 48a50a5998..c7ab976187 100644
--- a/src/core/nm-l3cfg.c
+++ b/src/core/nm-l3cfg.c
@@ -3938,10 +3938,10 @@ _l3cfg_routed_dns(NML3Cfg *self, NML3ConfigData *l3cd)
NMPlatformIPXRoute route_new;
char addr_buf[INET6_ADDRSTRLEN];
char route_buf[128];
- NMIPAddr addr;
+ NMDnsServer dns;
int r;
- if (!nm_utils_dnsname_parse_assert(addr_family, nameservers[i], NULL, &addr, NULL))
+ if (!nm_dns_uri_parse(addr_family, nameservers[i], &dns))
continue;
/* Find the gateway to the DNS over the current interface. When
@@ -3950,13 +3950,13 @@ _l3cfg_routed_dns(NML3Cfg *self, NML3ConfigData *l3cd)
* the table containing DNS routes. */
r = nm_platform_ip_route_get(self->priv.platform,
addr_family,
- &addr,
+ &dns.addr,
DNS_ROUTES_FWMARK_TABLE_PRIO,
self->priv.ifindex,
&obj);
if (r < 0) {
_LOGT("could not get route to DNS %s",
- nm_inet_ntop(addr_family, addr.addr_ptr, addr_buf));
+ nm_inet_ntop(addr_family, dns.addr.addr_ptr, addr_buf));
continue;
}
@@ -3964,7 +3964,7 @@ _l3cfg_routed_dns(NML3Cfg *self, NML3ConfigData *l3cd)
if (IS_IPv4) {
route_new.r4 = (NMPlatformIP4Route) {
- .network = addr.addr4,
+ .network = dns.addr.addr4,
.plen = 32,
.table_any = FALSE,
.metric_any = TRUE,
@@ -3976,7 +3976,7 @@ _l3cfg_routed_dns(NML3Cfg *self, NML3ConfigData *l3cd)
nm_platform_ip_route_normalize(addr_family, &route_new.rx);
_LOGT("route to %s: %s",
- nm_inet4_ntop(addr.addr4, addr_buf),
+ nm_inet4_ntop(dns.addr.addr4, addr_buf),
nm_platform_ip4_route_to_string(&route_new.r4, route_buf, sizeof(route_buf)));
nm_l3_config_data_add_route_4(l3cd, &route_new.r4);
@@ -3987,7 +3987,7 @@ _l3cfg_routed_dns(NML3Cfg *self, NML3ConfigData *l3cd)
route_added = TRUE;
} else {
route_new.r6 = (NMPlatformIP6Route) {
- .network = addr.addr6,
+ .network = dns.addr.addr6,
.plen = 128,
.table_any = FALSE,
.metric_any = TRUE,
@@ -3999,7 +3999,7 @@ _l3cfg_routed_dns(NML3Cfg *self, NML3ConfigData *l3cd)
nm_platform_ip_route_normalize(addr_family, &route_new.rx);
_LOGT("route to %s: %s",
- nm_inet6_ntop(&addr.addr6, addr_buf),
+ nm_inet6_ntop(&dns.addr.addr6, addr_buf),
nm_platform_ip6_route_to_string(&route_new.r6, route_buf, sizeof(route_buf)));
nm_l3_config_data_add_route_6(l3cd, &route_new.r6);
diff --git a/src/core/ppp/nm-ppp-manager.c b/src/core/ppp/nm-ppp-manager.c
index aa95cab28f..2eebd17d0b 100644
--- a/src/core/ppp/nm-ppp-manager.c
+++ b/src/core/ppp/nm-ppp-manager.c
@@ -583,7 +583,7 @@ impl_ppp_manager_set_ip4_config(NMDBusObject *obj,
if (g_variant_lookup(config_dict, NM_PPP_IP4_CONFIG_DNS, "au", &iter)) {
while (g_variant_iter_next(iter, "u", &u32))
- nm_l3_config_data_add_nameserver_detail(l3cd, AF_INET, &u32, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, AF_INET, &u32);
g_variant_iter_free(iter);
}
diff --git a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
index 81964de6c3..d64052cbde 100644
--- a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
+++ b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
@@ -2056,9 +2056,8 @@ make_ip4_setting(shvarFile *ifcfg,
* Pick up just IPv4 addresses (IPv6 addresses are taken by make_ip6_setting())
*/
for (i = 1; i < 10000; i++) {
- int af;
- NMIPAddr ip;
- char tag[256];
+ NMDnsServer dns;
+ char tag[256];
numbered_tag(tag, "DNS", i);
nm_clear_g_free(&value);
@@ -2066,14 +2065,16 @@ make_ip4_setting(shvarFile *ifcfg,
if (!v)
break;
- if (!nm_utils_dnsname_parse(AF_UNSPEC, v, &af, &ip, NULL)) {
+ if (!nm_dns_uri_parse(AF_UNSPEC, v, &dns)) {
g_set_error(error,
NM_SETTINGS_ERROR,
NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid DNS server address '%s'",
v);
return NULL;
- } else if (af == AF_INET) {
+ }
+
+ if (dns.addr_family == AF_INET) {
if (!nm_setting_ip_config_add_dns(s_ip4, v))
PARSE_WARNING("duplicate DNS server %s", tag);
} else {
@@ -2606,9 +2607,8 @@ make_ip6_setting(shvarFile *ifcfg, shvarFile *network_ifcfg, gboolean routes_rea
* Pick up just IPv6 addresses (IPv4 addresses are taken by make_ip4_setting())
*/
for (i = 1; i < 10000; i++) {
- int af;
- NMIPAddr ip;
- char tag[256];
+ NMDnsServer dns;
+ char tag[256];
numbered_tag(tag, "DNS", i);
nm_clear_g_free(&value);
@@ -2616,7 +2616,7 @@ make_ip6_setting(shvarFile *ifcfg, shvarFile *network_ifcfg, gboolean routes_rea
if (!v)
break;
- if (!nm_utils_dnsname_parse(AF_UNSPEC, v, &af, &ip, NULL)) {
+ if (!nm_dns_uri_parse(AF_UNSPEC, v, &dns)) {
if (is_disabled)
continue;
g_set_error(error,
@@ -2625,7 +2625,8 @@ make_ip6_setting(shvarFile *ifcfg, shvarFile *network_ifcfg, gboolean routes_rea
"Invalid DNS server address '%s'",
v);
return NULL;
- } else if (af == AF_INET6) {
+ }
+ if (dns.addr_family == AF_INET6) {
if (is_disabled) {
PARSE_WARNING("ignore DNS server addresses with method disabled/ignore");
break;
diff --git a/src/core/vpn/nm-vpn-connection.c b/src/core/vpn/nm-vpn-connection.c
index 724af51e22..d0607160cd 100644
--- a/src/core/vpn/nm-vpn-connection.c
+++ b/src/core/vpn/nm-vpn-connection.c
@@ -2060,14 +2060,14 @@ _dbus_signal_ip_config_cb(NMVpnConnection *self, int addr_family, GVariant *dict
if (IS_IPv4) {
if (g_variant_lookup(dict, NM_VPN_PLUGIN_IP4_CONFIG_DNS, "au", &var_iter)) {
while (g_variant_iter_next(var_iter, "u", &u32))
- nm_l3_config_data_add_nameserver_detail(l3cd, addr_family, &u32, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, addr_family, &u32);
g_variant_iter_free(var_iter);
}
} else {
if (g_variant_lookup(dict, NM_VPN_PLUGIN_IP6_CONFIG_DNS, "aay", &var_iter)) {
while (g_variant_iter_next(var_iter, "@ay", &v)) {
if (nm_ip_addr_set_from_variant(AF_INET6, &v_addr, v, NULL))
- nm_l3_config_data_add_nameserver_detail(l3cd, addr_family, &v_addr, NULL);
+ nm_l3_config_data_add_nameserver_addr(l3cd, addr_family, &v_addr);
g_variant_unref(v);
}
g_variant_iter_free(var_iter);
diff --git a/src/libnm-client-impl/nm-ip-config.c b/src/libnm-client-impl/nm-ip-config.c
index c054f2ccd6..ad5f08f814 100644
--- a/src/libnm-client-impl/nm-ip-config.c
+++ b/src/libnm-client-impl/nm-ip-config.c
@@ -166,25 +166,32 @@ _notify_update_prop_nameservers(NMClient *client,
g_variant_iter_init(&iter, value);
while (g_variant_iter_next(&iter, "a{sv}", &iter_v)) {
- const char *key;
- GVariant *val;
+ const char *key;
+ GVariant *val;
+ gs_free char *nameserver = NULL;
while (g_variant_iter_next(iter_v, "{&sv}", &key, &val)) {
- if (nm_streq(key, "address")) {
+ if (nm_streq(key, "address") && !nameserver) {
gs_free char *val_str = NULL;
if (!g_variant_is_of_type(val, G_VARIANT_TYPE_STRING))
goto next;
if (!nm_inet_parse_str(AF_INET, g_variant_get_string(val, NULL), &val_str))
goto next;
- if (!arr)
- arr = g_ptr_array_new();
- g_ptr_array_add(arr, g_steal_pointer(&val_str));
- goto next;
+ nameserver = g_steal_pointer(&val_str);
+ } else if (nm_streq(key, "uri")) {
+ nameserver = g_variant_dup_string(val, NULL);
}
next:
g_variant_unref(val);
}
+
+ if (nameserver) {
+ if (!arr)
+ arr = g_ptr_array_new();
+ g_ptr_array_add(arr, g_steal_pointer(&nameserver));
+ }
+
g_variant_iter_free(iter_v);
}
if (arr && arr->len > 0)
diff --git a/src/libnm-core-aux-intern/nm-libnm-core-utils.c b/src/libnm-core-aux-intern/nm-libnm-core-utils.c
index 3d619a3c1b..5e7b90a497 100644
--- a/src/libnm-core-aux-intern/nm-libnm-core-utils.c
+++ b/src/libnm-core-aux-intern/nm-libnm-core-utils.c
@@ -700,112 +700,295 @@ nm_mptcp_flags_normalize(NMMptcpFlags flags)
/*****************************************************************************/
+/*
+ * nm_dns_uri_parse:
+ * @addr_family: the address family, or AF_UNSPEC to autodetect it
+ * @str: the name server URI string to parse
+ * @dns: the name server descriptor to fill, or %NULL
+ *
+ * Parses the given name server URI string. Each name server is represented
+ * by the following grammar:
+ *
+ * NAMESERVER := { PLAIN | TLS_URI | UDP_URI }
+ * PLAIN := { ipv4address | ipv6address } [ '#' SERVERNAME ]
+ * TLS_URI := 'dns+tls://' URI_ADDRESS [ ':' PORT ] [ '#' SERVERNAME ]
+ * UDP_URI := 'dns+udp://' URI_ADDRESS [ ':' PORT ]
+ * URI_ADDRESS := { ipv4address | '[' ipv6address [ '%' ifname ] ']' }
+ *
+ * Examples:
+ *
+ * 192.0.2.0
+ * 192.0.2.0#example.com
+ * 2001:db8::1
+ * dns+tls://192.0.2.0
+ * dns+tls://[2001:db8::1]
+ * dns+tls://192.0.2.0:53#example.com
+ * dns+udp://[fe80::1%enp1s0]
+ *
+ * Note that on return, the lifetime of the members in the @dns struct is
+ * the same as the input string @str.
+ *
+ * Returns: %TRUE on success, %FALSE on failure
+ */
gboolean
-nm_utils_dnsname_parse(int addr_family,
- const char *dns,
- int *out_addr_family,
- gpointer /* (NMIPAddr **) */ out_addr,
- const char **out_servername)
+nm_dns_uri_parse(int addr_family, const char *str, NMDnsServer *dns)
{
- gs_free char *dns_heap = NULL;
- const char *s;
- NMIPAddr addr;
+ NMDnsServer dns_stack;
+ gs_free char *addr_port_heap = NULL;
+ gs_free char *addr_heap = NULL;
+ const char *addr_port;
+ const char *addr;
+ const char *name;
+ const char *port;
nm_assert_addr_family_or_unspec(addr_family);
- nm_assert(!out_addr || out_addr_family || NM_IN_SET(addr_family, AF_INET, AF_INET6));
if (!dns)
+ dns = &dns_stack;
+
+ if (!str)
return FALSE;
- s = strchr(dns, '#');
+ *dns = (NMDnsServer) {
+ .port = -1,
+ };
- if (s) {
- dns = nm_strndup_a(200, dns, s - dns, &dns_heap);
- s++;
+ if (NM_STR_HAS_PREFIX(str, "dns+tls://")) {
+ dns->scheme = NM_DNS_URI_SCHEME_TLS;
+ str += NM_STRLEN("dns+tls://");
+ } else if (NM_STR_HAS_PREFIX(str, "dns+udp://")) {
+ dns->scheme = NM_DNS_URI_SCHEME_UDP;
+ str += NM_STRLEN("dns+udp://");
+ } else {
+ name = strchr(str, '#');
+ if (name) {
+ str = nm_strndup_a(200, str, name - str, &addr_heap);
+ name++;
+ }
+
+ if (name && name[0] == '\0') {
+ /* empty DoT server name is not allowed */
+ return FALSE;
+ }
+
+ if (!nm_inet_parse_bin(addr_family, str, &dns->addr_family, &dns->addr))
+ return FALSE;
+
+ dns->servername = name;
+ dns->scheme = NM_DNS_URI_SCHEME_NONE;
+
+ return TRUE;
}
- if (s && s[0] == '\0') {
- /* "ADDR#" empty DoT SNI name is not allowed. */
+ addr_port = str;
+ name = strrchr(addr_port, '#');
+ if (name) {
+ addr_port = nm_strndup_a(100, addr_port, name - addr_port, &addr_port_heap);
+ name++;
+ if (*name == '\0') {
+ /* empty DoT server name not allowed */
+ return FALSE;
+ }
+ dns->servername = name;
+ }
+
+ if (addr_family != AF_INET && *addr_port == '[') {
+ const char *end;
+ char *perc;
+
+ addr_family = AF_INET6;
+ addr_port++;
+ end = strchr(addr_port, ']');
+ if (!end)
+ return FALSE;
+ addr = nm_strndup_a(100, addr_port, end - addr_port, &addr_heap);
+
+ /* IPv6 link-local scope-id */
+ perc = strchr(addr, '%');
+ if (perc) {
+ *perc = '\0';
+ if (g_strlcpy(dns->interface, perc + 1, sizeof(dns->interface))
+ >= sizeof(dns->interface))
+ return FALSE;
+ }
+
+ /* port */
+ end++;
+ if (*end == ':') {
+ end++;
+ dns->port = _nm_utils_ascii_str_to_int64(end, 10, 0, 65535, G_MAXINT32);
+ if (dns->port == G_MAXINT32)
+ return FALSE;
+ }
+ } else if (addr_family != AF_INET6) {
+ /* square brackets are mandatory for IPv6, so it must be IPv4 */
+
+ addr_family = AF_INET;
+ addr = addr_port;
+
+ /* port */
+ port = strchr(addr_port, ':');
+ if (port) {
+ addr = nm_strndup_a(100, addr_port, port - addr_port, &addr_heap);
+ port++;
+ dns->port = _nm_utils_ascii_str_to_int64(port, 10, 0, 65535, G_MAXINT32);
+ if (dns->port == G_MAXINT32)
+ return FALSE;
+ }
+ } else {
return FALSE;
}
- if (!nm_inet_parse_bin(addr_family, dns, &addr_family, out_addr ? &addr : NULL))
+ if (!nm_inet_parse_bin(addr_family, addr, &dns->addr_family, &dns->addr))
+ return FALSE;
+
+ if (dns->scheme != NM_DNS_URI_SCHEME_TLS && dns->servername)
+ return FALSE;
+
+ /* For now, allow the interface only for IPv6 link-local addresses */
+ if (dns->interface[0]
+ && (dns->addr_family != AF_INET6 || !IN6_IS_ADDR_LINKLOCAL(&dns->addr.addr6)))
return FALSE;
- NM_SET_OUT(out_addr_family, addr_family);
- if (out_addr)
- nm_ip_addr_set(addr_family, out_addr, &addr);
- NM_SET_OUT(out_servername, s);
return TRUE;
}
-const char *
-nm_utils_dnsname_construct(int addr_family,
- gconstpointer /* (const NMIPAddr *) */ addr,
- const char *server_name,
- char *result,
- gsize result_len)
+/* @nm_dns_uri_parse_plain:
+ * @addr_family: the address family, or AF_UNSPEC to autodetect it
+ * @str: the name server URI string
+ * @out_addrstr: the buffer to fill with the address string on return,
+ * or %NULL. Must be of size at least NM_INET_ADDRSTRLEN.
+ * @out_addr: the %NMIPAddr struct to fill on return, or %NULL
+ *
+ * Returns whether the string contains a "plain" (DNS over UDP on port 53)
+ * name server. In such case, it fills the arguments with the address
+ * of the name server.
+ *
+ * Returns: %TRUE on success, %FALSE if the string can't be parsed or
+ * if it's not a plain name server.
+ */
+gboolean
+nm_dns_uri_parse_plain(int addr_family, const char *str, char *out_addrstr, NMIPAddr *out_addr)
{
- char sbuf[NM_INET_ADDRSTRLEN];
- gsize l;
- int d;
+ NMDnsServer dns;
- nm_assert_addr_family(addr_family);
- nm_assert(addr);
- nm_assert(!server_name || !nm_str_is_empty(server_name));
+ if (!nm_dns_uri_parse(addr_family, str, &dns))
+ return FALSE;
- nm_inet_ntop(addr_family, addr, sbuf);
-
- if (!server_name) {
- l = g_strlcpy(result, sbuf, result_len);
- } else {
- d = g_snprintf(result, result_len, "%s#%s", sbuf, server_name);
- nm_assert(d >= 0);
- l = (gsize) d;
+ switch (dns.scheme) {
+ case NM_DNS_URI_SCHEME_TLS:
+ return FALSE;
+ case NM_DNS_URI_SCHEME_NONE:
+ NM_SET_OUT(out_addr, dns.addr);
+ if (out_addrstr) {
+ nm_inet_ntop(dns.addr_family, &dns.addr, out_addrstr);
+ }
+ return TRUE;
+ case NM_DNS_URI_SCHEME_UDP:
+ if (dns.port != -1 && dns.port != 53)
+ return FALSE;
+ if (dns.interface[0])
+ return FALSE;
+ NM_SET_OUT(out_addr, dns.addr);
+ if (out_addrstr) {
+ nm_inet_ntop(dns.addr_family, &dns.addr, out_addrstr);
+ }
+ return TRUE;
+ case NM_DNS_URI_SCHEME_UNKNOWN:
+ default:
+ return FALSE;
}
-
- return l < result_len ? result : NULL;
}
+/* @nm_dns_uri_normalize:
+ * @addr_family: the address family, or AF_UNSPEC to autodetect it
+ * @str: the name server URI string
+ * @out_free: the newly-allocated string to set on return, or %NULL
+ *
+ * Returns the "normal" representation for the given name server URI.
+ * Note that a plain name server (DNS over UDP on port 53) is always
+ * represented in the "legacy" (non-URI) form.
+ *
+ * Returns: the normalized DNS URI
+ */
const char *
-nm_utils_dnsname_normalize(int addr_family, const char *dns, char **out_free)
+nm_dns_uri_normalize(int addr_family, const char *str, char **out_free)
{
- char sbuf[NM_INET_ADDRSTRLEN];
- const char *server_name;
- char *s;
- NMIPAddr a;
- gsize l;
+ NMDnsServer dns;
+ char addrstr[NM_INET_ADDRSTRLEN];
+ char portstr[32];
+ char *ret;
+ gsize len;
nm_assert_addr_family_or_unspec(addr_family);
- nm_assert(dns);
+ nm_assert(str);
nm_assert(out_free && !*out_free);
- if (!nm_utils_dnsname_parse(addr_family, dns, &addr_family, &a, &server_name))
+ if (!nm_dns_uri_parse(addr_family, str, &dns))
return NULL;
- nm_inet_ntop(addr_family, &a, sbuf);
+ nm_inet_ntop(dns.addr_family, &dns.addr, addrstr);
- l = strlen(sbuf);
-
- /* In the vast majority of cases, the name is in fact normalized. Check
- * whether it is, and don't duplicate the string. */
- if (strncmp(dns, sbuf, l) == 0) {
- if (server_name) {
- if (dns[l] == '#' && nm_streq(&dns[l + 1], server_name))
- return dns;
- } else {
- if (dns[l] == '\0')
- return dns;
- }
+ if (dns.port != -1) {
+ nm_assert(dns.port >= 0 && dns.port <= 65535);
+ g_snprintf(portstr, sizeof(portstr), "%d", dns.port);
}
- if (!server_name)
- s = g_strdup(sbuf);
- else
- s = g_strconcat(sbuf, "#", server_name, NULL);
+ switch (dns.scheme) {
+ case NM_DNS_URI_SCHEME_NONE:
+ len = strlen(addrstr);
+ /* In the vast majority of cases, the name is in fact normalized. Check
+ * whether it is, and don't duplicate the string. */
+ if (strncmp(str, addrstr, len) == 0) {
+ if (dns.servername) {
+ if (str[len] == '#' && nm_streq(&str[len + 1], dns.servername))
+ return str;
+ } else {
+ if (str[len] == '\0')
+ return str;
+ }
+ }
- *out_free = s;
- return s;
+ if (!dns.servername)
+ ret = g_strdup(addrstr);
+ else
+ ret = g_strconcat(addrstr, "#", dns.servername, NULL);
+ break;
+ case NM_DNS_URI_SCHEME_UDP:
+ if (dns.interface[0] || dns.port != -1) {
+ ret = g_strdup_printf("dns+udp://%s%s%s%s%s%s%s",
+ dns.addr_family == AF_INET6 ? "[" : "",
+ addrstr,
+ dns.interface[0] ? "%" : "",
+ dns.interface[0] ? dns.interface : "",
+ dns.addr_family == AF_INET6 ? "]" : "",
+ dns.port != -1 ? ":" : "",
+ dns.port != -1 ? portstr : "");
+ break;
+ }
+ ret = g_strdup_printf("%s%s%s", addrstr, dns.servername ? "#" : "", dns.servername ?: "");
+ break;
+ case NM_DNS_URI_SCHEME_TLS:
+ ret = g_strdup_printf("dns+tls://%s%s%s%s%s%s%s%s%s",
+ dns.addr_family == AF_INET6 ? "[" : "",
+ addrstr,
+ dns.interface[0] ? "%%" : "",
+ dns.interface[0] ? dns.interface : "",
+ dns.addr_family == AF_INET6 ? "]" : "",
+ dns.port != -1 ? ":" : "",
+ dns.port != -1 ? portstr : "",
+ dns.servername ? "#" : "",
+ dns.servername ?: "");
+ break;
+ case NM_DNS_URI_SCHEME_UNKNOWN:
+ default:
+ nm_assert_not_reached();
+ ret = NULL;
+ }
+
+ *out_free = ret;
+
+ return ret;
}
/*****************************************************************************/
diff --git a/src/libnm-core-aux-intern/nm-libnm-core-utils.h b/src/libnm-core-aux-intern/nm-libnm-core-utils.h
index b296ca898a..9d1637a2ad 100644
--- a/src/libnm-core-aux-intern/nm-libnm-core-utils.h
+++ b/src/libnm-core-aux-intern/nm-libnm-core-utils.h
@@ -312,32 +312,26 @@ NMMptcpFlags nm_mptcp_flags_normalize(NMMptcpFlags flags);
/*****************************************************************************/
-gboolean nm_utils_dnsname_parse(int addr_family,
- const char *dns,
- int *out_addr_family,
- gpointer /* (NMIPAddr **) */ out_addr,
- const char **out_servername);
+typedef enum {
+ NM_DNS_URI_SCHEME_UNKNOWN,
+ NM_DNS_URI_SCHEME_NONE,
+ NM_DNS_URI_SCHEME_UDP,
+ NM_DNS_URI_SCHEME_TLS,
+} NMDnsUriScheme;
-#define nm_utils_dnsname_parse_assert(addr_family, dns, out_addr_family, out_addr, out_servername) \
- ({ \
- gboolean _good; \
- \
- _good = nm_utils_dnsname_parse((addr_family), \
- (dns), \
- (out_addr_family), \
- (out_addr), \
- (out_servername)); \
- nm_assert(_good); \
- _good; \
- })
+typedef struct {
+ NMIPAddr addr;
+ const char *servername;
+ char interface[NM_IFNAMSIZ];
+ NMDnsUriScheme scheme;
+ int addr_family;
+ int port;
+} NMDnsServer;
-const char *nm_utils_dnsname_construct(int addr_family,
- gconstpointer /* (const NMIPAddr *) */ addr,
- const char *server_name,
- char *result,
- gsize result_len);
-
-const char *nm_utils_dnsname_normalize(int addr_family, const char *dns, char **out_free);
+gboolean nm_dns_uri_parse(int addr_family, const char *str, NMDnsServer *out_dns);
+gboolean
+nm_dns_uri_parse_plain(int addr_family, const char *str, char *out_addrstr, NMIPAddr *out_addr);
+const char *nm_dns_uri_normalize(int addr_family, const char *str, char **out_free);
/*****************************************************************************/
diff --git a/src/libnm-core-impl/nm-keyfile.c b/src/libnm-core-impl/nm-keyfile.c
index 3292142fdb..69b49fa0b7 100644
--- a/src/libnm-core-impl/nm-keyfile.c
+++ b/src/libnm-core-impl/nm-keyfile.c
@@ -1157,7 +1157,7 @@ ip_dns_parser(KeyfileReaderInfo *info, NMSetting *setting, const char *key)
addr_family = NM_SETTING_IP_CONFIG_GET_ADDR_FAMILY(setting);
for (i = 0, n = 0; i < length; i++) {
- if (!nm_utils_dnsname_parse(addr_family, list[i], NULL, NULL, NULL)) {
+ if (!nm_dns_uri_parse(addr_family, list[i], NULL)) {
if (!read_handle_warn(info,
key,
key,
diff --git a/src/libnm-core-impl/nm-setting-ip-config.c b/src/libnm-core-impl/nm-setting-ip-config.c
index 86fd1c953f..1ec97029a0 100644
--- a/src/libnm-core-impl/nm-setting-ip-config.c
+++ b/src/libnm-core-impl/nm-setting-ip-config.c
@@ -4095,7 +4095,7 @@ _ip_config_add_dns(NMSettingIPConfig *setting, const char *dns)
priv = NM_SETTING_IP_CONFIG_GET_PRIVATE(setting);
- s = nm_utils_dnsname_normalize(NM_SETTING_IP_CONFIG_GET_ADDR_FAMILY(setting), dns, &s_free);
+ s = nm_dns_uri_normalize(NM_SETTING_IP_CONFIG_GET_ADDR_FAMILY(setting), dns, &s_free);
if (!s)
s = dns;
@@ -4186,7 +4186,7 @@ nm_setting_ip_config_remove_dns_by_value(NMSettingIPConfig *setting, const char
gs_free char *s_free = NULL;
const char *s;
- s = nm_utils_dnsname_normalize(NM_SETTING_IP_CONFIG_GET_ADDR_FAMILY(setting), dns, &s_free);
+ s = nm_dns_uri_normalize(NM_SETTING_IP_CONFIG_GET_ADDR_FAMILY(setting), dns, &s_free);
if (s && !nm_streq(dns, s))
idx = nm_strv_ptrarray_find_first(priv->dns, dns);
}
@@ -5615,11 +5615,7 @@ verify(NMSetting *setting, NMConnection *connection, GError **error)
for (i = 0; i < priv->dns->len; i++) {
const char *dns = priv->dns->pdata[i];
- if (!nm_utils_dnsname_parse(NM_SETTING_IP_CONFIG_GET_ADDR_FAMILY(setting),
- dns,
- NULL,
- NULL,
- NULL)) {
+ if (!nm_dns_uri_parse(NM_SETTING_IP_CONFIG_GET_ADDR_FAMILY(setting), dns, NULL)) {
g_set_error(error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
@@ -6510,11 +6506,16 @@ nm_setting_ip_config_class_init(NMSettingIPConfigClass *klass)
/**
* NMSettingIPConfig:dns:
*
- * Array of IP addresses of DNS servers.
+ * Array of DNS servers.
*
- * For DoT (DNS over TLS), the SNI server name can be specified by appending
- * "#example.com" to the IP address of the DNS server. This currently only has
- * effect when using systemd-resolved.
+ * Each server can be specified either as a plain IP address (optionally followed
+ * by a "#" and the SNI server name for DNS over TLS) or with a URI syntax.
+ *
+ * When it is specified as an URI, the following forms are supported:
+ * dns+udp://ADDRESS[:PORT], dns+tls://ADDRESS[:PORT][#SERVERNAME] .
+ *
+ * When using the URI syntax, IPv6 addresses must be enclosed in square
+ * brackets ('[', ']').
**/
obj_properties[PROP_DNS] =
g_param_spec_boxed(NM_SETTING_IP_CONFIG_DNS,
diff --git a/src/libnm-core-impl/nm-utils.c b/src/libnm-core-impl/nm-utils.c
index d5c683eef4..13ff0a5bef 100644
--- a/src/libnm-core-impl/nm-utils.c
+++ b/src/libnm-core-impl/nm-utils.c
@@ -1309,11 +1309,11 @@ nm_utils_dns_to_variant(int addr_family, const char *const *dns, gssize len)
/* We can only represent the IP address on the legacy property "ipv[46].dns".
* Expose what we can. */
- if (!nm_utils_dnsname_parse(addr_family, dns[i], NULL, &ip, NULL))
+ if (!nm_dns_uri_parse_plain(addr_family, dns[i], NULL, &ip))
continue;
if (IS_IPv4)
- g_variant_builder_add(&builder, "u", ip);
+ g_variant_builder_add(&builder, "u", ip.addr4);
else
g_variant_builder_add(&builder, "@ay", nm_g_variant_new_ay_in6addr(&ip.addr6));
}
diff --git a/src/libnm-core-impl/tests/test-general.c b/src/libnm-core-impl/tests/test-general.c
index c08789a0d5..3113fc4621 100644
--- a/src/libnm-core-impl/tests/test-general.c
+++ b/src/libnm-core-impl/tests/test-general.c
@@ -11397,176 +11397,179 @@ test_connection_path(void)
/*****************************************************************************/
static void
-_t_dnsname_1(const char *str, const char *exp_addr, const char *exp_server_name)
+t_dns_0(const char *str)
{
- int addr_family;
- NMIPAddr exp_addr_bin;
- gboolean addr_family_request;
- gboolean r;
- int detect_addr_family;
- NMIPAddr detect_addr;
- const char *detect_server_name;
- int *p_detect_addr_family = &detect_addr_family;
- NMIPAddr *p_detect_addr = &detect_addr;
- const char **p_detect_server_name = &detect_server_name;
- char str_construct_buf[100];
- char str_construct_buf2[100];
- const char *str_construct;
- const char *str_construct2;
- gsize l;
- const char *str_normalized;
- gs_free char *str_normalized_alloc = NULL;
+ NMDnsServer server = {};
+ gboolean ret;
- g_assert(str);
- g_assert(exp_addr);
+ ret = nm_dns_uri_parse(AF_UNSPEC, str, &server);
- r = nm_inet_parse_bin(AF_UNSPEC, exp_addr, &addr_family, &exp_addr_bin);
- g_assert(r);
- g_assert(NM_IN_SET(addr_family, AF_INET, AF_INET6));
-
- addr_family_request = nmtst_get_rand_bool();
- if (nmtst_get_rand_bool())
- p_detect_addr = NULL;
- if ((addr_family_request || !p_detect_addr) && nmtst_get_rand_bool())
- p_detect_addr_family = NULL;
- if (nmtst_get_rand_bool())
- p_detect_server_name = NULL;
-
- r = nm_utils_dnsname_parse(addr_family_request ? addr_family : AF_UNSPEC,
- str,
- p_detect_addr_family,
- p_detect_addr,
- p_detect_server_name);
- g_assert(r);
-
- if (p_detect_addr_family)
- g_assert_cmpint(addr_family, ==, detect_addr_family);
- if (p_detect_addr)
- g_assert_cmpstr(nmtst_inet_to_string(addr_family, &detect_addr), ==, exp_addr);
- if (p_detect_server_name)
- g_assert_cmpstr(detect_server_name, ==, exp_server_name);
-
- r = nm_utils_dnsname_parse(addr_family == AF_INET ? AF_INET6 : AF_INET,
- str,
- p_detect_addr_family,
- p_detect_addr,
- p_detect_server_name);
- g_assert(!r);
-
- /* Construct the expected value. */
- str_construct = nm_utils_dnsname_construct(addr_family,
- &exp_addr_bin,
- exp_server_name,
- str_construct_buf,
- sizeof(str_construct_buf));
- g_assert(str_construct);
- g_assert(str_construct == str_construct_buf);
- g_assert(strlen(str_construct) < sizeof(str_construct_buf));
-
- /* Check that a too short buffer causes truncation. */
- l = nmtst_get_rand_uint32() % (strlen(str_construct) + 10);
- str_construct2 = nm_utils_dnsname_construct(addr_family,
- &exp_addr_bin,
- exp_server_name,
- str_construct_buf2,
- l);
- if (str_construct2) {
- g_assert(str_construct2 == str_construct_buf2);
- g_assert_cmpstr(str_construct2, ==, str_construct);
- g_assert(l > strlen(str_construct));
- } else
- g_assert(l <= strlen(str_construct));
-
- if (!nm_streq(str_construct, str)) {
- _t_dnsname_1(str_construct, exp_addr, exp_server_name);
- }
-
- str_normalized = nm_utils_dnsname_normalize(nmtst_get_rand_bool() ? addr_family : AF_UNSPEC,
- str,
- &str_normalized_alloc);
- g_assert(str_normalized);
- if (str_normalized_alloc) {
- g_assert(str_normalized == str_normalized_alloc);
- g_assert_cmpstr(str_normalized, !=, str);
- } else {
- g_assert(str == str_normalized);
- }
- g_assert_cmpstr(str_normalized, ==, str_construct);
-
- nm_clear_g_free(&str_normalized_alloc);
- str_normalized = nm_utils_dnsname_normalize(addr_family == AF_INET ? AF_INET6 : AF_INET,
- str,
- &str_normalized_alloc);
- g_assert(!str_normalized);
- g_assert(!str_normalized_alloc);
+ g_assert(!ret);
}
static void
-_t_dnsname_0(const char *str)
+dns_uri_parse_ok(const char *str,
+ int addr_family,
+ NMDnsUriScheme scheme,
+ const char *addr,
+ int port,
+ const char *sname,
+ const char *ifname)
{
- gboolean addr_family_request;
- int detect_addr_family;
- NMIPAddr detect_addr;
- const char *detect_server_name;
- int *p_detect_addr_family = &detect_addr_family;
- NMIPAddr *p_detect_addr = &detect_addr;
- const char **p_detect_server_name = &detect_server_name;
- const char *str_normalized;
- gs_free char *str_normalized_alloc = NULL;
- gboolean r;
+ NMDnsServer dns = {};
+ char addrstr[NM_INET_ADDRSTRLEN];
+ gboolean ret;
- g_assert(str);
+ for (int i = 0; i < 2; i++) {
+ gboolean af_unspec = i;
- addr_family_request = nmtst_get_rand_bool();
- if (nmtst_get_rand_bool())
- p_detect_addr = NULL;
- if ((addr_family_request || !p_detect_addr) && nmtst_get_rand_bool())
- p_detect_addr_family = NULL;
- if (nmtst_get_rand_bool())
- p_detect_server_name = NULL;
+ ret = nm_dns_uri_parse(af_unspec ? AF_UNSPEC : addr_family, str, &dns);
+ g_assert(ret);
- r = nm_utils_dnsname_parse(addr_family_request ? nmtst_rand_select(AF_INET, AF_INET6)
- : AF_UNSPEC,
- str,
- p_detect_addr_family,
- p_detect_addr,
- p_detect_server_name);
- g_assert(!r);
+ g_assert_cmpint(addr_family, ==, dns.addr_family);
+ g_assert_cmpint(port, ==, dns.port);
+ g_assert_cmpstr(sname, ==, dns.servername);
+ g_assert_cmpstr(ifname ?: "", ==, dns.interface);
- str_normalized = nm_utils_dnsname_normalize(nmtst_rand_select(AF_UNSPEC, AF_INET, AF_INET6),
- str,
- &str_normalized_alloc);
- g_assert(!str_normalized);
- g_assert(!str_normalized_alloc);
+ nm_inet_ntop(dns.addr_family, &dns.addr, addrstr);
+ g_assert_cmpstr(addrstr, ==, addr);
+
+ /* Parse with the wrong address family must fail */
+ ret = nm_dns_uri_parse(addr_family == AF_INET ? AF_INET6 : AF_INET, str, &dns);
+ g_assert(!ret);
+ }
+}
+
+#define t_dns_1(str, af, scheme, addr, port, sname, ifname) \
+ dns_uri_parse_ok((str), \
+ (AF_##af), \
+ (NM_DNS_URI_SCHEME_##scheme), \
+ (addr), \
+ (port), \
+ (sname), \
+ (ifname))
+
+static void
+test_dns_uri_parse(void)
+{
+ /* clang-format off */
+ t_dns_1("dns+tls://8.8.8.8", INET, TLS, "8.8.8.8", -1, NULL, NULL);
+ t_dns_1("dns+tls://8.8.8.8", INET, TLS, "8.8.8.8", -1, NULL, NULL);
+ t_dns_1("dns+tls://1.2.3.4#name", INET, TLS, "1.2.3.4", -1, "name", NULL);
+ t_dns_1("dns+tls://1.2.3.4#a.b.c", INET, TLS, "1.2.3.4", -1, "a.b.c", NULL);
+ t_dns_1("dns+tls://1.2.3.4:53", INET, TLS, "1.2.3.4", 53, NULL, NULL);
+ t_dns_1("dns+tls://1.2.3.4:53#foobar", INET, TLS, "1.2.3.4", 53, "foobar", NULL);
+ t_dns_1("dns+tls://192.168.120.250:99", INET, TLS, "192.168.120.250", 99, NULL, NULL);
+ t_dns_1("dns+udp://8.8.8.8:65535", INET, UDP, "8.8.8.8", 65535, NULL, NULL);
+
+ t_dns_1("dns+udp://[fd01::1]", INET6, UDP, "fd01::1", -1, NULL, NULL);
+ t_dns_1("dns+tls://[fd01::2]:5353", INET6, UDP, "fd01::2", 5353, NULL, NULL);
+ t_dns_1("dns+tls://[::1]#name", INET6, UDP, "::1", -1, "name", NULL);
+ t_dns_1("dns+tls://[::2]:65535#name", INET6, UDP, "::2", 65535, "name", NULL);
+ t_dns_1("dns+udp://[::ffff:1.2.3.4]", INET6, UDP, "::ffff:1.2.3.4", -1, NULL, NULL);
+ t_dns_1("dns+tls://[fe80::1%eth0]", INET6, UDP, "fe80::1", -1, NULL, "eth0");
+ t_dns_1("dns+tls://[fe80::2%en1]:53#a", INET6, UDP, "fe80::2", 53, "a", "en1");
+ t_dns_1("dns+tls://[fe80::1%en3456789012345]", INET6, UDP, "fe80::1", -1, NULL, "en3456789012345");
+
+ t_dns_1("1.2.3.4", INET, NONE, "1.2.3.4", -1, NULL, NULL);
+ t_dns_1("1.2.3.4#foo", INET, NONE, "1.2.3.4", -1, "foo", NULL);
+ t_dns_1("1::#x", INET6, NONE, "1::", -1, "x", NULL);
+ t_dns_1("1::0#x", INET6, NONE, "1::", -1, "x", NULL);
+ t_dns_1("192.168.0.1", INET, NONE, "192.168.0.1", -1, NULL, NULL);
+ t_dns_1("192.168.0.1#tst.com", INET, NONE, "192.168.0.1", -1, "tst.com", NULL);
+ t_dns_1("fe80::18", INET6, NONE, "fe80::18", -1, NULL, NULL);
+ t_dns_1("fe80::18#foo.com", INET6, NONE, "fe80::18", -1, "foo.com", NULL);
+ /* clang-format on */
+
+ t_dns_0("http://8.8.8.8"); /* unsupported schema */
+ t_dns_0("dns+udp://1.2.3.4#name"); /* servername not supported for plain UDP */
+ t_dns_0("dns+tls://1.2.3"); /* invalid address */
+ t_dns_0("dns+tls://fd01::1"); /* IPv6 requires brackets */
+ t_dns_0("dns+tls://[fd13:a:aaaa]"); /* invalid address */
+ t_dns_0("dns+tls://1.2.3.4:1:1"); /* invalid syntax */
+ t_dns_0("dns+tls://1.2.3.4#name#name"); /* invalid syntax */
+ t_dns_0("dns+tls://1.2.3.4%eth0"); /* interface only allowed for IPv6 */
+ t_dns_0("dns+tls://[2001::1%eth0]"); /* interface only allowed for IPv6 link-local */
+ t_dns_0("dns+tls://[fe80::1%en34567890123456]"); /* interface name too long */
+ t_dns_0("1.2.3.4#");
+ t_dns_0("1::0#");
+ t_dns_0("192.168.0.1:53");
+ t_dns_0("192.168.0.1:53#example.com");
+ t_dns_0("fe80::18%19");
+ t_dns_0("fe80::18%lo");
+ t_dns_0("[fe80::18]:53");
+ t_dns_0("[fe80::18]:53%19");
+ t_dns_0("[fe80::18]:53%lo");
+ t_dns_0("fe80::18%19#hoge.com");
+ t_dns_0("[fe80::18]:53#hoge.com");
+ t_dns_0("[fe80::18]:53%19");
+ t_dns_0("[fe80::18]:53%19#hoge.com");
+ t_dns_0("[fe80::18]:53%lo");
+ t_dns_0("[fe80::18]:53%lo#hoge.com");
}
static void
-test_dnsname(void)
+test_dns_uri_parse_plain(void)
{
- _t_dnsname_1("1.2.3.4", "1.2.3.4", NULL);
- _t_dnsname_1("1.2.3.4#foo", "1.2.3.4", "foo");
- _t_dnsname_1("1::#x", "1::", "x");
- _t_dnsname_1("1::0#x", "1::", "x");
- _t_dnsname_1("192.168.0.1", "192.168.0.1", NULL);
- _t_dnsname_1("192.168.0.1#test.com", "192.168.0.1", "test.com");
- _t_dnsname_1("fe80::18", "fe80::18", NULL);
- _t_dnsname_1("fe80::18#hoge.com", "fe80::18", "hoge.com");
+ struct {
+ const char *input;
+ int input_af;
+ gboolean result;
+ const char *addrstr;
+ } values[] = {
+ {"1.2.3.4", AF_INET, TRUE, "1.2.3.4"},
+ {"1.2.3.4", AF_INET6, FALSE, NULL},
+ {"1.2.3.4", AF_UNSPEC, TRUE, "1.2.3.4"},
+ {"1234:5555:ffff:dddd::4321", AF_INET, FALSE, NULL},
+ {"1234:5555:ffff:dddd::4321", AF_INET6, TRUE, "1234:5555:ffff:dddd::4321"},
+ {"1234:5555:ffff:dddd::4321", AF_UNSPEC, TRUE, "1234:5555:ffff:dddd::4321"},
+ {"192.0.2.1#example.com", AF_INET, TRUE, "192.0.2.1"},
+ {"192.0.2.1#example.com", AF_UNSPEC, TRUE, "192.0.2.1"},
+ {"192.0.2.1#example.com", AF_INET6, FALSE, NULL},
+ {"dns+tls://1.2.3.4", AF_INET, FALSE, NULL},
+ {"dns+tls://[fd01::1]", AF_INET, FALSE, NULL},
+ {"dns+udp://1.2.3.4:53", AF_INET, TRUE, "1.2.3.4"},
+ {"dns+udp://1.2.3.4:54", AF_INET, FALSE, NULL},
+ {"dns+udp://[fd01::1]", AF_INET6, TRUE, "fd01::1"},
+ {"dns+udp://[fd01::1]:53", AF_INET6, TRUE, "fd01::1"},
+ {"dns+udp://[fd01::1]:60000", AF_INET, FALSE, NULL},
+ };
+ guint i;
- _t_dnsname_0("1.2.3.4#");
- _t_dnsname_0("1::0#");
- _t_dnsname_0("192.168.0.1:53");
- _t_dnsname_0("192.168.0.1:53#example.com");
- _t_dnsname_0("fe80::18%19");
- _t_dnsname_0("fe80::18%lo");
- _t_dnsname_0("[fe80::18]:53");
- _t_dnsname_0("[fe80::18]:53%19");
- _t_dnsname_0("[fe80::18]:53%lo");
- _t_dnsname_0("fe80::18%19#hoge.com");
- _t_dnsname_0("[fe80::18]:53#hoge.com");
- _t_dnsname_0("[fe80::18]:53%19");
- _t_dnsname_0("[fe80::18]:53%19#hoge.com");
- _t_dnsname_0("[fe80::18]:53%lo");
- _t_dnsname_0("[fe80::18]:53%lo#hoge.com");
+ for (i = 0; i < G_N_ELEMENTS(values); i++) {
+ char addrstr[NM_INET_ADDRSTRLEN];
+ gboolean result;
+ NMIPAddr addr;
+
+ result = nm_dns_uri_parse_plain(values[i].input_af, values[i].input, addrstr, &addr);
+ g_assert_cmpint(result, ==, values[i].result);
+ if (result) {
+ char buf[NM_INET_ADDRSTRLEN];
+
+ nm_inet_ntop(strchr(addrstr, ':') ? AF_INET6 : AF_INET, addr.addr_ptr, buf);
+ g_assert_cmpstr(buf, ==, addrstr);
+ g_assert_cmpstr(addrstr, ==, values[i].addrstr);
+ }
+ }
+}
+
+static void
+t_dns_uri_normalize(const char *input, const char *expected)
+{
+ const char *str;
+ gs_free char *str_free = NULL;
+
+ str = nm_dns_uri_normalize(AF_UNSPEC, input, &str_free);
+ g_assert_cmpstr(str, ==, expected);
+}
+
+static void
+test_dns_uri_normalize(void)
+{
+ t_dns_uri_normalize("8.8.8.8", "8.8.8.8");
+ t_dns_uri_normalize("dns+tls://[2001:0:0::1234]:999#name", "dns+tls://[2001::1234]:999#name");
+ t_dns_uri_normalize("dns+udp://[0::1]:0123", "dns+udp://[::1]:123");
+ t_dns_uri_normalize("8.8.8.888", NULL);
}
/*****************************************************************************/
@@ -11945,7 +11948,9 @@ main(int argc, char **argv)
g_test_add_func("/core/general/test_system_encodings", test_system_encodings);
g_test_add_func("/core/general/test_direct_string_is_refstr", test_direct_string_is_refstr);
g_test_add_func("/core/general/test_connection_path", test_connection_path);
- g_test_add_func("/core/general/test_dnsname", test_dnsname);
+ g_test_add_func("/core/general/test_dns_uri_parse", test_dns_uri_parse);
+ g_test_add_func("/core/general/test_dns_uri_get_legacy", test_dns_uri_parse_plain);
+ g_test_add_func("/core/general/test_dns_uri_normalize", test_dns_uri_normalize);
g_test_add_func("/core/general/test_dhcp_iaid_hexstr", test_dhcp_iaid_hexstr);
return g_test_run();
diff --git a/src/libnmc-setting/settings-docs.h.in b/src/libnmc-setting/settings-docs.h.in
index 25c4d0abfa..dd719afad6 100644
--- a/src/libnmc-setting/settings-docs.h.in
+++ b/src/libnmc-setting/settings-docs.h.in
@@ -196,7 +196,7 @@
#define DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_DHCP_SEND_RELEASE N_("Whether the DHCP client will send RELEASE message when bringing the connection down. The default value is \"default\" (-1). When the default value is specified, then the global value from NetworkManager configuration is looked up, if not set, it is considered as FALSE.")
#define DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_DHCP_TIMEOUT N_("A timeout for a DHCP transaction in seconds. If zero (the default), a globally configured default is used. If still unspecified, a device specific timeout is used (usually 45 seconds). Set to 2147483647 (MAXINT32) for infinity.")
#define DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER N_("The Vendor Class Identifier DHCP option (60). Special characters in the data string may be escaped using C-style escapes, nevertheless this property cannot contain nul bytes. If the per-profile value is unspecified (the default), a global connection default gets consulted. If still unspecified, the DHCP option is not sent to the server.")
-#define DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_DNS N_("Array of IP addresses of DNS servers. For DoT (DNS over TLS), the SNI server name can be specified by appending \"#example.com\" to the IP address of the DNS server. This currently only has effect when using systemd-resolved.")
+#define DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_DNS N_("Array of DNS servers. Each server can be specified either as a plain IP address (optionally followed by a \"#\" and the SNI server name for DNS over TLS) or with a URI syntax. When it is specified as an URI, the following forms are supported: dns+udp://ADDRESS[:PORT], dns+tls://ADDRESS[:PORT][SERVERNAME] . When using the URI syntax, IPv6 addresses must be enclosed in square brackets ('[', ']').")
#define DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_DNS_OPTIONS N_("DNS options for /etc/resolv.conf as described in resolv.conf(5) manual. The currently supported options are \"attempts\", \"debug\", \"edns0\", \"ndots\", \"no-aaaa\", \"no-check-names\", \"no-reload\", \"no-tld-query\", \"rotate\", \"single-request\", \"single-request-reopen\", \"timeout\", \"trust-ad\", \"use-vc\". See the resolv.conf(5) manual. Note that there is a distinction between an unset (default) list and an empty list. In nmcli, to unset the list set the value to \"\". To set an empty list, set it to \" \". Currently, an unset list has the same meaning as an empty list. That might change in the future. The \"trust-ad\" setting is only honored if the profile contributes name servers to resolv.conf, and if all contributing profiles have \"trust-ad\" enabled. When using a caching DNS plugin (dnsmasq or systemd-resolved in NetworkManager.conf) then \"edns0\" and \"trust-ad\" are automatically added. The valid \"ipv4.dns-options\" and \"ipv6.dns-options\" get merged together.")
#define DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_DNS_PRIORITY N_("DNS servers priority. The relative priority for DNS servers specified by this setting. A lower numerical value is better (higher priority). Negative values have the special effect of excluding other configurations with a greater numerical priority value; so in presence of at least one negative priority, only DNS servers from connections with the lowest priority value will be used. To avoid all DNS leaks, set the priority of the profile that should be used to the most negative value of all active connections profiles. Zero selects a globally configured default value. If the latter is missing or zero too, it defaults to 50 for VPNs (including WireGuard) and 100 for other connections. Note that the priority is to order DNS settings for multiple active connections. It does not disambiguate multiple DNS servers within the same connection profile. When multiple devices have configurations with the same priority, VPNs will be considered first, then devices with the best (lowest metric) default route and then all other devices. When using dns=default, servers with higher priority will be on top of resolv.conf. To prioritize a given server over another one within the same connection, just specify them in the desired order. Note that commonly the resolver tries name servers in /etc/resolv.conf in the order listed, proceeding with the next server in the list on failure. See for example the \"rotate\" option of the dns-options setting. If there are any negative DNS priorities, then only name servers from the devices with that lowest priority will be considered. When using a DNS resolver that supports Conditional Forwarding or Split DNS (with dns=dnsmasq or dns=systemd-resolved settings), each connection is used to query domains in its search list. The search domains determine which name servers to ask, and the DNS priority is used to prioritize name servers based on the domain. Queries for domains not present in any search list are routed through connections having the '~.' special wildcard domain, which is added automatically to connections with the default route (or can be added manually). When multiple connections specify the same domain, the one with the best priority (lowest numerical value) wins. If a sub domain is configured on another interface it will be accepted regardless the priority, unless parent domain on the other interface has a negative priority, which causes the sub domain to be shadowed. With Split DNS one can avoid undesired DNS leaks by properly configuring DNS priorities and the search domains, so that only name servers of the desired interface are configured.")
#define DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_DNS_SEARCH N_("List of DNS search domains. Domains starting with a tilde ('~') are considered 'routing' domains and are used only to decide the interface over which a query must be forwarded; they are not used to complete unqualified host names. When using a DNS plugin that supports Conditional Forwarding or Split DNS, then the search domains specify which name servers to query. This makes the behavior different from running with plain /etc/resolv.conf. For more information see also the dns-priority setting. When set on a profile that also enabled DHCP, the DNS search list received automatically (option 119 for DHCPv4 and option 24 for DHCPv6) gets merged with the manual list. This can be prevented by setting \"ignore-auto-dns\". Note that if no DNS searches are configured, the fallback will be derived from the domain from DHCP (option 15).")
@@ -231,7 +231,7 @@
#define DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_DHCP_SEND_HOSTNAME_V2 N_("If TRUE, a hostname is sent to the DHCP server when acquiring a lease. Some DHCP servers use this hostname to update DNS databases, essentially providing a static hostname for the computer. If the dhcp-hostname property is NULL and this property is TRUE, the current persistent hostname of the computer is sent. The default value is default (-1). In this case the global value from NetworkManager configuration is looked up. If it's not set, the value from dhcp-send-hostname-deprecated, which defaults to TRUE, is used for backwards compatibility. In the future this will change and, in absence of a global default, it will always fallback to TRUE.")
#define DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_DHCP_SEND_RELEASE N_("Whether the DHCP client will send RELEASE message when bringing the connection down. The default value is \"default\" (-1). When the default value is specified, then the global value from NetworkManager configuration is looked up, if not set, it is considered as FALSE.")
#define DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_DHCP_TIMEOUT N_("A timeout for a DHCP transaction in seconds. If zero (the default), a globally configured default is used. If still unspecified, a device specific timeout is used (usually 45 seconds). Set to 2147483647 (MAXINT32) for infinity.")
-#define DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_DNS N_("Array of IP addresses of DNS servers. For DoT (DNS over TLS), the SNI server name can be specified by appending \"#example.com\" to the IP address of the DNS server. This currently only has effect when using systemd-resolved.")
+#define DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_DNS N_("Array of DNS servers. Each server can be specified either as a plain IP address (optionally followed by a \"#\" and the SNI server name for DNS over TLS) or with a URI syntax. When it is specified as an URI, the following forms are supported: dns+udp://ADDRESS[:PORT], dns+tls://ADDRESS[:PORT][SERVERNAME] . When using the URI syntax, IPv6 addresses must be enclosed in square brackets ('[', ']').")
#define DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_DNS_OPTIONS N_("DNS options for /etc/resolv.conf as described in resolv.conf(5) manual. The currently supported options are \"attempts\", \"debug\", \"edns0\", \"ndots\", \"no-aaaa\", \"no-check-names\", \"no-reload\", \"no-tld-query\", \"rotate\", \"single-request\", \"single-request-reopen\", \"timeout\", \"trust-ad\", \"use-vc\" and \"inet6\", \"ip6-bytestring\", \"ip6-dotint\", \"no-ip6-dotint\". See the resolv.conf(5) manual. Note that there is a distinction between an unset (default) list and an empty list. In nmcli, to unset the list set the value to \"\". To set an empty list, set it to \" \". Currently, an unset list has the same meaning as an empty list. That might change in the future. The \"trust-ad\" setting is only honored if the profile contributes name servers to resolv.conf, and if all contributing profiles have \"trust-ad\" enabled. When using a caching DNS plugin (dnsmasq or systemd-resolved in NetworkManager.conf) then \"edns0\" and \"trust-ad\" are automatically added. The valid \"ipv4.dns-options\" and \"ipv6.dns-options\" get merged together.")
#define DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_DNS_PRIORITY N_("DNS servers priority. The relative priority for DNS servers specified by this setting. A lower numerical value is better (higher priority). Negative values have the special effect of excluding other configurations with a greater numerical priority value; so in presence of at least one negative priority, only DNS servers from connections with the lowest priority value will be used. To avoid all DNS leaks, set the priority of the profile that should be used to the most negative value of all active connections profiles. Zero selects a globally configured default value. If the latter is missing or zero too, it defaults to 50 for VPNs (including WireGuard) and 100 for other connections. Note that the priority is to order DNS settings for multiple active connections. It does not disambiguate multiple DNS servers within the same connection profile. When multiple devices have configurations with the same priority, VPNs will be considered first, then devices with the best (lowest metric) default route and then all other devices. When using dns=default, servers with higher priority will be on top of resolv.conf. To prioritize a given server over another one within the same connection, just specify them in the desired order. Note that commonly the resolver tries name servers in /etc/resolv.conf in the order listed, proceeding with the next server in the list on failure. See for example the \"rotate\" option of the dns-options setting. If there are any negative DNS priorities, then only name servers from the devices with that lowest priority will be considered. When using a DNS resolver that supports Conditional Forwarding or Split DNS (with dns=dnsmasq or dns=systemd-resolved settings), each connection is used to query domains in its search list. The search domains determine which name servers to ask, and the DNS priority is used to prioritize name servers based on the domain. Queries for domains not present in any search list are routed through connections having the '~.' special wildcard domain, which is added automatically to connections with the default route (or can be added manually). When multiple connections specify the same domain, the one with the best priority (lowest numerical value) wins. If a sub domain is configured on another interface it will be accepted regardless the priority, unless parent domain on the other interface has a negative priority, which causes the sub domain to be shadowed. With Split DNS one can avoid undesired DNS leaks by properly configuring DNS priorities and the search domains, so that only name servers of the desired interface are configured.")
#define DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_DNS_SEARCH N_("List of DNS search domains. Domains starting with a tilde ('~') are considered 'routing' domains and are used only to decide the interface over which a query must be forwarded; they are not used to complete unqualified host names. When using a DNS plugin that supports Conditional Forwarding or Split DNS, then the search domains specify which name servers to query. This makes the behavior different from running with plain /etc/resolv.conf. For more information see also the dns-priority setting. When set on a profile that also enabled DHCP, the DNS search list received automatically (option 119 for DHCPv4 and option 24 for DHCPv6) gets merged with the manual list. This can be prevented by setting \"ignore-auto-dns\". Note that if no DNS searches are configured, the fallback will be derived from the domain from DHCP (option 15).")
diff --git a/src/nm-dispatcher/nm-dispatcher-utils.c b/src/nm-dispatcher/nm-dispatcher-utils.c
index 6659936f48..030506e944 100644
--- a/src/nm-dispatcher/nm-dispatcher-utils.c
+++ b/src/nm-dispatcher/nm-dispatcher-utils.c
@@ -264,22 +264,43 @@ construct_ip_items(GPtrArray *items, int addr_family, GVariant *ip_config, const
g_variant_unref(val);
}
- val = g_variant_lookup_value(ip_config,
- "nameservers",
- addr_family == AF_INET ? G_VARIANT_TYPE("au")
- : G_VARIANT_TYPE("aay"));
+ /* For name servers, prefer the new key and fall back to the old one. */
+ val = g_variant_lookup_value(ip_config, "nameservers-full", G_VARIANT_TYPE("as"));
if (val) {
- gs_strfreev char **v = NULL;
+ nm_auto_unref_ptrarray GPtrArray *arr = NULL;
+ GVariantIter iter;
+ const char *str;
+
+ arr = g_ptr_array_new_full(g_variant_n_children(val) + 1, NULL);
+ g_variant_iter_init(&iter, val);
+ while (g_variant_iter_next(&iter, "&s", &str)) {
+ g_ptr_array_add(arr, (gpointer) str);
+ }
+ g_ptr_array_add(arr, NULL);
- if (addr_family == AF_INET)
- v = nm_utils_ip4_dns_from_variant(val);
- else
- v = nm_utils_ip6_dns_from_variant(val);
_items_add_strv(items,
prefix,
addr_family == AF_INET ? "IP4_NAMESERVERS" : "IP6_NAMESERVERS",
- NM_CAST_STRV_CC(v));
+ (const char *const *) arr->pdata);
g_variant_unref(val);
+ } else {
+ val = g_variant_lookup_value(ip_config,
+ "nameservers",
+ addr_family == AF_INET ? G_VARIANT_TYPE("au")
+ : G_VARIANT_TYPE("aay"));
+ if (val) {
+ gs_strfreev char **v = NULL;
+
+ if (addr_family == AF_INET)
+ v = nm_utils_ip4_dns_from_variant(val);
+ else
+ v = nm_utils_ip6_dns_from_variant(val);
+ _items_add_strv(items,
+ prefix,
+ addr_family == AF_INET ? "IP4_NAMESERVERS" : "IP6_NAMESERVERS",
+ NM_CAST_STRV_CC(v));
+ g_variant_unref(val);
+ }
}
val = g_variant_lookup_value(ip_config, "domains", G_VARIANT_TYPE_STRING_ARRAY);
diff --git a/src/nm-initrd-generator/nm-initrd-generator.c b/src/nm-initrd-generator/nm-initrd-generator.c
index 844aebf74f..14a168341e 100644
--- a/src/nm-initrd-generator/nm-initrd-generator.c
+++ b/src/nm-initrd-generator/nm-initrd-generator.c
@@ -154,6 +154,7 @@ main(int argc, char *argv[])
gint64 carrier_timeout_sec = 0;
gs_unref_array GArray *confs = NULL;
guint i;
+ gs_strfreev char **global_dns_servers = NULL;
option_context = g_option_context_new(
"-- [ip=...] [rd.route=...] [bridge=...] [bond=...] [team=...] [vlan=...] "
@@ -193,7 +194,8 @@ main(int argc, char *argv[])
sysfs_dir,
(const char *const *) remaining,
&hostname,
- &carrier_timeout_sec);
+ &carrier_timeout_sec,
+ &global_dns_servers);
confs = g_array_new(FALSE, FALSE, sizeof(NMUtilsNamedValue));
g_array_set_clear_func(confs, (GDestroyNotify) nm_utils_named_value_clear_with_g_free);
@@ -233,6 +235,36 @@ main(int argc, char *argv[])
g_array_append_val(confs, v);
}
+ if (global_dns_servers) {
+ nm_auto_unref_keyfile GKeyFile *keyfile = NULL;
+ NMUtilsNamedValue v;
+ gs_free char *value = NULL;
+
+ value = g_strjoinv(",", global_dns_servers);
+
+ keyfile = g_key_file_new();
+ g_key_file_set_list_separator(keyfile, NM_CONFIG_KEYFILE_LIST_SEPARATOR);
+
+ g_key_file_set_value(keyfile,
+ NM_CONFIG_KEYFILE_GROUP_GLOBAL_DNS,
+ NM_CONFIG_KEYFILE_KEY_GLOBAL_DNS_OPTIONS,
+ "");
+ g_key_file_set_value(keyfile,
+ NM_CONFIG_KEYFILE_GROUPPREFIX_GLOBAL_DNS_DOMAIN "*",
+ NM_CONFIG_KEYFILE_KEY_GLOBAL_DNS_DOMAIN_SERVERS,
+ value);
+
+ if (!dump_to_stdout) {
+ add_keyfile_comment(keyfile, "from \"rd.net.dns\"");
+ }
+
+ v = (NMUtilsNamedValue) {
+ .name = g_strdup_printf("%s/16-global-dns.conf", run_config_dir),
+ .value_str = g_key_file_to_data(keyfile, NULL, NULL),
+ };
+ g_array_append_val(confs, v);
+ }
+
if (dump_to_stdout) {
nm_clear_g_free(&connections_dir);
nm_clear_g_free(&initrd_dir);
diff --git a/src/nm-initrd-generator/nm-initrd-generator.h b/src/nm-initrd-generator/nm-initrd-generator.h
index 87db9fc665..23dd484889 100644
--- a/src/nm-initrd-generator/nm-initrd-generator.h
+++ b/src/nm-initrd-generator/nm-initrd-generator.h
@@ -45,6 +45,7 @@ GHashTable *nmi_cmdline_reader_parse(const char *etc_connections_dir,
const char *sysfs_dir,
const char *const *argv,
char **hostname,
- gint64 *carrier_timeout_sec);
+ gint64 *carrier_timeout_sec,
+ char ***global_dns_servers);
#endif /* __NM_INITRD_GENERATOR_H__ */
diff --git a/src/nm-initrd-generator/nmi-cmdline-reader.c b/src/nm-initrd-generator/nmi-cmdline-reader.c
index 80fb550ce1..91fe5d1d51 100644
--- a/src/nm-initrd-generator/nmi-cmdline-reader.c
+++ b/src/nm-initrd-generator/nmi-cmdline-reader.c
@@ -36,10 +36,11 @@ typedef struct {
GHashTable *znet_ifnames;
/* Parameters to be set for all connections */
- gboolean ignore_auto_dns;
- int dhcp_timeout;
- char *dhcp4_vci;
- char *dhcp_dscp;
+ gboolean ignore_auto_dns;
+ int dhcp_timeout;
+ char *dhcp4_vci;
+ char *dhcp_dscp;
+ GPtrArray *global_dns;
gint64 carrier_timeout_sec;
} Reader;
@@ -69,6 +70,7 @@ reader_destroy(Reader *reader, gboolean free_hash)
g_ptr_array_unref(reader->array);
g_ptr_array_unref(reader->vlan_parents);
+ nm_clear_pointer(&reader->global_dns, g_ptr_array_unref);
g_hash_table_unref(reader->explicit_ip_connections);
hash = g_steal_pointer(&reader->hash);
nm_clear_g_free(&reader->hostname);
@@ -1219,6 +1221,21 @@ reader_parse_rd_znet(Reader *reader, char *argument, gboolean net_ifnames)
}
}
+static void
+reader_parse_global_dns(Reader *reader, char *argument)
+{
+ if (!nm_dns_uri_parse(AF_UNSPEC, argument, NULL)) {
+ _LOGW(LOGD_CORE, "rd.net.dns: invalid server '%s'", argument);
+ return;
+ }
+
+ if (!reader->global_dns) {
+ reader->global_dns = g_ptr_array_new_with_free_func(g_free);
+ }
+
+ g_ptr_array_add(reader->global_dns, g_strdup(argument));
+}
+
static void
reader_parse_ethtool(Reader *reader, char *argument)
{
@@ -1392,7 +1409,8 @@ nmi_cmdline_reader_parse(const char *etc_connections_dir,
const char *sysfs_dir,
const char *const *argv,
char **hostname,
- gint64 *carrier_timeout_sec)
+ gint64 *carrier_timeout_sec,
+ char ***global_dns_servers)
{
Reader *reader;
const char *tag;
@@ -1509,6 +1527,8 @@ nmi_cmdline_reader_parse(const char *etc_connections_dir,
bootif_val = g_strdup(argument);
} else if (nm_streq(tag, "rd.ethtool")) {
reader_parse_ethtool(reader, argument);
+ } else if (nm_streq(tag, "rd.net.dns")) {
+ reader_parse_global_dns(reader, argument);
}
}
@@ -1626,5 +1646,15 @@ nmi_cmdline_reader_parse(const char *etc_connections_dir,
NM_SET_OUT(carrier_timeout_sec, reader->carrier_timeout_sec);
+ if (reader->global_dns) {
+ if (global_dns_servers) {
+ g_ptr_array_add(reader->global_dns, NULL);
+ *global_dns_servers = (char **) g_ptr_array_free(reader->global_dns, FALSE);
+ reader->global_dns = NULL;
+ }
+ } else {
+ NM_SET_OUT(global_dns_servers, NULL);
+ }
+
return reader_destroy(reader, FALSE);
}
diff --git a/src/nm-initrd-generator/tests/test-cmdline-reader.c b/src/nm-initrd-generator/tests/test-cmdline-reader.c
index 33b834979c..07cf57bdaa 100644
--- a/src/nm-initrd-generator/tests/test-cmdline-reader.c
+++ b/src/nm-initrd-generator/tests/test-cmdline-reader.c
@@ -23,7 +23,7 @@
/*****************************************************************************/
-#define _parse(ARGV, out_hostname, out_carrier_timeout_sec) \
+#define _parse(ARGV, out_hostname, out_carrier_timeout_sec, _out_global_dns_servers) \
({ \
const char *const *const _ARGV = (ARGV); \
char **const _out_hostname = (out_hostname); \
@@ -34,26 +34,28 @@
TEST_INITRD_DIR "/sysfs", \
_ARGV, \
_out_hostname, \
- _out_carrier_timeout_sec); \
+ _out_carrier_timeout_sec, \
+ _out_global_dns_servers); \
\
g_assert(_connections); \
\
_connections; \
})
-#define _parse_cons(ARGV) \
- ({ \
- GHashTable *_con_connections; \
- gs_free char *_con_hostname = NULL; \
- gint64 _con_carrier_timeout_sec = 0; \
- \
- _con_connections = _parse((ARGV), \
- nmtst_get_rand_bool() ? &_con_hostname : NULL, \
- nmtst_get_rand_bool() ? &_con_carrier_timeout_sec : NULL); \
- g_assert_cmpstr(_con_hostname, ==, NULL); \
- g_assert_cmpint(_con_carrier_timeout_sec, ==, 0); \
- \
- _con_connections; \
+#define _parse_cons(ARGV) \
+ ({ \
+ GHashTable *_con_connections; \
+ gs_free char *_con_hostname = NULL; \
+ gint64 _con_carrier_timeout_sec = 0; \
+ \
+ _con_connections = _parse((ARGV), \
+ nmtst_get_rand_bool() ? &_con_hostname : NULL, \
+ nmtst_get_rand_bool() ? &_con_carrier_timeout_sec : NULL, \
+ NULL); \
+ g_assert_cmpstr(_con_hostname, ==, NULL); \
+ g_assert_cmpint(_con_carrier_timeout_sec, ==, 0); \
+ \
+ _con_connections; \
})
#define _parse_con(ARGV, connection_name) \
@@ -154,7 +156,7 @@ test_dhcp_with_hostname(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 1);
g_assert_cmpstr(hostname, ==, "host1");
g_assert_cmpint(carrier_timeout_sec, ==, 0);
@@ -424,7 +426,7 @@ test_if_ip4_manual(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 2);
g_assert_cmpstr(hostname, ==, "hostname1.example.com");
g_assert_cmpint(carrier_timeout_sec, ==, 0);
@@ -505,7 +507,7 @@ test_if_ip4_auto(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 1);
g_assert_cmpstr(hostname, ==, "myhostname");
g_assert_cmpint(carrier_timeout_sec, ==, 0);
@@ -596,7 +598,7 @@ test_if_ip6_manual(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 1);
g_assert_cmpstr(hostname, ==, "hostname0.example.com");
g_assert_cmpint(carrier_timeout_sec, ==, 0);
@@ -684,7 +686,7 @@ test_if_mac_ifname(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 1);
g_assert_cmpstr(hostname, ==, "hostname0");
g_assert_cmpint(carrier_timeout_sec, ==, 0);
@@ -1840,7 +1842,7 @@ test_rd_znet(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 2);
g_assert_cmpstr(hostname, ==, "foo.example.com");
g_assert_cmpint(carrier_timeout_sec, ==, 0);
@@ -1927,7 +1929,7 @@ test_rd_znet_legacy(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 2);
g_assert_cmpstr(hostname, ==, "foo.example.com");
g_assert_cmpint(carrier_timeout_sec, ==, 0);
@@ -2006,7 +2008,7 @@ test_rd_znet_ifnames(void)
gint64 carrier_timeout_sec = 0;
const char *const *v_subchannels;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 2);
connection = g_hash_table_lookup(connections, "zeth0");
@@ -2281,7 +2283,7 @@ test_nameserver(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 3);
g_assert_cmpstr(hostname, ==, "foo.example.com");
g_assert_cmpint(carrier_timeout_sec, ==, 0);
@@ -2460,12 +2462,38 @@ test_carrier_timeout(void)
gs_free char *hostname = NULL;
gint64 carrier_timeout_sec = 0;
- connections = _parse(ARGV, &hostname, &carrier_timeout_sec);
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, NULL);
g_assert_cmpint(g_hash_table_size(connections), ==, 0);
g_assert_cmpstr(hostname, ==, NULL);
g_assert_cmpint(carrier_timeout_sec, ==, 20);
}
+static void
+test_global_dns(void)
+{
+ gs_unref_hashtable GHashTable *connections = NULL;
+ const char *const *ARGV = NM_MAKE_STRV("rd.net.dns=dns+tls://8.8.8.8",
+ "rd.net.dns=1.1.1.1",
+ "rd.net.dns=foobar",
+ "rd.net.dns=dns+tls://[fd01::1]:35#name");
+ gs_free char *hostname = NULL;
+ gs_strfreev char **global_dns_servers = NULL;
+ gint64 carrier_timeout_sec = 0;
+
+ NMTST_EXPECT_NM_WARN("cmdline-reader: rd.net.dns: invalid server 'foobar'");
+ connections = _parse(ARGV, &hostname, &carrier_timeout_sec, &global_dns_servers);
+ g_test_assert_expected_messages();
+
+ g_assert_cmpint(g_hash_table_size(connections), ==, 0);
+ g_assert_cmpstr(hostname, ==, NULL);
+ g_assert_cmpint(carrier_timeout_sec, ==, 0);
+ g_assert(global_dns_servers != NULL);
+ g_assert_cmpstr(global_dns_servers[0], ==, "dns+tls://8.8.8.8");
+ g_assert_cmpstr(global_dns_servers[1], ==, "1.1.1.1");
+ g_assert_cmpstr(global_dns_servers[2], ==, "dns+tls://[fd01::1]:35#name");
+ g_assert_cmpstr(global_dns_servers[3], ==, NULL);
+}
+
#define _ethtool_check_inval(arg) \
G_STMT_START \
{ \
@@ -2686,6 +2714,7 @@ main(int argc, char **argv)
g_test_add_func("/initrd/cmdline/carrier_timeout", test_carrier_timeout);
g_test_add_func("/initrd/cmdline/rd_ethtool", test_rd_ethtool);
g_test_add_func("/initrd/cmdline/plain_equal_char", test_plain_equal_char);
+ g_test_add_func("/initrd/cmdline/global_dns", test_global_dns);
return g_test_run();
}
diff --git a/src/nmcli/gen-metadata-nm-settings-nmcli.xml.in b/src/nmcli/gen-metadata-nm-settings-nmcli.xml.in
index 6004b110ac..9aa1751e7e 100644
--- a/src/nmcli/gen-metadata-nm-settings-nmcli.xml.in
+++ b/src/nmcli/gen-metadata-nm-settings-nmcli.xml.in
@@ -1325,7 +1325,7 @@
format="string"
values="auto, link-local, manual, shared, disabled" />