]>
Commit | Line | Data |
---|---|---|
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 | 42 | int 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 | 86 | static 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 | ||
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); | |
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 | ||
53fda2bb DR |
127 | sd_event_source_unref(s->announce_event_source); |
128 | ||
322345fd | 129 | dns_cache_flush(&s->cache); |
623a4c97 | 130 | dns_zone_flush(&s->zone); |
322345fd | 131 | |
74b2466e | 132 | LIST_REMOVE(scopes, s->manager->dns_scopes, s); |
6b430fdb | 133 | return mfree(s); |
74b2466e LP |
134 | } |
135 | ||
2c27fbca | 136 | DnsServer *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 | ||
148 | void 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 |
160 | void 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 | ||
170 | void 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 | 177 | static 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 | |
b30bf55d | 235 | r = manager_send(s->manager, fd, ifindex, family, &addr, LLMNR_PORT, NULL, p); |
b4f1862d DM |
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 | ||
b30bf55d | 260 | r = manager_send(s->manager, fd, ifindex, family, &addr, MDNS_PORT, NULL, 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 | 273 | int 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 |
298 | static 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 | 309 | static const int one = 1; |
2817157b | 310 | int ret, r, ifindex; |
ad867662 LP |
311 | |
312 | assert(s); | |
be808ea0 | 313 | |
519ef046 LP |
314 | if (server) { |
315 | assert(family == AF_UNSPEC); | |
316 | assert(!address); | |
be808ea0 | 317 | |
2817157b LP |
318 | ifindex = dns_server_ifindex(server); |
319 | ||
519ef046 LP |
320 | sa.sa.sa_family = server->family; |
321 | if (server->family == AF_INET) { | |
623a4c97 | 322 | sa.in.sin_port = htobe16(port); |
519ef046 | 323 | sa.in.sin_addr = server->address.in; |
623a4c97 | 324 | salen = sizeof(sa.in); |
519ef046 | 325 | } else if (server->family == AF_INET6) { |
623a4c97 | 326 | sa.in6.sin6_port = htobe16(port); |
519ef046 | 327 | sa.in6.sin6_addr = server->address.in6; |
2817157b | 328 | sa.in6.sin6_scope_id = ifindex; |
623a4c97 LP |
329 | salen = sizeof(sa.in6); |
330 | } else | |
331 | return -EAFNOSUPPORT; | |
332 | } else { | |
519ef046 LP |
333 | assert(family != AF_UNSPEC); |
334 | assert(address); | |
335 | ||
623a4c97 | 336 | sa.sa.sa_family = family; |
2817157b | 337 | ifindex = s->link ? s->link->ifindex : 0; |
623a4c97 LP |
338 | |
339 | if (family == AF_INET) { | |
340 | sa.in.sin_port = htobe16(port); | |
341 | sa.in.sin_addr = address->in; | |
342 | salen = sizeof(sa.in); | |
343 | } else if (family == AF_INET6) { | |
344 | sa.in6.sin6_port = htobe16(port); | |
345 | sa.in6.sin6_addr = address->in6; | |
2817157b | 346 | sa.in6.sin6_scope_id = ifindex; |
623a4c97 LP |
347 | salen = sizeof(sa.in6); |
348 | } else | |
349 | return -EAFNOSUPPORT; | |
350 | } | |
ad867662 | 351 | |
0db64366 | 352 | fd = socket(sa.sa.sa_family, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
ad867662 LP |
353 | if (fd < 0) |
354 | return -errno; | |
355 | ||
0db64366 TG |
356 | if (type == SOCK_STREAM) { |
357 | r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); | |
358 | if (r < 0) | |
359 | return -errno; | |
360 | } | |
623a4c97 LP |
361 | |
362 | if (s->link) { | |
2817157b | 363 | be32_t ifindex_be = htobe32(ifindex); |
623a4c97 LP |
364 | |
365 | if (sa.sa.sa_family == AF_INET) { | |
2817157b | 366 | r = setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)); |
623a4c97 LP |
367 | if (r < 0) |
368 | return -errno; | |
369 | } else if (sa.sa.sa_family == AF_INET6) { | |
2817157b | 370 | r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)); |
623a4c97 LP |
371 | if (r < 0) |
372 | return -errno; | |
373 | } | |
374 | } | |
375 | ||
376 | if (s->protocol == DNS_PROTOCOL_LLMNR) { | |
bf3f1271 | 377 | /* RFC 4795, section 2.5 requires the TTL to be set to 1 */ |
623a4c97 LP |
378 | |
379 | if (sa.sa.sa_family == AF_INET) { | |
380 | r = setsockopt(fd, IPPROTO_IP, IP_TTL, &one, sizeof(one)); | |
381 | if (r < 0) | |
382 | return -errno; | |
383 | } else if (sa.sa.sa_family == AF_INET6) { | |
384 | r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &one, sizeof(one)); | |
385 | if (r < 0) | |
386 | return -errno; | |
387 | } | |
388 | } | |
ad867662 LP |
389 | |
390 | r = connect(fd, &sa.sa, salen); | |
391 | if (r < 0 && errno != EINPROGRESS) | |
392 | return -errno; | |
393 | ||
394 | ret = fd; | |
395 | fd = -1; | |
623a4c97 | 396 | |
ad867662 LP |
397 | return ret; |
398 | } | |
399 | ||
519ef046 LP |
400 | int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) { |
401 | return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, server, port); | |
0db64366 TG |
402 | } |
403 | ||
519ef046 LP |
404 | int dns_scope_socket_tcp(DnsScope *s, int family, const union in_addr_union *address, DnsServer *server, uint16_t port) { |
405 | return dns_scope_socket(s, SOCK_STREAM, family, address, server, port); | |
0db64366 TG |
406 | } |
407 | ||
51323288 | 408 | DnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, const char *domain) { |
a51c1048 | 409 | DnsSearchDomain *d; |
b9fe94ca | 410 | DnsServer *dns_server; |
74b2466e LP |
411 | |
412 | assert(s); | |
413 | assert(domain); | |
414 | ||
28b9b764 LP |
415 | /* Checks if the specified domain is something to look up on |
416 | * this scope. Note that this accepts non-qualified hostnames, | |
417 | * i.e. those without any search path prefixed yet. */ | |
418 | ||
51323288 LP |
419 | if (ifindex != 0 && (!s->link || s->link->ifindex != ifindex)) |
420 | return DNS_SCOPE_NO; | |
421 | ||
931851e8 | 422 | if ((SD_RESOLVED_FLAGS_MAKE(s->protocol, s->family, 0) & flags) == 0) |
51323288 LP |
423 | return DNS_SCOPE_NO; |
424 | ||
78c6a153 LP |
425 | /* Never resolve any loopback hostname or IP address via DNS, |
426 | * LLMNR or mDNS. Instead, always rely on synthesized RRs for | |
427 | * these. */ | |
428 | if (is_localhost(domain) || | |
429 | dns_name_endswith(domain, "127.in-addr.arpa") > 0 || | |
9436e8ca LP |
430 | 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) |
431 | return DNS_SCOPE_NO; | |
432 | ||
c9ad0edb LP |
433 | /* Never respond to some of the domains listed in RFC6303 */ |
434 | if (dns_name_endswith(domain, "0.in-addr.arpa") > 0 || | |
435 | dns_name_equal(domain, "255.255.255.255.in-addr.arpa") > 0 || | |
436 | 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) | |
437 | return DNS_SCOPE_NO; | |
438 | ||
7bcffc2e LP |
439 | /* Never respond to some of the domains listed in RFC6761 */ |
440 | if (dns_name_endswith(domain, "invalid") > 0) | |
441 | return DNS_SCOPE_NO; | |
442 | ||
801ad6a6 LP |
443 | /* Always honour search domains for routing queries. Note that |
444 | * we return DNS_SCOPE_YES here, rather than just | |
445 | * DNS_SCOPE_MAYBE, which means wildcard scopes won't be | |
446 | * considered anymore. */ | |
a51c1048 LP |
447 | LIST_FOREACH(domains, d, dns_scope_get_search_domains(s)) |
448 | if (dns_name_endswith(domain, d->name) > 0) | |
9b644bf9 LP |
449 | return DNS_SCOPE_YES; |
450 | ||
b9fe94ca MP |
451 | /* If the DNS server has route-only domains, don't send other requests |
452 | * to it. This would be a privacy violation, will most probably fail | |
453 | * anyway, and adds unnecessary load. */ | |
454 | dns_server = dns_scope_get_dns_server(s); | |
455 | if (dns_server && dns_server_limited_domains(dns_server)) | |
456 | return DNS_SCOPE_NO; | |
457 | ||
106784eb | 458 | switch (s->protocol) { |
801ad6a6 | 459 | |
dc477e73 | 460 | case DNS_PROTOCOL_DNS: |
801ad6a6 | 461 | |
28b9b764 LP |
462 | /* Exclude link-local IP ranges */ |
463 | if (dns_name_endswith(domain, "254.169.in-addr.arpa") == 0 && | |
c9ad0edb LP |
464 | dns_name_endswith(domain, "8.e.f.ip6.arpa") == 0 && |
465 | dns_name_endswith(domain, "9.e.f.ip6.arpa") == 0 && | |
466 | dns_name_endswith(domain, "a.e.f.ip6.arpa") == 0 && | |
b43d96b0 DM |
467 | dns_name_endswith(domain, "b.e.f.ip6.arpa") == 0 && |
468 | /* If networks use .local in their private setups, they are supposed to also add .local to their search | |
469 | * domains, which we already checked above. Otherwise, we consider .local specific to mDNS and won't | |
470 | * send such queries ordinary DNS servers. */ | |
471 | dns_name_endswith(domain, "local") == 0) | |
faa133f3 | 472 | return DNS_SCOPE_MAYBE; |
1716f6dc | 473 | |
faa133f3 | 474 | return DNS_SCOPE_NO; |
1716f6dc | 475 | |
106784eb | 476 | case DNS_PROTOCOL_MDNS: |
78c6a153 LP |
477 | if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) || |
478 | (s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) || | |
479 | (dns_name_endswith(domain, "local") > 0 && /* only resolve names ending in .local via mDNS */ | |
480 | dns_name_equal(domain, "local") == 0 && /* but not the single-label "local" name itself */ | |
481 | manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via mDNS */ | |
d8b7e75f | 482 | return DNS_SCOPE_MAYBE; |
74b2466e LP |
483 | |
484 | return DNS_SCOPE_NO; | |
74b2466e | 485 | |
106784eb | 486 | case DNS_PROTOCOL_LLMNR: |
78c6a153 LP |
487 | if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) || |
488 | (s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) || | |
dc477e73 | 489 | (dns_name_is_single_label(domain) && /* only resolve single label names via LLMNR */ |
78c6a153 LP |
490 | !is_gateway_hostname(domain) && /* don't resolve "gateway" with LLMNR, let nss-myhostname handle this */ |
491 | manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via LLMNR */ | |
1716f6dc | 492 | return DNS_SCOPE_MAYBE; |
74b2466e | 493 | |
1716f6dc | 494 | return DNS_SCOPE_NO; |
74b2466e | 495 | |
106784eb DM |
496 | default: |
497 | assert_not_reached("Unknown scope protocol"); | |
498 | } | |
1716f6dc LP |
499 | } |
500 | ||
011696f7 LP |
501 | bool dns_scope_good_key(DnsScope *s, const DnsResourceKey *key) { |
502 | int key_family; | |
503 | ||
1716f6dc LP |
504 | assert(s); |
505 | assert(key); | |
506 | ||
28b9b764 LP |
507 | /* Check if it makes sense to resolve the specified key on |
508 | * this scope. Note that this call assumes as fully qualified | |
509 | * name, i.e. the search suffixes already appended. */ | |
510 | ||
011696f7 LP |
511 | if (key->class != DNS_CLASS_IN) |
512 | return false; | |
513 | ||
28b9b764 LP |
514 | if (s->protocol == DNS_PROTOCOL_DNS) { |
515 | ||
e5abebab | 516 | /* On classic DNS, looking up non-address RRs is always |
28b9b764 LP |
517 | * fine. (Specifically, we want to permit looking up |
518 | * DNSKEY and DS records on the root and top-level | |
519 | * domains.) */ | |
520 | if (!dns_resource_key_is_address(key)) | |
521 | return true; | |
522 | ||
523 | /* However, we refuse to look up A and AAAA RRs on the | |
524 | * root and single-label domains, under the assumption | |
525 | * that those should be resolved via LLMNR or search | |
526 | * path only, and should not be leaked onto the | |
527 | * internet. */ | |
1c02e7ba ZJS |
528 | return !(dns_name_is_single_label(dns_resource_key_name(key)) || |
529 | dns_name_is_root(dns_resource_key_name(key))); | |
28b9b764 | 530 | } |
1716f6dc LP |
531 | |
532 | /* On mDNS and LLMNR, send A and AAAA queries only on the | |
533 | * respective scopes */ | |
534 | ||
011696f7 LP |
535 | key_family = dns_type_to_af(key->type); |
536 | if (key_family < 0) | |
537 | return true; | |
1716f6dc | 538 | |
011696f7 | 539 | return key_family == s->family; |
1716f6dc LP |
540 | } |
541 | ||
0db4c90a | 542 | static int dns_scope_multicast_membership(DnsScope *s, bool b, struct in_addr in, struct in6_addr in6) { |
1716f6dc LP |
543 | int fd; |
544 | ||
5ba73e9b | 545 | assert(s); |
5ba73e9b LP |
546 | assert(s->link); |
547 | ||
1716f6dc LP |
548 | if (s->family == AF_INET) { |
549 | struct ip_mreqn mreqn = { | |
0db4c90a | 550 | .imr_multiaddr = in, |
1716f6dc LP |
551 | .imr_ifindex = s->link->ifindex, |
552 | }; | |
553 | ||
d37baf40 DR |
554 | if (s->protocol == DNS_PROTOCOL_LLMNR) |
555 | fd = manager_llmnr_ipv4_udp_fd(s->manager); | |
556 | else | |
557 | fd = manager_mdns_ipv4_fd(s->manager); | |
558 | ||
1716f6dc LP |
559 | if (fd < 0) |
560 | return fd; | |
561 | ||
7b4c2ee7 LP |
562 | /* Always first try to drop membership before we add |
563 | * one. This is necessary on some devices, such as | |
564 | * veth. */ | |
565 | if (b) | |
dc751688 | 566 | (void) setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)); |
7b4c2ee7 | 567 | |
1716f6dc LP |
568 | if (setsockopt(fd, IPPROTO_IP, b ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)) < 0) |
569 | return -errno; | |
570 | ||
571 | } else if (s->family == AF_INET6) { | |
572 | struct ipv6_mreq mreq = { | |
0db4c90a | 573 | .ipv6mr_multiaddr = in6, |
1716f6dc LP |
574 | .ipv6mr_interface = s->link->ifindex, |
575 | }; | |
576 | ||
d37baf40 DR |
577 | if (s->protocol == DNS_PROTOCOL_LLMNR) |
578 | fd = manager_llmnr_ipv6_udp_fd(s->manager); | |
579 | else | |
580 | fd = manager_mdns_ipv6_fd(s->manager); | |
581 | ||
1716f6dc LP |
582 | if (fd < 0) |
583 | return fd; | |
584 | ||
7b4c2ee7 | 585 | if (b) |
dc751688 | 586 | (void) setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); |
7b4c2ee7 | 587 | |
1716f6dc LP |
588 | if (setsockopt(fd, IPPROTO_IPV6, b ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) |
589 | return -errno; | |
590 | } else | |
591 | return -EAFNOSUPPORT; | |
592 | ||
593 | return 0; | |
74b2466e | 594 | } |
623a4c97 | 595 | |
0db4c90a | 596 | int dns_scope_llmnr_membership(DnsScope *s, bool b) { |
f471bc11 | 597 | assert(s); |
0db4c90a DM |
598 | |
599 | if (s->protocol != DNS_PROTOCOL_LLMNR) | |
600 | return 0; | |
601 | ||
602 | return dns_scope_multicast_membership(s, b, LLMNR_MULTICAST_IPV4_ADDRESS, LLMNR_MULTICAST_IPV6_ADDRESS); | |
603 | } | |
604 | ||
605 | int dns_scope_mdns_membership(DnsScope *s, bool b) { | |
f471bc11 | 606 | assert(s); |
0db4c90a DM |
607 | |
608 | if (s->protocol != DNS_PROTOCOL_MDNS) | |
609 | return 0; | |
610 | ||
611 | return dns_scope_multicast_membership(s, b, MDNS_MULTICAST_IPV4_ADDRESS, MDNS_MULTICAST_IPV6_ADDRESS); | |
612 | } | |
613 | ||
3b991089 | 614 | int dns_scope_make_reply_packet( |
ec2c5e43 LP |
615 | DnsScope *s, |
616 | uint16_t id, | |
617 | int rcode, | |
618 | DnsQuestion *q, | |
619 | DnsAnswer *answer, | |
620 | DnsAnswer *soa, | |
621 | bool tentative, | |
622 | DnsPacket **ret) { | |
623 | ||
623a4c97 | 624 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; |
623a4c97 LP |
625 | int r; |
626 | ||
627 | assert(s); | |
a4076574 | 628 | assert(ret); |
623a4c97 | 629 | |
501e8eb0 LP |
630 | if (dns_question_isempty(q) && |
631 | dns_answer_isempty(answer) && | |
632 | dns_answer_isempty(soa)) | |
623a4c97 LP |
633 | return -EINVAL; |
634 | ||
635 | r = dns_packet_new(&p, s->protocol, 0); | |
636 | if (r < 0) | |
637 | return r; | |
638 | ||
639 | DNS_PACKET_HEADER(p)->id = id; | |
ea917db9 LP |
640 | DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS( |
641 | 1 /* qr */, | |
642 | 0 /* opcode */, | |
643 | 0 /* c */, | |
644 | 0 /* tc */, | |
ec2c5e43 | 645 | tentative, |
ea917db9 LP |
646 | 0 /* (ra) */, |
647 | 0 /* (ad) */, | |
648 | 0 /* (cd) */, | |
649 | rcode)); | |
623a4c97 | 650 | |
f471bc11 LP |
651 | r = dns_packet_append_question(p, q); |
652 | if (r < 0) | |
653 | return r; | |
654 | DNS_PACKET_HEADER(p)->qdcount = htobe16(dns_question_size(q)); | |
8bf52d3d | 655 | |
f471bc11 LP |
656 | r = dns_packet_append_answer(p, answer); |
657 | if (r < 0) | |
658 | return r; | |
659 | DNS_PACKET_HEADER(p)->ancount = htobe16(dns_answer_size(answer)); | |
8bf52d3d | 660 | |
f471bc11 LP |
661 | r = dns_packet_append_answer(p, soa); |
662 | if (r < 0) | |
663 | return r; | |
664 | DNS_PACKET_HEADER(p)->arcount = htobe16(dns_answer_size(soa)); | |
623a4c97 LP |
665 | |
666 | *ret = p; | |
667 | p = NULL; | |
668 | ||
669 | return 0; | |
670 | } | |
671 | ||
a4076574 | 672 | static void dns_scope_verify_conflicts(DnsScope *s, DnsPacket *p) { |
2a3900d7 LP |
673 | DnsResourceRecord *rr; |
674 | DnsResourceKey *key; | |
a4076574 LP |
675 | |
676 | assert(s); | |
677 | assert(p); | |
678 | ||
2a3900d7 LP |
679 | DNS_QUESTION_FOREACH(key, p->question) |
680 | dns_zone_verify_conflicts(&s->zone, key); | |
681 | ||
682 | DNS_ANSWER_FOREACH(rr, p->answer) | |
683 | dns_zone_verify_conflicts(&s->zone, rr->key); | |
a4076574 LP |
684 | } |
685 | ||
623a4c97 | 686 | void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) { |
8bf52d3d | 687 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL; |
b30bf55d | 688 | _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL; |
5032b16d | 689 | DnsResourceKey *key = NULL; |
ec2c5e43 | 690 | bool tentative = false; |
b30bf55d | 691 | int r; |
623a4c97 LP |
692 | |
693 | assert(s); | |
694 | assert(p); | |
695 | ||
696 | if (p->protocol != DNS_PROTOCOL_LLMNR) | |
697 | return; | |
698 | ||
2442b93d LP |
699 | if (p->ipproto == IPPROTO_UDP) { |
700 | /* Don't accept UDP queries directed to anything but | |
701 | * the LLMNR multicast addresses. See RFC 4795, | |
d076c6f9 | 702 | * section 2.5. */ |
2442b93d LP |
703 | |
704 | if (p->family == AF_INET && !in_addr_equal(AF_INET, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV4_ADDRESS)) | |
705 | return; | |
706 | ||
707 | if (p->family == AF_INET6 && !in_addr_equal(AF_INET6, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV6_ADDRESS)) | |
708 | return; | |
709 | } | |
710 | ||
623a4c97 LP |
711 | r = dns_packet_extract(p); |
712 | if (r < 0) { | |
b30bf55d | 713 | log_debug_errno(r, "Failed to extract resource records from incoming packet: %m"); |
623a4c97 LP |
714 | return; |
715 | } | |
716 | ||
8b757a38 | 717 | if (DNS_PACKET_LLMNR_C(p)) { |
a4076574 LP |
718 | /* Somebody notified us about a possible conflict */ |
719 | dns_scope_verify_conflicts(s, p); | |
ea917db9 LP |
720 | return; |
721 | } | |
722 | ||
501e8eb0 | 723 | assert(dns_question_size(p->question) == 1); |
5032b16d LP |
724 | key = p->question->keys[0]; |
725 | ||
97ebebbc | 726 | r = dns_zone_lookup(&s->zone, key, 0, &answer, &soa, &tentative); |
623a4c97 | 727 | if (r < 0) { |
da927ba9 | 728 | log_debug_errno(r, "Failed to lookup key: %m"); |
623a4c97 LP |
729 | return; |
730 | } | |
731 | if (r == 0) | |
732 | return; | |
733 | ||
fcf57f9c LP |
734 | if (answer) |
735 | dns_answer_order_by_scope(answer, in_addr_is_link_local(p->family, &p->sender) > 0); | |
af93291c | 736 | |
ec2c5e43 | 737 | r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, p->question, answer, soa, tentative, &reply); |
623a4c97 | 738 | if (r < 0) { |
da927ba9 | 739 | log_debug_errno(r, "Failed to build reply packet: %m"); |
623a4c97 LP |
740 | return; |
741 | } | |
742 | ||
b30bf55d | 743 | if (stream) { |
623a4c97 | 744 | r = dns_stream_write_packet(stream, reply); |
b30bf55d LP |
745 | if (r < 0) { |
746 | log_debug_errno(r, "Failed to enqueue reply packet: %m"); | |
747 | return; | |
748 | } | |
749 | ||
750 | /* Let's take an extra reference on this stream, so that it stays around after returning. The reference | |
751 | * will be dangling until the stream is disconnected, and the default completion handler of the stream | |
752 | * will then unref the stream and destroy it */ | |
753 | if (DNS_STREAM_QUEUED(stream)) | |
754 | dns_stream_ref(stream); | |
755 | } else { | |
756 | int fd; | |
757 | ||
aea2429d LP |
758 | if (!ratelimit_test(&s->ratelimit)) |
759 | return; | |
760 | ||
623a4c97 LP |
761 | if (p->family == AF_INET) |
762 | fd = manager_llmnr_ipv4_udp_fd(s->manager); | |
763 | else if (p->family == AF_INET6) | |
764 | fd = manager_llmnr_ipv6_udp_fd(s->manager); | |
765 | else { | |
766 | log_debug("Unknown protocol"); | |
767 | return; | |
768 | } | |
769 | if (fd < 0) { | |
da927ba9 | 770 | log_debug_errno(fd, "Failed to get reply socket: %m"); |
623a4c97 LP |
771 | return; |
772 | } | |
773 | ||
6e068472 LP |
774 | /* Note that we always immediately reply to all LLMNR |
775 | * requests, and do not wait any time, since we | |
776 | * verified uniqueness for all records. Also see RFC | |
777 | * 4795, Section 2.7 */ | |
778 | ||
b30bf55d LP |
779 | r = manager_send(s->manager, fd, p->ifindex, p->family, &p->sender, p->sender_port, NULL, reply); |
780 | if (r < 0) { | |
781 | log_debug_errno(r, "Failed to send reply packet: %m"); | |
782 | return; | |
783 | } | |
623a4c97 LP |
784 | } |
785 | } | |
ec2c5e43 | 786 | |
f52e61da | 787 | DnsTransaction *dns_scope_find_transaction(DnsScope *scope, DnsResourceKey *key, bool cache_ok) { |
ec2c5e43 LP |
788 | DnsTransaction *t; |
789 | ||
790 | assert(scope); | |
f52e61da | 791 | assert(key); |
ec2c5e43 | 792 | |
da0c630e LP |
793 | /* Try to find an ongoing transaction that is a equal to the |
794 | * specified question */ | |
f9ebb22a | 795 | t = hashmap_get(scope->transactions_by_key, key); |
4a46ed1b LP |
796 | if (!t) |
797 | return NULL; | |
dc4d47e2 | 798 | |
da0c630e LP |
799 | /* Refuse reusing transactions that completed based on cached |
800 | * data instead of a real packet, if that's requested. */ | |
801 | if (!cache_ok && | |
3bbdc31d | 802 | IN_SET(t->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_RCODE_FAILURE) && |
c3bc53e6 | 803 | t->answer_source != DNS_TRANSACTION_NETWORK) |
da0c630e | 804 | return NULL; |
ec2c5e43 | 805 | |
da0c630e | 806 | return t; |
ec2c5e43 | 807 | } |
a4076574 LP |
808 | |
809 | static int dns_scope_make_conflict_packet( | |
810 | DnsScope *s, | |
811 | DnsResourceRecord *rr, | |
812 | DnsPacket **ret) { | |
813 | ||
814 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
815 | int r; | |
816 | ||
817 | assert(s); | |
818 | assert(rr); | |
819 | assert(ret); | |
820 | ||
821 | r = dns_packet_new(&p, s->protocol, 0); | |
822 | if (r < 0) | |
823 | return r; | |
824 | ||
825 | DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS( | |
826 | 0 /* qr */, | |
827 | 0 /* opcode */, | |
828 | 1 /* conflict */, | |
829 | 0 /* tc */, | |
830 | 0 /* t */, | |
831 | 0 /* (ra) */, | |
832 | 0 /* (ad) */, | |
833 | 0 /* (cd) */, | |
834 | 0)); | |
fe2dfc8b DM |
835 | |
836 | /* For mDNS, the transaction ID should always be 0 */ | |
837 | if (s->protocol != DNS_PROTOCOL_MDNS) | |
838 | random_bytes(&DNS_PACKET_HEADER(p)->id, sizeof(uint16_t)); | |
839 | ||
a4076574 LP |
840 | DNS_PACKET_HEADER(p)->qdcount = htobe16(1); |
841 | DNS_PACKET_HEADER(p)->arcount = htobe16(1); | |
842 | ||
58ab31d5 | 843 | r = dns_packet_append_key(p, rr->key, 0, NULL); |
a4076574 LP |
844 | if (r < 0) |
845 | return r; | |
846 | ||
58ab31d5 | 847 | r = dns_packet_append_rr(p, rr, 0, NULL, NULL); |
a4076574 LP |
848 | if (r < 0) |
849 | return r; | |
850 | ||
851 | *ret = p; | |
852 | p = NULL; | |
853 | ||
854 | return 0; | |
855 | } | |
856 | ||
857 | static int on_conflict_dispatch(sd_event_source *es, usec_t usec, void *userdata) { | |
858 | DnsScope *scope = userdata; | |
859 | int r; | |
860 | ||
861 | assert(es); | |
862 | assert(scope); | |
863 | ||
864 | scope->conflict_event_source = sd_event_source_unref(scope->conflict_event_source); | |
865 | ||
866 | for (;;) { | |
867 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
868 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
869 | ||
1e43061b | 870 | rr = ordered_hashmap_steal_first(scope->conflict_queue); |
a4076574 LP |
871 | if (!rr) |
872 | break; | |
873 | ||
874 | r = dns_scope_make_conflict_packet(scope, rr, &p); | |
875 | if (r < 0) { | |
da927ba9 | 876 | log_error_errno(r, "Failed to make conflict packet: %m"); |
a4076574 LP |
877 | return 0; |
878 | } | |
879 | ||
519ef046 | 880 | r = dns_scope_emit_udp(scope, -1, p); |
a4076574 | 881 | if (r < 0) |
da927ba9 | 882 | log_debug_errno(r, "Failed to send conflict packet: %m"); |
a4076574 LP |
883 | } |
884 | ||
885 | return 0; | |
886 | } | |
887 | ||
888 | int dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) { | |
889 | usec_t jitter; | |
890 | int r; | |
891 | ||
892 | assert(scope); | |
893 | assert(rr); | |
894 | ||
895 | /* We don't send these queries immediately. Instead, we queue | |
896 | * them, and send them after some jitter delay. */ | |
1e43061b | 897 | r = ordered_hashmap_ensure_allocated(&scope->conflict_queue, &dns_resource_key_hash_ops); |
a4076574 LP |
898 | if (r < 0) { |
899 | log_oom(); | |
900 | return r; | |
901 | } | |
902 | ||
903 | /* We only place one RR per key in the conflict | |
904 | * messages, not all of them. That should be enough to | |
905 | * indicate where there might be a conflict */ | |
1e43061b | 906 | r = ordered_hashmap_put(scope->conflict_queue, rr->key, rr); |
a4076574 LP |
907 | if (r == -EEXIST || r == 0) |
908 | return 0; | |
f647962d MS |
909 | if (r < 0) |
910 | return log_debug_errno(r, "Failed to queue conflicting RR: %m"); | |
a4076574 LP |
911 | |
912 | dns_resource_record_ref(rr); | |
913 | ||
914 | if (scope->conflict_event_source) | |
915 | return 0; | |
916 | ||
917 | random_bytes(&jitter, sizeof(jitter)); | |
918 | jitter %= LLMNR_JITTER_INTERVAL_USEC; | |
919 | ||
920 | r = sd_event_add_time(scope->manager->event, | |
921 | &scope->conflict_event_source, | |
922 | clock_boottime_or_monotonic(), | |
923 | now(clock_boottime_or_monotonic()) + jitter, | |
924 | LLMNR_JITTER_INTERVAL_USEC, | |
925 | on_conflict_dispatch, scope); | |
f647962d MS |
926 | if (r < 0) |
927 | return log_debug_errno(r, "Failed to add conflict dispatch event: %m"); | |
a4076574 | 928 | |
aa4a9deb LP |
929 | (void) sd_event_source_set_description(scope->conflict_event_source, "scope-conflict"); |
930 | ||
a4076574 LP |
931 | return 0; |
932 | } | |
933 | ||
934 | void dns_scope_check_conflicts(DnsScope *scope, DnsPacket *p) { | |
935 | unsigned i; | |
936 | int r; | |
937 | ||
938 | assert(scope); | |
939 | assert(p); | |
940 | ||
53fda2bb | 941 | if (!IN_SET(p->protocol, DNS_PROTOCOL_LLMNR, DNS_PROTOCOL_MDNS)) |
a4076574 LP |
942 | return; |
943 | ||
944 | if (DNS_PACKET_RRCOUNT(p) <= 0) | |
945 | return; | |
946 | ||
53fda2bb DR |
947 | if (p->protocol == DNS_PROTOCOL_LLMNR) { |
948 | if (DNS_PACKET_LLMNR_C(p) != 0) | |
949 | return; | |
a4076574 | 950 | |
53fda2bb DR |
951 | if (DNS_PACKET_LLMNR_T(p) != 0) |
952 | return; | |
953 | } | |
a4076574 LP |
954 | |
955 | if (manager_our_packet(scope->manager, p)) | |
956 | return; | |
957 | ||
958 | r = dns_packet_extract(p); | |
959 | if (r < 0) { | |
da927ba9 | 960 | log_debug_errno(r, "Failed to extract packet: %m"); |
a4076574 LP |
961 | return; |
962 | } | |
963 | ||
964 | log_debug("Checking for conflicts..."); | |
965 | ||
966 | for (i = 0; i < p->answer->n_rrs; i++) { | |
967 | ||
968 | /* Check for conflicts against the local zone. If we | |
969 | * found one, we won't check any further */ | |
78c6a153 | 970 | r = dns_zone_check_conflicts(&scope->zone, p->answer->items[i].rr); |
a4076574 LP |
971 | if (r != 0) |
972 | continue; | |
973 | ||
974 | /* Check for conflicts against the local cache. If so, | |
975 | * send out an advisory query, to inform everybody */ | |
78c6a153 | 976 | r = dns_cache_check_conflicts(&scope->cache, p->answer->items[i].rr, p->family, &p->sender); |
a4076574 LP |
977 | if (r <= 0) |
978 | continue; | |
979 | ||
78c6a153 | 980 | dns_scope_notify_conflict(scope, p->answer->items[i].rr); |
a4076574 LP |
981 | } |
982 | } | |
4d506d6b LP |
983 | |
984 | void dns_scope_dump(DnsScope *s, FILE *f) { | |
985 | assert(s); | |
986 | ||
987 | if (!f) | |
988 | f = stdout; | |
989 | ||
990 | fputs("[Scope protocol=", f); | |
991 | fputs(dns_protocol_to_string(s->protocol), f); | |
992 | ||
993 | if (s->link) { | |
994 | fputs(" interface=", f); | |
995 | fputs(s->link->name, f); | |
996 | } | |
997 | ||
998 | if (s->family != AF_UNSPEC) { | |
999 | fputs(" family=", f); | |
1000 | fputs(af_to_name(s->family), f); | |
1001 | } | |
1002 | ||
1003 | fputs("]\n", f); | |
1004 | ||
1005 | if (!dns_zone_is_empty(&s->zone)) { | |
1006 | fputs("ZONE:\n", f); | |
1007 | dns_zone_dump(&s->zone, f); | |
1008 | } | |
1009 | ||
1010 | if (!dns_cache_is_empty(&s->cache)) { | |
1011 | fputs("CACHE:\n", f); | |
1012 | dns_cache_dump(&s->cache, f); | |
1013 | } | |
1014 | } | |
a51c1048 LP |
1015 | |
1016 | DnsSearchDomain *dns_scope_get_search_domains(DnsScope *s) { | |
801ad6a6 LP |
1017 | assert(s); |
1018 | ||
a51c1048 LP |
1019 | if (s->protocol != DNS_PROTOCOL_DNS) |
1020 | return NULL; | |
1021 | ||
1022 | if (s->link) | |
1023 | return s->link->search_domains; | |
1024 | ||
28b9b764 | 1025 | return s->manager->search_domains; |
801ad6a6 LP |
1026 | } |
1027 | ||
dc477e73 | 1028 | bool dns_scope_name_needs_search_domain(DnsScope *s, const char *name) { |
801ad6a6 LP |
1029 | assert(s); |
1030 | ||
1031 | if (s->protocol != DNS_PROTOCOL_DNS) | |
dc477e73 | 1032 | return false; |
801ad6a6 | 1033 | |
dc477e73 | 1034 | return dns_name_is_single_label(name); |
801ad6a6 | 1035 | } |
edbcc1fd LP |
1036 | |
1037 | bool dns_scope_network_good(DnsScope *s) { | |
edbcc1fd LP |
1038 | /* Checks whether the network is in good state for lookups on this scope. For mDNS/LLMNR/Classic DNS scopes |
1039 | * bound to links this is easy, as they don't even exist if the link isn't in a suitable state. For the global | |
1040 | * DNS scope we check whether there are any links that are up and have an address. */ | |
1041 | ||
1042 | if (s->link) | |
1043 | return true; | |
1044 | ||
011696f7 | 1045 | return manager_routable(s->manager, AF_UNSPEC); |
edbcc1fd | 1046 | } |
97ebebbc LP |
1047 | |
1048 | int dns_scope_ifindex(DnsScope *s) { | |
1049 | assert(s); | |
1050 | ||
1051 | if (s->link) | |
1052 | return s->link->ifindex; | |
1053 | ||
1054 | return 0; | |
1055 | } | |
53fda2bb DR |
1056 | |
1057 | static int on_announcement_timeout(sd_event_source *s, usec_t usec, void *userdata) { | |
1058 | DnsScope *scope = userdata; | |
1059 | ||
1060 | assert(s); | |
1061 | ||
1062 | scope->announce_event_source = sd_event_source_unref(scope->announce_event_source); | |
1063 | ||
1a63fc54 | 1064 | (void) dns_scope_announce(scope, false); |
53fda2bb DR |
1065 | return 0; |
1066 | } | |
1067 | ||
1a63fc54 | 1068 | int dns_scope_announce(DnsScope *scope, bool goodbye) { |
53fda2bb DR |
1069 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
1070 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
1071 | LinkAddress *a; | |
1072 | int r; | |
1073 | ||
1074 | if (!scope) | |
1a63fc54 | 1075 | return 0; |
53fda2bb DR |
1076 | |
1077 | if (scope->protocol != DNS_PROTOCOL_MDNS) | |
1a63fc54 | 1078 | return 0; |
53fda2bb | 1079 | |
fc0195fa | 1080 | answer = dns_answer_new(scope->link->n_addresses * 2); |
1a63fc54 LP |
1081 | if (!answer) |
1082 | return log_oom(); | |
1083 | ||
53fda2bb | 1084 | LIST_FOREACH(addresses, a, scope->link->addresses) { |
c3ae4188 | 1085 | r = dns_answer_add(answer, a->mdns_address_rr, 0, goodbye ? DNS_ANSWER_GOODBYE : DNS_ANSWER_CACHE_FLUSH); |
1a63fc54 LP |
1086 | if (r < 0) |
1087 | return log_debug_errno(r, "Failed to add address RR to answer: %m"); | |
1088 | ||
c3ae4188 | 1089 | r = dns_answer_add(answer, a->mdns_ptr_rr, 0, goodbye ? DNS_ANSWER_GOODBYE : DNS_ANSWER_CACHE_FLUSH); |
1a63fc54 LP |
1090 | if (r < 0) |
1091 | return log_debug_errno(r, "Failed to add PTR RR to answer: %m"); | |
53fda2bb DR |
1092 | } |
1093 | ||
1094 | if (dns_answer_isempty(answer)) | |
1a63fc54 | 1095 | return 0; |
53fda2bb DR |
1096 | |
1097 | r = dns_scope_make_reply_packet(scope, 0, DNS_RCODE_SUCCESS, NULL, answer, NULL, false, &p); | |
1a63fc54 LP |
1098 | if (r < 0) |
1099 | return log_debug_errno(r, "Failed to build reply packet: %m"); | |
1100 | ||
53fda2bb | 1101 | r = dns_scope_emit_udp(scope, -1, p); |
1a63fc54 LP |
1102 | if (r < 0) |
1103 | return log_debug_errno(r, "Failed to send reply packet: %m"); | |
53fda2bb DR |
1104 | |
1105 | /* In section 8.3 of RFC6762: "The Multicast DNS responder MUST send at least two unsolicited | |
1106 | * responses, one second apart." */ | |
1107 | if (!scope->announced) { | |
1108 | usec_t ts; | |
1109 | ||
1110 | scope->announced = true; | |
1111 | ||
1112 | assert_se(sd_event_now(scope->manager->event, clock_boottime_or_monotonic(), &ts) >= 0); | |
1113 | ts += MDNS_ANNOUNCE_DELAY; | |
1114 | ||
1115 | r = sd_event_add_time( | |
1116 | scope->manager->event, | |
1117 | &scope->announce_event_source, | |
1118 | clock_boottime_or_monotonic(), | |
1119 | ts, | |
1120 | MDNS_JITTER_RANGE_USEC, | |
1121 | on_announcement_timeout, scope); | |
1122 | if (r < 0) | |
1a63fc54 | 1123 | return log_debug_errno(r, "Failed to schedule second announcement: %m"); |
19fee3ef LP |
1124 | |
1125 | (void) sd_event_source_set_description(scope->announce_event_source, "mdns-announce"); | |
53fda2bb | 1126 | } |
1a63fc54 LP |
1127 | |
1128 | return 0; | |
53fda2bb | 1129 | } |