1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
7 #include "alloc-util.h"
10 #include "netlink-util.h"
11 #include "network-internal.h"
13 #include "time-util.h"
16 static bool manager_ignore_link(Manager
*m
, Link
*link
) {
20 /* always ignore the loopback interface */
21 if (link
->flags
& IFF_LOOPBACK
)
24 /* if interfaces are given on the command line, ignore all others */
25 if (m
->interfaces
&& !hashmap_contains(m
->interfaces
, link
->ifname
))
28 if (!link
->required_for_online
)
31 /* ignore interfaces we explicitly are asked to ignore */
32 return strv_fnmatch(m
->ignore
, link
->ifname
, 0);
35 static int manager_link_is_online(Manager
*m
, Link
*l
, LinkOperationalState s
) {
36 /* This returns the following:
37 * -EAGAIN: not processed by udev or networkd
38 * 0: operstate is not enough
42 return log_link_debug_errno(l
, SYNTHETIC_ERRNO(EAGAIN
),
43 "link has not yet been processed by udev");
45 if (STR_IN_SET(l
->state
, "configuring", "pending"))
46 return log_link_debug_errno(l
, SYNTHETIC_ERRNO(EAGAIN
),
47 "link is being processed by networkd");
50 s
= m
->required_operstate
>= 0 ? m
->required_operstate
: l
->required_operstate
;
52 if (l
->operational_state
< s
) {
53 log_link_debug(l
, "Operational state '%s' is below '%s'",
54 link_operstate_to_string(l
->operational_state
),
55 link_operstate_to_string(s
));
62 bool manager_configured(Manager
*m
) {
63 bool one_ready
= false;
70 if (!hashmap_isempty(m
->interfaces
)) {
71 /* wait for all the links given on the command line to appear */
72 HASHMAP_FOREACH_KEY(p
, ifname
, m
->interfaces
, i
) {
73 LinkOperationalState s
= PTR_TO_INT(p
);
75 l
= hashmap_get(m
->links_by_name
, ifname
);
77 log_debug("still waiting for %s", ifname
);
83 if (manager_link_is_online(m
, l
, s
) <= 0) {
92 /* all interfaces given by the command line are online, or
93 * one of the specified interfaces is online. */
97 /* wait for all links networkd manages to be in admin state 'configured'
98 * and at least one link to gain a carrier */
99 HASHMAP_FOREACH(l
, m
->links
, i
) {
100 if (manager_ignore_link(m
, l
)) {
101 log_link_debug(l
, "link is ignored");
105 r
= manager_link_is_online(m
, l
, _LINK_OPERSTATE_INVALID
);
106 if (r
< 0 && !m
->any
)
109 /* we wait for at least one link to be ready,
110 * regardless of who manages it */
117 static int manager_process_link(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
118 Manager
*m
= userdata
;
128 r
= sd_netlink_message_get_type(mm
, &type
);
130 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
134 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
136 log_warning_errno(r
, "rtnl: Could not get ifindex from link, ignoring: %m");
138 } else if (ifindex
<= 0) {
139 log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex
);
143 r
= sd_netlink_message_read_string(mm
, IFLA_IFNAME
, &ifname
);
145 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
149 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
155 log_debug("Found link %i", ifindex
);
157 r
= link_new(m
, &l
, ifindex
, ifname
);
159 return log_error_errno(r
, "Failed to create link object: %m");
162 r
= link_update_rtnl(l
, mm
);
164 log_link_warning_errno(l
, r
, "Failed to process RTNL link message, ignoring: %m");
166 r
= link_update_monitor(l
);
167 if (r
< 0 && r
!= -ENODATA
)
168 log_link_warning_errno(l
, r
, "Failed to update link state, ignoring: %m");
174 log_link_debug(l
, "Removing link");
184 static int on_rtnl_event(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
185 Manager
*m
= userdata
;
188 r
= manager_process_link(rtnl
, mm
, m
);
192 if (manager_configured(m
))
193 sd_event_exit(m
->event
, 0);
198 static int manager_rtnl_listen(Manager
*m
) {
199 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
200 sd_netlink_message
*i
;
205 /* First, subscribe to interfaces coming and going */
206 r
= sd_netlink_open(&m
->rtnl
);
210 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
214 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-NEWLINK");
218 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-DELLINK");
222 /* Then, enumerate all links */
223 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
227 r
= sd_netlink_message_request_dump(req
, true);
231 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
235 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
236 r
= manager_process_link(m
->rtnl
, i
, m
);
244 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
245 Manager
*m
= userdata
;
252 sd_network_monitor_flush(m
->network_monitor
);
254 HASHMAP_FOREACH(l
, m
->links
, i
) {
255 r
= link_update_monitor(l
);
256 if (r
< 0 && r
!= -ENODATA
)
257 log_link_warning_errno(l
, r
, "Failed to update link state, ignoring: %m");
260 if (manager_configured(m
))
261 sd_event_exit(m
->event
, 0);
266 static int manager_network_monitor_listen(Manager
*m
) {
271 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
275 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
279 events
= sd_network_monitor_get_events(m
->network_monitor
);
283 r
= sd_event_add_io(m
->event
, &m
->network_monitor_event_source
,
284 fd
, events
, &on_network_event
, m
);
291 int manager_new(Manager
**ret
, Hashmap
*interfaces
, char **ignore
,
292 LinkOperationalState required_operstate
,
293 bool any
, usec_t timeout
) {
294 _cleanup_(manager_freep
) Manager
*m
= NULL
;
304 .interfaces
= interfaces
,
306 .required_operstate
= required_operstate
,
310 r
= sd_event_default(&m
->event
);
314 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
315 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
320 usec
= now(clock_boottime_or_monotonic()) + timeout
;
322 r
= sd_event_add_time(m
->event
, NULL
, clock_boottime_or_monotonic(), usec
, 0, NULL
, INT_TO_PTR(-ETIMEDOUT
));
327 sd_event_set_watchdog(m
->event
, true);
329 r
= manager_network_monitor_listen(m
);
333 r
= manager_rtnl_listen(m
);
342 void manager_free(Manager
*m
) {
346 hashmap_free_with_destructor(m
->links
, link_free
);
347 hashmap_free(m
->links_by_name
);
349 sd_event_source_unref(m
->network_monitor_event_source
);
350 sd_network_monitor_unref(m
->network_monitor
);
352 sd_event_source_unref(m
->rtnl_event_source
);
353 sd_netlink_unref(m
->rtnl
);
355 sd_event_unref(m
->event
);