2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-07-16 10:52:47 +02:00
|
|
|
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <linux/if.h>
|
2015-02-01 15:13:17 +01:00
|
|
|
#include <fnmatch.h>
|
2014-07-16 10:52:47 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2016-11-13 01:29:28 +01:00
|
|
|
#include "link.h"
|
|
|
|
#include "manager.h"
|
2015-06-12 16:31:33 +02:00
|
|
|
#include "netlink-util.h"
|
2014-07-16 10:52:47 +02:00
|
|
|
#include "network-internal.h"
|
2018-08-22 07:30:49 +02:00
|
|
|
#include "strv.h"
|
2015-02-01 21:04:35 +01:00
|
|
|
#include "time-util.h"
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "util.h"
|
2014-07-16 10:52:47 +02:00
|
|
|
|
2019-03-06 15:46:33 +01:00
|
|
|
static bool manager_ignore_link(Manager *m, Link *link) {
|
2015-01-29 07:34:34 +01:00
|
|
|
assert(m);
|
|
|
|
assert(link);
|
|
|
|
|
2015-07-29 01:41:24 +02:00
|
|
|
/* always ignore the loopback interface */
|
2015-01-29 07:34:34 +01:00
|
|
|
if (link->flags & IFF_LOOPBACK)
|
|
|
|
return true;
|
|
|
|
|
2015-07-29 01:41:24 +02:00
|
|
|
/* if interfaces are given on the command line, ignore all others */
|
2019-03-06 15:46:33 +01:00
|
|
|
if (m->interfaces && !hashmap_contains(m->interfaces, link->ifname))
|
2015-07-29 01:41:24 +02:00
|
|
|
return true;
|
|
|
|
|
2017-11-30 18:03:50 +01:00
|
|
|
if (!link->required_for_online)
|
|
|
|
return true;
|
|
|
|
|
2015-07-29 01:41:24 +02:00
|
|
|
/* ignore interfaces we explicitly are asked to ignore */
|
2020-01-12 12:26:26 +01:00
|
|
|
return strv_fnmatch(m->ignore, link->ifname);
|
2015-01-29 07:34:34 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 21:31:50 +01:00
|
|
|
static int manager_link_is_online(Manager *m, Link *l, LinkOperationalStateRange s) {
|
2019-03-06 15:46:33 +01:00
|
|
|
/* This returns the following:
|
|
|
|
* -EAGAIN: not processed by udev or networkd
|
|
|
|
* 0: operstate is not enough
|
|
|
|
* 1: online */
|
|
|
|
|
2019-03-06 16:16:54 +01:00
|
|
|
if (!l->state)
|
|
|
|
return log_link_debug_errno(l, SYNTHETIC_ERRNO(EAGAIN),
|
|
|
|
"link has not yet been processed by udev");
|
2019-03-06 15:46:33 +01:00
|
|
|
|
2019-03-06 16:16:54 +01:00
|
|
|
if (STR_IN_SET(l->state, "configuring", "pending"))
|
|
|
|
return log_link_debug_errno(l, SYNTHETIC_ERRNO(EAGAIN),
|
|
|
|
"link is being processed by networkd");
|
2019-03-06 15:46:33 +01:00
|
|
|
|
2020-01-09 21:31:50 +01:00
|
|
|
if (s.min < 0)
|
|
|
|
s.min = m->required_operstate.min >= 0 ? m->required_operstate.min
|
|
|
|
: l->required_operstate.min;
|
2019-03-06 15:46:33 +01:00
|
|
|
|
2020-01-09 21:31:50 +01:00
|
|
|
if (s.max < 0)
|
|
|
|
s.max = m->required_operstate.max >= 0 ? m->required_operstate.max
|
|
|
|
: l->required_operstate.max;
|
|
|
|
|
|
|
|
if (l->operational_state < s.min || l->operational_state > s.max) {
|
|
|
|
log_link_debug(l, "Operational state '%s' is not in range ['%s':'%s']",
|
2019-03-06 16:16:54 +01:00
|
|
|
link_operstate_to_string(l->operational_state),
|
2020-01-09 21:31:50 +01:00
|
|
|
link_operstate_to_string(s.min), link_operstate_to_string(s.max));
|
2019-03-06 15:46:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-31 23:43:48 +02:00
|
|
|
bool manager_configured(Manager *m) {
|
2019-03-06 15:46:33 +01:00
|
|
|
bool one_ready = false;
|
2014-07-16 10:52:47 +02:00
|
|
|
Iterator i;
|
2019-03-06 15:46:33 +01:00
|
|
|
const char *ifname;
|
|
|
|
void *p;
|
2014-07-16 10:52:47 +02:00
|
|
|
Link *l;
|
2019-03-06 15:46:33 +01:00
|
|
|
int r;
|
2014-07-16 10:52:47 +02:00
|
|
|
|
2019-03-31 23:43:48 +02:00
|
|
|
if (!hashmap_isempty(m->interfaces)) {
|
|
|
|
/* wait for all the links given on the command line to appear */
|
|
|
|
HASHMAP_FOREACH_KEY(p, ifname, m->interfaces, i) {
|
2020-01-09 21:31:50 +01:00
|
|
|
LinkOperationalStateRange *range = p;
|
2019-03-31 23:43:48 +02:00
|
|
|
|
|
|
|
l = hashmap_get(m->links_by_name, ifname);
|
2020-01-14 14:08:03 +01:00
|
|
|
if (!l && range->min == LINK_OPERSTATE_MISSING) {
|
|
|
|
one_ready = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-31 23:43:48 +02:00
|
|
|
if (!l) {
|
|
|
|
log_debug("still waiting for %s", ifname);
|
|
|
|
if (!m->any)
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-09 21:31:50 +01:00
|
|
|
if (manager_link_is_online(m, l, *range) <= 0) {
|
2019-03-31 23:43:48 +02:00
|
|
|
if (!m->any)
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-06 15:46:33 +01:00
|
|
|
|
2019-03-31 23:43:48 +02:00
|
|
|
one_ready = true;
|
2014-07-16 10:52:47 +02:00
|
|
|
}
|
2019-03-06 15:46:33 +01:00
|
|
|
|
2019-03-31 23:43:48 +02:00
|
|
|
/* all interfaces given by the command line are online, or
|
|
|
|
* one of the specified interfaces is online. */
|
|
|
|
return one_ready;
|
2014-07-16 10:52:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for all links networkd manages to be in admin state 'configured'
|
2019-03-06 15:46:33 +01:00
|
|
|
* and at least one link to gain a carrier */
|
2014-07-16 10:52:47 +02:00
|
|
|
HASHMAP_FOREACH(l, m->links, i) {
|
2015-01-29 07:34:34 +01:00
|
|
|
if (manager_ignore_link(m, l)) {
|
2019-07-16 03:21:18 +02:00
|
|
|
log_link_debug(l, "link is ignored");
|
2014-07-16 10:52:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-09 21:31:50 +01:00
|
|
|
r = manager_link_is_online(m, l,
|
|
|
|
(LinkOperationalStateRange) { _LINK_OPERSTATE_INVALID,
|
|
|
|
_LINK_OPERSTATE_INVALID });
|
2019-03-31 23:43:48 +02:00
|
|
|
if (r < 0 && !m->any)
|
2014-07-16 10:52:47 +02:00
|
|
|
return false;
|
2019-03-06 15:46:33 +01:00
|
|
|
if (r > 0)
|
2014-07-16 10:52:47 +02:00
|
|
|
/* we wait for at least one link to be ready,
|
2019-03-06 15:46:33 +01:00
|
|
|
* regardless of who manages it */
|
2014-07-16 10:52:47 +02:00
|
|
|
one_ready = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return one_ready;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
|
2014-07-16 10:52:47 +02:00
|
|
|
Manager *m = userdata;
|
|
|
|
uint16_t type;
|
|
|
|
Link *l;
|
2014-07-18 13:25:18 +02:00
|
|
|
const char *ifname;
|
2014-07-16 10:52:47 +02:00
|
|
|
int ifindex, r;
|
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
assert(m);
|
|
|
|
assert(mm);
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_type(mm, &type);
|
2019-01-18 23:59:17 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-16 10:52:47 +02:00
|
|
|
|
|
|
|
r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
|
2019-01-18 23:59:17 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_warning_errno(r, "rtnl: Could not get ifindex from link, ignoring: %m");
|
|
|
|
return 0;
|
|
|
|
} else if (ifindex <= 0) {
|
|
|
|
log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-16 10:52:47 +02:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_read_string(mm, IFLA_IFNAME, &ifname);
|
2019-01-18 23:59:17 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-16 10:52:47 +02:00
|
|
|
|
|
|
|
l = hashmap_get(m->links, INT_TO_PTR(ifindex));
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
if (!l) {
|
|
|
|
log_debug("Found link %i", ifindex);
|
|
|
|
|
|
|
|
r = link_new(m, &l, ifindex, ifname);
|
|
|
|
if (r < 0)
|
2019-01-18 23:59:17 +01:00
|
|
|
return log_error_errno(r, "Failed to create link object: %m");
|
2014-07-16 10:52:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r = link_update_rtnl(l, mm);
|
|
|
|
if (r < 0)
|
2019-03-06 16:16:54 +01:00
|
|
|
log_link_warning_errno(l, r, "Failed to process RTNL link message, ignoring: %m");
|
2019-03-06 15:46:33 +01:00
|
|
|
|
|
|
|
r = link_update_monitor(l);
|
2019-05-15 06:15:16 +02:00
|
|
|
if (r < 0 && r != -ENODATA)
|
2019-03-06 16:16:54 +01:00
|
|
|
log_link_warning_errno(l, r, "Failed to update link state, ignoring: %m");
|
2014-07-16 10:52:47 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTM_DELLINK:
|
|
|
|
if (l) {
|
2019-03-06 16:16:54 +01:00
|
|
|
log_link_debug(l, "Removing link");
|
2014-07-16 10:52:47 +02:00
|
|
|
link_free(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
static int on_rtnl_event(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
|
2014-07-16 10:52:47 +02:00
|
|
|
Manager *m = userdata;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = manager_process_link(rtnl, mm, m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2019-03-31 23:43:48 +02:00
|
|
|
if (manager_configured(m))
|
2014-07-16 10:52:47 +02:00
|
|
|
sd_event_exit(m->event, 0);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int manager_rtnl_listen(Manager *m) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message *i;
|
2014-07-16 10:52:47 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2015-01-20 12:56:14 +01:00
|
|
|
/* First, subscribe to interfaces coming and going */
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_open(&m->rtnl);
|
2014-07-16 10:52:47 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_attach_event(m->rtnl, m->event, 0);
|
2014-07-16 10:52:47 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-10-15 10:49:53 +02:00
|
|
|
r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, on_rtnl_event, NULL, m, "wait-online-on-NEWLINK");
|
2014-07-16 10:52:47 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-10-15 10:49:53 +02:00
|
|
|
r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, on_rtnl_event, NULL, m, "wait-online-on-DELLINK");
|
2014-07-16 10:52:47 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* Then, enumerate all links */
|
|
|
|
r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_request_dump(req, true);
|
2014-07-16 10:52:47 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_call(m->rtnl, req, 0, &reply);
|
2014-07-16 10:52:47 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
for (i = reply; i; i = sd_netlink_message_next(i)) {
|
2014-07-16 10:52:47 +02:00
|
|
|
r = manager_process_link(m->rtnl, i, m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Iterator i;
|
|
|
|
Link *l;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
sd_network_monitor_flush(m->network_monitor);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(l, m->links, i) {
|
|
|
|
r = link_update_monitor(l);
|
2019-05-15 06:15:16 +02:00
|
|
|
if (r < 0 && r != -ENODATA)
|
|
|
|
log_link_warning_errno(l, r, "Failed to update link state, ignoring: %m");
|
2014-07-16 10:52:47 +02:00
|
|
|
}
|
|
|
|
|
2019-03-31 23:43:48 +02:00
|
|
|
if (manager_configured(m))
|
2014-07-16 10:52:47 +02:00
|
|
|
sd_event_exit(m->event, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int manager_network_monitor_listen(Manager *m) {
|
|
|
|
int r, fd, events;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_network_monitor_new(&m->network_monitor, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
fd = sd_network_monitor_get_fd(m->network_monitor);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
events = sd_network_monitor_get_events(m->network_monitor);
|
|
|
|
if (events < 0)
|
|
|
|
return events;
|
|
|
|
|
|
|
|
r = sd_event_add_io(m->event, &m->network_monitor_event_source,
|
|
|
|
fd, events, &on_network_event, m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-06 15:46:33 +01:00
|
|
|
int manager_new(Manager **ret, Hashmap *interfaces, char **ignore,
|
2020-01-09 21:31:50 +01:00
|
|
|
LinkOperationalStateRange required_operstate,
|
2019-03-31 23:43:48 +02:00
|
|
|
bool any, usec_t timeout) {
|
2014-07-16 10:52:47 +02:00
|
|
|
_cleanup_(manager_freep) Manager *m = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(ret);
|
|
|
|
|
2019-03-06 15:46:33 +01:00
|
|
|
m = new(Manager, 1);
|
2014-07-16 10:52:47 +02:00
|
|
|
if (!m)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-03-06 15:46:33 +01:00
|
|
|
*m = (Manager) {
|
|
|
|
.interfaces = interfaces,
|
|
|
|
.ignore = ignore,
|
|
|
|
.required_operstate = required_operstate,
|
2019-03-31 23:43:48 +02:00
|
|
|
.any = any,
|
2019-03-06 15:46:33 +01:00
|
|
|
};
|
2014-07-16 10:52:47 +02:00
|
|
|
|
|
|
|
r = sd_event_default(&m->event);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-07-14 20:40:26 +02:00
|
|
|
(void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
|
2018-10-12 18:54:37 +02:00
|
|
|
(void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
|
2014-07-16 10:52:47 +02:00
|
|
|
|
2015-02-01 21:04:35 +01:00
|
|
|
if (timeout > 0) {
|
|
|
|
usec_t usec;
|
|
|
|
|
|
|
|
usec = now(clock_boottime_or_monotonic()) + timeout;
|
|
|
|
|
|
|
|
r = sd_event_add_time(m->event, NULL, clock_boottime_or_monotonic(), usec, 0, NULL, INT_TO_PTR(-ETIMEDOUT));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-16 10:52:47 +02:00
|
|
|
sd_event_set_watchdog(m->event, true);
|
|
|
|
|
|
|
|
r = manager_network_monitor_listen(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = manager_rtnl_listen(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
*ret = TAKE_PTR(m);
|
2014-07-16 10:52:47 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void manager_free(Manager *m) {
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
|
2018-11-12 07:34:39 +01:00
|
|
|
hashmap_free_with_destructor(m->links, link_free);
|
2014-07-16 10:52:47 +02:00
|
|
|
hashmap_free(m->links_by_name);
|
|
|
|
|
|
|
|
sd_event_source_unref(m->network_monitor_event_source);
|
|
|
|
sd_network_monitor_unref(m->network_monitor);
|
|
|
|
|
|
|
|
sd_event_source_unref(m->rtnl_event_source);
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_unref(m->rtnl);
|
2014-07-16 10:52:47 +02:00
|
|
|
|
|
|
|
sd_event_unref(m->event);
|
|
|
|
free(m);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|