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