link-config.c revision 2a73e0d39a9bec82c3800071e375d27164727e71
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering This file is part of systemd.
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering systemd is free software; you can redistribute it and/or modify it
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering under the terms of the GNU Lesser General Public License as published by
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering (at your option) any later version.
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering systemd is distributed in the hope that it will be useful, but
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering Lesser General Public License for more details.
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering You should have received a copy of the GNU Lesser General Public License
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringint link_config_ctx_new(link_config_ctx **ret) {
bf7fabd6000fd95495904ed55db62caf361b3d8fLennart Poettering ctx->link_dirs = strv_new("/etc/net/links",
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_error("failed to build link config directory array");
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering if (!path_strv_canonicalize_uniq(ctx->link_dirs)) {
4b95f1798f22c1bb75295f448188560cb6ec9eceLennart Poettering log_error("failed to canonicalize link config directories\n");
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering ctx->link_dirs_ts_usec = calloc(strv_length(ctx->link_dirs), sizeof(usec_t));
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic void link_configs_free(link_config_ctx *ctx) {
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poettering LIST_FOREACH_SAFE(links, link, link_next, ctx->links) {
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poetteringvoid link_config_ctx_free(link_config_ctx *ctx) {
452b4e327d23b9e468d3198f4c91e8efd0b52a71Lennart Poetteringstatic int load_link(link_config_ctx *ctx, const char *filename) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering r = config_parse(NULL, filename, file, "Match\0Link\0Ethernet\0", config_item_perf_lookup,
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering (void*) link_config_gperf_lookup, false, false, link);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_warning("Colud not parse config file %s: %s", filename, strerror(-r));
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_info("Parsed configuration file %s", filename);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringint link_config_load(link_config_ctx *ctx) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering /* update timestamps */
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering paths_check_timestamp(ctx->link_dirs, ctx->link_dirs_ts_usec, true);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering r = conf_files_list_strv(&files, ".link", NULL, (const char **)ctx->link_dirs);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_error("failed to enumerate link files: %s", strerror(-r));
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringbool link_config_should_reload(link_config_ctx *ctx) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering return paths_check_timestamp(ctx->link_dirs, ctx->link_dirs_ts_usec, false);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic bool match_config(link_config *match, struct udev_device *device) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering property = udev_device_get_sysattr_value(device, "address");
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering if (!property || !streq(match->match_mac, property)) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_debug("Device MAC address (%s) did not match MACAddress=%s", property, match->match_mac);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering property = udev_device_get_property_value(device, "ID_PATH");
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering if (!property || !streq(match->match_path, property)) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_debug("Device's persistent path (%s) did not match Path=%s", property, match->match_path);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering property = udev_device_get_driver(device);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering if (!property || !streq(match->match_driver, property)) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_debug("Device driver (%s) did not match Driver=%s", property, match->match_driver);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering property = udev_device_get_devtype(device);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering if (!property || !streq(match->match_type, property)) {
9176a57c101d51b4a7fb4141240b5ce03abac57dLennart Poettering log_debug("Device type (%s) did not match Type=%s", property, match->match_type);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringint link_config_get(link_config_ctx *ctx, struct udev_device *device, link_config **ret) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_info("Config file %s does not apply to device %s", link->filename, udev_device_get_sysname(device));
9176a57c101d51b4a7fb4141240b5ce03abac57dLennart Poettering log_info("Config file %s applies to device %s", link->filename, udev_device_get_sysname(device));
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, const char *mac, unsigned int mtu) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
9176a57c101d51b4a7fb4141240b5ce03abac57dLennart Poettering r = sd_rtnl_message_append(message, IFLA_IFNAME, new_name);
need_update = true;
if (mac) {
return -EINVAL;
need_update = true;
if (mtu > 0) {
need_update = true;
if (need_update) {
const char *name;
int r, ifindex;
if (!name)
return -EINVAL;
if (ifindex <= 0) {
return -ENODEV;