link-config.c revision 74df0fca09b3c31ed19e14ba80f996fdff772417
/*-*- 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 "net-util.h"
struct link_config_ctx {
int ethtool_fd;
bool enable_name_policy;
char **link_dirs;
};
if (!ret)
return -EINVAL;
if (!ctx)
return -ENOMEM;
ctx->enable_name_policy = true;
#ifdef HAVE_SPLIT_USR
#endif
NULL);
log_error("failed to build link config directory array");
return -ENOMEM;
}
log_error("failed to canonicalize link config directories\n");
return -ENOMEM;
}
return 0;
}
int r;
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
if (!ctx)
return;
}
}
if (!ctx)
return;
if (ctx->ethtool_fd >= 0)
return;
}
int r;
if (!file) {
return 0;
else
return errno;
}
if (!link) {
r = log_oom();
goto failure;
}
(void*) link_config_gperf_lookup, false, false, link);
if (r < 0) {
goto failure;
} else
return 0;
return r;
}
static bool enable_name_policy(void) {
_cleanup_free_ char *line;
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;
char **files, **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;
}
const char *s;
unsigned type;
int r;
if (!s)
return true; /* if we don't know, assume it is permanent */
if (r < 0)
return true;
/* check for NET_ADDR_PERM */
return type == 0;
}
unsigned int seed;
int r, i;
if (want_random)
else {
const char *name;
char machineid_buf[33];
const char *seed_str;
/* fetch some persistent data unique (on this machine) to this device */
if (!name) {
if (!name) {
if (!name)
return -ENOENT;
}
}
/* fetch some persistent data unique to this machine */
r = sd_id128_get_machine(&machine);
if (r < 0)
return r;
/* combine the data */
/* hash to get seed */
}
for(i = 0; i < ETH_ALEN; i++) {
}
/* 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 (config->description) {
if (r < 0)
log_warning("Could not set description of %s to '%s': %s",
}
if (r < 0)
log_warning("Could not set speed or duplex of %s to %u Mbytes (%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_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_permanent(device)) {
if (r < 0)
return r;
mac = &generated_mac;
}
break;
case MACPOLICY_RANDOM:
if (!mac_is_random(device)) {
if (r < 0)
return r;
mac = &generated_mac;
}
break;
default:
}
if (r < 0) {
return r;
}
return 0;
}
static const char* const mac_policy_table[] = {
[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_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");