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>
27 #include "link-config.h"
28 #include "ethtool-util.h"
30 #include "libudev-private.h"
35 #include "path-util.h"
36 #include "conf-parser.h"
37 #include "conf-files.h"
40 #include "rtnl-util.h"
42 struct link_config_ctx {
43 LIST_HEAD(link_config, links);
50 usec_t link_dirs_ts_usec;
53 DEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
54 #define _cleanup_link_config_ctx_free_ _cleanup_(link_config_ctx_freep)
56 int link_config_ctx_new(link_config_ctx **ret) {
57 _cleanup_link_config_ctx_free_ link_config_ctx *ctx = NULL;
62 ctx = new0(link_config_ctx, 1);
66 LIST_HEAD_INIT(ctx->links);
70 ctx->link_dirs = strv_new("/etc/systemd/network",
71 "/run/systemd/network",
72 "/usr/lib/systemd/network",
74 if (!ctx->link_dirs) {
75 log_error("failed to build link config directory array");
79 if (!path_strv_canonicalize_uniq(ctx->link_dirs)) {
80 log_error("failed to canonicalize link config directories\n");
90 static int link_config_ctx_connect(link_config_ctx *ctx) {
93 if (ctx->ethtool_fd >= 0 && ctx->rtnl)
96 r = ethtool_connect(&ctx->ethtool_fd);
100 r = sd_rtnl_open(0, &ctx->rtnl);
107 static void link_configs_free(link_config_ctx *ctx) {
108 link_config *link, *link_next;
113 LIST_FOREACH_SAFE(links, link, link_next, ctx->links) {
114 free(link->filename);
115 free(link->match_path);
116 free(link->match_driver);
117 free(link->match_type);
118 free(link->description);
124 void link_config_ctx_free(link_config_ctx *ctx) {
128 if (ctx->ethtool_fd >= 0)
129 close_nointr_nofail(ctx->ethtool_fd);
131 sd_rtnl_unref(ctx->rtnl);
133 strv_free(ctx->link_dirs);
134 link_configs_free(ctx);
141 static int load_link(link_config_ctx *ctx, const char *filename) {
146 file = fopen(filename, "re");
154 link = new0(link_config, 1);
160 link->mac_policy = _MACPOLICY_INVALID;
161 link->wol = _WOL_INVALID;
162 link->duplex = _DUP_INVALID;
165 r = config_parse(NULL, filename, file, "Match\0Link\0Ethernet\0", config_item_perf_lookup,
166 (void*) link_config_gperf_lookup, false, false, link);
168 log_warning("Colud not parse config file %s: %s", filename, strerror(-r));
171 log_info("Parsed configuration file %s", filename);
173 link->filename = strdup(filename);
175 LIST_PREPEND(links, ctx->links, link);
184 int link_config_load(link_config_ctx *ctx) {
188 link_configs_free(ctx);
190 /* update timestamp */
191 paths_check_timestamp(ctx->link_dirs, &ctx->link_dirs_ts_usec, true);
193 r = conf_files_list_strv(&files, ".link", NULL, (const char **)ctx->link_dirs);
195 log_error("failed to enumerate link files: %s", strerror(-r));
199 STRV_FOREACH_BACKWARDS(f, files) {
200 r = load_link(ctx, *f);
208 bool link_config_should_reload(link_config_ctx *ctx) {
209 return paths_check_timestamp(ctx->link_dirs, &ctx->link_dirs_ts_usec, false);
212 static bool match_config(link_config *match, struct udev_device *device) {
213 const char *property;
215 if (match->match_mac) {
216 property = udev_device_get_sysattr_value(device, "address");
217 if (!property || memcmp(match->match_mac, ether_aton(property), ETH_ALEN)) {
218 log_debug("Device MAC address (%s) did not match MACAddress=%s",
219 property, ether_ntoa(match->match_mac));
224 if (match->match_path) {
225 property = udev_device_get_property_value(device, "ID_PATH");
226 if (!streq_ptr(match->match_path, property)) {
227 log_debug("Device's persistent path (%s) did not match Path=%s",
228 property, match->match_path);
233 if (match->match_driver) {
234 property = udev_device_get_driver(device);
235 if (!streq_ptr(match->match_driver, property)) {
236 log_debug("Device driver (%s) did not match Driver=%s",
237 property, match->match_driver);
242 if (match->match_type) {
243 property = udev_device_get_devtype(device);
244 if (!streq_ptr(match->match_type, property)) {
245 log_debug("Device type (%s) did not match Type=%s",
246 property, match->match_type);
254 int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_config **ret) {
257 LIST_FOREACH(links, link, ctx->links) {
258 if (!match_config(link, device)) {
259 log_info("Config file %s does not apply to device %s", link->filename, udev_device_get_sysname(device));
261 log_info("Config file %s applies to device %s", link->filename, udev_device_get_sysname(device));
270 static bool enable_name_policy(void) {
271 _cleanup_free_ char *line;
276 r = read_one_line_file("/proc/cmdline", &line);
278 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
279 return true; /* something is very wrong, let's not make it worse */
282 FOREACH_WORD_QUOTED(w, l, line, state)
283 if (strneq(w, "net.ifnames=0", l))
289 static bool mac_is_random(struct udev_device *device) {
294 s = udev_device_get_sysattr_value(device, "addr_assign_type");
296 return false; /* if we don't know, assume it is not random */
297 r = safe_atou(s, &type);
301 /* check for NET_ADDR_RANDOM */
305 static bool mac_is_permanent(struct udev_device *device) {
310 s = udev_device_get_sysattr_value(device, "addr_assign_type");
312 return true; /* if we don't know, assume it is permanent */
313 r = safe_atou(s, &type);
317 /* check for NET_ADDR_PERM */
321 static int get_mac(struct udev_device *device, bool want_random, struct ether_addr *mac) {
330 char machineid_buf[33];
331 const char *seed_str;
333 /* fetch some persistent data unique (on this machine) to this device */
334 name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
336 name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
338 name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
343 /* fetch some persistent data unique to this machine */
344 r = sd_id128_get_machine(&machine);
348 /* combine the data */
349 seed_str = strappenda(name, sd_id128_to_string(machine, machineid_buf));
351 /* hash to get seed */
352 seed = string_hash_func(seed_str);
357 for(i = 0; i < ETH_ALEN; i++) {
358 mac->ether_addr_octet[i] = random();
361 /* see eth_random_addr in the kernel */
362 mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
363 mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
368 int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device) {
370 const char *new_name = NULL;
371 struct ether_addr generated_mac;
372 struct ether_addr *mac = NULL;
375 r = link_config_ctx_connect(ctx);
379 name = udev_device_get_sysname(device);
383 log_info("Configuring %s", name);
385 if (config->description) {
386 r = udev_device_set_sysattr_value(device, "ifalias",
387 config->description);
389 log_warning("Could not set description of %s to '%s': %s",
390 name, config->description, strerror(-r));
393 r = ethtool_set_speed(ctx->ethtool_fd, name, config->speed, config->duplex);
395 log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
396 name, config->speed, duplex_to_string(config->duplex), strerror(-r));
398 r = ethtool_set_wol(ctx->ethtool_fd, name, config->wol);
400 log_warning("Could not set WakeOnLan of %s to %s: %s",
401 name, wol_to_string(config->wol), strerror(-r));
403 ifindex = udev_device_get_ifindex(device);
405 log_warning("Could not find ifindex");
409 if (config->name_policy && enable_name_policy()) {
412 for (policy = config->name_policy; !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
414 case NAMEPOLICY_ONBOARD:
415 new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
417 case NAMEPOLICY_SLOT:
418 new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
420 case NAMEPOLICY_PATH:
421 new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
424 new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
432 if (!new_name && config->name) {
433 new_name = config->name;
436 switch (config->mac_policy) {
437 case MACPOLICY_PERSISTENT:
438 if (!mac_is_permanent(device)) {
439 r = get_mac(device, false, &generated_mac);
442 mac = &generated_mac;
445 case MACPOLICY_RANDOM:
446 if (!mac_is_random(device)) {
447 r = get_mac(device, true, &generated_mac);
450 mac = &generated_mac;
457 r = rtnl_set_link_properties(ctx->rtnl, ifindex, new_name, mac, config->mtu);
459 log_warning("Could not set Name, MACAddress or MTU on %s: %s", name, strerror(-r));