link-config.c revision dc75168823540076b354135f6e2de7a9a978fbca
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering/***
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering This file is part of systemd.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering systemd is free software; you can redistribute it and/or modify it
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering under the terms of the GNU Lesser General Public License as published by
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering (at your option) any later version.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering systemd is distributed in the hope that it will be useful, but
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Lesser General Public License for more details.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering You should have received a copy of the GNU Lesser General Public License
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering***/
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include <netinet/ether.h>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include <linux/netdevice.h>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "missing.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "link-config.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "ethtool-util.h"
9eb977db5b89b44f254ab40c1876a76b7d7ea2d0Kay Sievers
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "libudev-private.h"
a5c32cff1f56afe6f0c6c70d91a88a7a8238b2d7Harald Hoyer#include "sd-rtnl.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "util.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "log.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "strv.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "path-util.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "conf-parser.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "conf-files.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "rtnl-util.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "network-internal.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstruct link_config_ctx {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering LIST_HEAD(link_config, links);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering int ethtool_fd;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering bool enable_name_policy;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
2b6bf07dd23bb467099d213c97b3875c5e453491Zbigniew Jędrzejewski-Szmek sd_rtnl *rtnl;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering usec_t link_dirs_ts_usec;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering};
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic const char* const link_dirs[] = {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "/etc/systemd/network",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "/run/systemd/network",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "/usr/lib/systemd/network",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#ifdef HAVE_SPLIT_USR
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "/lib/systemd/network",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#endif
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering NULL};
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic void link_config_free(link_config *link) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!link)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(link->filename);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->match_mac);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(link->match_path);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(link->match_driver);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(link->match_type);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->match_name);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->match_host);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->match_virt);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->match_kernel);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->match_arch);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->description);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->mac);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link->name_policy);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(link->name);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(link->alias);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(link);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
d2e54fae5ca7a0f71b5ac8b356a589ff0a09ea0aKay SieversDEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic void link_configs_free(link_config_ctx *ctx) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link_config *link, *link_next;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!ctx)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link_config_free(link);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringvoid link_config_ctx_free(link_config_ctx *ctx) {
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (!ctx)
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek return;
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering safe_close(ctx->ethtool_fd);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek sd_rtnl_unref(ctx->rtnl);
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link_configs_free(ctx);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(ctx);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint link_config_ctx_new(link_config_ctx **ret) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_(link_config_ctx_freep) link_config_ctx *ctx = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (!ret)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return -EINVAL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering ctx = new0(link_config_ctx, 1);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!ctx)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return -ENOMEM;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering LIST_HEAD_INIT(ctx->links);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering ctx->ethtool_fd = -1;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering ctx->enable_name_policy = true;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering *ret = ctx;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering ctx = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic int load_link(link_config_ctx *ctx, const char *filename) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_(link_config_freep) link_config *link = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_fclose_ FILE *file = NULL;
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek int r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(ctx);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(filename);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering file = fopen(filename, "re");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!file) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (errno == ENOENT)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering else
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return -errno;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering }
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (null_or_empty_fd(fileno(file))) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_debug("Skipping empty file: %s", filename);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering return 0;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link = new0(link_config, 1);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!link)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return log_oom();
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering link->mac_policy = _MACPOLICY_INVALID;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link->wol = _WOL_INVALID;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link->duplex = _DUP_INVALID;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = config_parse(NULL, filename, file,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "Match\0Link\0Ethernet\0",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering config_item_perf_lookup, link_config_gperf_lookup,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering false, false, true, link);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering else
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering log_debug("Parsed configuration file %s", filename);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link->filename = strdup(filename);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering LIST_PREPEND(links, ctx->links, link);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return 0;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic bool enable_name_policy(void) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_free_ char *line = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering const char *word, *state;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering int r;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering size_t l;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = proc_cmdline(&line);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return true;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering }
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering FOREACH_WORD_QUOTED(word, l, line, state)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (strneq(word, "net.ifnames=0", l))
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return false;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return true;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint link_config_load(link_config_ctx *ctx) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering int r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_strv_free_ char **files;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering char **f;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering link_configs_free(ctx);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!enable_name_policy()) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering ctx->enable_name_policy = false;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* update timestamp */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, true);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering r = conf_files_list_strv(&files, ".link", NULL, link_dirs);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (r < 0)
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering return log_error_errno(r, "failed to enumerate link files: %m");
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn STRV_FOREACH_BACKWARDS(f, files) {
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn r = load_link(ctx, *f);
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn if (r < 0)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return r;
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn }
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn return 0;
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn}
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringbool link_config_should_reload(link_config_ctx *ctx) {
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn return paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, false);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint link_config_get(link_config_ctx *ctx, struct udev_device *device,
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering link_config **ret) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering link_config *link;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering LIST_FOREACH(links, link, ctx->links) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering const char* attr_value;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering attr_value = udev_device_get_sysattr_value(device, "address");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (net_match_config(link->match_mac, link->match_path, link->match_driver,
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering link->match_type, link->match_name, link->match_host,
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering link->match_virt, link->match_kernel, link->match_arch,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering attr_value ? ether_aton(attr_value) : NULL,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering udev_device_get_property_value(device, "ID_PATH"),
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering udev_device_get_driver(udev_device_get_parent(device)),
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering udev_device_get_property_value(device, "ID_NET_DRIVER"),
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering udev_device_get_devtype(device),
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering udev_device_get_sysname(device))) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (link->match_name) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering unsigned char name_assign_type = NET_NAME_UNKNOWN;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering attr_value = udev_device_get_sysattr_value(device, "name_assign_type");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (attr_value)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering (void) safe_atou8(attr_value, &name_assign_type);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (name_assign_type == NET_NAME_ENUM) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_warning("Config file %s applies to device based on potentially unpredictable interface name '%s'",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link->filename, udev_device_get_sysname(device));
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering *ret = link;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return 0;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering } else if (name_assign_type == NET_NAME_RENAMED) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_warning("Config file %s matches device based on renamed interface name '%s', ignoring",
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering link->filename, udev_device_get_sysname(device));
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering continue;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering }
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering }
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_debug("Config file %s applies to device %s",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering link->filename, udev_device_get_sysname(device));
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering *ret = link;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
d2e54fae5ca7a0f71b5ac8b356a589ff0a09ea0aKay Sievers *ret = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return -ENOENT;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic bool mac_is_random(struct udev_device *device) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering const char *s;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering unsigned type;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering int r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* if we can't get the assign type, assume it is not random */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering s = udev_device_get_sysattr_value(device, "addr_assign_type");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!s)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return false;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = safe_atou(s, &type);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return false;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return type == NET_ADDR_RANDOM;
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic bool should_rename(struct udev_device *device, bool respect_predictable) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering const char *s;
718db96199eb307751264e4163555662c9a389faLennart Poettering unsigned type;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering int r;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* if we can't get the assgin type, assume we should rename */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering s = udev_device_get_sysattr_value(device, "name_assign_type");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!s)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return true;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = safe_atou(s, &type);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return true;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering switch (type) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering case NET_NAME_USER:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering case NET_NAME_RENAMED:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* these were already named by userspace, do not touch again */
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return false;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering case NET_NAME_PREDICTABLE:
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering /* the kernel claims to have given a predictable name */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (respect_predictable)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return false;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* fall through */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering case NET_NAME_ENUM:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering default:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* the name is known to be bad, or of an unknown type */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return true;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic int get_mac(struct udev_device *device, bool want_random,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering struct ether_addr *mac) {
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering int r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (want_random)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering random_bytes(mac->ether_addr_octet, ETH_ALEN);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering else {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering uint8_t result[8];
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering r = net_get_unique_predictable_data(device, result);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (r < 0)
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert_cc(ETH_ALEN <= sizeof(result));
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering memcpy(mac->ether_addr_octet, result, ETH_ALEN);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering /* see eth_random_addr in the kernel */
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering return 0;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering}
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poetteringint link_config_apply(link_config_ctx *ctx, link_config *config,
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering struct udev_device *device, const char **name) {
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering const char *old_name;
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering const char *new_name = NULL;
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering struct ether_addr generated_mac;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering struct ether_addr *mac = NULL;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering bool respect_predictable = false;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering int r, ifindex;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering assert(ctx);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering assert(config);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering assert(device);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering assert(name);
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering old_name = udev_device_get_sysname(device);
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering if (!old_name)
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering return -EINVAL;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering r = ethtool_set_speed(&ctx->ethtool_fd, old_name, config->speed / 1024,
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering config->duplex);
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering if (r < 0)
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering log_warning_errno(r, "Could not set speed or duplex of %s to %u Mbps (%s): %m",
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering old_name, config->speed / 1024,
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering duplex_to_string(config->duplex));
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (r < 0)
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering old_name, wol_to_string(config->wol));
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering ifindex = udev_device_get_ifindex(device);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (ifindex <= 0) {
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering log_warning("Could not find ifindex");
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering return -ENODEV;
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering }
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering if (ctx->enable_name_policy && config->name_policy) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering NamePolicy *policy;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering for (policy = config->name_policy;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering switch (*policy) {
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering case NAMEPOLICY_KERNEL:
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering respect_predictable = true;
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering break;
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering case NAMEPOLICY_DATABASE:
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering new_name = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering break;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering case NAMEPOLICY_ONBOARD:
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering break;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering case NAMEPOLICY_SLOT:
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering break;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering case NAMEPOLICY_PATH:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
ec202eae8e84a4c99f054f771cb832046cb8769fShawn Landden break;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering case NAMEPOLICY_MAC:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering break;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering default:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering break;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering }
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering }
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering }
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (should_rename(device, respect_predictable)) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering /* if not set by policy, fall back manually set name */
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (!new_name)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering new_name = config->name;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering } else
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering new_name = NULL;
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering switch (config->mac_policy) {
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering case MACPOLICY_PERSISTENT:
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (mac_is_random(device)) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering r = get_mac(device, false, &generated_mac);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (r == -ENOENT) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering log_warning_errno(r, "Could not generate persistent MAC address for %s: %m", old_name);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering break;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering } else if (r < 0)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering return r;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering mac = &generated_mac;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering break;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering case MACPOLICY_RANDOM:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!mac_is_random(device)) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = get_mac(device, true, &generated_mac);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r == -ENOENT) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_warning_errno(r, "Could not generate random MAC address for %s: %m", old_name);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering break;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering } else if (r < 0)
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering mac = &generated_mac;
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen }
4943c1c94ba751c98763f4232b4350481b22c90aLennart Poettering break;
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen default:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering mac = config->mac;
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen }
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac,
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering config->mtu);
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen if (r < 0)
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering return log_warning_errno(r, "Could not set Alias, MACAddress or MTU on %s: %m", old_name);
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering *name = new_name;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering const char *name;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering char *driver;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering int r;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering name = udev_device_get_sysname(device);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (!name)
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering return -EINVAL;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
if (r < 0)
return r;
*ret = driver;
return 0;
}
static const char* const mac_policy_table[_MACPOLICY_MAX] = {
[MACPOLICY_PERSISTENT] = "persistent",
[MACPOLICY_RANDOM] = "random"
};
DEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
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_KERNEL] = "kernel",
[NAMEPOLICY_DATABASE] = "database",
[NAMEPOLICY_ONBOARD] = "onboard",
[NAMEPOLICY_SLOT] = "slot",
[NAMEPOLICY_PATH] = "path",
[NAMEPOLICY_MAC] = "mac"
};
DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
_NAMEPOLICY_INVALID,
"Failed to parse interface name policy");