]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-dns-scope.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / resolve / resolved-dns-scope.c
CommitLineData
74b2466e
LP
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
ad867662
LP
20#include <netinet/tcp.h>
21
46f08bea 22#include "af-list.h"
b5efdb8a 23#include "alloc-util.h"
4ad7f276 24#include "dns-domain.h"
3ffd4af2
LP
25#include "fd-util.h"
26#include "hostname-util.h"
27#include "missing.h"
28#include "random-util.h"
74b2466e 29#include "resolved-dns-scope.h"
3ffd4af2 30#include "resolved-llmnr.h"
b4f1862d 31#include "resolved-mdns.h"
3ffd4af2
LP
32#include "socket-util.h"
33#include "strv.h"
74b2466e 34
aea2429d
LP
35#define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
36#define MULTICAST_RATELIMIT_BURST 1000
37
9df3ba6c
TG
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
0dd25fb9 42int dns_scope_new(Manager *m, DnsScope **ret, Link *l, DnsProtocol protocol, int family) {
74b2466e
LP
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;
1716f6dc
LP
53 s->link = l;
54 s->protocol = protocol;
55 s->family = family;
9df3ba6c 56 s->resend_timeout = MULTICAST_RESEND_TIMEOUT_MIN_USEC;
74b2466e 57
ad6c0475
LP
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)
c69fa7e3
LP
66 s->dnssec_mode = link_get_dnssec_mode(l);
67 else
68 s->dnssec_mode = manager_get_dnssec_mode(m);
658f7f02
LP
69 } else
70 s->dnssec_mode = DNSSEC_NO;
ad6c0475 71
74b2466e
LP
72 LIST_PREPEND(scopes, m->dns_scopes, s);
73
1716f6dc 74 dns_scope_llmnr_membership(s, true);
0db4c90a 75 dns_scope_mdns_membership(s, true);
1716f6dc 76
46f08bea 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));
1716f6dc 78
aea2429d
LP
79 /* Enforce ratelimiting for the multicast protocols */
80 RATELIMIT_INIT(s->ratelimit, MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST);
81
74b2466e
LP
82 *ret = s;
83 return 0;
84}
85
801ad6a6 86static void dns_scope_abort_transactions(DnsScope *s) {
801ad6a6 87 assert(s);
1716f6dc 88
f9ebb22a
LP
89 while (s->transactions) {
90 DnsTransaction *t = s->transactions;
91
faa133f3
LP
92 /* Abort the transaction, but make sure it is not
93 * freed while we still look at it */
74b2466e 94
faa133f3 95 t->block_gc++;
f4e38037
LP
96 if (DNS_TRANSACTION_IS_LIVE(t->state))
97 dns_transaction_complete(t, DNS_TRANSACTION_ABORTED);
faa133f3 98 t->block_gc--;
74b2466e 99
ec2c5e43 100 dns_transaction_free(t);
74b2466e 101 }
801ad6a6
LP
102}
103
104DnsScope* 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);
0db4c90a 113 dns_scope_mdns_membership(s, false);
801ad6a6
LP
114 dns_scope_abort_transactions(s);
115
116 while (s->query_candidates)
117 dns_query_candidate_free(s->query_candidates);
74b2466e 118
f9ebb22a 119 hashmap_free(s->transactions_by_key);
da0c630e 120
1e43061b 121 while ((rr = ordered_hashmap_steal_first(s->conflict_queue)))
a4076574
LP
122 dns_resource_record_unref(rr);
123
1e43061b 124 ordered_hashmap_free(s->conflict_queue);
a4076574
LP
125 sd_event_source_unref(s->conflict_event_source);
126
322345fd 127 dns_cache_flush(&s->cache);
623a4c97 128 dns_zone_flush(&s->zone);
322345fd 129
74b2466e 130 LIST_REMOVE(scopes, s->manager->dns_scopes, s);
74b2466e
LP
131 free(s);
132
133 return NULL;
134}
135
2c27fbca 136DnsServer *dns_scope_get_dns_server(DnsScope *s) {
74b2466e
LP
137 assert(s);
138
1716f6dc
LP
139 if (s->protocol != DNS_PROTOCOL_DNS)
140 return NULL;
141
74b2466e
LP
142 if (s->link)
143 return link_get_dns_server(s->link);
144 else
145 return manager_get_dns_server(s->manager);
146}
147
148void dns_scope_next_dns_server(DnsScope *s) {
149 assert(s);
150
1716f6dc
LP
151 if (s->protocol != DNS_PROTOCOL_DNS)
152 return;
153
74b2466e
LP
154 if (s->link)
155 link_next_dns_server(s->link);
156 else
157 manager_next_dns_server(s->manager);
158}
159
9df3ba6c
TG
160void dns_scope_packet_received(DnsScope *s, usec_t rtt) {
161 assert(s);
162
84129d46
LP
163 if (rtt <= s->max_rtt)
164 return;
165
166 s->max_rtt = rtt;
167 s->resend_timeout = MIN(MAX(MULTICAST_RESEND_TIMEOUT_MIN_USEC, s->max_rtt * 2), MULTICAST_RESEND_TIMEOUT_MAX_USEC);
9df3ba6c
TG
168}
169
170void dns_scope_packet_lost(DnsScope *s, usec_t usec) {
171 assert(s);
172
173 if (s->resend_timeout <= usec)
174 s->resend_timeout = MIN(s->resend_timeout * 2, MULTICAST_RESEND_TIMEOUT_MAX_USEC);
175}
176
519ef046 177static int dns_scope_emit_one(DnsScope *s, int fd, DnsPacket *p) {
1716f6dc
LP
178 union in_addr_union addr;
179 int ifindex = 0, r;
0dd25fb9 180 int family;
1716f6dc 181 uint32_t mtu;
74b2466e
LP
182
183 assert(s);
184 assert(p);
1716f6dc 185 assert(p->protocol == s->protocol);
ad867662
LP
186
187 if (s->link) {
1716f6dc 188 mtu = s->link->mtu;
74b2466e 189 ifindex = s->link->ifindex;
1716f6dc 190 } else
e1c95994 191 mtu = manager_find_mtu(s->manager);
74b2466e 192
106784eb 193 switch (s->protocol) {
519ef046 194
106784eb 195 case DNS_PROTOCOL_DNS:
519ef046 196 assert(fd >= 0);
9c5e12a4 197
623a4c97 198 if (DNS_PACKET_QDCOUNT(p) > 1)
15411c0c 199 return -EOPNOTSUPP;
623a4c97 200
1716f6dc
LP
201 if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
202 return -EMSGSIZE;
203
a0166609 204 if (p->size + UDP_PACKET_HEADER_SIZE > mtu)
1716f6dc
LP
205 return -EMSGSIZE;
206
72290734
TG
207 r = manager_write(s->manager, fd, p);
208 if (r < 0)
209 return r;
210
106784eb 211 break;
1716f6dc 212
106784eb 213 case DNS_PROTOCOL_LLMNR:
519ef046
LP
214 assert(fd < 0);
215
1716f6dc 216 if (DNS_PACKET_QDCOUNT(p) > 1)
15411c0c 217 return -EOPNOTSUPP;
1716f6dc 218
aea2429d
LP
219 if (!ratelimit_test(&s->ratelimit))
220 return -EBUSY;
221
1716f6dc 222 family = s->family;
1716f6dc
LP
223
224 if (family == AF_INET) {
225 addr.in = LLMNR_MULTICAST_IPV4_ADDRESS;
1716f6dc
LP
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);
1716f6dc
LP
230 } else
231 return -EAFNOSUPPORT;
232 if (fd < 0)
233 return fd;
72290734 234
b4f1862d
DM
235 r = manager_send(s->manager, fd, ifindex, family, &addr, LLMNR_PORT, p);
236 if (r < 0)
237 return r;
238
239 break;
240
241 case DNS_PROTOCOL_MDNS:
519ef046
LP
242 assert(fd < 0);
243
b4f1862d
DM
244 if (!ratelimit_test(&s->ratelimit))
245 return -EBUSY;
246
247 family = s->family;
248
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);
255 } else
256 return -EAFNOSUPPORT;
257 if (fd < 0)
258 return fd;
259
260 r = manager_send(s->manager, fd, ifindex, family, &addr, MDNS_PORT, p);
72290734
TG
261 if (r < 0)
262 return r;
106784eb
DM
263
264 break;
265
266 default:
74b2466e 267 return -EAFNOSUPPORT;
106784eb 268 }
74b2466e 269
74b2466e
LP
270 return 1;
271}
272
519ef046 273int dns_scope_emit_udp(DnsScope *s, int fd, DnsPacket *p) {
80a62095
DM
274 int r;
275
276 assert(s);
277 assert(p);
278 assert(p->protocol == s->protocol);
519ef046 279 assert((s->protocol == DNS_PROTOCOL_DNS) == (fd >= 0));
80a62095
DM
280
281 do {
282 /* If there are multiple linked packets, set the TC bit in all but the last of them */
283 if (p->more) {
284 assert(p->protocol == DNS_PROTOCOL_MDNS);
261f3673 285 dns_packet_set_flags(p, true, true);
80a62095
DM
286 }
287
519ef046 288 r = dns_scope_emit_one(s, fd, p);
80a62095
DM
289 if (r < 0)
290 return r;
291
292 p = p->more;
519ef046 293 } while (p);
80a62095
DM
294
295 return 0;
296}
297
519ef046
LP
298static int dns_scope_socket(
299 DnsScope *s,
300 int type,
301 int family,
302 const union in_addr_union *address,
303 DnsServer *server,
304 uint16_t port) {
305
ad867662
LP
306 _cleanup_close_ int fd = -1;
307 union sockaddr_union sa = {};
308 socklen_t salen;
623a4c97
LP
309 static const int one = 1;
310 int ret, r;
ad867662
LP
311
312 assert(s);
be808ea0 313
519ef046
LP
314 if (server) {
315 assert(family == AF_UNSPEC);
316 assert(!address);
be808ea0 317
519ef046
LP
318 sa.sa.sa_family = server->family;
319 if (server->family == AF_INET) {
623a4c97 320 sa.in.sin_port = htobe16(port);
519ef046 321 sa.in.sin_addr = server->address.in;
623a4c97 322 salen = sizeof(sa.in);
519ef046 323 } else if (server->family == AF_INET6) {
623a4c97 324 sa.in6.sin6_port = htobe16(port);
519ef046 325 sa.in6.sin6_addr = server->address.in6;
623a4c97
LP
326 sa.in6.sin6_scope_id = s->link ? s->link->ifindex : 0;
327 salen = sizeof(sa.in6);
328 } else
329 return -EAFNOSUPPORT;
330 } else {
519ef046
LP
331 assert(family != AF_UNSPEC);
332 assert(address);
333
623a4c97
LP
334 sa.sa.sa_family = family;
335
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);
345 } else
346 return -EAFNOSUPPORT;
347 }
ad867662 348
0db64366 349 fd = socket(sa.sa.sa_family, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
ad867662
LP
350 if (fd < 0)
351 return -errno;
352
0db64366
TG
353 if (type == SOCK_STREAM) {
354 r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
355 if (r < 0)
356 return -errno;
357 }
623a4c97
LP
358
359 if (s->link) {
360 uint32_t ifindex = htobe32(s->link->ifindex);
361
362 if (sa.sa.sa_family == AF_INET) {
363 r = setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex));
364 if (r < 0)
365 return -errno;
366 } else if (sa.sa.sa_family == AF_INET6) {
367 r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex, sizeof(ifindex));
368 if (r < 0)
369 return -errno;
370 }
371 }
372
373 if (s->protocol == DNS_PROTOCOL_LLMNR) {
bf3f1271 374 /* RFC 4795, section 2.5 requires the TTL to be set to 1 */
623a4c97
LP
375
376 if (sa.sa.sa_family == AF_INET) {
377 r = setsockopt(fd, IPPROTO_IP, IP_TTL, &one, sizeof(one));
378 if (r < 0)
379 return -errno;
380 } else if (sa.sa.sa_family == AF_INET6) {
381 r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &one, sizeof(one));
382 if (r < 0)
383 return -errno;
384 }
385 }
ad867662
LP
386
387 r = connect(fd, &sa.sa, salen);
388 if (r < 0 && errno != EINPROGRESS)
389 return -errno;
390
391 ret = fd;
392 fd = -1;
623a4c97 393
ad867662
LP
394 return ret;
395}
396
519ef046
LP
397int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) {
398 return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, server, port);
0db64366
TG
399}
400
519ef046
LP
401int 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);
0db64366
TG
403}
404
51323288 405DnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, const char *domain) {
a51c1048 406 DnsSearchDomain *d;
74b2466e
LP
407
408 assert(s);
409 assert(domain);
410
28b9b764
LP
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. */
414
51323288
LP
415 if (ifindex != 0 && (!s->link || s->link->ifindex != ifindex))
416 return DNS_SCOPE_NO;
417
931851e8 418 if ((SD_RESOLVED_FLAGS_MAKE(s->protocol, s->family, 0) & flags) == 0)
51323288
LP
419 return DNS_SCOPE_NO;
420
78c6a153
LP
421 /* Never resolve any loopback hostname or IP address via DNS,
422 * LLMNR or mDNS. Instead, always rely on synthesized RRs for
423 * these. */
424 if (is_localhost(domain) ||
425 dns_name_endswith(domain, "127.in-addr.arpa") > 0 ||
9436e8ca
LP
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)
427 return DNS_SCOPE_NO;
428
c9ad0edb
LP
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)
433 return DNS_SCOPE_NO;
434
7bcffc2e
LP
435 /* Never respond to some of the domains listed in RFC6761 */
436 if (dns_name_endswith(domain, "invalid") > 0)
437 return DNS_SCOPE_NO;
438
801ad6a6
LP
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. */
a51c1048
LP
443 LIST_FOREACH(domains, d, dns_scope_get_search_domains(s))
444 if (dns_name_endswith(domain, d->name) > 0)
9b644bf9
LP
445 return DNS_SCOPE_YES;
446
106784eb 447 switch (s->protocol) {
801ad6a6 448
dc477e73 449 case DNS_PROTOCOL_DNS:
801ad6a6 450
28b9b764
LP
451 /* Exclude link-local IP ranges */
452 if (dns_name_endswith(domain, "254.169.in-addr.arpa") == 0 &&
c9ad0edb
LP
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 &&
b43d96b0
DM
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)
faa133f3 461 return DNS_SCOPE_MAYBE;
1716f6dc 462
faa133f3 463 return DNS_SCOPE_NO;
1716f6dc 464
106784eb 465 case DNS_PROTOCOL_MDNS:
78c6a153
LP
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 */
d8b7e75f 471 return DNS_SCOPE_MAYBE;
74b2466e
LP
472
473 return DNS_SCOPE_NO;
74b2466e 474
106784eb 475 case DNS_PROTOCOL_LLMNR:
78c6a153
LP
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) ||
dc477e73 478 (dns_name_is_single_label(domain) && /* only resolve single label names via LLMNR */
78c6a153
LP
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 */
1716f6dc 481 return DNS_SCOPE_MAYBE;
74b2466e 482
1716f6dc 483 return DNS_SCOPE_NO;
74b2466e 484
106784eb
DM
485 default:
486 assert_not_reached("Unknown scope protocol");
487 }
1716f6dc
LP
488}
489
011696f7
LP
490bool dns_scope_good_key(DnsScope *s, const DnsResourceKey *key) {
491 int key_family;
492
1716f6dc
LP
493 assert(s);
494 assert(key);
495
28b9b764
LP
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. */
499
011696f7
LP
500 if (key->class != DNS_CLASS_IN)
501 return false;
502
28b9b764
LP
503 if (s->protocol == DNS_PROTOCOL_DNS) {
504
e5abebab 505 /* On classic DNS, looking up non-address RRs is always
28b9b764
LP
506 * fine. (Specifically, we want to permit looking up
507 * DNSKEY and DS records on the root and top-level
508 * domains.) */
509 if (!dns_resource_key_is_address(key))
510 return true;
511
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
516 * internet. */
517 return !(dns_name_is_single_label(DNS_RESOURCE_KEY_NAME(key)) ||
518 dns_name_is_root(DNS_RESOURCE_KEY_NAME(key)));
519 }
1716f6dc
LP
520
521 /* On mDNS and LLMNR, send A and AAAA queries only on the
522 * respective scopes */
523
011696f7
LP
524 key_family = dns_type_to_af(key->type);
525 if (key_family < 0)
526 return true;
1716f6dc 527
011696f7 528 return key_family == s->family;
1716f6dc
LP
529}
530
0db4c90a 531static int dns_scope_multicast_membership(DnsScope *s, bool b, struct in_addr in, struct in6_addr in6) {
1716f6dc
LP
532 int fd;
533
5ba73e9b 534 assert(s);
5ba73e9b
LP
535 assert(s->link);
536
1716f6dc
LP
537 if (s->family == AF_INET) {
538 struct ip_mreqn mreqn = {
0db4c90a 539 .imr_multiaddr = in,
1716f6dc
LP
540 .imr_ifindex = s->link->ifindex,
541 };
542
543 fd = manager_llmnr_ipv4_udp_fd(s->manager);
544 if (fd < 0)
545 return fd;
546
7b4c2ee7
LP
547 /* Always first try to drop membership before we add
548 * one. This is necessary on some devices, such as
549 * veth. */
550 if (b)
dc751688 551 (void) setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn));
7b4c2ee7 552
1716f6dc
LP
553 if (setsockopt(fd, IPPROTO_IP, b ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)) < 0)
554 return -errno;
555
556 } else if (s->family == AF_INET6) {
557 struct ipv6_mreq mreq = {
0db4c90a 558 .ipv6mr_multiaddr = in6,
1716f6dc
LP
559 .ipv6mr_interface = s->link->ifindex,
560 };
561
562 fd = manager_llmnr_ipv6_udp_fd(s->manager);
563 if (fd < 0)
564 return fd;
565
7b4c2ee7 566 if (b)
dc751688 567 (void) setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
7b4c2ee7 568
1716f6dc
LP
569 if (setsockopt(fd, IPPROTO_IPV6, b ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
570 return -errno;
571 } else
572 return -EAFNOSUPPORT;
573
574 return 0;
74b2466e 575}
623a4c97 576
0db4c90a
DM
577int dns_scope_llmnr_membership(DnsScope *s, bool b) {
578
579 if (s->protocol != DNS_PROTOCOL_LLMNR)
580 return 0;
581
582 return dns_scope_multicast_membership(s, b, LLMNR_MULTICAST_IPV4_ADDRESS, LLMNR_MULTICAST_IPV6_ADDRESS);
583}
584
585int dns_scope_mdns_membership(DnsScope *s, bool b) {
586
587 if (s->protocol != DNS_PROTOCOL_MDNS)
588 return 0;
589
590 return dns_scope_multicast_membership(s, b, MDNS_MULTICAST_IPV4_ADDRESS, MDNS_MULTICAST_IPV6_ADDRESS);
591}
592
ec2c5e43
LP
593static int dns_scope_make_reply_packet(
594 DnsScope *s,
595 uint16_t id,
596 int rcode,
597 DnsQuestion *q,
598 DnsAnswer *answer,
599 DnsAnswer *soa,
600 bool tentative,
601 DnsPacket **ret) {
602
623a4c97
LP
603 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
604 unsigned i;
605 int r;
606
607 assert(s);
a4076574 608 assert(ret);
623a4c97 609
75cd513e
TA
610 if ((!q || q->n_keys <= 0)
611 && (!answer || answer->n_rrs <= 0)
612 && (!soa || soa->n_rrs <= 0))
623a4c97
LP
613 return -EINVAL;
614
615 r = dns_packet_new(&p, s->protocol, 0);
616 if (r < 0)
617 return r;
618
619 DNS_PACKET_HEADER(p)->id = id;
ea917db9
LP
620 DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
621 1 /* qr */,
622 0 /* opcode */,
623 0 /* c */,
624 0 /* tc */,
ec2c5e43 625 tentative,
ea917db9
LP
626 0 /* (ra) */,
627 0 /* (ad) */,
628 0 /* (cd) */,
629 rcode));
623a4c97
LP
630
631 if (q) {
632 for (i = 0; i < q->n_keys; i++) {
633 r = dns_packet_append_key(p, q->keys[i], NULL);
634 if (r < 0)
635 return r;
636 }
637
638 DNS_PACKET_HEADER(p)->qdcount = htobe16(q->n_keys);
639 }
640
8bf52d3d
LP
641 if (answer) {
642 for (i = 0; i < answer->n_rrs; i++) {
a8812dd7 643 r = dns_packet_append_rr(p, answer->items[i].rr, NULL, NULL);
623a4c97
LP
644 if (r < 0)
645 return r;
646 }
647
8bf52d3d
LP
648 DNS_PACKET_HEADER(p)->ancount = htobe16(answer->n_rrs);
649 }
650
651 if (soa) {
652 for (i = 0; i < soa->n_rrs; i++) {
a8812dd7 653 r = dns_packet_append_rr(p, soa->items[i].rr, NULL, NULL);
8bf52d3d
LP
654 if (r < 0)
655 return r;
656 }
657
658 DNS_PACKET_HEADER(p)->arcount = htobe16(soa->n_rrs);
623a4c97
LP
659 }
660
661 *ret = p;
662 p = NULL;
663
664 return 0;
665}
666
a4076574
LP
667static void dns_scope_verify_conflicts(DnsScope *s, DnsPacket *p) {
668 unsigned n;
669
670 assert(s);
671 assert(p);
672
673 if (p->question)
674 for (n = 0; n < p->question->n_keys; n++)
675 dns_zone_verify_conflicts(&s->zone, p->question->keys[n]);
676 if (p->answer)
677 for (n = 0; n < p->answer->n_rrs; n++)
78c6a153 678 dns_zone_verify_conflicts(&s->zone, p->answer->items[n].rr->key);
a4076574
LP
679}
680
623a4c97
LP
681void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) {
682 _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL;
8bf52d3d 683 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
5032b16d 684 DnsResourceKey *key = NULL;
ec2c5e43 685 bool tentative = false;
623a4c97
LP
686 int r, fd;
687
688 assert(s);
689 assert(p);
690
691 if (p->protocol != DNS_PROTOCOL_LLMNR)
692 return;
693
2442b93d
LP
694 if (p->ipproto == IPPROTO_UDP) {
695 /* Don't accept UDP queries directed to anything but
696 * the LLMNR multicast addresses. See RFC 4795,
d076c6f9 697 * section 2.5. */
2442b93d
LP
698
699 if (p->family == AF_INET && !in_addr_equal(AF_INET, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV4_ADDRESS))
700 return;
701
702 if (p->family == AF_INET6 && !in_addr_equal(AF_INET6, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV6_ADDRESS))
703 return;
704 }
705
623a4c97
LP
706 r = dns_packet_extract(p);
707 if (r < 0) {
da927ba9 708 log_debug_errno(r, "Failed to extract resources from incoming packet: %m");
623a4c97
LP
709 return;
710 }
711
8b757a38 712 if (DNS_PACKET_LLMNR_C(p)) {
a4076574
LP
713 /* Somebody notified us about a possible conflict */
714 dns_scope_verify_conflicts(s, p);
ea917db9
LP
715 return;
716 }
717
5032b16d
LP
718 assert(p->question->n_keys == 1);
719 key = p->question->keys[0];
720
721 r = dns_zone_lookup(&s->zone, key, &answer, &soa, &tentative);
623a4c97 722 if (r < 0) {
da927ba9 723 log_debug_errno(r, "Failed to lookup key: %m");
623a4c97
LP
724 return;
725 }
726 if (r == 0)
727 return;
728
fcf57f9c
LP
729 if (answer)
730 dns_answer_order_by_scope(answer, in_addr_is_link_local(p->family, &p->sender) > 0);
af93291c 731
ec2c5e43 732 r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, p->question, answer, soa, tentative, &reply);
623a4c97 733 if (r < 0) {
da927ba9 734 log_debug_errno(r, "Failed to build reply packet: %m");
623a4c97
LP
735 return;
736 }
737
738 if (stream)
739 r = dns_stream_write_packet(stream, reply);
740 else {
aea2429d
LP
741 if (!ratelimit_test(&s->ratelimit))
742 return;
743
623a4c97
LP
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);
748 else {
749 log_debug("Unknown protocol");
750 return;
751 }
752 if (fd < 0) {
da927ba9 753 log_debug_errno(fd, "Failed to get reply socket: %m");
623a4c97
LP
754 return;
755 }
756
6e068472
LP
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 */
761
623a4c97
LP
762 r = manager_send(s->manager, fd, p->ifindex, p->family, &p->sender, p->sender_port, reply);
763 }
764
765 if (r < 0) {
da927ba9 766 log_debug_errno(r, "Failed to send reply packet: %m");
623a4c97
LP
767 return;
768 }
769}
ec2c5e43 770
f52e61da 771DnsTransaction *dns_scope_find_transaction(DnsScope *scope, DnsResourceKey *key, bool cache_ok) {
ec2c5e43
LP
772 DnsTransaction *t;
773
774 assert(scope);
f52e61da 775 assert(key);
ec2c5e43 776
da0c630e
LP
777 /* Try to find an ongoing transaction that is a equal to the
778 * specified question */
f9ebb22a 779 t = hashmap_get(scope->transactions_by_key, key);
da0c630e
LP
780 if (!t)
781 return NULL;
dc4d47e2 782
da0c630e
LP
783 /* Refuse reusing transactions that completed based on cached
784 * data instead of a real packet, if that's requested. */
785 if (!cache_ok &&
3bbdc31d 786 IN_SET(t->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_RCODE_FAILURE) &&
c3bc53e6 787 t->answer_source != DNS_TRANSACTION_NETWORK)
da0c630e 788 return NULL;
ec2c5e43 789
da0c630e 790 return t;
ec2c5e43 791}
a4076574
LP
792
793static int dns_scope_make_conflict_packet(
794 DnsScope *s,
795 DnsResourceRecord *rr,
796 DnsPacket **ret) {
797
798 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
799 int r;
800
801 assert(s);
802 assert(rr);
803 assert(ret);
804
805 r = dns_packet_new(&p, s->protocol, 0);
806 if (r < 0)
807 return r;
808
809 DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
810 0 /* qr */,
811 0 /* opcode */,
812 1 /* conflict */,
813 0 /* tc */,
814 0 /* t */,
815 0 /* (ra) */,
816 0 /* (ad) */,
817 0 /* (cd) */,
818 0));
fe2dfc8b
DM
819
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));
823
a4076574
LP
824 DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
825 DNS_PACKET_HEADER(p)->arcount = htobe16(1);
826
827 r = dns_packet_append_key(p, rr->key, NULL);
828 if (r < 0)
829 return r;
830
a8812dd7 831 r = dns_packet_append_rr(p, rr, NULL, NULL);
a4076574
LP
832 if (r < 0)
833 return r;
834
835 *ret = p;
836 p = NULL;
837
838 return 0;
839}
840
841static int on_conflict_dispatch(sd_event_source *es, usec_t usec, void *userdata) {
842 DnsScope *scope = userdata;
843 int r;
844
845 assert(es);
846 assert(scope);
847
848 scope->conflict_event_source = sd_event_source_unref(scope->conflict_event_source);
849
850 for (;;) {
851 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
852 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
853
1e43061b 854 rr = ordered_hashmap_steal_first(scope->conflict_queue);
a4076574
LP
855 if (!rr)
856 break;
857
858 r = dns_scope_make_conflict_packet(scope, rr, &p);
859 if (r < 0) {
da927ba9 860 log_error_errno(r, "Failed to make conflict packet: %m");
a4076574
LP
861 return 0;
862 }
863
519ef046 864 r = dns_scope_emit_udp(scope, -1, p);
a4076574 865 if (r < 0)
da927ba9 866 log_debug_errno(r, "Failed to send conflict packet: %m");
a4076574
LP
867 }
868
869 return 0;
870}
871
872int dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) {
873 usec_t jitter;
874 int r;
875
876 assert(scope);
877 assert(rr);
878
879 /* We don't send these queries immediately. Instead, we queue
880 * them, and send them after some jitter delay. */
1e43061b 881 r = ordered_hashmap_ensure_allocated(&scope->conflict_queue, &dns_resource_key_hash_ops);
a4076574
LP
882 if (r < 0) {
883 log_oom();
884 return r;
885 }
886
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 */
1e43061b 890 r = ordered_hashmap_put(scope->conflict_queue, rr->key, rr);
a4076574
LP
891 if (r == -EEXIST || r == 0)
892 return 0;
f647962d
MS
893 if (r < 0)
894 return log_debug_errno(r, "Failed to queue conflicting RR: %m");
a4076574
LP
895
896 dns_resource_record_ref(rr);
897
898 if (scope->conflict_event_source)
899 return 0;
900
901 random_bytes(&jitter, sizeof(jitter));
902 jitter %= LLMNR_JITTER_INTERVAL_USEC;
903
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);
f647962d
MS
910 if (r < 0)
911 return log_debug_errno(r, "Failed to add conflict dispatch event: %m");
a4076574 912
aa4a9deb
LP
913 (void) sd_event_source_set_description(scope->conflict_event_source, "scope-conflict");
914
a4076574
LP
915 return 0;
916}
917
918void dns_scope_check_conflicts(DnsScope *scope, DnsPacket *p) {
919 unsigned i;
920 int r;
921
922 assert(scope);
923 assert(p);
924
925 if (p->protocol != DNS_PROTOCOL_LLMNR)
926 return;
927
928 if (DNS_PACKET_RRCOUNT(p) <= 0)
929 return;
930
8b757a38 931 if (DNS_PACKET_LLMNR_C(p) != 0)
a4076574
LP
932 return;
933
8b757a38 934 if (DNS_PACKET_LLMNR_T(p) != 0)
a4076574
LP
935 return;
936
937 if (manager_our_packet(scope->manager, p))
938 return;
939
940 r = dns_packet_extract(p);
941 if (r < 0) {
da927ba9 942 log_debug_errno(r, "Failed to extract packet: %m");
a4076574
LP
943 return;
944 }
945
946 log_debug("Checking for conflicts...");
947
948 for (i = 0; i < p->answer->n_rrs; i++) {
949
950 /* Check for conflicts against the local zone. If we
951 * found one, we won't check any further */
78c6a153 952 r = dns_zone_check_conflicts(&scope->zone, p->answer->items[i].rr);
a4076574
LP
953 if (r != 0)
954 continue;
955
956 /* Check for conflicts against the local cache. If so,
957 * send out an advisory query, to inform everybody */
78c6a153 958 r = dns_cache_check_conflicts(&scope->cache, p->answer->items[i].rr, p->family, &p->sender);
a4076574
LP
959 if (r <= 0)
960 continue;
961
78c6a153 962 dns_scope_notify_conflict(scope, p->answer->items[i].rr);
a4076574
LP
963 }
964}
4d506d6b
LP
965
966void dns_scope_dump(DnsScope *s, FILE *f) {
967 assert(s);
968
969 if (!f)
970 f = stdout;
971
972 fputs("[Scope protocol=", f);
973 fputs(dns_protocol_to_string(s->protocol), f);
974
975 if (s->link) {
976 fputs(" interface=", f);
977 fputs(s->link->name, f);
978 }
979
980 if (s->family != AF_UNSPEC) {
981 fputs(" family=", f);
982 fputs(af_to_name(s->family), f);
983 }
984
985 fputs("]\n", f);
986
987 if (!dns_zone_is_empty(&s->zone)) {
988 fputs("ZONE:\n", f);
989 dns_zone_dump(&s->zone, f);
990 }
991
992 if (!dns_cache_is_empty(&s->cache)) {
993 fputs("CACHE:\n", f);
994 dns_cache_dump(&s->cache, f);
995 }
996}
a51c1048
LP
997
998DnsSearchDomain *dns_scope_get_search_domains(DnsScope *s) {
801ad6a6
LP
999 assert(s);
1000
a51c1048
LP
1001 if (s->protocol != DNS_PROTOCOL_DNS)
1002 return NULL;
1003
1004 if (s->link)
1005 return s->link->search_domains;
1006
28b9b764 1007 return s->manager->search_domains;
801ad6a6
LP
1008}
1009
dc477e73 1010bool dns_scope_name_needs_search_domain(DnsScope *s, const char *name) {
801ad6a6
LP
1011 assert(s);
1012
1013 if (s->protocol != DNS_PROTOCOL_DNS)
dc477e73 1014 return false;
801ad6a6 1015
dc477e73 1016 return dns_name_is_single_label(name);
801ad6a6 1017}
edbcc1fd
LP
1018
1019bool dns_scope_network_good(DnsScope *s) {
edbcc1fd
LP
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. */
1023
1024 if (s->link)
1025 return true;
1026
011696f7 1027 return manager_routable(s->manager, AF_UNSPEC);
edbcc1fd 1028}