link-config.c revision 489124365d1d391864898b9869dd668eea5b2e28
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define _cleanup_link_config_ctx_free_ _cleanup_(link_config_ctx_freep)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint link_config_ctx_new(link_config_ctx **ret) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_link_config_ctx_free_ link_config_ctx *ctx = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ctx->link_dirs = strv_new("/etc/systemd/network",
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek log_error("failed to build link config directory array");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!path_strv_canonicalize_uniq(ctx->link_dirs)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("failed to canonicalize link config directories\n");
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringstatic int link_config_ctx_connect(link_config_ctx *ctx) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = ethtool_connect(&ctx->ethtool_fd);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = sd_rtnl_open(0, &ctx->rtnl);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic void link_configs_free(link_config_ctx *ctx) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen LIST_FOREACH_SAFE(links, link, link_next, ctx->links) {
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersenvoid link_config_ctx_free(link_config_ctx *ctx) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek close_nointr_nofail(ctx->ethtool_fd);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekstatic int load_link(link_config_ctx *ctx, const char *filename) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = config_parse(NULL, filename, file, "Match\0Link\0Ethernet\0", config_item_perf_lookup,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen (void*) link_config_gperf_lookup, false, false, link);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Could not parse config file %s: %s", filename, strerror(-r));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_debug("Parsed configuration file %s", filename);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersenstatic bool enable_name_policy(void) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l == sizeof("net.ifnames=0") - 1 && strneq(w, "net.ifnames=0", l))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint link_config_load(link_config_ctx *ctx) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt log_info("Network interface NamePolicy= disabled on kernel commandline, ignoring.");
NULL)) {
return -ENOENT;
unsigned type;
unsigned type;
return type == 0;
unsigned int seed;
if (want_random)
const char *name;
const char *seed_str;
if (!name) {
if (!name) {
if (!name)
return -ENOENT;
for(i = 0; i < ETH_ALEN; i++) {
int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device, const char **name) {
const char *old_name;
int r, ifindex;
if (!old_name)
return -EINVAL;
if (ifindex <= 0) {
return -ENODEV;
switch (*policy) {
case NAMEPOLICY_ONBOARD:
case NAMEPOLICY_SLOT:
case NAMEPOLICY_PATH:
case NAMEPOLICY_MAC:
if (new_name)
case MACPOLICY_PERSISTENT:
case MACPOLICY_RANDOM:
static const char* const mac_policy_table[] = {
DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy, "Failed to parse MAC address policy");
static const char* const name_policy_table[] = {
DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, _NAMEPOLICY_INVALID, "Failed to parse interface name policy");