2aacef
From d34f971ad09e43f583ff570e26c87e6cdc83d69d Mon Sep 17 00:00:00 2001
2aacef
From: Yu Watanabe <watanabe.yu+github@gmail.com>
2aacef
Date: Fri, 4 Nov 2022 12:53:07 +0900
2aacef
Subject: [PATCH] resolve: introduce link_get_llmnr_support() and
2aacef
 link_get_mdns_support()
2aacef
2aacef
(cherry picked from commit bce459e3275249574f2142236275b2c33a9f88e3)
2aacef
2aacef
Related: #2138081
2aacef
---
2aacef
 src/resolve/resolved-link.c | 49 ++++++++++++++++++++++---------------
2aacef
 src/resolve/resolved-link.h |  3 +++
2aacef
 src/shared/resolve-util.h   |  3 ++-
2aacef
 3 files changed, 34 insertions(+), 21 deletions(-)
2aacef
2aacef
diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c
2aacef
index 409d725686..86112f3a3b 100644
2aacef
--- a/src/resolve/resolved-link.c
2aacef
+++ b/src/resolve/resolved-link.c
2aacef
@@ -140,8 +140,7 @@ void link_allocate_scopes(Link *l) {
2aacef
                 l->unicast_scope = dns_scope_free(l->unicast_scope);
2aacef
 
2aacef
         if (link_relevant(l, AF_INET, true) &&
2aacef
-            l->llmnr_support != RESOLVE_SUPPORT_NO &&
2aacef
-            l->manager->llmnr_support != RESOLVE_SUPPORT_NO) {
2aacef
+            link_get_llmnr_support(l) != RESOLVE_SUPPORT_NO) {
2aacef
                 if (!l->llmnr_ipv4_scope) {
2aacef
                         r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
2aacef
                         if (r < 0)
2aacef
@@ -151,8 +150,7 @@ void link_allocate_scopes(Link *l) {
2aacef
                 l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
2aacef
 
2aacef
         if (link_relevant(l, AF_INET6, true) &&
2aacef
-            l->llmnr_support != RESOLVE_SUPPORT_NO &&
2aacef
-            l->manager->llmnr_support != RESOLVE_SUPPORT_NO) {
2aacef
+            link_get_llmnr_support(l) != RESOLVE_SUPPORT_NO) {
2aacef
                 if (!l->llmnr_ipv6_scope) {
2aacef
                         r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
2aacef
                         if (r < 0)
2aacef
@@ -162,8 +160,7 @@ void link_allocate_scopes(Link *l) {
2aacef
                 l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
2aacef
 
2aacef
         if (link_relevant(l, AF_INET, true) &&
2aacef
-            l->mdns_support != RESOLVE_SUPPORT_NO &&
2aacef
-            l->manager->mdns_support != RESOLVE_SUPPORT_NO) {
2aacef
+            link_get_mdns_support(l) != RESOLVE_SUPPORT_NO) {
2aacef
                 if (!l->mdns_ipv4_scope) {
2aacef
                         r = dns_scope_new(l->manager, &l->mdns_ipv4_scope, l, DNS_PROTOCOL_MDNS, AF_INET);
2aacef
                         if (r < 0)
2aacef
@@ -173,8 +170,7 @@ void link_allocate_scopes(Link *l) {
2aacef
                 l->mdns_ipv4_scope = dns_scope_free(l->mdns_ipv4_scope);
2aacef
 
2aacef
         if (link_relevant(l, AF_INET6, true) &&
2aacef
-            l->mdns_support != RESOLVE_SUPPORT_NO &&
2aacef
-            l->manager->mdns_support != RESOLVE_SUPPORT_NO) {
2aacef
+            link_get_mdns_support(l) != RESOLVE_SUPPORT_NO) {
2aacef
                 if (!l->mdns_ipv6_scope) {
2aacef
                         r = dns_scope_new(l->manager, &l->mdns_ipv6_scope, l, DNS_PROTOCOL_MDNS, AF_INET6);
2aacef
                         if (r < 0)
2aacef
@@ -191,8 +187,7 @@ void link_add_rrs(Link *l, bool force_remove) {
2aacef
                 link_address_add_rrs(a, force_remove);
2aacef
 
2aacef
         if (!force_remove &&
2aacef
-            l->mdns_support == RESOLVE_SUPPORT_YES &&
2aacef
-            l->manager->mdns_support == RESOLVE_SUPPORT_YES) {
2aacef
+            link_get_mdns_support(l) == RESOLVE_SUPPORT_YES) {
2aacef
 
2aacef
                 if (l->mdns_ipv4_scope) {
2aacef
                         r = dns_scope_add_dnssd_services(l->mdns_ipv4_scope);
2aacef
@@ -651,13 +646,13 @@ int link_update(Link *l) {
2aacef
         if (r < 0)
2aacef
                 return r;
2aacef
 
2aacef
-        if (l->llmnr_support != RESOLVE_SUPPORT_NO) {
2aacef
+        if (link_get_llmnr_support(l) != RESOLVE_SUPPORT_NO) {
2aacef
                 r = manager_llmnr_start(l->manager);
2aacef
                 if (r < 0)
2aacef
                         return r;
2aacef
         }
2aacef
 
2aacef
-        if (l->mdns_support != RESOLVE_SUPPORT_NO) {
2aacef
+        if (link_get_mdns_support(l) != RESOLVE_SUPPORT_NO) {
2aacef
                 r = manager_mdns_start(l->manager);
2aacef
                 if (r < 0)
2aacef
                         return r;
2aacef
@@ -802,6 +797,24 @@ bool link_dnssec_supported(Link *l) {
2aacef
         return true;
2aacef
 }
2aacef
 
2aacef
+ResolveSupport link_get_llmnr_support(Link *link) {
2aacef
+        assert(link);
2aacef
+        assert(link->manager);
2aacef
+
2aacef
+        /* This provides the effective LLMNR support level for the link, instead of the 'internal' per-link setting. */
2aacef
+
2aacef
+        return MIN(link->llmnr_support, link->manager->llmnr_support);
2aacef
+}
2aacef
+
2aacef
+ResolveSupport link_get_mdns_support(Link *link) {
2aacef
+        assert(link);
2aacef
+        assert(link->manager);
2aacef
+
2aacef
+        /* This provides the effective mDNS support level for the link, instead of the 'internal' per-link setting. */
2aacef
+
2aacef
+        return MIN(link->mdns_support, link->manager->mdns_support);
2aacef
+}
2aacef
+
2aacef
 int link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr) {
2aacef
         LinkAddress *a;
2aacef
 
2aacef
@@ -885,8 +898,7 @@ void link_address_add_rrs(LinkAddress *a, bool force_remove) {
2aacef
                 if (!force_remove &&
2aacef
                     link_address_relevant(a, true) &&
2aacef
                     a->link->llmnr_ipv4_scope &&
2aacef
-                    a->link->llmnr_support == RESOLVE_SUPPORT_YES &&
2aacef
-                    a->link->manager->llmnr_support == RESOLVE_SUPPORT_YES) {
2aacef
+                    link_get_llmnr_support(a->link) == RESOLVE_SUPPORT_YES) {
2aacef
 
2aacef
                         if (!a->link->manager->llmnr_host_ipv4_key) {
2aacef
                                 a->link->manager->llmnr_host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->llmnr_hostname);
2aacef
@@ -939,8 +951,7 @@ void link_address_add_rrs(LinkAddress *a, bool force_remove) {
2aacef
                 if (!force_remove &&
2aacef
                     link_address_relevant(a, true) &&
2aacef
                     a->link->mdns_ipv4_scope &&
2aacef
-                    a->link->mdns_support == RESOLVE_SUPPORT_YES &&
2aacef
-                    a->link->manager->mdns_support == RESOLVE_SUPPORT_YES) {
2aacef
+                    link_get_mdns_support(a->link) == RESOLVE_SUPPORT_YES) {
2aacef
                         if (!a->link->manager->mdns_host_ipv4_key) {
2aacef
                                 a->link->manager->mdns_host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->mdns_hostname);
2aacef
                                 if (!a->link->manager->mdns_host_ipv4_key) {
2aacef
@@ -995,8 +1006,7 @@ void link_address_add_rrs(LinkAddress *a, bool force_remove) {
2aacef
                 if (!force_remove &&
2aacef
                     link_address_relevant(a, true) &&
2aacef
                     a->link->llmnr_ipv6_scope &&
2aacef
-                    a->link->llmnr_support == RESOLVE_SUPPORT_YES &&
2aacef
-                    a->link->manager->llmnr_support == RESOLVE_SUPPORT_YES) {
2aacef
+                    link_get_llmnr_support(a->link) == RESOLVE_SUPPORT_YES) {
2aacef
 
2aacef
                         if (!a->link->manager->llmnr_host_ipv6_key) {
2aacef
                                 a->link->manager->llmnr_host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->llmnr_hostname);
2aacef
@@ -1049,8 +1059,7 @@ void link_address_add_rrs(LinkAddress *a, bool force_remove) {
2aacef
                 if (!force_remove &&
2aacef
                     link_address_relevant(a, true) &&
2aacef
                     a->link->mdns_ipv6_scope &&
2aacef
-                    a->link->mdns_support == RESOLVE_SUPPORT_YES &&
2aacef
-                    a->link->manager->mdns_support == RESOLVE_SUPPORT_YES) {
2aacef
+                    link_get_mdns_support(a->link) == RESOLVE_SUPPORT_YES) {
2aacef
 
2aacef
                         if (!a->link->manager->mdns_host_ipv6_key) {
2aacef
                                 a->link->manager->mdns_host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->mdns_hostname);
2aacef
diff --git a/src/resolve/resolved-link.h b/src/resolve/resolved-link.h
2aacef
index b5299e0b5b..d2043a1000 100644
2aacef
--- a/src/resolve/resolved-link.h
2aacef
+++ b/src/resolve/resolved-link.h
2aacef
@@ -104,6 +104,9 @@ bool link_dnssec_supported(Link *l);
2aacef
 
2aacef
 DnsOverTlsMode link_get_dns_over_tls_mode(Link *l);
2aacef
 
2aacef
+ResolveSupport link_get_llmnr_support(Link *link);
2aacef
+ResolveSupport link_get_mdns_support(Link *link);
2aacef
+
2aacef
 int link_save_user(Link *l);
2aacef
 int link_load_user(Link *l);
2aacef
 void link_remove_user(Link *l);
2aacef
diff --git a/src/shared/resolve-util.h b/src/shared/resolve-util.h
2aacef
index d9ab387301..e58173d864 100644
2aacef
--- a/src/shared/resolve-util.h
2aacef
+++ b/src/shared/resolve-util.h
2aacef
@@ -25,10 +25,11 @@ typedef enum ResolveSupport ResolveSupport;
2aacef
 typedef enum DnssecMode DnssecMode;
2aacef
 typedef enum DnsOverTlsMode DnsOverTlsMode;
2aacef
 
2aacef
+/* Do not change the order, see link_get_llmnr_support() or link_get_mdns_support(). */
2aacef
 enum ResolveSupport {
2aacef
         RESOLVE_SUPPORT_NO,
2aacef
-        RESOLVE_SUPPORT_YES,
2aacef
         RESOLVE_SUPPORT_RESOLVE,
2aacef
+        RESOLVE_SUPPORT_YES,
2aacef
         _RESOLVE_SUPPORT_MAX,
2aacef
         _RESOLVE_SUPPORT_INVALID = -EINVAL,
2aacef
 };