]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/net/link-config.c
udev: do not fail if kernel does not support alternative names
[thirdparty/systemd.git] / src / udev / net / link-config.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
af6f0d42 2
01234e1f 3#include <linux/netdevice.h>
43b3a5ef
TG
4#include <netinet/ether.h>
5
e5eadf53 6#include "sd-device.h"
07630cea 7#include "sd-netlink.h"
af6f0d42 8
b5efdb8a 9#include "alloc-util.h"
07630cea
LP
10#include "conf-files.h"
11#include "conf-parser.h"
dc0d4078 12#include "def.h"
b220632c 13#include "device-util.h"
a5010333 14#include "ethtool-util.h"
3ffd4af2 15#include "fd-util.h"
07630cea 16#include "link-config.h"
af6f0d42 17#include "log.h"
0a970718 18#include "memory-util.h"
73d2bb08 19#include "naming-scheme.h"
1c4baffc 20#include "netlink-util.h"
c6f7c917 21#include "network-internal.h"
6bedfcbb 22#include "parse-util.h"
07630cea 23#include "path-util.h"
4e731273 24#include "proc-cmdline.h"
3df3e884 25#include "random-util.h"
8fcde012 26#include "stat-util.h"
8b43440b 27#include "string-table.h"
07630cea
LP
28#include "string-util.h"
29#include "strv.h"
af6f0d42
TG
30
31struct link_config_ctx {
32 LIST_HEAD(link_config, links);
33
a5010333
TG
34 int ethtool_fd;
35
f6194225
TG
36 bool enable_name_policy;
37
1c4baffc 38 sd_netlink *rtnl;
43b3a5ef 39
dc0d4078 40 usec_t network_dirs_ts_usec;
af6f0d42
TG
41};
42
9a4b012e
TG
43static void link_config_free(link_config *link) {
44 if (!link)
45 return;
5b9d4dc0 46
9a4b012e
TG
47 free(link->filename);
48
e90d0374 49 set_free_free(link->match_mac);
43d60b77
TG
50 strv_free(link->match_path);
51 strv_free(link->match_driver);
52 strv_free(link->match_type);
391f6bc1 53 strv_free(link->match_name);
44005bfb 54 strv_free(link->match_property);
c4f58dea 55 condition_free_list(link->conditions);
9a4b012e
TG
56
57 free(link->description);
58 free(link->mac);
59 free(link->name_policy);
60 free(link->name);
a5053a15 61 strv_free(link->alternative_names);
9a4b012e
TG
62 free(link->alias);
63
64 free(link);
af6f0d42
TG
65}
66
9a4b012e
TG
67DEFINE_TRIVIAL_CLEANUP_FUNC(link_config*, link_config_free);
68
af6f0d42
TG
69static void link_configs_free(link_config_ctx *ctx) {
70 link_config *link, *link_next;
71
72 if (!ctx)
73 return;
74
9a4b012e
TG
75 LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
76 link_config_free(link);
af6f0d42
TG
77}
78
79void link_config_ctx_free(link_config_ctx *ctx) {
80 if (!ctx)
81 return;
82
03e334a1 83 safe_close(ctx->ethtool_fd);
43b3a5ef 84
1c4baffc 85 sd_netlink_unref(ctx->rtnl);
43b3a5ef 86
af6f0d42
TG
87 link_configs_free(ctx);
88
89 free(ctx);
90
91 return;
92}
93
9a4b012e
TG
94int link_config_ctx_new(link_config_ctx **ret) {
95 _cleanup_(link_config_ctx_freep) link_config_ctx *ctx = NULL;
96
97 if (!ret)
98 return -EINVAL;
99
100 ctx = new0(link_config_ctx, 1);
101 if (!ctx)
102 return -ENOMEM;
103
104 LIST_HEAD_INIT(ctx->links);
105
106 ctx->ethtool_fd = -1;
107
108 ctx->enable_name_policy = true;
109
1cc6c93a 110 *ret = TAKE_PTR(ctx);
9a4b012e
TG
111
112 return 0;
113}
114
a378400b 115int link_load_one(link_config_ctx *ctx, const char *filename) {
9a4b012e 116 _cleanup_(link_config_freep) link_config *link = NULL;
6e37cd2f 117 _cleanup_fclose_ FILE *file = NULL;
6cdab9f1 118 _cleanup_free_ char *name = NULL;
79a60834 119 size_t i;
af6f0d42
TG
120 int r;
121
187dc6e5
TA
122 assert(ctx);
123 assert(filename);
124
af6f0d42 125 file = fopen(filename, "re");
f2d251cd
YW
126 if (!file)
127 return errno == ENOENT ? 0 : -errno;
af6f0d42 128
ed88bcfb
ZJS
129 if (null_or_empty_fd(fileno(file))) {
130 log_debug("Skipping empty file: %s", filename);
131 return 0;
132 }
133
6cdab9f1
YW
134 name = strdup(filename);
135 if (!name)
136 return -ENOMEM;
137
138 link = new(link_config, 1);
ecb08ec6 139 if (!link)
e8a42907 140 return -ENOMEM;
af6f0d42 141
6cdab9f1
YW
142 *link = (link_config) {
143 .filename = TAKE_PTR(name),
54ed9f88 144 .mac_address_policy = _MAC_ADDRESS_POLICY_INVALID,
6cdab9f1
YW
145 .wol = _WOL_INVALID,
146 .duplex = _DUP_INVALID,
147 .port = _NET_DEV_PORT_INVALID,
148 .autonegotiation = -1,
149 };
5fde13d7 150
79a60834 151 for (i = 0; i < ELEMENTSOF(link->features); i++)
cc2ff878 152 link->features[i] = -1;
50725d10 153
e9f3d2d5 154 r = config_parse(NULL, filename, file,
c6b3370a 155 "Match\0Link\0",
e9f3d2d5 156 config_item_perf_lookup, link_config_gperf_lookup,
bcde742e 157 CONFIG_PARSE_WARN, link);
36f822c4 158 if (r < 0)
ecb08ec6 159 return r;
af6f0d42 160
4e964aa0 161 if (link->speed > UINT_MAX)
dab495dc
TG
162 return -ERANGE;
163
84ea567e
YW
164 if (set_isempty(link->match_mac) && strv_isempty(link->match_path) &&
165 strv_isempty(link->match_driver) && strv_isempty(link->match_type) &&
44005bfb 166 strv_isempty(link->match_name) && strv_isempty(link->match_property) && !link->conditions)
84ea567e
YW
167 log_warning("%s: No valid settings found in the [Match] section. "
168 "The file will match all interfaces. "
169 "If that is intended, please add OriginalName=* in the [Match] section.",
170 filename);
171
c4f58dea 172 if (!condition_test_list(link->conditions, NULL, NULL, NULL)) {
176d9c0e
YW
173 log_debug("%s: Conditions do not match the system environment, skipping.", filename);
174 return 0;
175 }
176
6cdab9f1 177 log_debug("Parsed configuration file %s", filename);
af6f0d42 178
4b4a6c9b 179 LIST_PREPEND(links, ctx->links, TAKE_PTR(link));
af6f0d42 180 return 0;
af6f0d42
TG
181}
182
f6194225 183static bool enable_name_policy(void) {
1d84ad94 184 bool b;
f6194225 185
1d84ad94 186 return proc_cmdline_get_bool("net.ifnames", &b) <= 0 || b;
f6194225
TG
187}
188
015b097c 189static int link_unsigned_attribute(sd_device *device, const char *attr, unsigned *type) {
0b189e8f
ZJS
190 const char *s;
191 int r;
192
015b097c 193 r = sd_device_get_sysattr_value(device, attr, &s);
0b189e8f 194 if (r < 0)
015b097c 195 return log_device_debug_errno(device, r, "Failed to query %s: %m", attr);
0b189e8f
ZJS
196
197 r = safe_atou(s, type);
198 if (r < 0)
015b097c 199 return log_device_warning_errno(device, r, "Failed to parse %s \"%s\": %m", attr, s);
0b189e8f 200
015b097c 201 log_device_debug(device, "Device has %s=%u", attr, *type);
0b189e8f
ZJS
202 return 0;
203}
204
af6f0d42 205int link_config_load(link_config_ctx *ctx) {
edf029b7
TG
206 _cleanup_strv_free_ char **files;
207 char **f;
a39f92d3 208 int r;
af6f0d42
TG
209
210 link_configs_free(ctx);
211
f6194225
TG
212 if (!enable_name_policy()) {
213 ctx->enable_name_policy = false;
3f85ef0f 214 log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
f6194225
TG
215 }
216
97f2d76d 217 /* update timestamp */
dc0d4078 218 paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, true);
af6f0d42 219
dc0d4078 220 r = conf_files_list_strv(&files, ".link", NULL, 0, NETWORK_DIRS);
f647962d
MS
221 if (r < 0)
222 return log_error_errno(r, "failed to enumerate link files: %m");
af6f0d42
TG
223
224 STRV_FOREACH_BACKWARDS(f, files) {
a378400b 225 r = link_load_one(ctx, *f);
af6f0d42 226 if (r < 0)
e8a42907 227 log_error_errno(r, "Failed to load %s, ignoring: %m", *f);
af6f0d42
TG
228 }
229
230 return 0;
231}
232
233bool link_config_should_reload(link_config_ctx *ctx) {
dc0d4078 234 return paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, false);
af6f0d42
TG
235}
236
e5eadf53 237int link_config_get(link_config_ctx *ctx, sd_device *device, link_config **ret) {
af6f0d42
TG
238 link_config *link;
239
3b64e4d4
TG
240 assert(ctx);
241 assert(device);
242 assert(ret);
243
af6f0d42 244 LIST_FOREACH(links, link, ctx->links) {
edbb03e9 245 if (net_match_config(link->match_mac, link->match_path, link->match_driver,
78404d22 246 link->match_type, link->match_name, link->match_property, NULL, NULL, NULL,
572b21d9 247 device, NULL, NULL, NULL, 0, NULL, NULL)) {
56637e5c 248 if (link->match_name && !strv_contains(link->match_name, "*")) {
015b097c 249 unsigned name_assign_type = NET_NAME_UNKNOWN;
32bc8adc 250
015b097c 251 (void) link_unsigned_attribute(device, "name_assign_type", &name_assign_type);
32bc8adc 252
56637e5c 253 if (name_assign_type == NET_NAME_ENUM) {
b38de0e9
YW
254 log_device_warning(device, "Config file %s applies to device based on potentially unpredictable interface name",
255 link->filename);
32bc8adc
TG
256 *ret = link;
257
258 return 0;
259 } else if (name_assign_type == NET_NAME_RENAMED) {
b38de0e9
YW
260 log_device_warning(device, "Config file %s matches device based on renamed interface name, ignoring",
261 link->filename);
32bc8adc 262
ca6038b8 263 continue;
32bc8adc 264 }
ca6038b8 265 }
32bc8adc 266
b38de0e9 267 log_device_debug(device, "Config file %s is applied", link->filename);
32bc8adc 268
ca6038b8 269 *ret = link;
ca6038b8 270 return 0;
af6f0d42
TG
271 }
272 }
273
be32eb9b 274 *ret = NULL;
af6f0d42
TG
275 return -ENOENT;
276}
277
54ed9f88 278static int get_mac(sd_device *device, MACAddressPolicy policy, struct ether_addr *mac) {
015b097c 279 unsigned addr_type;
54ed9f88 280 bool want_random = policy == MAC_ADDRESS_POLICY_RANDOM;
f1ac7002 281 int r;
16b9b87a 282
54ed9f88 283 assert(IN_SET(policy, MAC_ADDRESS_POLICY_RANDOM, MAC_ADDRESS_POLICY_PERSISTENT));
3c9b8860 284
015b097c 285 r = link_unsigned_attribute(device, "addr_assign_type", &addr_type);
f1ac7002 286 if (r < 0)
015b097c
ZJS
287 return r;
288 switch (addr_type) {
289 case NET_ADDR_SET:
290 return log_device_debug(device, "MAC on the device already set by userspace");
291 case NET_ADDR_STOLEN:
292 return log_device_debug(device, "MAC on the device already set based on another device");
293 case NET_ADDR_RANDOM:
294 case NET_ADDR_PERM:
295 break;
296 default:
297 return log_device_warning(device, "Unknown addr_assign_type %u, ignoring", addr_type);
298 }
04b67d49 299
015b097c
ZJS
300 if (want_random == (addr_type == NET_ADDR_RANDOM))
301 return log_device_debug(device, "MAC on the device already matches policy *%s*",
54ed9f88 302 mac_address_policy_to_string(policy));
16b9b87a 303
015b097c
ZJS
304 if (want_random) {
305 log_device_debug(device, "Using random bytes to generate MAC");
9bf3b535 306 random_bytes(mac->ether_addr_octet, ETH_ALEN);
015b097c 307 } else {
dbe81cbd 308 uint64_t result;
9bf3b535 309
96848152
ZJS
310 r = net_get_unique_predictable_data(device,
311 naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS),
312 &result);
16b9b87a 313 if (r < 0)
015b097c 314 return log_device_warning_errno(device, r, "Could not generate persistent MAC: %m");
16b9b87a 315
96848152 316 log_device_debug(device, "Using generated persistent MAC address");
9bf3b535 317 assert_cc(ETH_ALEN <= sizeof(result));
dbe81cbd 318 memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
16b9b87a
TG
319 }
320
321 /* see eth_random_addr in the kernel */
3c9b8860
TG
322 mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
323 mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
015b097c 324 return 1;
16b9b87a
TG
325}
326
464cf22f 327int link_config_apply(link_config_ctx *ctx, link_config *config,
e5eadf53 328 sd_device *device, const char **name) {
5fde13d7 329 struct ether_addr generated_mac;
16b9b87a 330 struct ether_addr *mac = NULL;
a39f92d3
SS
331 const char *new_name = NULL;
332 const char *old_name;
0b189e8f
ZJS
333 unsigned speed, name_type = NET_NAME_UNKNOWN;
334 NamePolicy policy;
43b3a5ef 335 int r, ifindex;
af6f0d42 336
3e137a1b
TG
337 assert(ctx);
338 assert(config);
339 assert(device);
340 assert(name);
341
e5eadf53
YW
342 r = sd_device_get_sysname(device, &old_name);
343 if (r < 0)
344 return r;
af6f0d42 345
5c2316c6
YW
346 r = ethtool_set_glinksettings(&ctx->ethtool_fd, old_name,
347 config->autonegotiation, config->advertise,
348 config->speed, config->duplex, config->port);
a39f92d3
SS
349 if (r < 0) {
350
bb79318e 351 if (config->port != _NET_DEV_PORT_INVALID)
49c603bd 352 log_warning_errno(r, "Could not set port (%s) of %s: %m", port_to_string(config->port), old_name);
bb79318e 353
5dd10118 354 if (!eqzero(config->advertise))
2d18ac44 355 log_warning_errno(r, "Could not set advertise mode: %m"); /* TODO: include modes in the log message. */
a39f92d3 356
6cf0a204 357 if (config->speed) {
6cf0a204
SS
358 speed = DIV_ROUND_UP(config->speed, 1000000);
359 if (r == -EOPNOTSUPP) {
360 r = ethtool_set_speed(&ctx->ethtool_fd, old_name, speed, config->duplex);
361 if (r < 0)
362 log_warning_errno(r, "Could not set speed of %s to %u Mbps: %m", old_name, speed);
363 }
364 }
365
6eee8857
YW
366 if (config->duplex != _DUP_INVALID)
367 log_warning_errno(r, "Could not set duplex of %s to %s: %m", old_name, duplex_to_string(config->duplex));
a39f92d3 368 }
a5010333 369
aedca892 370 r = ethtool_set_wol(&ctx->ethtool_fd, old_name, config->wol);
5fde13d7 371 if (r < 0)
755bde37
LP
372 log_warning_errno(r, "Could not set WakeOnLan of %s to %s: %m",
373 old_name, wol_to_string(config->wol));
af6f0d42 374
50725d10
SS
375 r = ethtool_set_features(&ctx->ethtool_fd, old_name, config->features);
376 if (r < 0)
377 log_warning_errno(r, "Could not set offload features of %s: %m", old_name);
378
5f945202
SS
379 if (config->channels.rx_count_set || config->channels.tx_count_set || config->channels.other_count_set || config->channels.combined_count_set) {
380 r = ethtool_set_channels(&ctx->ethtool_fd, old_name, &config->channels);
381 if (r < 0)
382 log_warning_errno(r, "Could not set channels of %s: %m", old_name);
383 }
384
224ded67
SS
385 if (config->ring.rx_pending_set || config->ring.tx_pending_set) {
386 r = ethtool_set_nic_buffer_size(&ctx->ethtool_fd, old_name, &config->ring);
387 if (r < 0)
388 log_warning_errno(r, "Could not set ring buffer of %s: %m", old_name);
389 }
390
e5eadf53
YW
391 r = sd_device_get_ifindex(device, &ifindex);
392 if (r < 0)
b220632c 393 return log_device_warning_errno(device, r, "Could not find ifindex: %m");
43b3a5ef 394
015b097c 395 (void) link_unsigned_attribute(device, "name_assign_type", &name_type);
0b189e8f 396
73d2bb08
ZJS
397 if (IN_SET(name_type, NET_NAME_USER, NET_NAME_RENAMED)
398 && !naming_scheme_has(NAMING_ALLOW_RERENAMES)) {
399 log_device_debug(device, "Device already has a name given by userspace, not renaming.");
400 goto no_rename;
401 }
402
0b189e8f 403 if (ctx->enable_name_policy && config->name_policy)
78f8849f 404 for (NamePolicy *p = config->name_policy; *p != _NAMEPOLICY_INVALID; p++) {
0b189e8f
ZJS
405 policy = *p;
406
407 switch (policy) {
408 case NAMEPOLICY_KERNEL:
409 if (name_type != NET_NAME_PREDICTABLE)
410 continue;
411
412 /* The kernel claims to have given a predictable name, keep it. */
413 log_device_debug(device, "Policy *%s*: keeping predictable kernel name",
414 name_policy_to_string(policy));
415 goto no_rename;
3907446f
ZJS
416 case NAMEPOLICY_KEEP:
417 if (!IN_SET(name_type, NET_NAME_USER, NET_NAME_RENAMED))
418 continue;
419
420 log_device_debug(device, "Policy *%s*: keeping existing userspace name",
421 name_policy_to_string(policy));
422 goto no_rename;
0b189e8f
ZJS
423 case NAMEPOLICY_DATABASE:
424 (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &new_name);
425 break;
426 case NAMEPOLICY_ONBOARD:
427 (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &new_name);
428 break;
429 case NAMEPOLICY_SLOT:
430 (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &new_name);
431 break;
432 case NAMEPOLICY_PATH:
433 (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &new_name);
434 break;
435 case NAMEPOLICY_MAC:
436 (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &new_name);
437 break;
438 default:
439 assert_not_reached("invalid policy");
5fde13d7 440 }
78f8849f
YW
441 if (ifname_valid(new_name))
442 break;
daeb71a3 443 }
daeb71a3 444
0b189e8f
ZJS
445 if (new_name)
446 log_device_debug(device, "Policy *%s* yields \"%s\".", name_policy_to_string(policy), new_name);
447 else if (config->name) {
448 new_name = config->name;
449 log_device_debug(device, "Policies didn't yield a name, using specified Name=%s.", new_name);
ed308023 450 } else
0b189e8f
ZJS
451 log_device_debug(device, "Policies didn't yield a name and Name= is not given, not renaming.");
452 no_rename:
04b67d49 453
54ed9f88
ZJS
454 if (IN_SET(config->mac_address_policy, MAC_ADDRESS_POLICY_PERSISTENT, MAC_ADDRESS_POLICY_RANDOM)) {
455 if (get_mac(device, config->mac_address_policy, &generated_mac) > 0)
015b097c
ZJS
456 mac = &generated_mac;
457 } else
458 mac = config->mac;
16b9b87a 459
dab495dc 460 r = rtnl_set_link_properties(&ctx->rtnl, ifindex, config->alias, mac, config->mtu);
f647962d 461 if (r < 0)
7b72fe21 462 return log_warning_errno(r, "Could not set Alias=, MACAddress= or MTU= on %s: %m", old_name);
43b3a5ef 463
a5053a15 464 r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, config->alternative_names);
bb181dd4
YW
465 if (r == -EOPNOTSUPP)
466 log_debug_errno(r, "Could not set AlternativeName= on %s, ignoring: %m", old_name);
467 else if (r < 0)
a5053a15
YW
468 return log_warning_errno(r, "Could not set AlternativeName= on %s: %m", old_name);
469
d95b83b8
TG
470 *name = new_name;
471
af6f0d42
TG
472 return 0;
473}
be32eb9b 474
e5eadf53 475int link_get_driver(link_config_ctx *ctx, sd_device *device, char **ret) {
847a8a5f 476 const char *name;
a7f7d1bd 477 char *driver = NULL;
847a8a5f
TG
478 int r;
479
e5eadf53
YW
480 r = sd_device_get_sysname(device, &name);
481 if (r < 0)
482 return r;
847a8a5f 483
aedca892 484 r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
847a8a5f
TG
485 if (r < 0)
486 return r;
487
488 *ret = driver;
489 return 0;
490}
491
54ed9f88
ZJS
492static const char* const mac_address_policy_table[_MAC_ADDRESS_POLICY_MAX] = {
493 [MAC_ADDRESS_POLICY_PERSISTENT] = "persistent",
494 [MAC_ADDRESS_POLICY_RANDOM] = "random",
495 [MAC_ADDRESS_POLICY_NONE] = "none",
be32eb9b
TG
496};
497
54ed9f88
ZJS
498DEFINE_STRING_TABLE_LOOKUP(mac_address_policy, MACAddressPolicy);
499DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_address_policy, mac_address_policy, MACAddressPolicy,
464cf22f 500 "Failed to parse MAC address policy");
be32eb9b 501
2c5859af 502static const char* const name_policy_table[_NAMEPOLICY_MAX] = {
04b67d49 503 [NAMEPOLICY_KERNEL] = "kernel",
3907446f 504 [NAMEPOLICY_KEEP] = "keep",
e51660ae 505 [NAMEPOLICY_DATABASE] = "database",
be32eb9b
TG
506 [NAMEPOLICY_ONBOARD] = "onboard",
507 [NAMEPOLICY_SLOT] = "slot",
508 [NAMEPOLICY_PATH] = "path",
3907446f 509 [NAMEPOLICY_MAC] = "mac",
be32eb9b
TG
510};
511
512DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
464cf22f
TG
513DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
514 _NAMEPOLICY_INVALID,
515 "Failed to parse interface name policy");