2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/ether.h>
24 #include "alloc-util.h"
25 #include "netlink-util.h"
26 #include "network-internal.h"
27 #include "networkd-wait-online-link.h"
28 #include "networkd-wait-online.h"
29 #include "time-util.h"
32 bool manager_ignore_link(Manager
*m
, Link
*link
) {
38 /* always ignore the loopback interface */
39 if (link
->flags
& IFF_LOOPBACK
)
42 /* if interfaces are given on the command line, ignore all others */
43 if (m
->interfaces
&& !strv_contains(m
->interfaces
, link
->ifname
))
46 /* ignore interfaces we explicitly are asked to ignore */
47 STRV_FOREACH(ignore
, m
->ignore
)
48 if (fnmatch(*ignore
, link
->ifname
, 0) == 0)
54 bool manager_all_configured(Manager
*m
) {
58 bool one_ready
= false;
60 /* wait for all the links given on the command line to appear */
61 STRV_FOREACH(ifname
, m
->interfaces
) {
62 l
= hashmap_get(m
->links_by_name
, *ifname
);
64 log_debug("still waiting for %s", *ifname
);
69 /* wait for all links networkd manages to be in admin state 'configured'
70 and at least one link to gain a carrier */
71 HASHMAP_FOREACH(l
, m
->links
, i
) {
72 if (manager_ignore_link(m
, l
)) {
73 log_info("ignoring: %s", l
->ifname
);
78 log_debug("link %s has not yet been processed by udev",
83 if (STR_IN_SET(l
->state
, "configuring", "pending")) {
84 log_debug("link %s is being processed by networkd",
89 if (l
->operational_state
&&
90 STR_IN_SET(l
->operational_state
, "degraded", "routable"))
91 /* we wait for at least one link to be ready,
92 regardless of who manages it */
99 static int manager_process_link(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
100 Manager
*m
= userdata
;
110 r
= sd_netlink_message_get_type(mm
, &type
);
114 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
118 r
= sd_netlink_message_read_string(mm
, IFLA_IFNAME
, &ifname
);
122 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
128 log_debug("Found link %i", ifindex
);
130 r
= link_new(m
, &l
, ifindex
, ifname
);
134 r
= link_update_monitor(l
);
139 r
= link_update_rtnl(l
, mm
);
147 log_debug("Removing link %i", l
->ifindex
);
157 log_warning_errno(r
, "Failed to process RTNL link message: %m");
161 static int on_rtnl_event(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
162 Manager
*m
= userdata
;
165 r
= manager_process_link(rtnl
, mm
, m
);
169 if (manager_all_configured(m
))
170 sd_event_exit(m
->event
, 0);
175 static int manager_rtnl_listen(Manager
*m
) {
176 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
177 sd_netlink_message
*i
;
182 /* First, subscribe to interfaces coming and going */
183 r
= sd_netlink_open(&m
->rtnl
);
187 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
191 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWLINK
, on_rtnl_event
, m
);
195 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELLINK
, on_rtnl_event
, m
);
199 /* Then, enumerate all links */
200 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
204 r
= sd_netlink_message_request_dump(req
, true);
208 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
212 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
213 r
= manager_process_link(m
->rtnl
, i
, m
);
221 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
222 Manager
*m
= userdata
;
229 sd_network_monitor_flush(m
->network_monitor
);
231 HASHMAP_FOREACH(l
, m
->links
, i
) {
232 r
= link_update_monitor(l
);
234 log_warning_errno(r
, "Failed to update monitor information for %i: %m", l
->ifindex
);
237 if (manager_all_configured(m
))
238 sd_event_exit(m
->event
, 0);
243 static int manager_network_monitor_listen(Manager
*m
) {
248 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
252 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
256 events
= sd_network_monitor_get_events(m
->network_monitor
);
260 r
= sd_event_add_io(m
->event
, &m
->network_monitor_event_source
,
261 fd
, events
, &on_network_event
, m
);
268 int manager_new(Manager
**ret
, char **interfaces
, char **ignore
, usec_t timeout
) {
269 _cleanup_(manager_freep
) Manager
*m
= NULL
;
274 m
= new0(Manager
, 1);
278 m
->interfaces
= interfaces
;
281 r
= sd_event_default(&m
->event
);
285 sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
286 sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
291 usec
= now(clock_boottime_or_monotonic()) + timeout
;
293 r
= sd_event_add_time(m
->event
, NULL
, clock_boottime_or_monotonic(), usec
, 0, NULL
, INT_TO_PTR(-ETIMEDOUT
));
298 sd_event_set_watchdog(m
->event
, true);
300 r
= manager_network_monitor_listen(m
);
304 r
= manager_rtnl_listen(m
);
314 void manager_free(Manager
*m
) {
320 while ((l
= hashmap_first(m
->links
)))
322 hashmap_free(m
->links
);
323 hashmap_free(m
->links_by_name
);
325 sd_event_source_unref(m
->network_monitor_event_source
);
326 sd_network_monitor_unref(m
->network_monitor
);
328 sd_event_source_unref(m
->rtnl_event_source
);
329 sd_netlink_unref(m
->rtnl
);
331 sd_event_unref(m
->event
);