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