link-config.c revision 8fcde01280adcbd07e8205b91ac52b06305b6208
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/***
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer systemd is free software; you can redistribute it and/or modify it
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer under the terms of the GNU Lesser General Public License as published by
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer the Free Software Foundation; either version 2.1 of the License, or
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt (at your option) any later version.
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt systemd is distributed in the hope that it will be useful, but
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt WITHOUT ANY WARRANTY; without even the implied warranty of
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1c36b4a73b876258fbe01fbe9bc9b750b7dcc9ceEvgeny Vereshchagin Lesser General Public License for more details.
c50a4525aa8151b180d5a325e88c5f3812e66c36Martin Pitt
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier You should have received a copy of the GNU Lesser General Public License
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier***/
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <netinet/ether.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <linux/netdevice.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "sd-netlink.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "conf-files.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "conf-parser.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "ethtool-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "fd-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "libudev-private.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "link-config.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "log.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "missing.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "netlink-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "network-internal.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "parse-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "path-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "random-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "stat-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "string-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "strv.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstruct link_config_ctx {
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer LIST_HEAD(link_config, links);
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer int ethtool_fd;
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer bool enable_name_policy;
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer sd_netlink *rtnl;
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin usec_t link_dirs_ts_usec;
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin};
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchaginstatic const char* const link_dirs[] = {
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin "/etc/systemd/network",
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier "/run/systemd/network",
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier "/usr/lib/systemd/network",
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#ifdef HAVE_SPLIT_USR
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier "/lib/systemd/network",
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#endif
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier NULL};
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalierstatic void link_config_free(link_config *link) {
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt if (!link)
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier return;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->filename);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->match_mac);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier strv_free(link->match_path);
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack strv_free(link->match_driver);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier strv_free(link->match_type);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->match_name);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->match_host);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->match_virt);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->match_kernel);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->match_arch);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->description);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->mac);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier free(link->name_policy);
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack free(link->name);
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack free(link->alias);
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack
dbf43a42b8bb66d53c7cbab05f104c28097f811eDaniel Mack free(link);
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek}
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek
889a90422dd47284dffa32b9234a6e58991b000cRonny ChevalierDEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void link_configs_free(link_config_ctx *ctx) {
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek link_config *link, *link_next;
24be78d72b931b0175f08cee12fd23d631c024bfEvgeny Vereshchagin
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!ctx)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link_config_free(link);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevaliervoid link_config_ctx_free(link_config_ctx *ctx) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!ctx)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier safe_close(ctx->ethtool_fd);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sd_netlink_unref(ctx->rtnl);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link_configs_free(ctx);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier free(ctx);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny ChevalierDEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierint link_config_ctx_new(link_config_ctx **ret) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_(link_config_ctx_freep) link_config_ctx *ctx = NULL;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin if (!ret)
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin return -EINVAL;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin ctx = new0(link_config_ctx, 1);
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin if (!ctx)
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin return -ENOMEM;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin LIST_HEAD_INIT(ctx->links);
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin ctx->ethtool_fd = -1;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin ctx->enable_name_policy = true;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin *ret = ctx;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin ctx = NULL;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin return 0;
a2fbff31c9c319da51528f85ae97d019f1e61a86Evgeny Vereshchagin}
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchaginstatic int load_link(link_config_ctx *ctx, const char *filename) {
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin _cleanup_(link_config_freep) link_config *link = NULL;
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin _cleanup_fclose_ FILE *file = NULL;
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin int r;
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin assert(ctx);
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin assert(filename);
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin file = fopen(filename, "re");
cb2f9d3f296bc80b55f09880d61dfdf47fc98212Evgeny Vereshchagin if (!file) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (errno == ENOENT)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -errno;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (null_or_empty_fd(fileno(file))) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_debug("Skipping empty file: %s", filename);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
2375607039517c88df51ef16ddbb624ec1c10654Kay Sievers link = new0(link_config, 1);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!link)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return log_oom();
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->mac_policy = _MACPOLICY_INVALID;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->wol = _WOL_INVALID;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->duplex = _DUP_INVALID;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = config_parse(NULL, filename, file,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier "Match\0Link\0Ethernet\0",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier config_item_perf_lookup, link_config_gperf_lookup,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier false, false, true, link);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek else
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_debug("Parsed configuration file %s", filename);
1b1eae69ce52ef6c89a1200e8d3758549b291991Daniel Mack
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (link->mtu > UINT_MAX || link->speed > UINT_MAX)
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek return -ERANGE;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
edbced8a151c1b7ded685e2ec644950d2adec5f5Harald Hoyer link->filename = strdup(filename);
1b1eae69ce52ef6c89a1200e8d3758549b291991Daniel Mack
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier LIST_PREPEND(links, ctx->links, link);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool enable_name_policy(void) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_free_ char *line = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *word, *state;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier size_t l;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = proc_cmdline(&line);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return true;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier FOREACH_WORD_QUOTED(word, l, line, state)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (strneq(word, "net.ifnames=0", l))
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return true;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierint link_config_load(link_config_ctx *ctx) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_strv_free_ char **files;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier char **f;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link_configs_free(ctx);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin if (!enable_name_policy()) {
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin ctx->enable_name_policy = false;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin }
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin /* update timestamp */
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, true);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin r = conf_files_list_strv(&files, ".link", NULL, link_dirs);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin if (r < 0)
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin return log_error_errno(r, "failed to enumerate link files: %m");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier STRV_FOREACH_BACKWARDS(f, files) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = load_link(ctx, *f);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierbool link_config_should_reload(link_config_ctx *ctx) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, false);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierint link_config_get(link_config_ctx *ctx, struct udev_device *device,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link_config **ret) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link_config *link;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(ctx);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(device);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(ret);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier LIST_FOREACH(links, link, ctx->links) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char* attr_value;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier attr_value = udev_device_get_sysattr_value(device, "address");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (net_match_config(link->match_mac, link->match_path, link->match_driver,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->match_type, link->match_name, link->match_host,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->match_virt, link->match_kernel, link->match_arch,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier attr_value ? ether_aton(attr_value) : NULL,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier udev_device_get_property_value(device, "ID_PATH"),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier udev_device_get_driver(udev_device_get_parent(device)),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier udev_device_get_property_value(device, "ID_NET_DRIVER"),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier udev_device_get_devtype(device),
bf3a947cb44f31359bba313e0252cbcc0dc95b03Evgeny Vereshchagin udev_device_get_sysname(device))) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (link->match_name) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier unsigned char name_assign_type = NET_NAME_UNKNOWN;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier attr_value = udev_device_get_sysattr_value(device, "name_assign_type");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (attr_value)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier (void) safe_atou8(attr_value, &name_assign_type);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (name_assign_type == NET_NAME_ENUM) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Config file %s applies to device based on potentially unpredictable interface name '%s'",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->filename, udev_device_get_sysname(device));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier *ret = link;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (name_assign_type == NET_NAME_RENAMED) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_warning("Config file %s matches device based on renamed interface name '%s', ignoring",
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier link->filename, udev_device_get_sysname(device));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier continue;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt log_debug("Config file %s applies to device %s",
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt link->filename, udev_device_get_sysname(device));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier *ret = link;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier *ret = NULL;
cffae62bcb6912fbaf1b7b282d9d170c9d308897Martin Pitt
99877b7e3782a51b31bf191825f0335500f52fe5Harald Hoyer return -ENOENT;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool mac_is_random(struct udev_device *device) {
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt const char *s;
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt unsigned type;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
e63b61be5350dbe92ea12e1eeb96dde251ed9292Evgeny Vereshchagin /* if we can't get the assign type, assume it is not random */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier s = udev_device_get_sysattr_value(device, "addr_assign_type");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!s)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = safe_atou(s, &type);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin return false;
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return type == NET_ADDR_RANDOM;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic bool should_rename(struct udev_device *device, bool respect_predictable) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *s;
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin unsigned type;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin /* if we can't get the assgin type, assume we should rename */
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin s = udev_device_get_sysattr_value(device, "name_assign_type");
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin if (!s)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return true;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = safe_atou(s, &type);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return true;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier switch (type) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier case NET_NAME_USER:
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier case NET_NAME_RENAMED:
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* these were already named by userspace, do not touch again */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier case NET_NAME_PREDICTABLE:
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* the kernel claims to have given a predictable name */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (respect_predictable)
25b47f96d9601ff566257b2a31bfb5f4bd25d661Marko Myllynen return false;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* fall through */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier case NET_NAME_ENUM:
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier default:
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* the name is known to be bad, or of an unknown type */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return true;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int get_mac(struct udev_device *device, bool want_random,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier struct ether_addr *mac) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (want_random)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier random_bytes(mac->ether_addr_octet, ETH_ALEN);
53d90f9582f96208b3674da823ad1a3d2c3b1aa4Martin Pitt else {
5c404f1ab8e96efedb983806443ca982a1b2a372Evgeny Vereshchagin uint8_t result[8];
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = net_get_unique_predictable_data(device, result);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert_cc(ETH_ALEN <= sizeof(result));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier memcpy(mac->ether_addr_octet, result, ETH_ALEN);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* see eth_random_addr in the kernel */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierint link_config_apply(link_config_ctx *ctx, link_config *config,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer struct udev_device *device, const char **name) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *old_name;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *new_name = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier struct ether_addr generated_mac;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier struct ether_addr *mac = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer bool respect_predictable = false;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r, ifindex;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(ctx);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert(config);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(device);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer old_name = udev_device_get_sysname(device);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!old_name)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -EINVAL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = ethtool_set_speed(&ctx->ethtool_fd, old_name, config->speed / 1024, config->duplex);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_errno(r, "Could not set speed or duplex of %s to %zu Mbps (%s): %m",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer old_name, config->speed / 1024,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer duplex_to_string(config->duplex));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer old_name, wol_to_string(config->wol));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer ifindex = udev_device_get_ifindex(device);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ifindex <= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Could not find ifindex");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENODEV;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ctx->enable_name_policy && config->name_policy) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer NamePolicy *policy;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (policy = config->name_policy;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer switch (*policy) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case NAMEPOLICY_KERNEL:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer respect_predictable = true;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case NAMEPOLICY_DATABASE:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer new_name = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case NAMEPOLICY_ONBOARD:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case NAMEPOLICY_SLOT:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier case NAMEPOLICY_PATH:
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier break;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier case NAMEPOLICY_MAC:
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer default:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (should_rename(device, respect_predictable)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* if not set by policy, fall back manually set name */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!new_name)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer new_name = config->name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer new_name = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer switch (config->mac_policy) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case MACPOLICY_PERSISTENT:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (mac_is_random(device)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = get_mac(device, false, &generated_mac);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == -ENOENT) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_errno(r, "Could not generate persistent MAC address for %s: %m", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer mac = &generated_mac;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case MACPOLICY_RANDOM:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!mac_is_random(device)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = get_mac(device, true, &generated_mac);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == -ENOENT) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning_errno(r, "Could not generate random MAC address for %s: %m", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer } else if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer mac = &generated_mac;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer break;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer case MACPOLICY_NONE:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer default:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer mac = config->mac;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac, config->mtu);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return log_warning_errno(r, "Could not set Alias, MACAddress or MTU on %s: %m", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *name = new_name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *driver = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer name = udev_device_get_sysname(device);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!name)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -EINVAL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *ret = driver;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic const char* const mac_policy_table[_MACPOLICY_MAX] = {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [MACPOLICY_PERSISTENT] = "persistent",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [MACPOLICY_RANDOM] = "random",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [MACPOLICY_NONE] = "none"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer};
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Failed to parse MAC address policy");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic const char* const name_policy_table[_NAMEPOLICY_MAX] = {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [NAMEPOLICY_KERNEL] = "kernel",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [NAMEPOLICY_DATABASE] = "database",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [NAMEPOLICY_ONBOARD] = "onboard",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [NAMEPOLICY_SLOT] = "slot",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [NAMEPOLICY_PATH] = "path",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer [NAMEPOLICY_MAC] = "mac"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer};
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerDEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _NAMEPOLICY_INVALID,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Failed to parse interface name policy");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer