/***
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-netlink.h"
#include "alloc-util.h"
#include "conf-files.h"
#include "conf-parser.h"
#include "ethtool-util.h"
#include "fd-util.h"
#include "libudev-private.h"
#include "link-config.h"
#include "log.h"
#include "missing.h"
#include "netlink-util.h"
#include "network-internal.h"
#include "parse-util.h"
#include "path-util.h"
#include "proc-cmdline.h"
#include "random-util.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "util.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 (!link)
return;
}
if (!ctx)
return;
}
if (!ctx)
return;
return;
}
if (!ret)
return -EINVAL;
if (!ctx)
return -ENOMEM;
ctx->enable_name_policy = true;
return 0;
}
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 -ERANGE;
return 0;
}
static bool enable_name_policy(void) {
int r;
size_t l;
r = proc_cmdline(&line);
if (r < 0) {
log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
return true;
}
return false;
return true;
}
int r;
char **f;
if (!enable_name_policy()) {
ctx->enable_name_policy = false;
log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
}
/* update timestamp */
if (r < 0)
return log_error_errno(r, "failed to enumerate link files: %m");
if (r < 0)
return r;
}
return 0;
}
}
link_config **ret) {
const char* attr_value;
if (link->match_name) {
if (attr_value)
if (name_assign_type == NET_NAME_ENUM) {
log_warning("Config file %s applies to device based on potentially unpredictable interface name '%s'",
return 0;
} else if (name_assign_type == NET_NAME_RENAMED) {
log_warning("Config file %s matches device based on renamed interface name '%s', ignoring",
continue;
}
}
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;
bool respect_predictable = false;
int r, ifindex;
if (!old_name)
return -EINVAL;
if (r < 0)
log_warning_errno(r, "Could not set speed or duplex of %s to %zu Mbps (%s): %m",
if (r < 0)
log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
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;
case MACPOLICY_NONE:
default:
}
if (r < 0)
return 0;
}
const char *name;
int r;
if (!name)
return -EINVAL;
if (r < 0)
return r;
return 0;
}
[MACPOLICY_PERSISTENT] = "persistent",
[MACPOLICY_RANDOM] = "random",
[MACPOLICY_NONE] = "none"
};
"Failed to parse MAC address policy");
[NAMEPOLICY_KERNEL] = "kernel",
[NAMEPOLICY_DATABASE] = "database",
[NAMEPOLICY_ONBOARD] = "onboard",
[NAMEPOLICY_SLOT] = "slot",
[NAMEPOLICY_PATH] = "path",
[NAMEPOLICY_MAC] = "mac"
};
"Failed to parse interface name policy");