2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/tcp.h>
23 #include "alloc-util.h"
24 #include "dns-domain.h"
26 #include "hostname-util.h"
28 #include "random-util.h"
29 #include "resolved-dns-scope.h"
30 #include "resolved-llmnr.h"
31 #include "resolved-mdns.h"
32 #include "socket-util.h"
35 #define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
36 #define MULTICAST_RATELIMIT_BURST 1000
38 /* After how much time to repeat LLMNR requests, see RFC 4795 Section 7 */
39 #define MULTICAST_RESEND_TIMEOUT_MIN_USEC (100 * USEC_PER_MSEC)
40 #define MULTICAST_RESEND_TIMEOUT_MAX_USEC (1 * USEC_PER_SEC)
42 int dns_scope_new(Manager
*m
, DnsScope
**ret
, Link
*l
, DnsProtocol protocol
, int family
) {
48 s
= new0(DnsScope
, 1);
54 s
->protocol
= protocol
;
56 s
->resend_timeout
= MULTICAST_RESEND_TIMEOUT_MIN_USEC
;
58 if (protocol
== DNS_PROTOCOL_DNS
) {
59 /* Copy DNSSEC mode from the link if it is set there,
60 * otherwise take the manager's DNSSEC mode. Note that
61 * we copy this only at scope creation time, and do
62 * not update it from the on, even if the setting
66 s
->dnssec_mode
= link_get_dnssec_mode(l
);
68 s
->dnssec_mode
= manager_get_dnssec_mode(m
);
70 s
->dnssec_mode
= DNSSEC_NO
;
72 LIST_PREPEND(scopes
, m
->dns_scopes
, s
);
74 dns_scope_llmnr_membership(s
, true);
75 dns_scope_mdns_membership(s
, true);
77 log_debug("New scope on link %s, protocol %s, family %s", l
? l
->name
: "*", dns_protocol_to_string(protocol
), family
== AF_UNSPEC
? "*" : af_to_name(family
));
79 /* Enforce ratelimiting for the multicast protocols */
80 RATELIMIT_INIT(s
->ratelimit
, MULTICAST_RATELIMIT_INTERVAL_USEC
, MULTICAST_RATELIMIT_BURST
);
86 static void dns_scope_abort_transactions(DnsScope
*s
) {
89 while (s
->transactions
) {
90 DnsTransaction
*t
= s
->transactions
;
92 /* Abort the transaction, but make sure it is not
93 * freed while we still look at it */
96 if (DNS_TRANSACTION_IS_LIVE(t
->state
))
97 dns_transaction_complete(t
, DNS_TRANSACTION_ABORTED
);
100 dns_transaction_free(t
);
104 DnsScope
* dns_scope_free(DnsScope
*s
) {
105 DnsResourceRecord
*rr
;
110 log_debug("Removing scope on link %s, protocol %s, family %s", s
->link
? s
->link
->name
: "*", dns_protocol_to_string(s
->protocol
), s
->family
== AF_UNSPEC
? "*" : af_to_name(s
->family
));
112 dns_scope_llmnr_membership(s
, false);
113 dns_scope_mdns_membership(s
, false);
114 dns_scope_abort_transactions(s
);
116 while (s
->query_candidates
)
117 dns_query_candidate_free(s
->query_candidates
);
119 hashmap_free(s
->transactions_by_key
);
121 while ((rr
= ordered_hashmap_steal_first(s
->conflict_queue
)))
122 dns_resource_record_unref(rr
);
124 ordered_hashmap_free(s
->conflict_queue
);
125 sd_event_source_unref(s
->conflict_event_source
);
127 dns_cache_flush(&s
->cache
);
128 dns_zone_flush(&s
->zone
);
130 LIST_REMOVE(scopes
, s
->manager
->dns_scopes
, s
);
136 DnsServer
*dns_scope_get_dns_server(DnsScope
*s
) {
139 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
143 return link_get_dns_server(s
->link
);
145 return manager_get_dns_server(s
->manager
);
148 void dns_scope_next_dns_server(DnsScope
*s
) {
151 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
155 link_next_dns_server(s
->link
);
157 manager_next_dns_server(s
->manager
);
160 void dns_scope_packet_received(DnsScope
*s
, usec_t rtt
) {
163 if (rtt
<= s
->max_rtt
)
167 s
->resend_timeout
= MIN(MAX(MULTICAST_RESEND_TIMEOUT_MIN_USEC
, s
->max_rtt
* 2), MULTICAST_RESEND_TIMEOUT_MAX_USEC
);
170 void dns_scope_packet_lost(DnsScope
*s
, usec_t usec
) {
173 if (s
->resend_timeout
<= usec
)
174 s
->resend_timeout
= MIN(s
->resend_timeout
* 2, MULTICAST_RESEND_TIMEOUT_MAX_USEC
);
177 static int dns_scope_emit_one(DnsScope
*s
, int fd
, DnsPacket
*p
) {
178 union in_addr_union addr
;
185 assert(p
->protocol
== s
->protocol
);
189 ifindex
= s
->link
->ifindex
;
191 mtu
= manager_find_mtu(s
->manager
);
193 switch (s
->protocol
) {
195 case DNS_PROTOCOL_DNS
:
198 if (DNS_PACKET_QDCOUNT(p
) > 1)
201 if (p
->size
> DNS_PACKET_UNICAST_SIZE_MAX
)
204 if (p
->size
+ UDP_PACKET_HEADER_SIZE
> mtu
)
207 r
= manager_write(s
->manager
, fd
, p
);
213 case DNS_PROTOCOL_LLMNR
:
216 if (DNS_PACKET_QDCOUNT(p
) > 1)
219 if (!ratelimit_test(&s
->ratelimit
))
224 if (family
== AF_INET
) {
225 addr
.in
= LLMNR_MULTICAST_IPV4_ADDRESS
;
226 fd
= manager_llmnr_ipv4_udp_fd(s
->manager
);
227 } else if (family
== AF_INET6
) {
228 addr
.in6
= LLMNR_MULTICAST_IPV6_ADDRESS
;
229 fd
= manager_llmnr_ipv6_udp_fd(s
->manager
);
231 return -EAFNOSUPPORT
;
235 r
= manager_send(s
->manager
, fd
, ifindex
, family
, &addr
, LLMNR_PORT
, p
);
241 case DNS_PROTOCOL_MDNS
:
244 if (!ratelimit_test(&s
->ratelimit
))
249 if (family
== AF_INET
) {
250 addr
.in
= MDNS_MULTICAST_IPV4_ADDRESS
;
251 fd
= manager_mdns_ipv4_fd(s
->manager
);
252 } else if (family
== AF_INET6
) {
253 addr
.in6
= MDNS_MULTICAST_IPV6_ADDRESS
;
254 fd
= manager_mdns_ipv6_fd(s
->manager
);
256 return -EAFNOSUPPORT
;
260 r
= manager_send(s
->manager
, fd
, ifindex
, family
, &addr
, MDNS_PORT
, p
);
267 return -EAFNOSUPPORT
;
273 int dns_scope_emit_udp(DnsScope
*s
, int fd
, DnsPacket
*p
) {
278 assert(p
->protocol
== s
->protocol
);
279 assert((s
->protocol
== DNS_PROTOCOL_DNS
) == (fd
>= 0));
282 /* If there are multiple linked packets, set the TC bit in all but the last of them */
284 assert(p
->protocol
== DNS_PROTOCOL_MDNS
);
285 dns_packet_set_flags(p
, true, true);
288 r
= dns_scope_emit_one(s
, fd
, p
);
298 static int dns_scope_socket(
302 const union in_addr_union
*address
,
306 _cleanup_close_
int fd
= -1;
307 union sockaddr_union sa
= {};
309 static const int one
= 1;
315 assert(family
== AF_UNSPEC
);
318 sa
.sa
.sa_family
= server
->family
;
319 if (server
->family
== AF_INET
) {
320 sa
.in
.sin_port
= htobe16(port
);
321 sa
.in
.sin_addr
= server
->address
.in
;
322 salen
= sizeof(sa
.in
);
323 } else if (server
->family
== AF_INET6
) {
324 sa
.in6
.sin6_port
= htobe16(port
);
325 sa
.in6
.sin6_addr
= server
->address
.in6
;
326 sa
.in6
.sin6_scope_id
= s
->link
? s
->link
->ifindex
: 0;
327 salen
= sizeof(sa
.in6
);
329 return -EAFNOSUPPORT
;
331 assert(family
!= AF_UNSPEC
);
334 sa
.sa
.sa_family
= family
;
336 if (family
== AF_INET
) {
337 sa
.in
.sin_port
= htobe16(port
);
338 sa
.in
.sin_addr
= address
->in
;
339 salen
= sizeof(sa
.in
);
340 } else if (family
== AF_INET6
) {
341 sa
.in6
.sin6_port
= htobe16(port
);
342 sa
.in6
.sin6_addr
= address
->in6
;
343 sa
.in6
.sin6_scope_id
= s
->link
? s
->link
->ifindex
: 0;
344 salen
= sizeof(sa
.in6
);
346 return -EAFNOSUPPORT
;
349 fd
= socket(sa
.sa
.sa_family
, type
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
353 if (type
== SOCK_STREAM
) {
354 r
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &one
, sizeof(one
));
360 uint32_t ifindex
= htobe32(s
->link
->ifindex
);
362 if (sa
.sa
.sa_family
== AF_INET
) {
363 r
= setsockopt(fd
, IPPROTO_IP
, IP_UNICAST_IF
, &ifindex
, sizeof(ifindex
));
366 } else if (sa
.sa
.sa_family
== AF_INET6
) {
367 r
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_UNICAST_IF
, &ifindex
, sizeof(ifindex
));
373 if (s
->protocol
== DNS_PROTOCOL_LLMNR
) {
374 /* RFC 4795, section 2.5 requires the TTL to be set to 1 */
376 if (sa
.sa
.sa_family
== AF_INET
) {
377 r
= setsockopt(fd
, IPPROTO_IP
, IP_TTL
, &one
, sizeof(one
));
380 } else if (sa
.sa
.sa_family
== AF_INET6
) {
381 r
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &one
, sizeof(one
));
387 r
= connect(fd
, &sa
.sa
, salen
);
388 if (r
< 0 && errno
!= EINPROGRESS
)
397 int dns_scope_socket_udp(DnsScope
*s
, DnsServer
*server
, uint16_t port
) {
398 return dns_scope_socket(s
, SOCK_DGRAM
, AF_UNSPEC
, NULL
, server
, port
);
401 int dns_scope_socket_tcp(DnsScope
*s
, int family
, const union in_addr_union
*address
, DnsServer
*server
, uint16_t port
) {
402 return dns_scope_socket(s
, SOCK_STREAM
, family
, address
, server
, port
);
405 DnsScopeMatch
dns_scope_good_domain(DnsScope
*s
, int ifindex
, uint64_t flags
, const char *domain
) {
411 /* Checks if the specified domain is something to look up on
412 * this scope. Note that this accepts non-qualified hostnames,
413 * i.e. those without any search path prefixed yet. */
415 if (ifindex
!= 0 && (!s
->link
|| s
->link
->ifindex
!= ifindex
))
418 if ((SD_RESOLVED_FLAGS_MAKE(s
->protocol
, s
->family
, 0) & flags
) == 0)
421 /* Never resolve any loopback hostname or IP address via DNS,
422 * LLMNR or mDNS. Instead, always rely on synthesized RRs for
424 if (is_localhost(domain
) ||
425 dns_name_endswith(domain
, "127.in-addr.arpa") > 0 ||
426 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)
429 /* Never respond to some of the domains listed in RFC6303 */
430 if (dns_name_endswith(domain
, "0.in-addr.arpa") > 0 ||
431 dns_name_equal(domain
, "255.255.255.255.in-addr.arpa") > 0 ||
432 dns_name_equal(domain
, "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.0.ip6.arpa") > 0)
435 /* Never respond to some of the domains listed in RFC6761 */
436 if (dns_name_endswith(domain
, "invalid") > 0)
439 /* Always honour search domains for routing queries. Note that
440 * we return DNS_SCOPE_YES here, rather than just
441 * DNS_SCOPE_MAYBE, which means wildcard scopes won't be
442 * considered anymore. */
443 LIST_FOREACH(domains
, d
, dns_scope_get_search_domains(s
))
444 if (dns_name_endswith(domain
, d
->name
) > 0)
445 return DNS_SCOPE_YES
;
447 switch (s
->protocol
) {
449 case DNS_PROTOCOL_DNS
:
451 /* Exclude link-local IP ranges */
452 if (dns_name_endswith(domain
, "254.169.in-addr.arpa") == 0 &&
453 dns_name_endswith(domain
, "8.e.f.ip6.arpa") == 0 &&
454 dns_name_endswith(domain
, "9.e.f.ip6.arpa") == 0 &&
455 dns_name_endswith(domain
, "a.e.f.ip6.arpa") == 0 &&
456 dns_name_endswith(domain
, "b.e.f.ip6.arpa") == 0 &&
457 /* If networks use .local in their private setups, they are supposed to also add .local to their search
458 * domains, which we already checked above. Otherwise, we consider .local specific to mDNS and won't
459 * send such queries ordinary DNS servers. */
460 dns_name_endswith(domain
, "local") == 0)
461 return DNS_SCOPE_MAYBE
;
465 case DNS_PROTOCOL_MDNS
:
466 if ((s
->family
== AF_INET
&& dns_name_endswith(domain
, "in-addr.arpa") > 0) ||
467 (s
->family
== AF_INET6
&& dns_name_endswith(domain
, "ip6.arpa") > 0) ||
468 (dns_name_endswith(domain
, "local") > 0 && /* only resolve names ending in .local via mDNS */
469 dns_name_equal(domain
, "local") == 0 && /* but not the single-label "local" name itself */
470 manager_is_own_hostname(s
->manager
, domain
) <= 0)) /* never resolve the local hostname via mDNS */
471 return DNS_SCOPE_MAYBE
;
475 case DNS_PROTOCOL_LLMNR
:
476 if ((s
->family
== AF_INET
&& dns_name_endswith(domain
, "in-addr.arpa") > 0) ||
477 (s
->family
== AF_INET6
&& dns_name_endswith(domain
, "ip6.arpa") > 0) ||
478 (dns_name_is_single_label(domain
) && /* only resolve single label names via LLMNR */
479 !is_gateway_hostname(domain
) && /* don't resolve "gateway" with LLMNR, let nss-myhostname handle this */
480 manager_is_own_hostname(s
->manager
, domain
) <= 0)) /* never resolve the local hostname via LLMNR */
481 return DNS_SCOPE_MAYBE
;
486 assert_not_reached("Unknown scope protocol");
490 bool dns_scope_good_key(DnsScope
*s
, const DnsResourceKey
*key
) {
496 /* Check if it makes sense to resolve the specified key on
497 * this scope. Note that this call assumes as fully qualified
498 * name, i.e. the search suffixes already appended. */
500 if (key
->class != DNS_CLASS_IN
)
503 if (s
->protocol
== DNS_PROTOCOL_DNS
) {
505 /* On classic DNS, looking up non-address RRs is always
506 * fine. (Specifically, we want to permit looking up
507 * DNSKEY and DS records on the root and top-level
509 if (!dns_resource_key_is_address(key
))
512 /* However, we refuse to look up A and AAAA RRs on the
513 * root and single-label domains, under the assumption
514 * that those should be resolved via LLMNR or search
515 * path only, and should not be leaked onto the
517 return !(dns_name_is_single_label(DNS_RESOURCE_KEY_NAME(key
)) ||
518 dns_name_is_root(DNS_RESOURCE_KEY_NAME(key
)));
521 /* On mDNS and LLMNR, send A and AAAA queries only on the
522 * respective scopes */
524 key_family
= dns_type_to_af(key
->type
);
528 return key_family
== s
->family
;
531 static int dns_scope_multicast_membership(DnsScope
*s
, bool b
, struct in_addr in
, struct in6_addr in6
) {
537 if (s
->family
== AF_INET
) {
538 struct ip_mreqn mreqn
= {
540 .imr_ifindex
= s
->link
->ifindex
,
543 fd
= manager_llmnr_ipv4_udp_fd(s
->manager
);
547 /* Always first try to drop membership before we add
548 * one. This is necessary on some devices, such as
551 (void) setsockopt(fd
, IPPROTO_IP
, IP_DROP_MEMBERSHIP
, &mreqn
, sizeof(mreqn
));
553 if (setsockopt(fd
, IPPROTO_IP
, b
? IP_ADD_MEMBERSHIP
: IP_DROP_MEMBERSHIP
, &mreqn
, sizeof(mreqn
)) < 0)
556 } else if (s
->family
== AF_INET6
) {
557 struct ipv6_mreq mreq
= {
558 .ipv6mr_multiaddr
= in6
,
559 .ipv6mr_interface
= s
->link
->ifindex
,
562 fd
= manager_llmnr_ipv6_udp_fd(s
->manager
);
567 (void) setsockopt(fd
, IPPROTO_IPV6
, IPV6_DROP_MEMBERSHIP
, &mreq
, sizeof(mreq
));
569 if (setsockopt(fd
, IPPROTO_IPV6
, b
? IPV6_ADD_MEMBERSHIP
: IPV6_DROP_MEMBERSHIP
, &mreq
, sizeof(mreq
)) < 0)
572 return -EAFNOSUPPORT
;
577 int dns_scope_llmnr_membership(DnsScope
*s
, bool b
) {
579 if (s
->protocol
!= DNS_PROTOCOL_LLMNR
)
582 return dns_scope_multicast_membership(s
, b
, LLMNR_MULTICAST_IPV4_ADDRESS
, LLMNR_MULTICAST_IPV6_ADDRESS
);
585 int dns_scope_mdns_membership(DnsScope
*s
, bool b
) {
587 if (s
->protocol
!= DNS_PROTOCOL_MDNS
)
590 return dns_scope_multicast_membership(s
, b
, MDNS_MULTICAST_IPV4_ADDRESS
, MDNS_MULTICAST_IPV6_ADDRESS
);
593 static int dns_scope_make_reply_packet(
603 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
610 if ((!q
|| q
->n_keys
<= 0)
611 && (!answer
|| answer
->n_rrs
<= 0)
612 && (!soa
|| soa
->n_rrs
<= 0))
615 r
= dns_packet_new(&p
, s
->protocol
, 0);
619 DNS_PACKET_HEADER(p
)->id
= id
;
620 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
632 for (i
= 0; i
< q
->n_keys
; i
++) {
633 r
= dns_packet_append_key(p
, q
->keys
[i
], NULL
);
638 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(q
->n_keys
);
642 for (i
= 0; i
< answer
->n_rrs
; i
++) {
643 r
= dns_packet_append_rr(p
, answer
->items
[i
].rr
, NULL
, NULL
);
648 DNS_PACKET_HEADER(p
)->ancount
= htobe16(answer
->n_rrs
);
652 for (i
= 0; i
< soa
->n_rrs
; i
++) {
653 r
= dns_packet_append_rr(p
, soa
->items
[i
].rr
, NULL
, NULL
);
658 DNS_PACKET_HEADER(p
)->arcount
= htobe16(soa
->n_rrs
);
667 static void dns_scope_verify_conflicts(DnsScope
*s
, DnsPacket
*p
) {
674 for (n
= 0; n
< p
->question
->n_keys
; n
++)
675 dns_zone_verify_conflicts(&s
->zone
, p
->question
->keys
[n
]);
677 for (n
= 0; n
< p
->answer
->n_rrs
; n
++)
678 dns_zone_verify_conflicts(&s
->zone
, p
->answer
->items
[n
].rr
->key
);
681 void dns_scope_process_query(DnsScope
*s
, DnsStream
*stream
, DnsPacket
*p
) {
682 _cleanup_(dns_packet_unrefp
) DnsPacket
*reply
= NULL
;
683 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
, *soa
= NULL
;
684 DnsResourceKey
*key
= NULL
;
685 bool tentative
= false;
691 if (p
->protocol
!= DNS_PROTOCOL_LLMNR
)
694 if (p
->ipproto
== IPPROTO_UDP
) {
695 /* Don't accept UDP queries directed to anything but
696 * the LLMNR multicast addresses. See RFC 4795,
699 if (p
->family
== AF_INET
&& !in_addr_equal(AF_INET
, &p
->destination
, (union in_addr_union
*) &LLMNR_MULTICAST_IPV4_ADDRESS
))
702 if (p
->family
== AF_INET6
&& !in_addr_equal(AF_INET6
, &p
->destination
, (union in_addr_union
*) &LLMNR_MULTICAST_IPV6_ADDRESS
))
706 r
= dns_packet_extract(p
);
708 log_debug_errno(r
, "Failed to extract resources from incoming packet: %m");
712 if (DNS_PACKET_LLMNR_C(p
)) {
713 /* Somebody notified us about a possible conflict */
714 dns_scope_verify_conflicts(s
, p
);
718 assert(p
->question
->n_keys
== 1);
719 key
= p
->question
->keys
[0];
721 r
= dns_zone_lookup(&s
->zone
, key
, &answer
, &soa
, &tentative
);
723 log_debug_errno(r
, "Failed to lookup key: %m");
730 dns_answer_order_by_scope(answer
, in_addr_is_link_local(p
->family
, &p
->sender
) > 0);
732 r
= dns_scope_make_reply_packet(s
, DNS_PACKET_ID(p
), DNS_RCODE_SUCCESS
, p
->question
, answer
, soa
, tentative
, &reply
);
734 log_debug_errno(r
, "Failed to build reply packet: %m");
739 r
= dns_stream_write_packet(stream
, reply
);
741 if (!ratelimit_test(&s
->ratelimit
))
744 if (p
->family
== AF_INET
)
745 fd
= manager_llmnr_ipv4_udp_fd(s
->manager
);
746 else if (p
->family
== AF_INET6
)
747 fd
= manager_llmnr_ipv6_udp_fd(s
->manager
);
749 log_debug("Unknown protocol");
753 log_debug_errno(fd
, "Failed to get reply socket: %m");
757 /* Note that we always immediately reply to all LLMNR
758 * requests, and do not wait any time, since we
759 * verified uniqueness for all records. Also see RFC
760 * 4795, Section 2.7 */
762 r
= manager_send(s
->manager
, fd
, p
->ifindex
, p
->family
, &p
->sender
, p
->sender_port
, reply
);
766 log_debug_errno(r
, "Failed to send reply packet: %m");
771 DnsTransaction
*dns_scope_find_transaction(DnsScope
*scope
, DnsResourceKey
*key
, bool cache_ok
) {
777 /* Try to find an ongoing transaction that is a equal to the
778 * specified question */
779 t
= hashmap_get(scope
->transactions_by_key
, key
);
783 /* Refuse reusing transactions that completed based on cached
784 * data instead of a real packet, if that's requested. */
786 IN_SET(t
->state
, DNS_TRANSACTION_SUCCESS
, DNS_TRANSACTION_RCODE_FAILURE
) &&
787 t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
793 static int dns_scope_make_conflict_packet(
795 DnsResourceRecord
*rr
,
798 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
805 r
= dns_packet_new(&p
, s
->protocol
, 0);
809 DNS_PACKET_HEADER(p
)->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(
820 /* For mDNS, the transaction ID should always be 0 */
821 if (s
->protocol
!= DNS_PROTOCOL_MDNS
)
822 random_bytes(&DNS_PACKET_HEADER(p
)->id
, sizeof(uint16_t));
824 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(1);
825 DNS_PACKET_HEADER(p
)->arcount
= htobe16(1);
827 r
= dns_packet_append_key(p
, rr
->key
, NULL
);
831 r
= dns_packet_append_rr(p
, rr
, NULL
, NULL
);
841 static int on_conflict_dispatch(sd_event_source
*es
, usec_t usec
, void *userdata
) {
842 DnsScope
*scope
= userdata
;
848 scope
->conflict_event_source
= sd_event_source_unref(scope
->conflict_event_source
);
851 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
852 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
854 rr
= ordered_hashmap_steal_first(scope
->conflict_queue
);
858 r
= dns_scope_make_conflict_packet(scope
, rr
, &p
);
860 log_error_errno(r
, "Failed to make conflict packet: %m");
864 r
= dns_scope_emit_udp(scope
, -1, p
);
866 log_debug_errno(r
, "Failed to send conflict packet: %m");
872 int dns_scope_notify_conflict(DnsScope
*scope
, DnsResourceRecord
*rr
) {
879 /* We don't send these queries immediately. Instead, we queue
880 * them, and send them after some jitter delay. */
881 r
= ordered_hashmap_ensure_allocated(&scope
->conflict_queue
, &dns_resource_key_hash_ops
);
887 /* We only place one RR per key in the conflict
888 * messages, not all of them. That should be enough to
889 * indicate where there might be a conflict */
890 r
= ordered_hashmap_put(scope
->conflict_queue
, rr
->key
, rr
);
891 if (r
== -EEXIST
|| r
== 0)
894 return log_debug_errno(r
, "Failed to queue conflicting RR: %m");
896 dns_resource_record_ref(rr
);
898 if (scope
->conflict_event_source
)
901 random_bytes(&jitter
, sizeof(jitter
));
902 jitter
%= LLMNR_JITTER_INTERVAL_USEC
;
904 r
= sd_event_add_time(scope
->manager
->event
,
905 &scope
->conflict_event_source
,
906 clock_boottime_or_monotonic(),
907 now(clock_boottime_or_monotonic()) + jitter
,
908 LLMNR_JITTER_INTERVAL_USEC
,
909 on_conflict_dispatch
, scope
);
911 return log_debug_errno(r
, "Failed to add conflict dispatch event: %m");
913 (void) sd_event_source_set_description(scope
->conflict_event_source
, "scope-conflict");
918 void dns_scope_check_conflicts(DnsScope
*scope
, DnsPacket
*p
) {
925 if (p
->protocol
!= DNS_PROTOCOL_LLMNR
)
928 if (DNS_PACKET_RRCOUNT(p
) <= 0)
931 if (DNS_PACKET_LLMNR_C(p
) != 0)
934 if (DNS_PACKET_LLMNR_T(p
) != 0)
937 if (manager_our_packet(scope
->manager
, p
))
940 r
= dns_packet_extract(p
);
942 log_debug_errno(r
, "Failed to extract packet: %m");
946 log_debug("Checking for conflicts...");
948 for (i
= 0; i
< p
->answer
->n_rrs
; i
++) {
950 /* Check for conflicts against the local zone. If we
951 * found one, we won't check any further */
952 r
= dns_zone_check_conflicts(&scope
->zone
, p
->answer
->items
[i
].rr
);
956 /* Check for conflicts against the local cache. If so,
957 * send out an advisory query, to inform everybody */
958 r
= dns_cache_check_conflicts(&scope
->cache
, p
->answer
->items
[i
].rr
, p
->family
, &p
->sender
);
962 dns_scope_notify_conflict(scope
, p
->answer
->items
[i
].rr
);
966 void dns_scope_dump(DnsScope
*s
, FILE *f
) {
972 fputs("[Scope protocol=", f
);
973 fputs(dns_protocol_to_string(s
->protocol
), f
);
976 fputs(" interface=", f
);
977 fputs(s
->link
->name
, f
);
980 if (s
->family
!= AF_UNSPEC
) {
981 fputs(" family=", f
);
982 fputs(af_to_name(s
->family
), f
);
987 if (!dns_zone_is_empty(&s
->zone
)) {
989 dns_zone_dump(&s
->zone
, f
);
992 if (!dns_cache_is_empty(&s
->cache
)) {
993 fputs("CACHE:\n", f
);
994 dns_cache_dump(&s
->cache
, f
);
998 DnsSearchDomain
*dns_scope_get_search_domains(DnsScope
*s
) {
1001 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
1005 return s
->link
->search_domains
;
1007 return s
->manager
->search_domains
;
1010 bool dns_scope_name_needs_search_domain(DnsScope
*s
, const char *name
) {
1013 if (s
->protocol
!= DNS_PROTOCOL_DNS
)
1016 return dns_name_is_single_label(name
);
1019 bool dns_scope_network_good(DnsScope
*s
) {
1020 /* Checks whether the network is in good state for lookups on this scope. For mDNS/LLMNR/Classic DNS scopes
1021 * bound to links this is easy, as they don't even exist if the link isn't in a suitable state. For the global
1022 * DNS scope we check whether there are any links that are up and have an address. */
1027 return manager_routable(s
->manager
, AF_UNSPEC
);