Debian Codegraph @

src/network/wait-online/wait-online-manager.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if.h>
4
5 #include "sd-event.h"
6 #include "sd-json.h"
7 #include "sd-netlink.h"
8 #include "sd-network.h"
9 #include "sd-varlink.h"
10
11 #include "alloc-util.h"
12 #include "dns-configuration.h"
13 #include "hashmap.h"
14 #include "json-util.h"
15 #include "log-link.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "wait-online-link.h"
19 #include "wait-online-manager.h"
20
21 static bool link_in_command_line_interfaces(Link *link, Manager *m) {
22 assert(link);
23 assert(m);
24
25 if (hashmap_contains(m->command_line_interfaces_by_name, link->ifname))
26 return true;
27
28 STRV_FOREACH(n, link->altnames)
29 if (hashmap_contains(m->command_line_interfaces_by_name, *n))
30 return true;
31
32 return false;
33 }
34
35 static bool manager_ignore_link(Manager *m, Link *link) {
36 assert(m);
37 assert(link);
38
39 /* always ignore the loopback interface */
40 if (link->flags & IFF_LOOPBACK)
41 return true;
42
43 /* if interfaces are given on the command line, ignore all others */
44 if (m->command_line_interfaces_by_name &&
45 !link_in_command_line_interfaces(link, m))
46 return true;
47
48 if (!link->required_for_online)
49 return true;
50
51 /* ignore interfaces we explicitly are asked to ignore */
52 if (strv_fnmatch(m->ignored_interfaces, link->ifname))
53 return true;
54
55 STRV_FOREACH(n, link->altnames)
56 if (strv_fnmatch(m->ignored_interfaces, *n))
57 return true;
58
59 return false;
60 }
61
62 static const LinkOperationalStateRange* get_state_range(Manager *m, Link *l, const LinkOperationalStateRange *from_cmdline) {
63 assert(m);
64 assert(l);
65
66 const LinkOperationalStateRange *range;
67 FOREACH_ARGUMENT(range, from_cmdline, &m->required_operstate, &l->required_operstate)
68 if (operational_state_range_is_valid(range))
69 return range;
70
71 /* l->required_operstate should be always valid. */
72 assert_not_reached();
73 }
74
75 static int manager_link_is_online(Manager *m, Link *l, const LinkOperationalStateRange *range) {
76 AddressFamily required_family;
77 bool needs_ipv4;
78 bool needs_ipv6;
79
80 assert(m);
81 assert(l);
82 assert(range);
83
84 /* This returns the following:
85 * -EAGAIN : not processed by udev
86 * -EBUSY : being processed by networkd
87 * -EADDRNOTAVAIL: requested conditions (operstate and/or addresses) are not satisfied
88 * false : unmanaged
89 * true : online */
90
91 if (!l->state || streq(l->state, "pending"))
92 /* If no state string exists, networkd (and possibly also udevd) has not detected the
93 * interface yet, that mean we cannot determine whether the interface is managed or
94 * not. Hence, return negative value.
95 * If the link is in pending state, then udevd has not processed the link, and networkd
96 * has not tried to find .network file for the link. Hence, return negative value. */
97 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EAGAIN),
98 "link has not yet been processed by udev: setup state is %s.",
99 strna(l->state));
100
101 if (streq(l->state, "unmanaged")) {
102 /* If the link is in unmanaged state, then ignore the interface unless the interface is
103 * specified in '--interface/-i' option. */
104 if (!link_in_command_line_interfaces(l, m)) {
105 log_link_debug(l, "link is not managed by networkd.");
106 return false;
107 }
108
109 } else if (!streq(l->state, "configured"))
110 /* If the link is in non-configured state, return negative value here. */
111 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EBUSY),
112 "link is being processed by networkd: setup state is %s.",
113 l->state);
114
115 if (!operational_state_is_in_range(l->operational_state, range))
116 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EADDRNOTAVAIL),
117 "Operational state '%s' is not in range ['%s':'%s']",
118 link_operstate_to_string(l->operational_state),
119 link_operstate_to_string(range->min), link_operstate_to_string(range->max));
120
121 required_family = m->required_family > 0 ? m->required_family : l->required_family;
122 needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
123 needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
124
125 if (range->min < LINK_OPERSTATE_ROUTABLE) {
126 if (needs_ipv4 && l->ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
127 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EADDRNOTAVAIL),
128 "No routable or link-local IPv4 address is configured.");
129
130 if (needs_ipv6 && l->ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
131 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EADDRNOTAVAIL),
132 "No routable or link-local IPv6 address is configured.");
133 } else {
134 if (needs_ipv4 && l->ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
135 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EADDRNOTAVAIL),
136 "No routable IPv4 address is configured.");
137
138 if (needs_ipv6 && l->ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
139 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EADDRNOTAVAIL),
140 "No routable IPv6 address is configured.");
141 }
142
143 if (m->requires_dns) {
144 if (!l->dns_configuration)
145 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EADDRNOTAVAIL),
146 "No DNS configuration yet");
147
148 /* If a link is configured with DNSDefaultRoute=yes, or is configured with the
149 * search domain '.', then require link-specific DNS servers to be available.
150 * Otherwise, we check the global DNS configuration. */
151 if (l->dns_configuration->default_route ||
152 dns_configuration_contains_search_domain(l->dns_configuration, ".")) {
153
154 if (!dns_is_accessible(l->dns_configuration))
155 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EADDRNOTAVAIL),
156 "No link-specific DNS server is accessible.");
157
158 } else if (!dns_is_accessible(m->dns_configuration))
159 return log_link_debug_errno(l, SYNTHETIC_ERRNO(EADDRNOTAVAIL),
160 "No DNS server is accessible.");
161 }
162
163 log_link_debug(l, "link is configured by networkd and online.");
164 return true;
165 }
166
167 bool manager_configured(Manager *m) {
168 Link *l;
169 int r;
170
171 if (!hashmap_isempty(m->command_line_interfaces_by_name)) {
172 const LinkOperationalStateRange *range;
173 const char *ifname;
174
175 /* wait for all the links given on the command line to appear */
176 HASHMAP_FOREACH_KEY(range, ifname, m->command_line_interfaces_by_name) {
177
178 l = hashmap_get(m->links_by_name, ifname);
179 if (!l) {
180 if (range->min == LINK_OPERSTATE_MISSING) {
181 if (m->any)
182 return true;
183 } else {
184 log_debug("still waiting for %s", ifname);
185 if (!m->any)
186 return false;
187 }
188 continue;
189 }
190
191 range = get_state_range(m, l, range);
192
193 r = manager_link_is_online(m, l, range);
194 if (r <= 0 && !m->any)
195 return false;
196 if (r > 0 && m->any)
197 return true;
198 }
199
200 /* With '--any' : no interface is ready → return false
201 * Without '--any': all interfaces are ready → return true */
202 return !m->any;
203 }
204
205 /* wait for all links networkd manages */
206 bool has_online = false;
207 HASHMAP_FOREACH(l, m->links_by_index) {
208 const LinkOperationalStateRange *range;
209
210 if (manager_ignore_link(m, l)) {
211 log_link_debug(l, "link is ignored");
212 continue;
213 }
214
215 range = get_state_range(m, l, /* from_cmdline= */ NULL);
216
217 r = manager_link_is_online(m, l, range);
218 /* Unlike the above loop, unmanaged interfaces are ignored here. Also, Configured but offline
219 * interfaces are ignored. See issue #29506. */
220 if (r < 0 && r != -EADDRNOTAVAIL && !m->any)
221 return false;
222 if (r > 0) {
223 if (m->any)
224 return true;
225 has_online = true;
226 }
227 }
228
229 /* With '--any' : no interface is ready → return false
230 * Without '--any': all interfaces are ready or unmanaged
231 *
232 * In this stage, drivers for interfaces may not be loaded yet, and there may be only lo.
233 * To avoid that wait-online exits earlier than that drivers are loaded, let's request at least one
234 * managed online interface exists. See issue #27822. */
235 return !m->any && has_online;
236 }
237
238 static int on_rtnl_event(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
239 Manager *m = userdata;
240 int r;
241
242 r = rtnl_process_link(rtnl, mm, m);
243 if (r < 0)
244 return r;
245
246 if (manager_configured(m))
247 sd_event_exit(m->event, 0);
248
249 return 1;
250 }
251
252 static int manager_rtnl_listen(Manager *m) {
253 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
254 int r;
255
256 assert(m);
257
258 /* First, subscribe to interfaces coming and going */
259 r = sd_netlink_open(&m->rtnl);
260 if (r < 0)
261 return r;
262
263 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
264 if (r < 0)
265 return r;
266
267 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, on_rtnl_event, NULL, m, "wait-online-on-NEWLINK");
268 if (r < 0)
269 return r;
270
271 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, on_rtnl_event, NULL, m, "wait-online-on-DELLINK");
272 if (r < 0)
273 return r;
274
275 /* Then, enumerate all links */
276 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
277 if (r < 0)
278 return r;
279
280 r = sd_netlink_message_set_request_dump(req, true);
281 if (r < 0)
282 return r;
283
284 r = sd_netlink_call(m->rtnl, req, 0, &reply);
285 if (r < 0)
286 return r;
287
288 for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) {
289 r = rtnl_process_link(m->rtnl, i, m);
290 if (r < 0)
291 return r;
292 }
293
294 return r;
295 }
296
297 static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
298 Manager *m = ASSERT_PTR(userdata);
299 Link *l;
300 int r;
301
302 sd_network_monitor_flush(m->network_monitor);
303
304 HASHMAP_FOREACH(l, m->links_by_index) {
305 r = link_update_monitor(l);
306 if (r < 0)
307 log_link_full_errno(l, IN_SET(r, -ENODATA, -ENOENT) ? LOG_DEBUG : LOG_WARNING, r,
308 "Failed to update link state, ignoring: %m");
309 }
310
311 if (manager_configured(m))
312 sd_event_exit(m->event, 0);
313
314 return 0;
315 }
316
317 static int manager_network_monitor_listen(Manager *m) {
318 int r, fd, events;
319
320 assert(m);
321
322 r = sd_network_monitor_new(&m->network_monitor, NULL);
323 if (r < 0)
324 return r;
325
326 fd = sd_network_monitor_get_fd(m->network_monitor);
327 if (fd < 0)
328 return fd;
329
330 events = sd_network_monitor_get_events(m->network_monitor);
331 if (events < 0)
332 return events;
333
334 r = sd_event_add_io(m->event, &m->network_monitor_event_source,
335 fd, events, &on_network_event, m);
336 if (r < 0)
337 return r;
338
339 return 0;
340 }
341
342 static int manager_dns_configuration_listen(Manager *m);
343
344 static int on_dns_configuration_event(
345 sd_varlink *link,
346 sd_json_variant *parameters,
347 const char *error_id,
348 sd_varlink_reply_flags_t flags,
349 void *userdata) {
350
351 Manager *m = ASSERT_PTR(userdata);
352 sd_json_variant *configurations = NULL, *v = NULL;
353 int r;
354
355 assert(link);
356
357 if (error_id) {
358 if (streq(error_id, SD_VARLINK_ERROR_DISCONNECTED)) {
359 log_debug("DNS configuration monitor disconnected, reconnecting...");
360
361 return manager_dns_configuration_listen(m);
362 }
363
364 log_warning("DNS configuration event error, ignoring: %s", error_id);
365 return 0;
366 }
367
368 configurations = sd_json_variant_by_key(parameters, "configuration");
369 if (!sd_json_variant_is_array(configurations)) {
370 log_warning("DNS configuration JSON data does not have configuration key, ignoring.");
371 return 0;
372 }
373
374 /* Clear any existing link DNS configuration saved by the manager. */
375 hashmap_clear(m->dns_configuration_by_link_index);
376
377 JSON_VARIANT_ARRAY_FOREACH(v, configurations) {
378 _cleanup_(dns_configuration_freep) DNSConfiguration *c = NULL;
379
380 r = dns_configuration_from_json(v, &c);
381 if (r < 0) {
382 log_warning_errno(r, "Failed to get DNS configuration JSON, ignoring: %m");
383 continue;
384 }
385
386 if (c->ifindex > 0) {
387 Link *l = hashmap_get(m->links_by_index, INT_TO_PTR(c->ifindex));
388 if (l)
389 free_and_replace_full(l->dns_configuration, c, dns_configuration_free);
390 else {
391 r = hashmap_ensure_put(
392 &m->dns_configuration_by_link_index,
393 &dns_configuration_hash_ops,
394 INT_TO_PTR(c->ifindex),
395 c);
396 if (r < 0) {
397 log_warning_errno(r, "Failed to save DNS configuration for link %i, ignoring: %m", c->ifindex);
398 continue;
399 }
400 TAKE_PTR(c);
401 }
402 } else
403 /* Global DNS configuration */
404 free_and_replace_full(m->dns_configuration, c, dns_configuration_free);
405 }
406
407 if (manager_configured(m))
408 sd_event_exit(m->event, 0);
409
410 return 0;
411 }
412
413 static int manager_dns_configuration_listen(Manager *m) {
414 _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
415 int r;
416
417 assert(m);
418 assert(m->event);
419
420 if (!m->requires_dns)
421 return 0;
422
423 m->varlink_client = sd_varlink_unref(m->varlink_client);
424
425 r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor");
426 if (r < 0)
427 return log_error_errno(r, "Failed to connect to io.systemd.Resolve.Monitor: %m");
428
429 r = sd_varlink_set_relative_timeout(vl, USEC_INFINITY);
430 if (r < 0)
431 return log_error_errno(r, "Failed to set varlink timeout: %m");
432
433 r = sd_varlink_attach_event(vl, m->event, SD_EVENT_PRIORITY_NORMAL);
434 if (r < 0)
435 return log_error_errno(r, "Failed to attach varlink connection to event loop: %m");
436
437 (void) sd_varlink_set_userdata(vl, m);
438
439 r = sd_varlink_bind_reply(vl, on_dns_configuration_event);
440 if (r < 0)
441 return log_error_errno(r, "Failed to bind varlink reply callback: %m");
442
443 r = sd_varlink_observebo(
444 vl,
445 "io.systemd.Resolve.Monitor.SubscribeDNSConfiguration",
446 SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", false));
447 if (r < 0)
448 return log_error_errno(r, "Failed to issue SubscribeDNSConfiguration: %m");
449
450 m->varlink_client = TAKE_PTR(vl);
451
452 return 0;
453 }
454
455 int manager_new(Manager **ret,
456 Hashmap *command_line_interfaces_by_name,
457 char **ignored_interfaces,
458 LinkOperationalStateRange required_operstate,
459 AddressFamily required_family,
460 bool any,
461 usec_t timeout,
462 bool requires_dns) {
463
464 _cleanup_(manager_freep) Manager *m = NULL;
465 int r;
466
467 assert(ret);
468
469 m = new(Manager, 1);
470 if (!m)
471 return -ENOMEM;
472
473 *m = (Manager) {
474 .command_line_interfaces_by_name = command_line_interfaces_by_name,
475 .ignored_interfaces = ignored_interfaces,
476 .required_operstate = required_operstate,
477 .required_family = required_family,
478 .any = any,
479 .requires_dns = requires_dns,
480 };
481
482 r = sd_event_default(&m->event);
483 if (r < 0)
484 return r;
485
486 (void) sd_event_set_signal_exit(m->event, true);
487
488 if (timeout > 0) {
489 r = sd_event_add_time_relative(m->event, NULL, CLOCK_BOOTTIME, timeout, 0, NULL, INT_TO_PTR(-ETIMEDOUT));
490 if (r < 0 && r != -EOVERFLOW)
491 return r;
492 }
493
494 sd_event_set_watchdog(m->event, true);
495
496 r = manager_network_monitor_listen(m);
497 if (r < 0)
498 return r;
499
500 r = manager_rtnl_listen(m);
501 if (r < 0)
502 return r;
503
504 r = manager_dns_configuration_listen(m);
505 if (r < 0)
506 return r;
507
508 *ret = TAKE_PTR(m);
509
510 return 0;
511 }
512
513 Manager* manager_free(Manager *m) {
514 if (!m)
515 return NULL;
516
517 /* free links_by_index at first, as it has ownership of links. */
518 hashmap_free(m->links_by_index);
519 hashmap_free(m->links_by_name);
520
521 sd_event_source_unref(m->network_monitor_event_source);
522 sd_network_monitor_unref(m->network_monitor);
523 sd_event_source_unref(m->rtnl_event_source);
524 sd_netlink_unref(m->rtnl);
525 sd_event_unref(m->event);
526 sd_varlink_unref(m->varlink_client);
527
528 dns_configuration_free(m->dns_configuration);
529 hashmap_free(m->dns_configuration_by_link_index);
530
531 return mfree(m);
532 }