1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/ether.h>
26 #include "netlink-util.h"
28 #include "network-internal.h"
29 #include "networkd-wait-online-link.h"
30 #include "networkd-wait-online.h"
33 #include "time-util.h"
35 bool manager_ignore_link(Manager
*m
, Link
*link
) {
41 /* always ignore the loopback interface */
42 if (link
->flags
& IFF_LOOPBACK
)
45 /* if interfaces are given on the command line, ignore all others */
46 if (m
->interfaces
&& !strv_contains(m
->interfaces
, link
->ifname
))
49 /* ignore interfaces we explicitly are asked to ignore */
50 STRV_FOREACH(ignore
, m
->ignore
)
51 if (fnmatch(*ignore
, link
->ifname
, 0) == 0)
57 bool manager_all_configured(Manager
*m
) {
61 bool one_ready
= false;
63 /* wait for all the links given on the command line to appear */
64 STRV_FOREACH(ifname
, m
->interfaces
) {
65 l
= hashmap_get(m
->links_by_name
, *ifname
);
67 log_debug("still waiting for %s", *ifname
);
72 /* wait for all links networkd manages to be in admin state 'configured'
73 and at least one link to gain a carrier */
74 HASHMAP_FOREACH(l
, m
->links
, i
) {
75 if (manager_ignore_link(m
, l
)) {
76 log_info("ignoring: %s", l
->ifname
);
81 log_debug("link %s has not yet been processed by udev",
86 if (STR_IN_SET(l
->state
, "configuring", "pending")) {
87 log_debug("link %s is being processed by networkd",
92 if (l
->operational_state
&&
93 STR_IN_SET(l
->operational_state
, "degraded", "routable"))
94 /* we wait for at least one link to be ready,
95 regardless of who manages it */
102 static int manager_process_link(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
103 Manager
*m
= userdata
;
113 r
= sd_netlink_message_get_type(mm
, &type
);
117 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
121 r
= sd_netlink_message_read_string(mm
, IFLA_IFNAME
, &ifname
);
125 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
131 log_debug("Found link %i", ifindex
);
133 r
= link_new(m
, &l
, ifindex
, ifname
);
137 r
= link_update_monitor(l
);
142 r
= link_update_rtnl(l
, mm
);
150 log_debug("Removing link %i", l
->ifindex
);
160 log_warning_errno(r
, "Failed to process RTNL link message: %m");
164 static int on_rtnl_event(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
165 Manager
*m
= userdata
;
168 r
= manager_process_link(rtnl
, mm
, m
);
172 if (manager_all_configured(m
))
173 sd_event_exit(m
->event
, 0);
178 static int manager_rtnl_listen(Manager
*m
) {
179 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
, *reply
= NULL
;
180 sd_netlink_message
*i
;
185 /* First, subscribe to interfaces coming and going */
186 r
= sd_netlink_open(&m
->rtnl
);
190 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
194 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWLINK
, on_rtnl_event
, m
);
198 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELLINK
, on_rtnl_event
, m
);
202 /* Then, enumerate all links */
203 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
207 r
= sd_netlink_message_request_dump(req
, true);
211 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
215 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
216 r
= manager_process_link(m
->rtnl
, i
, m
);
224 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
225 Manager
*m
= userdata
;
232 sd_network_monitor_flush(m
->network_monitor
);
234 HASHMAP_FOREACH(l
, m
->links
, i
) {
235 r
= link_update_monitor(l
);
237 log_warning_errno(r
, "Failed to update monitor information for %i: %m", l
->ifindex
);
240 if (manager_all_configured(m
))
241 sd_event_exit(m
->event
, 0);
246 static int manager_network_monitor_listen(Manager
*m
) {
251 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
255 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
259 events
= sd_network_monitor_get_events(m
->network_monitor
);
263 r
= sd_event_add_io(m
->event
, &m
->network_monitor_event_source
,
264 fd
, events
, &on_network_event
, m
);
271 int manager_new(Manager
**ret
, char **interfaces
, char **ignore
, usec_t timeout
) {
272 _cleanup_(manager_freep
) Manager
*m
= NULL
;
277 m
= new0(Manager
, 1);
281 m
->interfaces
= interfaces
;
284 r
= sd_event_default(&m
->event
);
288 sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
289 sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
294 usec
= now(clock_boottime_or_monotonic()) + timeout
;
296 r
= sd_event_add_time(m
->event
, NULL
, clock_boottime_or_monotonic(), usec
, 0, NULL
, INT_TO_PTR(-ETIMEDOUT
));
301 sd_event_set_watchdog(m
->event
, true);
303 r
= manager_network_monitor_listen(m
);
307 r
= manager_rtnl_listen(m
);
317 void manager_free(Manager
*m
) {
323 while ((l
= hashmap_first(m
->links
)))
325 hashmap_free(m
->links
);
326 hashmap_free(m
->links_by_name
);
328 sd_event_source_unref(m
->network_monitor_event_source
);
329 sd_network_monitor_unref(m
->network_monitor
);
331 sd_event_source_unref(m
->rtnl_event_source
);
332 sd_netlink_unref(m
->rtnl
);
334 sd_event_unref(m
->event
);