link-config.c revision 8fcde01280adcbd07e8205b91ac52b06305b6208
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer systemd is free software; you can redistribute it and/or modify it
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer under the terms of the GNU Lesser General Public License as published by
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer the Free Software Foundation; either version 2.1 of the License, or
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt (at your option) any later version.
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt systemd is distributed in the hope that it will be useful, but
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt WITHOUT ANY WARRANTY; without even the implied warranty of
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1c36b4a73b876258fbe01fbe9bc9b750b7dcc9ceEvgeny Vereshchagin Lesser General Public License for more details.
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier You should have received a copy of the GNU Lesser General Public License
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchaginstatic const char* const link_dirs[] = {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalierstatic void link_config_free(link_config *link) {
889a90422dd47284dffa32b9234a6e58991b000cRonny ChevalierDEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void link_configs_free(link_config_ctx *ctx) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevaliervoid link_config_ctx_free(link_config_ctx *ctx) {
889a90422dd47284dffa32b9234a6e58991b000cRonny ChevalierDEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierint link_config_ctx_new(link_config_ctx **ret) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_(link_config_ctx_freep) link_config_ctx *ctx = NULL;
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchaginstatic int load_link(link_config_ctx *ctx, const char *filename) {
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin _cleanup_(link_config_freep) link_config *link = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_debug("Skipping empty file: %s", filename);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier "Match\0Link\0Ethernet\0",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier config_item_perf_lookup, link_config_gperf_lookup,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier false, false, true, link);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_debug("Parsed configuration file %s", filename);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (link->mtu > UINT_MAX || link->speed > UINT_MAX)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool enable_name_policy(void) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return false;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin /* update timestamp */
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, true);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin r = conf_files_list_strv(&files, ".link", NULL, link_dirs);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin return log_error_errno(r, "failed to enumerate link files: %m");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierbool link_config_should_reload(link_config_ctx *ctx) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, false);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierint link_config_get(link_config_ctx *ctx, struct udev_device *device,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier attr_value = udev_device_get_sysattr_value(device, "address");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (net_match_config(link->match_mac, link->match_path, link->match_driver,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->match_type, link->match_name, link->match_host,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->match_virt, link->match_kernel, link->match_arch,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier udev_device_get_property_value(device, "ID_PATH"),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier udev_device_get_driver(udev_device_get_parent(device)),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier udev_device_get_property_value(device, "ID_NET_DRIVER"),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier unsigned char name_assign_type = NET_NAME_UNKNOWN;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier attr_value = udev_device_get_sysattr_value(device, "name_assign_type");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier (void) safe_atou8(attr_value, &name_assign_type);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Config file %s applies to device based on potentially unpredictable interface name '%s'",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->filename, udev_device_get_sysname(device));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (name_assign_type == NET_NAME_RENAMED) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Config file %s matches device based on renamed interface name '%s', ignoring",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->filename, udev_device_get_sysname(device));
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt log_debug("Config file %s applies to device %s",
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt link->filename, udev_device_get_sysname(device));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool mac_is_random(struct udev_device *device) {
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt const char *s;
e63b61be5350dbe92ea12e1eeb96dde251ed9292Evgeny Vereshchagin /* if we can't get the assign type, assume it is not random */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier s = udev_device_get_sysattr_value(device, "addr_assign_type");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool should_rename(struct udev_device *device, bool respect_predictable) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *s;
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin /* if we can't get the assgin type, assume we should rename */
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin s = udev_device_get_sysattr_value(device, "name_assign_type");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* these were already named by userspace, do not touch again */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* the kernel claims to have given a predictable name */
25b47f96d9601ff566257b2a31bfb5f4bd25d661Marko Myllynen return false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* fall through */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* the name is known to be bad, or of an unknown type */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int get_mac(struct udev_device *device, bool want_random,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier random_bytes(mac->ether_addr_octet, ETH_ALEN);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = net_get_unique_predictable_data(device, result);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier memcpy(mac->ether_addr_octet, result, ETH_ALEN);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* see eth_random_addr in the kernel */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierint link_config_apply(link_config_ctx *ctx, link_config *config,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct udev_device *device, const char **name) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = ethtool_set_speed(&ctx->ethtool_fd, old_name, config->speed / 1024, config->duplex);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_errno(r, "Could not set speed or duplex of %s to %zu Mbps (%s): %m",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ctx->enable_name_policy && config->name_policy) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer new_name = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (should_rename(device, respect_predictable)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* if not set by policy, fall back manually set name */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_errno(r, "Could not generate persistent MAC address for %s: %m", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_errno(r, "Could not generate random MAC address for %s: %m", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac, config->mtu);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return log_warning_errno(r, "Could not set Alias, MACAddress or MTU on %s: %m", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic const char* const mac_policy_table[_MACPOLICY_MAX] = {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Failed to parse MAC address policy");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic const char* const name_policy_table[_NAMEPOLICY_MAX] = {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Failed to parse interface name policy");