1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/tcp.h>
6 #include "alloc-util.h"
7 #include "dns-domain.h"
8 #include "errno-util.h"
10 #include "hostname-util.h"
11 #include "missing_network.h"
12 #include "random-util.h"
13 #include "resolved-dnssd.h"
14 #include "resolved-dns-scope.h"
15 #include "resolved-dns-zone.h"
16 #include "resolved-llmnr.h"
17 #include "resolved-mdns.h"
18 #include "socket-util.h"
21 #define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
22 #define MULTICAST_RATELIMIT_BURST 1000
24 /* After how much time to repeat LLMNR requests, see RFC 4795 Section 7 */
25 #define MULTICAST_RESEND_TIMEOUT_MIN_USEC (100 * USEC_PER_MSEC)
26 #define MULTICAST_RESEND_TIMEOUT_MAX_USEC (1 * USEC_PER_SEC)
28 int dns_scope_new(Manager
*m
, DnsScope
**ret
, Link
*l
, DnsProtocol protocol
, int family
) {
43 .resend_timeout
= MULTICAST_RESEND_TIMEOUT_MIN_USEC
,
46 if (protocol
== DNS_PROTOCOL_DNS
) {
47 /* Copy DNSSEC mode from the link if it is set there,
48 * otherwise take the manager's DNSSEC mode. Note that
49 * we copy this only at scope creation time, and do
50 * not update it from the on, even if the setting
54 s
->dnssec_mode
= link_get_dnssec_mode(l
);
55 s
->dns_over_tls_mode
= link_get_dns_over_tls_mode(l
);
57 s
->dnssec_mode
= manager_get_dnssec_mode(m
);
58 s
->dns_over_tls_mode
= manager_get_dns_over_tls_mode(m
);
62 s
->dnssec_mode
= DNSSEC_NO
;
63 s
->dns_over_tls_mode
= DNS_OVER_TLS_NO
;
66 LIST_PREPEND(scopes
, m
->dns_scopes
, s
);
68 dns_scope_llmnr_membership(s
, true);
69 dns_scope_mdns_membership(s
, true);
71 log_debug("New scope on link %s, protocol %s, family %s", l
? l
->ifname
: "*", dns_protocol_to_string(protocol
), family
== AF_UNSPEC
? "*" : af_to_name(family
));
73 /* Enforce ratelimiting for the multicast protocols */
74 s
->ratelimit
= (const RateLimit
) { MULTICAST_RATELIMIT_INTERVAL_USEC
, MULTICAST_RATELIMIT_BURST
};
80 static void dns_scope_abort_transactions(DnsScope
*s
) {
83 while (s
->transactions
) {
84 DnsTransaction
*t
= s
->transactions
;
86 /* Abort the transaction, but make sure it is not
87 * freed while we still look at it */
90 if (DNS_TRANSACTION_IS_LIVE(t
->state
))
91 dns_transaction_complete(t
, DNS_TRANSACTION_ABORTED
);
94 dns_transaction_free(t
);
98 DnsScope
* dns_scope_free(DnsScope
*s
) {
102 log_debug("Removing scope on link %s, protocol %s, family %s", s
->link
? s
->link
->ifname
: "*", dns_protocol_to_string(s
->protocol
), s
->family
== AF_UNSPEC
? "*" : af_to_name(s
->family
));
104 dns_scope_llmnr_membership(s
, false);
105 dns_scope_mdns_membership(s
, false);
106 dns_scope_abort_transactions(s
);
108 while (s
->query_candidates
)
109 dns_query_candidate_unref(s
->query_candidates
);
111 hashmap_free(s
->transactions_by_key
);
113 ordered_hashmap_free_with_destructor(s
->conflict_queue
, dns_resource_record_unref
);
114 sd_event_source_disable_unref(s
->conflict_event_source
);
116 sd_event_source_disable_unref(s
->announce_event_source
);
118 dns_cache_flush(&s
->cache
);
119 dns_zone_flush(&s
->zone
);
121 LIST_REMOVE(scopes
, s
->manager
->dns_scopes
, s
);
125 DnsServer
*dns_scope_get_dns_server(DnsScope
*s
) {
128 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
132 return link_get_dns_server(s
->link
);
134 return manager_get_dns_server(s
->manager
);
137 unsigned dns_scope_get_n_dns_servers(DnsScope
*s
) {
143 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
147 i
= s
->link
->dns_servers
;
149 i
= s
->manager
->dns_servers
;
151 for (; i
; i
= i
->servers_next
)
157 void dns_scope_next_dns_server(DnsScope
*s
, DnsServer
*if_current
) {
160 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
163 /* Changes to the next DNS server in the list. If 'if_current' is passed will do so only if the
164 * current DNS server still matches it. */
167 link_next_dns_server(s
->link
, if_current
);
169 manager_next_dns_server(s
->manager
, if_current
);
172 void dns_scope_packet_received(DnsScope
*s
, usec_t rtt
) {
175 if (rtt
<= s
->max_rtt
)
179 s
->resend_timeout
= MIN(MAX(MULTICAST_RESEND_TIMEOUT_MIN_USEC
, s
->max_rtt
* 2), MULTICAST_RESEND_TIMEOUT_MAX_USEC
);
182 void dns_scope_packet_lost(DnsScope
*s
, usec_t usec
) {
185 if (s
->resend_timeout
<= usec
)
186 s
->resend_timeout
= MIN(s
->resend_timeout
* 2, MULTICAST_RESEND_TIMEOUT_MAX_USEC
);
189 static int dns_scope_emit_one(DnsScope
*s
, int fd
, int family
, DnsPacket
*p
) {
194 assert(p
->protocol
== s
->protocol
);
196 if (family
== AF_UNSPEC
) {
197 if (s
->family
== AF_UNSPEC
)
198 return -EAFNOSUPPORT
;
203 switch (s
->protocol
) {
205 case DNS_PROTOCOL_DNS
: {
206 size_t mtu
, udp_size
, min_mtu
, socket_mtu
= 0;
210 if (DNS_PACKET_QDCOUNT(p
) > 1) /* Classic DNS only allows one question per packet */
213 if (p
->size
> DNS_PACKET_UNICAST_SIZE_MAX
)
216 /* Determine the local most accurate MTU */
220 mtu
= manager_find_mtu(s
->manager
);
222 /* Acquire the socket's PMDU MTU */
223 r
= socket_get_mtu(fd
, family
, &socket_mtu
);
224 if (r
< 0 && !ERRNO_IS_DISCONNECT(r
)) /* Will return ENOTCONN if no information is available yet */
225 return log_debug_errno(r
, "Failed to read socket MTU: %m");
227 /* Determine the appropriate UDP header size */
228 udp_size
= udp_header_size(family
);
229 min_mtu
= udp_size
+ DNS_PACKET_HEADER_SIZE
;
231 log_debug("Emitting UDP, link MTU is %zu, socket MTU is %zu, minimal MTU is %zu",
232 mtu
, socket_mtu
, min_mtu
);
234 /* Clamp by the kernel's idea of the (path) MTU */
235 if (socket_mtu
!= 0 && socket_mtu
< mtu
)
238 /* Put a lower limit, in case all MTU data we acquired was rubbish */
242 /* Now check our packet size against the MTU we determined */
243 if (udp_size
+ p
->size
> mtu
)
244 return -EMSGSIZE
; /* This means: try TCP instead */
246 r
= manager_write(s
->manager
, fd
, p
);
253 case DNS_PROTOCOL_LLMNR
: {
254 union in_addr_union addr
;
258 if (DNS_PACKET_QDCOUNT(p
) > 1)
261 if (!ratelimit_below(&s
->ratelimit
))
264 if (family
== AF_INET
) {
265 addr
.in
= LLMNR_MULTICAST_IPV4_ADDRESS
;
266 fd
= manager_llmnr_ipv4_udp_fd(s
->manager
);
267 } else if (family
== AF_INET6
) {
268 addr
.in6
= LLMNR_MULTICAST_IPV6_ADDRESS
;
269 fd
= manager_llmnr_ipv6_udp_fd(s
->manager
);
271 return -EAFNOSUPPORT
;
275 r
= manager_send(s
->manager
, fd
, s
->link
->ifindex
, family
, &addr
, LLMNR_PORT
, NULL
, p
);
282 case DNS_PROTOCOL_MDNS
: {
283 union in_addr_union addr
;
286 if (!ratelimit_below(&s
->ratelimit
))
289 if (family
== AF_INET
) {
290 if (in4_addr_is_null(&p
->destination
.in
))
291 addr
.in
= MDNS_MULTICAST_IPV4_ADDRESS
;
293 addr
= p
->destination
;
294 fd
= manager_mdns_ipv4_fd(s
->manager
);
295 } else if (family
== AF_INET6
) {
296 if (in6_addr_is_null(&p
->destination
.in6
))
297 addr
.in6
= MDNS_MULTICAST_IPV6_ADDRESS
;
299 addr
= p
->destination
;
300 fd
= manager_mdns_ipv6_fd(s
->manager
);
302 return -EAFNOSUPPORT
;
306 r
= manager_send(s
->manager
, fd
, s
->link
->ifindex
, family
, &addr
, p
->destination_port
?: MDNS_PORT
, NULL
, p
);
314 return -EAFNOSUPPORT
;
320 int dns_scope_emit_udp(DnsScope
*s
, int fd
, int af
, DnsPacket
*p
) {
325 assert(p
->protocol
== s
->protocol
);
326 assert((s
->protocol
== DNS_PROTOCOL_DNS
) == (fd
>= 0));
329 /* If there are multiple linked packets, set the TC bit in all but the last of them */
331 assert(p
->protocol
== DNS_PROTOCOL_MDNS
);
332 dns_packet_set_flags(p
, true, true);
335 r
= dns_scope_emit_one(s
, fd
, af
, p
);
345 static int dns_scope_socket(
349 const union in_addr_union
*address
,
352 union sockaddr_union
*ret_socket_address
) {
354 _cleanup_close_
int fd
= -EBADF
;
355 union sockaddr_union sa
;
362 assert(family
== AF_UNSPEC
);
365 ifindex
= dns_server_ifindex(server
);
367 switch (server
->family
) {
369 sa
= (union sockaddr_union
) {
370 .in
.sin_family
= server
->family
,
371 .in
.sin_port
= htobe16(port
),
372 .in
.sin_addr
= server
->address
.in
,
374 salen
= sizeof(sa
.in
);
377 sa
= (union sockaddr_union
) {
378 .in6
.sin6_family
= server
->family
,
379 .in6
.sin6_port
= htobe16(port
),
380 .in6
.sin6_addr
= server
->address
.in6
,
381 .in6
.sin6_scope_id
= ifindex
,
383 salen
= sizeof(sa
.in6
);
386 return -EAFNOSUPPORT
;
389 assert(family
!= AF_UNSPEC
);
392 ifindex
= s
->link
? s
->link
->ifindex
: 0;
396 sa
= (union sockaddr_union
) {
397 .in
.sin_family
= family
,
398 .in
.sin_port
= htobe16(port
),
399 .in
.sin_addr
= address
->in
,
401 salen
= sizeof(sa
.in
);
404 sa
= (union sockaddr_union
) {
405 .in6
.sin6_family
= family
,
406 .in6
.sin6_port
= htobe16(port
),
407 .in6
.sin6_addr
= address
->in6
,
408 .in6
.sin6_scope_id
= ifindex
,
410 salen
= sizeof(sa
.in6
);
413 return -EAFNOSUPPORT
;
417 fd
= socket(sa
.sa
.sa_family
, type
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
421 if (type
== SOCK_STREAM
) {
422 r
= setsockopt_int(fd
, IPPROTO_TCP
, TCP_NODELAY
, true);
428 r
= socket_set_unicast_if(fd
, sa
.sa
.sa_family
, ifindex
);
433 if (s
->protocol
== DNS_PROTOCOL_LLMNR
) {
434 /* RFC 4795, section 2.5 requires the TTL to be set to 1 */
435 r
= socket_set_ttl(fd
, sa
.sa
.sa_family
, 1);
440 if (type
== SOCK_DGRAM
) {
441 /* Set IP_RECVERR or IPV6_RECVERR to get ICMP error feedback. See discussion in #10345. */
442 r
= socket_set_recverr(fd
, sa
.sa
.sa_family
, true);
446 r
= socket_set_recvpktinfo(fd
, sa
.sa
.sa_family
, true);
450 /* Turn of path MTU discovery for security reasons */
451 r
= socket_disable_pmtud(fd
, sa
.sa
.sa_family
);
453 log_debug_errno(r
, "Failed to disable UDP PMTUD, ignoring: %m");
455 /* Learn about fragmentation taking place */
456 r
= socket_set_recvfragsize(fd
, sa
.sa
.sa_family
, true);
458 log_debug_errno(r
, "Failed to enable fragment size reception, ignoring: %m");
461 if (ret_socket_address
)
462 *ret_socket_address
= sa
;
466 /* Let's temporarily bind the socket to the specified ifindex. The kernel currently takes
467 * only the SO_BINDTODEVICE/SO_BINDTOINDEX ifindex into account when making routing decisions
468 * in connect() — and not IP_UNICAST_IF. We don't really want any of the other semantics of
469 * SO_BINDTODEVICE/SO_BINDTOINDEX, hence we immediately unbind the socket after the fact
472 * As a special exception we don't do this if we notice that the specified IP address is on
473 * the local host. SO_BINDTODEVICE in combination with destination addresses on the local
474 * host result in EHOSTUNREACH, since Linux won't send the packets out of the specified
475 * interface, but delivers them directly to the local socket. */
477 !manager_find_link_address(s
->manager
, sa
.sa
.sa_family
, sockaddr_in_addr(&sa
.sa
)) &&
478 in_addr_is_localhost(sa
.sa
.sa_family
, sockaddr_in_addr(&sa
.sa
)) == 0) {
479 r
= socket_bind_to_ifindex(fd
, ifindex
);
486 r
= connect(fd
, &sa
.sa
, salen
);
487 if (r
< 0 && errno
!= EINPROGRESS
)
491 r
= socket_bind_to_ifindex(fd
, 0);
500 int dns_scope_socket_udp(DnsScope
*s
, DnsServer
*server
) {
501 return dns_scope_socket(s
, SOCK_DGRAM
, AF_UNSPEC
, NULL
, server
, dns_server_port(server
), NULL
);
504 int dns_scope_socket_tcp(DnsScope
*s
, int family
, const union in_addr_union
*address
, DnsServer
*server
, uint16_t port
, union sockaddr_union
*ret_socket_address
) {
505 /* If ret_socket_address is not NULL, the caller is responsible
506 * for calling connect() or sendmsg(). This is required by TCP
507 * Fast Open, to be able to send the initial SYN packet along
508 * with the first data packet. */
509 return dns_scope_socket(s
, SOCK_STREAM
, family
, address
, server
, port
, ret_socket_address
);
512 static DnsScopeMatch
match_link_local_reverse_lookups(const char *domain
) {
515 if (dns_name_endswith(domain
, "254.169.in-addr.arpa") > 0)
516 return DNS_SCOPE_YES_BASE
+ 4; /* 4 labels match */
518 if (dns_name_endswith(domain
, "8.e.f.ip6.arpa") > 0 ||
519 dns_name_endswith(domain
, "9.e.f.ip6.arpa") > 0 ||
520 dns_name_endswith(domain
, "a.e.f.ip6.arpa") > 0 ||
521 dns_name_endswith(domain
, "b.e.f.ip6.arpa") > 0)
522 return DNS_SCOPE_YES_BASE
+ 5; /* 5 labels match */
524 return _DNS_SCOPE_MATCH_INVALID
;
527 static DnsScopeMatch
match_subnet_reverse_lookups(
532 union in_addr_union ia
;
538 /* Checks whether the specified domain is a reverse address domain (i.e. in the .in-addr.arpa or
539 * .ip6.arpa area), and if so, whether the address matches any of the local subnets of the link the
540 * scope is associated with. If so, our scope should consider itself relevant for any lookup in the
541 * domain, since it apparently refers to hosts on this link's subnet.
543 * If 'exclude_own' is true this will return DNS_SCOPE_NO for any IP addresses assigned locally. This
544 * is useful for LLMNR/mDNS as we never want to look up our own hostname on LLMNR/mDNS but always use
545 * the locally synthesized one. */
548 return _DNS_SCOPE_MATCH_INVALID
; /* No link, hence no local addresses to check */
550 r
= dns_name_address(domain
, &f
, &ia
);
552 log_debug_errno(r
, "Failed to determine whether '%s' is an address domain: %m", domain
);
554 return _DNS_SCOPE_MATCH_INVALID
;
556 if (s
->family
!= AF_UNSPEC
&& f
!= s
->family
)
557 return _DNS_SCOPE_MATCH_INVALID
; /* Don't look for IPv4 addresses on LLMNR/mDNS over IPv6 and vice versa */
559 LIST_FOREACH(addresses
, a
, s
->link
->addresses
) {
564 /* Equals our own address? nah, let's not use this scope. The local synthesizer will pick it up for us. */
566 in_addr_equal(f
, &a
->in_addr
, &ia
) > 0)
569 if (a
->prefixlen
== UCHAR_MAX
) /* don't know subnet mask */
572 /* Check if the address is in the local subnet */
573 r
= in_addr_prefix_covers(f
, &a
->in_addr
, a
->prefixlen
, &ia
);
575 log_debug_errno(r
, "Failed to determine whether link address covers lookup address '%s': %m", domain
);
577 /* Note that we only claim zero labels match. This is so that this is at the same
578 * priority a DNS scope with "." as routing domain is. */
579 return DNS_SCOPE_YES_BASE
+ 0;
582 return _DNS_SCOPE_MATCH_INVALID
;
585 DnsScopeMatch
dns_scope_good_domain(
589 DnsQuestion
*question
;
594 /* This returns the following return values:
596 * DNS_SCOPE_NO → This scope is not suitable for lookups of this domain, at all
597 * DNS_SCOPE_MAYBE → This scope is suitable, but only if nothing else wants it
598 * DNS_SCOPE_YES_BASE+n → This scope is suitable, and 'n' suffix labels match
600 * (The idea is that the caller will only use the scopes with the longest 'n' returned. If no scopes return
601 * DNS_SCOPE_YES_BASE+n, then it should use those which returned DNS_SCOPE_MAYBE. It should never use those
602 * which returned DNS_SCOPE_NO.)
608 question
= dns_query_question_for_protocol(q
, s
->protocol
);
612 domain
= dns_question_first_name(question
);
616 ifindex
= q
->ifindex
;
619 /* Checks if the specified domain is something to look up on this scope. Note that this accepts
620 * non-qualified hostnames, i.e. those without any search path suffixed. */
622 if (ifindex
!= 0 && (!s
->link
|| s
->link
->ifindex
!= ifindex
))
625 if ((SD_RESOLVED_FLAGS_MAKE(s
->protocol
, s
->family
, false, false) & flags
) == 0)
628 /* Never resolve any loopback hostname or IP address via DNS, LLMNR or mDNS. Instead, always rely on
629 * synthesized RRs for these. */
630 if (is_localhost(domain
) ||
631 dns_name_endswith(domain
, "127.in-addr.arpa") > 0 ||
632 dns_name_equal(domain
, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") > 0)
635 /* Never respond to some of the domains listed in RFC6303 + RFC6761 */
636 if (dns_name_dont_resolve(domain
))
639 /* Never go to network for the _gateway, _outbound, _localdnsstub, _localdnsproxy domain — they're something special, synthesized locally. */
640 if (is_gateway_hostname(domain
) ||
641 is_outbound_hostname(domain
) ||
642 is_dns_stub_hostname(domain
) ||
643 is_dns_proxy_stub_hostname(domain
))
646 switch (s
->protocol
) {
648 case DNS_PROTOCOL_DNS
: {
649 bool has_search_domains
= false;
653 if (dns_name_is_root(domain
)) {
657 /* Refuse root name if only A and/or AAAA records are requested. */
659 DNS_QUESTION_FOREACH(t
, question
)
660 if (!IN_SET(t
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
)) {
669 /* Never route things to scopes that lack DNS servers */
670 if (!dns_scope_get_dns_server(s
))
673 /* Always honour search domains for routing queries, except if this scope lacks DNS servers. Note that
674 * we return DNS_SCOPE_YES here, rather than just DNS_SCOPE_MAYBE, which means other wildcard scopes
675 * won't be considered anymore. */
676 LIST_FOREACH(domains
, d
, dns_scope_get_search_domains(s
)) {
678 if (!d
->route_only
&& !dns_name_is_root(d
->name
))
679 has_search_domains
= true;
681 if (dns_name_endswith(domain
, d
->name
) > 0) {
684 c
= dns_name_count_labels(d
->name
);
693 /* If there's a true search domain defined for this scope, and the query is single-label,
694 * then let's resolve things here, preferably. Note that LLMNR considers itself
695 * authoritative for single-label names too, at the same preference, see below. */
696 if (has_search_domains
&& dns_name_is_single_label(domain
))
697 return DNS_SCOPE_YES_BASE
+ 1;
699 /* If ResolveUnicastSingleLabel=yes and the query is single-label, then bump match result
700 to prevent LLMNR monopoly among candidates. */
701 if (s
->manager
->resolve_unicast_single_label
&& dns_name_is_single_label(domain
))
702 return DNS_SCOPE_YES_BASE
+ 1;
704 /* Let's return the number of labels in the best matching result */
706 assert(n_best
<= DNS_SCOPE_YES_END
- DNS_SCOPE_YES_BASE
);
707 return DNS_SCOPE_YES_BASE
+ n_best
;
710 /* Exclude link-local IP ranges */
711 if (match_link_local_reverse_lookups(domain
) >= DNS_SCOPE_YES_BASE
||
712 /* If networks use .local in their private setups, they are supposed to also add .local
713 * to their search domains, which we already checked above. Otherwise, we consider .local
714 * specific to mDNS and won't send such queries ordinary DNS servers. */
715 dns_name_endswith(domain
, "local") > 0)
718 /* If the IP address to look up matches the local subnet, then implicitly synthesizes
719 * DNS_SCOPE_YES_BASE + 0 on this interface, i.e. preferably resolve IP addresses via the DNS
720 * server belonging to this interface. */
721 m
= match_subnet_reverse_lookups(s
, domain
, false);
725 /* If there was no match at all, then see if this scope is suitable as default route. */
726 if (!dns_scope_is_default_route(s
))
729 return DNS_SCOPE_MAYBE
;
732 case DNS_PROTOCOL_MDNS
: {
735 m
= match_link_local_reverse_lookups(domain
);
739 m
= match_subnet_reverse_lookups(s
, domain
, true);
743 if ((s
->family
== AF_INET
&& dns_name_endswith(domain
, "in-addr.arpa") > 0) ||
744 (s
->family
== AF_INET6
&& dns_name_endswith(domain
, "ip6.arpa") > 0))
745 return DNS_SCOPE_MAYBE
;
747 if ((dns_name_endswith(domain
, "local") > 0 && /* only resolve names ending in .local via mDNS */
748 dns_name_equal(domain
, "local") == 0 && /* but not the single-label "local" name itself */
749 manager_is_own_hostname(s
->manager
, domain
) <= 0)) /* never resolve the local hostname via mDNS */
750 return DNS_SCOPE_YES_BASE
+ 1; /* Return +1, as the top-level .local domain matches, i.e. one label */
755 case DNS_PROTOCOL_LLMNR
: {
758 m
= match_link_local_reverse_lookups(domain
);
762 m
= match_subnet_reverse_lookups(s
, domain
, true);
766 if ((s
->family
== AF_INET
&& dns_name_endswith(domain
, "in-addr.arpa") > 0) ||
767 (s
->family
== AF_INET6
&& dns_name_endswith(domain
, "ip6.arpa") > 0))
768 return DNS_SCOPE_MAYBE
;
770 if ((dns_name_is_single_label(domain
) && /* only resolve single label names via LLMNR */
771 dns_name_equal(domain
, "local") == 0 && /* don't resolve "local" with LLMNR, it's the top-level domain of mDNS after all, see above */
772 manager_is_own_hostname(s
->manager
, domain
) <= 0)) /* never resolve the local hostname via LLMNR */
773 return DNS_SCOPE_YES_BASE
+ 1; /* Return +1, as we consider ourselves authoritative
774 * for single-label names, i.e. one label. This is
775 * particularly relevant as it means a "." route on some
776 * other scope won't pull all traffic away from
777 * us. (If people actually want to pull traffic away
778 * from us they should turn off LLMNR on the
779 * link). Note that unicast DNS scopes with search
780 * domains also consider themselves authoritative for
781 * single-label domains, at the same preference (see
788 assert_not_reached();
792 bool dns_scope_good_key(DnsScope
*s
, const DnsResourceKey
*key
) {
798 /* Check if it makes sense to resolve the specified key on this scope. Note that this call assumes a
799 * fully qualified name, i.e. the search suffixes already appended. */
801 if (!IN_SET(key
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
804 if (s
->protocol
== DNS_PROTOCOL_DNS
) {
806 /* On classic DNS, looking up non-address RRs is always fine. (Specifically, we want to
807 * permit looking up DNSKEY and DS records on the root and top-level domains.) */
808 if (!dns_resource_key_is_address(key
))
811 /* Unless explicitly overridden, we refuse to look up A and AAAA RRs on the root and
812 * single-label domains, under the assumption that those should be resolved via LLMNR or
813 * search path only, and should not be leaked onto the internet. */
814 const char* name
= dns_resource_key_name(key
);
816 if (!s
->manager
->resolve_unicast_single_label
&&
817 dns_name_is_single_label(name
))
820 return !dns_name_is_root(name
);
823 /* Never route DNSSEC RR queries to LLMNR/mDNS scopes */
824 if (dns_type_is_dnssec(key
->type
))
827 /* On mDNS and LLMNR, send A and AAAA queries only on the respective scopes */
829 key_family
= dns_type_to_af(key
->type
);
833 return key_family
== s
->family
;
836 static int dns_scope_multicast_membership(DnsScope
*s
, bool b
, struct in_addr in
, struct in6_addr in6
) {
842 if (s
->family
== AF_INET
) {
843 struct ip_mreqn mreqn
= {
845 .imr_ifindex
= s
->link
->ifindex
,
848 if (s
->protocol
== DNS_PROTOCOL_LLMNR
)
849 fd
= manager_llmnr_ipv4_udp_fd(s
->manager
);
851 fd
= manager_mdns_ipv4_fd(s
->manager
);
856 /* Always first try to drop membership before we add
857 * one. This is necessary on some devices, such as
860 (void) setsockopt(fd
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, &mreqn
, sizeof(mreqn
));
862 if (setsockopt(fd
, IPPROTO_IP
, b
? IP_ADD_MEMBERSHIP
: IP_DROP_MEMBERSHIP
, &mreqn
, sizeof(mreqn
)) < 0)
865 } else if (s
->family
== AF_INET6
) {
866 struct ipv6_mreq mreq
= {
867 .ipv6mr_multiaddr
= in6
,
868 .ipv6mr_interface
= s
->link
->ifindex
,
871 if (s
->protocol
== DNS_PROTOCOL_LLMNR
)
872 fd
= manager_llmnr_ipv6_udp_fd(s
->manager
);
874 fd
= manager_mdns_ipv6_fd(s
->manager
);
880 (void) setsockopt(fd
, IPPROTO_IPV6
, IPV6_DROP_MEMBERSHIP
, &mreq
, sizeof(mreq
));
882 if (setsockopt(fd
, IPPROTO_IPV6
, b
? IPV6_ADD_MEMBERSHIP
: IPV6_DROP_MEMBERSHIP
, &mreq
, sizeof(mreq
)) < 0)
885 return -EAFNOSUPPORT
;
890 int dns_scope_llmnr_membership(DnsScope
*s
, bool b
) {
893 if (s
->protocol
!= DNS_PROTOCOL_LLMNR
)
896 return dns_scope_multicast_membership(s
, b
, LLMNR_MULTICAST_IPV4_ADDRESS
, LLMNR_MULTICAST_IPV6_ADDRESS
);
899 int dns_scope_mdns_membership(DnsScope
*s
, bool b
) {
902 if (s
->protocol
!= DNS_PROTOCOL_MDNS
)
905 return dns_scope_multicast_membership(s
, b
, MDNS_MULTICAST_IPV4_ADDRESS
, MDNS_MULTICAST_IPV6_ADDRESS
);
908 int dns_scope_make_reply_packet(
918 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
919 unsigned n_answer
= 0, n_soa
= 0;
926 if (dns_question_isempty(q
) &&
927 dns_answer_isempty(answer
) &&
928 dns_answer_isempty(soa
))
931 r
= dns_packet_new(&p
, s
->protocol
, 0, DNS_PACKET_SIZE_MAX
);
935 /* mDNS answers must have the Authoritative Answer bit set, see RFC 6762, section 18.4. */
936 c_or_aa
= s
->protocol
== DNS_PROTOCOL_MDNS
;
938 DNS_PACKET_HEADER(p
)->id
= id
;
939 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
950 r
= dns_packet_append_question(p
, q
);
953 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(dns_question_size(q
));
955 r
= dns_packet_append_answer(p
, answer
, &n_answer
);
958 DNS_PACKET_HEADER(p
)->ancount
= htobe16(n_answer
);
960 r
= dns_packet_append_answer(p
, soa
, &n_soa
);
963 DNS_PACKET_HEADER(p
)->arcount
= htobe16(n_soa
);
970 static void dns_scope_verify_conflicts(DnsScope
*s
, DnsPacket
*p
) {
971 DnsResourceRecord
*rr
;
977 DNS_QUESTION_FOREACH(key
, p
->question
)
978 dns_zone_verify_conflicts(&s
->zone
, key
);
980 DNS_ANSWER_FOREACH(rr
, p
->answer
)
981 dns_zone_verify_conflicts(&s
->zone
, rr
->key
);
984 void dns_scope_process_query(DnsScope
*s
, DnsStream
*stream
, DnsPacket
*p
) {
985 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
, *soa
= NULL
;
986 _cleanup_(dns_packet_unrefp
) DnsPacket
*reply
= NULL
;
987 DnsResourceKey
*key
= NULL
;
988 bool tentative
= false;
994 if (p
->protocol
!= DNS_PROTOCOL_LLMNR
)
997 if (p
->ipproto
== IPPROTO_UDP
) {
998 /* Don't accept UDP queries directed to anything but
999 * the LLMNR multicast addresses. See RFC 4795,
1002 if (p
->family
== AF_INET
&& !in4_addr_equal(&p
->destination
.in
, &LLMNR_MULTICAST_IPV4_ADDRESS
))
1005 if (p
->family
== AF_INET6
&& !in6_addr_equal(&p
->destination
.in6
, &LLMNR_MULTICAST_IPV6_ADDRESS
))
1009 r
= dns_packet_extract(p
);
1011 log_debug_errno(r
, "Failed to extract resource records from incoming packet: %m");
1015 if (DNS_PACKET_LLMNR_C(p
)) {
1016 /* Somebody notified us about a possible conflict */
1017 dns_scope_verify_conflicts(s
, p
);
1021 if (dns_question_size(p
->question
) != 1)
1022 return (void) log_debug("Received LLMNR query without question or multiple questions, ignoring.");
1024 key
= dns_question_first_key(p
->question
);
1026 r
= dns_zone_lookup(&s
->zone
, key
, 0, &answer
, &soa
, &tentative
);
1028 log_debug_errno(r
, "Failed to look up key: %m");
1035 dns_answer_order_by_scope(answer
, in_addr_is_link_local(p
->family
, &p
->sender
) > 0);
1037 r
= dns_scope_make_reply_packet(s
, DNS_PACKET_ID(p
), DNS_RCODE_SUCCESS
, p
->question
, answer
, soa
, tentative
, &reply
);
1039 log_debug_errno(r
, "Failed to build reply packet: %m");
1044 r
= dns_stream_write_packet(stream
, reply
);
1046 log_debug_errno(r
, "Failed to enqueue reply packet: %m");
1050 /* Let's take an extra reference on this stream, so that it stays around after returning. The reference
1051 * will be dangling until the stream is disconnected, and the default completion handler of the stream
1052 * will then unref the stream and destroy it */
1053 if (DNS_STREAM_QUEUED(stream
))
1054 dns_stream_ref(stream
);
1058 if (!ratelimit_below(&s
->ratelimit
))
1061 if (p
->family
== AF_INET
)
1062 fd
= manager_llmnr_ipv4_udp_fd(s
->manager
);
1063 else if (p
->family
== AF_INET6
)
1064 fd
= manager_llmnr_ipv6_udp_fd(s
->manager
);
1066 log_debug("Unknown protocol");
1070 log_debug_errno(fd
, "Failed to get reply socket: %m");
1074 /* Note that we always immediately reply to all LLMNR
1075 * requests, and do not wait any time, since we
1076 * verified uniqueness for all records. Also see RFC
1077 * 4795, Section 2.7 */
1079 r
= manager_send(s
->manager
, fd
, p
->ifindex
, p
->family
, &p
->sender
, p
->sender_port
, NULL
, reply
);
1081 log_debug_errno(r
, "Failed to send reply packet: %m");
1087 DnsTransaction
*dns_scope_find_transaction(
1089 DnsResourceKey
*key
,
1090 uint64_t query_flags
) {
1092 DnsTransaction
*first
;
1097 /* Iterate through the list of transactions with a matching key */
1098 first
= hashmap_get(scope
->transactions_by_key
, key
);
1099 LIST_FOREACH(transactions_by_key
, t
, first
) {
1101 /* These four flags must match exactly: we cannot use a validated response for a
1102 * non-validating client, and we cannot use a non-validated response for a validating
1103 * client. Similar, if the sources don't match things aren't usable either. */
1104 if (((query_flags
^ t
->query_flags
) &
1105 (SD_RESOLVED_NO_VALIDATE
|
1106 SD_RESOLVED_NO_ZONE
|
1107 SD_RESOLVED_NO_TRUST_ANCHOR
|
1108 SD_RESOLVED_NO_NETWORK
)) != 0)
1111 /* We can reuse a primary query if a regular one is requested, but not vice versa */
1112 if ((query_flags
& SD_RESOLVED_REQUIRE_PRIMARY
) &&
1113 !(t
->query_flags
& SD_RESOLVED_REQUIRE_PRIMARY
))
1116 /* Don't reuse a transaction that allowed caching when we got told not to use it */
1117 if ((query_flags
& SD_RESOLVED_NO_CACHE
) &&
1118 !(t
->query_flags
& SD_RESOLVED_NO_CACHE
))
1121 /* If we are asked to clamp ttls and the existing transaction doesn't do it, we can't
1123 if ((query_flags
& SD_RESOLVED_CLAMP_TTL
) &&
1124 !(t
->query_flags
& SD_RESOLVED_CLAMP_TTL
))
1133 static int dns_scope_make_conflict_packet(
1135 DnsResourceRecord
*rr
,
1138 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1145 r
= dns_packet_new(&p
, s
->protocol
, 0, DNS_PACKET_SIZE_MAX
);
1149 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
1160 /* For mDNS, the transaction ID should always be 0 */
1161 if (s
->protocol
!= DNS_PROTOCOL_MDNS
)
1162 random_bytes(&DNS_PACKET_HEADER(p
)->id
, sizeof(uint16_t));
1164 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(1);
1165 DNS_PACKET_HEADER(p
)->arcount
= htobe16(1);
1167 r
= dns_packet_append_key(p
, rr
->key
, 0, NULL
);
1171 r
= dns_packet_append_rr(p
, rr
, 0, NULL
, NULL
);
1180 static int on_conflict_dispatch(sd_event_source
*es
, usec_t usec
, void *userdata
) {
1181 DnsScope
*scope
= ASSERT_PTR(userdata
);
1186 scope
->conflict_event_source
= sd_event_source_disable_unref(scope
->conflict_event_source
);
1189 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1190 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1191 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1193 key
= ordered_hashmap_first_key(scope
->conflict_queue
);
1197 rr
= ordered_hashmap_remove(scope
->conflict_queue
, key
);
1200 r
= dns_scope_make_conflict_packet(scope
, rr
, &p
);
1202 log_error_errno(r
, "Failed to make conflict packet: %m");
1206 r
= dns_scope_emit_udp(scope
, -1, AF_UNSPEC
, p
);
1208 log_debug_errno(r
, "Failed to send conflict packet: %m");
1214 int dns_scope_notify_conflict(DnsScope
*scope
, DnsResourceRecord
*rr
) {
1220 /* We don't send these queries immediately. Instead, we queue
1221 * them, and send them after some jitter delay. */
1222 r
= ordered_hashmap_ensure_allocated(&scope
->conflict_queue
, &dns_resource_key_hash_ops
);
1228 /* We only place one RR per key in the conflict
1229 * messages, not all of them. That should be enough to
1230 * indicate where there might be a conflict */
1231 r
= ordered_hashmap_put(scope
->conflict_queue
, rr
->key
, rr
);
1232 if (IN_SET(r
, 0, -EEXIST
))
1235 return log_debug_errno(r
, "Failed to queue conflicting RR: %m");
1237 dns_resource_key_ref(rr
->key
);
1238 dns_resource_record_ref(rr
);
1240 if (scope
->conflict_event_source
)
1243 r
= sd_event_add_time_relative(
1244 scope
->manager
->event
,
1245 &scope
->conflict_event_source
,
1247 random_u64_range(LLMNR_JITTER_INTERVAL_USEC
),
1249 on_conflict_dispatch
, scope
);
1251 return log_debug_errno(r
, "Failed to add conflict dispatch event: %m");
1253 (void) sd_event_source_set_description(scope
->conflict_event_source
, "scope-conflict");
1258 void dns_scope_check_conflicts(DnsScope
*scope
, DnsPacket
*p
) {
1259 DnsResourceRecord
*rr
;
1265 if (!IN_SET(p
->protocol
, DNS_PROTOCOL_LLMNR
, DNS_PROTOCOL_MDNS
))
1268 if (DNS_PACKET_RRCOUNT(p
) <= 0)
1271 if (p
->protocol
== DNS_PROTOCOL_LLMNR
) {
1272 if (DNS_PACKET_LLMNR_C(p
) != 0)
1275 if (DNS_PACKET_LLMNR_T(p
) != 0)
1279 if (manager_packet_from_local_address(scope
->manager
, p
))
1282 r
= dns_packet_extract(p
);
1284 log_debug_errno(r
, "Failed to extract packet: %m");
1288 log_debug("Checking for conflicts...");
1290 DNS_ANSWER_FOREACH(rr
, p
->answer
) {
1291 /* No conflict if it is DNS-SD RR used for service enumeration. */
1292 if (dns_resource_key_is_dnssd_ptr(rr
->key
))
1295 /* Check for conflicts against the local zone. If we
1296 * found one, we won't check any further */
1297 r
= dns_zone_check_conflicts(&scope
->zone
, rr
);
1301 /* Check for conflicts against the local cache. If so,
1302 * send out an advisory query, to inform everybody */
1303 r
= dns_cache_check_conflicts(&scope
->cache
, rr
, p
->family
, &p
->sender
);
1307 dns_scope_notify_conflict(scope
, rr
);
1311 void dns_scope_dump(DnsScope
*s
, FILE *f
) {
1317 fputs("[Scope protocol=", f
);
1318 fputs(dns_protocol_to_string(s
->protocol
), f
);
1321 fputs(" interface=", f
);
1322 fputs(s
->link
->ifname
, f
);
1325 if (s
->family
!= AF_UNSPEC
) {
1326 fputs(" family=", f
);
1327 fputs(af_to_name(s
->family
), f
);
1332 if (!dns_zone_is_empty(&s
->zone
)) {
1333 fputs("ZONE:\n", f
);
1334 dns_zone_dump(&s
->zone
, f
);
1337 if (!dns_cache_is_empty(&s
->cache
)) {
1338 fputs("CACHE:\n", f
);
1339 dns_cache_dump(&s
->cache
, f
);
1343 DnsSearchDomain
*dns_scope_get_search_domains(DnsScope
*s
) {
1346 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
1350 return s
->link
->search_domains
;
1352 return s
->manager
->search_domains
;
1355 bool dns_scope_name_wants_search_domain(DnsScope
*s
, const char *name
) {
1358 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
1361 return dns_name_is_single_label(name
);
1364 bool dns_scope_network_good(DnsScope
*s
) {
1365 /* Checks whether the network is in good state for lookups on this scope. For mDNS/LLMNR/Classic DNS scopes
1366 * bound to links this is easy, as they don't even exist if the link isn't in a suitable state. For the global
1367 * DNS scope we check whether there are any links that are up and have an address.
1369 * Note that Linux routing is complex and even systems that superficially have no IPv4 address might
1370 * be able to route IPv4 (and similar for IPv6), hence let's make a check here independent of address
1376 return manager_routable(s
->manager
);
1379 int dns_scope_ifindex(DnsScope
*s
) {
1383 return s
->link
->ifindex
;
1388 static int on_announcement_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
1389 DnsScope
*scope
= userdata
;
1393 scope
->announce_event_source
= sd_event_source_disable_unref(scope
->announce_event_source
);
1395 (void) dns_scope_announce(scope
, false);
1399 int dns_scope_announce(DnsScope
*scope
, bool goodbye
) {
1400 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
1401 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1402 _cleanup_set_free_ Set
*types
= NULL
;
1411 if (scope
->protocol
!= DNS_PROTOCOL_MDNS
)
1414 /* Check if we're done with probing. */
1415 LIST_FOREACH(transactions_by_scope
, t
, scope
->transactions
)
1416 if (DNS_TRANSACTION_IS_LIVE(t
->state
))
1419 /* Check if there're services pending conflict resolution. */
1420 if (manager_next_dnssd_names(scope
->manager
))
1421 return 0; /* we reach this point only if changing hostname didn't help */
1423 /* Calculate answer's size. */
1424 HASHMAP_FOREACH(z
, scope
->zone
.by_key
) {
1425 if (z
->state
!= DNS_ZONE_ITEM_ESTABLISHED
)
1428 if (z
->rr
->key
->type
== DNS_TYPE_PTR
&&
1429 !dns_zone_contains_name(&scope
->zone
, z
->rr
->ptr
.name
)) {
1430 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1432 log_debug("Skip PTR RR <%s> since its counterparts seem to be withdrawn", dns_resource_key_to_string(z
->rr
->key
, key_str
, sizeof key_str
));
1433 z
->state
= DNS_ZONE_ITEM_WITHDRAWN
;
1437 /* Collect service types for _services._dns-sd._udp.local RRs in a set */
1438 if (!scope
->announced
&&
1439 dns_resource_key_is_dnssd_ptr(z
->rr
->key
)) {
1440 if (!set_contains(types
, dns_resource_key_name(z
->rr
->key
))) {
1441 r
= set_ensure_put(&types
, &dns_name_hash_ops
, dns_resource_key_name(z
->rr
->key
));
1443 return log_debug_errno(r
, "Failed to add item to set: %m");
1447 LIST_FOREACH(by_key
, i
, z
)
1451 answer
= dns_answer_new(size
+ set_size(types
));
1455 /* Second iteration, actually add RRs to the answer. */
1456 HASHMAP_FOREACH(z
, scope
->zone
.by_key
)
1457 LIST_FOREACH (by_key
, i
, z
) {
1458 DnsAnswerFlags flags
;
1460 if (i
->state
!= DNS_ZONE_ITEM_ESTABLISHED
)
1463 if (dns_resource_key_is_dnssd_ptr(i
->rr
->key
))
1464 flags
= goodbye
? DNS_ANSWER_GOODBYE
: 0;
1466 flags
= goodbye
? (DNS_ANSWER_GOODBYE
|DNS_ANSWER_CACHE_FLUSH
) : DNS_ANSWER_CACHE_FLUSH
;
1468 r
= dns_answer_add(answer
, i
->rr
, 0, flags
, NULL
);
1470 return log_debug_errno(r
, "Failed to add RR to announce: %m");
1473 /* Since all the active services are in the zone make them discoverable now. */
1474 SET_FOREACH(service_type
, types
) {
1475 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1477 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_PTR
,
1478 "_services._dns-sd._udp.local");
1482 rr
->ptr
.name
= strdup(service_type
);
1486 rr
->ttl
= MDNS_DEFAULT_TTL
;
1488 r
= dns_zone_put(&scope
->zone
, scope
, rr
, false);
1490 log_warning_errno(r
, "Failed to add DNS-SD PTR record to MDNS zone, ignoring: %m");
1492 r
= dns_answer_add(answer
, rr
, 0, 0, NULL
);
1494 return log_debug_errno(r
, "Failed to add RR to announce: %m");
1497 if (dns_answer_isempty(answer
))
1500 r
= dns_scope_make_reply_packet(scope
, 0, DNS_RCODE_SUCCESS
, NULL
, answer
, NULL
, false, &p
);
1502 return log_debug_errno(r
, "Failed to build reply packet: %m");
1504 r
= dns_scope_emit_udp(scope
, -1, AF_UNSPEC
, p
);
1506 return log_debug_errno(r
, "Failed to send reply packet: %m");
1508 /* In section 8.3 of RFC6762: "The Multicast DNS responder MUST send at least two unsolicited
1509 * responses, one second apart." */
1510 if (!scope
->announced
) {
1511 scope
->announced
= true;
1513 r
= sd_event_add_time_relative(
1514 scope
->manager
->event
,
1515 &scope
->announce_event_source
,
1517 MDNS_ANNOUNCE_DELAY
,
1519 on_announcement_timeout
, scope
);
1521 return log_debug_errno(r
, "Failed to schedule second announcement: %m");
1523 (void) sd_event_source_set_description(scope
->announce_event_source
, "mdns-announce");
1529 int dns_scope_add_dnssd_services(DnsScope
*scope
) {
1530 DnssdService
*service
;
1535 if (hashmap_size(scope
->manager
->dnssd_services
) == 0)
1538 scope
->announced
= false;
1540 HASHMAP_FOREACH(service
, scope
->manager
->dnssd_services
) {
1541 service
->withdrawn
= false;
1543 r
= dns_zone_put(&scope
->zone
, scope
, service
->ptr_rr
, false);
1545 log_warning_errno(r
, "Failed to add PTR record to MDNS zone: %m");
1547 r
= dns_zone_put(&scope
->zone
, scope
, service
->srv_rr
, true);
1549 log_warning_errno(r
, "Failed to add SRV record to MDNS zone: %m");
1551 LIST_FOREACH(items
, txt_data
, service
->txt_data_items
) {
1552 r
= dns_zone_put(&scope
->zone
, scope
, txt_data
->rr
, true);
1554 log_warning_errno(r
, "Failed to add TXT record to MDNS zone: %m");
1561 int dns_scope_remove_dnssd_services(DnsScope
*scope
) {
1562 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1563 DnssdService
*service
;
1568 key
= dns_resource_key_new(DNS_CLASS_IN
, DNS_TYPE_PTR
,
1569 "_services._dns-sd._udp.local");
1573 r
= dns_zone_remove_rrs_by_key(&scope
->zone
, key
);
1577 HASHMAP_FOREACH(service
, scope
->manager
->dnssd_services
) {
1578 dns_zone_remove_rr(&scope
->zone
, service
->ptr_rr
);
1579 dns_zone_remove_rr(&scope
->zone
, service
->srv_rr
);
1580 LIST_FOREACH(items
, txt_data
, service
->txt_data_items
)
1581 dns_zone_remove_rr(&scope
->zone
, txt_data
->rr
);
1587 static bool dns_scope_has_route_only_domains(DnsScope
*scope
) {
1588 DnsSearchDomain
*first
;
1589 bool route_only
= false;
1592 assert(scope
->protocol
== DNS_PROTOCOL_DNS
);
1594 /* Returns 'true' if this scope is suitable for queries to specific domains only. For that we check
1595 * if there are any route-only domains on this interface, as a heuristic to discern VPN-style links
1596 * from non-VPN-style links. Returns 'false' for all other cases, i.e. if the scope is intended to
1597 * take queries to arbitrary domains, i.e. has no routing domains set. */
1600 first
= scope
->link
->search_domains
;
1602 first
= scope
->manager
->search_domains
;
1604 LIST_FOREACH(domains
, domain
, first
) {
1605 /* "." means "any domain", thus the interface takes any kind of traffic. Thus, we exit early
1606 * here, as it doesn't really matter whether this link has any route-only domains or not,
1607 * "~." really trumps everything and clearly indicates that this interface shall receive all
1608 * traffic it can get. */
1609 if (dns_name_is_root(DNS_SEARCH_DOMAIN_NAME(domain
)))
1612 if (domain
->route_only
)
1619 bool dns_scope_is_default_route(DnsScope
*scope
) {
1622 /* Only use DNS scopes as default routes */
1623 if (scope
->protocol
!= DNS_PROTOCOL_DNS
)
1626 /* The global DNS scope is always suitable as default route */
1630 /* Honour whatever is explicitly configured. This is really the best approach, and trumps any
1631 * automatic logic. */
1632 if (scope
->link
->default_route
>= 0)
1633 return scope
->link
->default_route
;
1635 /* Otherwise check if we have any route-only domains, as a sensible heuristic: if so, let's not
1636 * volunteer as default route. */
1637 return !dns_scope_has_route_only_domains(scope
);