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 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
&& !strv_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 bool manager_all_configured(Manager
*m
) {
39 bool one_ready
= false;
41 /* wait for all the links given on the command line to appear */
42 STRV_FOREACH(ifname
, m
->interfaces
) {
43 l
= hashmap_get(m
->links_by_name
, *ifname
);
45 log_debug("still waiting for %s", *ifname
);
50 /* wait for all links networkd manages to be in admin state 'configured'
51 and at least one link to gain a carrier */
52 HASHMAP_FOREACH(l
, m
->links
, i
) {
53 if (manager_ignore_link(m
, l
)) {
54 log_info("ignoring: %s", l
->ifname
);
59 log_debug("link %s has not yet been processed by udev",
64 if (STR_IN_SET(l
->state
, "configuring", "pending")) {
65 log_debug("link %s is being processed by networkd",
70 if (l
->operational_state
&&
71 STR_IN_SET(l
->operational_state
, "degraded", "routable"))
72 /* we wait for at least one link to be ready,
73 regardless of who manages it */
80 static int manager_process_link(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
81 Manager
*m
= userdata
;
91 r
= sd_netlink_message_get_type(mm
, &type
);
93 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
97 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
99 log_warning_errno(r
, "rtnl: Could not get ifindex from link, ignoring: %m");
101 } else if (ifindex
<= 0) {
102 log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex
);
106 r
= sd_netlink_message_read_string(mm
, IFLA_IFNAME
, &ifname
);
108 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
112 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
118 log_debug("Found link %i", ifindex
);
120 r
= link_new(m
, &l
, ifindex
, ifname
);
122 return log_error_errno(r
, "Failed to create link object: %m");
124 r
= link_update_monitor(l
);
126 return log_error_errno(r
, "Failed to initialize link object: %m");
129 r
= link_update_rtnl(l
, mm
);
131 return log_warning_errno(r
, "Failed to process RTNL link message: %m");;
137 log_debug("Removing link %i", l
->ifindex
);
147 static int on_rtnl_event(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
148 Manager
*m
= userdata
;
151 r
= manager_process_link(rtnl
, mm
, m
);
155 if (manager_all_configured(m
))
156 sd_event_exit(m
->event
, 0);
161 static int manager_rtnl_listen(Manager
*m
) {
162 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
163 sd_netlink_message
*i
;
168 /* First, subscribe to interfaces coming and going */
169 r
= sd_netlink_open(&m
->rtnl
);
173 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
177 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-NEWLINK");
181 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-DELLINK");
185 /* Then, enumerate all links */
186 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
190 r
= sd_netlink_message_request_dump(req
, true);
194 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
198 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
199 r
= manager_process_link(m
->rtnl
, i
, m
);
207 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
208 Manager
*m
= userdata
;
215 sd_network_monitor_flush(m
->network_monitor
);
217 HASHMAP_FOREACH(l
, m
->links
, i
) {
218 r
= link_update_monitor(l
);
220 log_warning_errno(r
, "Failed to update monitor information for %i: %m", l
->ifindex
);
223 if (manager_all_configured(m
))
224 sd_event_exit(m
->event
, 0);
229 static int manager_network_monitor_listen(Manager
*m
) {
234 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
238 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
242 events
= sd_network_monitor_get_events(m
->network_monitor
);
246 r
= sd_event_add_io(m
->event
, &m
->network_monitor_event_source
,
247 fd
, events
, &on_network_event
, m
);
254 int manager_new(Manager
**ret
, char **interfaces
, char **ignore
, usec_t timeout
) {
255 _cleanup_(manager_freep
) Manager
*m
= NULL
;
260 m
= new0(Manager
, 1);
264 m
->interfaces
= interfaces
;
267 r
= sd_event_default(&m
->event
);
271 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
272 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
277 usec
= now(clock_boottime_or_monotonic()) + timeout
;
279 r
= sd_event_add_time(m
->event
, NULL
, clock_boottime_or_monotonic(), usec
, 0, NULL
, INT_TO_PTR(-ETIMEDOUT
));
284 sd_event_set_watchdog(m
->event
, true);
286 r
= manager_network_monitor_listen(m
);
290 r
= manager_rtnl_listen(m
);
299 void manager_free(Manager
*m
) {
303 hashmap_free_with_destructor(m
->links
, link_free
);
304 hashmap_free(m
->links_by_name
);
306 sd_event_source_unref(m
->network_monitor_event_source
);
307 sd_network_monitor_unref(m
->network_monitor
);
309 sd_event_source_unref(m
->rtnl_event_source
);
310 sd_netlink_unref(m
->rtnl
);
312 sd_event_unref(m
->event
);