resolved: use DNS_{QUESTION|ANSWER}_FOREACH macros at two more places
[thirdparty/systemd.git] / src / resolve / resolved-manager.c
CommitLineData
091a364c
TG
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
07630cea
LP
20#include <netinet/in.h>
21#include <poll.h>
74b2466e 22#include <sys/ioctl.h>
091a364c 23
a2a416f7 24#include "af-list.h"
b5efdb8a 25#include "alloc-util.h"
943ef07c 26#include "dirent-util.h"
07630cea 27#include "dns-domain.h"
3ffd4af2 28#include "fd-util.h"
a5a807e6 29#include "fileio-label.h"
07630cea 30#include "hostname-util.h"
c004493c 31#include "io-util.h"
07630cea
LP
32#include "netlink-util.h"
33#include "network-internal.h"
822db23c 34#include "ordered-set.h"
6bedfcbb 35#include "parse-util.h"
3df3e884 36#include "random-util.h"
39d8db04 37#include "resolved-bus.h"
07630cea 38#include "resolved-conf.h"
dd0bc0f1 39#include "resolved-etc-hosts.h"
5f402ae8 40#include "resolved-llmnr.h"
07630cea 41#include "resolved-manager.h"
bc7702b0 42#include "resolved-mdns.h"
dd0bc0f1 43#include "resolved-resolv-conf.h"
07630cea 44#include "socket-util.h"
8b43440b 45#include "string-table.h"
07630cea
LP
46#include "string-util.h"
47#include "utf8.h"
74b2466e
LP
48
49#define SEND_TIMEOUT_USEC (200 * USEC_PER_MSEC)
50
1c4baffc 51static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
74b2466e
LP
52 Manager *m = userdata;
53 uint16_t type;
54 Link *l;
55 int ifindex, r;
56
57 assert(rtnl);
58 assert(m);
59 assert(mm);
60
1c4baffc 61 r = sd_netlink_message_get_type(mm, &type);
74b2466e
LP
62 if (r < 0)
63 goto fail;
64
65 r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
66 if (r < 0)
67 goto fail;
68
69 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
70
71 switch (type) {
72
a2a416f7
LP
73 case RTM_NEWLINK:{
74 bool is_new = !l;
74b2466e 75
a2a416f7 76 if (!l) {
74b2466e
LP
77 r = link_new(m, &l, ifindex);
78 if (r < 0)
79 goto fail;
80 }
81
943ef07c 82 r = link_process_rtnl(l, mm);
74b2466e
LP
83 if (r < 0)
84 goto fail;
85
943ef07c 86 r = link_update(l);
21d73c87
LP
87 if (r < 0)
88 goto fail;
89
a2a416f7
LP
90 if (is_new)
91 log_debug("Found new link %i/%s", ifindex, l->name);
92
74b2466e 93 break;
a2a416f7 94 }
74b2466e
LP
95
96 case RTM_DELLINK:
97 if (l) {
a2a416f7 98 log_debug("Removing link %i/%s", l->ifindex, l->name);
943ef07c 99 link_remove_user(l);
74b2466e
LP
100 link_free(l);
101 }
102
103 break;
104 }
105
106 return 0;
107
108fail:
da927ba9 109 log_warning_errno(r, "Failed to process RTNL link message: %m");
74b2466e
LP
110 return 0;
111}
112
1c4baffc 113static int manager_process_address(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
74b2466e
LP
114 Manager *m = userdata;
115 union in_addr_union address;
74b2466e 116 uint16_t type;
0dd25fb9 117 int r, ifindex, family;
74b2466e
LP
118 LinkAddress *a;
119 Link *l;
120
121 assert(rtnl);
122 assert(mm);
123 assert(m);
124
1c4baffc 125 r = sd_netlink_message_get_type(mm, &type);
74b2466e
LP
126 if (r < 0)
127 goto fail;
128
129 r = sd_rtnl_message_addr_get_ifindex(mm, &ifindex);
130 if (r < 0)
131 goto fail;
132
133 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
134 if (!l)
135 return 0;
136
137 r = sd_rtnl_message_addr_get_family(mm, &family);
138 if (r < 0)
139 goto fail;
140
141 switch (family) {
142
143 case AF_INET:
1c4baffc 144 r = sd_netlink_message_read_in_addr(mm, IFA_LOCAL, &address.in);
74b2466e 145 if (r < 0) {
1c4baffc 146 r = sd_netlink_message_read_in_addr(mm, IFA_ADDRESS, &address.in);
74b2466e
LP
147 if (r < 0)
148 goto fail;
149 }
150
151 break;
152
153 case AF_INET6:
1c4baffc 154 r = sd_netlink_message_read_in6_addr(mm, IFA_LOCAL, &address.in6);
74b2466e 155 if (r < 0) {
1c4baffc 156 r = sd_netlink_message_read_in6_addr(mm, IFA_ADDRESS, &address.in6);
74b2466e
LP
157 if (r < 0)
158 goto fail;
159 }
160
161 break;
162
163 default:
164 return 0;
165 }
166
167 a = link_find_address(l, family, &address);
168
169 switch (type) {
170
171 case RTM_NEWADDR:
172
173 if (!a) {
174 r = link_address_new(l, &a, family, &address);
175 if (r < 0)
176 return r;
177 }
178
179 r = link_address_update_rtnl(a, mm);
180 if (r < 0)
181 return r;
182
183 break;
184
185 case RTM_DELADDR:
3e044c49 186 link_address_free(a);
74b2466e
LP
187 break;
188 }
189
190 return 0;
191
192fail:
da927ba9 193 log_warning_errno(r, "Failed to process RTNL address message: %m");
74b2466e
LP
194 return 0;
195}
196
74b2466e 197static int manager_rtnl_listen(Manager *m) {
4afd3348 198 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c4baffc 199 sd_netlink_message *i;
74b2466e
LP
200 int r;
201
202 assert(m);
203
cc98b302 204 /* First, subscribe to interfaces coming and going */
1c4baffc 205 r = sd_netlink_open(&m->rtnl);
74b2466e
LP
206 if (r < 0)
207 return r;
208
2d895038 209 r = sd_netlink_attach_event(m->rtnl, m->event, SD_EVENT_PRIORITY_IMPORTANT);
74b2466e
LP
210 if (r < 0)
211 return r;
212
1c4baffc 213 r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, manager_process_link, m);
74b2466e
LP
214 if (r < 0)
215 return r;
216
1c4baffc 217 r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, manager_process_link, m);
74b2466e
LP
218 if (r < 0)
219 return r;
220
1c4baffc 221 r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, manager_process_address, m);
74b2466e
LP
222 if (r < 0)
223 return r;
091a364c 224
1c4baffc 225 r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, manager_process_address, m);
74b2466e
LP
226 if (r < 0)
227 return r;
228
229 /* Then, enumerate all links */
230 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
231 if (r < 0)
232 return r;
233
1c4baffc 234 r = sd_netlink_message_request_dump(req, true);
74b2466e
LP
235 if (r < 0)
236 return r;
237
1c4baffc 238 r = sd_netlink_call(m->rtnl, req, 0, &reply);
74b2466e
LP
239 if (r < 0)
240 return r;
241
1c4baffc 242 for (i = reply; i; i = sd_netlink_message_next(i)) {
74b2466e
LP
243 r = manager_process_link(m->rtnl, i, m);
244 if (r < 0)
245 return r;
246 }
247
1c4baffc
TG
248 req = sd_netlink_message_unref(req);
249 reply = sd_netlink_message_unref(reply);
74b2466e
LP
250
251 /* Finally, enumerate all addresses, too */
252 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
253 if (r < 0)
254 return r;
255
1c4baffc 256 r = sd_netlink_message_request_dump(req, true);
74b2466e
LP
257 if (r < 0)
258 return r;
259
1c4baffc 260 r = sd_netlink_call(m->rtnl, req, 0, &reply);
74b2466e
LP
261 if (r < 0)
262 return r;
263
1c4baffc 264 for (i = reply; i; i = sd_netlink_message_next(i)) {
74b2466e
LP
265 r = manager_process_address(m->rtnl, i, m);
266 if (r < 0)
267 return r;
268 }
269
270 return r;
271}
272
273static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
274 Manager *m = userdata;
275 Iterator i;
276 Link *l;
277 int r;
278
279 assert(m);
280
281 sd_network_monitor_flush(m->network_monitor);
282
283 HASHMAP_FOREACH(l, m->links, i) {
943ef07c 284 r = link_update(l);
74b2466e 285 if (r < 0)
da927ba9 286 log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex);
74b2466e
LP
287 }
288
7207052d 289 (void) manager_write_resolv_conf(m);
74b2466e
LP
290
291 return 0;
292}
293
294static int manager_network_monitor_listen(Manager *m) {
295 int r, fd, events;
296
297 assert(m);
298
0014a4ad 299 r = sd_network_monitor_new(&m->network_monitor, NULL);
74b2466e
LP
300 if (r < 0)
301 return r;
302
303 fd = sd_network_monitor_get_fd(m->network_monitor);
304 if (fd < 0)
305 return fd;
306
307 events = sd_network_monitor_get_events(m->network_monitor);
308 if (events < 0)
309 return events;
310
311 r = sd_event_add_io(m->event, &m->network_event_source, fd, events, &on_network_event, m);
312 if (r < 0)
2d895038
LP
313 return r;
314
315 r = sd_event_source_set_priority(m->network_event_source, SD_EVENT_PRIORITY_IMPORTANT+5);
316 if (r < 0)
74b2466e
LP
317 return r;
318
aa4a9deb
LP
319 (void) sd_event_source_set_description(m->network_event_source, "network-monitor");
320
74b2466e
LP
321 return 0;
322}
323
78c6a153 324static int determine_hostname(char **llmnr_hostname, char **mdns_hostname) {
eb60f9cd 325 _cleanup_free_ char *h = NULL, *n = NULL;
78c6a153
LP
326 char label[DNS_LABEL_MAX];
327 const char *p;
328 int r, k;
eb60f9cd 329
78c6a153
LP
330 assert(llmnr_hostname);
331 assert(mdns_hostname);
332
333 /* Extract and normalize the first label of the locally
334 * configured hostname, and check it's not "localhost". */
eb60f9cd
LP
335
336 h = gethostname_malloc();
337 if (!h)
338 return log_oom();
339
78c6a153
LP
340 p = h;
341 r = dns_label_unescape(&p, label, sizeof(label));
342 if (r < 0)
343 return log_error_errno(r, "Failed to unescape host name: %m");
344 if (r == 0) {
345 log_error("Couldn't find a single label in hosntame.");
346 return -EINVAL;
347 }
348
349 k = dns_label_undo_idna(label, r, label, sizeof(label));
350 if (k < 0)
351 return log_error_errno(k, "Failed to undo IDNA: %m");
352 if (k > 0)
353 r = k;
354
355 if (!utf8_is_valid(label)) {
eb60f9cd
LP
356 log_error("System hostname is not UTF-8 clean.");
357 return -EINVAL;
358 }
359
422baca0 360 r = dns_label_escape_new(label, r, &n);
78c6a153
LP
361 if (r < 0)
362 return log_error_errno(r, "Failed to escape host name: %m");
363
364 if (is_localhost(n)) {
365 log_debug("System hostname is 'localhost', ignoring.");
366 return -EINVAL;
eb60f9cd
LP
367 }
368
78c6a153
LP
369 r = dns_name_concat(n, "local", mdns_hostname);
370 if (r < 0)
371 return log_error_errno(r, "Failed to determine mDNS hostname: %m");
372
373 *llmnr_hostname = n;
eb60f9cd
LP
374 n = NULL;
375
376 return 0;
377}
378
379static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
78c6a153 380 _cleanup_free_ char *llmnr_hostname = NULL, *mdns_hostname = NULL;
eb60f9cd
LP
381 Manager *m = userdata;
382 int r;
383
384 assert(m);
385
78c6a153 386 r = determine_hostname(&llmnr_hostname, &mdns_hostname);
eb60f9cd
LP
387 if (r < 0)
388 return 0; /* ignore invalid hostnames */
389
78c6a153 390 if (streq(llmnr_hostname, m->llmnr_hostname) && streq(mdns_hostname, m->mdns_hostname))
eb60f9cd
LP
391 return 0;
392
78c6a153
LP
393 log_info("System hostname changed to '%s'.", llmnr_hostname);
394
395 free(m->llmnr_hostname);
396 free(m->mdns_hostname);
397
398 m->llmnr_hostname = llmnr_hostname;
399 m->mdns_hostname = mdns_hostname;
400
401 llmnr_hostname = mdns_hostname = NULL;
eb60f9cd
LP
402
403 manager_refresh_rrs(m);
404
405 return 0;
406}
407
408static int manager_watch_hostname(Manager *m) {
eb60f9cd
LP
409 int r;
410
411 assert(m);
412
413 m->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY|O_NOCTTY);
414 if (m->hostname_fd < 0) {
56f64d95 415 log_warning_errno(errno, "Failed to watch hostname: %m");
eb60f9cd
LP
416 return 0;
417 }
418
419 r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m);
420 if (r < 0) {
421 if (r == -EPERM)
422 /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
423 m->hostname_fd = safe_close(m->hostname_fd);
8d3d7072
MS
424 else
425 return log_error_errno(r, "Failed to add hostname event source: %m");
eb60f9cd
LP
426 }
427
aa4a9deb
LP
428 (void) sd_event_source_set_description(m->hostname_event_source, "hostname");
429
78c6a153 430 r = determine_hostname(&m->llmnr_hostname, &m->mdns_hostname);
eb60f9cd
LP
431 if (r < 0) {
432 log_info("Defaulting to hostname 'linux'.");
78c6a153
LP
433 m->llmnr_hostname = strdup("linux");
434 if (!m->llmnr_hostname)
435 return log_oom();
436
437 m->mdns_hostname = strdup("linux.local");
438 if (!m->mdns_hostname)
eb60f9cd
LP
439 return log_oom();
440 } else
78c6a153 441 log_info("Using system hostname '%s'.", m->llmnr_hostname);
eb60f9cd
LP
442
443 return 0;
444}
445
4d506d6b
LP
446static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
447 _cleanup_free_ char *buffer = NULL;
448 _cleanup_fclose_ FILE *f = NULL;
449 Manager *m = userdata;
450 size_t size = 0;
451 DnsScope *scope;
452
453 assert(s);
454 assert(si);
455 assert(m);
456
457 f = open_memstream(&buffer, &size);
458 if (!f)
459 return log_oom();
460
461 LIST_FOREACH(scopes, scope, m->dns_scopes)
462 dns_scope_dump(scope, f);
463
464 if (fflush_and_check(f) < 0)
465 return log_oom();
466
467 log_dump(LOG_INFO, buffer);
468 return 0;
469}
470
bc81447e
LP
471static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
472 Manager *m = userdata;
bc81447e
LP
473
474 assert(s);
475 assert(si);
476 assert(m);
477
ba35662f 478 manager_flush_caches(m);
ba35662f 479
bc81447e
LP
480 return 0;
481}
482
091a364c 483int manager_new(Manager **ret) {
74b2466e 484 _cleanup_(manager_freep) Manager *m = NULL;
091a364c
TG
485 int r;
486
c92e531c
LP
487 assert(ret);
488
091a364c
TG
489 m = new0(Manager, 1);
490 if (!m)
491 return -ENOMEM;
492
1716f6dc 493 m->llmnr_ipv4_udp_fd = m->llmnr_ipv6_udp_fd = -1;
623a4c97 494 m->llmnr_ipv4_tcp_fd = m->llmnr_ipv6_tcp_fd = -1;
bc7702b0 495 m->mdns_ipv4_fd = m->mdns_ipv6_fd = -1;
eb60f9cd 496 m->hostname_fd = -1;
1716f6dc 497
af49ca27 498 m->llmnr_support = RESOLVE_SUPPORT_YES;
ad6c0475 499 m->mdns_support = RESOLVE_SUPPORT_NO;
61ecb465 500 m->dnssec_mode = DEFAULT_DNSSEC_MODE;
5cb36f41 501 m->read_resolv_conf = true;
00fa60ae 502 m->need_builtin_fallbacks = true;
dd0bc0f1 503 m->etc_hosts_last = m->etc_hosts_mtime = USEC_INFINITY;
091a364c 504
0d2cd476
LP
505 r = dns_trust_anchor_load(&m->trust_anchor);
506 if (r < 0)
507 return r;
508
ad6c0475
LP
509 r = manager_parse_config_file(m);
510 if (r < 0)
511 return r;
512
091a364c
TG
513 r = sd_event_default(&m->event);
514 if (r < 0)
515 return r;
516
517 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
518 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
519
520 sd_event_set_watchdog(m->event, true);
521
eb60f9cd
LP
522 r = manager_watch_hostname(m);
523 if (r < 0)
524 return r;
525
1716f6dc 526 r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC);
74b2466e
LP
527 if (r < 0)
528 return r;
529
530 r = manager_network_monitor_listen(m);
531 if (r < 0)
532 return r;
533
534 r = manager_rtnl_listen(m);
535 if (r < 0)
536 return r;
537
538 r = manager_connect_bus(m);
539 if (r < 0)
540 return r;
541
4d506d6b 542 (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR1, manager_sigusr1, m);
bc81447e 543 (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR2, manager_sigusr2, m);
4d506d6b 544
943ef07c
LP
545 manager_cleanup_saved_user(m);
546
091a364c
TG
547 *ret = m;
548 m = NULL;
549
550 return 0;
551}
552
edc501d4
LP
553int manager_start(Manager *m) {
554 int r;
555
556 assert(m);
557
558 r = manager_llmnr_start(m);
559 if (r < 0)
560 return r;
561
bc7702b0
DM
562 r = manager_mdns_start(m);
563 if (r < 0)
564 return r;
565
edc501d4
LP
566 return 0;
567}
568
74b2466e
LP
569Manager *manager_free(Manager *m) {
570 Link *l;
091a364c
TG
571
572 if (!m)
74b2466e
LP
573 return NULL;
574
4b95f179
LP
575 dns_server_unlink_all(m->dns_servers);
576 dns_server_unlink_all(m->fallback_dns_servers);
a51c1048
LP
577 dns_search_domain_unlink_all(m->search_domains);
578
74b2466e
LP
579 while ((l = hashmap_first(m->links)))
580 link_free(l);
f0e15467
LP
581
582 while (m->dns_queries)
583 dns_query_free(m->dns_queries);
74b2466e 584
cab5b059
LP
585 dns_scope_free(m->unicast_scope);
586
f0e15467
LP
587 hashmap_free(m->links);
588 hashmap_free(m->dns_transactions);
589
096b6773
LP
590 sd_event_source_unref(m->network_event_source);
591 sd_network_monitor_unref(m->network_monitor);
091a364c 592
a564ca2f
LP
593 sd_netlink_unref(m->rtnl);
594 sd_event_source_unref(m->rtnl_event_source);
595
edc501d4 596 manager_llmnr_stop(m);
bc7702b0 597 manager_mdns_stop(m);
623a4c97 598
902bb5d8 599 sd_bus_slot_unref(m->prepare_for_sleep_slot);
74b2466e
LP
600 sd_event_source_unref(m->bus_retry_event_source);
601 sd_bus_unref(m->bus);
091a364c 602
4d506d6b 603 sd_event_source_unref(m->sigusr1_event_source);
bc81447e 604 sd_event_source_unref(m->sigusr2_event_source);
4d506d6b 605
74b2466e 606 sd_event_unref(m->event);
623a4c97 607
78c6a153
LP
608 dns_resource_key_unref(m->llmnr_host_ipv4_key);
609 dns_resource_key_unref(m->llmnr_host_ipv6_key);
eb60f9cd 610
eb60f9cd 611 sd_event_source_unref(m->hostname_event_source);
d9fcf2ba 612 safe_close(m->hostname_fd);
78c6a153
LP
613 free(m->llmnr_hostname);
614 free(m->mdns_hostname);
eb60f9cd 615
0d2cd476 616 dns_trust_anchor_flush(&m->trust_anchor);
dd0bc0f1 617 manager_etc_hosts_flush(m);
0d2cd476 618
091a364c 619 free(m);
74b2466e
LP
620
621 return NULL;
091a364c
TG
622}
623
1716f6dc 624int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
74b2466e 625 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1716f6dc
LP
626 union {
627 struct cmsghdr header; /* For alignment */
40a1eebd 628 uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
1716f6dc 629 + CMSG_SPACE(int) /* ttl/hoplimit */
623a4c97 630 + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
1716f6dc
LP
631 } control;
632 union sockaddr_union sa;
74b2466e 633 struct msghdr mh = {};
1716f6dc 634 struct cmsghdr *cmsg;
74b2466e 635 struct iovec iov;
4edc2c9b
LP
636 ssize_t ms, l;
637 int r;
74b2466e
LP
638
639 assert(m);
1716f6dc 640 assert(fd >= 0);
74b2466e
LP
641 assert(ret);
642
4edc2c9b 643 ms = next_datagram_size_fd(fd);
74b2466e 644 if (ms < 0)
4edc2c9b 645 return ms;
74b2466e 646
1716f6dc 647 r = dns_packet_new(&p, protocol, ms);
74b2466e
LP
648 if (r < 0)
649 return r;
650
651 iov.iov_base = DNS_PACKET_DATA(p);
652 iov.iov_len = p->allocated;
653
1716f6dc
LP
654 mh.msg_name = &sa.sa;
655 mh.msg_namelen = sizeof(sa);
74b2466e
LP
656 mh.msg_iov = &iov;
657 mh.msg_iovlen = 1;
1716f6dc
LP
658 mh.msg_control = &control;
659 mh.msg_controllen = sizeof(control);
74b2466e 660
a38d9945 661 l = recvmsg(fd, &mh, 0);
f134289a
EV
662 if (l == 0)
663 return 0;
74b2466e 664 if (l < 0) {
ad867662 665 if (errno == EAGAIN || errno == EINTR)
74b2466e
LP
666 return 0;
667
668 return -errno;
091a364c
TG
669 }
670
1716f6dc
LP
671 assert(!(mh.msg_flags & MSG_CTRUNC));
672 assert(!(mh.msg_flags & MSG_TRUNC));
673
74b2466e 674 p->size = (size_t) l;
091a364c 675
1716f6dc 676 p->family = sa.sa.sa_family;
623a4c97
LP
677 p->ipproto = IPPROTO_UDP;
678 if (p->family == AF_INET) {
1716f6dc 679 p->sender.in = sa.in.sin_addr;
623a4c97
LP
680 p->sender_port = be16toh(sa.in.sin_port);
681 } else if (p->family == AF_INET6) {
1716f6dc 682 p->sender.in6 = sa.in6.sin6_addr;
623a4c97
LP
683 p->sender_port = be16toh(sa.in6.sin6_port);
684 p->ifindex = sa.in6.sin6_scope_id;
685 } else
1716f6dc 686 return -EAFNOSUPPORT;
74b2466e 687
2a1288ff 688 CMSG_FOREACH(cmsg, &mh) {
74b2466e 689
1716f6dc
LP
690 if (cmsg->cmsg_level == IPPROTO_IPV6) {
691 assert(p->family == AF_INET6);
74b2466e 692
1716f6dc 693 switch (cmsg->cmsg_type) {
74b2466e 694
1716f6dc
LP
695 case IPV6_PKTINFO: {
696 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
74b2466e 697
623a4c97
LP
698 if (p->ifindex <= 0)
699 p->ifindex = i->ipi6_ifindex;
700
1716f6dc
LP
701 p->destination.in6 = i->ipi6_addr;
702 break;
703 }
74b2466e 704
1716f6dc
LP
705 case IPV6_HOPLIMIT:
706 p->ttl = *(int *) CMSG_DATA(cmsg);
707 break;
74b2466e 708
1716f6dc
LP
709 }
710 } else if (cmsg->cmsg_level == IPPROTO_IP) {
711 assert(p->family == AF_INET);
74b2466e 712
1716f6dc 713 switch (cmsg->cmsg_type) {
74b2466e 714
1716f6dc
LP
715 case IP_PKTINFO: {
716 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
091a364c 717
623a4c97
LP
718 if (p->ifindex <= 0)
719 p->ifindex = i->ipi_ifindex;
720
1716f6dc
LP
721 p->destination.in = i->ipi_addr;
722 break;
723 }
74b2466e 724
623a4c97 725 case IP_TTL:
1716f6dc
LP
726 p->ttl = *(int *) CMSG_DATA(cmsg);
727 break;
728 }
729 }
730 }
74b2466e 731
623a4c97
LP
732 /* The Linux kernel sets the interface index to the loopback
733 * device if the packet came from the local host since it
734 * avoids the routing table in such a case. Let's unset the
735 * interface index in such a case. */
a5f03596 736 if (p->ifindex == LOOPBACK_IFINDEX)
623a4c97
LP
737 p->ifindex = 0;
738
86ad4cd7
TG
739 if (protocol != DNS_PROTOCOL_DNS) {
740 /* If we don't know the interface index still, we look for the
741 * first local interface with a matching address. Yuck! */
742 if (p->ifindex <= 0)
743 p->ifindex = manager_find_ifindex(m, p->family, &p->destination);
744 }
623a4c97 745
74b2466e
LP
746 *ret = p;
747 p = NULL;
748
749 return 1;
750}
751
74b2466e
LP
752static int sendmsg_loop(int fd, struct msghdr *mh, int flags) {
753 int r;
754
755 assert(fd >= 0);
756 assert(mh);
757
758 for (;;) {
759 if (sendmsg(fd, mh, flags) >= 0)
760 return 0;
761
762 if (errno == EINTR)
763 continue;
764
765 if (errno != EAGAIN)
766 return -errno;
767
768 r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC);
769 if (r < 0)
770 return r;
771 if (r == 0)
772 return -ETIMEDOUT;
773 }
774}
775
72290734
TG
776static int write_loop(int fd, void *message, size_t length) {
777 int r;
778
779 assert(fd >= 0);
780 assert(message);
781
782 for (;;) {
783 if (write(fd, message, length) >= 0)
784 return 0;
785
786 if (errno == EINTR)
787 continue;
788
789 if (errno != EAGAIN)
790 return -errno;
791
792 r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC);
793 if (r < 0)
794 return r;
795 if (r == 0)
796 return -ETIMEDOUT;
797 }
798}
799
800int manager_write(Manager *m, int fd, DnsPacket *p) {
801 int r;
802
deb3f3d3 803 log_debug("Sending %s packet with id %" PRIu16 ".", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p));
72290734
TG
804
805 r = write_loop(fd, DNS_PACKET_DATA(p), p->size);
806 if (r < 0)
807 return r;
808
809 return 0;
810}
811
623a4c97 812static int manager_ipv4_send(Manager *m, int fd, int ifindex, const struct in_addr *addr, uint16_t port, DnsPacket *p) {
74b2466e
LP
813 union sockaddr_union sa = {
814 .in.sin_family = AF_INET,
74b2466e 815 };
1716f6dc
LP
816 union {
817 struct cmsghdr header; /* For alignment */
818 uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
819 } control;
74b2466e
LP
820 struct msghdr mh = {};
821 struct iovec iov;
74b2466e
LP
822
823 assert(m);
1716f6dc
LP
824 assert(fd >= 0);
825 assert(addr);
826 assert(port > 0);
74b2466e
LP
827 assert(p);
828
74b2466e
LP
829 iov.iov_base = DNS_PACKET_DATA(p);
830 iov.iov_len = p->size;
091a364c 831
1716f6dc
LP
832 sa.in.sin_addr = *addr;
833 sa.in.sin_port = htobe16(port),
091a364c 834
74b2466e
LP
835 mh.msg_iov = &iov;
836 mh.msg_iovlen = 1;
837 mh.msg_name = &sa.sa;
838 mh.msg_namelen = sizeof(sa.in);
091a364c 839
74b2466e
LP
840 if (ifindex > 0) {
841 struct cmsghdr *cmsg;
842 struct in_pktinfo *pi;
843
844 zero(control);
845
1716f6dc 846 mh.msg_control = &control;
74b2466e
LP
847 mh.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
848
849 cmsg = CMSG_FIRSTHDR(&mh);
850 cmsg->cmsg_len = mh.msg_controllen;
851 cmsg->cmsg_level = IPPROTO_IP;
852 cmsg->cmsg_type = IP_PKTINFO;
853
854 pi = (struct in_pktinfo*) CMSG_DATA(cmsg);
855 pi->ipi_ifindex = ifindex;
856 }
857
858 return sendmsg_loop(fd, &mh, 0);
859}
860
623a4c97 861static int manager_ipv6_send(Manager *m, int fd, int ifindex, const struct in6_addr *addr, uint16_t port, DnsPacket *p) {
74b2466e
LP
862 union sockaddr_union sa = {
863 .in6.sin6_family = AF_INET6,
74b2466e 864 };
1716f6dc
LP
865 union {
866 struct cmsghdr header; /* For alignment */
867 uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))];
868 } control;
74b2466e
LP
869 struct msghdr mh = {};
870 struct iovec iov;
74b2466e
LP
871
872 assert(m);
1716f6dc
LP
873 assert(fd >= 0);
874 assert(addr);
875 assert(port > 0);
74b2466e
LP
876 assert(p);
877
74b2466e
LP
878 iov.iov_base = DNS_PACKET_DATA(p);
879 iov.iov_len = p->size;
880
1716f6dc
LP
881 sa.in6.sin6_addr = *addr;
882 sa.in6.sin6_port = htobe16(port),
74b2466e
LP
883 sa.in6.sin6_scope_id = ifindex;
884
885 mh.msg_iov = &iov;
886 mh.msg_iovlen = 1;
887 mh.msg_name = &sa.sa;
888 mh.msg_namelen = sizeof(sa.in6);
889
890 if (ifindex > 0) {
891 struct cmsghdr *cmsg;
892 struct in6_pktinfo *pi;
893
894 zero(control);
895
1716f6dc 896 mh.msg_control = &control;
74b2466e
LP
897 mh.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo));
898
899 cmsg = CMSG_FIRSTHDR(&mh);
900 cmsg->cmsg_len = mh.msg_controllen;
901 cmsg->cmsg_level = IPPROTO_IPV6;
902 cmsg->cmsg_type = IPV6_PKTINFO;
903
904 pi = (struct in6_pktinfo*) CMSG_DATA(cmsg);
905 pi->ipi6_ifindex = ifindex;
906 }
907
908 return sendmsg_loop(fd, &mh, 0);
909}
910
623a4c97 911int manager_send(Manager *m, int fd, int ifindex, int family, const union in_addr_union *addr, uint16_t port, DnsPacket *p) {
1716f6dc
LP
912 assert(m);
913 assert(fd >= 0);
914 assert(addr);
915 assert(port > 0);
916 assert(p);
917
deb3f3d3 918 log_debug("Sending %s packet with id %" PRIu16 " on interface %i/%s.", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p), ifindex, af_to_name(family));
a2a416f7 919
1716f6dc
LP
920 if (family == AF_INET)
921 return manager_ipv4_send(m, fd, ifindex, &addr->in, port, p);
2817157b 922 if (family == AF_INET6)
1716f6dc
LP
923 return manager_ipv6_send(m, fd, ifindex, &addr->in6, port, p);
924
925 return -EAFNOSUPPORT;
926}
927
e1c95994
LP
928uint32_t manager_find_mtu(Manager *m) {
929 uint32_t mtu = 0;
930 Link *l;
931 Iterator i;
932
933 /* If we don't know on which link a DNS packet would be
934 * delivered, let's find the largest MTU that works on all
935 * interfaces we know of */
936
937 HASHMAP_FOREACH(l, m->links, i) {
938 if (l->mtu <= 0)
939 continue;
940
941 if (mtu <= 0 || l->mtu < mtu)
942 mtu = l->mtu;
943 }
944
945 return mtu;
946}
1716f6dc 947
623a4c97 948int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) {
ec2c5e43
LP
949 LinkAddress *a;
950
951 assert(m);
952
4e945a6f 953 a = manager_find_link_address(m, family, in_addr);
ec2c5e43
LP
954 if (a)
955 return a->link->ifindex;
956
957 return 0;
958}
959
eb60f9cd
LP
960void manager_refresh_rrs(Manager *m) {
961 Iterator i;
962 Link *l;
963
964 assert(m);
965
78c6a153
LP
966 m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key);
967 m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key);
eb60f9cd
LP
968
969 HASHMAP_FOREACH(l, m->links, i) {
970 link_add_rrs(l, true);
971 link_add_rrs(l, false);
972 }
973}
974
ec2c5e43
LP
975int manager_next_hostname(Manager *m) {
976 const char *p;
556a2294 977 uint64_t u, a;
78c6a153
LP
978 char *h, *k;
979 int r;
623a4c97
LP
980
981 assert(m);
982
78c6a153 983 p = strchr(m->llmnr_hostname, 0);
ec2c5e43
LP
984 assert(p);
985
78c6a153 986 while (p > m->llmnr_hostname) {
ec2c5e43
LP
987 if (!strchr("0123456789", p[-1]))
988 break;
989
990 p--;
991 }
992
993 if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0)
994 u = 1;
995
556a2294
LP
996 /* Add a random number to the old value. This way we can avoid
997 * that two hosts pick the same hostname, win on IPv4 and lose
998 * on IPv6 (or vice versa), and pick the same hostname
999 * replacement hostname, ad infinitum. We still want the
1000 * numbers to go up monotonically, hence we just add a random
1001 * value 1..10 */
1002
1003 random_bytes(&a, sizeof(a));
1004 u += 1 + a % 10;
ec2c5e43 1005
78c6a153 1006 if (asprintf(&h, "%.*s%" PRIu64, (int) (p - m->llmnr_hostname), m->llmnr_hostname, u) < 0)
ec2c5e43
LP
1007 return -ENOMEM;
1008
78c6a153
LP
1009 r = dns_name_concat(h, "local", &k);
1010 if (r < 0) {
1011 free(h);
1012 return r;
1013 }
1014
1015 log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h);
1016
1017 free(m->llmnr_hostname);
1018 m->llmnr_hostname = h;
ec2c5e43 1019
78c6a153
LP
1020 free(m->mdns_hostname);
1021 m->mdns_hostname = k;
ec2c5e43 1022
eb60f9cd 1023 manager_refresh_rrs(m);
623a4c97
LP
1024
1025 return 0;
1026}
ec2c5e43 1027
4e945a6f 1028LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) {
ec2c5e43
LP
1029 Iterator i;
1030 Link *l;
1031
1032 assert(m);
1033
1034 HASHMAP_FOREACH(l, m->links, i) {
1035 LinkAddress *a;
1036
1037 a = link_find_address(l, family, in_addr);
1038 if (a)
1039 return a;
1040 }
1041
1042 return NULL;
1043}
1044
a4076574 1045bool manager_our_packet(Manager *m, DnsPacket *p) {
ec2c5e43
LP
1046 assert(m);
1047 assert(p);
1048
4e945a6f 1049 return !!manager_find_link_address(m, p->family, &p->sender);
ec2c5e43 1050}
4e945a6f 1051
a4076574
LP
1052DnsScope* manager_find_scope(Manager *m, DnsPacket *p) {
1053 Link *l;
1054
1055 assert(m);
1056 assert(p);
1057
1058 l = hashmap_get(m->links, INT_TO_PTR(p->ifindex));
1059 if (!l)
1060 return NULL;
1061
b4f1862d
DM
1062 switch (p->protocol) {
1063 case DNS_PROTOCOL_LLMNR:
a4076574
LP
1064 if (p->family == AF_INET)
1065 return l->llmnr_ipv4_scope;
1066 else if (p->family == AF_INET6)
1067 return l->llmnr_ipv6_scope;
b4f1862d
DM
1068
1069 break;
1070
1071 case DNS_PROTOCOL_MDNS:
1072 if (p->family == AF_INET)
1073 return l->mdns_ipv4_scope;
1074 else if (p->family == AF_INET6)
1075 return l->mdns_ipv6_scope;
1076
1077 break;
1078
1079 default:
1080 break;
a4076574
LP
1081 }
1082
1083 return NULL;
1084}
1085
902bb5d8
LP
1086void manager_verify_all(Manager *m) {
1087 DnsScope *s;
1088
1089 assert(m);
1090
1091 LIST_FOREACH(scopes, s, m->dns_scopes)
1092 dns_zone_verify_all(&s->zone);
1093}
1094
78c6a153 1095int manager_is_own_hostname(Manager *m, const char *name) {
78c6a153
LP
1096 int r;
1097
1098 assert(m);
1099 assert(name);
1100
1101 if (m->llmnr_hostname) {
1102 r = dns_name_equal(name, m->llmnr_hostname);
1103 if (r != 0)
1104 return r;
1105 }
1106
1107 if (m->mdns_hostname)
1108 return dns_name_equal(name, m->mdns_hostname);
1109
1110 return 0;
1111}
1112
9176a57c
LP
1113int manager_compile_dns_servers(Manager *m, OrderedSet **dns) {
1114 DnsServer *s;
1115 Iterator i;
1116 Link *l;
1117 int r;
1118
1119 assert(m);
1120 assert(dns);
1121
1122 r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops);
1123 if (r < 0)
1124 return r;
1125
1126 /* First add the system-wide servers and domains */
1127 LIST_FOREACH(servers, s, m->dns_servers) {
1128 r = ordered_set_put(*dns, s);
1129 if (r == -EEXIST)
1130 continue;
1131 if (r < 0)
1132 return r;
1133 }
1134
1135 /* Then, add the per-link servers */
1136 HASHMAP_FOREACH(l, m->links, i) {
1137 LIST_FOREACH(servers, s, l->dns_servers) {
1138 r = ordered_set_put(*dns, s);
1139 if (r == -EEXIST)
1140 continue;
1141 if (r < 0)
1142 return r;
1143 }
1144 }
1145
1146 /* If we found nothing, add the fallback servers */
1147 if (ordered_set_isempty(*dns)) {
1148 LIST_FOREACH(servers, s, m->fallback_dns_servers) {
1149 r = ordered_set_put(*dns, s);
1150 if (r == -EEXIST)
1151 continue;
1152 if (r < 0)
1153 return r;
1154 }
1155 }
1156
1157 return 0;
1158}
1159
6f7da49d 1160int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) {
9176a57c
LP
1161 DnsSearchDomain *d;
1162 Iterator i;
1163 Link *l;
1164 int r;
1165
1166 assert(m);
1167 assert(domains);
1168
1169 r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops);
1170 if (r < 0)
1171 return r;
1172
1173 LIST_FOREACH(domains, d, m->search_domains) {
6f7da49d
LP
1174
1175 if (filter_route >= 0 &&
1176 d->route_only != !!filter_route)
1177 continue;
1178
9176a57c
LP
1179 r = ordered_set_put(*domains, d->name);
1180 if (r == -EEXIST)
1181 continue;
1182 if (r < 0)
1183 return r;
1184 }
1185
1186 HASHMAP_FOREACH(l, m->links, i) {
1187
1188 LIST_FOREACH(domains, d, l->search_domains) {
6f7da49d
LP
1189
1190 if (filter_route >= 0 &&
1191 d->route_only != !!filter_route)
1192 continue;
1193
9176a57c
LP
1194 r = ordered_set_put(*domains, d->name);
1195 if (r == -EEXIST)
1196 continue;
1197 if (r < 0)
1198 return r;
1199 }
1200 }
1201
1202 return 0;
1203}
c69fa7e3
LP
1204
1205DnssecMode manager_get_dnssec_mode(Manager *m) {
1206 assert(m);
1207
1208 if (m->dnssec_mode != _DNSSEC_MODE_INVALID)
1209 return m->dnssec_mode;
1210
1211 return DNSSEC_NO;
1212}
1213
1214bool manager_dnssec_supported(Manager *m) {
1215 DnsServer *server;
1216 Iterator i;
1217 Link *l;
1218
1219 assert(m);
1220
1221 if (manager_get_dnssec_mode(m) == DNSSEC_NO)
1222 return false;
1223
1224 server = manager_get_dns_server(m);
1225 if (server && !dns_server_dnssec_supported(server))
1226 return false;
1227
1228 HASHMAP_FOREACH(l, m->links, i)
1229 if (!link_dnssec_supported(l))
1230 return false;
1231
1232 return true;
1233}
59c5b597
LP
1234
1235void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) {
1236
1237 assert(verdict >= 0);
1238 assert(verdict < _DNSSEC_VERDICT_MAX);
1239
1240 if (log_get_max_level() >= LOG_DEBUG) {
202b76ae 1241 char s[DNS_RESOURCE_KEY_STRING_MAX];
59c5b597 1242
202b76ae
ZJS
1243 log_debug("Found verdict for lookup %s: %s",
1244 dns_resource_key_to_string(key, s, sizeof s),
1245 dnssec_verdict_to_string(verdict));
59c5b597
LP
1246 }
1247
1248 m->n_dnssec_verdict[verdict]++;
1249}
011696f7
LP
1250
1251bool manager_routable(Manager *m, int family) {
1252 Iterator i;
1253 Link *l;
1254
1255 assert(m);
1256
1257 /* Returns true if the host has at least one interface with a routable address of the specified type */
1258
1259 HASHMAP_FOREACH(l, m->links, i)
1260 if (link_relevant(l, family, false))
1261 return true;
1262
1263 return false;
1264}
ba35662f
LP
1265
1266void manager_flush_caches(Manager *m) {
1267 DnsScope *scope;
1268
1269 assert(m);
1270
1271 LIST_FOREACH(scopes, scope, m->dns_scopes)
1272 dns_cache_flush(&scope->cache);
aac57b35
ZJS
1273
1274 log_info("Flushed all caches.");
ba35662f 1275}
943ef07c
LP
1276
1277void manager_cleanup_saved_user(Manager *m) {
1278 _cleanup_closedir_ DIR *d = NULL;
1279 struct dirent *de;
1280 int r;
1281
1282 assert(m);
1283
1284 /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface
1285 * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can
1286 * be restarted without losing this data. */
1287
1288 d = opendir("/run/systemd/resolve/netif/");
1289 if (!d) {
1290 if (errno == ENOENT)
1291 return;
1292
1293 log_warning_errno(errno, "Failed to open interface directory: %m");
1294 return;
1295 }
1296
1297 FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) {
1298 _cleanup_free_ char *p = NULL;
1299 int ifindex;
1300 Link *l;
1301
1302 if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG))
1303 continue;
1304
1305 if (STR_IN_SET(de->d_name, ".", ".."))
1306 continue;
1307
1308 r = parse_ifindex(de->d_name, &ifindex);
1309 if (r < 0) /* Probably some temporary file from a previous run. Delete it */
1310 goto rm;
1311
1312 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1313 if (!l) /* link vanished */
1314 goto rm;
1315
1316 if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */
1317 goto rm;
1318
1319 continue;
1320
1321 rm:
1322 p = strappend("/run/systemd/resolve/netif/", de->d_name);
1323 if (!p) {
1324 log_oom();
1325 return;
1326 }
1327
1328 (void) unlink(p);
1329 }
1330}