1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/ether.h>
7 #include "alloc-util.h"
10 #include "netlink-util.h"
12 #include "time-util.h"
15 static bool manager_ignore_link(Manager
*m
, Link
*link
) {
19 /* always ignore the loopback interface */
20 if (link
->flags
& IFF_LOOPBACK
)
23 /* if interfaces are given on the command line, ignore all others */
24 if (m
->interfaces
&& !hashmap_contains(m
->interfaces
, link
->ifname
))
27 if (!link
->required_for_online
)
30 /* ignore interfaces we explicitly are asked to ignore */
31 return strv_fnmatch(m
->ignore
, link
->ifname
);
34 static int manager_link_is_online(Manager
*m
, Link
*l
, LinkOperationalStateRange s
) {
35 /* This returns the following:
36 * -EAGAIN: not processed by udev or networkd
37 * 0: operstate is not enough
41 return log_link_debug_errno(l
, SYNTHETIC_ERRNO(EAGAIN
),
42 "link has not yet been processed by udev");
44 if (STR_IN_SET(l
->state
, "configuring", "pending"))
45 return log_link_debug_errno(l
, SYNTHETIC_ERRNO(EAGAIN
),
46 "link is being processed by networkd");
49 s
.min
= m
->required_operstate
.min
>= 0 ? m
->required_operstate
.min
50 : l
->required_operstate
.min
;
53 s
.max
= m
->required_operstate
.max
>= 0 ? m
->required_operstate
.max
54 : l
->required_operstate
.max
;
56 if (l
->operational_state
< s
.min
|| l
->operational_state
> s
.max
) {
57 log_link_debug(l
, "Operational state '%s' is not in range ['%s':'%s']",
58 link_operstate_to_string(l
->operational_state
),
59 link_operstate_to_string(s
.min
), link_operstate_to_string(s
.max
));
66 bool manager_configured(Manager
*m
) {
67 bool one_ready
= false;
73 if (!hashmap_isempty(m
->interfaces
)) {
74 /* wait for all the links given on the command line to appear */
75 HASHMAP_FOREACH_KEY(p
, ifname
, m
->interfaces
) {
76 LinkOperationalStateRange
*range
= p
;
78 l
= hashmap_get(m
->links_by_name
, ifname
);
79 if (!l
&& range
->min
== LINK_OPERSTATE_MISSING
) {
85 log_debug("still waiting for %s", ifname
);
91 if (manager_link_is_online(m
, l
, *range
) <= 0) {
100 /* all interfaces given by the command line are online, or
101 * one of the specified interfaces is online. */
105 /* wait for all links networkd manages to be in admin state 'configured'
106 * and at least one link to gain a carrier */
107 HASHMAP_FOREACH(l
, m
->links
) {
108 if (manager_ignore_link(m
, l
)) {
109 log_link_debug(l
, "link is ignored");
113 r
= manager_link_is_online(m
, l
,
114 (LinkOperationalStateRange
) { _LINK_OPERSTATE_INVALID
,
115 _LINK_OPERSTATE_INVALID
});
116 if (r
< 0 && !m
->any
)
119 /* we wait for at least one link to be ready,
120 * regardless of who manages it */
127 static int manager_process_link(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
128 Manager
*m
= userdata
;
138 r
= sd_netlink_message_get_type(mm
, &type
);
140 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
144 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
146 log_warning_errno(r
, "rtnl: Could not get ifindex from link, ignoring: %m");
148 } else if (ifindex
<= 0) {
149 log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex
);
153 r
= sd_netlink_message_read_string(mm
, IFLA_IFNAME
, &ifname
);
155 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
159 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
165 log_debug("Found link %i", ifindex
);
167 r
= link_new(m
, &l
, ifindex
, ifname
);
169 return log_error_errno(r
, "Failed to create link object: %m");
172 r
= link_update_rtnl(l
, mm
);
174 log_link_warning_errno(l
, r
, "Failed to process RTNL link message, ignoring: %m");
176 r
= link_update_monitor(l
);
177 if (r
< 0 && r
!= -ENODATA
)
178 log_link_warning_errno(l
, r
, "Failed to update link state, ignoring: %m");
184 log_link_debug(l
, "Removing link");
194 static int on_rtnl_event(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
195 Manager
*m
= userdata
;
198 r
= manager_process_link(rtnl
, mm
, m
);
202 if (manager_configured(m
))
203 sd_event_exit(m
->event
, 0);
208 static int manager_rtnl_listen(Manager
*m
) {
209 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
214 /* First, subscribe to interfaces coming and going */
215 r
= sd_netlink_open(&m
->rtnl
);
219 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
223 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-NEWLINK");
227 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-DELLINK");
231 /* Then, enumerate all links */
232 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
236 r
= sd_netlink_message_request_dump(req
, true);
240 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
244 for (sd_netlink_message
*i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
245 r
= manager_process_link(m
->rtnl
, i
, m
);
253 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
254 Manager
*m
= userdata
;
260 sd_network_monitor_flush(m
->network_monitor
);
262 HASHMAP_FOREACH(l
, m
->links
) {
263 r
= link_update_monitor(l
);
264 if (r
< 0 && r
!= -ENODATA
)
265 log_link_warning_errno(l
, r
, "Failed to update link state, ignoring: %m");
268 if (manager_configured(m
))
269 sd_event_exit(m
->event
, 0);
274 static int manager_network_monitor_listen(Manager
*m
) {
279 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
283 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
287 events
= sd_network_monitor_get_events(m
->network_monitor
);
291 r
= sd_event_add_io(m
->event
, &m
->network_monitor_event_source
,
292 fd
, events
, &on_network_event
, m
);
299 int manager_new(Manager
**ret
, Hashmap
*interfaces
, char **ignore
,
300 LinkOperationalStateRange required_operstate
,
301 bool any
, usec_t timeout
) {
302 _cleanup_(manager_freep
) Manager
*m
= NULL
;
312 .interfaces
= interfaces
,
314 .required_operstate
= required_operstate
,
318 r
= sd_event_default(&m
->event
);
322 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
323 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
328 usec
= now(clock_boottime_or_monotonic()) + timeout
;
330 r
= sd_event_add_time(m
->event
, NULL
, clock_boottime_or_monotonic(), usec
, 0, NULL
, INT_TO_PTR(-ETIMEDOUT
));
335 sd_event_set_watchdog(m
->event
, true);
337 r
= manager_network_monitor_listen(m
);
341 r
= manager_rtnl_listen(m
);
350 Manager
* manager_free(Manager
*m
) {
354 hashmap_free_with_destructor(m
->links
, link_free
);
355 hashmap_free(m
->links_by_name
);
357 sd_event_source_unref(m
->network_monitor_event_source
);
358 sd_network_monitor_unref(m
->network_monitor
);
359 sd_event_source_unref(m
->rtnl_event_source
);
360 sd_netlink_unref(m
->rtnl
);
361 sd_event_unref(m
->event
);