link-config.c revision b5db00e52ee2e20578839e4e4488f7b9af9abc38
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char* const link_dirs[] = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-SzmekDEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define _cleanup_link_config_ctx_free_ _cleanup_(link_config_ctx_freep)
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poetteringint link_config_ctx_new(link_config_ctx **ret) {
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering _cleanup_link_config_ctx_free_ link_config_ctx *ctx = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int link_config_ctx_connect(link_config_ctx *ctx) {
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek r = ethtool_connect(&ctx->ethtool_fd);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmekstatic void link_configs_free(link_config_ctx *ctx) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH_SAFE(links, link, link_next, ctx->links) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid link_config_ctx_free(link_config_ctx *ctx) {
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimerstatic int load_link(link_config_ctx *ctx, const char *filename) {
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek link->mac_policy = _MACPOLICY_INVALID;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek r = config_parse(NULL, filename, file, "Match\0Link\0Ethernet\0", config_item_perf_lookup,
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek (void*) link_config_gperf_lookup, false, false, link);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek log_warning("Could not parse config file %s: %s", filename, strerror(-r));
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek log_debug("Parsed configuration file %s", filename);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->filename = strdup(filename);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek LIST_PREPEND(links, ctx->links, link);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmekstatic bool enable_name_policy(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek FOREACH_WORD_QUOTED(w, l, line, state)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (strneq(w, "net.ifnames=0", l))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint link_config_load(link_config_ctx *ctx) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ctx->enable_name_policy = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("Network interface NamePolicy= disabled on kernel commandline, ignoring.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* update timestamp */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, true);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = conf_files_list_strv(&files, ".link", NULL, link_dirs);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("failed to enumerate link files: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek STRV_FOREACH_BACKWARDS(f, files) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekbool link_config_should_reload(link_config_ctx *ctx) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, false);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint link_config_get(link_config_ctx *ctx, struct udev_device *device, link_config **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(links, link, ctx->links) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (net_match_config(link->match_mac, link->match_path, link->match_driver,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->match_type, NULL, link->match_host,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek link->match_virt, link->match_kernel, link->match_arch,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_get_sysattr_value(device, "address"),
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek udev_device_get_property_value(device, "ID_PATH"),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_get_driver(udev_device_get_parent(device)),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_get_property_value(device, "ID_NET_DRIVER"),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_get_devtype(device),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Config file %s applies to device %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_get_sysname(device));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic bool mac_is_random(struct udev_device *device) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s = udev_device_get_sysattr_value(device, "addr_assign_type");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return false; /* if we don't know, assume it is not random */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* check for NET_ADDR_RANDOM */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic bool mac_is_permanent(struct udev_device *device) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s = udev_device_get_sysattr_value(device, "addr_assign_type");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return true; /* if we don't know, assume it is permanent */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* check for NET_ADDR_PERM */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int get_mac(struct udev_device *device, bool want_random, struct ether_addr *mac) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek random_bytes(mac->ether_addr_octet, ETH_ALEN);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = net_get_unique_predictable_data(device, result);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_cc(ETH_ALEN <= sizeof(result));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek memcpy(mac->ether_addr_octet, result, ETH_ALEN);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek /* see eth_random_addr in the kernel */
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device, const char **name) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ether_addr generated_mac;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = link_config_ctx_connect(ctx);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek old_name = udev_device_get_sysname(device);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = ethtool_set_speed(ctx->ethtool_fd, old_name, config->speed / 1024, config->duplex);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk log_warning("Could not set speed or duplex of %s to %u Mbps (%s): %s",
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk old_name, config->speed / 1024, duplex_to_string(config->duplex),
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = ethtool_set_wol(ctx->ethtool_fd, old_name, config->wol);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk log_warning("Could not set WakeOnLan of %s to %s: %s",
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk old_name, wol_to_string(config->wol), strerror(-r));
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (ctx->enable_name_policy && config->name_policy) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering for (policy = config->name_policy; !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering new_name = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *name = new_name; /* a name was set by a policy */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *name = config->name; /* a name was set manually in the config */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!mac_is_permanent(device)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = get_mac(device, false, &generated_mac);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = get_mac(device, true, &generated_mac);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = rtnl_set_link_properties(ctx->rtnl, ifindex, config->alias, mac, config->mtu);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_warning("Could not set Alias, MACAddress or MTU on %s: %s", old_name, strerror(-r));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringint link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = ethtool_get_driver(ctx->ethtool_fd, name, &driver);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic const char* const mac_policy_table[_MACPOLICY_MAX] = {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart PoetteringDEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy, "Failed to parse MAC address policy");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char* const name_policy_table[_NAMEPOLICY_MAX] = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek [NAMEPOLICY_DATABASE] = "database",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek [NAMEPOLICY_ONBOARD] = "onboard",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-SzmekDEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-SzmekDEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, _NAMEPOLICY_INVALID, "Failed to parse interface name policy");