]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: introduce link_get_use_dns()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 15 Apr 2024 07:16:13 +0000 (16:16 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 19 Apr 2024 01:23:00 +0000 (10:23 +0900)
No functional change, just refactoring.

src/network/networkd-dhcp-server.c
src/network/networkd-dhcp4.c
src/network/networkd-dhcp6.c
src/network/networkd-dns.c
src/network/networkd-dns.h
src/network/networkd-json.c
src/network/networkd-ndisc.c
src/network/networkd-network-gperf.gperf
src/network/networkd-network.c
src/network/networkd-network.h
src/network/networkd-state-file.c

index b1953545eda769e3885fea87b9056f7fd5bc7838..6b9dcd6fe6616ccfc3257f1744dec8ff577155d1 100644 (file)
@@ -342,7 +342,7 @@ static int link_push_uplink_to_dhcp_server(
                         addresses[n_addresses++] = ia;
                 }
 
-                use_dhcp_lease_data = link->network->dhcp_use_dns;
+                use_dhcp_lease_data = link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4);
                 break;
 
         case SD_DHCP_LEASE_NTP: {
index 89622e6cfcf979f62bfbe4319a6504ccec3cd3ca..e02b9257f8affe4ea01305d820348a9ba02970f2 100644 (file)
@@ -728,7 +728,7 @@ static int dhcp4_request_routes_to_dns(Link *link) {
         assert(link->dhcp_lease);
         assert(link->network);
 
-        if (!link->network->dhcp_use_dns ||
+        if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4) ||
             !link->network->dhcp_routes_to_dns)
                 return 0;
 
index bf7d3c7e8ec97cddecf1e866271084497ee031ab..426d2de2356d294e5253e20f3503a3005258ec24 100644 (file)
@@ -633,7 +633,7 @@ static int dhcp6_configure(Link *link) {
                         return log_link_debug_errno(link, r, "DHCPv6 CLIENT: Failed to set MUD URL: %m");
         }
 
-        if (link->network->dhcp6_use_dns) {
+        if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
                 r = sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DNS_SERVER);
                 if (r < 0)
                         return log_link_debug_errno(link, r, "DHCPv6 CLIENT: Failed to request DNS servers: %m");
index eb01f4758ebff9cdf6a813401ee3e127fdb11048..150ce624821f007c4cdd29b41d976d8cc7f7cba3 100644 (file)
@@ -53,6 +53,43 @@ UseDomains link_get_use_domains(Link *link, NetworkConfigSource proto) {
         return USE_DOMAINS_NO;
 }
 
+bool link_get_use_dns(Link *link, NetworkConfigSource proto) {
+        int n, c;
+
+        assert(link);
+
+        if (!link->network)
+                return false;
+
+        switch (proto) {
+        case NETWORK_CONFIG_SOURCE_DHCP4:
+                n = link->network->dhcp_use_dns;
+                c = link->network->compat_dhcp_use_dns;
+                break;
+        case NETWORK_CONFIG_SOURCE_DHCP6:
+                n = link->network->dhcp6_use_dns;
+                c = link->network->compat_dhcp_use_dns;
+                break;
+        case NETWORK_CONFIG_SOURCE_NDISC:
+                n = link->network->ndisc_use_dns;
+                c = -1;
+                break;
+        default:
+                assert_not_reached();
+        }
+
+        /* If per-network and per-protocol setting is specified, use it. */
+        if (n >= 0)
+                return n;
+
+        /* If compat setting is specified, use it. */
+        if (c >= 0)
+                return c;
+
+        /* Otherwise, defaults to yes. */
+        return true;
+}
+
 int config_parse_domains(
                 const char *unit,
                 const char *filename,
@@ -243,57 +280,6 @@ int config_parse_dnssec_negative_trust_anchors(
         }
 }
 
-int config_parse_dhcp_use_dns(
-                const char* unit,
-                const char *filename,
-                unsigned line,
-                const char *section,
-                unsigned section_line,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
-
-        Network *network = userdata;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
-        assert(rvalue);
-        assert(data);
-
-        r = parse_boolean(rvalue);
-        if (r < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Failed to parse UseDNS=%s, ignoring assignment: %m", rvalue);
-                return 0;
-        }
-
-        switch (ltype) {
-        case AF_INET:
-                network->dhcp_use_dns = r;
-                network->dhcp_use_dns_set = true;
-                break;
-        case AF_INET6:
-                network->dhcp6_use_dns = r;
-                network->dhcp6_use_dns_set = true;
-                break;
-        case AF_UNSPEC:
-                /* For backward compatibility. */
-                if (!network->dhcp_use_dns_set)
-                        network->dhcp_use_dns = r;
-                if (!network->dhcp6_use_dns_set)
-                        network->dhcp6_use_dns = r;
-                break;
-        default:
-                assert_not_reached();
-        }
-
-        return 0;
-}
-
 static const char* const use_domains_table[_USE_DOMAINS_MAX] = {
         [USE_DOMAINS_NO]    = "no",
         [USE_DOMAINS_ROUTE] = "route",
index c1b3429b8bacffb34081164e27571aaa056abcce..915cb32fb12bb72137d2b6e66873f5a61c363534 100644 (file)
@@ -16,6 +16,7 @@ typedef enum UseDomains {
 } UseDomains;
 
 UseDomains link_get_use_domains(Link *link, NetworkConfigSource proto);
+bool link_get_use_dns(Link *link, NetworkConfigSource proto);
 
 const char* use_domains_to_string(UseDomains p) _const_;
 UseDomains use_domains_from_string(const char *s) _pure_;
index 4fd75f4bfe4858520b1cff7763a3714bc9074508..d9eeaff1226c2dae0a615f4a714f144445f947fe 100644 (file)
@@ -448,7 +448,7 @@ static int dns_append_json(Link *link, JsonVariant **v) {
                                 return r;
                 }
 
-                if (link->dhcp_lease && link->network->dhcp_use_dns) {
+                if (link->dhcp_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
                         const struct in_addr *dns;
                         union in_addr_union s;
                         int n_dns;
@@ -469,7 +469,7 @@ static int dns_append_json(Link *link, JsonVariant **v) {
                         }
                 }
 
-                if (link->dhcp6_lease && link->network->dhcp6_use_dns) {
+                if (link->dhcp6_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
                         const struct in6_addr *dns;
                         union in_addr_union s;
                         int n_dns;
@@ -490,7 +490,7 @@ static int dns_append_json(Link *link, JsonVariant **v) {
                         }
                 }
 
-                if (link->network->ndisc_use_dns) {
+                if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
                         NDiscRDNSS *a;
 
                         SET_FOREACH(a, link->ndisc_rdnss) {
index c7d7fc7efc68907345f069ba943c28e2a3c3a21e..92ff7c71f36e2e3dd0de325ff0a03a0a57812a27 100644 (file)
@@ -1409,7 +1409,7 @@ static int ndisc_router_process_rdnss(Link *link, sd_ndisc_router *rt) {
         assert(link->network);
         assert(rt);
 
-        if (!link->network->ndisc_use_dns)
+        if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC))
                 return 0;
 
         r = sd_ndisc_router_get_sender_address(rt, &router);
index ef60797f2e31b080b08b059b23ca8d24773614ae..f5a6debc0aa5aea30d2267e027cb65ae850e573b 100644 (file)
@@ -222,7 +222,7 @@ NextHop.Blackhole,                           config_parse_nexthop_blackhole,
 NextHop.Group,                               config_parse_nexthop_group,                               0,                             0
 DHCPv4.RequestAddress,                       config_parse_in_addr_non_null,                            AF_INET,                       offsetof(Network, dhcp_request_address)
 DHCPv4.ClientIdentifier,                     config_parse_dhcp_client_identifier,                      0,                             offsetof(Network, dhcp_client_identifier)
-DHCPv4.UseDNS,                               config_parse_dhcp_use_dns,                                AF_INET,                       0
+DHCPv4.UseDNS,                               config_parse_tristate,                                    0,                             offsetof(Network, dhcp_use_dns)
 DHCPv4.RoutesToDNS,                          config_parse_bool,                                        0,                             offsetof(Network, dhcp_routes_to_dns)
 DHCPv4.UseNTP,                               config_parse_dhcp_use_ntp,                                AF_INET,                       0
 DHCPv4.RoutesToNTP,                          config_parse_bool,                                        0,                             offsetof(Network, dhcp_routes_to_ntp)
@@ -271,7 +271,7 @@ DHCPv4.NFTSet,                               config_parse_nft_set,
 DHCPv4.RapidCommit,                          config_parse_tristate,                                    0,                             offsetof(Network, dhcp_use_rapid_commit)
 DHCPv6.UseAddress,                           config_parse_bool,                                        0,                             offsetof(Network, dhcp6_use_address)
 DHCPv6.UseDelegatedPrefix,                   config_parse_bool,                                        0,                             offsetof(Network, dhcp6_use_pd_prefix)
-DHCPv6.UseDNS,                               config_parse_dhcp_use_dns,                                AF_INET6,                      0
+DHCPv6.UseDNS,                               config_parse_tristate,                                    0,                             offsetof(Network, dhcp6_use_dns)
 DHCPv6.UseHostname,                          config_parse_bool,                                        0,                             offsetof(Network, dhcp6_use_hostname)
 DHCPv6.UseDomains,                           config_parse_use_domains,                                 0,                             offsetof(Network, dhcp6_use_domains)
 DHCPv6.UseNTP,                               config_parse_dhcp_use_ntp,                                AF_INET6,                      0
@@ -299,7 +299,7 @@ IPv6AcceptRA.UseRoutePrefix,                 config_parse_bool,
 IPv6AcceptRA.UseAutonomousPrefix,            config_parse_bool,                                        0,                             offsetof(Network, ndisc_use_autonomous_prefix)
 IPv6AcceptRA.UseOnLinkPrefix,                config_parse_bool,                                        0,                             offsetof(Network, ndisc_use_onlink_prefix)
 IPv6AcceptRA.UsePREF64,                      config_parse_bool,                                        0,                             offsetof(Network, ndisc_use_pref64)
-IPv6AcceptRA.UseDNS,                         config_parse_bool,                                        0,                             offsetof(Network, ndisc_use_dns)
+IPv6AcceptRA.UseDNS,                         config_parse_tristate,                                    0,                             offsetof(Network, ndisc_use_dns)
 IPv6AcceptRA.UseDomains,                     config_parse_use_domains,                                 0,                             offsetof(Network, ndisc_use_domains)
 IPv6AcceptRA.UseMTU,                         config_parse_bool,                                        0,                             offsetof(Network, ndisc_use_mtu)
 IPv6AcceptRA.UseHopLimit,                    config_parse_bool,                                        0,                             offsetof(Network, ndisc_use_hop_limit)
@@ -588,7 +588,7 @@ IPv6PrefixDelegation.Domains,                config_parse_radv_search_domains,
 IPv6PrefixDelegation.DNSLifetimeSec,         config_parse_sec,                                         0,                             offsetof(Network, router_dns_lifetime_usec)
 DHCPv4.BlackList,                            config_parse_in_addr_prefixes,                            AF_INET,                       offsetof(Network, dhcp_deny_listed_ip)
 DHCP.ClientIdentifier,                       config_parse_dhcp_client_identifier,                      0,                             offsetof(Network, dhcp_client_identifier)
-DHCP.UseDNS,                                 config_parse_dhcp_use_dns,                                AF_UNSPEC,                     0
+DHCP.UseDNS,                                 config_parse_tristate,                                    0,                             offsetof(Network, compat_dhcp_use_dns)
 DHCP.UseNTP,                                 config_parse_dhcp_use_ntp,                                AF_UNSPEC,                     0
 DHCP.UseMTU,                                 config_parse_bool,                                        0,                             offsetof(Network, dhcp_use_mtu)
 DHCP.UseHostname,                            config_parse_bool,                                        0,                             offsetof(Network, dhcp_use_hostname)
index f270142d55708b94beffcd20a25190090bb5f11c..979d770f686020833874116bd65a2ee6e4034a57 100644 (file)
@@ -381,6 +381,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
                 .keep_configuration = manager->keep_configuration,
 
                 .compat_dhcp_use_domains = _USE_DOMAINS_INVALID,
+                .compat_dhcp_use_dns = -1,
 
                 .dhcp_duid.type = _DUID_TYPE_INVALID,
                 .dhcp_critical = -1,
@@ -388,7 +389,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
                 .dhcp_routes_to_ntp = true,
                 .dhcp_use_sip = true,
                 .dhcp_use_captive_portal = true,
-                .dhcp_use_dns = true,
+                .dhcp_use_dns = -1,
                 .dhcp_routes_to_dns = true,
                 .dhcp_use_domains = _USE_DOMAINS_INVALID,
                 .dhcp_use_hostname = true,
@@ -406,7 +407,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
 
                 .dhcp6_use_address = true,
                 .dhcp6_use_pd_prefix = true,
-                .dhcp6_use_dns = true,
+                .dhcp6_use_dns = -1,
                 .dhcp6_use_domains = _USE_DOMAINS_INVALID,
                 .dhcp6_use_hostname = true,
                 .dhcp6_use_ntp = true,
@@ -480,7 +481,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
 
                 .ndisc = -1,
                 .ndisc_use_redirect = true,
-                .ndisc_use_dns = true,
+                .ndisc_use_dns = -1,
                 .ndisc_use_gateway = true,
                 .ndisc_use_captive_portal = true,
                 .ndisc_use_route_prefix = true,
index 6fc2eddf00d76737a258cfbeb9bc7fc53715101f..0f1621d831df07e4bec33877a24a6e2453810649 100644 (file)
@@ -115,6 +115,7 @@ struct Network {
 
         /* For backward compatibility, only applied to DHCPv4 and DHCPv6. */
         UseDomains compat_dhcp_use_domains;
+        int compat_dhcp_use_dns;
 
         /* DHCP Client Support */
         AddressFamily dhcp;
@@ -147,8 +148,7 @@ struct Network {
         int dhcp_broadcast;
         int dhcp_ipv6_only_mode;
         int dhcp_use_rapid_commit;
-        bool dhcp_use_dns;
-        bool dhcp_use_dns_set;
+        int dhcp_use_dns;
         bool dhcp_routes_to_dns;
         bool dhcp_use_ntp;
         bool dhcp_use_ntp_set;
@@ -180,8 +180,7 @@ struct Network {
         bool dhcp6_use_pd_prefix;
         bool dhcp6_send_hostname;
         bool dhcp6_send_hostname_set;
-        bool dhcp6_use_dns;
-        bool dhcp6_use_dns_set;
+        int dhcp6_use_dns;
         bool dhcp6_use_hostname;
         bool dhcp6_use_ntp;
         bool dhcp6_use_ntp_set;
@@ -340,7 +339,7 @@ struct Network {
         /* NDisc support */
         int ndisc;
         bool ndisc_use_redirect;
-        bool ndisc_use_dns;
+        int ndisc_use_dns;
         bool ndisc_use_gateway;
         bool ndisc_use_route_prefix;
         bool ndisc_use_autonomous_prefix;
index 182b985cacaaf08dffa065b43b43e9e3ae90a51b..b217b9cc44e0be159d11080d2dcc4603b6291232 100644 (file)
@@ -101,7 +101,7 @@ static int link_put_dns(Link *link, OrderedSet **s) {
         if (r < 0)
                 return r;
 
-        if (link->dhcp_lease && link->network->dhcp_use_dns) {
+        if (link->dhcp_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
                 const struct in_addr *addresses;
 
                 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
@@ -112,7 +112,7 @@ static int link_put_dns(Link *link, OrderedSet **s) {
                 }
         }
 
-        if (link->dhcp6_lease && link->network->dhcp6_use_dns) {
+        if (link->dhcp6_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
                 const struct in6_addr *addresses;
 
                 r = sd_dhcp6_lease_get_dns(link->dhcp6_lease, &addresses);
@@ -123,7 +123,7 @@ static int link_put_dns(Link *link, OrderedSet **s) {
                 }
         }
 
-        if (link->network->ndisc_use_dns) {
+        if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
                 NDiscRDNSS *a;
 
                 SET_FOREACH(a, link->ndisc_rdnss) {
@@ -666,14 +666,14 @@ static int link_save(Link *link) {
                         serialize_addresses(f, NULL, &space,
                                             NULL,
                                             link->dhcp_lease,
-                                            link->network->dhcp_use_dns,
+                                            link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4),
                                             SD_DHCP_LEASE_DNS,
                                             link->dhcp6_lease,
-                                            link->network->dhcp6_use_dns,
+                                            link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6),
                                             sd_dhcp6_lease_get_dns,
                                             NULL);
 
-                        if (link->network->ndisc_use_dns) {
+                        if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
                                 NDiscRDNSS *dd;
 
                                 SET_FOREACH(dd, link->ndisc_rdnss)