link-config.c revision 2ad8416dd057e7e3185169609ca3006e7649f576
/*-*- 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"
#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;
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;
}
int r;
if (want_random)
else {
const char *name;
uint8_t *v;
/* fetch some persistent data unique (on this machine) to this device */
if (!name) {
if (!name) {
if (!name)
return -ENOENT;
}
}
sz = sizeof(sd_id128_t) + l;
/* fetch some persistent data unique to this machine */
r = sd_id128_get_machine((sd_id128_t*) v);
if (r < 0)
return r;
/* Let's hash the machine ID plus the device name. We
* use a fixed, but originally randomly created hash
* key here. */
}
/* 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 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");