af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen This file is part of systemd.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is free software; you can redistribute it and/or modify it
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen under the terms of the GNU Lesser General Public License as published by
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen (at your option) any later version.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is distributed in the hope that it will be useful, but
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Lesser General Public License for more details.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen You should have received a copy of the GNU Lesser General Public License
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmekstatic const char* const link_dirs[] = {
9a4b012e43f23516373bf398dd9a458439d19939Tom Gundersenstatic void link_config_free(link_config *link) {
9a4b012e43f23516373bf398dd9a458439d19939Tom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic void link_configs_free(link_config_ctx *ctx) {
9a4b012e43f23516373bf398dd9a458439d19939Tom Gundersen LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenvoid link_config_ctx_free(link_config_ctx *ctx) {
9a4b012e43f23516373bf398dd9a458439d19939Tom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
9a4b012e43f23516373bf398dd9a458439d19939Tom Gundersenint link_config_ctx_new(link_config_ctx **ret) {
9a4b012e43f23516373bf398dd9a458439d19939Tom Gundersen _cleanup_(link_config_ctx_freep) link_config_ctx *ctx = NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic int load_link(link_config_ctx *ctx, const char *filename) {
9a4b012e43f23516373bf398dd9a458439d19939Tom Gundersen _cleanup_(link_config_freep) link_config *link = NULL;
6e37cd2f4af8928d905203108a4331e375d7127cThomas Hindoe Paaboel Andersen _cleanup_fclose_ FILE *file = NULL;
ed88bcfb7c15029f9fc95ee2380759a9eb782d46Zbigniew Jędrzejewski-Szmek if (null_or_empty_fd(fileno(file))) {
ed88bcfb7c15029f9fc95ee2380759a9eb782d46Zbigniew Jędrzejewski-Szmek log_debug("Skipping empty file: %s", filename);
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek r = config_parse(NULL, filename, file,
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek "Match\0Link\0Ethernet\0",
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek config_item_perf_lookup, link_config_gperf_lookup,
36f822c4bd077f9121757e24b6516e5c7ada63b5Zbigniew Jędrzejewski-Szmek false, false, true, link);
98a375f6d5cac24eb80d6d4e00699851324afdecTom Gundersen log_debug("Parsed configuration file %s", filename);
dab495dc23bf9a5ba0487a057bb594355555a0e9Tom Gundersen if (link->mtu > UINT_MAX || link->speed > UINT_MAX)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek FOREACH_WORD_QUOTED(word, l, line, state)
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek if (strneq(word, "net.ifnames=0", l))
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return false;
3f85ef0f05ffc51e19f86fb83a1c51e8e3cd6817Harald Hoyer log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen /* update timestamp */
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, true);
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek r = conf_files_list_strv(&files, ".link", NULL, link_dirs);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "failed to enumerate link files: %m");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenbool link_config_should_reload(link_config_ctx *ctx) {
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek return paths_check_timestamp(link_dirs, &ctx->link_dirs_ts_usec, false);
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersenint link_config_get(link_config_ctx *ctx, struct udev_device *device,
7eb08da4b388b920c8a894b1500c9cc7dc1f31efTom Gundersen attr_value = udev_device_get_sysattr_value(device, "address");
edbb03e95a3c31bf719d5c6c46eec14d0bcb9c8fTom Gundersen if (net_match_config(link->match_mac, link->match_path, link->match_driver,
7eb08da4b388b920c8a894b1500c9cc7dc1f31efTom Gundersen link->match_type, link->match_name, link->match_host,
edbb03e95a3c31bf719d5c6c46eec14d0bcb9c8fTom Gundersen link->match_virt, link->match_kernel, link->match_arch,
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen udev_device_get_property_value(device, "ID_PATH"),
9b1c2626cef16722603bded9bb52033aba34dd74Tom Gundersen udev_device_get_driver(udev_device_get_parent(device)),
bf175aafd20c9ef974709ef12c5acf836121af33Tom Gundersen udev_device_get_property_value(device, "ID_NET_DRIVER"),
32bc8adcd836baff68e4d0f53b9a382f358cccf8Tom Gundersen unsigned char name_assign_type = NET_NAME_UNKNOWN;
32bc8adcd836baff68e4d0f53b9a382f358cccf8Tom Gundersen attr_value = udev_device_get_sysattr_value(device, "name_assign_type");
dc75168823540076b354135f6e2de7a9a978fbcaZbigniew Jędrzejewski-Szmek (void) safe_atou8(attr_value, &name_assign_type);
ca6038b89645c0c1bd547d6a420bf95eb3d6f4ccTom Gundersen log_warning("Config file %s applies to device based on potentially unpredictable interface name '%s'",
32bc8adcd836baff68e4d0f53b9a382f358cccf8Tom Gundersen link->filename, udev_device_get_sysname(device));
32bc8adcd836baff68e4d0f53b9a382f358cccf8Tom Gundersen } else if (name_assign_type == NET_NAME_RENAMED) {
32bc8adcd836baff68e4d0f53b9a382f358cccf8Tom Gundersen log_warning("Config file %s matches device based on renamed interface name '%s', ignoring",
32bc8adcd836baff68e4d0f53b9a382f358cccf8Tom Gundersen link->filename, udev_device_get_sysname(device));
ca6038b89645c0c1bd547d6a420bf95eb3d6f4ccTom Gundersen log_debug("Config file %s applies to device %s",
ca6038b89645c0c1bd547d6a420bf95eb3d6f4ccTom Gundersen link->filename, udev_device_get_sysname(device));
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersenstatic bool mac_is_random(struct udev_device *device) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen const char *s;
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen /* if we can't get the assign type, assume it is not random */
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen s = udev_device_get_sysattr_value(device, "addr_assign_type");
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen return false;
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen return false;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersenstatic bool should_rename(struct udev_device *device, bool respect_predictable) {
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen const char *s;
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen /* if we can't get the assgin type, assume we should rename */
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen s = udev_device_get_sysattr_value(device, "name_assign_type");
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen /* these were already named by userspace, do not touch again */
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen return false;
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen /* the kernel claims to have given a predictable name */
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen return false;
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen /* fall through */
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen /* the name is known to be bad, or of an unknown type */
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersenstatic int get_mac(struct udev_device *device, bool want_random,
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering random_bytes(mac->ether_addr_octet, ETH_ALEN);
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt r = net_get_unique_predictable_data(device, &result);
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen /* see eth_random_addr in the kernel */
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersenint link_config_apply(link_config_ctx *ctx, link_config *config,
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen struct udev_device *device, const char **name) {
dab495dc23bf9a5ba0487a057bb594355555a0e9Tom Gundersen r = ethtool_set_speed(&ctx->ethtool_fd, old_name, config->speed / 1024, config->duplex);
dab495dc23bf9a5ba0487a057bb594355555a0e9Tom Gundersen log_warning_errno(r, "Could not set speed or duplex of %s to %zu Mbps (%s): %m",
aedca89268ed4fd6be41e55a605f011033ad1fb5Tom Gundersen r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen if (ctx->enable_name_policy && config->name_policy) {
68ba38770640413b4fa06773447666eb88a38d4cTom Gundersen !new_name && *policy != _NAMEPOLICY_INVALID; policy++) {
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen new_name = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
04b67d49254d956d31bcfe80340fb9df7ed332d3Tom Gundersen if (should_rename(device, respect_predictable)) {
3c9b886068d99e5d3cbabcac32a4decf37244c54Tom Gundersen /* if not set by policy, fall back manually set name */
1c25683e0f40c6169676cc44fa1897082597feecTom Gundersen log_warning_errno(r, "Could not generate persistent MAC address for %s: %m", old_name);
1c25683e0f40c6169676cc44fa1897082597feecTom Gundersen } else if (r < 0)
1c25683e0f40c6169676cc44fa1897082597feecTom Gundersen log_warning_errno(r, "Could not generate random MAC address for %s: %m", old_name);
1c25683e0f40c6169676cc44fa1897082597feecTom Gundersen } else if (r < 0)
dab495dc23bf9a5ba0487a057bb594355555a0e9Tom Gundersen r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac, config->mtu);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_warning_errno(r, "Could not set Alias, MACAddress or MTU on %s: %m", old_name);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersenint link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret) {
aedca89268ed4fd6be41e55a605f011033ad1fb5Tom Gundersen r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mackstatic const char* const mac_policy_table[_MACPOLICY_MAX] = {
be32eb9b7fbcb22e4b648086d644135e38279633Tom GundersenDEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom GundersenDEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy,
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen "Failed to parse MAC address policy");
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mackstatic const char* const name_policy_table[_NAMEPOLICY_MAX] = {
be32eb9b7fbcb22e4b648086d644135e38279633Tom GundersenDEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom GundersenDEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
464cf22f17e0cf2d8bfa6d72b5e7a662d634f149Tom Gundersen "Failed to parse interface name policy");