]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-scope.c
tree-wide: use mfree more
[thirdparty/systemd.git] / src / resolve / resolved-dns-scope.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <netinet/tcp.h>
21
22 #include "af-list.h"
23 #include "alloc-util.h"
24 #include "dns-domain.h"
25 #include "fd-util.h"
26 #include "hostname-util.h"
27 #include "missing.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"
33 #include "strv.h"
34
35 #define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
36 #define MULTICAST_RATELIMIT_BURST 1000
37
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)
41
42 int dns_scope_new(Manager *m, DnsScope **ret, Link *l, DnsProtocol protocol, int family) {
43 DnsScope *s;
44
45 assert(m);
46 assert(ret);
47
48 s = new0(DnsScope, 1);
49 if (!s)
50 return -ENOMEM;
51
52 s->manager = m;
53 s->link = l;
54 s->protocol = protocol;
55 s->family = family;
56 s->resend_timeout = MULTICAST_RESEND_TIMEOUT_MIN_USEC;
57
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
63 * changes. */
64
65 if (l)
66 s->dnssec_mode = link_get_dnssec_mode(l);
67 else
68 s->dnssec_mode = manager_get_dnssec_mode(m);
69 } else
70 s->dnssec_mode = DNSSEC_NO;
71
72 LIST_PREPEND(scopes, m->dns_scopes, s);
73
74 dns_scope_llmnr_membership(s, true);
75 dns_scope_mdns_membership(s, true);
76
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));
78
79 /* Enforce ratelimiting for the multicast protocols */
80 RATELIMIT_INIT(s->ratelimit, MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST);
81
82 *ret = s;
83 return 0;
84 }
85
86 static void dns_scope_abort_transactions(DnsScope *s) {
87 assert(s);
88
89 while (s->transactions) {
90 DnsTransaction *t = s->transactions;
91
92 /* Abort the transaction, but make sure it is not
93 * freed while we still look at it */
94
95 t->block_gc++;
96 if (DNS_TRANSACTION_IS_LIVE(t->state))
97 dns_transaction_complete(t, DNS_TRANSACTION_ABORTED);
98 t->block_gc--;
99
100 dns_transaction_free(t);
101 }
102 }
103
104 DnsScope* dns_scope_free(DnsScope *s) {
105 DnsResourceRecord *rr;
106
107 if (!s)
108 return NULL;
109
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));
111
112 dns_scope_llmnr_membership(s, false);
113 dns_scope_mdns_membership(s, false);
114 dns_scope_abort_transactions(s);
115
116 while (s->query_candidates)
117 dns_query_candidate_free(s->query_candidates);
118
119 hashmap_free(s->transactions_by_key);
120
121 while ((rr = ordered_hashmap_steal_first(s->conflict_queue)))
122 dns_resource_record_unref(rr);
123
124 ordered_hashmap_free(s->conflict_queue);
125 sd_event_source_unref(s->conflict_event_source);
126
127 dns_cache_flush(&s->cache);
128 dns_zone_flush(&s->zone);
129
130 LIST_REMOVE(scopes, s->manager->dns_scopes, s);
131 return mfree(s);
132 }
133
134 DnsServer *dns_scope_get_dns_server(DnsScope *s) {
135 assert(s);
136
137 if (s->protocol != DNS_PROTOCOL_DNS)
138 return NULL;
139
140 if (s->link)
141 return link_get_dns_server(s->link);
142 else
143 return manager_get_dns_server(s->manager);
144 }
145
146 void dns_scope_next_dns_server(DnsScope *s) {
147 assert(s);
148
149 if (s->protocol != DNS_PROTOCOL_DNS)
150 return;
151
152 if (s->link)
153 link_next_dns_server(s->link);
154 else
155 manager_next_dns_server(s->manager);
156 }
157
158 void dns_scope_packet_received(DnsScope *s, usec_t rtt) {
159 assert(s);
160
161 if (rtt <= s->max_rtt)
162 return;
163
164 s->max_rtt = rtt;
165 s->resend_timeout = MIN(MAX(MULTICAST_RESEND_TIMEOUT_MIN_USEC, s->max_rtt * 2), MULTICAST_RESEND_TIMEOUT_MAX_USEC);
166 }
167
168 void dns_scope_packet_lost(DnsScope *s, usec_t usec) {
169 assert(s);
170
171 if (s->resend_timeout <= usec)
172 s->resend_timeout = MIN(s->resend_timeout * 2, MULTICAST_RESEND_TIMEOUT_MAX_USEC);
173 }
174
175 static int dns_scope_emit_one(DnsScope *s, int fd, DnsPacket *p) {
176 union in_addr_union addr;
177 int ifindex = 0, r;
178 int family;
179 uint32_t mtu;
180
181 assert(s);
182 assert(p);
183 assert(p->protocol == s->protocol);
184
185 if (s->link) {
186 mtu = s->link->mtu;
187 ifindex = s->link->ifindex;
188 } else
189 mtu = manager_find_mtu(s->manager);
190
191 switch (s->protocol) {
192
193 case DNS_PROTOCOL_DNS:
194 assert(fd >= 0);
195
196 if (DNS_PACKET_QDCOUNT(p) > 1)
197 return -EOPNOTSUPP;
198
199 if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
200 return -EMSGSIZE;
201
202 if (p->size + UDP_PACKET_HEADER_SIZE > mtu)
203 return -EMSGSIZE;
204
205 r = manager_write(s->manager, fd, p);
206 if (r < 0)
207 return r;
208
209 break;
210
211 case DNS_PROTOCOL_LLMNR:
212 assert(fd < 0);
213
214 if (DNS_PACKET_QDCOUNT(p) > 1)
215 return -EOPNOTSUPP;
216
217 if (!ratelimit_test(&s->ratelimit))
218 return -EBUSY;
219
220 family = s->family;
221
222 if (family == AF_INET) {
223 addr.in = LLMNR_MULTICAST_IPV4_ADDRESS;
224 fd = manager_llmnr_ipv4_udp_fd(s->manager);
225 } else if (family == AF_INET6) {
226 addr.in6 = LLMNR_MULTICAST_IPV6_ADDRESS;
227 fd = manager_llmnr_ipv6_udp_fd(s->manager);
228 } else
229 return -EAFNOSUPPORT;
230 if (fd < 0)
231 return fd;
232
233 r = manager_send(s->manager, fd, ifindex, family, &addr, LLMNR_PORT, NULL, p);
234 if (r < 0)
235 return r;
236
237 break;
238
239 case DNS_PROTOCOL_MDNS:
240 assert(fd < 0);
241
242 if (!ratelimit_test(&s->ratelimit))
243 return -EBUSY;
244
245 family = s->family;
246
247 if (family == AF_INET) {
248 addr.in = MDNS_MULTICAST_IPV4_ADDRESS;
249 fd = manager_mdns_ipv4_fd(s->manager);
250 } else if (family == AF_INET6) {
251 addr.in6 = MDNS_MULTICAST_IPV6_ADDRESS;
252 fd = manager_mdns_ipv6_fd(s->manager);
253 } else
254 return -EAFNOSUPPORT;
255 if (fd < 0)
256 return fd;
257
258 r = manager_send(s->manager, fd, ifindex, family, &addr, MDNS_PORT, NULL, p);
259 if (r < 0)
260 return r;
261
262 break;
263
264 default:
265 return -EAFNOSUPPORT;
266 }
267
268 return 1;
269 }
270
271 int dns_scope_emit_udp(DnsScope *s, int fd, DnsPacket *p) {
272 int r;
273
274 assert(s);
275 assert(p);
276 assert(p->protocol == s->protocol);
277 assert((s->protocol == DNS_PROTOCOL_DNS) == (fd >= 0));
278
279 do {
280 /* If there are multiple linked packets, set the TC bit in all but the last of them */
281 if (p->more) {
282 assert(p->protocol == DNS_PROTOCOL_MDNS);
283 dns_packet_set_flags(p, true, true);
284 }
285
286 r = dns_scope_emit_one(s, fd, p);
287 if (r < 0)
288 return r;
289
290 p = p->more;
291 } while (p);
292
293 return 0;
294 }
295
296 static int dns_scope_socket(
297 DnsScope *s,
298 int type,
299 int family,
300 const union in_addr_union *address,
301 DnsServer *server,
302 uint16_t port) {
303
304 _cleanup_close_ int fd = -1;
305 union sockaddr_union sa = {};
306 socklen_t salen;
307 static const int one = 1;
308 int ret, r, ifindex;
309
310 assert(s);
311
312 if (server) {
313 assert(family == AF_UNSPEC);
314 assert(!address);
315
316 ifindex = dns_server_ifindex(server);
317
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 = ifindex;
327 salen = sizeof(sa.in6);
328 } else
329 return -EAFNOSUPPORT;
330 } else {
331 assert(family != AF_UNSPEC);
332 assert(address);
333
334 sa.sa.sa_family = family;
335 ifindex = s->link ? s->link->ifindex : 0;
336
337 if (family == AF_INET) {
338 sa.in.sin_port = htobe16(port);
339 sa.in.sin_addr = address->in;
340 salen = sizeof(sa.in);
341 } else if (family == AF_INET6) {
342 sa.in6.sin6_port = htobe16(port);
343 sa.in6.sin6_addr = address->in6;
344 sa.in6.sin6_scope_id = ifindex;
345 salen = sizeof(sa.in6);
346 } else
347 return -EAFNOSUPPORT;
348 }
349
350 fd = socket(sa.sa.sa_family, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
351 if (fd < 0)
352 return -errno;
353
354 if (type == SOCK_STREAM) {
355 r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
356 if (r < 0)
357 return -errno;
358 }
359
360 if (s->link) {
361 be32_t ifindex_be = htobe32(ifindex);
362
363 if (sa.sa.sa_family == AF_INET) {
364 r = setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be));
365 if (r < 0)
366 return -errno;
367 } else if (sa.sa.sa_family == AF_INET6) {
368 r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be));
369 if (r < 0)
370 return -errno;
371 }
372 }
373
374 if (s->protocol == DNS_PROTOCOL_LLMNR) {
375 /* RFC 4795, section 2.5 requires the TTL to be set to 1 */
376
377 if (sa.sa.sa_family == AF_INET) {
378 r = setsockopt(fd, IPPROTO_IP, IP_TTL, &one, sizeof(one));
379 if (r < 0)
380 return -errno;
381 } else if (sa.sa.sa_family == AF_INET6) {
382 r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &one, sizeof(one));
383 if (r < 0)
384 return -errno;
385 }
386 }
387
388 r = connect(fd, &sa.sa, salen);
389 if (r < 0 && errno != EINPROGRESS)
390 return -errno;
391
392 ret = fd;
393 fd = -1;
394
395 return ret;
396 }
397
398 int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) {
399 return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, server, port);
400 }
401
402 int dns_scope_socket_tcp(DnsScope *s, int family, const union in_addr_union *address, DnsServer *server, uint16_t port) {
403 return dns_scope_socket(s, SOCK_STREAM, family, address, server, port);
404 }
405
406 DnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, const char *domain) {
407 DnsSearchDomain *d;
408 DnsServer *dns_server;
409
410 assert(s);
411 assert(domain);
412
413 /* Checks if the specified domain is something to look up on
414 * this scope. Note that this accepts non-qualified hostnames,
415 * i.e. those without any search path prefixed yet. */
416
417 if (ifindex != 0 && (!s->link || s->link->ifindex != ifindex))
418 return DNS_SCOPE_NO;
419
420 if ((SD_RESOLVED_FLAGS_MAKE(s->protocol, s->family, 0) & flags) == 0)
421 return DNS_SCOPE_NO;
422
423 /* Never resolve any loopback hostname or IP address via DNS,
424 * LLMNR or mDNS. Instead, always rely on synthesized RRs for
425 * these. */
426 if (is_localhost(domain) ||
427 dns_name_endswith(domain, "127.in-addr.arpa") > 0 ||
428 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 return DNS_SCOPE_NO;
430
431 /* Never respond to some of the domains listed in RFC6303 */
432 if (dns_name_endswith(domain, "0.in-addr.arpa") > 0 ||
433 dns_name_equal(domain, "255.255.255.255.in-addr.arpa") > 0 ||
434 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 return DNS_SCOPE_NO;
436
437 /* Never respond to some of the domains listed in RFC6761 */
438 if (dns_name_endswith(domain, "invalid") > 0)
439 return DNS_SCOPE_NO;
440
441 /* Always honour search domains for routing queries. Note that
442 * we return DNS_SCOPE_YES here, rather than just
443 * DNS_SCOPE_MAYBE, which means wildcard scopes won't be
444 * considered anymore. */
445 LIST_FOREACH(domains, d, dns_scope_get_search_domains(s))
446 if (dns_name_endswith(domain, d->name) > 0)
447 return DNS_SCOPE_YES;
448
449 /* If the DNS server has route-only domains, don't send other requests
450 * to it. This would be a privacy violation, will most probably fail
451 * anyway, and adds unnecessary load. */
452 dns_server = dns_scope_get_dns_server(s);
453 if (dns_server && dns_server_limited_domains(dns_server))
454 return DNS_SCOPE_NO;
455
456 switch (s->protocol) {
457
458 case DNS_PROTOCOL_DNS:
459
460 /* Exclude link-local IP ranges */
461 if (dns_name_endswith(domain, "254.169.in-addr.arpa") == 0 &&
462 dns_name_endswith(domain, "8.e.f.ip6.arpa") == 0 &&
463 dns_name_endswith(domain, "9.e.f.ip6.arpa") == 0 &&
464 dns_name_endswith(domain, "a.e.f.ip6.arpa") == 0 &&
465 dns_name_endswith(domain, "b.e.f.ip6.arpa") == 0 &&
466 /* If networks use .local in their private setups, they are supposed to also add .local to their search
467 * domains, which we already checked above. Otherwise, we consider .local specific to mDNS and won't
468 * send such queries ordinary DNS servers. */
469 dns_name_endswith(domain, "local") == 0)
470 return DNS_SCOPE_MAYBE;
471
472 return DNS_SCOPE_NO;
473
474 case DNS_PROTOCOL_MDNS:
475 if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
476 (s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) ||
477 (dns_name_endswith(domain, "local") > 0 && /* only resolve names ending in .local via mDNS */
478 dns_name_equal(domain, "local") == 0 && /* but not the single-label "local" name itself */
479 manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via mDNS */
480 return DNS_SCOPE_MAYBE;
481
482 return DNS_SCOPE_NO;
483
484 case DNS_PROTOCOL_LLMNR:
485 if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
486 (s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) ||
487 (dns_name_is_single_label(domain) && /* only resolve single label names via LLMNR */
488 !is_gateway_hostname(domain) && /* don't resolve "gateway" with LLMNR, let nss-myhostname handle this */
489 manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via LLMNR */
490 return DNS_SCOPE_MAYBE;
491
492 return DNS_SCOPE_NO;
493
494 default:
495 assert_not_reached("Unknown scope protocol");
496 }
497 }
498
499 bool dns_scope_good_key(DnsScope *s, const DnsResourceKey *key) {
500 int key_family;
501
502 assert(s);
503 assert(key);
504
505 /* Check if it makes sense to resolve the specified key on
506 * this scope. Note that this call assumes as fully qualified
507 * name, i.e. the search suffixes already appended. */
508
509 if (key->class != DNS_CLASS_IN)
510 return false;
511
512 if (s->protocol == DNS_PROTOCOL_DNS) {
513
514 /* On classic DNS, looking up non-address RRs is always
515 * fine. (Specifically, we want to permit looking up
516 * DNSKEY and DS records on the root and top-level
517 * domains.) */
518 if (!dns_resource_key_is_address(key))
519 return true;
520
521 /* However, we refuse to look up A and AAAA RRs on the
522 * root and single-label domains, under the assumption
523 * that those should be resolved via LLMNR or search
524 * path only, and should not be leaked onto the
525 * internet. */
526 return !(dns_name_is_single_label(dns_resource_key_name(key)) ||
527 dns_name_is_root(dns_resource_key_name(key)));
528 }
529
530 /* On mDNS and LLMNR, send A and AAAA queries only on the
531 * respective scopes */
532
533 key_family = dns_type_to_af(key->type);
534 if (key_family < 0)
535 return true;
536
537 return key_family == s->family;
538 }
539
540 static int dns_scope_multicast_membership(DnsScope *s, bool b, struct in_addr in, struct in6_addr in6) {
541 int fd;
542
543 assert(s);
544 assert(s->link);
545
546 if (s->family == AF_INET) {
547 struct ip_mreqn mreqn = {
548 .imr_multiaddr = in,
549 .imr_ifindex = s->link->ifindex,
550 };
551
552 fd = manager_llmnr_ipv4_udp_fd(s->manager);
553 if (fd < 0)
554 return fd;
555
556 /* Always first try to drop membership before we add
557 * one. This is necessary on some devices, such as
558 * veth. */
559 if (b)
560 (void) setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn));
561
562 if (setsockopt(fd, IPPROTO_IP, b ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)) < 0)
563 return -errno;
564
565 } else if (s->family == AF_INET6) {
566 struct ipv6_mreq mreq = {
567 .ipv6mr_multiaddr = in6,
568 .ipv6mr_interface = s->link->ifindex,
569 };
570
571 fd = manager_llmnr_ipv6_udp_fd(s->manager);
572 if (fd < 0)
573 return fd;
574
575 if (b)
576 (void) setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
577
578 if (setsockopt(fd, IPPROTO_IPV6, b ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
579 return -errno;
580 } else
581 return -EAFNOSUPPORT;
582
583 return 0;
584 }
585
586 int dns_scope_llmnr_membership(DnsScope *s, bool b) {
587 assert(s);
588
589 if (s->protocol != DNS_PROTOCOL_LLMNR)
590 return 0;
591
592 return dns_scope_multicast_membership(s, b, LLMNR_MULTICAST_IPV4_ADDRESS, LLMNR_MULTICAST_IPV6_ADDRESS);
593 }
594
595 int dns_scope_mdns_membership(DnsScope *s, bool b) {
596 assert(s);
597
598 if (s->protocol != DNS_PROTOCOL_MDNS)
599 return 0;
600
601 return dns_scope_multicast_membership(s, b, MDNS_MULTICAST_IPV4_ADDRESS, MDNS_MULTICAST_IPV6_ADDRESS);
602 }
603
604 static int dns_scope_make_reply_packet(
605 DnsScope *s,
606 uint16_t id,
607 int rcode,
608 DnsQuestion *q,
609 DnsAnswer *answer,
610 DnsAnswer *soa,
611 bool tentative,
612 DnsPacket **ret) {
613
614 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
615 int r;
616
617 assert(s);
618 assert(ret);
619
620 if (dns_question_isempty(q) &&
621 dns_answer_isempty(answer) &&
622 dns_answer_isempty(soa))
623 return -EINVAL;
624
625 r = dns_packet_new(&p, s->protocol, 0);
626 if (r < 0)
627 return r;
628
629 DNS_PACKET_HEADER(p)->id = id;
630 DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
631 1 /* qr */,
632 0 /* opcode */,
633 0 /* c */,
634 0 /* tc */,
635 tentative,
636 0 /* (ra) */,
637 0 /* (ad) */,
638 0 /* (cd) */,
639 rcode));
640
641 r = dns_packet_append_question(p, q);
642 if (r < 0)
643 return r;
644 DNS_PACKET_HEADER(p)->qdcount = htobe16(dns_question_size(q));
645
646 r = dns_packet_append_answer(p, answer);
647 if (r < 0)
648 return r;
649 DNS_PACKET_HEADER(p)->ancount = htobe16(dns_answer_size(answer));
650
651 r = dns_packet_append_answer(p, soa);
652 if (r < 0)
653 return r;
654 DNS_PACKET_HEADER(p)->arcount = htobe16(dns_answer_size(soa));
655
656 *ret = p;
657 p = NULL;
658
659 return 0;
660 }
661
662 static void dns_scope_verify_conflicts(DnsScope *s, DnsPacket *p) {
663 DnsResourceRecord *rr;
664 DnsResourceKey *key;
665
666 assert(s);
667 assert(p);
668
669 DNS_QUESTION_FOREACH(key, p->question)
670 dns_zone_verify_conflicts(&s->zone, key);
671
672 DNS_ANSWER_FOREACH(rr, p->answer)
673 dns_zone_verify_conflicts(&s->zone, rr->key);
674 }
675
676 void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) {
677 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
678 _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL;
679 DnsResourceKey *key = NULL;
680 bool tentative = false;
681 int r;
682
683 assert(s);
684 assert(p);
685
686 if (p->protocol != DNS_PROTOCOL_LLMNR)
687 return;
688
689 if (p->ipproto == IPPROTO_UDP) {
690 /* Don't accept UDP queries directed to anything but
691 * the LLMNR multicast addresses. See RFC 4795,
692 * section 2.5. */
693
694 if (p->family == AF_INET && !in_addr_equal(AF_INET, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV4_ADDRESS))
695 return;
696
697 if (p->family == AF_INET6 && !in_addr_equal(AF_INET6, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV6_ADDRESS))
698 return;
699 }
700
701 r = dns_packet_extract(p);
702 if (r < 0) {
703 log_debug_errno(r, "Failed to extract resource records from incoming packet: %m");
704 return;
705 }
706
707 if (DNS_PACKET_LLMNR_C(p)) {
708 /* Somebody notified us about a possible conflict */
709 dns_scope_verify_conflicts(s, p);
710 return;
711 }
712
713 assert(dns_question_size(p->question) == 1);
714 key = p->question->keys[0];
715
716 r = dns_zone_lookup(&s->zone, key, 0, &answer, &soa, &tentative);
717 if (r < 0) {
718 log_debug_errno(r, "Failed to lookup key: %m");
719 return;
720 }
721 if (r == 0)
722 return;
723
724 if (answer)
725 dns_answer_order_by_scope(answer, in_addr_is_link_local(p->family, &p->sender) > 0);
726
727 r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, p->question, answer, soa, tentative, &reply);
728 if (r < 0) {
729 log_debug_errno(r, "Failed to build reply packet: %m");
730 return;
731 }
732
733 if (stream) {
734 r = dns_stream_write_packet(stream, reply);
735 if (r < 0) {
736 log_debug_errno(r, "Failed to enqueue reply packet: %m");
737 return;
738 }
739
740 /* Let's take an extra reference on this stream, so that it stays around after returning. The reference
741 * will be dangling until the stream is disconnected, and the default completion handler of the stream
742 * will then unref the stream and destroy it */
743 if (DNS_STREAM_QUEUED(stream))
744 dns_stream_ref(stream);
745 } else {
746 int fd;
747
748 if (!ratelimit_test(&s->ratelimit))
749 return;
750
751 if (p->family == AF_INET)
752 fd = manager_llmnr_ipv4_udp_fd(s->manager);
753 else if (p->family == AF_INET6)
754 fd = manager_llmnr_ipv6_udp_fd(s->manager);
755 else {
756 log_debug("Unknown protocol");
757 return;
758 }
759 if (fd < 0) {
760 log_debug_errno(fd, "Failed to get reply socket: %m");
761 return;
762 }
763
764 /* Note that we always immediately reply to all LLMNR
765 * requests, and do not wait any time, since we
766 * verified uniqueness for all records. Also see RFC
767 * 4795, Section 2.7 */
768
769 r = manager_send(s->manager, fd, p->ifindex, p->family, &p->sender, p->sender_port, NULL, reply);
770 if (r < 0) {
771 log_debug_errno(r, "Failed to send reply packet: %m");
772 return;
773 }
774 }
775 }
776
777 DnsTransaction *dns_scope_find_transaction(DnsScope *scope, DnsResourceKey *key, bool cache_ok) {
778 DnsTransaction *t;
779
780 assert(scope);
781 assert(key);
782
783 /* Try to find an ongoing transaction that is a equal to the
784 * specified question */
785 t = hashmap_get(scope->transactions_by_key, key);
786 if (!t)
787 return NULL;
788
789 /* Refuse reusing transactions that completed based on cached
790 * data instead of a real packet, if that's requested. */
791 if (!cache_ok &&
792 IN_SET(t->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_RCODE_FAILURE) &&
793 t->answer_source != DNS_TRANSACTION_NETWORK)
794 return NULL;
795
796 return t;
797 }
798
799 static int dns_scope_make_conflict_packet(
800 DnsScope *s,
801 DnsResourceRecord *rr,
802 DnsPacket **ret) {
803
804 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
805 int r;
806
807 assert(s);
808 assert(rr);
809 assert(ret);
810
811 r = dns_packet_new(&p, s->protocol, 0);
812 if (r < 0)
813 return r;
814
815 DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
816 0 /* qr */,
817 0 /* opcode */,
818 1 /* conflict */,
819 0 /* tc */,
820 0 /* t */,
821 0 /* (ra) */,
822 0 /* (ad) */,
823 0 /* (cd) */,
824 0));
825
826 /* For mDNS, the transaction ID should always be 0 */
827 if (s->protocol != DNS_PROTOCOL_MDNS)
828 random_bytes(&DNS_PACKET_HEADER(p)->id, sizeof(uint16_t));
829
830 DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
831 DNS_PACKET_HEADER(p)->arcount = htobe16(1);
832
833 r = dns_packet_append_key(p, rr->key, NULL);
834 if (r < 0)
835 return r;
836
837 r = dns_packet_append_rr(p, rr, NULL, NULL);
838 if (r < 0)
839 return r;
840
841 *ret = p;
842 p = NULL;
843
844 return 0;
845 }
846
847 static int on_conflict_dispatch(sd_event_source *es, usec_t usec, void *userdata) {
848 DnsScope *scope = userdata;
849 int r;
850
851 assert(es);
852 assert(scope);
853
854 scope->conflict_event_source = sd_event_source_unref(scope->conflict_event_source);
855
856 for (;;) {
857 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
858 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
859
860 rr = ordered_hashmap_steal_first(scope->conflict_queue);
861 if (!rr)
862 break;
863
864 r = dns_scope_make_conflict_packet(scope, rr, &p);
865 if (r < 0) {
866 log_error_errno(r, "Failed to make conflict packet: %m");
867 return 0;
868 }
869
870 r = dns_scope_emit_udp(scope, -1, p);
871 if (r < 0)
872 log_debug_errno(r, "Failed to send conflict packet: %m");
873 }
874
875 return 0;
876 }
877
878 int dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) {
879 usec_t jitter;
880 int r;
881
882 assert(scope);
883 assert(rr);
884
885 /* We don't send these queries immediately. Instead, we queue
886 * them, and send them after some jitter delay. */
887 r = ordered_hashmap_ensure_allocated(&scope->conflict_queue, &dns_resource_key_hash_ops);
888 if (r < 0) {
889 log_oom();
890 return r;
891 }
892
893 /* We only place one RR per key in the conflict
894 * messages, not all of them. That should be enough to
895 * indicate where there might be a conflict */
896 r = ordered_hashmap_put(scope->conflict_queue, rr->key, rr);
897 if (r == -EEXIST || r == 0)
898 return 0;
899 if (r < 0)
900 return log_debug_errno(r, "Failed to queue conflicting RR: %m");
901
902 dns_resource_record_ref(rr);
903
904 if (scope->conflict_event_source)
905 return 0;
906
907 random_bytes(&jitter, sizeof(jitter));
908 jitter %= LLMNR_JITTER_INTERVAL_USEC;
909
910 r = sd_event_add_time(scope->manager->event,
911 &scope->conflict_event_source,
912 clock_boottime_or_monotonic(),
913 now(clock_boottime_or_monotonic()) + jitter,
914 LLMNR_JITTER_INTERVAL_USEC,
915 on_conflict_dispatch, scope);
916 if (r < 0)
917 return log_debug_errno(r, "Failed to add conflict dispatch event: %m");
918
919 (void) sd_event_source_set_description(scope->conflict_event_source, "scope-conflict");
920
921 return 0;
922 }
923
924 void dns_scope_check_conflicts(DnsScope *scope, DnsPacket *p) {
925 unsigned i;
926 int r;
927
928 assert(scope);
929 assert(p);
930
931 if (p->protocol != DNS_PROTOCOL_LLMNR)
932 return;
933
934 if (DNS_PACKET_RRCOUNT(p) <= 0)
935 return;
936
937 if (DNS_PACKET_LLMNR_C(p) != 0)
938 return;
939
940 if (DNS_PACKET_LLMNR_T(p) != 0)
941 return;
942
943 if (manager_our_packet(scope->manager, p))
944 return;
945
946 r = dns_packet_extract(p);
947 if (r < 0) {
948 log_debug_errno(r, "Failed to extract packet: %m");
949 return;
950 }
951
952 log_debug("Checking for conflicts...");
953
954 for (i = 0; i < p->answer->n_rrs; i++) {
955
956 /* Check for conflicts against the local zone. If we
957 * found one, we won't check any further */
958 r = dns_zone_check_conflicts(&scope->zone, p->answer->items[i].rr);
959 if (r != 0)
960 continue;
961
962 /* Check for conflicts against the local cache. If so,
963 * send out an advisory query, to inform everybody */
964 r = dns_cache_check_conflicts(&scope->cache, p->answer->items[i].rr, p->family, &p->sender);
965 if (r <= 0)
966 continue;
967
968 dns_scope_notify_conflict(scope, p->answer->items[i].rr);
969 }
970 }
971
972 void dns_scope_dump(DnsScope *s, FILE *f) {
973 assert(s);
974
975 if (!f)
976 f = stdout;
977
978 fputs("[Scope protocol=", f);
979 fputs(dns_protocol_to_string(s->protocol), f);
980
981 if (s->link) {
982 fputs(" interface=", f);
983 fputs(s->link->name, f);
984 }
985
986 if (s->family != AF_UNSPEC) {
987 fputs(" family=", f);
988 fputs(af_to_name(s->family), f);
989 }
990
991 fputs("]\n", f);
992
993 if (!dns_zone_is_empty(&s->zone)) {
994 fputs("ZONE:\n", f);
995 dns_zone_dump(&s->zone, f);
996 }
997
998 if (!dns_cache_is_empty(&s->cache)) {
999 fputs("CACHE:\n", f);
1000 dns_cache_dump(&s->cache, f);
1001 }
1002 }
1003
1004 DnsSearchDomain *dns_scope_get_search_domains(DnsScope *s) {
1005 assert(s);
1006
1007 if (s->protocol != DNS_PROTOCOL_DNS)
1008 return NULL;
1009
1010 if (s->link)
1011 return s->link->search_domains;
1012
1013 return s->manager->search_domains;
1014 }
1015
1016 bool dns_scope_name_needs_search_domain(DnsScope *s, const char *name) {
1017 assert(s);
1018
1019 if (s->protocol != DNS_PROTOCOL_DNS)
1020 return false;
1021
1022 return dns_name_is_single_label(name);
1023 }
1024
1025 bool dns_scope_network_good(DnsScope *s) {
1026 /* Checks whether the network is in good state for lookups on this scope. For mDNS/LLMNR/Classic DNS scopes
1027 * bound to links this is easy, as they don't even exist if the link isn't in a suitable state. For the global
1028 * DNS scope we check whether there are any links that are up and have an address. */
1029
1030 if (s->link)
1031 return true;
1032
1033 return manager_routable(s->manager, AF_UNSPEC);
1034 }
1035
1036 int dns_scope_ifindex(DnsScope *s) {
1037 assert(s);
1038
1039 if (s->link)
1040 return s->link->ifindex;
1041
1042 return 0;
1043 }