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