Bug Summary

File:build-scan/../src/resolve/resolved-manager.c
Warning:line 446, column 13
Null pointer passed to 1st parameter expecting 'nonnull'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name resolved-manager.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fdenormal-fp-math=preserve-sign,preserve-sign -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/12.0.0 -include config.h -I systemd-resolved.p -I . -I .. -I src/basic -I ../src/basic -I src/shared -I ../src/shared -I src/systemd -I ../src/systemd -I src/journal -I ../src/journal -I src/journal-remote -I ../src/journal-remote -I src/nspawn -I ../src/nspawn -I src/resolve -I ../src/resolve -I src/timesync -I ../src/timesync -I ../src/time-wait-sync -I src/login -I ../src/login -I src/udev -I ../src/udev -I src/libudev -I ../src/libudev -I src/core -I ../src/core -I ../src/libsystemd/sd-bus -I ../src/libsystemd/sd-device -I ../src/libsystemd/sd-hwdb -I ../src/libsystemd/sd-id128 -I ../src/libsystemd/sd-netlink -I ../src/libsystemd/sd-network -I src/libsystemd-network -I ../src/libsystemd-network -I /usr/include/p11-kit-1 -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-unused-parameter -Wno-missing-field-initializers -Wno-unused-result -Wno-format-signedness -Wno-error=nonnull -std=gnu99 -fconst-strings -fdebug-compilation-dir /home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan -ferror-limit 19 -fvisibility hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-07-16-221226-1465241-1 -x c ../src/resolve/resolved-manager.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <netinet/in.h>
4#include <poll.h>
5#include <stdio_ext.h>
6#include <sys/ioctl.h>
7
8#if HAVE_LIBIDN21
9#include <idn2.h>
10#endif
11
12#include "af-list.h"
13#include "alloc-util.h"
14#include "dirent-util.h"
15#include "dns-domain.h"
16#include "fd-util.h"
17#include "fileio-label.h"
18#include "hostname-util.h"
19#include "io-util.h"
20#include "netlink-util.h"
21#include "network-internal.h"
22#include "ordered-set.h"
23#include "parse-util.h"
24#include "random-util.h"
25#include "resolved-bus.h"
26#include "resolved-conf.h"
27#include "resolved-dnssd.h"
28#include "resolved-dns-stub.h"
29#include "resolved-etc-hosts.h"
30#include "resolved-llmnr.h"
31#include "resolved-manager.h"
32#include "resolved-mdns.h"
33#include "resolved-resolv-conf.h"
34#include "socket-util.h"
35#include "string-table.h"
36#include "string-util.h"
37#include "utf8.h"
38
39#define SEND_TIMEOUT_USEC(200 * ((usec_t) 1000ULL)) (200 * USEC_PER_MSEC((usec_t) 1000ULL))
40
41static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
42 Manager *m = userdata;
43 uint16_t type;
44 Link *l;
45 int ifindex, r;
46
47 assert(rtnl)do { if ((__builtin_expect(!!(!(rtnl)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("rtnl"), "../src/resolve/resolved-manager.c"
, 47, __PRETTY_FUNCTION__); } while (0)
;
48 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 48, __PRETTY_FUNCTION__); } while (0)
;
49 assert(mm)do { if ((__builtin_expect(!!(!(mm)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("mm"), "../src/resolve/resolved-manager.c"
, 49, __PRETTY_FUNCTION__); } while (0)
;
50
51 r = sd_netlink_message_get_type(mm, &type);
52 if (r < 0)
53 goto fail;
54
55 r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
56 if (r < 0)
57 goto fail;
58
59 l = hashmap_get(m->links, INT_TO_PTR(ifindex)((void *) ((intptr_t) (ifindex))));
60
61 switch (type) {
62
63 case RTM_NEWLINKRTM_NEWLINK:{
64 bool_Bool is_new = !l;
65
66 if (!l) {
67 r = link_new(m, &l, ifindex);
68 if (r < 0)
69 goto fail;
70 }
71
72 r = link_process_rtnl(l, mm);
73 if (r < 0)
74 goto fail;
75
76 r = link_update(l);
77 if (r < 0)
78 goto fail;
79
80 if (is_new)
81 log_debug("Found new link %i/%s", ifindex, l->name)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 81, __func__, "Found new link %i/%s"
, ifindex, l->name) : -abs(_e); })
;
82
83 break;
84 }
85
86 case RTM_DELLINKRTM_DELLINK:
87 if (l) {
88 log_debug("Removing link %i/%s", l->ifindex, l->name)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 88, __func__, "Removing link %i/%s"
, l->ifindex, l->name) : -abs(_e); })
;
89 link_remove_user(l);
90 link_free(l);
91 }
92
93 break;
94 }
95
96 return 0;
97
98fail:
99 log_warning_errno(r, "Failed to process RTNL link message: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 99, __func__, "Failed to process RTNL link message: %m"
) : -abs(_e); })
;
100 return 0;
101}
102
103static int manager_process_address(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
104 Manager *m = userdata;
105 union in_addr_union address;
106 uint16_t type;
107 int r, ifindex, family;
108 LinkAddress *a;
109 Link *l;
110
111 assert(rtnl)do { if ((__builtin_expect(!!(!(rtnl)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("rtnl"), "../src/resolve/resolved-manager.c"
, 111, __PRETTY_FUNCTION__); } while (0)
;
112 assert(mm)do { if ((__builtin_expect(!!(!(mm)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("mm"), "../src/resolve/resolved-manager.c"
, 112, __PRETTY_FUNCTION__); } while (0)
;
113 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 113, __PRETTY_FUNCTION__); } while (0)
;
114
115 r = sd_netlink_message_get_type(mm, &type);
116 if (r < 0)
117 goto fail;
118
119 r = sd_rtnl_message_addr_get_ifindex(mm, &ifindex);
120 if (r < 0)
121 goto fail;
122
123 l = hashmap_get(m->links, INT_TO_PTR(ifindex)((void *) ((intptr_t) (ifindex))));
124 if (!l)
125 return 0;
126
127 r = sd_rtnl_message_addr_get_family(mm, &family);
128 if (r < 0)
129 goto fail;
130
131 switch (family) {
132
133 case AF_INET2:
134 r = sd_netlink_message_read_in_addr(mm, IFA_LOCAL, &address.in);
135 if (r < 0) {
136 r = sd_netlink_message_read_in_addr(mm, IFA_ADDRESS, &address.in);
137 if (r < 0)
138 goto fail;
139 }
140
141 break;
142
143 case AF_INET610:
144 r = sd_netlink_message_read_in6_addr(mm, IFA_LOCAL, &address.in6);
145 if (r < 0) {
146 r = sd_netlink_message_read_in6_addr(mm, IFA_ADDRESS, &address.in6);
147 if (r < 0)
148 goto fail;
149 }
150
151 break;
152
153 default:
154 return 0;
155 }
156
157 a = link_find_address(l, family, &address);
158
159 switch (type) {
160
161 case RTM_NEWADDRRTM_NEWADDR:
162
163 if (!a) {
164 r = link_address_new(l, &a, family, &address);
165 if (r < 0)
166 return r;
167 }
168
169 r = link_address_update_rtnl(a, mm);
170 if (r < 0)
171 return r;
172
173 break;
174
175 case RTM_DELADDRRTM_DELADDR:
176 link_address_free(a);
177 break;
178 }
179
180 return 0;
181
182fail:
183 log_warning_errno(r, "Failed to process RTNL address message: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 183, __func__, "Failed to process RTNL address message: %m"
) : -abs(_e); })
;
184 return 0;
185}
186
187static int manager_rtnl_listen(Manager *m) {
188 _cleanup_(sd_netlink_message_unrefp)__attribute__((cleanup(sd_netlink_message_unrefp))) sd_netlink_message *req = NULL((void*)0), *reply = NULL((void*)0);
189 sd_netlink_message *i;
190 int r;
191
192 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 192, __PRETTY_FUNCTION__); } while (0)
;
193
194 /* First, subscribe to interfaces coming and going */
195 r = sd_netlink_open(&m->rtnl);
196 if (r < 0)
197 return r;
198
199 r = sd_netlink_attach_event(m->rtnl, m->event, SD_EVENT_PRIORITY_IMPORTANT);
200 if (r < 0)
201 return r;
202
203 r = sd_netlink_add_match(m->rtnl, RTM_NEWLINKRTM_NEWLINK, manager_process_link, m);
204 if (r < 0)
205 return r;
206
207 r = sd_netlink_add_match(m->rtnl, RTM_DELLINKRTM_DELLINK, manager_process_link, m);
208 if (r < 0)
209 return r;
210
211 r = sd_netlink_add_match(m->rtnl, RTM_NEWADDRRTM_NEWADDR, manager_process_address, m);
212 if (r < 0)
213 return r;
214
215 r = sd_netlink_add_match(m->rtnl, RTM_DELADDRRTM_DELADDR, manager_process_address, m);
216 if (r < 0)
217 return r;
218
219 /* Then, enumerate all links */
220 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINKRTM_GETLINK, 0);
221 if (r < 0)
222 return r;
223
224 r = sd_netlink_message_request_dump(req, true1);
225 if (r < 0)
226 return r;
227
228 r = sd_netlink_call(m->rtnl, req, 0, &reply);
229 if (r < 0)
230 return r;
231
232 for (i = reply; i; i = sd_netlink_message_next(i)) {
233 r = manager_process_link(m->rtnl, i, m);
234 if (r < 0)
235 return r;
236 }
237
238 req = sd_netlink_message_unref(req);
239 reply = sd_netlink_message_unref(reply);
240
241 /* Finally, enumerate all addresses, too */
242 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDRRTM_GETADDR, 0, AF_UNSPEC0);
243 if (r < 0)
244 return r;
245
246 r = sd_netlink_message_request_dump(req, true1);
247 if (r < 0)
248 return r;
249
250 r = sd_netlink_call(m->rtnl, req, 0, &reply);
251 if (r < 0)
252 return r;
253
254 for (i = reply; i; i = sd_netlink_message_next(i)) {
255 r = manager_process_address(m->rtnl, i, m);
256 if (r < 0)
257 return r;
258 }
259
260 return r;
261}
262
263static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
264 Manager *m = userdata;
265 Iterator i;
266 Link *l;
267 int r;
268
269 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 269, __PRETTY_FUNCTION__); } while (0)
;
270
271 sd_network_monitor_flush(m->network_monitor);
272
273 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
{
274 r = link_update(l);
275 if (r < 0)
276 log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex)({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 276, __func__, "Failed to update monitor information for %i: %m"
, l->ifindex) : -abs(_e); })
;
277 }
278
279 (void) manager_write_resolv_conf(m);
280
281 return 0;
282}
283
284static int manager_network_monitor_listen(Manager *m) {
285 int r, fd, events;
286
287 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 287, __PRETTY_FUNCTION__); } while (0)
;
288
289 r = sd_network_monitor_new(&m->network_monitor, NULL((void*)0));
290 if (r < 0)
291 return r;
292
293 fd = sd_network_monitor_get_fd(m->network_monitor);
294 if (fd < 0)
295 return fd;
296
297 events = sd_network_monitor_get_events(m->network_monitor);
298 if (events < 0)
299 return events;
300
301 r = sd_event_add_io(m->event, &m->network_event_source, fd, events, &on_network_event, m);
302 if (r < 0)
303 return r;
304
305 r = sd_event_source_set_priority(m->network_event_source, SD_EVENT_PRIORITY_IMPORTANT+5);
306 if (r < 0)
307 return r;
308
309 (void) sd_event_source_set_description(m->network_event_source, "network-monitor");
310
311 return 0;
312}
313
314static int determine_hostname(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) {
315 _cleanup_free___attribute__((cleanup(freep))) char *h = NULL((void*)0), *n = NULL((void*)0);
316#if HAVE_LIBIDN21
317 _cleanup_free___attribute__((cleanup(freep))) char *utf8 = NULL((void*)0);
318#elif HAVE_LIBIDN0
319 int k;
320#endif
321 char label[DNS_LABEL_MAX63];
322 const char *p, *decoded;
323 int r;
324
325 assert(full_hostname)do { if ((__builtin_expect(!!(!(full_hostname)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("full_hostname"), "../src/resolve/resolved-manager.c"
, 325, __PRETTY_FUNCTION__); } while (0)
;
6
Taking false branch
7
Loop condition is false. Exiting loop
326 assert(llmnr_hostname)do { if ((__builtin_expect(!!(!(llmnr_hostname)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("llmnr_hostname"), "../src/resolve/resolved-manager.c"
, 326, __PRETTY_FUNCTION__); } while (0)
;
8
Taking false branch
9
Loop condition is false. Exiting loop
327 assert(mdns_hostname)do { if ((__builtin_expect(!!(!(mdns_hostname)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("mdns_hostname"), "../src/resolve/resolved-manager.c"
, 327, __PRETTY_FUNCTION__); } while (0)
;
10
Taking false branch
11
Loop condition is false. Exiting loop
328
329 /* Extract and normalize the first label of the locally configured hostname, and check it's not "localhost". */
330
331 r = gethostname_strict(&h);
332 if (r < 0)
12
Assuming 'r' is < 0
13
Taking true branch
333 return log_debug_errno(r, "Can't determine system hostname: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 333, __func__, "Can't determine system hostname: %m"
) : -abs(_e); })
;
14
Assuming the condition is false
15
'?' condition is false
16
Returning without writing to '*full_hostname'
334
335 p = h;
336 r = dns_label_unescape(&p, label, sizeof label);
337 if (r < 0)
338 return log_error_errno(r, "Failed to unescape host name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 338, __func__, "Failed to unescape host name: %m"
) : -abs(_e); })
;
339 if (r == 0) {
340 log_error("Couldn't find a single label in hostname.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 340, __func__, "Couldn't find a single label in hostname."
) : -abs(_e); })
;
341 return -EINVAL22;
342 }
343
344#if HAVE_LIBIDN21
345 r = idn2_to_unicode_8z8z(label, &utf8, 0);
346 if (r != IDN2_OK)
347 return log_error("Failed to undo IDNA: %s", idn2_strerror(r))({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 347, __func__, "Failed to undo IDNA: %s"
, idn2_strerror(r)) : -abs(_e); })
;
348 assert(utf8_is_valid(utf8))do { if ((__builtin_expect(!!(!(utf8_is_valid(utf8))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("utf8_is_valid(utf8)"), "../src/resolve/resolved-manager.c"
, 348, __PRETTY_FUNCTION__); } while (0)
;
349
350 r = strlen(utf8);
351 decoded = utf8;
352#elif HAVE_LIBIDN0
353 k = dns_label_undo_idna(label, r, label, sizeof label);
354 if (k < 0)
355 return log_error_errno(k, "Failed to undo IDNA: %m")({ int _level = ((3)), _e = ((k)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 355, __func__, "Failed to undo IDNA: %m"
) : -abs(_e); })
;
356 if (k > 0)
357 r = k;
358
359 if (!utf8_is_valid(label)) {
360 log_error("System hostname is not UTF-8 clean.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 360, __func__, "System hostname is not UTF-8 clean."
) : -abs(_e); })
;
361 return -EINVAL22;
362 }
363 decoded = label;
364#else
365 decoded = label; /* no decoding */
366#endif
367
368 r = dns_label_escape_new(decoded, r, &n);
369 if (r < 0)
370 return log_error_errno(r, "Failed to escape host name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 370, __func__, "Failed to escape host name: %m"
) : -abs(_e); })
;
371
372 if (is_localhost(n)) {
373 log_debug("System hostname is 'localhost', ignoring.")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 373, __func__, "System hostname is 'localhost', ignoring."
) : -abs(_e); })
;
374 return -EINVAL22;
375 }
376
377 r = dns_name_concat(n, "local", mdns_hostname);
378 if (r < 0)
379 return log_error_errno(r, "Failed to determine mDNS hostname: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 379, __func__, "Failed to determine mDNS hostname: %m"
) : -abs(_e); })
;
380
381 *llmnr_hostname = TAKE_PTR(n)({ typeof(n) _ptr_ = (n); (n) = ((void*)0); _ptr_; });
382 *full_hostname = TAKE_PTR(h)({ typeof(h) _ptr_ = (h); (h) = ((void*)0); _ptr_; });
383
384 return 0;
385}
386
387static const char *fallback_hostname(void) {
388
389 /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it to be
390 * "localhost" even if that's the compiled in hostname. In this case, let's revert to "linux" instead. */
391
392 if (is_localhost(FALLBACK_HOSTNAME"localhost"))
393 return "linux";
394
395 return FALLBACK_HOSTNAME"localhost";
396}
397
398static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) {
399 _cleanup_free___attribute__((cleanup(freep))) char *n = NULL((void*)0), *m = NULL((void*)0);
400 char label[DNS_LABEL_MAX63], *h;
401 const char *p;
402 int r;
403
404 assert(full_hostname)do { if ((__builtin_expect(!!(!(full_hostname)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("full_hostname"), "../src/resolve/resolved-manager.c"
, 404, __PRETTY_FUNCTION__); } while (0)
;
405 assert(llmnr_hostname)do { if ((__builtin_expect(!!(!(llmnr_hostname)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("llmnr_hostname"), "../src/resolve/resolved-manager.c"
, 405, __PRETTY_FUNCTION__); } while (0)
;
406 assert(mdns_hostname)do { if ((__builtin_expect(!!(!(mdns_hostname)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("mdns_hostname"), "../src/resolve/resolved-manager.c"
, 406, __PRETTY_FUNCTION__); } while (0)
;
407
408 p = fallback_hostname();
409 r = dns_label_unescape(&p, label, sizeof(label));
410 if (r < 0)
411 return log_error_errno(r, "Failed to unescape fallback host name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 411, __func__, "Failed to unescape fallback host name: %m"
) : -abs(_e); })
;
412
413 assert(r > 0)do { if ((__builtin_expect(!!(!(r > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("r > 0"), "../src/resolve/resolved-manager.c"
, 413, __PRETTY_FUNCTION__); } while (0)
; /* The fallback hostname must have at least one label */
414
415 r = dns_label_escape_new(label, r, &n);
416 if (r < 0)
417 return log_error_errno(r, "Failed to escape fallback hostname: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 417, __func__, "Failed to escape fallback hostname: %m"
) : -abs(_e); })
;
418
419 r = dns_name_concat(n, "local", &m);
420 if (r < 0)
421 return log_error_errno(r, "Failed to concatenate mDNS hostname: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 421, __func__, "Failed to concatenate mDNS hostname: %m"
) : -abs(_e); })
;
422
423 h = strdup(fallback_hostname());
424 if (!h)
425 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/resolve/resolved-manager.c"
, 425, __func__)
;
426
427 *llmnr_hostname = TAKE_PTR(n)({ typeof(n) _ptr_ = (n); (n) = ((void*)0); _ptr_; });
428 *mdns_hostname = TAKE_PTR(m)({ typeof(m) _ptr_ = (m); (m) = ((void*)0); _ptr_; });
429
430 *full_hostname = h;
431
432 return 0;
433}
434
435static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
436 _cleanup_free___attribute__((cleanup(freep))) char *full_hostname = NULL((void*)0), *llmnr_hostname = NULL((void*)0), *mdns_hostname = NULL((void*)0);
1
'full_hostname' initialized to a null pointer value
437 Manager *m = userdata;
438 int r;
439
440 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 440, __PRETTY_FUNCTION__); } while (0)
;
2
Assuming 'm' is non-null
3
Taking false branch
4
Loop condition is false. Exiting loop
441
442 r = determine_hostname(&full_hostname, &llmnr_hostname, &mdns_hostname);
5
Calling 'determine_hostname'
17
Returning from 'determine_hostname'
443 if (r < 0)
18
Assuming 'r' is >= 0
19
Taking false branch
444 return 0; /* ignore invalid hostnames */
445
446 if (streq(full_hostname, m->full_hostname)(strcmp((full_hostname),(m->full_hostname)) == 0) &&
20
Null pointer passed to 1st parameter expecting 'nonnull'
447 streq(llmnr_hostname, m->llmnr_hostname)(strcmp((llmnr_hostname),(m->llmnr_hostname)) == 0) &&
448 streq(mdns_hostname, m->mdns_hostname)(strcmp((mdns_hostname),(m->mdns_hostname)) == 0))
449 return 0;
450
451 log_info("System hostname changed to '%s'.", full_hostname)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 451, __func__, "System hostname changed to '%s'."
, full_hostname) : -abs(_e); })
;
452
453 free_and_replace(m->full_hostname, full_hostname)({ free(m->full_hostname); (m->full_hostname) = (full_hostname
); (full_hostname) = ((void*)0); 0; })
;
454 free_and_replace(m->llmnr_hostname, llmnr_hostname)({ free(m->llmnr_hostname); (m->llmnr_hostname) = (llmnr_hostname
); (llmnr_hostname) = ((void*)0); 0; })
;
455 free_and_replace(m->mdns_hostname, mdns_hostname)({ free(m->mdns_hostname); (m->mdns_hostname) = (mdns_hostname
); (mdns_hostname) = ((void*)0); 0; })
;
456
457 manager_refresh_rrs(m);
458
459 return 0;
460}
461
462static int manager_watch_hostname(Manager *m) {
463 int r;
464
465 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 465, __PRETTY_FUNCTION__); } while (0)
;
466
467 m->hostname_fd = open("/proc/sys/kernel/hostname",
468 O_RDONLY00|O_CLOEXEC02000000|O_NONBLOCK04000|O_NOCTTY0400);
469 if (m->hostname_fd < 0) {
470 log_warning_errno(errno, "Failed to watch hostname: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/resolve/resolved-manager.c", 470
, __func__, "Failed to watch hostname: %m") : -abs(_e); })
;
471 return 0;
472 }
473
474 r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m);
475 if (r < 0) {
476 if (r == -EPERM1)
477 /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
478 m->hostname_fd = safe_close(m->hostname_fd);
479 else
480 return log_error_errno(r, "Failed to add hostname event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 480, __func__, "Failed to add hostname event source: %m"
) : -abs(_e); })
;
481 }
482
483 (void) sd_event_source_set_description(m->hostname_event_source, "hostname");
484
485 r = determine_hostname(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname);
486 if (r < 0) {
487 log_info("Defaulting to hostname '%s'.", fallback_hostname())({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 487, __func__, "Defaulting to hostname '%s'."
, fallback_hostname()) : -abs(_e); })
;
488
489 r = make_fallback_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname);
490 if (r < 0)
491 return r;
492 } else
493 log_info("Using system hostname '%s'.", m->full_hostname)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 493, __func__, "Using system hostname '%s'."
, m->full_hostname) : -abs(_e); })
;
494
495 return 0;
496}
497
498static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
499 _cleanup_free___attribute__((cleanup(freep))) char *buffer = NULL((void*)0);
500 _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0);
501 Manager *m = userdata;
502 DnsServer *server;
503 size_t size = 0;
504 DnsScope *scope;
505 Iterator i;
506 Link *l;
507
508 assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("s"), "../src/resolve/resolved-manager.c"
, 508, __PRETTY_FUNCTION__); } while (0)
;
509 assert(si)do { if ((__builtin_expect(!!(!(si)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("si"), "../src/resolve/resolved-manager.c"
, 509, __PRETTY_FUNCTION__); } while (0)
;
510 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 510, __PRETTY_FUNCTION__); } while (0)
;
511
512 f = open_memstream(&buffer, &size);
513 if (!f)
514 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/resolve/resolved-manager.c"
, 514, __func__)
;
515
516 (void) __fsetlocking(f, FSETLOCKING_BYCALLERFSETLOCKING_BYCALLER);
517
518 LIST_FOREACH(scopes, scope, m->dns_scopes)for ((scope) = (m->dns_scopes); (scope); (scope) = (scope)
->scopes_next)
519 dns_scope_dump(scope, f);
520
521 LIST_FOREACH(servers, server, m->dns_servers)for ((server) = (m->dns_servers); (server); (server) = (server
)->servers_next)
522 dns_server_dump(server, f);
523 LIST_FOREACH(servers, server, m->fallback_dns_servers)for ((server) = (m->fallback_dns_servers); (server); (server
) = (server)->servers_next)
524 dns_server_dump(server, f);
525 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
526 LIST_FOREACH(servers, server, l->dns_servers)for ((server) = (l->dns_servers); (server); (server) = (server
)->servers_next)
527 dns_server_dump(server, f);
528
529 if (fflush_and_check(f) < 0)
530 return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/resolve/resolved-manager.c"
, 530, __func__)
;
531
532 log_dump(LOG_INFO, buffer)log_dump_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0,
"../src/resolve/resolved-manager.c", 532, __func__, buffer)
;
533 return 0;
534}
535
536static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
537 Manager *m = userdata;
538
539 assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("s"), "../src/resolve/resolved-manager.c"
, 539, __PRETTY_FUNCTION__); } while (0)
;
540 assert(si)do { if ((__builtin_expect(!!(!(si)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("si"), "../src/resolve/resolved-manager.c"
, 540, __PRETTY_FUNCTION__); } while (0)
;
541 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 541, __PRETTY_FUNCTION__); } while (0)
;
542
543 manager_flush_caches(m);
544
545 return 0;
546}
547
548static int manager_sigrtmin1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
549 Manager *m = userdata;
550
551 assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("s"), "../src/resolve/resolved-manager.c"
, 551, __PRETTY_FUNCTION__); } while (0)
;
552 assert(si)do { if ((__builtin_expect(!!(!(si)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("si"), "../src/resolve/resolved-manager.c"
, 552, __PRETTY_FUNCTION__); } while (0)
;
553 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 553, __PRETTY_FUNCTION__); } while (0)
;
554
555 manager_reset_server_features(m);
556 return 0;
557}
558
559int manager_new(Manager **ret) {
560 _cleanup_(manager_freep)__attribute__((cleanup(manager_freep))) Manager *m = NULL((void*)0);
561 int r;
562
563 assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/resolve/resolved-manager.c"
, 563, __PRETTY_FUNCTION__); } while (0)
;
564
565 m = new0(Manager, 1)((Manager*) calloc((1), sizeof(Manager)));
566 if (!m)
567 return -ENOMEM12;
568
569 m->llmnr_ipv4_udp_fd = m->llmnr_ipv6_udp_fd = -1;
570 m->llmnr_ipv4_tcp_fd = m->llmnr_ipv6_tcp_fd = -1;
571 m->mdns_ipv4_fd = m->mdns_ipv6_fd = -1;
572 m->dns_stub_udp_fd = m->dns_stub_tcp_fd = -1;
573 m->hostname_fd = -1;
574
575 m->llmnr_support = RESOLVE_SUPPORT_YES;
576 m->mdns_support = RESOLVE_SUPPORT_YES;
577 m->dnssec_mode = DEFAULT_DNSSEC_MODEDNSSEC_ALLOW_DOWNGRADE;
578 m->dns_over_tls_mode = DEFAULT_DNS_OVER_TLS_MODEDNS_OVER_TLS_NO;
579 m->enable_cache = true1;
580 m->dns_stub_listener_mode = DNS_STUB_LISTENER_UDP;
581 m->read_resolv_conf = true1;
582 m->need_builtin_fallbacks = true1;
583 m->etc_hosts_last = m->etc_hosts_mtime = USEC_INFINITY((usec_t) -1);
584
585 r = dns_trust_anchor_load(&m->trust_anchor);
586 if (r < 0)
587 return r;
588
589 r = manager_parse_config_file(m);
590 if (r < 0)
591 log_warning_errno(r, "Failed to parse configuration file: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 591, __func__, "Failed to parse configuration file: %m"
) : -abs(_e); })
;
592
593 r = sd_event_default(&m->event);
594 if (r < 0)
595 return r;
596
597 sd_event_add_signal(m->event, NULL((void*)0), SIGTERM15, NULL((void*)0), NULL((void*)0));
598 sd_event_add_signal(m->event, NULL((void*)0), SIGINT2, NULL((void*)0), NULL((void*)0));
599
600 sd_event_set_watchdog(m->event, true1);
601
602 r = manager_watch_hostname(m);
603 if (r < 0)
604 return r;
605
606 r = dnssd_load(m);
607 if (r < 0)
608 log_warning_errno(r, "Failed to load DNS-SD configuration files: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 608, __func__, "Failed to load DNS-SD configuration files: %m"
) : -abs(_e); })
;
609
610 r = dns_scope_new(m, &m->unicast_scope, NULL((void*)0), DNS_PROTOCOL_DNS, AF_UNSPEC0);
611 if (r < 0)
612 return r;
613
614 r = manager_network_monitor_listen(m);
615 if (r < 0)
616 return r;
617
618 r = manager_rtnl_listen(m);
619 if (r < 0)
620 return r;
621
622 r = manager_connect_bus(m);
623 if (r < 0)
624 return r;
625
626 (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR110, manager_sigusr1, m);
627 (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR212, manager_sigusr2, m);
628 (void) sd_event_add_signal(m->event, &m->sigrtmin1_event_source, SIGRTMIN(__libc_current_sigrtmin ())+1, manager_sigrtmin1, m);
629
630 manager_cleanup_saved_user(m);
631
632 *ret = TAKE_PTR(m)({ typeof(m) _ptr_ = (m); (m) = ((void*)0); _ptr_; });
633
634 return 0;
635}
636
637int manager_start(Manager *m) {
638 int r;
639
640 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 640, __PRETTY_FUNCTION__); } while (0)
;
641
642 r = manager_dns_stub_start(m);
643 if (r < 0)
644 return r;
645
646 return 0;
647}
648
649Manager *manager_free(Manager *m) {
650 Link *l;
651 DnssdService *s;
652
653 if (!m)
654 return NULL((void*)0);
655
656 dns_server_unlink_all(m->dns_servers);
657 dns_server_unlink_all(m->fallback_dns_servers);
658 dns_search_domain_unlink_all(m->search_domains);
659
660 while ((l = hashmap_first(m->links)))
661 link_free(l);
662
663 while (m->dns_queries)
664 dns_query_free(m->dns_queries);
665
666 dns_scope_free(m->unicast_scope);
667
668 /* At this point only orphaned streams should remain. All others should have been freed already by their
669 * owners */
670 while (m->dns_streams)
671 dns_stream_unref(m->dns_streams);
672
673 hashmap_free(m->links);
674 hashmap_free(m->dns_transactions);
675
676 sd_event_source_unref(m->network_event_source);
677 sd_network_monitor_unref(m->network_monitor);
678
679 sd_netlink_unref(m->rtnl);
680 sd_event_source_unref(m->rtnl_event_source);
681
682 manager_llmnr_stop(m);
683 manager_mdns_stop(m);
684 manager_dns_stub_stop(m);
685
686 sd_bus_slot_unref(m->prepare_for_sleep_slot);
687 sd_bus_unref(m->bus);
688
689 sd_event_source_unref(m->sigusr1_event_source);
690 sd_event_source_unref(m->sigusr2_event_source);
691 sd_event_source_unref(m->sigrtmin1_event_source);
692
693 sd_event_unref(m->event);
694
695 dns_resource_key_unref(m->llmnr_host_ipv4_key);
696 dns_resource_key_unref(m->llmnr_host_ipv6_key);
697 dns_resource_key_unref(m->mdns_host_ipv4_key);
698 dns_resource_key_unref(m->mdns_host_ipv6_key);
699
700 sd_event_source_unref(m->hostname_event_source);
701 safe_close(m->hostname_fd);
702
703 free(m->full_hostname);
704 free(m->llmnr_hostname);
705 free(m->mdns_hostname);
706
707 while ((s = hashmap_first(m->dnssd_services)))
708 dnssd_service_free(s);
709 hashmap_free(m->dnssd_services);
710
711 dns_trust_anchor_flush(&m->trust_anchor);
712 manager_etc_hosts_flush(m);
713
714 return mfree(m);
715}
716
717int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
718 _cleanup_(dns_packet_unrefp)__attribute__((cleanup(dns_packet_unrefp))) DnsPacket *p = NULL((void*)0);
719 union {
720 struct cmsghdr header; /* For alignment */
721 uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))(((((sizeof(union __attribute__ ((packed)) { typeof(struct in_pktinfo
) a; typeof(struct in6_pktinfo) b; }))) + sizeof (size_t) - 1
) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr
)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1
)))
722 + CMSG_SPACE(int)((((int) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t
) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) &
(size_t) ~(sizeof (size_t) - 1)))
/* ttl/hoplimit */
723 + EXTRA_CMSG_SPACE1024 /* kernel appears to require extra buffer space */];
724 } control;
725 union sockaddr_union sa;
726 struct msghdr mh = {};
727 struct cmsghdr *cmsg;
728 struct iovec iov;
729 ssize_t ms, l;
730 int r;
731
732 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 732, __PRETTY_FUNCTION__); } while (0)
;
733 assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/resolve/resolved-manager.c"
, 733, __PRETTY_FUNCTION__); } while (0)
;
734 assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/resolve/resolved-manager.c"
, 734, __PRETTY_FUNCTION__); } while (0)
;
735
736 ms = next_datagram_size_fd(fd);
737 if (ms < 0)
738 return ms;
739
740 r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX0xFFFFu);
741 if (r < 0)
742 return r;
743
744 iov.iov_base = DNS_PACKET_DATA(p);
745 iov.iov_len = p->allocated;
746
747 mh.msg_name = &sa.sa;
748 mh.msg_namelen = sizeof(sa);
749 mh.msg_iov = &iov;
750 mh.msg_iovlen = 1;
751 mh.msg_control = &control;
752 mh.msg_controllen = sizeof(control);
753
754 l = recvmsg(fd, &mh, 0);
755 if (l == 0)
756 return 0;
757 if (l < 0) {
758 if (IN_SET(errno, EAGAIN, EINTR)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){11, 4})/sizeof(int)]; switch((*__errno_location
())) { case 11: case 4: _found = 1; break; default: break; }
_found; })
)
759 return 0;
760
761 return -errno(*__errno_location ());
762 }
763
764 assert(!(mh.msg_flags & MSG_CTRUNC))do { if ((__builtin_expect(!!(!(!(mh.msg_flags & MSG_CTRUNC
))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!(mh.msg_flags & MSG_CTRUNC)"
), "../src/resolve/resolved-manager.c", 764, __PRETTY_FUNCTION__
); } while (0)
;
765 assert(!(mh.msg_flags & MSG_TRUNC))do { if ((__builtin_expect(!!(!(!(mh.msg_flags & MSG_TRUNC
))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!(mh.msg_flags & MSG_TRUNC)"
), "../src/resolve/resolved-manager.c", 765, __PRETTY_FUNCTION__
); } while (0)
;
766
767 p->size = (size_t) l;
768
769 p->family = sa.sa.sa_family;
770 p->ipproto = IPPROTO_UDPIPPROTO_UDP;
771 if (p->family == AF_INET2) {
772 p->sender.in = sa.in.sin_addr;
773 p->sender_port = be16toh(sa.in.sin_port);
774 } else if (p->family == AF_INET610) {
775 p->sender.in6 = sa.in6.sin6_addr;
776 p->sender_port = be16toh(sa.in6.sin6_port);
777 p->ifindex = sa.in6.sin6_scope_id;
778 } else
779 return -EAFNOSUPPORT97;
780
781 CMSG_FOREACH(cmsg, &mh)for ((cmsg) = ((size_t) (&mh)->msg_controllen >= sizeof
(struct cmsghdr) ? (struct cmsghdr *) (&mh)->msg_control
: (struct cmsghdr *) 0); (cmsg); (cmsg) = __cmsg_nxthdr ((&
mh), (cmsg)))
{
782
783 if (cmsg->cmsg_level == IPPROTO_IPV6IPPROTO_IPV6) {
784 assert(p->family == AF_INET6)do { if ((__builtin_expect(!!(!(p->family == 10)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p->family == AF_INET6"), "../src/resolve/resolved-manager.c"
, 784, __PRETTY_FUNCTION__); } while (0)
;
785
786 switch (cmsg->cmsg_type) {
787
788 case IPV6_PKTINFO50: {
789 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg)((cmsg)->__cmsg_data);
790
791 if (p->ifindex <= 0)
792 p->ifindex = i->ipi6_ifindex;
793
794 p->destination.in6 = i->ipi6_addr;
795 break;
796 }
797
798 case IPV6_HOPLIMIT52:
799 p->ttl = *(int *) CMSG_DATA(cmsg)((cmsg)->__cmsg_data);
800 break;
801
802 }
803 } else if (cmsg->cmsg_level == IPPROTO_IPIPPROTO_IP) {
804 assert(p->family == AF_INET)do { if ((__builtin_expect(!!(!(p->family == 2)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p->family == AF_INET"), "../src/resolve/resolved-manager.c"
, 804, __PRETTY_FUNCTION__); } while (0)
;
805
806 switch (cmsg->cmsg_type) {
807
808 case IP_PKTINFO8: {
809 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg)((cmsg)->__cmsg_data);
810
811 if (p->ifindex <= 0)
812 p->ifindex = i->ipi_ifindex;
813
814 p->destination.in = i->ipi_addr;
815 break;
816 }
817
818 case IP_TTL2:
819 p->ttl = *(int *) CMSG_DATA(cmsg)((cmsg)->__cmsg_data);
820 break;
821 }
822 }
823 }
824
825 /* The Linux kernel sets the interface index to the loopback
826 * device if the packet came from the local host since it
827 * avoids the routing table in such a case. Let's unset the
828 * interface index in such a case. */
829 if (p->ifindex == LOOPBACK_IFINDEX1)
830 p->ifindex = 0;
831
832 if (protocol != DNS_PROTOCOL_DNS) {
833 /* If we don't know the interface index still, we look for the
834 * first local interface with a matching address. Yuck! */
835 if (p->ifindex <= 0)
836 p->ifindex = manager_find_ifindex(m, p->family, &p->destination);
837 }
838
839 *ret = TAKE_PTR(p)({ typeof(p) _ptr_ = (p); (p) = ((void*)0); _ptr_; });
840
841 return 1;
842}
843
844static int sendmsg_loop(int fd, struct msghdr *mh, int flags) {
845 int r;
846
847 assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/resolve/resolved-manager.c"
, 847, __PRETTY_FUNCTION__); } while (0)
;
848 assert(mh)do { if ((__builtin_expect(!!(!(mh)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("mh"), "../src/resolve/resolved-manager.c"
, 848, __PRETTY_FUNCTION__); } while (0)
;
849
850 for (;;) {
851 if (sendmsg(fd, mh, flags) >= 0)
852 return 0;
853
854 if (errno(*__errno_location ()) == EINTR4)
855 continue;
856
857 if (errno(*__errno_location ()) != EAGAIN11)
858 return -errno(*__errno_location ());
859
860 r = fd_wait_for_event(fd, POLLOUT0x004, SEND_TIMEOUT_USEC(200 * ((usec_t) 1000ULL)));
861 if (r < 0)
862 return r;
863 if (r == 0)
864 return -ETIMEDOUT110;
865 }
866}
867
868static int write_loop(int fd, void *message, size_t length) {
869 int r;
870
871 assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/resolve/resolved-manager.c"
, 871, __PRETTY_FUNCTION__); } while (0)
;
872 assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("message"), "../src/resolve/resolved-manager.c"
, 872, __PRETTY_FUNCTION__); } while (0)
;
873
874 for (;;) {
875 if (write(fd, message, length) >= 0)
876 return 0;
877
878 if (errno(*__errno_location ()) == EINTR4)
879 continue;
880
881 if (errno(*__errno_location ()) != EAGAIN11)
882 return -errno(*__errno_location ());
883
884 r = fd_wait_for_event(fd, POLLOUT0x004, SEND_TIMEOUT_USEC(200 * ((usec_t) 1000ULL)));
885 if (r < 0)
886 return r;
887 if (r == 0)
888 return -ETIMEDOUT110;
889 }
890}
891
892int manager_write(Manager *m, int fd, DnsPacket *p) {
893 int r;
894
895 log_debug("Sending %s packet with id %" PRIu16 ".", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 895, __func__, "Sending %s packet with id %"
"u" ".", ((be16toh(((DnsPacketHeader*) DNS_PACKET_DATA(p))->
flags) >> 15) & 1) ? "response" : "query", ((DnsPacketHeader
*) DNS_PACKET_DATA(p))->id) : -abs(_e); })
;
896
897 r = write_loop(fd, DNS_PACKET_DATA(p), p->size);
898 if (r < 0)
899 return r;
900
901 return 0;
902}
903
904static int manager_ipv4_send(
905 Manager *m,
906 int fd,
907 int ifindex,
908 const struct in_addr *destination,
909 uint16_t port,
910 const struct in_addr *source,
911 DnsPacket *p) {
912 union sockaddr_union sa = {
913 .in.sin_family = AF_INET2,
914 };
915 union {
916 struct cmsghdr header; /* For alignment */
917 uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))((((sizeof(struct in_pktinfo)) + sizeof (size_t) - 1) & (
size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr))
+ sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)
))
];
918 } control;
919 struct msghdr mh = {};
920 struct iovec iov;
921
922 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 922, __PRETTY_FUNCTION__); } while (0)
;
923 assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/resolve/resolved-manager.c"
, 923, __PRETTY_FUNCTION__); } while (0)
;
924 assert(destination)do { if ((__builtin_expect(!!(!(destination)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("destination"), "../src/resolve/resolved-manager.c"
, 924, __PRETTY_FUNCTION__); } while (0)
;
925 assert(port > 0)do { if ((__builtin_expect(!!(!(port > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("port > 0"), "../src/resolve/resolved-manager.c"
, 925, __PRETTY_FUNCTION__); } while (0)
;
926 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/resolve/resolved-manager.c"
, 926, __PRETTY_FUNCTION__); } while (0)
;
927
928 iov.iov_base = DNS_PACKET_DATA(p);
929 iov.iov_len = p->size;
930
931 sa.in.sin_addr = *destination;
932 sa.in.sin_port = htobe16(port),
933
934 mh.msg_iov = &iov;
935 mh.msg_iovlen = 1;
936 mh.msg_name = &sa.sa;
937 mh.msg_namelen = sizeof(sa.in);
938
939 if (ifindex > 0) {
940 struct cmsghdr *cmsg;
941 struct in_pktinfo *pi;
942
943 zero(control)(({ size_t _l_ = (sizeof(control)); void *_x_ = (&(control
)); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); }))
;
944
945 mh.msg_control = &control;
946 mh.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo))((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t
) ~(sizeof (size_t) - 1)) + (sizeof(struct in_pktinfo)))
;
947
948 cmsg = CMSG_FIRSTHDR(&mh)((size_t) (&mh)->msg_controllen >= sizeof (struct cmsghdr
) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr
*) 0)
;
949 cmsg->cmsg_len = mh.msg_controllen;
950 cmsg->cmsg_level = IPPROTO_IPIPPROTO_IP;
951 cmsg->cmsg_type = IP_PKTINFO8;
952
953 pi = (struct in_pktinfo*) CMSG_DATA(cmsg)((cmsg)->__cmsg_data);
954 pi->ipi_ifindex = ifindex;
955
956 if (source)
957 pi->ipi_spec_dst = *source;
958 }
959
960 return sendmsg_loop(fd, &mh, 0);
961}
962
963static int manager_ipv6_send(
964 Manager *m,
965 int fd,
966 int ifindex,
967 const struct in6_addr *destination,
968 uint16_t port,
969 const struct in6_addr *source,
970 DnsPacket *p) {
971
972 union sockaddr_union sa = {
973 .in6.sin6_family = AF_INET610,
974 };
975 union {
976 struct cmsghdr header; /* For alignment */
977 uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))((((sizeof(struct in6_pktinfo)) + sizeof (size_t) - 1) & (
size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr))
+ sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)
))
];
978 } control;
979 struct msghdr mh = {};
980 struct iovec iov;
981
982 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 982, __PRETTY_FUNCTION__); } while (0)
;
983 assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/resolve/resolved-manager.c"
, 983, __PRETTY_FUNCTION__); } while (0)
;
984 assert(destination)do { if ((__builtin_expect(!!(!(destination)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("destination"), "../src/resolve/resolved-manager.c"
, 984, __PRETTY_FUNCTION__); } while (0)
;
985 assert(port > 0)do { if ((__builtin_expect(!!(!(port > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("port > 0"), "../src/resolve/resolved-manager.c"
, 985, __PRETTY_FUNCTION__); } while (0)
;
986 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/resolve/resolved-manager.c"
, 986, __PRETTY_FUNCTION__); } while (0)
;
987
988 iov.iov_base = DNS_PACKET_DATA(p);
989 iov.iov_len = p->size;
990
991 sa.in6.sin6_addr = *destination;
992 sa.in6.sin6_port = htobe16(port),
993 sa.in6.sin6_scope_id = ifindex;
994
995 mh.msg_iov = &iov;
996 mh.msg_iovlen = 1;
997 mh.msg_name = &sa.sa;
998 mh.msg_namelen = sizeof(sa.in6);
999
1000 if (ifindex > 0) {
1001 struct cmsghdr *cmsg;
1002 struct in6_pktinfo *pi;
1003
1004 zero(control)(({ size_t _l_ = (sizeof(control)); void *_x_ = (&(control
)); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); }))
;
1005
1006 mh.msg_control = &control;
1007 mh.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo))((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t
) ~(sizeof (size_t) - 1)) + (sizeof(struct in6_pktinfo)))
;
1008
1009 cmsg = CMSG_FIRSTHDR(&mh)((size_t) (&mh)->msg_controllen >= sizeof (struct cmsghdr
) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr
*) 0)
;
1010 cmsg->cmsg_len = mh.msg_controllen;
1011 cmsg->cmsg_level = IPPROTO_IPV6IPPROTO_IPV6;
1012 cmsg->cmsg_type = IPV6_PKTINFO50;
1013
1014 pi = (struct in6_pktinfo*) CMSG_DATA(cmsg)((cmsg)->__cmsg_data);
1015 pi->ipi6_ifindex = ifindex;
1016
1017 if (source)
1018 pi->ipi6_addr = *source;
1019 }
1020
1021 return sendmsg_loop(fd, &mh, 0);
1022}
1023
1024int manager_send(
1025 Manager *m,
1026 int fd,
1027 int ifindex,
1028 int family,
1029 const union in_addr_union *destination,
1030 uint16_t port,
1031 const union in_addr_union *source,
1032 DnsPacket *p) {
1033
1034 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1034, __PRETTY_FUNCTION__); } while (0)
;
1035 assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/resolve/resolved-manager.c"
, 1035, __PRETTY_FUNCTION__); } while (0)
;
1036 assert(destination)do { if ((__builtin_expect(!!(!(destination)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("destination"), "../src/resolve/resolved-manager.c"
, 1036, __PRETTY_FUNCTION__); } while (0)
;
1037 assert(port > 0)do { if ((__builtin_expect(!!(!(port > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("port > 0"), "../src/resolve/resolved-manager.c"
, 1037, __PRETTY_FUNCTION__); } while (0)
;
1038 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/resolve/resolved-manager.c"
, 1038, __PRETTY_FUNCTION__); } while (0)
;
1039
1040 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))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1040, __func__, "Sending %s packet with id %"
"u" " on interface %i/%s.", ((be16toh(((DnsPacketHeader*) DNS_PACKET_DATA
(p))->flags) >> 15) & 1) ? "response" : "query",
((DnsPacketHeader*) DNS_PACKET_DATA(p))->id, ifindex, af_to_name
(family)) : -abs(_e); })
;
1041
1042 if (family == AF_INET2)
1043 return manager_ipv4_send(m, fd, ifindex, &destination->in, port, &source->in, p);
1044 if (family == AF_INET610)
1045 return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, &source->in6, p);
1046
1047 return -EAFNOSUPPORT97;
1048}
1049
1050uint32_t manager_find_mtu(Manager *m) {
1051 uint32_t mtu = 0;
1052 Link *l;
1053 Iterator i;
1054
1055 /* If we don't know on which link a DNS packet would be
1056 * delivered, let's find the largest MTU that works on all
1057 * interfaces we know of */
1058
1059 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
{
1060 if (l->mtu <= 0)
1061 continue;
1062
1063 if (mtu <= 0 || l->mtu < mtu)
1064 mtu = l->mtu;
1065 }
1066
1067 return mtu;
1068}
1069
1070int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) {
1071 LinkAddress *a;
1072
1073 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1073, __PRETTY_FUNCTION__); } while (0)
;
1074
1075 a = manager_find_link_address(m, family, in_addr);
1076 if (a)
1077 return a->link->ifindex;
1078
1079 return 0;
1080}
1081
1082void manager_refresh_rrs(Manager *m) {
1083 Iterator i;
1084 Link *l;
1085 DnssdService *s;
1086
1087 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1087, __PRETTY_FUNCTION__); } while (0)
;
1088
1089 m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key);
1090 m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key);
1091 m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key);
1092 m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key);
1093
1094 if (m->mdns_support == RESOLVE_SUPPORT_YES)
1095 HASHMAP_FOREACH(s, m->dnssd_services, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->dnssd_services
), &(i), (void**)&(s), ((void*)0)); )
1096 if (dnssd_update_rrs(s) < 0)
1097 log_warning("Failed to refresh DNS-SD service '%s'", s->name)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1097, __func__, "Failed to refresh DNS-SD service '%s'"
, s->name) : -abs(_e); })
;
1098
1099 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
{
1100 link_add_rrs(l, true1);
1101 link_add_rrs(l, false0);
1102 }
1103}
1104
1105static int manager_next_random_name(const char *old, char **ret_new) {
1106 const char *p;
1107 uint64_t u, a;
1108 char *n;
1109
1110 p = strchr(old, 0);
1111 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/resolve/resolved-manager.c"
, 1111, __PRETTY_FUNCTION__); } while (0)
;
1112
1113 while (p > old) {
1114 if (!strchr(DIGITS"0123456789", p[-1]))
1115 break;
1116
1117 p--;
1118 }
1119
1120 if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0)
1121 u = 1;
1122
1123 /* Add a random number to the old value. This way we can avoid
1124 * that two hosts pick the same hostname, win on IPv4 and lose
1125 * on IPv6 (or vice versa), and pick the same hostname
1126 * replacement hostname, ad infinitum. We still want the
1127 * numbers to go up monotonically, hence we just add a random
1128 * value 1..10 */
1129
1130 random_bytes(&a, sizeof(a));
1131 u += 1 + a % 10;
1132
1133 if (asprintf(&n, "%.*s%" PRIu64"l" "u", (int) (p - old), old, u) < 0)
1134 return -ENOMEM12;
1135
1136 *ret_new = n;
1137
1138 return 0;
1139}
1140
1141int manager_next_hostname(Manager *m) {
1142 _cleanup_free___attribute__((cleanup(freep))) char *h = NULL((void*)0), *k = NULL((void*)0);
1143 int r;
1144
1145 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1145, __PRETTY_FUNCTION__); } while (0)
;
1146
1147 r = manager_next_random_name(m->llmnr_hostname, &h);
1148 if (r < 0)
1149 return r;
1150
1151 r = dns_name_concat(h, "local", &k);
1152 if (r < 0)
1153 return r;
1154
1155 log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h)({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1155, __func__, "Hostname conflict, changing published hostname from '%s' to '%s'."
, m->llmnr_hostname, h) : -abs(_e); })
;
1156
1157 free_and_replace(m->llmnr_hostname, h)({ free(m->llmnr_hostname); (m->llmnr_hostname) = (h); (
h) = ((void*)0); 0; })
;
1158 free_and_replace(m->mdns_hostname, k)({ free(m->mdns_hostname); (m->mdns_hostname) = (k); (k
) = ((void*)0); 0; })
;
1159
1160 manager_refresh_rrs(m);
1161
1162 return 0;
1163}
1164
1165LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) {
1166 Iterator i;
1167 Link *l;
1168
1169 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1169, __PRETTY_FUNCTION__); } while (0)
;
1170
1171 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
{
1172 LinkAddress *a;
1173
1174 a = link_find_address(l, family, in_addr);
1175 if (a)
1176 return a;
1177 }
1178
1179 return NULL((void*)0);
1180}
1181
1182bool_Bool manager_our_packet(Manager *m, DnsPacket *p) {
1183 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1183, __PRETTY_FUNCTION__); } while (0)
;
1184 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/resolve/resolved-manager.c"
, 1184, __PRETTY_FUNCTION__); } while (0)
;
1185
1186 return !!manager_find_link_address(m, p->family, &p->sender);
1187}
1188
1189DnsScope* manager_find_scope(Manager *m, DnsPacket *p) {
1190 Link *l;
1191
1192 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1192, __PRETTY_FUNCTION__); } while (0)
;
1193 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/resolve/resolved-manager.c"
, 1193, __PRETTY_FUNCTION__); } while (0)
;
1194
1195 l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)((void *) ((intptr_t) (p->ifindex))));
1196 if (!l)
1197 return NULL((void*)0);
1198
1199 switch (p->protocol) {
1200 case DNS_PROTOCOL_LLMNR:
1201 if (p->family == AF_INET2)
1202 return l->llmnr_ipv4_scope;
1203 else if (p->family == AF_INET610)
1204 return l->llmnr_ipv6_scope;
1205
1206 break;
1207
1208 case DNS_PROTOCOL_MDNS:
1209 if (p->family == AF_INET2)
1210 return l->mdns_ipv4_scope;
1211 else if (p->family == AF_INET610)
1212 return l->mdns_ipv6_scope;
1213
1214 break;
1215
1216 default:
1217 break;
1218 }
1219
1220 return NULL((void*)0);
1221}
1222
1223void manager_verify_all(Manager *m) {
1224 DnsScope *s;
1225
1226 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1226, __PRETTY_FUNCTION__); } while (0)
;
1227
1228 LIST_FOREACH(scopes, s, m->dns_scopes)for ((s) = (m->dns_scopes); (s); (s) = (s)->scopes_next
)
1229 dns_zone_verify_all(&s->zone);
1230}
1231
1232int manager_is_own_hostname(Manager *m, const char *name) {
1233 int r;
1234
1235 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1235, __PRETTY_FUNCTION__); } while (0)
;
1236 assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("name"), "../src/resolve/resolved-manager.c"
, 1236, __PRETTY_FUNCTION__); } while (0)
;
1237
1238 if (m->llmnr_hostname) {
1239 r = dns_name_equal(name, m->llmnr_hostname);
1240 if (r != 0)
1241 return r;
1242 }
1243
1244 if (m->mdns_hostname) {
1245 r = dns_name_equal(name, m->mdns_hostname);
1246 if (r != 0)
1247 return r;
1248 }
1249
1250 if (m->full_hostname)
1251 return dns_name_equal(name, m->full_hostname);
1252
1253 return 0;
1254}
1255
1256int manager_compile_dns_servers(Manager *m, OrderedSet **dns) {
1257 DnsServer *s;
1258 Iterator i;
1259 Link *l;
1260 int r;
1261
1262 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1262, __PRETTY_FUNCTION__); } while (0)
;
1263 assert(dns)do { if ((__builtin_expect(!!(!(dns)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("dns"), "../src/resolve/resolved-manager.c"
, 1263, __PRETTY_FUNCTION__); } while (0)
;
1264
1265 r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops);
1266 if (r < 0)
1267 return r;
1268
1269 /* First add the system-wide servers and domains */
1270 LIST_FOREACH(servers, s, m->dns_servers)for ((s) = (m->dns_servers); (s); (s) = (s)->servers_next
)
{
1271 r = ordered_set_put(*dns, s);
1272 if (r == -EEXIST17)
1273 continue;
1274 if (r < 0)
1275 return r;
1276 }
1277
1278 /* Then, add the per-link servers */
1279 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
{
1280 LIST_FOREACH(servers, s, l->dns_servers)for ((s) = (l->dns_servers); (s); (s) = (s)->servers_next
)
{
1281 r = ordered_set_put(*dns, s);
1282 if (r == -EEXIST17)
1283 continue;
1284 if (r < 0)
1285 return r;
1286 }
1287 }
1288
1289 /* If we found nothing, add the fallback servers */
1290 if (ordered_set_isempty(*dns)) {
1291 LIST_FOREACH(servers, s, m->fallback_dns_servers)for ((s) = (m->fallback_dns_servers); (s); (s) = (s)->servers_next
)
{
1292 r = ordered_set_put(*dns, s);
1293 if (r == -EEXIST17)
1294 continue;
1295 if (r < 0)
1296 return r;
1297 }
1298 }
1299
1300 return 0;
1301}
1302
1303/* filter_route is a tri-state:
1304 * < 0: no filtering
1305 * = 0 or false: return only domains which should be used for searching
1306 * > 0 or true: return only domains which are for routing only
1307 */
1308int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) {
1309 DnsSearchDomain *d;
1310 Iterator i;
1311 Link *l;
1312 int r;
1313
1314 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1314, __PRETTY_FUNCTION__); } while (0)
;
1315 assert(domains)do { if ((__builtin_expect(!!(!(domains)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("domains"), "../src/resolve/resolved-manager.c"
, 1315, __PRETTY_FUNCTION__); } while (0)
;
1316
1317 r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops);
1318 if (r < 0)
1319 return r;
1320
1321 LIST_FOREACH(domains, d, m->search_domains)for ((d) = (m->search_domains); (d); (d) = (d)->domains_next
)
{
1322
1323 if (filter_route >= 0 &&
1324 d->route_only != !!filter_route)
1325 continue;
1326
1327 r = ordered_set_put(*domains, d->name);
1328 if (r == -EEXIST17)
1329 continue;
1330 if (r < 0)
1331 return r;
1332 }
1333
1334 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
{
1335
1336 LIST_FOREACH(domains, d, l->search_domains)for ((d) = (l->search_domains); (d); (d) = (d)->domains_next
)
{
1337
1338 if (filter_route >= 0 &&
1339 d->route_only != !!filter_route)
1340 continue;
1341
1342 r = ordered_set_put(*domains, d->name);
1343 if (r == -EEXIST17)
1344 continue;
1345 if (r < 0)
1346 return r;
1347 }
1348 }
1349
1350 return 0;
1351}
1352
1353DnssecMode manager_get_dnssec_mode(Manager *m) {
1354 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1354, __PRETTY_FUNCTION__); } while (0)
;
1355
1356 if (m->dnssec_mode != _DNSSEC_MODE_INVALID)
1357 return m->dnssec_mode;
1358
1359 return DNSSEC_NO;
1360}
1361
1362bool_Bool manager_dnssec_supported(Manager *m) {
1363 DnsServer *server;
1364 Iterator i;
1365 Link *l;
1366
1367 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1367, __PRETTY_FUNCTION__); } while (0)
;
1368
1369 if (manager_get_dnssec_mode(m) == DNSSEC_NO)
1370 return false0;
1371
1372 server = manager_get_dns_server(m);
1373 if (server && !dns_server_dnssec_supported(server))
1374 return false0;
1375
1376 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
1377 if (!link_dnssec_supported(l))
1378 return false0;
1379
1380 return true1;
1381}
1382
1383DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) {
1384 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1384, __PRETTY_FUNCTION__); } while (0)
;
1385
1386 if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID)
1387 return m->dns_over_tls_mode;
1388
1389 return DNS_OVER_TLS_NO;
1390}
1391
1392void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) {
1393
1394 assert(verdict >= 0)do { if ((__builtin_expect(!!(!(verdict >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("verdict >= 0"), "../src/resolve/resolved-manager.c"
, 1394, __PRETTY_FUNCTION__); } while (0)
;
1395 assert(verdict < _DNSSEC_VERDICT_MAX)do { if ((__builtin_expect(!!(!(verdict < _DNSSEC_VERDICT_MAX
)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("verdict < _DNSSEC_VERDICT_MAX"
), "../src/resolve/resolved-manager.c", 1395, __PRETTY_FUNCTION__
); } while (0)
;
1396
1397 if (DEBUG_LOGGING(__builtin_expect(!!(log_get_max_level_realm(LOG_REALM_SYSTEMD
) >= 7),0))
) {
1398 char s[DNS_RESOURCE_KEY_STRING_MAX((sizeof "CLASS" + (2+(sizeof(uint16_t) <= 1 ? 3 : sizeof(
uint16_t) <= 2 ? 5 : sizeof(uint16_t) <= 4 ? 10 : sizeof
(uint16_t) <= 8 ? 20 : sizeof(int[-2*(sizeof(uint16_t) >
8)])))) + (sizeof "CLASS" + (2+(sizeof(uint16_t) <= 1 ? 3
: sizeof(uint16_t) <= 2 ? 5 : sizeof(uint16_t) <= 4 ? 10
: sizeof(uint16_t) <= 8 ? 20 : sizeof(int[-2*(sizeof(uint16_t
) > 8)])))) + 253 + 1)
];
1399
1400 log_debug("Found verdict for lookup %s: %s",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1402, __func__, "Found verdict for lookup %s: %s"
, dns_resource_key_to_string(key, s, sizeof s), dnssec_verdict_to_string
(verdict)) : -abs(_e); })
1401 dns_resource_key_to_string(key, s, sizeof s),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1402, __func__, "Found verdict for lookup %s: %s"
, dns_resource_key_to_string(key, s, sizeof s), dnssec_verdict_to_string
(verdict)) : -abs(_e); })
1402 dnssec_verdict_to_string(verdict))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1402, __func__, "Found verdict for lookup %s: %s"
, dns_resource_key_to_string(key, s, sizeof s), dnssec_verdict_to_string
(verdict)) : -abs(_e); })
;
1403 }
1404
1405 m->n_dnssec_verdict[verdict]++;
1406}
1407
1408bool_Bool manager_routable(Manager *m, int family) {
1409 Iterator i;
1410 Link *l;
1411
1412 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1412, __PRETTY_FUNCTION__); } while (0)
;
1413
1414 /* Returns true if the host has at least one interface with a routable address of the specified type */
1415
1416 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
1417 if (link_relevant(l, family, false0))
1418 return true1;
1419
1420 return false0;
1421}
1422
1423void manager_flush_caches(Manager *m) {
1424 DnsScope *scope;
1425
1426 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1426, __PRETTY_FUNCTION__); } while (0)
;
1427
1428 LIST_FOREACH(scopes, scope, m->dns_scopes)for ((scope) = (m->dns_scopes); (scope); (scope) = (scope)
->scopes_next)
1429 dns_cache_flush(&scope->cache);
1430
1431 log_info("Flushed all caches.")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1431, __func__, "Flushed all caches."
) : -abs(_e); })
;
1432}
1433
1434void manager_reset_server_features(Manager *m) {
1435 Iterator i;
1436 Link *l;
1437
1438 dns_server_reset_features_all(m->dns_servers);
1439 dns_server_reset_features_all(m->fallback_dns_servers);
1440
1441 HASHMAP_FOREACH(l, m->links, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->links), &
(i), (void**)&(l), ((void*)0)); )
1442 dns_server_reset_features_all(l->dns_servers);
1443
1444 log_info("Resetting learnt feature levels on all servers.")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1444, __func__, "Resetting learnt feature levels on all servers."
) : -abs(_e); })
;
1445}
1446
1447void manager_cleanup_saved_user(Manager *m) {
1448 _cleanup_closedir___attribute__((cleanup(closedirp))) DIR *d = NULL((void*)0);
1449 struct dirent *de;
1450 int r;
1451
1452 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1452, __PRETTY_FUNCTION__); } while (0)
;
1453
1454 /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface
1455 * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can
1456 * be restarted without losing this data. */
1457
1458 d = opendir("/run/systemd/resolve/netif/");
1459 if (!d) {
1460 if (errno(*__errno_location ()) == ENOENT2)
1461 return;
1462
1463 log_warning_errno(errno, "Failed to open interface directory: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm
= (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >=
((_level) & 0x07)) ? log_internal_realm(((_realm) <<
10 | (_level)), _e, "../src/resolve/resolved-manager.c", 1463
, __func__, "Failed to open interface directory: %m") : -abs(
_e); })
;
1464 return;
1465 }
1466
1467 FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m"))for ((*__errno_location ()) = 0, de = readdir(d);; (*__errno_location
()) = 0, de = readdir(d)) if (!de) { if ((*__errno_location (
)) > 0) { ({ int _level = ((3)), _e = (((*__errno_location
()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm
(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((
_realm) << 10 | (_level)), _e, "../src/resolve/resolved-manager.c"
, 1467, __func__, "Failed to read interface directory: %m") :
-abs(_e); }); } break; } else
{
1468 _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0);
1469 int ifindex;
1470 Link *l;
1471
1472 if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended
[20 - sizeof((int[]){DT_UNKNOWN, DT_REG})/sizeof(int)]; switch
(de->d_type) { case DT_UNKNOWN: case DT_REG: _found = 1; break
; default: break; } _found; })
)
1473 continue;
1474
1475 if (dot_or_dot_dot(de->d_name))
1476 continue;
1477
1478 r = parse_ifindex(de->d_name, &ifindex);
1479 if (r < 0) /* Probably some temporary file from a previous run. Delete it */
1480 goto rm;
1481
1482 l = hashmap_get(m->links, INT_TO_PTR(ifindex)((void *) ((intptr_t) (ifindex))));
1483 if (!l) /* link vanished */
1484 goto rm;
1485
1486 if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */
1487 goto rm;
1488
1489 continue;
1490
1491 rm:
1492 p = strappend("/run/systemd/resolve/netif/", de->d_name);
1493 if (!p) {
1494 log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/resolve/resolved-manager.c"
, 1494, __func__)
;
1495 return;
1496 }
1497
1498 (void) unlink(p);
1499 }
1500}
1501
1502bool_Bool manager_next_dnssd_names(Manager *m) {
1503 Iterator i;
1504 DnssdService *s;
1505 bool_Bool tried = false0;
1506 int r;
1507
1508 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/resolve/resolved-manager.c"
, 1508, __PRETTY_FUNCTION__); } while (0)
;
1509
1510 HASHMAP_FOREACH(s, m->dnssd_services, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), .
next_key = ((void*)0) }); hashmap_iterate((m->dnssd_services
), &(i), (void**)&(s), ((void*)0)); )
{
1511 _cleanup_free___attribute__((cleanup(freep))) char * new_name = NULL((void*)0);
1512
1513 if (!s->withdrawn)
1514 continue;
1515
1516 r = manager_next_random_name(s->name_template, &new_name);
1517 if (r < 0) {
1518 log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name)({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/resolve/resolved-manager.c", 1518, __func__, "Failed to get new name for service '%s': %m"
, s->name) : -abs(_e); })
;
1519 continue;
1520 }
1521
1522 free_and_replace(s->name_template, new_name)({ free(s->name_template); (s->name_template) = (new_name
); (new_name) = ((void*)0); 0; })
;
1523
1524 s->withdrawn = false0;
1525
1526 tried = true1;
1527 }
1528
1529 if (tried)
1530 manager_refresh_rrs(m);
1531
1532 return tried;
1533}