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 log_debug("link %s has not yet been processed by udev",
47 if (STR_IN_SET(l
->state
, "configuring", "pending")) {
48 log_debug("link %s is being processed by networkd",
54 s
= m
->required_operstate
>= 0 ? m
->required_operstate
: l
->required_operstate
;
56 if (l
->operational_state
< s
) {
57 log_debug("Operational state of link %s does not reach to %s",
58 l
->ifname
, link_operstate_to_string(s
));
65 bool manager_all_configured(Manager
*m
) {
66 bool one_ready
= false;
73 /* wait for all the links given on the command line to appear */
74 HASHMAP_FOREACH_KEY(p
, ifname
, m
->interfaces
, i
) {
75 LinkOperationalState s
= PTR_TO_INT(p
);
77 l
= hashmap_get(m
->links_by_name
, ifname
);
79 log_debug("still waiting for %s", ifname
);
83 if (manager_link_is_online(m
, l
, s
) <= 0)
87 if (!hashmap_isempty(m
->interfaces
))
88 /* all interfaces given by the command line are online. */
91 /* wait for all links networkd manages to be in admin state 'configured'
92 * and at least one link to gain a carrier */
93 HASHMAP_FOREACH(l
, m
->links
, i
) {
94 if (manager_ignore_link(m
, l
)) {
95 log_info("ignoring: %s", l
->ifname
);
99 r
= manager_link_is_online(m
, l
, _LINK_OPERSTATE_INVALID
);
103 /* we wait for at least one link to be ready,
104 * regardless of who manages it */
111 static int manager_process_link(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
112 Manager
*m
= userdata
;
122 r
= sd_netlink_message_get_type(mm
, &type
);
124 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
128 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
130 log_warning_errno(r
, "rtnl: Could not get ifindex from link, ignoring: %m");
132 } else if (ifindex
<= 0) {
133 log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex
);
137 r
= sd_netlink_message_read_string(mm
, IFLA_IFNAME
, &ifname
);
139 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
143 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
149 log_debug("Found link %i", ifindex
);
151 r
= link_new(m
, &l
, ifindex
, ifname
);
153 return log_error_errno(r
, "Failed to create link object: %m");
156 r
= link_update_rtnl(l
, mm
);
158 log_warning_errno(r
, "Failed to process RTNL link message, ignoring: %m");
160 r
= link_update_monitor(l
);
162 log_warning_errno(r
, "Failed to initialize link object, ignoring: %m");
168 log_debug("Removing link %i", l
->ifindex
);
178 static int on_rtnl_event(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
179 Manager
*m
= userdata
;
182 r
= manager_process_link(rtnl
, mm
, m
);
186 if (manager_all_configured(m
))
187 sd_event_exit(m
->event
, 0);
192 static int manager_rtnl_listen(Manager
*m
) {
193 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
194 sd_netlink_message
*i
;
199 /* First, subscribe to interfaces coming and going */
200 r
= sd_netlink_open(&m
->rtnl
);
204 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
208 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-NEWLINK");
212 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-DELLINK");
216 /* Then, enumerate all links */
217 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
221 r
= sd_netlink_message_request_dump(req
, true);
225 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
229 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
230 r
= manager_process_link(m
->rtnl
, i
, m
);
238 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
239 Manager
*m
= userdata
;
246 sd_network_monitor_flush(m
->network_monitor
);
248 HASHMAP_FOREACH(l
, m
->links
, i
) {
249 r
= link_update_monitor(l
);
251 log_warning_errno(r
, "Failed to update monitor information for %i: %m", l
->ifindex
);
254 if (manager_all_configured(m
))
255 sd_event_exit(m
->event
, 0);
260 static int manager_network_monitor_listen(Manager
*m
) {
265 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
269 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
273 events
= sd_network_monitor_get_events(m
->network_monitor
);
277 r
= sd_event_add_io(m
->event
, &m
->network_monitor_event_source
,
278 fd
, events
, &on_network_event
, m
);
285 int manager_new(Manager
**ret
, Hashmap
*interfaces
, char **ignore
,
286 LinkOperationalState required_operstate
, usec_t timeout
) {
287 _cleanup_(manager_freep
) Manager
*m
= NULL
;
297 .interfaces
= interfaces
,
299 .required_operstate
= required_operstate
,
302 r
= sd_event_default(&m
->event
);
306 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
307 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
312 usec
= now(clock_boottime_or_monotonic()) + timeout
;
314 r
= sd_event_add_time(m
->event
, NULL
, clock_boottime_or_monotonic(), usec
, 0, NULL
, INT_TO_PTR(-ETIMEDOUT
));
319 sd_event_set_watchdog(m
->event
, true);
321 r
= manager_network_monitor_listen(m
);
325 r
= manager_rtnl_listen(m
);
334 void manager_free(Manager
*m
) {
338 hashmap_free_with_destructor(m
->links
, link_free
);
339 hashmap_free(m
->links_by_name
);
341 sd_event_source_unref(m
->network_monitor_event_source
);
342 sd_network_monitor_unref(m
->network_monitor
);
344 sd_event_source_unref(m
->rtnl_event_source
);
345 sd_netlink_unref(m
->rtnl
);
347 sd_event_unref(m
->event
);