link-config.c revision eb7040ec50fbfe5aad9eaf305bd442a4a235abaa
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright (C) 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 <linux/netdevice.h>
#include "sd-id128.h"
#include "missing.h"
#include "link-config.h"
#include "ethtool-util.h"
#include "libudev-private.h"
#include "sd-rtnl.h"
#include "util.h"
#include "log.h"
#include "strv.h"
#include "path-util.h"
#include "conf-parser.h"
#include "conf-files.h"
#include "fileio.h"
#include "hashmap.h"
#include "rtnl-util.h"
#include "network-internal.h"
#include "siphash24.h"
struct link_config_ctx {
int ethtool_fd;
bool enable_name_policy;
};
static const char* const link_dirs[] = {
#ifdef HAVE_SPLIT_USR
#endif
NULL};
if (!ret)
return -EINVAL;
if (!ctx)
return -ENOMEM;
ctx->enable_name_policy = true;
return 0;
}
int r;
if (r < 0) {
log_warning("link_config: could not connect to ethtool: %s",
strerror(-r));
return r;
}
}
if (r < 0) {
log_warning("link_config: could not connect to rtnl: %s",
strerror(-r));
return r;
}
}
return 0;
}
if (!ctx)
return;
}
}
if (!ctx)
return;
return;
}
int r;
if (!file) {
return 0;
else
return -errno;
}
return 0;
}
if (!link)
return log_oom();
"Match\0Link\0Ethernet\0",
false, false, true, link);
if (r < 0)
return r;
else
return 0;
}
static bool enable_name_policy(void) {
int r;
size_t l;
r = proc_cmdline(&line);
if (r < 0)
log_warning("Failed to read /proc/cmdline, ignoring: %s",
strerror(-r));
if (r <= 0)
return true;
return false;
return true;
}
int r;
_cleanup_strv_free_ char **files;
char **f;
if (!enable_name_policy()) {
ctx->enable_name_policy = false;
log_info("Network interface NamePolicy= disabled on kernel commandline, ignoring.");
}
/* update timestamp */
if (r < 0) {
return r;
}
STRV_FOREACH_BACKWARDS(f, files) {
if (r < 0)
return r;
}
return 0;
}
}
link_config **ret) {
NULL)) {
log_debug("Config file %s applies to device %s",
return 0;
}
}
return -ENOENT;
}
const char *s;
unsigned type;
int r;
/* if we can't get the assign type, assume it is not random */
if (!s)
return false;
if (r < 0)
return false;
return type == NET_ADDR_RANDOM;
}
const char *s;
unsigned type;
int r;
/* if we can't get the assgin type, assume we should rename */
if (!s)
return true;
if (r < 0)
return true;
switch (type) {
case NET_NAME_USER:
case NET_NAME_RENAMED:
/* these were already named by userspace, do not touch again */
return false;
case NET_NAME_PREDICTABLE:
/* the kernel claims to have given a predictable name */
if (respect_predictable)
return false;
/* fall through */
case NET_NAME_ENUM:
default:
/* the name is known to be bad, or of an unknown type */
return true;
}
}
struct ether_addr *mac) {
int r;
if (want_random)
else {
if (r < 0)
return r;
}
/* see eth_random_addr in the kernel */
return 0;
}
const char *old_name;
struct ether_addr generated_mac;
bool respect_predictable = false;
int r, ifindex;
r = link_config_ctx_connect(ctx);
if (r < 0)
return r;
if (!old_name)
return -EINVAL;
if (r < 0)
log_warning("Could not set speed or duplex of %s to %u Mbps (%s): %s",
if (r < 0)
log_warning("Could not set WakeOnLan of %s to %s: %s",
if (ifindex <= 0) {
log_warning("Could not find ifindex");
return -ENODEV;
}
switch (*policy) {
case NAMEPOLICY_KERNEL:
respect_predictable = true;
break;
case NAMEPOLICY_DATABASE:
break;
case NAMEPOLICY_ONBOARD:
break;
case NAMEPOLICY_SLOT:
break;
case NAMEPOLICY_PATH:
break;
case NAMEPOLICY_MAC:
break;
default:
break;
}
}
}
/* if not set by policy, fall back manually set name */
if (!new_name)
} else
switch (config->mac_policy) {
case MACPOLICY_PERSISTENT:
if (mac_is_random(device)) {
if (r == -ENOENT)
break;
else if (r < 0)
return r;
mac = &generated_mac;
}
break;
case MACPOLICY_RANDOM:
if (!mac_is_random(device)) {
if (r == -ENOENT)
break;
else if (r < 0)
return r;
mac = &generated_mac;
}
break;
default:
}
if (r < 0) {
log_warning("Could not set Alias, MACAddress or MTU on %s: %s",
return r;
}
return 0;
}
const char *name;
char *driver;
int r;
r = link_config_ctx_connect(ctx);
if (r < 0)
return r;
if (!name)
return -EINVAL;
if (r < 0)
return r;
return 0;
}
static const char* const mac_policy_table[_MACPOLICY_MAX] = {
[MACPOLICY_PERSISTENT] = "persistent",
[MACPOLICY_RANDOM] = "random"
};
"Failed to parse MAC address policy");
static const char* const name_policy_table[_NAMEPOLICY_MAX] = {
[NAMEPOLICY_KERNEL] = "kernel",
[NAMEPOLICY_DATABASE] = "database",
[NAMEPOLICY_ONBOARD] = "onboard",
[NAMEPOLICY_SLOT] = "slot",
[NAMEPOLICY_PATH] = "path",
[NAMEPOLICY_MAC] = "mac"
};
"Failed to parse interface name policy");