networkd-manager.c revision b5efdb8af40ea759a1ea584c1bc44ecc81dd00ce
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "sd-daemon.h"
#include "sd-netlink.h"
#include "alloc-util.h"
#include "bus-util.h"
#include "conf-parser.h"
#include "def.h"
#include "fd-util.h"
#include "fileio.h"
#include "libudev-private.h"
#include "local-addresses.h"
#include "netlink-util.h"
#include "networkd.h"
#include "path-util.h"
#include "set.h"
#include "udev-util.h"
#include "virt.h"
/* use 8 MB for receive socket kernel queue. */
const char* const network_dirs[] = {
#ifdef HAVE_SPLIT_USR
#endif
NULL};
static int setup_default_address_pool(Manager *m) {
AddressPool *p;
int r;
assert(m);
/* Add in the well-known private address ranges. */
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
assert(s);
assert(m);
return 0;
}
static int manager_reset_all(Manager *m) {
Iterator i;
int r;
assert(m);
r = link_carrier_reset(link);
if (r < 0)
}
return 0;
}
static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
int b, r;
if (r < 0) {
log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
return 0;
}
if (b)
return 0;
log_debug("Coming back from suspend, resetting all connections...");
return 0;
}
int manager_connect_bus(Manager *m) {
int r;
assert(m);
r = sd_bus_default_system(&m->bus);
if (r == -ENOENT) {
/* We failed to connect? Yuck, we must be in early
* boot. Let's try in 5s again. As soon as we have
* kdbus we can stop doing this... */
log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
if (r < 0)
return log_error_errno(r, "Failed to install bus reconnect time event: %m");
return 0;
}
if (r < 0)
return r;
"type='signal',"
"sender='org.freedesktop.login1',"
"interface='org.freedesktop.login1.Manager',"
"member='PrepareForSleep',"
"path='/org/freedesktop/login1'",
m);
if (r < 0)
return log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m);
if (r < 0)
return log_error_errno(r, "Failed to add manager object vtable: %m");
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m);
if (r < 0)
return log_error_errno(r, "Failed to add link object vtable: %m");
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m);
if (r < 0)
return log_error_errno(r, "Failed to add link enumerator: %m");
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m);
if (r < 0)
return log_error_errno(r, "Failed to add network object vtable: %m");
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m);
if (r < 0)
return log_error_errno(r, "Failed to add network enumerator: %m");
if (r < 0)
return log_error_errno(r, "Failed to register name: %m");
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
return 0;
}
int r, ifindex;
assert(m);
return 0;
if (ifindex <= 0) {
log_debug("Ignoring udev ADD event for device with invalid ifindex");
return 0;
}
if (r == -ENODEV)
return 0;
else if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
if (!device)
return -ENOMEM;
return 0;
}
static int manager_connect_udev(Manager *m) {
int r;
/* udev does not initialize devices inside containers,
* so we rely on them being already initialized before
* entering the container */
if (detect_container() > 0)
return 0;
if (!m->udev)
return -ENOMEM;
if (!m->udev_monitor)
return -ENOMEM;
if (r < 0)
return log_error_errno(r, "Could not add udev monitor filter: %m");
if (r < 0) {
log_error("Could not enable udev monitor");
return r;
}
r = sd_event_add_io(m->event,
&m->udev_event_source,
m);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
unsigned char flags;
int family;
unsigned char prefixlen;
unsigned char scope;
union in_addr_union in_addr;
struct ifa_cacheinfo cinfo;
int r, ifindex;
assert(m);
if (sd_netlink_message_is_error(message)) {
if (r < 0)
log_warning_errno(r, "rtnl: failed to receive address: %m");
return 0;
}
if (r < 0) {
log_warning_errno(r, "rtnl: could not get message type: %m");
return 0;
log_warning("rtnl: received unexpected message type when processing address");
return 0;
}
if (r < 0) {
log_warning_errno(r, "rtnl: could not get ifindex from address: %m");
return 0;
} else if (ifindex <= 0) {
return 0;
} else {
if (r < 0 || !link) {
/* when enumerating we might be out of sync, but we will
* get the address again, so just ignore it */
if (!m->enumerating)
return 0;
}
}
return 0;
}
if (r < 0) {
return 0;
}
if (r < 0) {
return 0;
}
if (r < 0) {
return 0;
}
switch (family) {
case AF_INET:
if (r < 0) {
return 0;
}
break;
case AF_INET6:
if (r < 0) {
return 0;
}
break;
default:
assert_not_reached("invalid address family");
}
return 0;
}
if (r >= 0) {
valid_str = "ever";
else
}
switch (type) {
case RTM_NEWADDR:
if (address)
else {
/* An address appeared that we did not request */
if (r < 0) {
return 0;
} else
}
break;
case RTM_DELADDR:
if (address) {
} else
log_link_warning(link, "Removing non-existent address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
break;
default:
assert_not_reached("Received invalid RTNL message type");
}
return 1;
}
static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
const char *name;
int r, ifindex;
assert(m);
if (sd_netlink_message_is_error(message)) {
if (r < 0)
log_warning_errno(r, "rtnl: Could not receive link: %m");
return 0;
}
if (r < 0) {
log_warning_errno(r, "rtnl: Could not get message type: %m");
return 0;
log_warning("rtnl: Received unexpected message type when processing link");
return 0;
}
if (r < 0) {
log_warning_errno(r, "rtnl: Could not get ifindex from link: %m");
return 0;
} else if (ifindex <= 0) {
return 0;
} else
if (r < 0) {
log_warning_errno(r, "rtnl: Received link message without ifname: %m");
return 0;
} else
switch (type) {
case RTM_NEWLINK:
if (!link) {
/* link is new, so add it */
if (r < 0) {
log_warning_errno(r, "Could not add new link: %m");
return 0;
}
}
if (netdev) {
/* netdev exists, so make sure the ifindex matches */
if (r < 0) {
log_warning_errno(r, "Could not set ifindex on netdev: %m");
return 0;
}
}
if (r < 0)
return 0;
break;
case RTM_DELLINK:
break;
default:
assert_not_reached("Received invalid RTNL message type.");
}
return 1;
}
static int systemd_netlink_fd(void) {
n = sd_listen_fds(true);
if (n <= 0)
return -EINVAL;
if (rtnl_fd >= 0)
return -EINVAL;
}
}
return rtnl_fd;
}
static int manager_connect_rtnl(Manager *m) {
int fd, r;
assert(m);
fd = systemd_netlink_fd();
if (fd < 0)
r = sd_netlink_open(&m->rtnl);
else
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
char *p;
int r;
assert(s);
if (r < 0)
return r;
r = set_consume(s, p);
if (r == -EEXIST)
return 0;
return r;
}
int r, i, c = 0;
assert(s);
for (i = 0; i < n; i++) {
r = set_put_in_addr(s, addresses+i);
if (r < 0)
return r;
c += r;
}
return c;
}
bool space = false;
Iterator i;
char *p;
if (set_isempty(s))
return;
SET_FOREACH(p, s, i) {
if (space)
fputc(' ', f);
fputs(p, f);
space = true;
}
fputc('\n', f);
}
static int manager_save(Manager *m) {
Iterator i;
const char *operstate_str;
int r;
assert(m);
assert(m->state_file);
/* We add all NTP and DNS server to a set, to filter out duplicates */
if (!dns)
return -ENOMEM;
if (!ntp)
return -ENOMEM;
if (!domains)
return -ENOMEM;
continue;
continue;
/* First add the static configured entries */
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (!link->dhcp_lease)
continue;
/* Secondly, add the entries acquired via DHCP */
if (r > 0) {
if (r < 0)
return r;
} else if (r < 0 && r != -ENODATA)
return r;
}
if (r > 0) {
if (r < 0)
return r;
} else if (r < 0 && r != -ENODATA)
return r;
}
const char *domainname;
if (r >= 0) {
if (r < 0)
return r;
} else if (r != -ENODATA)
return r;
}
}
if (r < 0)
return r;
fprintf(f,
"# This is private data. Do not parse.\n"
"OPER_STATE=%s\n", operstate_str);
r = fflush_and_check(f);
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
if (m->operational_state != operstate) {
m->operational_state = operstate;
if (r < 0)
log_error_errno(r, "Could not emit changed OperationalState: %m");
}
m->dirty = false;
return 0;
fail:
(void) unlink(m->state_file);
}
Iterator i;
int r;
assert(m);
if (m->dirty)
manager_save(m);
if (r >= 0)
}
return 1;
}
int r;
if (!m)
return -ENOMEM;
if (!m->state_file)
return -ENOMEM;
r = sd_event_default(&m->event);
if (r < 0)
return r;
sd_event_set_watchdog(m->event, true);
if (r < 0)
return r;
r = manager_connect_rtnl(m);
if (r < 0)
return r;
r = manager_connect_udev(m);
if (r < 0)
return r;
if (!m->netdevs)
return -ENOMEM;
LIST_HEAD_INIT(m->networks);
r = setup_default_address_pool(m);
if (r < 0)
return r;
*ret = m;
m = NULL;
return 0;
}
void manager_free(Manager *m) {
if (!m)
return;
free(m->state_file);
hashmap_free(m->links);
hashmap_free(m->netdevs);
while ((pool = m->address_pools))
sd_netlink_unref(m->rtnl);
sd_event_unref(m->event);
udev_unref(m->udev);
sd_bus_unref(m->bus);
free(m);
}
static bool manager_check_idle(void *userdata) {
Iterator i;
assert(m);
/* we are not woken on udev activity, so let's just wait for the
* pending udev event */
return false;
continue;
/* we are not woken on netork activity, so let's stay around */
if (link_lldp_enabled(link) ||
return false;
}
return true;
}
int manager_run(Manager *m) {
Iterator i;
assert(m);
/* The dirty handler will deal with future serialization, but the first one
must be done explicitly. */
manager_save(m);
if (m->bus)
return bus_event_loop_with_idle(
m->event,
m->bus,
"org.freedesktop.network1",
m);
else
/* failed to connect to the bus, so we lose exit-on-idle logic,
this should not happen except if dbus is not around at all */
return sd_event_loop(m->event);
}
int manager_load_config(Manager *m) {
int r;
/* update timestamp */
r = netdev_load(m);
if (r < 0)
return r;
r = network_load(m);
if (r < 0)
return r;
return 0;
}
bool manager_should_reload(Manager *m) {
}
int manager_rtnl_enumerate_links(Manager *m) {
int r;
assert(m);
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
if (r < 0)
return r;
if (r < 0)
return r;
int k;
m->enumerating = true;
if (k < 0)
r = k;
m->enumerating = false;
}
return r;
}
int manager_rtnl_enumerate_addresses(Manager *m) {
int r;
assert(m);
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
if (r < 0)
return r;
if (r < 0)
return r;
int k;
m->enumerating = true;
if (k < 0)
r = k;
m->enumerating = false;
}
return r;
}
int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
AddressPool *p;
int r;
assert(m);
continue;
if (r != 0)
return r;
}
return 0;
}
int n, i;
assert(m);
/* Looks for a suitable "uplink", via black magic: an
* interface that is up and where the default route with the
* highest priority points to. */
if (n < 0) {
log_warning_errno(n, "Failed to determine list of default gateways: %m");
return NULL;
}
for (i = 0; i < n; i++) {
if (!link) {
log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
continue;
}
continue;
continue;
return link;
}
return NULL;
}
/* the serialized state in /run is no longer up-to-date */
}