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