1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Tom Gundersen <teg@jklm.no>
6 #include <netinet/ether.h>
10 #include "alloc-util.h"
13 #include "netlink-util.h"
14 #include "network-internal.h"
15 #include "time-util.h"
18 bool manager_ignore_link(Manager
*m
, Link
*link
) {
22 /* always ignore the loopback interface */
23 if (link
->flags
& IFF_LOOPBACK
)
26 /* if interfaces are given on the command line, ignore all others */
27 if (m
->interfaces
&& !strv_contains(m
->interfaces
, link
->ifname
))
30 if (!link
->required_for_online
)
33 /* ignore interfaces we explicitly are asked to ignore */
34 return strv_fnmatch(m
->ignore
, link
->ifname
, 0);
37 bool manager_all_configured(Manager
*m
) {
41 bool one_ready
= false;
43 /* wait for all the links given on the command line to appear */
44 STRV_FOREACH(ifname
, m
->interfaces
) {
45 l
= hashmap_get(m
->links_by_name
, *ifname
);
47 log_debug("still waiting for %s", *ifname
);
52 /* wait for all links networkd manages to be in admin state 'configured'
53 and at least one link to gain a carrier */
54 HASHMAP_FOREACH(l
, m
->links
, i
) {
55 if (manager_ignore_link(m
, l
)) {
56 log_info("ignoring: %s", l
->ifname
);
61 log_debug("link %s has not yet been processed by udev",
66 if (STR_IN_SET(l
->state
, "configuring", "pending")) {
67 log_debug("link %s is being processed by networkd",
72 if (l
->operational_state
&&
73 STR_IN_SET(l
->operational_state
, "degraded", "routable"))
74 /* we wait for at least one link to be ready,
75 regardless of who manages it */
82 static int manager_process_link(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
83 Manager
*m
= userdata
;
93 r
= sd_netlink_message_get_type(mm
, &type
);
97 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
101 r
= sd_netlink_message_read_string(mm
, IFLA_IFNAME
, &ifname
);
105 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
111 log_debug("Found link %i", ifindex
);
113 r
= link_new(m
, &l
, ifindex
, ifname
);
117 r
= link_update_monitor(l
);
122 r
= link_update_rtnl(l
, mm
);
130 log_debug("Removing link %i", l
->ifindex
);
140 log_warning_errno(r
, "Failed to process RTNL link message: %m");
144 static int on_rtnl_event(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
145 Manager
*m
= userdata
;
148 r
= manager_process_link(rtnl
, mm
, m
);
152 if (manager_all_configured(m
))
153 sd_event_exit(m
->event
, 0);
158 static int manager_rtnl_listen(Manager
*m
) {
159 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
160 sd_netlink_message
*i
;
165 /* First, subscribe to interfaces coming and going */
166 r
= sd_netlink_open(&m
->rtnl
);
170 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
174 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWLINK
, on_rtnl_event
, m
);
178 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELLINK
, on_rtnl_event
, m
);
182 /* Then, enumerate all links */
183 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
187 r
= sd_netlink_message_request_dump(req
, true);
191 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
195 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
196 r
= manager_process_link(m
->rtnl
, i
, m
);
204 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
205 Manager
*m
= userdata
;
212 sd_network_monitor_flush(m
->network_monitor
);
214 HASHMAP_FOREACH(l
, m
->links
, i
) {
215 r
= link_update_monitor(l
);
217 log_warning_errno(r
, "Failed to update monitor information for %i: %m", l
->ifindex
);
220 if (manager_all_configured(m
))
221 sd_event_exit(m
->event
, 0);
226 static int manager_network_monitor_listen(Manager
*m
) {
231 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
235 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
239 events
= sd_network_monitor_get_events(m
->network_monitor
);
243 r
= sd_event_add_io(m
->event
, &m
->network_monitor_event_source
,
244 fd
, events
, &on_network_event
, m
);
251 int manager_new(Manager
**ret
, char **interfaces
, char **ignore
, usec_t timeout
) {
252 _cleanup_(manager_freep
) Manager
*m
= NULL
;
257 m
= new0(Manager
, 1);
261 m
->interfaces
= interfaces
;
264 r
= sd_event_default(&m
->event
);
268 sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
269 sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
274 usec
= now(clock_boottime_or_monotonic()) + timeout
;
276 r
= sd_event_add_time(m
->event
, NULL
, clock_boottime_or_monotonic(), usec
, 0, NULL
, INT_TO_PTR(-ETIMEDOUT
));
281 sd_event_set_watchdog(m
->event
, true);
283 r
= manager_network_monitor_listen(m
);
287 r
= manager_rtnl_listen(m
);
296 void manager_free(Manager
*m
) {
302 while ((l
= hashmap_first(m
->links
)))
304 hashmap_free(m
->links
);
305 hashmap_free(m
->links_by_name
);
307 sd_event_source_unref(m
->network_monitor_event_source
);
308 sd_network_monitor_unref(m
->network_monitor
);
310 sd_event_source_unref(m
->rtnl_event_source
);
311 sd_netlink_unref(m
->rtnl
);
313 sd_event_unref(m
->event
);