link-config.c revision 36f822c4bd077f9121757e24b6516e5c7ada63b5
/*-*- 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 "sd-id128.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 (null_or_empty_path(filename)) {
return 0;
}
if (!file) {
return 0;
else
return -errno;
}
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) {
char *w, *state;
int r;
size_t l;
r = proc_cmdline(&line);
if (r < 0)
if (r <= 0)
return true;
if (strneq(w, "net.ifnames=0", l))
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;
}
}
NULL)) {
log_debug("Config file %s applies to device %s",
return 0;
}
}
return -ENOENT;
}
const char *s;
unsigned type;
int r;
if (!s)
return false; /* if we don't know, assume it is not random */
if (r < 0)
return false;
/* check for NET_ADDR_RANDOM */
return type == 1;
}
int r;
if (want_random)
else {
if (r < 0)
return r;
}
/* see eth_random_addr in the kernel */
return 0;
}
int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device, const char **name) {
const char *old_name;
struct ether_addr generated_mac;
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",
strerror(-r));
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_DATABASE:
break;
case NAMEPOLICY_ONBOARD:
break;
case NAMEPOLICY_SLOT:
break;
case NAMEPOLICY_PATH:
break;
case NAMEPOLICY_MAC:
break;
default:
break;
}
}
}
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) {
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"
};
DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy, "Failed to parse MAC address policy");
static const char* const name_policy_table[_NAMEPOLICY_MAX] = {
[NAMEPOLICY_DATABASE] = "database",
[NAMEPOLICY_ONBOARD] = "onboard",
[NAMEPOLICY_SLOT] = "slot",
[NAMEPOLICY_PATH] = "path",
[NAMEPOLICY_MAC] = "mac"
};
DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, _NAMEPOLICY_INVALID, "Failed to parse interface name policy");