link-config.c revision 489124365d1d391864898b9869dd668eea5b2e28
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
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
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
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 Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <netinet/ether.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <net/if.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "sd-id128.h"
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "link-config.h"
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering#include "ethtool-util.h"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "libudev-private.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "sd-rtnl.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "log.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "strv.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "path-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "conf-parser.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "conf-files.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "fileio.h"
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering#include "hashmap.h"
b93312f5960b276bae915906ccde36f545bae3e0Zbigniew Jędrzejewski-Szmek#include "rtnl-util.h"
b93312f5960b276bae915906ccde36f545bae3e0Zbigniew Jędrzejewski-Szmek#include "net-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstruct link_config_ctx {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_HEAD(link_config, links);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int ethtool_fd;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bool enable_name_policy;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering sd_rtnl *rtnl;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering char **link_dirs;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering usec_t link_dirs_ts_usec;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering};
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
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)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
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
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!ret)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ctx = new0(link_config_ctx, 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!ctx)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return -ENOMEM;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering LIST_HEAD_INIT(ctx->links);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering ctx->ethtool_fd = -1;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ctx->enable_name_policy = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ctx->link_dirs = strv_new("/etc/systemd/network",
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering "/run/systemd/network",
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "/usr/lib/systemd/network",
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#ifdef HAVE_SPLIT_USR
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "/lib/systemd/network",
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#endif
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!ctx->link_dirs) {
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek log_error("failed to build link config directory array");
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return -ENOMEM;
c0eb11cfd016381fe02875a4ef29c1ade00c94e7Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!path_strv_canonicalize_uniq(ctx->link_dirs)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("failed to canonicalize link config directories\n");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *ret = ctx;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ctx = NULL;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return 0;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering}
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringstatic int link_config_ctx_connect(link_config_ctx *ctx) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering int r;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (ctx->ethtool_fd >= 0 && ctx->rtnl)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return 0;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = ethtool_connect(&ctx->ethtool_fd);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = sd_rtnl_open(0, &ctx->rtnl);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic void link_configs_free(link_config_ctx *ctx) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek link_config *link, *link_next;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!ctx)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen LIST_FOREACH_SAFE(links, link, link_next, ctx->links) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen free(link->filename);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen free(link->match_path);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen free(link->match_driver);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen free(link->match_type);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen free(link->description);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen free(link);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering}
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersenvoid link_config_ctx_free(link_config_ctx *ctx) {
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (!ctx)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (ctx->ethtool_fd >= 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek close_nointr_nofail(ctx->ethtool_fd);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek sd_rtnl_unref(ctx->rtnl);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek strv_free(ctx->link_dirs);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek link_configs_free(ctx);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek free(ctx);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekstatic int load_link(link_config_ctx *ctx, const char *filename) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek link_config *link;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek FILE *file;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek file = fopen(filename, "re");
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!file) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (errno == ENOENT)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen else
9ead3519c54b6d1b79b35541873b5cf7c8b3a7d3Lennart Poettering return errno;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen link = new0(link_config, 1);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!link) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = log_oom();
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen goto failure;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen link->mac_policy = _MACPOLICY_INVALID;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen link->wol = _WOL_INVALID;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen link->duplex = _DUP_INVALID;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
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);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Could not parse config file %s: %s", filename, strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto failure;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_debug("Parsed configuration file %s", filename);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering link->filename = strdup(filename);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering LIST_PREPEND(links, ctx->links, link);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringfailure:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(link);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen}
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersenstatic bool enable_name_policy(void) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering _cleanup_free_ char *line;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char *w, *state;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering size_t l;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = proc_cmdline(&line);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r <= 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return true;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering FOREACH_WORD_QUOTED(w, l, line, state)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l == sizeof("net.ifnames=0") - 1 && strneq(w, "net.ifnames=0", l))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return false;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return true;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint link_config_load(link_config_ctx *ctx) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering char **files, **f;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering link_configs_free(ctx);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!enable_name_policy()) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt ctx->enable_name_policy = false;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt log_info("Network interface NamePolicy= disabled on kernel commandline, ignoring.");
}
/* update timestamp */
paths_check_timestamp(ctx->link_dirs, &ctx->link_dirs_ts_usec, true);
r = conf_files_list_strv(&files, ".link", NULL, (const char **)ctx->link_dirs);
if (r < 0) {
log_error("failed to enumerate link files: %s", strerror(-r));
return r;
}
STRV_FOREACH_BACKWARDS(f, files) {
r = load_link(ctx, *f);
if (r < 0)
return r;
}
return 0;
}
bool link_config_should_reload(link_config_ctx *ctx) {
return paths_check_timestamp(ctx->link_dirs, &ctx->link_dirs_ts_usec, false);
}
int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_config **ret) {
link_config *link;
LIST_FOREACH(links, link, ctx->links) {
if (net_match_config(link->match_mac, link->match_path,
link->match_driver, link->match_type, NULL,
udev_device_get_sysattr_value(device, "address"),
udev_device_get_property_value(device, "ID_PATH"),
udev_device_get_driver(device),
udev_device_get_devtype(device),
NULL)) {
log_debug("Config file %s applies to device %s",
link->filename,
udev_device_get_sysname(device));
*ret = link;
return 0;
}
}
*ret = NULL;
return -ENOENT;
}
static bool mac_is_random(struct udev_device *device) {
const char *s;
unsigned type;
int r;
s = udev_device_get_sysattr_value(device, "addr_assign_type");
if (!s)
return false; /* if we don't know, assume it is not random */
r = safe_atou(s, &type);
if (r < 0)
return false;
/* check for NET_ADDR_RANDOM */
return type == 1;
}
static bool mac_is_permanent(struct udev_device *device) {
const char *s;
unsigned type;
int r;
s = udev_device_get_sysattr_value(device, "addr_assign_type");
if (!s)
return true; /* if we don't know, assume it is permanent */
r = safe_atou(s, &type);
if (r < 0)
return true;
/* check for NET_ADDR_PERM */
return type == 0;
}
static int get_mac(struct udev_device *device, bool want_random, struct ether_addr *mac) {
unsigned int seed;
int r, i;
if (want_random)
seed = random_u();
else {
const char *name;
sd_id128_t machine;
char machineid_buf[33];
const char *seed_str;
/* fetch some persistent data unique (on this machine) to this device */
name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
if (!name) {
name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
if (!name) {
name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
if (!name)
return -ENOENT;
}
}
/* fetch some persistent data unique to this machine */
r = sd_id128_get_machine(&machine);
if (r < 0)
return r;
/* combine the data */
seed_str = strappenda(name, sd_id128_to_string(machine, machineid_buf));
/* hash to get seed */
seed = string_hash_func(seed_str);
}
srandom(seed);
for(i = 0; i < ETH_ALEN; i++) {
mac->ether_addr_octet[i] = random();
}
/* see eth_random_addr in the kernel */
mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
return 0;
}
int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device, const char **name) {
const char *old_name;
const char *new_name = NULL;
struct ether_addr generated_mac;
struct ether_addr *mac = NULL;
int r, ifindex;
assert(ctx);
assert(config);
assert(device);
assert(name);
r = link_config_ctx_connect(ctx);
if (r < 0)
return r;
old_name = udev_device_get_sysname(device);
if (!old_name)
return -EINVAL;
if (config->description) {
r = udev_device_set_sysattr_value(device, "ifalias",
config->description);
if (r < 0)
log_warning("Could not set description of %s to '%s': %s",
old_name, config->description, strerror(-r));
}
r = ethtool_set_speed(ctx->ethtool_fd, old_name, config->speed, config->duplex);
if (r < 0)
log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
old_name, config->speed, duplex_to_string(config->duplex), strerror(-r));
r = ethtool_set_wol(ctx->ethtool_fd, old_name, config->wol);
if (r < 0)
log_warning("Could not set WakeOnLan of %s to %s: %s",
old_name, wol_to_string(config->wol), strerror(-r));
ifindex = udev_device_get_ifindex(device);
if (ifindex <= 0) {
log_warning("Could not find ifindex");
return -ENODEV;
}
if (ctx->enable_name_policy && config->name_policy) {
NamePolicy *policy;
for (policy = config->name_policy; !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
switch (*policy) {
case NAMEPOLICY_ONBOARD:
new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
break;
case NAMEPOLICY_SLOT:
new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
break;
case NAMEPOLICY_PATH:
new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
break;
case NAMEPOLICY_MAC:
new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
break;
default:
break;
}
}
}
if (new_name)
*name = new_name; /* a name was set by a policy */
else if (config->name)
*name = config->name; /* a name was set manually in the config */
else
*name = NULL;
switch (config->mac_policy) {
case MACPOLICY_PERSISTENT:
if (!mac_is_permanent(device)) {
r = get_mac(device, false, &generated_mac);
if (r < 0)
return r;
mac = &generated_mac;
}
break;
case MACPOLICY_RANDOM:
if (!mac_is_random(device)) {
r = get_mac(device, true, &generated_mac);
if (r < 0)
return r;
mac = &generated_mac;
}
break;
default:
mac = config->mac;
}
r = rtnl_set_link_properties(ctx->rtnl, ifindex, mac, config->mtu);
if (r < 0) {
log_warning("Could not set MACAddress or MTU on %s: %s", old_name, strerror(-r));
return r;
}
return 0;
}
static const char* const mac_policy_table[] = {
[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_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");