1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/ether.h>
25 #include "link-config.h"
27 #include "ethtool-util.h"
29 #include "libudev-private.h"
34 #include "path-util.h"
35 #include "conf-parser.h"
36 #include "conf-files.h"
38 struct link_config_ctx {
39 LIST_HEAD(link_config, links);
46 usec_t *link_dirs_ts_usec;
49 int link_config_ctx_new(link_config_ctx **ret) {
56 ctx = new0(link_config_ctx, 1);
60 r = ethtool_connect(&ctx->ethtool_fd);
62 link_config_ctx_free(ctx);
66 r = sd_rtnl_open(0, &ctx->rtnl);
68 link_config_ctx_free(ctx);
72 LIST_HEAD_INIT(ctx->links);
74 ctx->link_dirs = strv_new("/etc/net/links",
78 if (!ctx->link_dirs) {
79 log_error("failed to build link config directory array");
80 link_config_ctx_free(ctx);
83 if (!path_strv_canonicalize_uniq(ctx->link_dirs)) {
84 log_error("failed to canonicalize link config directories\n");
85 link_config_ctx_free(ctx);
89 ctx->link_dirs_ts_usec = calloc(strv_length(ctx->link_dirs), sizeof(usec_t));
90 if(!ctx->link_dirs_ts_usec) {
91 link_config_ctx_free(ctx);
99 static void link_configs_free(link_config_ctx *ctx) {
100 link_config *link, *link_next;
105 LIST_FOREACH_SAFE(links, link, link_next, ctx->links) {
106 free(link->filename);
107 free(link->match_path);
108 free(link->match_driver);
109 free(link->match_type);
110 free(link->description);
116 void link_config_ctx_free(link_config_ctx *ctx) {
120 if (ctx->ethtool_fd >= 0)
121 close_nointr_nofail(ctx->ethtool_fd);
123 sd_rtnl_unref(ctx->rtnl);
125 strv_free(ctx->link_dirs);
126 free(ctx->link_dirs_ts_usec);
127 link_configs_free(ctx);
134 static int load_link(link_config_ctx *ctx, const char *filename) {
139 file = fopen(filename, "re");
147 link = new0(link_config, 1);
153 r = config_parse(NULL, filename, file, "Match\0Link\0Ethernet\0", config_item_perf_lookup,
154 (void*) link_config_gperf_lookup, false, false, link);
156 log_warning("Colud not parse config file %s: %s", filename, strerror(-r));
159 log_info("Parsed configuration file %s", filename);
161 link->filename = strdup(filename);
163 LIST_PREPEND(links, ctx->links, link);
172 int link_config_load(link_config_ctx *ctx) {
176 link_configs_free(ctx);
178 /* update timestamps */
179 paths_check_timestamp(ctx->link_dirs, ctx->link_dirs_ts_usec, true);
181 r = conf_files_list_strv(&files, ".link", NULL, (const char **)ctx->link_dirs);
183 log_error("failed to enumerate link files: %s", strerror(-r));
187 STRV_FOREACH_BACKWARDS(f, files) {
188 r = load_link(ctx, *f);
196 bool link_config_should_reload(link_config_ctx *ctx) {
197 return paths_check_timestamp(ctx->link_dirs, ctx->link_dirs_ts_usec, false);
200 static bool match_config(link_config *match, struct udev_device *device) {
201 const char *property;
203 if (match->match_mac) {
204 property = udev_device_get_sysattr_value(device, "address");
205 if (!property || !streq(match->match_mac, property)) {
206 log_debug("Device MAC address (%s) did not match MACAddress=%s", property, match->match_mac);
211 if (match->match_path) {
212 property = udev_device_get_property_value(device, "ID_PATH");
213 if (!property || !streq(match->match_path, property)) {
214 log_debug("Device's persistent path (%s) did not match Path=%s", property, match->match_path);
219 if (match->match_driver) {
220 property = udev_device_get_driver(device);
221 if (!property || !streq(match->match_driver, property)) {
222 log_debug("Device driver (%s) did not match Driver=%s", property, match->match_driver);
227 if (match->match_type) {
228 property = udev_device_get_devtype(device);
229 if (!property || !streq(match->match_type, property)) {
230 log_debug("Device type (%s) did not match Type=%s", property, match->match_type);
238 int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_config **ret) {
241 LIST_FOREACH(links, link, ctx->links) {
242 if (!match_config(link, device)) {
243 log_info("Config file %s does not apply to device %s", link->filename, udev_device_get_sysname(device));
245 log_info("Config file %s applies to device %s", link->filename, udev_device_get_sysname(device));
254 static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, const char *mac, unsigned int mtu) {
255 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
256 char new_name[IFNAMSIZ];
257 struct ether_addr new_mac;
264 r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
269 strscpy(new_name, IFNAMSIZ, name);
270 r = sd_rtnl_message_append(message, IFLA_IFNAME, new_name);
278 r = sscanf(mac, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
279 &new_mac.ether_addr_octet[0],
280 &new_mac.ether_addr_octet[1],
281 &new_mac.ether_addr_octet[2],
282 &new_mac.ether_addr_octet[3],
283 &new_mac.ether_addr_octet[4],
284 &new_mac.ether_addr_octet[5]);
287 r = sd_rtnl_message_append(message, IFLA_ADDRESS, &new_mac);
295 r = sd_rtnl_message_append(message, IFLA_MTU, &mtu);
303 r = sd_rtnl_send_with_reply_and_block(rtnl, message, 250 * USEC_PER_MSEC, NULL);
311 int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device) {
315 name = udev_device_get_sysname(device);
319 log_info("Configuring %s", name);
321 if (config->description) {
322 r = udev_device_set_sysattr_value(device, "ifalias",
323 config->description);
325 log_warning("Could not set description of %s to '%s': %s",
326 name, config->description, strerror(-r));
328 log_info("Set link description of %s to '%s'", name,
329 config->description);
332 if (config->speed || config->duplex) {
333 r = ethtool_set_speed(ctx->ethtool_fd, name,
334 config->speed, config->duplex);
336 log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
337 name, config->speed, config->duplex, strerror(-r));
339 log_info("Set speed or duplex of %s to %u Mbytes (%s)", name,
340 config->speed, config->duplex);
344 r = ethtool_set_wol(ctx->ethtool_fd, name, config->wol);
346 log_warning("Could not set WakeOnLan of %s to %s: %s",
347 name, config->wol, strerror(-r));
349 log_info("Set WakeOnLan of %s to %s", name, config->wol);
352 ifindex = udev_device_get_ifindex(device);
354 log_warning("Could not find ifindex");
358 r = rtnl_set_properties(ctx->rtnl, ifindex, config->name, config->mac, config->mtu);
360 log_warning("Could not set Name, MACAddress or MTU on %s", name);