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
);
95 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
99 r
= sd_netlink_message_read_string(mm
, IFLA_IFNAME
, &ifname
);
103 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
109 log_debug("Found link %i", ifindex
);
111 r
= link_new(m
, &l
, ifindex
, ifname
);
115 r
= link_update_monitor(l
);
120 r
= link_update_rtnl(l
, mm
);
128 log_debug("Removing link %i", l
->ifindex
);
138 log_warning_errno(r
, "Failed to process RTNL link message: %m");
142 static int on_rtnl_event(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
143 Manager
*m
= userdata
;
146 r
= manager_process_link(rtnl
, mm
, m
);
150 if (manager_all_configured(m
))
151 sd_event_exit(m
->event
, 0);
156 static int manager_rtnl_listen(Manager
*m
) {
157 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
158 sd_netlink_message
*i
;
163 /* First, subscribe to interfaces coming and going */
164 r
= sd_netlink_open(&m
->rtnl
);
168 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
172 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-NEWLINK");
176 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, on_rtnl_event
, NULL
, m
, "wait-online-on-DELLINK");
180 /* Then, enumerate all links */
181 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
185 r
= sd_netlink_message_request_dump(req
, true);
189 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
193 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
194 r
= manager_process_link(m
->rtnl
, i
, m
);
202 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
203 Manager
*m
= userdata
;
210 sd_network_monitor_flush(m
->network_monitor
);
212 HASHMAP_FOREACH(l
, m
->links
, i
) {
213 r
= link_update_monitor(l
);
215 log_warning_errno(r
, "Failed to update monitor information for %i: %m", l
->ifindex
);
218 if (manager_all_configured(m
))
219 sd_event_exit(m
->event
, 0);
224 static int manager_network_monitor_listen(Manager
*m
) {
229 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
233 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
237 events
= sd_network_monitor_get_events(m
->network_monitor
);
241 r
= sd_event_add_io(m
->event
, &m
->network_monitor_event_source
,
242 fd
, events
, &on_network_event
, m
);
249 int manager_new(Manager
**ret
, char **interfaces
, char **ignore
, usec_t timeout
) {
250 _cleanup_(manager_freep
) Manager
*m
= NULL
;
255 m
= new0(Manager
, 1);
259 m
->interfaces
= interfaces
;
262 r
= sd_event_default(&m
->event
);
266 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
267 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
272 usec
= now(clock_boottime_or_monotonic()) + timeout
;
274 r
= sd_event_add_time(m
->event
, NULL
, clock_boottime_or_monotonic(), usec
, 0, NULL
, INT_TO_PTR(-ETIMEDOUT
));
279 sd_event_set_watchdog(m
->event
, true);
281 r
= manager_network_monitor_listen(m
);
285 r
= manager_rtnl_listen(m
);
294 void manager_free(Manager
*m
) {
298 hashmap_free_with_destructor(m
->links
, link_free
);
299 hashmap_free(m
->links_by_name
);
301 sd_event_source_unref(m
->network_monitor_event_source
);
302 sd_network_monitor_unref(m
->network_monitor
);
304 sd_event_source_unref(m
->rtnl_event_source
);
305 sd_netlink_unref(m
->rtnl
);
307 sd_event_unref(m
->event
);