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