2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-05-15 17:43:14 +02:00
|
|
|
#include <net/if.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2013-11-24 23:37:56 +01:00
|
|
|
#include "conf-files.h"
|
|
|
|
#include "conf-parser.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2013-11-24 23:37:56 +01:00
|
|
|
#include "list.h"
|
2015-08-27 13:59:06 +02:00
|
|
|
#include "netlink-util.h"
|
|
|
|
#include "network-internal.h"
|
2016-11-13 02:01:19 +01:00
|
|
|
#include "netdev/netdev.h"
|
2016-11-13 04:59:06 +01:00
|
|
|
#include "networkd-manager.h"
|
2017-04-30 13:12:32 +02:00
|
|
|
#include "networkd-link.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "siphash24.h"
|
2015-10-26 22:01:44 +01:00
|
|
|
#include "stat-util.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "string-table.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-08-22 07:30:49 +02:00
|
|
|
#include "strv.h"
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2016-11-13 04:59:06 +01:00
|
|
|
#include "netdev/bridge.h"
|
|
|
|
#include "netdev/bond.h"
|
2017-04-25 06:15:05 +02:00
|
|
|
#include "netdev/geneve.h"
|
2016-11-13 04:59:06 +01:00
|
|
|
#include "netdev/vlan.h"
|
|
|
|
#include "netdev/macvlan.h"
|
|
|
|
#include "netdev/ipvlan.h"
|
|
|
|
#include "netdev/vxlan.h"
|
|
|
|
#include "netdev/tunnel.h"
|
|
|
|
#include "netdev/tuntap.h"
|
|
|
|
#include "netdev/veth.h"
|
|
|
|
#include "netdev/dummy.h"
|
|
|
|
#include "netdev/vrf.h"
|
|
|
|
#include "netdev/vcan.h"
|
2017-11-22 08:23:22 +01:00
|
|
|
#include "netdev/vxcan.h"
|
2017-12-18 15:20:34 +01:00
|
|
|
#include "netdev/wireguard.h"
|
2018-06-02 15:06:33 +02:00
|
|
|
#include "netdev/netdevsim.h"
|
2018-06-03 09:07:41 +02:00
|
|
|
#include "netdev/fou-tunnel.h"
|
2016-11-13 04:59:06 +01:00
|
|
|
|
2014-07-06 14:07:34 +02:00
|
|
|
const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX] = {
|
|
|
|
[NETDEV_KIND_BRIDGE] = &bridge_vtable,
|
|
|
|
[NETDEV_KIND_BOND] = &bond_vtable,
|
|
|
|
[NETDEV_KIND_VLAN] = &vlan_vtable,
|
|
|
|
[NETDEV_KIND_MACVLAN] = &macvlan_vtable,
|
2015-07-27 07:48:28 +02:00
|
|
|
[NETDEV_KIND_MACVTAP] = &macvtap_vtable,
|
2015-01-19 22:24:32 +01:00
|
|
|
[NETDEV_KIND_IPVLAN] = &ipvlan_vtable,
|
2014-07-06 14:07:34 +02:00
|
|
|
[NETDEV_KIND_VXLAN] = &vxlan_vtable,
|
|
|
|
[NETDEV_KIND_IPIP] = &ipip_vtable,
|
|
|
|
[NETDEV_KIND_GRE] = &gre_vtable,
|
2014-12-20 09:05:06 +01:00
|
|
|
[NETDEV_KIND_GRETAP] = &gretap_vtable,
|
2015-01-18 18:54:24 +01:00
|
|
|
[NETDEV_KIND_IP6GRE] = &ip6gre_vtable,
|
|
|
|
[NETDEV_KIND_IP6GRETAP] = &ip6gretap_vtable,
|
2014-07-06 14:07:34 +02:00
|
|
|
[NETDEV_KIND_SIT] = &sit_vtable,
|
|
|
|
[NETDEV_KIND_VTI] = &vti_vtable,
|
2015-04-22 10:44:55 +02:00
|
|
|
[NETDEV_KIND_VTI6] = &vti6_vtable,
|
2014-07-06 14:07:34 +02:00
|
|
|
[NETDEV_KIND_VETH] = &veth_vtable,
|
|
|
|
[NETDEV_KIND_DUMMY] = &dummy_vtable,
|
|
|
|
[NETDEV_KIND_TUN] = &tun_vtable,
|
|
|
|
[NETDEV_KIND_TAP] = &tap_vtable,
|
2015-01-16 20:09:10 +01:00
|
|
|
[NETDEV_KIND_IP6TNL] = &ip6tnl_vtable,
|
2016-06-10 01:57:51 +02:00
|
|
|
[NETDEV_KIND_VRF] = &vrf_vtable,
|
2016-09-14 18:15:16 +02:00
|
|
|
[NETDEV_KIND_VCAN] = &vcan_vtable,
|
2017-04-25 06:15:05 +02:00
|
|
|
[NETDEV_KIND_GENEVE] = &geneve_vtable,
|
2017-11-22 08:23:22 +01:00
|
|
|
[NETDEV_KIND_VXCAN] = &vxcan_vtable,
|
2017-12-18 15:20:34 +01:00
|
|
|
[NETDEV_KIND_WIREGUARD] = &wireguard_vtable,
|
2018-06-02 15:06:33 +02:00
|
|
|
[NETDEV_KIND_NETDEVSIM] = &netdevsim_vtable,
|
2018-06-03 09:07:41 +02:00
|
|
|
[NETDEV_KIND_FOU] = &foutnl_vtable,
|
2014-07-06 14:07:34 +02:00
|
|
|
};
|
|
|
|
|
2014-03-07 21:38:48 +01:00
|
|
|
static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = {
|
2014-01-21 21:58:08 +01:00
|
|
|
[NETDEV_KIND_BRIDGE] = "bridge",
|
2014-01-22 14:49:24 +01:00
|
|
|
[NETDEV_KIND_BOND] = "bond",
|
|
|
|
[NETDEV_KIND_VLAN] = "vlan",
|
2014-02-25 21:16:17 +01:00
|
|
|
[NETDEV_KIND_MACVLAN] = "macvlan",
|
2015-07-27 07:48:28 +02:00
|
|
|
[NETDEV_KIND_MACVTAP] = "macvtap",
|
2015-01-19 22:24:32 +01:00
|
|
|
[NETDEV_KIND_IPVLAN] = "ipvlan",
|
2014-06-16 08:24:33 +02:00
|
|
|
[NETDEV_KIND_VXLAN] = "vxlan",
|
2014-05-12 07:18:24 +02:00
|
|
|
[NETDEV_KIND_IPIP] = "ipip",
|
|
|
|
[NETDEV_KIND_GRE] = "gre",
|
2014-12-20 09:05:06 +01:00
|
|
|
[NETDEV_KIND_GRETAP] = "gretap",
|
2015-01-18 18:54:24 +01:00
|
|
|
[NETDEV_KIND_IP6GRE] = "ip6gre",
|
|
|
|
[NETDEV_KIND_IP6GRETAP] = "ip6gretap",
|
2014-05-12 07:18:24 +02:00
|
|
|
[NETDEV_KIND_SIT] = "sit",
|
2014-05-21 10:31:04 +02:00
|
|
|
[NETDEV_KIND_VETH] = "veth",
|
2014-07-01 19:45:37 +02:00
|
|
|
[NETDEV_KIND_VTI] = "vti",
|
2015-04-22 10:44:55 +02:00
|
|
|
[NETDEV_KIND_VTI6] = "vti6",
|
2014-07-01 19:45:37 +02:00
|
|
|
[NETDEV_KIND_DUMMY] = "dummy",
|
2014-07-03 10:04:11 +02:00
|
|
|
[NETDEV_KIND_TUN] = "tun",
|
|
|
|
[NETDEV_KIND_TAP] = "tap",
|
2015-01-16 20:09:10 +01:00
|
|
|
[NETDEV_KIND_IP6TNL] = "ip6tnl",
|
2016-06-10 01:57:51 +02:00
|
|
|
[NETDEV_KIND_VRF] = "vrf",
|
2016-09-14 18:15:16 +02:00
|
|
|
[NETDEV_KIND_VCAN] = "vcan",
|
2017-04-25 06:15:05 +02:00
|
|
|
[NETDEV_KIND_GENEVE] = "geneve",
|
2017-11-22 08:23:22 +01:00
|
|
|
[NETDEV_KIND_VXCAN] = "vxcan",
|
2017-12-18 15:20:34 +01:00
|
|
|
[NETDEV_KIND_WIREGUARD] = "wireguard",
|
2018-06-02 15:06:33 +02:00
|
|
|
[NETDEV_KIND_NETDEVSIM] = "netdevsim",
|
2018-06-03 09:07:41 +02:00
|
|
|
[NETDEV_KIND_FOU] = "fou",
|
2014-01-21 21:58:08 +01:00
|
|
|
};
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-02-07 17:03:23 +01:00
|
|
|
DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
|
|
|
|
DEFINE_CONFIG_PARSE_ENUM(config_parse_netdev_kind, netdev_kind, NetDevKind, "Failed to parse netdev kind");
|
2014-01-21 21:58:08 +01:00
|
|
|
|
2014-05-08 20:40:56 +02:00
|
|
|
static void netdev_cancel_callbacks(NetDev *netdev) {
|
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 *m = NULL;
|
2014-07-05 14:53:54 +02:00
|
|
|
netdev_join_callback *callback;
|
2014-01-21 21:58:08 +01:00
|
|
|
|
2018-01-09 14:01:05 +01:00
|
|
|
if (!netdev || !netdev->manager)
|
2013-11-24 23:37:56 +01:00
|
|
|
return;
|
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
rtnl_message_new_synthetic_error(netdev->manager->rtnl, -ENODEV, 0, &m);
|
2014-05-08 20:40:56 +02:00
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
while ((callback = netdev->callbacks)) {
|
2014-05-08 20:40:56 +02:00
|
|
|
if (m) {
|
|
|
|
assert(callback->link);
|
|
|
|
assert(callback->callback);
|
|
|
|
assert(netdev->manager);
|
|
|
|
assert(netdev->manager->rtnl);
|
|
|
|
|
2015-06-29 14:23:17 +02:00
|
|
|
callback->callback(netdev->manager->rtnl, m, callback->link);
|
2014-05-08 20:40:56 +02:00
|
|
|
}
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
LIST_REMOVE(callbacks, netdev->callbacks, callback);
|
2015-06-29 14:24:40 +02:00
|
|
|
link_unref(callback->link);
|
2013-11-24 23:37:56 +01:00
|
|
|
free(callback);
|
|
|
|
}
|
2014-05-08 20:40:56 +02:00
|
|
|
}
|
|
|
|
|
2018-08-27 07:01:46 +02:00
|
|
|
static NetDev *netdev_free(NetDev *netdev) {
|
|
|
|
assert(netdev);
|
2014-05-08 20:40:56 +02:00
|
|
|
|
|
|
|
netdev_cancel_callbacks(netdev);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2017-12-20 10:17:37 +01:00
|
|
|
if (netdev->ifname && netdev->manager)
|
2014-05-15 15:48:37 +02:00
|
|
|
hashmap_remove(netdev->manager->netdevs, netdev->ifname);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
free(netdev->filename);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
free(netdev->description);
|
2014-05-15 15:48:37 +02:00
|
|
|
free(netdev->ifname);
|
2014-06-14 15:38:35 +02:00
|
|
|
free(netdev->mac);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-03-24 23:45:33 +01:00
|
|
|
condition_free_list(netdev->match_host);
|
|
|
|
condition_free_list(netdev->match_virt);
|
core,udev,networkd: add ConditionKernelVersion=
This adds a simple condition/assert/match to the service manager, to
udev's .link handling and to networkd, for matching the kernel version
string.
In this version we only do fnmatch() based globbing, but we might want
to extend that to version comparisons later on, if we like, by slightly
extending the syntax with ">=", "<=", ">", "<" and "==" expressions.
2017-12-13 20:34:13 +01:00
|
|
|
condition_free_list(netdev->match_kernel_cmdline);
|
|
|
|
condition_free_list(netdev->match_kernel_version);
|
2014-03-24 23:45:33 +01:00
|
|
|
condition_free_list(netdev->match_arch);
|
|
|
|
|
2018-01-04 11:36:58 +01:00
|
|
|
/* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
|
|
|
|
* because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
|
|
|
|
* allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
|
|
|
|
* comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in
|
|
|
|
* the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we
|
|
|
|
* should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw"
|
|
|
|
* call. */
|
|
|
|
if (netdev->state != _NETDEV_STATE_INVALID &&
|
|
|
|
NETDEV_VTABLE(netdev) &&
|
2014-07-16 13:17:10 +02:00
|
|
|
NETDEV_VTABLE(netdev)->done)
|
|
|
|
NETDEV_VTABLE(netdev)->done(netdev);
|
|
|
|
|
2018-08-27 07:01:46 +02:00
|
|
|
return mfree(netdev);
|
2014-05-08 18:54:26 +02:00
|
|
|
}
|
|
|
|
|
2018-08-27 07:01:46 +02:00
|
|
|
DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
|
2014-05-08 18:54:26 +02:00
|
|
|
|
2014-05-08 20:42:22 +02:00
|
|
|
void netdev_drop(NetDev *netdev) {
|
|
|
|
if (!netdev || netdev->state == NETDEV_STATE_LINGER)
|
|
|
|
return;
|
|
|
|
|
|
|
|
netdev->state = NETDEV_STATE_LINGER;
|
|
|
|
|
2014-11-27 20:20:23 +01:00
|
|
|
log_netdev_debug(netdev, "netdev removed");
|
2014-05-08 20:50:05 +02:00
|
|
|
|
2014-05-08 20:42:22 +02:00
|
|
|
netdev_cancel_callbacks(netdev);
|
|
|
|
|
|
|
|
netdev_unref(netdev);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 17:03:23 +01:00
|
|
|
int netdev_get(Manager *manager, const char *name, NetDev **ret) {
|
|
|
|
NetDev *netdev;
|
2013-11-24 23:37:56 +01:00
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
assert(name);
|
|
|
|
assert(ret);
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
netdev = hashmap_get(manager->netdevs, name);
|
|
|
|
if (!netdev) {
|
2013-11-24 23:37:56 +01:00
|
|
|
*ret = NULL;
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
*ret = netdev;
|
2013-11-24 23:37:56 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-07 17:03:23 +01:00
|
|
|
static int netdev_enter_failed(NetDev *netdev) {
|
2014-01-21 21:58:08 +01:00
|
|
|
netdev->state = NETDEV_STATE_FAILED;
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2015-06-29 14:24:40 +02:00
|
|
|
netdev_cancel_callbacks(netdev);
|
|
|
|
|
2013-11-24 23:37:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
static int netdev_enslave_ready(NetDev *netdev, Link* link, sd_netlink_message_handler_t callback) {
|
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;
|
2013-11-24 23:37:56 +01:00
|
|
|
int r;
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
assert(netdev);
|
|
|
|
assert(netdev->state == NETDEV_STATE_READY);
|
2014-02-18 00:10:08 +01:00
|
|
|
assert(netdev->manager);
|
|
|
|
assert(netdev->manager->rtnl);
|
2016-06-13 01:05:49 +02:00
|
|
|
assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF));
|
2013-11-24 23:37:56 +01:00
|
|
|
assert(link);
|
|
|
|
assert(callback);
|
|
|
|
|
2018-04-20 16:30:40 +02:00
|
|
|
if (link->flags & IFF_UP && netdev->kind == NETDEV_KIND_BOND) {
|
2017-04-30 13:12:32 +02:00
|
|
|
log_netdev_debug(netdev, "Link '%s' was up when attempting to enslave it. Bringing link down.", link->ifname);
|
|
|
|
r = link_down(link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not bring link down: %m");
|
|
|
|
}
|
|
|
|
|
2015-04-21 17:40:18 +02:00
|
|
|
r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
|
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m");
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_append_u32(req, IFLA_MASTER, netdev->ifindex);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_MASTER attribute: %m");
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_call_async(netdev->manager->rtnl, req, callback, link, 0, NULL);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
2018-06-05 20:18:47 +02:00
|
|
|
return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-07-03 09:43:31 +02:00
|
|
|
link_ref(link);
|
|
|
|
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_debug(netdev, "Enslaving link '%s'", link->ifname);
|
2014-01-12 15:24:11 +01:00
|
|
|
|
2013-11-24 23:37:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-07 17:03:23 +01:00
|
|
|
static int netdev_enter_ready(NetDev *netdev) {
|
2014-07-05 14:53:54 +02:00
|
|
|
netdev_join_callback *callback, *callback_next;
|
2014-07-03 09:55:59 +02:00
|
|
|
int r;
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
assert(netdev);
|
2014-05-15 15:48:37 +02:00
|
|
|
assert(netdev->ifname);
|
2014-01-11 21:20:14 +01:00
|
|
|
|
2014-03-28 18:20:31 +01:00
|
|
|
if (netdev->state != NETDEV_STATE_CREATING)
|
|
|
|
return 0;
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
netdev->state = NETDEV_STATE_READY;
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2015-04-21 01:26:59 +02:00
|
|
|
log_netdev_info(netdev, "netdev ready");
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-07-03 09:55:59 +02:00
|
|
|
LIST_FOREACH_SAFE(callbacks, callback, callback_next, netdev->callbacks) {
|
2014-05-08 19:46:06 +02:00
|
|
|
/* enslave the links that were attempted to be enslaved before the
|
2013-11-24 23:37:56 +01:00
|
|
|
* link was ready */
|
2014-07-03 09:55:59 +02:00
|
|
|
r = netdev_enslave_ready(netdev, callback->link, callback->callback);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
LIST_REMOVE(callbacks, netdev->callbacks, callback);
|
|
|
|
link_unref(callback->link);
|
|
|
|
free(callback);
|
2013-11-24 23:37:56 +01:00
|
|
|
}
|
|
|
|
|
2015-10-05 06:08:00 +02:00
|
|
|
if (NETDEV_VTABLE(netdev)->post_create)
|
|
|
|
NETDEV_VTABLE(netdev)->post_create(netdev, NULL, NULL);
|
|
|
|
|
2013-11-24 23:37:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-30 07:05:49 +02:00
|
|
|
|
2014-07-01 12:51:08 +02:00
|
|
|
/* callback for netdev's created without a backing Link */
|
2015-06-12 16:31:33 +02:00
|
|
|
static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(netdev_unrefp) NetDev *netdev = userdata;
|
2014-02-01 16:27:41 +01:00
|
|
|
int r;
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
assert(netdev->state != _NETDEV_STATE_INVALID);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
2014-03-15 20:03:34 +01:00
|
|
|
if (r == -EEXIST)
|
2015-04-21 01:26:59 +02:00
|
|
|
log_netdev_info(netdev, "netdev exists, using existing without changing its parameters");
|
2014-04-15 14:21:44 +02:00
|
|
|
else if (r < 0) {
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_warning_errno(netdev, r, "netdev could not be created: %m");
|
2014-05-08 21:08:12 +02:00
|
|
|
netdev_drop(netdev);
|
2013-12-03 18:48:20 +01:00
|
|
|
|
|
|
|
return 1;
|
2013-11-24 23:37:56 +01:00
|
|
|
}
|
|
|
|
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_debug(netdev, "Created");
|
2014-07-16 13:17:10 +02:00
|
|
|
|
2013-12-03 18:48:20 +01:00
|
|
|
return 1;
|
2013-11-24 23:37:56 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int netdev_enslave(NetDev *netdev, Link *link, sd_netlink_message_handler_t callback) {
|
2014-05-08 19:46:06 +02:00
|
|
|
int r;
|
|
|
|
|
2014-07-05 14:53:54 +02:00
|
|
|
assert(netdev);
|
2015-06-29 14:24:40 +02:00
|
|
|
assert(netdev->manager);
|
|
|
|
assert(netdev->manager->rtnl);
|
2016-06-13 01:05:49 +02:00
|
|
|
assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF));
|
2014-05-12 07:18:24 +02:00
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
if (netdev->state == NETDEV_STATE_READY) {
|
2014-05-08 19:46:06 +02:00
|
|
|
r = netdev_enslave_ready(netdev, link, callback);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2015-06-29 14:24:40 +02:00
|
|
|
} else if (IN_SET(netdev->state, NETDEV_STATE_LINGER, NETDEV_STATE_FAILED)) {
|
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 *m = NULL;
|
2015-06-29 14:24:40 +02:00
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
r = rtnl_message_new_synthetic_error(netdev->manager->rtnl, -ENODEV, 0, &m);
|
2015-06-29 14:24:40 +02:00
|
|
|
if (r >= 0)
|
|
|
|
callback(netdev->manager->rtnl, m, link);
|
2013-11-24 23:37:56 +01:00
|
|
|
} else {
|
2014-12-10 20:00:04 +01:00
|
|
|
/* the netdev is not yet read, save this request for when it is */
|
2014-07-05 14:53:54 +02:00
|
|
|
netdev_join_callback *cb;
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-07-05 14:53:54 +02:00
|
|
|
cb = new0(netdev_join_callback, 1);
|
2013-11-24 23:37:56 +01:00
|
|
|
if (!cb)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
cb->callback = callback;
|
|
|
|
cb->link = link;
|
2014-07-03 09:43:31 +02:00
|
|
|
link_ref(link);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
LIST_PREPEND(callbacks, netdev->callbacks, cb);
|
2014-07-06 14:07:34 +02:00
|
|
|
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_debug(netdev, "Will enslave '%s', when ready", link->ifname);
|
2014-07-16 13:17:10 +02:00
|
|
|
}
|
2014-07-05 14:53:54 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
|
2014-03-24 21:50:16 +01:00
|
|
|
uint16_t type;
|
2014-03-24 00:07:46 +01:00
|
|
|
const char *kind;
|
2014-07-18 02:35:16 +02:00
|
|
|
const char *received_kind;
|
|
|
|
const char *received_name;
|
2014-03-24 00:07:46 +01:00
|
|
|
int r, ifindex;
|
|
|
|
|
2014-01-29 21:24:44 +01:00
|
|
|
assert(netdev);
|
2014-03-24 21:50:16 +01:00
|
|
|
assert(message);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_type(message, &type);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not get rtnl message type: %m");
|
2014-03-24 21:50:16 +01:00
|
|
|
|
2014-03-28 18:20:31 +01:00
|
|
|
if (type != RTM_NEWLINK) {
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_error(netdev, "Cannot set ifindex from unexpected rtnl message type.");
|
2014-03-24 21:50:16 +01:00
|
|
|
return -EINVAL;
|
2014-03-28 18:20:31 +01:00
|
|
|
}
|
2014-03-24 00:07:46 +01:00
|
|
|
|
2014-04-19 17:14:36 +02:00
|
|
|
r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
|
|
|
|
if (r < 0) {
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_error_errno(netdev, r, "Could not get ifindex: %m");
|
2014-04-19 17:14:36 +02:00
|
|
|
netdev_enter_failed(netdev);
|
|
|
|
return r;
|
|
|
|
} else if (ifindex <= 0) {
|
2014-11-27 20:20:23 +01:00
|
|
|
log_netdev_error(netdev, "Got invalid ifindex: %d", ifindex);
|
2014-04-19 17:14:36 +02:00
|
|
|
netdev_enter_failed(netdev);
|
2015-04-21 17:40:18 +02:00
|
|
|
return -EINVAL;
|
2014-04-19 17:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (netdev->ifindex > 0) {
|
|
|
|
if (netdev->ifindex != ifindex) {
|
2014-11-27 20:20:23 +01:00
|
|
|
log_netdev_error(netdev, "Could not set ifindex to %d, already set to %d",
|
2014-04-19 17:14:36 +02:00
|
|
|
ifindex, netdev->ifindex);
|
|
|
|
netdev_enter_failed(netdev);
|
|
|
|
return -EEXIST;
|
|
|
|
} else
|
|
|
|
/* ifindex already set to the same for this netdev */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not get IFNAME: %m");
|
2014-04-18 10:28:42 +02:00
|
|
|
|
2014-05-15 15:48:37 +02:00
|
|
|
if (!streq(netdev->ifname, received_name)) {
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_error(netdev, "Received newlink with wrong IFNAME %s", received_name);
|
2014-04-18 10:28:42 +02:00
|
|
|
netdev_enter_failed(netdev);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not get LINKINFO: %m");
|
2014-03-24 00:07:46 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_read_string(message, IFLA_INFO_KIND, &received_kind);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not get KIND: %m");
|
2014-03-24 00:07:46 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_exit_container(message);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not exit container: %m");
|
2014-04-15 14:21:44 +02:00
|
|
|
|
2014-07-03 10:04:11 +02:00
|
|
|
if (netdev->kind == NETDEV_KIND_TAP)
|
|
|
|
/* the kernel does not distinguish between tun and tap */
|
|
|
|
kind = "tun";
|
|
|
|
else {
|
|
|
|
kind = netdev_kind_to_string(netdev->kind);
|
|
|
|
if (!kind) {
|
2014-11-27 20:20:23 +01:00
|
|
|
log_netdev_error(netdev, "Could not get kind");
|
2014-07-03 10:04:11 +02:00
|
|
|
netdev_enter_failed(netdev);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-03-24 21:50:16 +01:00
|
|
|
}
|
|
|
|
|
2014-03-24 00:07:46 +01:00
|
|
|
if (!streq(kind, received_kind)) {
|
2014-11-27 20:20:23 +01:00
|
|
|
log_netdev_error(netdev,
|
2014-07-03 10:04:11 +02:00
|
|
|
"Received newlink with wrong KIND %s, "
|
2014-04-18 10:28:42 +02:00
|
|
|
"expected %s", received_kind, kind);
|
2014-03-24 00:07:46 +01:00
|
|
|
netdev_enter_failed(netdev);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-01-29 21:24:44 +01:00
|
|
|
netdev->ifindex = ifindex;
|
2014-01-21 21:58:08 +01:00
|
|
|
|
2014-11-27 20:20:23 +01:00
|
|
|
log_netdev_debug(netdev, "netdev has index %d", netdev->ifindex);
|
2014-05-15 15:54:28 +02:00
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
netdev_enter_ready(netdev);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-14 15:48:16 +02:00
|
|
|
#define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
|
|
|
|
|
2014-07-06 14:07:34 +02:00
|
|
|
int netdev_get_mac(const char *ifname, struct ether_addr **ret) {
|
2014-06-14 15:48:16 +02:00
|
|
|
_cleanup_free_ struct ether_addr *mac = NULL;
|
2015-11-16 09:21:20 +01:00
|
|
|
uint64_t result;
|
2014-06-14 15:48:16 +02:00
|
|
|
size_t l, sz;
|
|
|
|
uint8_t *v;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(ifname);
|
|
|
|
assert(ret);
|
|
|
|
|
|
|
|
mac = new0(struct ether_addr, 1);
|
|
|
|
if (!mac)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
l = strlen(ifname);
|
|
|
|
sz = sizeof(sd_id128_t) + l;
|
|
|
|
v = alloca(sz);
|
|
|
|
|
|
|
|
/* fetch some persistent data unique to the machine */
|
|
|
|
r = sd_id128_get_machine((sd_id128_t*) v);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* combine with some data unique (on this machine) to this
|
|
|
|
* netdev */
|
|
|
|
memcpy(v + sizeof(sd_id128_t), ifname, l);
|
|
|
|
|
|
|
|
/* Let's hash the host machine ID plus the container name. We
|
|
|
|
* use a fixed, but originally randomly created hash key here. */
|
2015-11-16 23:17:52 +01:00
|
|
|
result = siphash24(v, sz, HASH_KEY.bytes);
|
2014-06-14 15:48:16 +02:00
|
|
|
|
|
|
|
assert_cc(ETH_ALEN <= sizeof(result));
|
2015-11-16 09:21:20 +01:00
|
|
|
memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
|
2014-06-14 15:48:16 +02:00
|
|
|
|
|
|
|
/* see eth_random_addr in the kernel */
|
|
|
|
mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
|
|
|
|
mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*ret = TAKE_PTR(mac);
|
2014-06-14 15:48:16 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-19 17:51:50 +02:00
|
|
|
static int netdev_create(NetDev *netdev, Link *link,
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message_handler_t callback) {
|
2014-07-16 13:17:10 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(netdev);
|
2014-08-19 17:51:50 +02:00
|
|
|
assert(!link || callback);
|
2014-07-16 13:17:10 +02:00
|
|
|
|
|
|
|
/* create netdev */
|
|
|
|
if (NETDEV_VTABLE(netdev)->create) {
|
2014-08-19 17:51:50 +02:00
|
|
|
assert(!link);
|
|
|
|
|
2014-07-16 13:17:10 +02:00
|
|
|
r = NETDEV_VTABLE(netdev)->create(netdev);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_debug(netdev, "Created");
|
2014-07-16 13:17:10 +02:00
|
|
|
} else {
|
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 *m = NULL;
|
2014-07-16 13:17:10 +02:00
|
|
|
|
|
|
|
r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not allocate RTM_NEWLINK message: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_append_string(m, IFLA_IFNAME, netdev->ifname);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_IFNAME, attribute: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
|
|
|
|
if (netdev->mac) {
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (netdev->mtu) {
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_MTU attribute: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (link) {
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINK attribute: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
|
|
|
|
if (NETDEV_VTABLE(netdev)->fill_message_create) {
|
|
|
|
r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_close_container(m);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
2016-09-15 06:48:59 +02:00
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_close_container(m);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
|
2014-08-19 17:51:50 +02:00
|
|
|
if (link) {
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_call_async(netdev->manager->rtnl, m, callback, link, 0, NULL);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
|
2014-07-16 13:17:10 +02:00
|
|
|
|
2014-08-19 17:51:50 +02:00
|
|
|
link_ref(link);
|
|
|
|
} else {
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_call_async(netdev->manager->rtnl, m, netdev_create_handler, netdev, 0, NULL);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
|
2014-08-19 17:51:50 +02:00
|
|
|
|
|
|
|
netdev_ref(netdev);
|
|
|
|
}
|
2014-07-16 13:17:10 +02:00
|
|
|
|
|
|
|
netdev->state = NETDEV_STATE_CREATING;
|
|
|
|
|
2015-04-21 17:40:18 +02:00
|
|
|
log_netdev_debug(netdev, "Creating");
|
2014-07-16 13:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the callback must be called, possibly after a timeout, as otherwise the Link will hang */
|
2015-06-12 16:31:33 +02:00
|
|
|
int netdev_join(NetDev *netdev, Link *link, sd_netlink_message_handler_t callback) {
|
2014-07-16 13:17:10 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(netdev);
|
|
|
|
assert(netdev->manager);
|
|
|
|
assert(netdev->manager->rtnl);
|
|
|
|
assert(NETDEV_VTABLE(netdev));
|
|
|
|
|
|
|
|
switch (NETDEV_VTABLE(netdev)->create_type) {
|
|
|
|
case NETDEV_CREATE_MASTER:
|
|
|
|
r = netdev_enslave(netdev, link, callback);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETDEV_CREATE_STACKED:
|
2014-08-19 17:51:50 +02:00
|
|
|
r = netdev_create(netdev, link, callback);
|
2014-07-16 13:17:10 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert_not_reached("Can not join independent netdev");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
static int netdev_load_one(Manager *manager, const char *filename) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(netdev_unrefp) NetDev *netdev_raw = NULL, *netdev = NULL;
|
2013-11-24 23:37:56 +01:00
|
|
|
_cleanup_fclose_ FILE *file = NULL;
|
2016-09-10 15:32:19 +02:00
|
|
|
const char *dropin_dirname;
|
2017-08-31 18:51:03 +02:00
|
|
|
bool independent = false;
|
2013-11-24 23:37:56 +01:00
|
|
|
int r;
|
|
|
|
|
2013-12-16 18:55:59 +01:00
|
|
|
assert(manager);
|
|
|
|
assert(filename);
|
|
|
|
|
2013-11-24 23:37:56 +01:00
|
|
|
file = fopen(filename, "re");
|
|
|
|
if (!file) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
2018-01-04 11:36:58 +01:00
|
|
|
|
|
|
|
return -errno;
|
2013-11-24 23:37:56 +01:00
|
|
|
}
|
|
|
|
|
2014-07-17 00:59:49 +02:00
|
|
|
if (null_or_empty_fd(fileno(file))) {
|
|
|
|
log_debug("Skipping empty file: %s", filename);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-16 13:17:10 +02:00
|
|
|
netdev_raw = new0(NetDev, 1);
|
|
|
|
if (!netdev_raw)
|
2013-11-24 23:37:56 +01:00
|
|
|
return log_oom();
|
|
|
|
|
2017-12-20 10:17:37 +01:00
|
|
|
netdev_raw->n_ref = 1;
|
2014-07-16 13:17:10 +02:00
|
|
|
netdev_raw->kind = _NETDEV_KIND_INVALID;
|
2018-01-04 11:36:58 +01:00
|
|
|
netdev_raw->state = _NETDEV_STATE_INVALID; /* an invalid state means done() of the implementation won't be called on destruction */
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2017-12-20 10:17:37 +01:00
|
|
|
dropin_dirname = strjoina(basename(filename), ".d");
|
2016-09-10 15:32:19 +02:00
|
|
|
r = config_parse_many(filename, network_dirs, dropin_dirname,
|
|
|
|
"Match\0NetDev\0",
|
|
|
|
config_item_perf_lookup, network_netdev_gperf_lookup,
|
2018-01-09 14:32:44 +01:00
|
|
|
CONFIG_PARSE_WARN|CONFIG_PARSE_RELAXED, netdev_raw);
|
2014-07-17 00:27:12 +02:00
|
|
|
if (r < 0)
|
2013-11-24 23:37:56 +01:00
|
|
|
return r;
|
|
|
|
|
2014-07-04 17:03:37 +02:00
|
|
|
/* skip out early if configuration does not match the environment */
|
|
|
|
if (net_match_config(NULL, NULL, NULL, NULL, NULL,
|
2014-07-16 13:17:10 +02:00
|
|
|
netdev_raw->match_host, netdev_raw->match_virt,
|
core,udev,networkd: add ConditionKernelVersion=
This adds a simple condition/assert/match to the service manager, to
udev's .link handling and to networkd, for matching the kernel version
string.
In this version we only do fnmatch() based globbing, but we might want
to extend that to version comparisons later on, if we like, by slightly
extending the syntax with ">=", "<=", ">", "<" and "==" expressions.
2017-12-13 20:34:13 +01:00
|
|
|
netdev_raw->match_kernel_cmdline, netdev_raw->match_kernel_version,
|
|
|
|
netdev_raw->match_arch,
|
2014-12-05 15:56:10 +01:00
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL) <= 0)
|
2014-07-04 17:03:37 +02:00
|
|
|
return 0;
|
|
|
|
|
2016-07-15 18:50:51 +02:00
|
|
|
if (netdev_raw->kind == _NETDEV_KIND_INVALID) {
|
2016-09-10 18:44:50 +02:00
|
|
|
log_warning("NetDev has no Kind configured in %s. Ignoring", filename);
|
2014-01-21 21:58:08 +01:00
|
|
|
return 0;
|
2014-07-06 14:07:34 +02:00
|
|
|
}
|
2014-07-04 17:03:37 +02:00
|
|
|
|
2014-07-16 13:17:10 +02:00
|
|
|
if (!netdev_raw->ifname) {
|
2014-02-07 17:03:23 +01:00
|
|
|
log_warning("NetDev without Name configured in %s. Ignoring", filename);
|
2014-06-16 08:24:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-20 10:17:37 +01:00
|
|
|
r = fseek(file, 0, SEEK_SET);
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2014-07-16 13:17:10 +02:00
|
|
|
netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size);
|
|
|
|
if (!netdev)
|
|
|
|
return log_oom();
|
2014-02-25 21:16:17 +01:00
|
|
|
|
2014-07-16 13:17:10 +02:00
|
|
|
netdev->n_ref = 1;
|
|
|
|
netdev->manager = manager;
|
|
|
|
netdev->kind = netdev_raw->kind;
|
2018-01-04 11:36:58 +01:00
|
|
|
netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time, so that done() will be called on destruction */
|
2014-06-16 08:24:33 +02:00
|
|
|
|
2014-07-16 13:17:10 +02:00
|
|
|
if (NETDEV_VTABLE(netdev)->init)
|
|
|
|
NETDEV_VTABLE(netdev)->init(netdev);
|
|
|
|
|
|
|
|
r = config_parse(NULL, filename, file,
|
|
|
|
NETDEV_VTABLE(netdev)->sections,
|
|
|
|
config_item_perf_lookup, network_netdev_gperf_lookup,
|
2017-11-09 00:26:11 +01:00
|
|
|
CONFIG_PARSE_WARN, netdev);
|
2014-07-16 13:17:10 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* verify configuration */
|
|
|
|
if (NETDEV_VTABLE(netdev)->config_verify) {
|
|
|
|
r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
|
|
|
|
if (r < 0)
|
|
|
|
return 0;
|
2014-02-25 21:16:17 +01:00
|
|
|
}
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
netdev->filename = strdup(filename);
|
|
|
|
if (!netdev->filename)
|
2013-11-24 23:37:56 +01:00
|
|
|
return log_oom();
|
|
|
|
|
networkd: do not generate a mac address for vlan interfaces (#3221)
While creating a VLAN the mac address should be copied from the parent interface, so that
the VLANs inherit the MAC address of the physical interface.
Before:
```
3: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:26:c6:85:a3:c2 brd ff:ff:ff:ff:ff:ff
...
6: vlan1@wlp3s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 22:07:73:9d:43:59 brd ff:ff:ff:ff:ff:ff
7: vlan2@wlp3s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 46:30:76:33:35:d4 brd ff:ff:ff:ff:ff:ff
```
After:
```
3: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:26:c6:85:a3:c2 brd ff:ff:ff:ff:ff:ff
...
11: vlan1@wlp3s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 00:26:c6:85:a3:c2 brd ff:ff:ff:ff:ff:ff
12: vlan2@wlp3s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 00:26:c6:85:a3:c2 brd ff:ff:ff:ff:ff:ff
```
v2 of #1573, with fixed commit message.
2016-05-15 15:45:30 +02:00
|
|
|
if (!netdev->mac && netdev->kind != NETDEV_KIND_VLAN) {
|
2014-06-14 15:48:16 +02:00
|
|
|
r = netdev_get_mac(netdev->ifname, &netdev->mac);
|
2015-04-21 17:40:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to generate predictable MAC address for %s: %m", netdev->ifname);
|
2014-06-14 15:48:16 +02:00
|
|
|
}
|
|
|
|
|
2014-05-15 15:48:37 +02:00
|
|
|
r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev);
|
2013-11-24 23:37:56 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
LIST_HEAD_INIT(netdev->callbacks);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2014-11-27 20:20:23 +01:00
|
|
|
log_netdev_debug(netdev, "loaded %s", netdev_kind_to_string(netdev->kind));
|
2014-07-06 14:07:34 +02:00
|
|
|
|
2014-07-16 13:17:10 +02:00
|
|
|
switch (NETDEV_VTABLE(netdev)->create_type) {
|
|
|
|
case NETDEV_CREATE_MASTER:
|
|
|
|
case NETDEV_CREATE_INDEPENDENT:
|
2014-08-19 17:51:50 +02:00
|
|
|
r = netdev_create(netdev, NULL, NULL);
|
2014-07-01 19:45:37 +02:00
|
|
|
if (r < 0)
|
2017-08-31 18:51:03 +02:00
|
|
|
return r;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-07-01 19:45:37 +02:00
|
|
|
|
2017-08-31 18:51:03 +02:00
|
|
|
switch (netdev->kind) {
|
|
|
|
case NETDEV_KIND_IPIP:
|
|
|
|
independent = IPIP(netdev)->independent;
|
|
|
|
break;
|
|
|
|
case NETDEV_KIND_GRE:
|
|
|
|
independent = GRE(netdev)->independent;
|
|
|
|
break;
|
|
|
|
case NETDEV_KIND_GRETAP:
|
|
|
|
independent = GRETAP(netdev)->independent;
|
|
|
|
break;
|
|
|
|
case NETDEV_KIND_IP6GRE:
|
|
|
|
independent = IP6GRE(netdev)->independent;
|
|
|
|
break;
|
|
|
|
case NETDEV_KIND_IP6GRETAP:
|
|
|
|
independent = IP6GRETAP(netdev)->independent;
|
|
|
|
break;
|
|
|
|
case NETDEV_KIND_SIT:
|
|
|
|
independent = SIT(netdev)->independent;
|
|
|
|
break;
|
|
|
|
case NETDEV_KIND_VTI:
|
|
|
|
independent = VTI(netdev)->independent;
|
|
|
|
break;
|
|
|
|
case NETDEV_KIND_VTI6:
|
|
|
|
independent = VTI6(netdev)->independent;
|
|
|
|
break;
|
|
|
|
case NETDEV_KIND_IP6TNL:
|
|
|
|
independent = IP6TNL(netdev)->independent;
|
2014-07-16 13:17:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-01-22 14:49:24 +01:00
|
|
|
}
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2017-08-31 18:51:03 +02:00
|
|
|
if (independent) {
|
|
|
|
r = netdev_create(netdev, NULL, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
netdev = NULL;
|
2013-11-24 23:37:56 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
int netdev_load(Manager *manager) {
|
2015-04-21 17:40:18 +02:00
|
|
|
_cleanup_strv_free_ char **files = NULL;
|
2014-02-07 17:03:23 +01:00
|
|
|
NetDev *netdev;
|
2015-04-21 17:40:18 +02:00
|
|
|
char **f;
|
2013-11-24 23:37:56 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
|
2014-01-21 21:58:08 +01:00
|
|
|
while ((netdev = hashmap_first(manager->netdevs)))
|
2014-05-08 18:54:26 +02:00
|
|
|
netdev_unref(netdev);
|
2013-11-24 23:37:56 +01:00
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
r = conf_files_list_strv(&files, ".netdev", NULL, 0, network_dirs);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to enumerate netdev files: %m");
|
2013-11-24 23:37:56 +01:00
|
|
|
|
|
|
|
STRV_FOREACH_BACKWARDS(f, files) {
|
2014-01-21 21:58:08 +01:00
|
|
|
r = netdev_load_one(manager, *f);
|
2013-11-24 23:37:56 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|