Systemd/src/udev/udev-builtin-net_link.c

97 lines
2.7 KiB
C
Raw Normal View History

udev: add network link configuration tool This tool applies hardware specific settings to network devices before they are announced via libudev. Settings that will probably eventually be supported are MTU, Speed, DuplexMode, WakeOnLan, MACAddress, MACAddressPolicy (e.g., 'hardware', 'synthetic' or 'random'), Name and NamePolicy (replacing our current interface naming logic). This patch only introduces support for Description, as a proof of concept. Some of these settings may later be overriden by a network management daemon/script. However, these tools should always listen and wait on libudev before touching a device (listening on netlink is not enough). This is no different from how things used to be, as we always supported changing the network interface name from udev rules, which does not work if someone has already started using it. The tool is configured by .link files in /etc/net/links/ (with the usual overriding logic in /run and /lib). The first (in lexicographical order) matching .link file is applied to a given device, and all others are ignored. The .link files contain a [Match] section with (currently) the keys MACAddress, Driver, Type (see DEVTYPE in udevadm info) and Path (this matches on the stable device path as exposed as ID_PATH, and not the unstable DEVPATH). A .link file matches a given device if all of the specified keys do. Currently the keys are treated as plain strings, but some limited globbing may later be added to the keys where it makes sense. Example: /etc/net/links/50-wireless.link [Match] MACAddress=98:f2:e4:42:c6:92 Path=pci-0000:02:00.0-bcma-0 Type=wlan [Link] Description=The wireless link
2013-10-26 00:34:01 +02:00
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "link-config.h"
#include "udev.h"
#include "log.h"
link_config_ctx *ctx;
static int builtin_net_link(struct udev_device *dev, int argc, char **argv, bool test) {
link_config *link;
int r;
if (argc > 1) {
log_error("This program takes no arguments.");
return EXIT_FAILURE;
}
r = link_config_get(ctx, dev, &link);
if (r < 0) {
if (r == -ENOENT) {
log_info("No matching link configuration found");
return EXIT_SUCCESS;
} else {
log_error("Could not get link config");
return EXIT_FAILURE;
}
}
r = link_config_apply(ctx, link, dev);
if (r < 0) {
log_error("Could not apply link config");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static int builtin_net_link_init(struct udev *udev) {
int r;
if (ctx)
return 0;
r = link_config_ctx_new(&ctx);
if (r < 0)
return r;
r = link_config_load(ctx);
if (r < 0)
return r;
log_debug("Created link configuration context");
return 0;
}
static void builtin_net_link_exit(struct udev *udev) {
link_config_ctx_free(ctx);
log_debug("Unloaded link configuration context");
}
static bool builtin_net_link_validate(struct udev *udev) {
log_debug("Check if link configuration needs reloading");
if (!ctx)
return false;
return link_config_should_reload(ctx);
}
const struct udev_builtin udev_builtin_net_link = {
.name = "net_link",
.cmd = builtin_net_link,
.init = builtin_net_link_init,
.exit = builtin_net_link_exit,
.validate = builtin_net_link_validate,
.help = "configure network link",
.run_once = false,
};