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 "rtnl-util.h"
28 #include "network-util.h"
29 #include "network-internal.h"
30 #include "networkd-wait-online-link.h"
31 #include "networkd-wait-online.h"
34 #include "time-util.h"
36 bool manager_ignore_link(Manager *m, Link *link) {
42 if (link->flags & IFF_LOOPBACK)
45 STRV_FOREACH(ignore, m->ignore)
46 if (fnmatch(*ignore, link->ifname, 0) == 0)
52 bool manager_all_configured(Manager *m) {
56 bool one_ready = false;
58 /* wait for all the links given on the command line to appear */
59 STRV_FOREACH(ifname, m->interfaces) {
60 l = hashmap_get(m->links_by_name, *ifname);
62 log_debug("still waiting for %s", *ifname);
67 /* wait for all links networkd manages to be in admin state 'configured'
68 and at least one link to gain a carrier */
69 HASHMAP_FOREACH(l, m->links, i) {
70 if (manager_ignore_link(m, l)) {
71 log_info("ignoring: %s", l->ifname);
76 log_debug("link %s has not yet been processed by udev",
81 if (streq(l->state, "configuring")) {
82 log_debug("link %s is being processed by networkd",
87 if (l->operational_state &&
88 STR_IN_SET(l->operational_state, "degraded", "routable"))
89 /* we wait for at least one link to be ready,
90 regardless of who manages it */
97 static int manager_process_link(sd_rtnl *rtnl, sd_rtnl_message *mm, void *userdata) {
98 Manager *m = userdata;
108 r = sd_rtnl_message_get_type(mm, &type);
112 r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
116 r = sd_rtnl_message_read_string(mm, IFLA_IFNAME, &ifname);
120 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
126 log_debug("Found link %i", ifindex);
128 r = link_new(m, &l, ifindex, ifname);
132 r = link_update_monitor(l);
137 r = link_update_rtnl(l, mm);
145 log_debug("Removing link %i", l->ifindex);
155 log_warning_errno(r, "Failed to process RTNL link message: %m");
159 static int on_rtnl_event(sd_rtnl *rtnl, sd_rtnl_message *mm, void *userdata) {
160 Manager *m = userdata;
163 r = manager_process_link(rtnl, mm, m);
167 if (manager_all_configured(m))
168 sd_event_exit(m->event, 0);
173 static int manager_rtnl_listen(Manager *m) {
174 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
180 /* First, subscribe to interfaces coming and going */
181 r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR);
185 r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
189 r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, on_rtnl_event, m);
193 r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, on_rtnl_event, m);
197 /* Then, enumerate all links */
198 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
202 r = sd_rtnl_message_request_dump(req, true);
206 r = sd_rtnl_call(m->rtnl, req, 0, &reply);
210 for (i = reply; i; i = sd_rtnl_message_next(i)) {
211 r = manager_process_link(m->rtnl, i, m);
219 static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
220 Manager *m = userdata;
227 sd_network_monitor_flush(m->network_monitor);
229 HASHMAP_FOREACH(l, m->links, i) {
230 r = link_update_monitor(l);
232 log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex);
235 if (manager_all_configured(m))
236 sd_event_exit(m->event, 0);
241 static int manager_network_monitor_listen(Manager *m) {
246 r = sd_network_monitor_new(&m->network_monitor, NULL);
250 fd = sd_network_monitor_get_fd(m->network_monitor);
254 events = sd_network_monitor_get_events(m->network_monitor);
258 r = sd_event_add_io(m->event, &m->network_monitor_event_source,
259 fd, events, &on_network_event, m);
266 int manager_new(Manager **ret, char **interfaces, char **ignore, usec_t timeout) {
267 _cleanup_(manager_freep) Manager *m = NULL;
272 m = new0(Manager, 1);
276 m->interfaces = interfaces;
279 r = sd_event_default(&m->event);
283 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
284 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
289 usec = now(clock_boottime_or_monotonic()) + timeout;
291 r = sd_event_add_time(m->event, NULL, clock_boottime_or_monotonic(), usec, 0, NULL, INT_TO_PTR(-ETIMEDOUT));
296 sd_event_set_watchdog(m->event, true);
298 r = manager_network_monitor_listen(m);
302 r = manager_rtnl_listen(m);
312 void manager_free(Manager *m) {
318 while ((l = hashmap_first(m->links)))
320 hashmap_free(m->links);
321 hashmap_free(m->links_by_name);
323 sd_event_source_unref(m->network_monitor_event_source);
324 sd_network_monitor_unref(m->network_monitor);
326 sd_event_source_unref(m->rtnl_event_source);
327 sd_rtnl_unref(m->rtnl);
329 sd_event_unref(m->event);