chiark / gitweb /
networkd: merge DNS and NTP entries when exporting
[elogind.git] / src / resolve / resolved-link.c
index 1b175c8121a7adc9f4bb339c211cc1adec5ce3c9..43f60d8bb6b5cff67a037315eb89909c9df05236 100644 (file)
@@ -68,8 +68,8 @@ Link *link_free(Link *l) {
                 hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
 
         dns_scope_free(l->unicast_scope);
-        dns_scope_free(l->mdns_ipv4_scope);
-        dns_scope_free(l->mdns_ipv6_scope);
+        dns_scope_free(l->llmnr_ipv4_scope);
+        dns_scope_free(l->llmnr_ipv6_scope);
 
         while (l->dhcp_dns_servers)
                 dns_server_free(l->dhcp_dns_servers);
@@ -79,74 +79,64 @@ Link *link_free(Link *l) {
 
         free(l);
         return NULL;
- }
+}
 
-int link_update_rtnl(Link *l, sd_rtnl_message *m) {
+static void link_allocate_scopes(Link *l) {
         int r;
 
         assert(l);
-        assert(m);
 
-        r = sd_rtnl_message_link_get_flags(m, &l->flags);
-        if (r < 0)
-                return r;
+        if (l->link_dns_servers || l->dhcp_dns_servers) {
+                if (!l->unicast_scope) {
+                        r = dns_scope_new(l->manager, &l->unicast_scope, l, DNS_PROTOCOL_DNS, AF_UNSPEC);
+                        if (r < 0)
+                                log_warning("Failed to allocate DNS scope: %s", strerror(-r));
+                }
+        } else
+                l->unicast_scope = dns_scope_free(l->unicast_scope);
 
-        sd_rtnl_message_read_u32(m, IFLA_MTU, &l->mtu);
-        return 0;
+        if (link_relevant(l, AF_INET) && l->manager->use_llmnr) {
+                if (!l->llmnr_ipv4_scope) {
+                        r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
+                        if (r < 0)
+                                log_warning("Failed to allocate LLMNR IPv4 scope: %s", strerror(-r));
+                }
+        } else
+                l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
+
+        if (link_relevant(l, AF_INET6) && l->manager->use_llmnr) {
+                if (!l->llmnr_ipv6_scope) {
+                        r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
+                        if (r < 0)
+                                log_warning("Failed to allocate LLMNR IPv6 scope: %s", strerror(-r));
+                }
+        } else
+                l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
 }
 
-static int update_dhcp_dns_servers(Link *l) {
-        _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
-        const struct in_addr *nameservers = NULL;
-        DnsServer *s, *nx;
-        int r, n, i;
+int link_update_rtnl(Link *l, sd_rtnl_message *m) {
+        const char *n = NULL;
+        int r;
 
         assert(l);
+        assert(m);
 
-        r = sd_network_dhcp_use_dns(l->ifindex);
-        if (r <= 0)
-                goto clear;
-
-        r = sd_network_get_dhcp_lease(l->ifindex, &lease);
+        r = sd_rtnl_message_link_get_flags(m, &l->flags);
         if (r < 0)
-                goto clear;
-
-        LIST_FOREACH(servers, s, l->dhcp_dns_servers)
-                s->marked = true;
-
-        n = sd_dhcp_lease_get_dns(lease, &nameservers);
-        if (n < 0) {
-                r = n;
-                goto clear;
-        }
+                return r;
 
-        for (i = 0; i < n; i++) {
-                union in_addr_union a = { .in = nameservers[i] };
+        sd_rtnl_message_read_u32(m, IFLA_MTU, &l->mtu);
 
-                s = link_find_dns_server(l, DNS_SERVER_DHCP, AF_INET, &a);
-                if (s)
-                        s->marked = false;
-                else {
-                        r = dns_server_new(l->manager, NULL, DNS_SERVER_DHCP, l, AF_INET, &a);
-                        if (r < 0)
-                                goto clear;
-                }
+        if (sd_rtnl_message_read_string(m, IFLA_IFNAME, &n) >= 0) {
+                strncpy(l->name, n, sizeof(l->name));
+                char_array_0(l->name);
         }
 
-        LIST_FOREACH_SAFE(servers, s, nx, l->dhcp_dns_servers)
-                if (s->marked)
-                        dns_server_free(s);
-
+        link_allocate_scopes(l);
         return 0;
-
-clear:
-        while (l->dhcp_dns_servers)
-                dns_server_free(l->dhcp_dns_servers);
-
-        return r;
 }
 
-static int update_link_dns_servers(Link *l) {
+static int link_update_link_dns_servers(Link *l) {
         _cleanup_free_ struct in_addr *nameservers = NULL;
         _cleanup_free_ struct in6_addr *nameservers6 = NULL;
         DnsServer *s, *nx;
@@ -211,18 +201,14 @@ clear:
 int link_update_monitor(Link *l) {
         assert(l);
 
-        free(l->operational_state);
-        l->operational_state = NULL;
-
-        sd_network_get_link_operational_state(l->ifindex, &l->operational_state);
-
-        update_dhcp_dns_servers(l);
-        update_link_dns_servers(l);
+        link_update_link_dns_servers(l);
+        link_allocate_scopes(l);
 
         return 0;
 }
 
-bool link_relevant(Link *l) {
+bool link_relevant(Link *l, int family) {
+        _cleanup_free_ char *state = NULL;
         LinkAddress *a;
 
         assert(l);
@@ -233,44 +219,39 @@ bool link_relevant(Link *l) {
         if (l->flags & IFF_LOOPBACK)
                 return false;
 
-        if (l->operational_state && !STR_IN_SET(l->operational_state, "unknown", "degraded", "routable"))
+        sd_network_get_link_operational_state(l->ifindex, &state);
+        if (state && !STR_IN_SET(state, "unknown", "degraded", "routable"))
                 return false;
 
         LIST_FOREACH(addresses, a, l->addresses)
-                if (link_address_relevant(a))
+                if (a->family == family && link_address_relevant(a))
                         return true;
 
         return false;
 }
 
-LinkAddress *link_find_address(Link *l, unsigned char family, union in_addr_union *in_addr) {
+LinkAddress *link_find_address(Link *l, int family, union in_addr_union *in_addr) {
         LinkAddress *a;
 
         assert(l);
 
-        LIST_FOREACH(addresses, a, l->addresses) {
-
-                if (a->family == family &&
-                    in_addr_equal(family, &a->in_addr, in_addr))
+        LIST_FOREACH(addresses, a, l->addresses)
+                if (a->family == family && in_addr_equal(family, &a->in_addr, in_addr))
                         return a;
-        }
 
         return NULL;
 }
 
-DnsServer* link_find_dns_server(Link *l, DnsServerSource source, unsigned char family, union in_addr_union *in_addr) {
+DnsServer* link_find_dns_server(Link *l, DnsServerSource source, int family, union in_addr_union *in_addr) {
         DnsServer *first, *s;
 
         assert(l);
 
         first = source == DNS_SERVER_DHCP ? l->dhcp_dns_servers : l->link_dns_servers;
 
-        LIST_FOREACH(servers, s, first) {
-
-                if (s->family == family &&
-                    in_addr_equal(family, &s->address, in_addr))
+        LIST_FOREACH(servers, s, first)
+                if (s->family == family && in_addr_equal(family, &s->address, in_addr))
                         return s;
-        }
 
         return NULL;
 }
@@ -319,7 +300,7 @@ void link_next_dns_server(Link *l) {
         }
 }
 
-int link_address_new(Link *l, LinkAddress **ret, unsigned char family, union in_addr_union *in_addr) {
+int link_address_new(Link *l, LinkAddress **ret, int family, union in_addr_union *in_addr) {
         LinkAddress *a;
 
         assert(l);
@@ -361,10 +342,9 @@ int link_address_update_rtnl(LinkAddress *a, sd_rtnl_message *m) {
         if (r < 0)
                 return r;
 
-        r = sd_rtnl_message_addr_get_scope(m, &a->scope);
-        if (r < 0)
-                return r;
+        sd_rtnl_message_addr_get_scope(m, &a->scope);
 
+        link_allocate_scopes(a->link);
         return 0;
 }