link-config.c revision dab495dc23bf9a5ba0487a057bb594355555a0e9
843e19887f64dde75055cf8842fc4db2171eff45johnlev/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
843e19887f64dde75055cf8842fc4db2171eff45johnlev This file is part of systemd.
843e19887f64dde75055cf8842fc4db2171eff45johnlev Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
843e19887f64dde75055cf8842fc4db2171eff45johnlev systemd is free software; you can redistribute it and/or modify it
843e19887f64dde75055cf8842fc4db2171eff45johnlev under the terms of the GNU Lesser General Public License as published by
843e19887f64dde75055cf8842fc4db2171eff45johnlev the Free Software Foundation; either version 2.1 of the License, or
843e19887f64dde75055cf8842fc4db2171eff45johnlev (at your option) any later version.
843e19887f64dde75055cf8842fc4db2171eff45johnlev systemd is distributed in the hope that it will be useful, but
843e19887f64dde75055cf8842fc4db2171eff45johnlev WITHOUT ANY WARRANTY; without even the implied warranty of
843e19887f64dde75055cf8842fc4db2171eff45johnlev MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
843e19887f64dde75055cf8842fc4db2171eff45johnlev Lesser General Public License for more details.
843e19887f64dde75055cf8842fc4db2171eff45johnlev You should have received a copy of the GNU Lesser General Public License
843e19887f64dde75055cf8842fc4db2171eff45johnlev along with systemd; If not, see <http://www.gnu.org/licenses/>.
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic const char* const link_dirs[] = {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rabDEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free);
5d2eda970e48f8985448151c73e699614ce9f357John Levonstatic void link_configs_free(link_config_ctx *ctx) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
843e19887f64dde75055cf8842fc4db2171eff45johnlevDEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab _cleanup_(link_config_ctx_freep) link_config_ctx *ctx = NULL;
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rabstatic int load_link(link_config_ctx *ctx, const char *filename) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev "Match\0Link\0Ethernet\0",
843e19887f64dde75055cf8842fc4db2171eff45johnlev false, false, true, link);
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rabstatic bool enable_name_policy(void) {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab if (r < 0) {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab return true;
843e19887f64dde75055cf8842fc4db2171eff45johnlev return false;
843e19887f64dde75055cf8842fc4db2171eff45johnlev return true;
843e19887f64dde75055cf8842fc4db2171eff45johnlev log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab /* update timestamp */
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, true);
843e19887f64dde75055cf8842fc4db2171eff45johnlev r = conf_files_list_strv(&files, ".link", NULL, link_dirs);
843e19887f64dde75055cf8842fc4db2171eff45johnlev return log_error_errno(r, "failed to enumerate link files: %m");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab if (r < 0)
843e19887f64dde75055cf8842fc4db2171eff45johnlevbool link_config_should_reload(link_config_ctx *ctx) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev return paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, false);
843e19887f64dde75055cf8842fc4db2171eff45johnlevint link_config_get(link_config_ctx *ctx, struct udev_device *device,
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab attr_value = udev_device_get_sysattr_value(device, "address");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab if (net_match_config(link->match_mac, link->match_path, link->match_driver,
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab udev_device_get_property_value(device, "ID_NET_DRIVER"),
843e19887f64dde75055cf8842fc4db2171eff45johnlev attr_value = udev_device_get_sysattr_value(device, "name_assign_type");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab log_warning("Config file %s applies to device based on potentially unpredictable interface name '%s'",
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab log_warning("Config file %s matches device based on renamed interface name '%s', ignoring",
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab const char *s;
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab /* if we can't get the assign type, assume it is not random */
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab s = udev_device_get_sysattr_value(device, "addr_assign_type");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab return false;
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab if (r < 0)
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab return false;
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rabstatic bool should_rename(struct udev_device *device, bool respect_predictable) {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab const char *s;
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab /* if we can't get the assgin type, assume we should rename */
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab s = udev_device_get_sysattr_value(device, "name_assign_type");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab return true;
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab if (r < 0)
843e19887f64dde75055cf8842fc4db2171eff45johnlev return true;
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* these were already named by userspace, do not touch again */
843e19887f64dde75055cf8842fc4db2171eff45johnlev return false;
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* the kernel claims to have given a predictable name */
843e19887f64dde75055cf8842fc4db2171eff45johnlev return false;
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* fall through */
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* the name is known to be bad, or of an unknown type */
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab return true;
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic int get_mac(struct udev_device *device, bool want_random,
843e19887f64dde75055cf8842fc4db2171eff45johnlev r = net_get_unique_predictable_data(device, result);
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* see eth_random_addr in the kernel */
843e19887f64dde75055cf8842fc4db2171eff45johnlev mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
843e19887f64dde75055cf8842fc4db2171eff45johnlevint link_config_apply(link_config_ctx *ctx, link_config *config,
843e19887f64dde75055cf8842fc4db2171eff45johnlev const char *old_name;
843e19887f64dde75055cf8842fc4db2171eff45johnlev r = ethtool_set_speed(&ctx->ethtool_fd, old_name, config->speed / 1024, config->duplex);
843e19887f64dde75055cf8842fc4db2171eff45johnlev log_warning_errno(r, "Could not set speed or duplex of %s to %zu Mbps (%s): %m",
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
843e19887f64dde75055cf8842fc4db2171eff45johnlev log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab if (ifindex <= 0) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (ctx->enable_name_policy && config->name_policy) {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab switch (*policy) {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab new_name = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab /* if not set by policy, fall back manually set name */
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab if (r == -ENOENT) {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab log_warning_errno(r, "Could not generate persistent MAC address for %s: %m", old_name);
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab } else if (r < 0)
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab if (r == -ENOENT) {
a576ab5b6e08c47732b3dedca9eaa8a8cbb85720rab log_warning_errno(r, "Could not generate random MAC address for %s: %m", old_name);
5d2eda970e48f8985448151c73e699614ce9f357John Levon } else if (r < 0)
5d2eda970e48f8985448151c73e699614ce9f357John Levon r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac, config->mtu);
5d2eda970e48f8985448151c73e699614ce9f357John Levon return log_warning_errno(r, "Could not set Alias, MACAddress or MTU on %s: %m", old_name);
5d2eda970e48f8985448151c73e699614ce9f357John Levonint link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret) {
5d2eda970e48f8985448151c73e699614ce9f357John Levon const char *name;
5d2eda970e48f8985448151c73e699614ce9f357John Levon r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
5d2eda970e48f8985448151c73e699614ce9f357John Levonstatic const char* const mac_policy_table[_MACPOLICY_MAX] = {
5d2eda970e48f8985448151c73e699614ce9f357John LevonDEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
5d2eda970e48f8985448151c73e699614ce9f357John LevonDEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy,
5d2eda970e48f8985448151c73e699614ce9f357John Levon "Failed to parse MAC address policy");
5d2eda970e48f8985448151c73e699614ce9f357John Levonstatic const char* const name_policy_table[_NAMEPOLICY_MAX] = {
843e19887f64dde75055cf8842fc4db2171eff45johnlevDEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
843e19887f64dde75055cf8842fc4db2171eff45johnlev "Failed to parse interface name policy");