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