]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-scope.c
Merge pull request #15314 from keszybz/network-server-access-functions
[thirdparty/systemd.git] / src / resolve / resolved-dns-scope.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <netinet/tcp.h>
4
5 #include "af-list.h"
6 #include "alloc-util.h"
7 #include "dns-domain.h"
8 #include "fd-util.h"
9 #include "hostname-util.h"
10 #include "missing_network.h"
11 #include "random-util.h"
12 #include "resolved-dnssd.h"
13 #include "resolved-dns-scope.h"
14 #include "resolved-dns-zone.h"
15 #include "resolved-llmnr.h"
16 #include "resolved-mdns.h"
17 #include "socket-util.h"
18 #include "strv.h"
19
20 #define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
21 #define MULTICAST_RATELIMIT_BURST 1000
22
23 /* After how much time to repeat LLMNR requests, see RFC 4795 Section 7 */
24 #define MULTICAST_RESEND_TIMEOUT_MIN_USEC (100 * USEC_PER_MSEC)
25 #define MULTICAST_RESEND_TIMEOUT_MAX_USEC (1 * USEC_PER_SEC)
26
27 int dns_scope_new(Manager *m, DnsScope **ret, Link *l, DnsProtocol protocol, int family) {
28 DnsScope *s;
29
30 assert(m);
31 assert(ret);
32
33 s = new(DnsScope, 1);
34 if (!s)
35 return -ENOMEM;
36
37 *s = (DnsScope) {
38 .manager = m,
39 .link = l,
40 .protocol = protocol,
41 .family = family,
42 .resend_timeout = MULTICAST_RESEND_TIMEOUT_MIN_USEC,
43 };
44
45 if (protocol == DNS_PROTOCOL_DNS) {
46 /* Copy DNSSEC mode from the link if it is set there,
47 * otherwise take the manager's DNSSEC mode. Note that
48 * we copy this only at scope creation time, and do
49 * not update it from the on, even if the setting
50 * changes. */
51
52 if (l) {
53 s->dnssec_mode = link_get_dnssec_mode(l);
54 s->dns_over_tls_mode = link_get_dns_over_tls_mode(l);
55 } else {
56 s->dnssec_mode = manager_get_dnssec_mode(m);
57 s->dns_over_tls_mode = manager_get_dns_over_tls_mode(m);
58 }
59
60 } else {
61 s->dnssec_mode = DNSSEC_NO;
62 s->dns_over_tls_mode = DNS_OVER_TLS_NO;
63 }
64
65 LIST_PREPEND(scopes, m->dns_scopes, s);
66
67 dns_scope_llmnr_membership(s, true);
68 dns_scope_mdns_membership(s, true);
69
70 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));
71
72 /* Enforce ratelimiting for the multicast protocols */
73 s->ratelimit = (RateLimit) { MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST };
74
75 *ret = s;
76 return 0;
77 }
78
79 static void dns_scope_abort_transactions(DnsScope *s) {
80 assert(s);
81
82 while (s->transactions) {
83 DnsTransaction *t = s->transactions;
84
85 /* Abort the transaction, but make sure it is not
86 * freed while we still look at it */
87
88 t->block_gc++;
89 if (DNS_TRANSACTION_IS_LIVE(t->state))
90 dns_transaction_complete(t, DNS_TRANSACTION_ABORTED);
91 t->block_gc--;
92
93 dns_transaction_free(t);
94 }
95 }
96
97 DnsScope* dns_scope_free(DnsScope *s) {
98 if (!s)
99 return NULL;
100
101 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));
102
103 dns_scope_llmnr_membership(s, false);
104 dns_scope_mdns_membership(s, false);
105 dns_scope_abort_transactions(s);
106
107 while (s->query_candidates)
108 dns_query_candidate_free(s->query_candidates);
109
110 hashmap_free(s->transactions_by_key);
111
112 ordered_hashmap_free_with_destructor(s->conflict_queue, dns_resource_record_unref);
113 sd_event_source_unref(s->conflict_event_source);
114
115 sd_event_source_unref(s->announce_event_source);
116
117 dns_cache_flush(&s->cache);
118 dns_zone_flush(&s->zone);
119
120 LIST_REMOVE(scopes, s->manager->dns_scopes, s);
121 return mfree(s);
122 }
123
124 DnsServer *dns_scope_get_dns_server(DnsScope *s) {
125 assert(s);
126
127 if (s->protocol != DNS_PROTOCOL_DNS)
128 return NULL;
129
130 if (s->link)
131 return link_get_dns_server(s->link);
132 else
133 return manager_get_dns_server(s->manager);
134 }
135
136 unsigned dns_scope_get_n_dns_servers(DnsScope *s) {
137 unsigned n = 0;
138 DnsServer *i;
139
140 assert(s);
141
142 if (s->protocol != DNS_PROTOCOL_DNS)
143 return 0;
144
145 if (s->link)
146 i = s->link->dns_servers;
147 else
148 i = s->manager->dns_servers;
149
150 for (; i; i = i->servers_next)
151 n++;
152
153 return n;
154 }
155
156 void dns_scope_next_dns_server(DnsScope *s) {
157 assert(s);
158
159 if (s->protocol != DNS_PROTOCOL_DNS)
160 return;
161
162 if (s->link)
163 link_next_dns_server(s->link);
164 else
165 manager_next_dns_server(s->manager);
166 }
167
168 void dns_scope_packet_received(DnsScope *s, usec_t rtt) {
169 assert(s);
170
171 if (rtt <= s->max_rtt)
172 return;
173
174 s->max_rtt = rtt;
175 s->resend_timeout = MIN(MAX(MULTICAST_RESEND_TIMEOUT_MIN_USEC, s->max_rtt * 2), MULTICAST_RESEND_TIMEOUT_MAX_USEC);
176 }
177
178 void dns_scope_packet_lost(DnsScope *s, usec_t usec) {
179 assert(s);
180
181 if (s->resend_timeout <= usec)
182 s->resend_timeout = MIN(s->resend_timeout * 2, MULTICAST_RESEND_TIMEOUT_MAX_USEC);
183 }
184
185 static int dns_scope_emit_one(DnsScope *s, int fd, DnsPacket *p) {
186 union in_addr_union addr;
187 int ifindex = 0, r;
188 int family;
189 uint32_t mtu;
190
191 assert(s);
192 assert(p);
193 assert(p->protocol == s->protocol);
194
195 if (s->link) {
196 mtu = s->link->mtu;
197 ifindex = s->link->ifindex;
198 } else
199 mtu = manager_find_mtu(s->manager);
200
201 switch (s->protocol) {
202
203 case DNS_PROTOCOL_DNS:
204 assert(fd >= 0);
205
206 if (DNS_PACKET_QDCOUNT(p) > 1)
207 return -EOPNOTSUPP;
208
209 if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
210 return -EMSGSIZE;
211
212 if (p->size + UDP_PACKET_HEADER_SIZE > mtu)
213 return -EMSGSIZE;
214
215 r = manager_write(s->manager, fd, p);
216 if (r < 0)
217 return r;
218
219 break;
220
221 case DNS_PROTOCOL_LLMNR:
222 assert(fd < 0);
223
224 if (DNS_PACKET_QDCOUNT(p) > 1)
225 return -EOPNOTSUPP;
226
227 if (!ratelimit_below(&s->ratelimit))
228 return -EBUSY;
229
230 family = s->family;
231
232 if (family == AF_INET) {
233 addr.in = LLMNR_MULTICAST_IPV4_ADDRESS;
234 fd = manager_llmnr_ipv4_udp_fd(s->manager);
235 } else if (family == AF_INET6) {
236 addr.in6 = LLMNR_MULTICAST_IPV6_ADDRESS;
237 fd = manager_llmnr_ipv6_udp_fd(s->manager);
238 } else
239 return -EAFNOSUPPORT;
240 if (fd < 0)
241 return fd;
242
243 r = manager_send(s->manager, fd, ifindex, family, &addr, LLMNR_PORT, NULL, p);
244 if (r < 0)
245 return r;
246
247 break;
248
249 case DNS_PROTOCOL_MDNS:
250 assert(fd < 0);
251
252 if (!ratelimit_below(&s->ratelimit))
253 return -EBUSY;
254
255 family = s->family;
256
257 if (family == AF_INET) {
258 addr.in = MDNS_MULTICAST_IPV4_ADDRESS;
259 fd = manager_mdns_ipv4_fd(s->manager);
260 } else if (family == AF_INET6) {
261 addr.in6 = MDNS_MULTICAST_IPV6_ADDRESS;
262 fd = manager_mdns_ipv6_fd(s->manager);
263 } else
264 return -EAFNOSUPPORT;
265 if (fd < 0)
266 return fd;
267
268 r = manager_send(s->manager, fd, ifindex, family, &addr, MDNS_PORT, NULL, p);
269 if (r < 0)
270 return r;
271
272 break;
273
274 default:
275 return -EAFNOSUPPORT;
276 }
277
278 return 1;
279 }
280
281 int dns_scope_emit_udp(DnsScope *s, int fd, DnsPacket *p) {
282 int r;
283
284 assert(s);
285 assert(p);
286 assert(p->protocol == s->protocol);
287 assert((s->protocol == DNS_PROTOCOL_DNS) == (fd >= 0));
288
289 do {
290 /* If there are multiple linked packets, set the TC bit in all but the last of them */
291 if (p->more) {
292 assert(p->protocol == DNS_PROTOCOL_MDNS);
293 dns_packet_set_flags(p, true, true);
294 }
295
296 r = dns_scope_emit_one(s, fd, p);
297 if (r < 0)
298 return r;
299
300 p = p->more;
301 } while (p);
302
303 return 0;
304 }
305
306 static int dns_scope_socket(
307 DnsScope *s,
308 int type,
309 int family,
310 const union in_addr_union *address,
311 DnsServer *server,
312 uint16_t port,
313 union sockaddr_union *ret_socket_address) {
314
315 _cleanup_close_ int fd = -1;
316 union sockaddr_union sa;
317 socklen_t salen;
318 int r, ifindex;
319
320 assert(s);
321
322 if (server) {
323 assert(family == AF_UNSPEC);
324 assert(!address);
325
326 ifindex = dns_server_ifindex(server);
327
328 switch (server->family) {
329 case AF_INET:
330 sa = (union sockaddr_union) {
331 .in.sin_family = server->family,
332 .in.sin_port = htobe16(port),
333 .in.sin_addr = server->address.in,
334 };
335 salen = sizeof(sa.in);
336 break;
337 case AF_INET6:
338 sa = (union sockaddr_union) {
339 .in6.sin6_family = server->family,
340 .in6.sin6_port = htobe16(port),
341 .in6.sin6_addr = server->address.in6,
342 .in6.sin6_scope_id = ifindex,
343 };
344 salen = sizeof(sa.in6);
345 break;
346 default:
347 return -EAFNOSUPPORT;
348 }
349 } else {
350 assert(family != AF_UNSPEC);
351 assert(address);
352
353 ifindex = s->link ? s->link->ifindex : 0;
354
355 switch (family) {
356 case AF_INET:
357 sa = (union sockaddr_union) {
358 .in.sin_family = family,
359 .in.sin_port = htobe16(port),
360 .in.sin_addr = address->in,
361 };
362 salen = sizeof(sa.in);
363 break;
364 case AF_INET6:
365 sa = (union sockaddr_union) {
366 .in6.sin6_family = family,
367 .in6.sin6_port = htobe16(port),
368 .in6.sin6_addr = address->in6,
369 .in6.sin6_scope_id = ifindex,
370 };
371 salen = sizeof(sa.in6);
372 break;
373 default:
374 return -EAFNOSUPPORT;
375 }
376 }
377
378 fd = socket(sa.sa.sa_family, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
379 if (fd < 0)
380 return -errno;
381
382 if (type == SOCK_STREAM) {
383 r = setsockopt_int(fd, IPPROTO_TCP, TCP_NODELAY, true);
384 if (r < 0)
385 return r;
386 }
387
388 if (s->link) {
389 be32_t ifindex_be = htobe32(ifindex);
390
391 if (sa.sa.sa_family == AF_INET) {
392 r = setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be));
393 if (r < 0)
394 return -errno;
395 } else if (sa.sa.sa_family == AF_INET6) {
396 r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be));
397 if (r < 0)
398 return -errno;
399 }
400 }
401
402 if (s->protocol == DNS_PROTOCOL_LLMNR) {
403 /* RFC 4795, section 2.5 requires the TTL to be set to 1 */
404
405 if (sa.sa.sa_family == AF_INET) {
406 r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, true);
407 if (r < 0)
408 return r;
409 } else if (sa.sa.sa_family == AF_INET6) {
410 r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, true);
411 if (r < 0)
412 return r;
413 }
414 }
415
416 if (type == SOCK_DGRAM) {
417 /* Set IP_RECVERR or IPV6_RECVERR to get ICMP error feedback. See discussion in #10345. */
418
419 if (sa.sa.sa_family == AF_INET) {
420 r = setsockopt_int(fd, IPPROTO_IP, IP_RECVERR, true);
421 if (r < 0)
422 return r;
423
424 r = setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, true);
425 if (r < 0)
426 return r;
427
428 } else if (sa.sa.sa_family == AF_INET6) {
429 r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVERR, true);
430 if (r < 0)
431 return r;
432
433 r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, true);
434 if (r < 0)
435 return r;
436 }
437 }
438
439 if (ret_socket_address)
440 *ret_socket_address = sa;
441 else {
442 r = connect(fd, &sa.sa, salen);
443 if (r < 0 && errno != EINPROGRESS)
444 return -errno;
445 }
446
447 return TAKE_FD(fd);
448 }
449
450 int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) {
451 return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, server, port, NULL);
452 }
453
454 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) {
455 /* If ret_socket_address is not NULL, the caller is responsible
456 * for calling connect() or sendmsg(). This is required by TCP
457 * Fast Open, to be able to send the initial SYN packet along
458 * with the first data packet. */
459 return dns_scope_socket(s, SOCK_STREAM, family, address, server, port, ret_socket_address);
460 }
461
462 static DnsScopeMatch accept_link_local_reverse_lookups(const char *domain) {
463 assert(domain);
464
465 if (dns_name_endswith(domain, "254.169.in-addr.arpa") > 0)
466 return DNS_SCOPE_YES_BASE + 4; /* 4 labels match */
467
468 if (dns_name_endswith(domain, "8.e.f.ip6.arpa") > 0 ||
469 dns_name_endswith(domain, "9.e.f.ip6.arpa") > 0 ||
470 dns_name_endswith(domain, "a.e.f.ip6.arpa") > 0 ||
471 dns_name_endswith(domain, "b.e.f.ip6.arpa") > 0)
472 return DNS_SCOPE_YES_BASE + 5; /* 5 labels match */
473
474 return _DNS_SCOPE_MATCH_INVALID;
475 }
476
477 DnsScopeMatch dns_scope_good_domain(
478 DnsScope *s,
479 int ifindex,
480 uint64_t flags,
481 const char *domain) {
482
483 DnsSearchDomain *d;
484
485 /* This returns the following return values:
486 *
487 * DNS_SCOPE_NO → This scope is not suitable for lookups of this domain, at all
488 * DNS_SCOPE_MAYBE → This scope is suitable, but only if nothing else wants it
489 * DNS_SCOPE_YES_BASE+n → This scope is suitable, and 'n' suffix labels match
490 *
491 * (The idea is that the caller will only use the scopes with the longest 'n' returned. If no scopes return
492 * DNS_SCOPE_YES_BASE+n, then it should use those which returned DNS_SCOPE_MAYBE. It should never use those
493 * which returned DNS_SCOPE_NO.)
494 */
495
496 assert(s);
497 assert(domain);
498
499 /* Checks if the specified domain is something to look up on this scope. Note that this accepts
500 * non-qualified hostnames, i.e. those without any search path suffixed. */
501
502 if (ifindex != 0 && (!s->link || s->link->ifindex != ifindex))
503 return DNS_SCOPE_NO;
504
505 if ((SD_RESOLVED_FLAGS_MAKE(s->protocol, s->family, 0) & flags) == 0)
506 return DNS_SCOPE_NO;
507
508 /* Never resolve any loopback hostname or IP address via DNS,
509 * LLMNR or mDNS. Instead, always rely on synthesized RRs for
510 * these. */
511 if (is_localhost(domain) ||
512 dns_name_endswith(domain, "127.in-addr.arpa") > 0 ||
513 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)
514 return DNS_SCOPE_NO;
515
516 /* Never respond to some of the domains listed in RFC6303 */
517 if (dns_name_endswith(domain, "0.in-addr.arpa") > 0 ||
518 dns_name_equal(domain, "255.255.255.255.in-addr.arpa") > 0 ||
519 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)
520 return DNS_SCOPE_NO;
521
522 /* Never respond to some of the domains listed in RFC6761 */
523 if (dns_name_endswith(domain, "invalid") > 0)
524 return DNS_SCOPE_NO;
525
526 switch (s->protocol) {
527
528 case DNS_PROTOCOL_DNS: {
529 bool has_search_domains = false;
530 int n_best = -1;
531
532 /* Never route things to scopes that lack DNS servers */
533 if (!dns_scope_get_dns_server(s))
534 return DNS_SCOPE_NO;
535
536 /* Always honour search domains for routing queries, except if this scope lacks DNS servers. Note that
537 * we return DNS_SCOPE_YES here, rather than just DNS_SCOPE_MAYBE, which means other wildcard scopes
538 * won't be considered anymore. */
539 LIST_FOREACH(domains, d, dns_scope_get_search_domains(s)) {
540
541 if (!d->route_only && !dns_name_is_root(d->name))
542 has_search_domains = true;
543
544 if (dns_name_endswith(domain, d->name) > 0) {
545 int c;
546
547 c = dns_name_count_labels(d->name);
548 if (c < 0)
549 continue;
550
551 if (c > n_best)
552 n_best = c;
553 }
554 }
555
556 /* If there's a true search domain defined for this scope, and the query is single-label,
557 * then let's resolve things here, prefereably. Note that LLMNR considers itself
558 * authoritative for single-label names too, at the same preference, see below. */
559 if (has_search_domains && dns_name_is_single_label(domain))
560 return DNS_SCOPE_YES_BASE + 1;
561
562 /* Let's return the number of labels in the best matching result */
563 if (n_best >= 0) {
564 assert(n_best <= DNS_SCOPE_YES_END - DNS_SCOPE_YES_BASE);
565 return DNS_SCOPE_YES_BASE + n_best;
566 }
567
568 /* See if this scope is suitable as default route. */
569 if (!dns_scope_is_default_route(s))
570 return DNS_SCOPE_NO;
571
572 /* Exclude link-local IP ranges */
573 if (dns_name_endswith(domain, "254.169.in-addr.arpa") == 0 &&
574 dns_name_endswith(domain, "8.e.f.ip6.arpa") == 0 &&
575 dns_name_endswith(domain, "9.e.f.ip6.arpa") == 0 &&
576 dns_name_endswith(domain, "a.e.f.ip6.arpa") == 0 &&
577 dns_name_endswith(domain, "b.e.f.ip6.arpa") == 0 &&
578 /* If networks use .local in their private setups, they are supposed to also add .local to their search
579 * domains, which we already checked above. Otherwise, we consider .local specific to mDNS and won't
580 * send such queries ordinary DNS servers. */
581 dns_name_endswith(domain, "local") == 0)
582 return DNS_SCOPE_MAYBE;
583
584 return DNS_SCOPE_NO;
585 }
586
587 case DNS_PROTOCOL_MDNS: {
588 DnsScopeMatch m;
589
590 m = accept_link_local_reverse_lookups(domain);
591 if (m >= 0)
592 return m;
593
594 if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
595 (s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0))
596 return DNS_SCOPE_MAYBE;
597
598 if ((dns_name_endswith(domain, "local") > 0 && /* only resolve names ending in .local via mDNS */
599 dns_name_equal(domain, "local") == 0 && /* but not the single-label "local" name itself */
600 manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via mDNS */
601 return DNS_SCOPE_YES_BASE + 1; /* Return +1, as the top-level .local domain matches, i.e. one label */
602
603 return DNS_SCOPE_NO;
604 }
605
606 case DNS_PROTOCOL_LLMNR: {
607 DnsScopeMatch m;
608
609 m = accept_link_local_reverse_lookups(domain);
610 if (m >= 0)
611 return m;
612
613 if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
614 (s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0))
615 return DNS_SCOPE_MAYBE;
616
617 if ((dns_name_is_single_label(domain) && /* only resolve single label names via LLMNR */
618 !is_gateway_hostname(domain) && /* don't resolve "gateway" with LLMNR, let nss-myhostname handle this */
619 manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via LLMNR */
620 return DNS_SCOPE_YES_BASE + 1; /* Return +1, as we consider ourselves authoritative
621 * for single-label names, i.e. one label. This is
622 * particular relevant as it means a "." route on some
623 * other scope won't pull all traffic away from
624 * us. (If people actually want to pull traffic away
625 * from us they should turn off LLMNR on the
626 * link). Note that unicast DNS scopes with search
627 * domains also consider themselves authoritative for
628 * single-label domains, at the same preference (see
629 * above). */
630
631 return DNS_SCOPE_NO;
632 }
633
634 default:
635 assert_not_reached("Unknown scope protocol");
636 }
637 }
638
639 bool dns_scope_good_key(DnsScope *s, const DnsResourceKey *key) {
640 int key_family;
641
642 assert(s);
643 assert(key);
644
645 /* Check if it makes sense to resolve the specified key on
646 * this scope. Note that this call assumes as fully qualified
647 * name, i.e. the search suffixes already appended. */
648
649 if (key->class != DNS_CLASS_IN)
650 return false;
651
652 if (s->protocol == DNS_PROTOCOL_DNS) {
653
654 /* On classic DNS, looking up non-address RRs is always
655 * fine. (Specifically, we want to permit looking up
656 * DNSKEY and DS records on the root and top-level
657 * domains.) */
658 if (!dns_resource_key_is_address(key))
659 return true;
660
661 /* However, we refuse to look up A and AAAA RRs on the
662 * root and single-label domains, under the assumption
663 * that those should be resolved via LLMNR or search
664 * path only, and should not be leaked onto the
665 * internet. */
666 return !(dns_name_is_single_label(dns_resource_key_name(key)) ||
667 dns_name_is_root(dns_resource_key_name(key)));
668 }
669
670 /* On mDNS and LLMNR, send A and AAAA queries only on the
671 * respective scopes */
672
673 key_family = dns_type_to_af(key->type);
674 if (key_family < 0)
675 return true;
676
677 return key_family == s->family;
678 }
679
680 static int dns_scope_multicast_membership(DnsScope *s, bool b, struct in_addr in, struct in6_addr in6) {
681 int fd;
682
683 assert(s);
684 assert(s->link);
685
686 if (s->family == AF_INET) {
687 struct ip_mreqn mreqn = {
688 .imr_multiaddr = in,
689 .imr_ifindex = s->link->ifindex,
690 };
691
692 if (s->protocol == DNS_PROTOCOL_LLMNR)
693 fd = manager_llmnr_ipv4_udp_fd(s->manager);
694 else
695 fd = manager_mdns_ipv4_fd(s->manager);
696
697 if (fd < 0)
698 return fd;
699
700 /* Always first try to drop membership before we add
701 * one. This is necessary on some devices, such as
702 * veth. */
703 if (b)
704 (void) setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn));
705
706 if (setsockopt(fd, IPPROTO_IP, b ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)) < 0)
707 return -errno;
708
709 } else if (s->family == AF_INET6) {
710 struct ipv6_mreq mreq = {
711 .ipv6mr_multiaddr = in6,
712 .ipv6mr_interface = s->link->ifindex,
713 };
714
715 if (s->protocol == DNS_PROTOCOL_LLMNR)
716 fd = manager_llmnr_ipv6_udp_fd(s->manager);
717 else
718 fd = manager_mdns_ipv6_fd(s->manager);
719
720 if (fd < 0)
721 return fd;
722
723 if (b)
724 (void) setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
725
726 if (setsockopt(fd, IPPROTO_IPV6, b ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
727 return -errno;
728 } else
729 return -EAFNOSUPPORT;
730
731 return 0;
732 }
733
734 int dns_scope_llmnr_membership(DnsScope *s, bool b) {
735 assert(s);
736
737 if (s->protocol != DNS_PROTOCOL_LLMNR)
738 return 0;
739
740 return dns_scope_multicast_membership(s, b, LLMNR_MULTICAST_IPV4_ADDRESS, LLMNR_MULTICAST_IPV6_ADDRESS);
741 }
742
743 int dns_scope_mdns_membership(DnsScope *s, bool b) {
744 assert(s);
745
746 if (s->protocol != DNS_PROTOCOL_MDNS)
747 return 0;
748
749 return dns_scope_multicast_membership(s, b, MDNS_MULTICAST_IPV4_ADDRESS, MDNS_MULTICAST_IPV6_ADDRESS);
750 }
751
752 int dns_scope_make_reply_packet(
753 DnsScope *s,
754 uint16_t id,
755 int rcode,
756 DnsQuestion *q,
757 DnsAnswer *answer,
758 DnsAnswer *soa,
759 bool tentative,
760 DnsPacket **ret) {
761
762 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
763 int r;
764
765 assert(s);
766 assert(ret);
767
768 if (dns_question_isempty(q) &&
769 dns_answer_isempty(answer) &&
770 dns_answer_isempty(soa))
771 return -EINVAL;
772
773 r = dns_packet_new(&p, s->protocol, 0, DNS_PACKET_SIZE_MAX);
774 if (r < 0)
775 return r;
776
777 DNS_PACKET_HEADER(p)->id = id;
778 DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
779 1 /* qr */,
780 0 /* opcode */,
781 0 /* c */,
782 0 /* tc */,
783 tentative,
784 0 /* (ra) */,
785 0 /* (ad) */,
786 0 /* (cd) */,
787 rcode));
788
789 r = dns_packet_append_question(p, q);
790 if (r < 0)
791 return r;
792 DNS_PACKET_HEADER(p)->qdcount = htobe16(dns_question_size(q));
793
794 r = dns_packet_append_answer(p, answer);
795 if (r < 0)
796 return r;
797 DNS_PACKET_HEADER(p)->ancount = htobe16(dns_answer_size(answer));
798
799 r = dns_packet_append_answer(p, soa);
800 if (r < 0)
801 return r;
802 DNS_PACKET_HEADER(p)->arcount = htobe16(dns_answer_size(soa));
803
804 *ret = TAKE_PTR(p);
805
806 return 0;
807 }
808
809 static void dns_scope_verify_conflicts(DnsScope *s, DnsPacket *p) {
810 DnsResourceRecord *rr;
811 DnsResourceKey *key;
812
813 assert(s);
814 assert(p);
815
816 DNS_QUESTION_FOREACH(key, p->question)
817 dns_zone_verify_conflicts(&s->zone, key);
818
819 DNS_ANSWER_FOREACH(rr, p->answer)
820 dns_zone_verify_conflicts(&s->zone, rr->key);
821 }
822
823 void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) {
824 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
825 _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL;
826 DnsResourceKey *key = NULL;
827 bool tentative = false;
828 int r;
829
830 assert(s);
831 assert(p);
832
833 if (p->protocol != DNS_PROTOCOL_LLMNR)
834 return;
835
836 if (p->ipproto == IPPROTO_UDP) {
837 /* Don't accept UDP queries directed to anything but
838 * the LLMNR multicast addresses. See RFC 4795,
839 * section 2.5. */
840
841 if (p->family == AF_INET && !in_addr_equal(AF_INET, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV4_ADDRESS))
842 return;
843
844 if (p->family == AF_INET6 && !in_addr_equal(AF_INET6, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV6_ADDRESS))
845 return;
846 }
847
848 r = dns_packet_extract(p);
849 if (r < 0) {
850 log_debug_errno(r, "Failed to extract resource records from incoming packet: %m");
851 return;
852 }
853
854 if (DNS_PACKET_LLMNR_C(p)) {
855 /* Somebody notified us about a possible conflict */
856 dns_scope_verify_conflicts(s, p);
857 return;
858 }
859
860 assert(dns_question_size(p->question) == 1);
861 key = p->question->keys[0];
862
863 r = dns_zone_lookup(&s->zone, key, 0, &answer, &soa, &tentative);
864 if (r < 0) {
865 log_debug_errno(r, "Failed to look up key: %m");
866 return;
867 }
868 if (r == 0)
869 return;
870
871 if (answer)
872 dns_answer_order_by_scope(answer, in_addr_is_link_local(p->family, &p->sender) > 0);
873
874 r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, p->question, answer, soa, tentative, &reply);
875 if (r < 0) {
876 log_debug_errno(r, "Failed to build reply packet: %m");
877 return;
878 }
879
880 if (stream) {
881 r = dns_stream_write_packet(stream, reply);
882 if (r < 0) {
883 log_debug_errno(r, "Failed to enqueue reply packet: %m");
884 return;
885 }
886
887 /* Let's take an extra reference on this stream, so that it stays around after returning. The reference
888 * will be dangling until the stream is disconnected, and the default completion handler of the stream
889 * will then unref the stream and destroy it */
890 if (DNS_STREAM_QUEUED(stream))
891 dns_stream_ref(stream);
892 } else {
893 int fd;
894
895 if (!ratelimit_below(&s->ratelimit))
896 return;
897
898 if (p->family == AF_INET)
899 fd = manager_llmnr_ipv4_udp_fd(s->manager);
900 else if (p->family == AF_INET6)
901 fd = manager_llmnr_ipv6_udp_fd(s->manager);
902 else {
903 log_debug("Unknown protocol");
904 return;
905 }
906 if (fd < 0) {
907 log_debug_errno(fd, "Failed to get reply socket: %m");
908 return;
909 }
910
911 /* Note that we always immediately reply to all LLMNR
912 * requests, and do not wait any time, since we
913 * verified uniqueness for all records. Also see RFC
914 * 4795, Section 2.7 */
915
916 r = manager_send(s->manager, fd, p->ifindex, p->family, &p->sender, p->sender_port, NULL, reply);
917 if (r < 0) {
918 log_debug_errno(r, "Failed to send reply packet: %m");
919 return;
920 }
921 }
922 }
923
924 DnsTransaction *dns_scope_find_transaction(DnsScope *scope, DnsResourceKey *key, bool cache_ok) {
925 DnsTransaction *t;
926
927 assert(scope);
928 assert(key);
929
930 /* Try to find an ongoing transaction that is a equal to the
931 * specified question */
932 t = hashmap_get(scope->transactions_by_key, key);
933 if (!t)
934 return NULL;
935
936 /* Refuse reusing transactions that completed based on cached
937 * data instead of a real packet, if that's requested. */
938 if (!cache_ok &&
939 IN_SET(t->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_RCODE_FAILURE) &&
940 t->answer_source != DNS_TRANSACTION_NETWORK)
941 return NULL;
942
943 return t;
944 }
945
946 static int dns_scope_make_conflict_packet(
947 DnsScope *s,
948 DnsResourceRecord *rr,
949 DnsPacket **ret) {
950
951 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
952 int r;
953
954 assert(s);
955 assert(rr);
956 assert(ret);
957
958 r = dns_packet_new(&p, s->protocol, 0, DNS_PACKET_SIZE_MAX);
959 if (r < 0)
960 return r;
961
962 DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
963 0 /* qr */,
964 0 /* opcode */,
965 1 /* conflict */,
966 0 /* tc */,
967 0 /* t */,
968 0 /* (ra) */,
969 0 /* (ad) */,
970 0 /* (cd) */,
971 0));
972
973 /* For mDNS, the transaction ID should always be 0 */
974 if (s->protocol != DNS_PROTOCOL_MDNS)
975 random_bytes(&DNS_PACKET_HEADER(p)->id, sizeof(uint16_t));
976
977 DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
978 DNS_PACKET_HEADER(p)->arcount = htobe16(1);
979
980 r = dns_packet_append_key(p, rr->key, 0, NULL);
981 if (r < 0)
982 return r;
983
984 r = dns_packet_append_rr(p, rr, 0, NULL, NULL);
985 if (r < 0)
986 return r;
987
988 *ret = TAKE_PTR(p);
989
990 return 0;
991 }
992
993 static int on_conflict_dispatch(sd_event_source *es, usec_t usec, void *userdata) {
994 DnsScope *scope = userdata;
995 int r;
996
997 assert(es);
998 assert(scope);
999
1000 scope->conflict_event_source = sd_event_source_unref(scope->conflict_event_source);
1001
1002 for (;;) {
1003 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1004 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1005 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1006
1007 key = ordered_hashmap_first_key(scope->conflict_queue);
1008 if (!key)
1009 break;
1010
1011 rr = ordered_hashmap_remove(scope->conflict_queue, key);
1012 assert(rr);
1013
1014 r = dns_scope_make_conflict_packet(scope, rr, &p);
1015 if (r < 0) {
1016 log_error_errno(r, "Failed to make conflict packet: %m");
1017 return 0;
1018 }
1019
1020 r = dns_scope_emit_udp(scope, -1, p);
1021 if (r < 0)
1022 log_debug_errno(r, "Failed to send conflict packet: %m");
1023 }
1024
1025 return 0;
1026 }
1027
1028 int dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) {
1029 usec_t jitter;
1030 int r;
1031
1032 assert(scope);
1033 assert(rr);
1034
1035 /* We don't send these queries immediately. Instead, we queue
1036 * them, and send them after some jitter delay. */
1037 r = ordered_hashmap_ensure_allocated(&scope->conflict_queue, &dns_resource_key_hash_ops);
1038 if (r < 0) {
1039 log_oom();
1040 return r;
1041 }
1042
1043 /* We only place one RR per key in the conflict
1044 * messages, not all of them. That should be enough to
1045 * indicate where there might be a conflict */
1046 r = ordered_hashmap_put(scope->conflict_queue, rr->key, rr);
1047 if (IN_SET(r, 0, -EEXIST))
1048 return 0;
1049 if (r < 0)
1050 return log_debug_errno(r, "Failed to queue conflicting RR: %m");
1051
1052 dns_resource_key_ref(rr->key);
1053 dns_resource_record_ref(rr);
1054
1055 if (scope->conflict_event_source)
1056 return 0;
1057
1058 random_bytes(&jitter, sizeof(jitter));
1059 jitter %= LLMNR_JITTER_INTERVAL_USEC;
1060
1061 r = sd_event_add_time(scope->manager->event,
1062 &scope->conflict_event_source,
1063 clock_boottime_or_monotonic(),
1064 now(clock_boottime_or_monotonic()) + jitter,
1065 LLMNR_JITTER_INTERVAL_USEC,
1066 on_conflict_dispatch, scope);
1067 if (r < 0)
1068 return log_debug_errno(r, "Failed to add conflict dispatch event: %m");
1069
1070 (void) sd_event_source_set_description(scope->conflict_event_source, "scope-conflict");
1071
1072 return 0;
1073 }
1074
1075 void dns_scope_check_conflicts(DnsScope *scope, DnsPacket *p) {
1076 DnsResourceRecord *rr;
1077 int r;
1078
1079 assert(scope);
1080 assert(p);
1081
1082 if (!IN_SET(p->protocol, DNS_PROTOCOL_LLMNR, DNS_PROTOCOL_MDNS))
1083 return;
1084
1085 if (DNS_PACKET_RRCOUNT(p) <= 0)
1086 return;
1087
1088 if (p->protocol == DNS_PROTOCOL_LLMNR) {
1089 if (DNS_PACKET_LLMNR_C(p) != 0)
1090 return;
1091
1092 if (DNS_PACKET_LLMNR_T(p) != 0)
1093 return;
1094 }
1095
1096 if (manager_our_packet(scope->manager, p))
1097 return;
1098
1099 r = dns_packet_extract(p);
1100 if (r < 0) {
1101 log_debug_errno(r, "Failed to extract packet: %m");
1102 return;
1103 }
1104
1105 log_debug("Checking for conflicts...");
1106
1107 DNS_ANSWER_FOREACH(rr, p->answer) {
1108 /* No conflict if it is DNS-SD RR used for service enumeration. */
1109 if (dns_resource_key_is_dnssd_ptr(rr->key))
1110 continue;
1111
1112 /* Check for conflicts against the local zone. If we
1113 * found one, we won't check any further */
1114 r = dns_zone_check_conflicts(&scope->zone, rr);
1115 if (r != 0)
1116 continue;
1117
1118 /* Check for conflicts against the local cache. If so,
1119 * send out an advisory query, to inform everybody */
1120 r = dns_cache_check_conflicts(&scope->cache, rr, p->family, &p->sender);
1121 if (r <= 0)
1122 continue;
1123
1124 dns_scope_notify_conflict(scope, rr);
1125 }
1126 }
1127
1128 void dns_scope_dump(DnsScope *s, FILE *f) {
1129 assert(s);
1130
1131 if (!f)
1132 f = stdout;
1133
1134 fputs("[Scope protocol=", f);
1135 fputs(dns_protocol_to_string(s->protocol), f);
1136
1137 if (s->link) {
1138 fputs(" interface=", f);
1139 fputs(s->link->ifname, f);
1140 }
1141
1142 if (s->family != AF_UNSPEC) {
1143 fputs(" family=", f);
1144 fputs(af_to_name(s->family), f);
1145 }
1146
1147 fputs("]\n", f);
1148
1149 if (!dns_zone_is_empty(&s->zone)) {
1150 fputs("ZONE:\n", f);
1151 dns_zone_dump(&s->zone, f);
1152 }
1153
1154 if (!dns_cache_is_empty(&s->cache)) {
1155 fputs("CACHE:\n", f);
1156 dns_cache_dump(&s->cache, f);
1157 }
1158 }
1159
1160 DnsSearchDomain *dns_scope_get_search_domains(DnsScope *s) {
1161 assert(s);
1162
1163 if (s->protocol != DNS_PROTOCOL_DNS)
1164 return NULL;
1165
1166 if (s->link)
1167 return s->link->search_domains;
1168
1169 return s->manager->search_domains;
1170 }
1171
1172 bool dns_scope_name_needs_search_domain(DnsScope *s, const char *name) {
1173 assert(s);
1174
1175 if (s->protocol != DNS_PROTOCOL_DNS)
1176 return false;
1177
1178 return dns_name_is_single_label(name);
1179 }
1180
1181 bool dns_scope_network_good(DnsScope *s) {
1182 /* Checks whether the network is in good state for lookups on this scope. For mDNS/LLMNR/Classic DNS scopes
1183 * bound to links this is easy, as they don't even exist if the link isn't in a suitable state. For the global
1184 * DNS scope we check whether there are any links that are up and have an address. */
1185
1186 if (s->link)
1187 return true;
1188
1189 return manager_routable(s->manager, AF_UNSPEC);
1190 }
1191
1192 int dns_scope_ifindex(DnsScope *s) {
1193 assert(s);
1194
1195 if (s->link)
1196 return s->link->ifindex;
1197
1198 return 0;
1199 }
1200
1201 static int on_announcement_timeout(sd_event_source *s, usec_t usec, void *userdata) {
1202 DnsScope *scope = userdata;
1203
1204 assert(s);
1205
1206 scope->announce_event_source = sd_event_source_unref(scope->announce_event_source);
1207
1208 (void) dns_scope_announce(scope, false);
1209 return 0;
1210 }
1211
1212 int dns_scope_announce(DnsScope *scope, bool goodbye) {
1213 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
1214 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1215 _cleanup_set_free_ Set *types = NULL;
1216 DnsTransaction *t;
1217 DnsZoneItem *z, *i;
1218 unsigned size = 0;
1219 Iterator iterator;
1220 char *service_type;
1221 int r;
1222
1223 if (!scope)
1224 return 0;
1225
1226 if (scope->protocol != DNS_PROTOCOL_MDNS)
1227 return 0;
1228
1229 /* Check if we're done with probing. */
1230 LIST_FOREACH(transactions_by_scope, t, scope->transactions)
1231 if (DNS_TRANSACTION_IS_LIVE(t->state))
1232 return 0;
1233
1234 /* Check if there're services pending conflict resolution. */
1235 if (manager_next_dnssd_names(scope->manager))
1236 return 0; /* we reach this point only if changing hostname didn't help */
1237
1238 /* Calculate answer's size. */
1239 HASHMAP_FOREACH(z, scope->zone.by_key, iterator) {
1240 if (z->state != DNS_ZONE_ITEM_ESTABLISHED)
1241 continue;
1242
1243 if (z->rr->key->type == DNS_TYPE_PTR &&
1244 !dns_zone_contains_name(&scope->zone, z->rr->ptr.name)) {
1245 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
1246
1247 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));
1248 z->state = DNS_ZONE_ITEM_WITHDRAWN;
1249 continue;
1250 }
1251
1252 /* Collect service types for _services._dns-sd._udp.local RRs in a set */
1253 if (!scope->announced &&
1254 dns_resource_key_is_dnssd_ptr(z->rr->key)) {
1255 if (!set_contains(types, dns_resource_key_name(z->rr->key))) {
1256 r = set_ensure_allocated(&types, &dns_name_hash_ops);
1257 if (r < 0)
1258 return log_debug_errno(r, "Failed to allocate set: %m");
1259
1260 r = set_put(types, dns_resource_key_name(z->rr->key));
1261 if (r < 0)
1262 return log_debug_errno(r, "Failed to add item to set: %m");
1263 }
1264 }
1265
1266 LIST_FOREACH(by_key, i, z)
1267 size++;
1268 }
1269
1270 answer = dns_answer_new(size + set_size(types));
1271 if (!answer)
1272 return log_oom();
1273
1274 /* Second iteration, actually add RRs to the answer. */
1275 HASHMAP_FOREACH(z, scope->zone.by_key, iterator)
1276 LIST_FOREACH (by_key, i, z) {
1277 DnsAnswerFlags flags;
1278
1279 if (i->state != DNS_ZONE_ITEM_ESTABLISHED)
1280 continue;
1281
1282 if (dns_resource_key_is_dnssd_ptr(i->rr->key))
1283 flags = goodbye ? DNS_ANSWER_GOODBYE : 0;
1284 else
1285 flags = goodbye ? (DNS_ANSWER_GOODBYE|DNS_ANSWER_CACHE_FLUSH) : DNS_ANSWER_CACHE_FLUSH;
1286
1287 r = dns_answer_add(answer, i->rr, 0 , flags);
1288 if (r < 0)
1289 return log_debug_errno(r, "Failed to add RR to announce: %m");
1290 }
1291
1292 /* Since all the active services are in the zone make them discoverable now. */
1293 SET_FOREACH(service_type, types, iterator) {
1294 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr;
1295
1296 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR,
1297 "_services._dns-sd._udp.local");
1298 rr->ptr.name = strdup(service_type);
1299 rr->ttl = MDNS_DEFAULT_TTL;
1300
1301 r = dns_zone_put(&scope->zone, scope, rr, false);
1302 if (r < 0)
1303 log_warning_errno(r, "Failed to add DNS-SD PTR record to MDNS zone: %m");
1304
1305 r = dns_answer_add(answer, rr, 0 , 0);
1306 if (r < 0)
1307 return log_debug_errno(r, "Failed to add RR to announce: %m");
1308 }
1309
1310 if (dns_answer_isempty(answer))
1311 return 0;
1312
1313 r = dns_scope_make_reply_packet(scope, 0, DNS_RCODE_SUCCESS, NULL, answer, NULL, false, &p);
1314 if (r < 0)
1315 return log_debug_errno(r, "Failed to build reply packet: %m");
1316
1317 r = dns_scope_emit_udp(scope, -1, p);
1318 if (r < 0)
1319 return log_debug_errno(r, "Failed to send reply packet: %m");
1320
1321 /* In section 8.3 of RFC6762: "The Multicast DNS responder MUST send at least two unsolicited
1322 * responses, one second apart." */
1323 if (!scope->announced) {
1324 usec_t ts;
1325
1326 scope->announced = true;
1327
1328 assert_se(sd_event_now(scope->manager->event, clock_boottime_or_monotonic(), &ts) >= 0);
1329 ts += MDNS_ANNOUNCE_DELAY;
1330
1331 r = sd_event_add_time(
1332 scope->manager->event,
1333 &scope->announce_event_source,
1334 clock_boottime_or_monotonic(),
1335 ts,
1336 MDNS_JITTER_RANGE_USEC,
1337 on_announcement_timeout, scope);
1338 if (r < 0)
1339 return log_debug_errno(r, "Failed to schedule second announcement: %m");
1340
1341 (void) sd_event_source_set_description(scope->announce_event_source, "mdns-announce");
1342 }
1343
1344 return 0;
1345 }
1346
1347 int dns_scope_add_dnssd_services(DnsScope *scope) {
1348 Iterator i;
1349 DnssdService *service;
1350 DnssdTxtData *txt_data;
1351 int r;
1352
1353 assert(scope);
1354
1355 if (hashmap_size(scope->manager->dnssd_services) == 0)
1356 return 0;
1357
1358 scope->announced = false;
1359
1360 HASHMAP_FOREACH(service, scope->manager->dnssd_services, i) {
1361 service->withdrawn = false;
1362
1363 r = dns_zone_put(&scope->zone, scope, service->ptr_rr, false);
1364 if (r < 0)
1365 log_warning_errno(r, "Failed to add PTR record to MDNS zone: %m");
1366
1367 r = dns_zone_put(&scope->zone, scope, service->srv_rr, true);
1368 if (r < 0)
1369 log_warning_errno(r, "Failed to add SRV record to MDNS zone: %m");
1370
1371 LIST_FOREACH(items, txt_data, service->txt_data_items) {
1372 r = dns_zone_put(&scope->zone, scope, txt_data->rr, true);
1373 if (r < 0)
1374 log_warning_errno(r, "Failed to add TXT record to MDNS zone: %m");
1375 }
1376 }
1377
1378 return 0;
1379 }
1380
1381 int dns_scope_remove_dnssd_services(DnsScope *scope) {
1382 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1383 Iterator i;
1384 DnssdService *service;
1385 DnssdTxtData *txt_data;
1386 int r;
1387
1388 assert(scope);
1389
1390 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR,
1391 "_services._dns-sd._udp.local");
1392 if (!key)
1393 return log_oom();
1394
1395 r = dns_zone_remove_rrs_by_key(&scope->zone, key);
1396 if (r < 0)
1397 return r;
1398
1399 HASHMAP_FOREACH(service, scope->manager->dnssd_services, i) {
1400 dns_zone_remove_rr(&scope->zone, service->ptr_rr);
1401 dns_zone_remove_rr(&scope->zone, service->srv_rr);
1402 LIST_FOREACH(items, txt_data, service->txt_data_items)
1403 dns_zone_remove_rr(&scope->zone, txt_data->rr);
1404 }
1405
1406 return 0;
1407 }
1408
1409 static bool dns_scope_has_route_only_domains(DnsScope *scope) {
1410 DnsSearchDomain *domain, *first;
1411 bool route_only = false;
1412
1413 assert(scope);
1414 assert(scope->protocol == DNS_PROTOCOL_DNS);
1415
1416 /* Returns 'true' if this scope is suitable for queries to specific domains only. For that we check
1417 * if there are any route-only domains on this interface, as a heuristic to discern VPN-style links
1418 * from non-VPN-style links. Returns 'false' for all other cases, i.e. if the scope is intended to
1419 * take queries to arbitrary domains, i.e. has no routing domains set. */
1420
1421 if (scope->link)
1422 first = scope->link->search_domains;
1423 else
1424 first = scope->manager->search_domains;
1425
1426 LIST_FOREACH(domains, domain, first) {
1427 /* "." means "any domain", thus the interface takes any kind of traffic. Thus, we exit early
1428 * here, as it doesn't really matter whether this link has any route-only domains or not,
1429 * "~." really trumps everything and clearly indicates that this interface shall receive all
1430 * traffic it can get. */
1431 if (dns_name_is_root(DNS_SEARCH_DOMAIN_NAME(domain)))
1432 return false;
1433
1434 if (domain->route_only)
1435 route_only = true;
1436 }
1437
1438 return route_only;
1439 }
1440
1441 bool dns_scope_is_default_route(DnsScope *scope) {
1442 assert(scope);
1443
1444 /* Only use DNS scopes as default routes */
1445 if (scope->protocol != DNS_PROTOCOL_DNS)
1446 return false;
1447
1448 /* The global DNS scope is always suitable as default route */
1449 if (!scope->link)
1450 return true;
1451
1452 /* Honour whatever is explicitly configured. This is really the best approach, and trumps any
1453 * automatic logic. */
1454 if (scope->link->default_route >= 0)
1455 return scope->link->default_route;
1456
1457 /* Otherwise check if we have any route-only domains, as a sensible heuristic: if so, let's not
1458 * volunteer as default route. */
1459 return !dns_scope_has_route_only_domains(scope);
1460 }