X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fresolve%2Fresolved-manager.c;h=c4a5b08773995b26b7c123f66f40fc703b52f6e5;hp=ba2380d68287dd9c1df085fef52b40cecb6f6015;hb=0e3434aeb1eed951fdbeddacc6126e10d82a1c8b;hpb=5cb36f41f01cf4b1f4395abfffd1b33116591e58 diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index ba2380d68..c4a5b0877 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -34,6 +34,7 @@ #include "socket-util.h" #include "af-list.h" #include "utf8.h" +#include "fileio-label.h" #include "resolved-dns-domain.h" #include "resolved-conf.h" @@ -77,6 +78,10 @@ static int manager_process_link(sd_rtnl *rtnl, sd_rtnl_message *mm, void *userda if (r < 0) goto fail; + r = link_update_monitor(l); + if (r < 0) + goto fail; + if (is_new) log_debug("Found new link %i/%s", ifindex, l->name); @@ -359,7 +364,6 @@ static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, voi } static int manager_watch_hostname(Manager *m) { - _cleanup_free_ char *h = NULL; int r; assert(m); @@ -393,6 +397,66 @@ static int manager_watch_hostname(Manager *m) { return 0; } +static void manager_llmnr_stop(Manager *m) { + assert(m); + + m->llmnr_ipv4_udp_event_source = sd_event_source_unref(m->llmnr_ipv4_udp_event_source); + m->llmnr_ipv4_udp_fd = safe_close(m->llmnr_ipv4_udp_fd); + + m->llmnr_ipv6_udp_event_source = sd_event_source_unref(m->llmnr_ipv6_udp_event_source); + m->llmnr_ipv6_udp_fd = safe_close(m->llmnr_ipv6_udp_fd); + + m->llmnr_ipv4_tcp_event_source = sd_event_source_unref(m->llmnr_ipv4_tcp_event_source); + m->llmnr_ipv4_tcp_fd = safe_close(m->llmnr_ipv4_tcp_fd); + + m->llmnr_ipv6_tcp_event_source = sd_event_source_unref(m->llmnr_ipv6_tcp_event_source); + m->llmnr_ipv6_tcp_fd = safe_close(m->llmnr_ipv6_tcp_fd); +} + +static int manager_llmnr_start(Manager *m) { + int r; + + assert(m); + + if (m->llmnr_support == SUPPORT_NO) + return 0; + + r = manager_llmnr_ipv4_udp_fd(m); + if (r == -EADDRINUSE) + goto eaddrinuse; + if (r < 0) + return r; + + r = manager_llmnr_ipv4_tcp_fd(m); + if (r == -EADDRINUSE) + goto eaddrinuse; + if (r < 0) + return r; + + if (socket_ipv6_is_supported()) { + r = manager_llmnr_ipv6_udp_fd(m); + if (r == -EADDRINUSE) + goto eaddrinuse; + if (r < 0) + return r; + + r = manager_llmnr_ipv6_tcp_fd(m); + if (r == -EADDRINUSE) + goto eaddrinuse; + if (r < 0) + return r; + } + + return 0; + +eaddrinuse: + log_warning("There appears to be another LLMNR responder running. Turning off LLMNR support."); + m->llmnr_support = SUPPORT_NO; + manager_llmnr_stop(m); + + return 0; +} + int manager_new(Manager **ret) { _cleanup_(manager_freep) Manager *m = NULL; int r; @@ -444,46 +508,43 @@ int manager_new(Manager **ret) { if (r < 0) return r; - r = manager_llmnr_ipv4_udp_fd(m); - if (r < 0) - return r; - r = manager_llmnr_ipv6_udp_fd(m); - if (r < 0) - return r; - r = manager_llmnr_ipv4_tcp_fd(m); - if (r < 0) - return r; - r = manager_llmnr_ipv6_tcp_fd(m); - if (r < 0) - return r; - *ret = m; m = NULL; return 0; } +int manager_start(Manager *m) { + int r; + + assert(m); + + r = manager_llmnr_start(m); + if (r < 0) + return r; + + return 0; +} + Manager *manager_free(Manager *m) { Link *l; if (!m) return NULL; - while (m->dns_queries) - dns_query_free(m->dns_queries); - - hashmap_free(m->dns_transactions); - while ((l = hashmap_first(m->links))) link_free(l); - hashmap_free(m->links); + + while (m->dns_queries) + dns_query_free(m->dns_queries); dns_scope_free(m->unicast_scope); - while (m->dns_servers) - dns_server_free(m->dns_servers); - while (m->fallback_dns_servers) - dns_server_free(m->fallback_dns_servers); + manager_flush_dns_servers(m, DNS_SERVER_SYSTEM); + manager_flush_dns_servers(m, DNS_SERVER_FALLBACK); + + hashmap_free(m->links); + hashmap_free(m->dns_transactions); sd_event_source_unref(m->network_event_source); sd_network_monitor_unref(m->network_monitor); @@ -493,16 +554,9 @@ Manager *manager_free(Manager *m) { safe_close(m->dns_ipv4_fd); safe_close(m->dns_ipv6_fd); - sd_event_source_unref(m->llmnr_ipv4_udp_event_source); - sd_event_source_unref(m->llmnr_ipv6_udp_event_source); - safe_close(m->llmnr_ipv4_udp_fd); - safe_close(m->llmnr_ipv6_udp_fd); - - sd_event_source_unref(m->llmnr_ipv4_tcp_event_source); - sd_event_source_unref(m->llmnr_ipv6_tcp_event_source); - safe_close(m->llmnr_ipv4_tcp_fd); - safe_close(m->llmnr_ipv6_tcp_fd); + manager_llmnr_stop(m); + sd_bus_slot_unref(m->prepare_for_sleep_slot); sd_event_source_unref(m->bus_retry_event_source); sd_bus_unref(m->bus); @@ -624,7 +678,7 @@ clear: return r; } -static void write_resolve_conf_server(DnsServer *s, FILE *f, unsigned *count) { +static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) { _cleanup_free_ char *t = NULL; int r; @@ -639,17 +693,72 @@ static void write_resolve_conf_server(DnsServer *s, FILE *f, unsigned *count) { } if (*count == MAXNS) - fputs("# Too many DNS servers configured, the following entries may be ignored\n", f); + fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f); fprintf(f, "nameserver %s\n", t); (*count) ++; } +static void write_resolv_conf_search(const char *domain, FILE *f, + unsigned *count, unsigned *length) { + assert(domain); + assert(f); + assert(length); + + if (*count >= MAXDNSRCH || + *length + strlen(domain) > 256) { + if (*count == MAXDNSRCH) + fputs(" # Too many search domains configured, remaining ones ignored.", f); + if (*length <= 256) + fputs(" # Total length of all search domains is too long, remaining ones ignored.", f); + + return; + } + + fprintf(f, " %s", domain); + + (*length) += strlen(domain); + (*count) ++; +} + +static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) { + Iterator i; + + fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n" + "# Third party programs must not access this file directly, but\n" + "# only through the symlink at /etc/resolv.conf. To manage\n" + "# resolv.conf(5) in a different way, replace the symlink by a\n" + "# static file or a different symlink.\n\n", f); + + if (set_isempty(dns)) + fputs("# No DNS servers known.\n", f); + else { + DnsServer *s; + unsigned count = 0; + + SET_FOREACH(s, dns, i) + write_resolv_conf_server(s, f, &count); + } + + if (!set_isempty(domains)) { + unsigned length = 0, count = 0; + char *domain; + + fputs("search", f); + SET_FOREACH(domain, domains, i) + write_resolv_conf_search(domain, f, &count, &length); + fputs("\n", f); + } + + return fflush_and_check(f); +} + + int manager_write_resolv_conf(Manager *m) { static const char path[] = "/run/systemd/resolve/resolv.conf"; _cleanup_free_ char *temp_path = NULL; _cleanup_fclose_ FILE *f = NULL; - unsigned count = 0; + _cleanup_set_free_ Set *dns = NULL, *domains = NULL; DnsServer *s; Iterator i; Link *l; @@ -660,31 +769,66 @@ int manager_write_resolv_conf(Manager *m) { /* Read the system /etc/resolv.conf first */ manager_read_resolv_conf(m); - r = fopen_temporary(path, &f, &temp_path); - if (r < 0) - return r; + /* Add the full list to a set, to filter out duplicates */ + dns = set_new(&dns_server_hash_ops); + if (!dns) + return -ENOMEM; - fchmod(fileno(f), 0644); + domains = set_new(&dns_name_hash_ops); + if (!domains) + return -ENOMEM; - fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n" - "# Third party programs must not access this file directly, but\n" - "# only through the symlink at /etc/resolv.conf. To manage\n" - "# resolv.conf(5) in a different way, replace the symlink by a\n" - "# static file or a different symlink.\n\n", f); + /* First add the system-wide servers */ + LIST_FOREACH(servers, s, m->dns_servers) { + r = set_put(dns, s); + if (r == -EEXIST) + continue; + if (r < 0) + return r; + } - LIST_FOREACH(servers, s, m->dns_servers) - write_resolve_conf_server(s, f, &count); + /* Then, add the per-link servers and domains */ + HASHMAP_FOREACH(l, m->links, i) { + char **domain; - HASHMAP_FOREACH(l, m->links, i) - LIST_FOREACH(servers, s, l->dns_servers) - write_resolve_conf_server(s, f, &count); + LIST_FOREACH(servers, s, l->dns_servers) { + r = set_put(dns, s); + if (r == -EEXIST) + continue; + if (r < 0) + return r; + } - if (count == 0) { - LIST_FOREACH(servers, s, m->fallback_dns_servers) - write_resolve_conf_server(s, f, &count); + if (!l->unicast_scope) + continue; + + STRV_FOREACH(domain, l->unicast_scope->domains) { + r = set_put(domains, *domain); + if (r == -EEXIST) + continue; + if (r < 0) + return r; + } + } + + /* If we found nothing, add the fallback servers */ + if (set_isempty(dns)) { + LIST_FOREACH(servers, s, m->fallback_dns_servers) { + r = set_put(dns, s); + if (r == -EEXIST) + continue; + if (r < 0) + return r; + } } - r = fflush_and_check(f); + r = fopen_temporary_label(path, path, &f, &temp_path); + if (r < 0) + return r; + + fchmod(fileno(f), 0644); + + r = write_resolv_conf_contents(f, dns, domains); if (r < 0) goto fail; @@ -705,7 +849,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; union { struct cmsghdr header; /* For alignment */ - uint8_t buffer[CMSG_SPACE(MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo))) + uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) + CMSG_SPACE(int) /* ttl/hoplimit */ + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */]; } control; @@ -816,7 +960,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { * device if the packet came from the local host since it * avoids the routing table in such a case. Let's unset the * interface index in such a case. */ - if (p->ifindex > 0 && manager_ifindex_is_loopback(m, p->ifindex) != 0) + if (p->ifindex == LOOPBACK_IFINDEX) p->ifindex = 0; /* If we don't know the interface index still, we look for the @@ -1053,7 +1197,7 @@ int manager_send(Manager *m, int fd, int ifindex, int family, const union in_add return -EAFNOSUPPORT; } -bool manager_known_dns_server(Manager *m, int family, const union in_addr_union *in_addr) { +DnsServer* manager_find_dns_server(Manager *m, int family, const union in_addr_union *in_addr) { DnsServer *s; assert(m); @@ -1061,16 +1205,16 @@ bool manager_known_dns_server(Manager *m, int family, const union in_addr_union LIST_FOREACH(servers, s, m->dns_servers) if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0) - return true; + return s; LIST_FOREACH(servers, s, m->fallback_dns_servers) if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0) - return true; + return s; - return false; + return NULL; } -static DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) { +DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) { assert(m); if (m->current_dns_server == s) @@ -1081,10 +1225,13 @@ static DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) { in_addr_to_string(s->family, &s->address, &ip); log_info("Switching to system DNS server %s.", strna(ip)); - } else - log_info("No system DNS server set."); + } m->current_dns_server = s; + + if (m->unicast_scope) + dns_cache_flush(&m->unicast_scope->cache); + return s; } @@ -1165,38 +1312,34 @@ static int on_llmnr_packet(sd_event_source *s, int fd, uint32_t revents, void *u _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; DnsTransaction *t = NULL; Manager *m = userdata; + DnsScope *scope; int r; r = manager_recv(m, fd, DNS_PROTOCOL_LLMNR, &p); if (r <= 0) return r; + scope = manager_find_scope(m, p); + if (!scope) { + log_warning("Got LLMNR UDP packet on unknown scope. Ignoring."); + return 0; + } + if (dns_packet_validate_reply(p) > 0) { log_debug("Got reply packet for id %u", DNS_PACKET_ID(p)); - t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); - if (!t) - return 0; - - dns_transaction_process_reply(t, p); + dns_scope_check_conflicts(scope, p); - } else if (dns_packet_validate_query(p) > 0) { - Link *l; - - l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); - if (l) { - DnsScope *scope = NULL; + t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); + if (t) + dns_transaction_process_reply(t, p); - if (p->family == AF_INET) - scope = l->llmnr_ipv4_scope; - else if (p->family == AF_INET6) - scope = l->llmnr_ipv6_scope; + } else if (dns_packet_validate_query(p) > 0) { + log_debug("Got query packet for id %u", DNS_PACKET_ID(p)); - if (scope) - dns_scope_process_query(scope, NULL, p); - } + dns_scope_process_query(scope, NULL, p); } else - log_debug("Invalid LLMNR packet."); + log_debug("Invalid LLMNR UDP packet."); return 0; } @@ -1359,29 +1502,26 @@ fail: } static int on_llmnr_stream_packet(DnsStream *s) { - assert(s); + DnsScope *scope; - if (dns_packet_validate_query(s->read_packet) > 0) { - Link *l; + assert(s); - l = hashmap_get(s->manager->links, INT_TO_PTR(s->read_packet->ifindex)); - if (l) { - DnsScope *scope = NULL; + scope = manager_find_scope(s->manager, s->read_packet); + if (!scope) { + log_warning("Got LLMNR TCP packet on unknown scope. Ignroing."); + return 0; + } - if (s->read_packet->family == AF_INET) - scope = l->llmnr_ipv4_scope; - else if (s->read_packet->family == AF_INET6) - scope = l->llmnr_ipv6_scope; + if (dns_packet_validate_query(s->read_packet) > 0) { + log_debug("Got query packet for id %u", DNS_PACKET_ID(s->read_packet)); - if (scope) { - dns_scope_process_query(scope, s, s->read_packet); + dns_scope_process_query(scope, s, s->read_packet); - /* If no reply packet was set, we free the stream */ - if (s->write_packet) - return 0; - } - } - } + /* If no reply packet was set, we free the stream */ + if (s->write_packet) + return 0; + } else + log_debug("Invalid LLMNR TCP packet."); dns_stream_free(s); return 0; @@ -1555,20 +1695,6 @@ fail: return r; } -int manager_ifindex_is_loopback(Manager *m, int ifindex) { - Link *l; - assert(m); - - if (ifindex <= 0) - return -EINVAL; - - l = hashmap_get(m->links, INT_TO_PTR(ifindex)); - if (l->flags & IFF_LOOPBACK) - return 1; - - return 0; -} - int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { LinkAddress *a; @@ -1598,7 +1724,7 @@ void manager_refresh_rrs(Manager *m) { int manager_next_hostname(Manager *m) { const char *p; - uint64_t u; + uint64_t u, a; char *h; assert(m); @@ -1616,7 +1742,15 @@ int manager_next_hostname(Manager *m) { if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) u = 1; - u++; + /* Add a random number to the old value. This way we can avoid + * that two hosts pick the same hostname, win on IPv4 and lose + * on IPv6 (or vice versa), and pick the same hostname + * replacement hostname, ad infinitum. We still want the + * numbers to go up monotonically, hence we just add a random + * value 1..10 */ + + random_bytes(&a, sizeof(a)); + u += 1 + a % 10; if (asprintf(&h, "%.*s%" PRIu64, (int) (p - m->hostname), m->hostname, u) < 0) return -ENOMEM; @@ -1648,13 +1782,54 @@ LinkAddress* manager_find_link_address(Manager *m, int family, const union in_ad return NULL; } -int manager_our_packet(Manager *m, DnsPacket *p) { +bool manager_our_packet(Manager *m, DnsPacket *p) { assert(m); assert(p); return !!manager_find_link_address(m, p->family, &p->sender); } +DnsScope* manager_find_scope(Manager *m, DnsPacket *p) { + Link *l; + + assert(m); + assert(p); + + l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); + if (!l) + return NULL; + + if (p->protocol == DNS_PROTOCOL_LLMNR) { + if (p->family == AF_INET) + return l->llmnr_ipv4_scope; + else if (p->family == AF_INET6) + return l->llmnr_ipv6_scope; + } + + return NULL; +} + +void manager_verify_all(Manager *m) { + DnsScope *s; + + assert(m); + + LIST_FOREACH(scopes, s, m->dns_scopes) + dns_zone_verify_all(&s->zone); +} + +void manager_flush_dns_servers(Manager *m, DnsServerType t) { + assert(m); + + if (t == DNS_SERVER_SYSTEM) + while (m->dns_servers) + dns_server_free(m->dns_servers); + + if (t == DNS_SERVER_FALLBACK) + while (m->fallback_dns_servers) + dns_server_free(m->fallback_dns_servers); +} + static const char* const support_table[_SUPPORT_MAX] = { [SUPPORT_NO] = "no", [SUPPORT_YES] = "yes",